2019-08-31 02:36:31 +03:00
|
|
|
--[[
|
|
|
|
|
|
|
|
TechAge
|
|
|
|
=======
|
|
|
|
|
2021-06-19 16:06:08 +03:00
|
|
|
Copyright (C) 2019-2021 DS-Minetest, Joachim Stolberg
|
2019-08-31 02:36:31 +03:00
|
|
|
|
2020-10-19 20:09:17 +03:00
|
|
|
AGPL v3
|
2019-08-31 02:36:31 +03:00
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
TA4 Power Wind Turbine Rotor
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2019-08-31 02:36:31 +03:00
|
|
|
Code by Joachim Stolberg, derived from DS-Minetest [1]
|
|
|
|
Rotor model and texture designed by DS-Minetest [1] (CC-0)
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2019-08-31 02:36:31 +03:00
|
|
|
[1] https://github.com/DS-Minetest/wind_turbine
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2019-08-31 02:36:31 +03:00
|
|
|
]]--
|
|
|
|
|
|
|
|
-- for lazy programmers
|
|
|
|
local M = minetest.get_meta
|
|
|
|
local S = techage.S
|
|
|
|
|
2021-06-19 16:06:08 +03:00
|
|
|
local STANDBY_TICKS = 4
|
2019-08-31 02:36:31 +03:00
|
|
|
local CYCLE_TIME = 2
|
2020-04-11 11:14:52 +03:00
|
|
|
local PWR_PERF = 70
|
2021-06-27 19:29:46 +03:00
|
|
|
local COUNTDOWN_TICKS = 2
|
2019-08-31 02:36:31 +03:00
|
|
|
|
|
|
|
local Cable = techage.ElectricCable
|
2021-06-19 16:06:08 +03:00
|
|
|
local power = networks.power
|
2022-01-03 23:40:31 +03:00
|
|
|
local control = networks.control
|
2019-08-31 02:36:31 +03:00
|
|
|
|
|
|
|
local Rotors = {}
|
|
|
|
|
|
|
|
local Face2Dir = {[0]=
|
|
|
|
{x=0, y=0, z=1},
|
|
|
|
{x=1, y=0, z=0},
|
|
|
|
{x=0, y=0, z=-1},
|
|
|
|
{x=-1, y=0, z=0},
|
|
|
|
{x=0, y=-1, z=0},
|
|
|
|
{x=0, y=1, z=0}
|
|
|
|
}
|
|
|
|
|
|
|
|
local function pos_and_yaw(pos, param2)
|
2021-05-21 18:12:58 +03:00
|
|
|
local dir = Face2Dir[param2] or Face2Dir[0]
|
2019-08-31 02:36:31 +03:00
|
|
|
local yaw = minetest.dir_to_yaw(dir)
|
2019-09-01 01:05:30 +03:00
|
|
|
dir = vector.multiply(dir, 1.1)
|
2019-08-31 02:36:31 +03:00
|
|
|
pos = vector.add(pos, dir)
|
|
|
|
return pos, {x=0, y=yaw, z=0}
|
|
|
|
end
|
|
|
|
|
2021-06-27 19:29:46 +03:00
|
|
|
local function is_windy()
|
2021-06-19 16:06:08 +03:00
|
|
|
local time = minetest.get_timeofday() or 0
|
|
|
|
return (time >= 5.00/24.00 and time <= 9.00/24.00) or (time >= 17.00/24.00 and time <= 21.00/24.00)
|
|
|
|
end
|
|
|
|
|
2021-03-14 18:00:45 +03:00
|
|
|
local function check_rotor(pos, nvm)
|
2021-07-12 22:55:40 +03:00
|
|
|
local resp, err = techage.valid_place_for_windturbine(pos, nil, 1)
|
|
|
|
if not resp then
|
|
|
|
nvm.error = err
|
|
|
|
return false
|
|
|
|
end
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2019-08-31 22:19:09 +03:00
|
|
|
local npos = techage.get_pos(pos, "F")
|
2020-02-24 22:59:26 +03:00
|
|
|
local node = techage.get_node_lvm(npos)
|
2019-08-31 22:19:09 +03:00
|
|
|
if node.name ~= "techage:ta4_wind_turbine_nacelle" then
|
2021-06-19 16:06:08 +03:00
|
|
|
nvm.error = S("Nacelle is missing")
|
2021-03-14 18:00:45 +03:00
|
|
|
return false
|
2019-08-31 22:19:09 +03:00
|
|
|
end
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2021-03-14 18:00:45 +03:00
|
|
|
local own_num = M(pos):get_string("node_number") or ""
|
2021-06-19 16:06:08 +03:00
|
|
|
M(pos):set_string("infotext", S("TA4 Wind Turbine")..": "..own_num)
|
2021-03-14 18:00:45 +03:00
|
|
|
nvm.error = false
|
|
|
|
return true
|
|
|
|
end
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2021-06-19 16:06:08 +03:00
|
|
|
local function formspec(self, pos, nvm)
|
|
|
|
return techage.generator_formspec(self, pos, nvm, S("TA4 Wind Turbine"), nvm.provided, PWR_PERF)
|
|
|
|
end
|
|
|
|
|
2021-06-27 19:29:46 +03:00
|
|
|
local function add_rotor(pos, nvm, force)
|
2021-07-18 17:25:03 +03:00
|
|
|
if (force and not nvm.error) or check_rotor(pos, nvm) then
|
2021-03-14 18:00:45 +03:00
|
|
|
local hash = minetest.hash_node_position(pos)
|
2021-07-23 20:21:46 +03:00
|
|
|
if Rotors[hash] then
|
|
|
|
Rotors[hash]:remove()
|
2021-03-14 18:00:45 +03:00
|
|
|
end
|
2021-07-23 20:21:46 +03:00
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
local npos, yaw = pos_and_yaw(pos, node.param2)
|
|
|
|
local obj = minetest.add_entity(npos, "techage:rotor_ent")
|
|
|
|
obj:set_animation({x = 0, y = 119}, 0, 0, true)
|
|
|
|
obj:set_rotation(yaw)
|
|
|
|
Rotors[hash] = obj
|
2021-03-14 18:00:45 +03:00
|
|
|
end
|
2022-01-03 23:40:31 +03:00
|
|
|
end
|
2021-03-14 18:00:45 +03:00
|
|
|
|
2021-06-19 16:06:08 +03:00
|
|
|
local function start_rotor(pos, nvm, state)
|
2021-03-14 18:00:45 +03:00
|
|
|
if not nvm.error then
|
2021-06-19 16:06:08 +03:00
|
|
|
local meta = M(pos)
|
2021-06-27 19:29:46 +03:00
|
|
|
nvm.provided = 0
|
2021-06-19 16:06:08 +03:00
|
|
|
techage.evaluate_charge_termination(nvm, meta)
|
|
|
|
power.start_storage_calc(pos, Cable, 5)
|
2021-03-14 18:00:45 +03:00
|
|
|
local hash = minetest.hash_node_position(pos)
|
2021-06-27 19:29:46 +03:00
|
|
|
if Rotors[hash] and is_windy() then
|
2021-03-14 18:00:45 +03:00
|
|
|
Rotors[hash]:set_animation_frame_speed(50)
|
|
|
|
end
|
2019-08-31 02:36:31 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-19 16:06:08 +03:00
|
|
|
local function stop_rotor(pos, nvm, state)
|
2021-06-27 19:29:46 +03:00
|
|
|
nvm.provided = 0
|
2021-06-19 16:06:08 +03:00
|
|
|
nvm.load = 0
|
|
|
|
power.start_storage_calc(pos, Cable, 5)
|
2019-08-31 02:36:31 +03:00
|
|
|
local hash = minetest.hash_node_position(pos)
|
|
|
|
if Rotors[hash] then
|
|
|
|
Rotors[hash]:set_animation_frame_speed(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-19 16:06:08 +03:00
|
|
|
local function can_start(pos, nvm)
|
|
|
|
if nvm.error then
|
|
|
|
return nvm.error
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local State = techage.NodeStates:new({
|
|
|
|
node_name_passive = "techage:ta4_wind_turbine",
|
|
|
|
cycle_time = CYCLE_TIME,
|
|
|
|
standby_ticks = STANDBY_TICKS,
|
|
|
|
formspec_func = formspec,
|
|
|
|
start_node = start_rotor,
|
|
|
|
stop_node = stop_rotor,
|
|
|
|
can_start = can_start,
|
|
|
|
})
|
|
|
|
|
2019-08-31 02:36:31 +03:00
|
|
|
local function node_timer(pos, elapsed)
|
2020-01-31 21:55:10 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
2021-06-27 19:29:46 +03:00
|
|
|
local running = techage.is_running(nvm)
|
|
|
|
local windy = is_windy()
|
|
|
|
if running and not windy then
|
|
|
|
State:standby(pos, nvm)
|
|
|
|
stop_rotor(pos, nvm, State)
|
|
|
|
elseif not running and windy then
|
|
|
|
State:start(pos, nvm)
|
|
|
|
-- start_node() is called implicit
|
|
|
|
elseif running then
|
|
|
|
local meta = M(pos)
|
2021-06-19 16:06:08 +03:00
|
|
|
local tp1 = tonumber(meta:get_string("termpoint1"))
|
|
|
|
local tp2 = tonumber(meta:get_string("termpoint2"))
|
|
|
|
nvm.provided = power.provide_power(pos, Cable, 5, PWR_PERF, tp1, tp2)
|
2021-06-27 19:29:46 +03:00
|
|
|
local val = power.get_storage_load(pos, Cable, 5, PWR_PERF)
|
|
|
|
if val > 0 then
|
|
|
|
nvm.load = val
|
|
|
|
end
|
|
|
|
State:keep_running(pos, nvm, COUNTDOWN_TICKS)
|
2021-06-19 16:06:08 +03:00
|
|
|
end
|
|
|
|
if techage.is_activeformspec(pos) then
|
2021-06-27 19:29:46 +03:00
|
|
|
M(pos):set_string("formspec", formspec(State, pos, nvm))
|
2019-08-31 02:36:31 +03:00
|
|
|
end
|
2021-06-27 19:29:46 +03:00
|
|
|
return State:is_active(nvm)
|
2019-08-31 02:36:31 +03:00
|
|
|
end
|
|
|
|
|
2021-06-19 16:06:08 +03:00
|
|
|
local function on_rightclick(pos, node, clicker)
|
|
|
|
techage.set_activeformspec(pos, clicker)
|
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
M(pos):set_string("formspec", formspec(State, pos, nvm))
|
|
|
|
end
|
|
|
|
|
|
|
|
local function on_receive_fields(pos, formname, fields, player)
|
|
|
|
if minetest.is_protected(pos, player:get_player_name()) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
State:state_button_event(pos, nvm, fields)
|
|
|
|
M(pos):set_string("formspec", formspec(State, pos, nvm))
|
|
|
|
end
|
|
|
|
|
2021-07-06 22:37:29 +03:00
|
|
|
local function get_generator_data(pos, outdir, tlib2)
|
2021-06-19 16:06:08 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
2021-06-27 19:29:46 +03:00
|
|
|
if techage.is_running(nvm) then
|
2021-06-19 16:06:08 +03:00
|
|
|
return {level = (nvm.load or 0) / PWR_PERF, perf = PWR_PERF, capa = PWR_PERF * 2}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-06 22:51:30 +03:00
|
|
|
local function after_place_node(pos, placer)
|
|
|
|
local meta = M(pos)
|
|
|
|
local nvm = techage.get_nvm(pos)
|
2021-06-19 16:06:08 +03:00
|
|
|
local number = techage.add_node(pos, "techage:ta4_wind_turbine")
|
|
|
|
State:node_init(pos, nvm, number)
|
2020-02-06 22:51:30 +03:00
|
|
|
meta:set_string("owner", placer:get_player_name())
|
2021-06-19 16:06:08 +03:00
|
|
|
M(pos):set_string("formspec", formspec(State, pos, nvm))
|
2021-03-14 18:00:45 +03:00
|
|
|
add_rotor(pos, nvm)
|
2020-02-06 22:51:30 +03:00
|
|
|
Cable:after_place_node(pos)
|
|
|
|
end
|
|
|
|
|
2020-06-14 00:32:00 +03:00
|
|
|
local function after_dig_node(pos, oldnode, oldmetadata)
|
2020-02-06 22:51:30 +03:00
|
|
|
local hash = minetest.hash_node_position(pos)
|
|
|
|
if Rotors[hash] and Rotors[hash]:get_luaentity() then
|
|
|
|
Rotors[hash]:remove()
|
|
|
|
end
|
|
|
|
Rotors[hash] = nil
|
|
|
|
Cable:after_dig_node(pos)
|
2020-06-14 00:32:00 +03:00
|
|
|
techage.remove_node(pos, oldnode, oldmetadata)
|
2020-02-06 22:51:30 +03:00
|
|
|
techage.del_mem(pos)
|
|
|
|
end
|
|
|
|
|
2019-08-31 02:36:31 +03:00
|
|
|
minetest.register_node("techage:ta4_wind_turbine", {
|
|
|
|
description = S("TA4 Wind Turbine"),
|
|
|
|
inventory_image = "techage_wind_turbine_inv.png",
|
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
|
|
|
"techage_rotor_top.png",
|
2019-08-31 22:32:36 +03:00
|
|
|
"techage_rotor_top.png^techage_appl_hole_electric.png",
|
|
|
|
"techage_rotor.png",
|
|
|
|
"techage_rotor.png",
|
2019-08-31 02:36:31 +03:00
|
|
|
"techage_rotor.png",
|
2019-08-31 22:32:36 +03:00
|
|
|
"techage_rotor.png^techage_appl_open.png",
|
2019-08-31 02:36:31 +03:00
|
|
|
},
|
2022-01-03 23:40:31 +03:00
|
|
|
|
2020-02-06 22:51:30 +03:00
|
|
|
after_place_node = after_place_node,
|
|
|
|
after_dig_node = after_dig_node,
|
2021-06-19 16:06:08 +03:00
|
|
|
get_generator_data = get_generator_data,
|
2019-08-31 02:36:31 +03:00
|
|
|
on_timer = node_timer,
|
2021-06-19 16:06:08 +03:00
|
|
|
on_rightclick = on_rightclick,
|
|
|
|
on_receive_fields = on_receive_fields,
|
2019-08-31 02:36:31 +03:00
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {cracky=2, crumbly=2, choppy=2},
|
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_metal_defaults(),
|
2021-06-19 16:06:08 +03:00
|
|
|
ta4_formspec = techage.generator_settings("ta4", PWR_PERF),
|
2019-08-31 02:36:31 +03:00
|
|
|
})
|
|
|
|
|
2021-06-19 16:06:08 +03:00
|
|
|
power.register_nodes({"techage:ta4_wind_turbine"}, Cable, "gen", {"D"})
|
|
|
|
|
|
|
|
control.register_nodes({"techage:ta4_wind_turbine"}, {
|
|
|
|
on_receive = function(pos, tlib2, topic, payload)
|
|
|
|
end,
|
|
|
|
on_request = function(pos, tlib2, topic)
|
|
|
|
if topic == "info" then
|
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
local meta = M(pos)
|
|
|
|
return {
|
|
|
|
type = S("TA4 Wind Turbine"),
|
|
|
|
number = meta:get_string("node_number") or "",
|
2021-06-27 19:29:46 +03:00
|
|
|
running = techage.is_running(nvm) or false,
|
2021-06-19 16:06:08 +03:00
|
|
|
available = PWR_PERF,
|
|
|
|
provided = nvm.provided or 0,
|
2022-01-03 23:40:31 +03:00
|
|
|
termpoint = meta:get_string("termpoint"),
|
2021-06-19 16:06:08 +03:00
|
|
|
}
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
)
|
2019-08-31 02:36:31 +03:00
|
|
|
|
|
|
|
minetest.register_node("techage:ta4_wind_turbine_nacelle", {
|
|
|
|
description = S("TA4 Wind Turbine Nacelle"),
|
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
|
|
|
"techage_rotor_top.png",
|
|
|
|
"techage_rotor_top.png",
|
|
|
|
"techage_rotor.png",
|
2019-08-31 22:32:36 +03:00
|
|
|
"techage_rotor.png",
|
|
|
|
"techage_rotor.png^techage_appl_open.png",
|
|
|
|
"techage_rotor.png",
|
2019-08-31 02:36:31 +03:00
|
|
|
},
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {cracky=2, crumbly=2, choppy=2},
|
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_metal_defaults(),
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_entity("techage:rotor_ent", {initial_properties = {
|
|
|
|
physical = false,
|
|
|
|
pointable = false,
|
|
|
|
visual = "mesh",
|
|
|
|
visual_size = {x = 1.5, y = 1.5, z = 1.5},
|
|
|
|
mesh = "techage_rotor.b3d",
|
|
|
|
textures = {"techage_rotor_blades.png"},
|
|
|
|
static_save = false,
|
|
|
|
}})
|
|
|
|
|
2022-01-03 23:40:31 +03:00
|
|
|
techage.register_node({"techage:ta4_wind_turbine"}, {
|
2019-08-31 02:36:31 +03:00
|
|
|
on_recv_message = function(pos, src, topic, payload)
|
2020-01-31 21:55:10 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
2019-08-31 02:36:31 +03:00
|
|
|
if topic == "state" then
|
2019-09-14 00:01:55 +03:00
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
if node.name == "ignore" then -- unloaded node?
|
|
|
|
return "unloaded"
|
|
|
|
end
|
2020-01-31 21:55:10 +03:00
|
|
|
if nvm.error then
|
2019-08-31 22:19:09 +03:00
|
|
|
return "error"
|
2021-06-27 19:29:46 +03:00
|
|
|
elseif techage.is_running(nvm) then
|
2019-08-31 02:36:31 +03:00
|
|
|
return "running"
|
|
|
|
else
|
|
|
|
return "stopped"
|
|
|
|
end
|
2020-03-02 23:51:18 +03:00
|
|
|
elseif topic == "delivered" then
|
2020-02-28 23:27:52 +03:00
|
|
|
return nvm.delivered or 0
|
2019-08-31 02:36:31 +03:00
|
|
|
elseif topic == "on" then
|
2021-06-27 19:29:46 +03:00
|
|
|
State:start(pos, nvm)
|
2019-08-31 02:36:31 +03:00
|
|
|
elseif topic == "off" then
|
2021-06-27 19:29:46 +03:00
|
|
|
State:stop(pos, nvm)
|
2019-08-31 02:36:31 +03:00
|
|
|
else
|
|
|
|
return "unsupported"
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
on_node_load = function(pos)
|
2020-01-31 21:55:10 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
2021-06-27 19:29:46 +03:00
|
|
|
add_rotor(pos, nvm, true)
|
2019-08-31 02:36:31 +03:00
|
|
|
minetest.get_node_timer(pos):start(CYCLE_TIME)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2019-08-31 22:19:09 +03:00
|
|
|
minetest.register_craftitem("techage:ta4_carbon_fiber", {
|
|
|
|
description = S("TA4 Carbon Fiber"),
|
|
|
|
inventory_image = "techage_carbon_fiber.png",
|
|
|
|
})
|
2019-08-31 02:36:31 +03:00
|
|
|
|
2019-08-31 22:19:09 +03:00
|
|
|
minetest.register_craftitem("techage:ta4_rotor_blade", {
|
|
|
|
description = S("TA4 Rotor Blade"),
|
|
|
|
inventory_image = "techage_rotor_blade.png",
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
output = "techage:ta4_wind_turbine",
|
|
|
|
recipe = {
|
|
|
|
{"dye:white", "techage:ta4_rotor_blade", "dye:red"},
|
|
|
|
{"basic_materials:gear_steel", "techage:generator", "basic_materials:gear_steel"},
|
|
|
|
{"techage:ta4_rotor_blade", "techage:electric_cableS", "techage:ta4_rotor_blade"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
output = "techage:ta4_wind_turbine_nacelle",
|
|
|
|
recipe = {
|
|
|
|
{"default:steel_ingot", "", "default:steel_ingot"},
|
|
|
|
{"dye:white", "techage:ta4_wlanchip", "dye:red"},
|
|
|
|
{"", "default:copper_ingot", ""},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
output = "techage:ta4_rotor_blade",
|
|
|
|
recipe = {
|
|
|
|
{"techage:ta4_carbon_fiber", "dye:white", "techage:ta4_carbon_fiber"},
|
2019-11-17 02:08:10 +03:00
|
|
|
{"techage:canister_epoxy", "techage:ta4_carbon_fiber", "techage:canister_epoxy"},
|
2019-08-31 22:19:09 +03:00
|
|
|
{"techage:ta4_carbon_fiber", "dye:red", "techage:ta4_carbon_fiber"},
|
|
|
|
},
|
2019-11-17 02:08:10 +03:00
|
|
|
replacements = {
|
|
|
|
{"techage:canister_epoxy", "techage:ta3_canister_empty"},
|
|
|
|
{"techage:canister_epoxy", "techage:ta3_canister_empty"},
|
|
|
|
},
|
2019-08-31 22:19:09 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
techage.furnace.register_recipe({
|
2022-01-03 23:40:31 +03:00
|
|
|
output = "techage:ta4_carbon_fiber",
|
|
|
|
recipe = {"default:papyrus", "default:stick", "default:papyrus", "default:stick"},
|
2019-08-31 22:19:09 +03:00
|
|
|
heat = 4,
|
|
|
|
time = 3,
|
2022-01-04 21:40:27 +03:00
|
|
|
})
|