2019-04-27 18:42:27 +03:00
|
|
|
--[[
|
|
|
|
|
|
|
|
TechAge
|
|
|
|
=======
|
|
|
|
|
2021-06-07 23:00:52 +03:00
|
|
|
Copyright (C) 2019-2021 Joachim Stolberg
|
2019-04-27 18:42:27 +03:00
|
|
|
|
2020-10-19 20:09:17 +03:00
|
|
|
AGPL v3
|
2019-04-27 18:42:27 +03:00
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
2019-05-05 23:29:00 +03:00
|
|
|
TA3 Power Station Generator
|
2019-04-27 18:42:27 +03:00
|
|
|
|
|
|
|
]]--
|
|
|
|
|
|
|
|
-- for lazy programmers
|
|
|
|
local M = minetest.get_meta
|
2019-07-02 22:33:12 +03:00
|
|
|
local S = techage.S
|
2019-04-27 18:42:27 +03:00
|
|
|
|
2019-05-05 23:29:00 +03:00
|
|
|
local Cable = techage.ElectricCable
|
2021-06-07 23:00:52 +03:00
|
|
|
local power = networks.power
|
|
|
|
local control = networks.control
|
2019-04-27 18:42:27 +03:00
|
|
|
|
2021-06-27 19:29:46 +03:00
|
|
|
local CYCLE_TIME = 2
|
|
|
|
local STANDBY_TICKS = 4
|
|
|
|
local COUNTDOWN_TICKS = 4
|
|
|
|
local PWR_PERF = 80
|
|
|
|
|
2020-01-26 01:15:44 +03:00
|
|
|
local function formspec(self, pos, nvm)
|
2021-06-27 19:29:46 +03:00
|
|
|
return techage.generator_formspec(self, pos, nvm, S("Generator"), nvm.provided, PWR_PERF)
|
2019-04-27 18:42:27 +03:00
|
|
|
end
|
|
|
|
|
2020-01-26 01:15:44 +03:00
|
|
|
local function transfer_turbine(pos, topic, payload)
|
|
|
|
return techage.transfer(pos, "L", topic, payload, nil,
|
|
|
|
{"techage:turbine", "techage:turbine_on"})
|
2019-05-05 23:29:00 +03:00
|
|
|
end
|
|
|
|
|
2020-01-26 01:15:44 +03:00
|
|
|
local function can_start(pos, nvm, state)
|
|
|
|
return (nvm.firebox_trigger or 0) > 0 -- by means of firebox
|
2019-04-27 18:42:27 +03:00
|
|
|
end
|
|
|
|
|
2021-06-27 19:29:46 +03:00
|
|
|
local function has_fire(nvm)
|
|
|
|
nvm.firebox_trigger = (nvm.firebox_trigger or 0) - 1
|
|
|
|
return nvm.firebox_trigger > 0
|
|
|
|
end
|
|
|
|
|
2020-01-26 01:15:44 +03:00
|
|
|
local function start_node(pos, nvm, state)
|
2021-06-27 19:29:46 +03:00
|
|
|
local meta = M(pos)
|
|
|
|
nvm.provided = 0
|
|
|
|
local outdir = meta:get_int("outdir")
|
2020-01-26 01:15:44 +03:00
|
|
|
transfer_turbine(pos, "start")
|
2021-06-09 23:09:59 +03:00
|
|
|
power.start_storage_calc(pos, Cable, outdir)
|
2021-06-27 19:29:46 +03:00
|
|
|
techage.evaluate_charge_termination(nvm, meta)
|
2020-01-26 01:15:44 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
local function stop_node(pos, nvm, state)
|
|
|
|
nvm.provided = 0
|
2021-06-27 19:29:46 +03:00
|
|
|
local outdir = M(pos):get_int("outdir")
|
2020-01-26 01:15:44 +03:00
|
|
|
transfer_turbine(pos, "stop")
|
2021-06-09 23:09:59 +03:00
|
|
|
power.start_storage_calc(pos, Cable, outdir)
|
2019-04-27 18:42:27 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
local State = techage.NodeStates:new({
|
2019-05-05 23:29:00 +03:00
|
|
|
node_name_passive = "techage:generator",
|
|
|
|
node_name_active = "techage:generator_on",
|
2019-04-27 18:42:27 +03:00
|
|
|
cycle_time = CYCLE_TIME,
|
|
|
|
standby_ticks = STANDBY_TICKS,
|
|
|
|
formspec_func = formspec,
|
2019-07-02 22:33:12 +03:00
|
|
|
infotext_name = S("TA3 Generator"),
|
2019-06-16 22:06:16 +03:00
|
|
|
can_start = can_start,
|
2019-04-27 18:42:27 +03:00
|
|
|
start_node = start_node,
|
|
|
|
stop_node = stop_node,
|
|
|
|
})
|
|
|
|
|
|
|
|
local function node_timer(pos, elapsed)
|
2020-01-26 01:15:44 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
2021-06-27 19:29:46 +03:00
|
|
|
local running = techage.is_running(nvm)
|
|
|
|
local fire = has_fire(nvm)
|
|
|
|
if running and not fire then
|
|
|
|
State:standby(pos, nvm)
|
2020-01-26 01:15:44 +03:00
|
|
|
stop_node(pos, nvm, State)
|
2021-06-27 19:29:46 +03:00
|
|
|
elseif not running and fire then
|
|
|
|
State:start(pos, nvm)
|
|
|
|
-- start_node() is called implicit
|
|
|
|
elseif running then
|
|
|
|
local meta = M(pos)
|
2021-06-09 23:09:59 +03:00
|
|
|
local outdir = meta:get_int("outdir")
|
|
|
|
local tp1 = tonumber(meta:get_string("termpoint1"))
|
|
|
|
local tp2 = tonumber(meta:get_string("termpoint2"))
|
2021-06-27 19:29:46 +03:00
|
|
|
nvm.provided = power.provide_power(pos, Cable, outdir, PWR_PERF, tp1, tp2)
|
|
|
|
local val = power.get_storage_load(pos, Cable, outdir, PWR_PERF)
|
|
|
|
if val > 0 then
|
|
|
|
nvm.load = val
|
|
|
|
end
|
2020-01-26 01:15:44 +03:00
|
|
|
State:keep_running(pos, nvm, COUNTDOWN_TICKS)
|
|
|
|
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-04-27 18:42:27 +03:00
|
|
|
end
|
2020-01-26 01:15:44 +03:00
|
|
|
return State:is_active(nvm)
|
2019-04-27 18:42:27 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
local function on_receive_fields(pos, formname, fields, player)
|
|
|
|
if minetest.is_protected(pos, player:get_player_name()) then
|
|
|
|
return
|
|
|
|
end
|
2020-02-06 22:51:30 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
2020-01-26 01:15:44 +03:00
|
|
|
State:state_button_event(pos, nvm, fields)
|
2019-04-27 18:42:27 +03:00
|
|
|
end
|
|
|
|
|
2020-01-26 01:15:44 +03:00
|
|
|
local function on_rightclick(pos, node, clicker)
|
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
techage.set_activeformspec(pos, clicker)
|
|
|
|
M(pos):set_string("formspec", formspec(State, pos, nvm))
|
2019-04-27 18:42:27 +03:00
|
|
|
end
|
|
|
|
|
2020-01-26 01:15:44 +03:00
|
|
|
local function after_place_node(pos)
|
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
local number = techage.add_node(pos, "techage:generator")
|
|
|
|
State:node_init(pos, nvm, number)
|
|
|
|
M(pos):set_int("outdir", networks.side_to_outdir(pos, "R"))
|
|
|
|
M(pos):set_string("formspec", formspec(State, pos, nvm))
|
|
|
|
Cable:after_place_node(pos)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function after_dig_node(pos, oldnode)
|
|
|
|
Cable:after_dig_node(pos)
|
|
|
|
techage.del_mem(pos)
|
|
|
|
end
|
|
|
|
|
2021-07-06 22:37:29 +03:00
|
|
|
local function get_generator_data(pos, outdir, tlib2)
|
2021-06-07 23:00:52 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
2021-06-27 19:29:46 +03:00
|
|
|
if techage.is_running(nvm) then
|
|
|
|
return {level = (nvm.load or 0) / PWR_PERF, perf = PWR_PERF, capa = PWR_PERF * 2}
|
2021-06-07 23:00:52 +03:00
|
|
|
end
|
2020-01-26 01:15:44 +03:00
|
|
|
end
|
|
|
|
|
2019-05-05 23:29:00 +03:00
|
|
|
minetest.register_node("techage:generator", {
|
2019-07-02 22:33:12 +03:00
|
|
|
description = S("TA3 Generator"),
|
2019-04-27 18:42:27 +03:00
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
2019-05-05 23:29:00 +03:00
|
|
|
"techage_filling_ta3.png^techage_frame_ta3.png",
|
|
|
|
"techage_filling_ta3.png^techage_frame_ta3.png",
|
|
|
|
"techage_filling_ta3.png^techage_appl_hole_electric.png^techage_frame_ta3.png",
|
|
|
|
"techage_filling_ta3.png^techage_appl_open.png^techage_frame_ta3.png",
|
|
|
|
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_generator.png",
|
|
|
|
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_generator.png^[transformFX]",
|
2019-04-27 18:42:27 +03:00
|
|
|
},
|
2020-01-26 01:15:44 +03:00
|
|
|
|
2019-04-27 18:42:27 +03:00
|
|
|
on_receive_fields = on_receive_fields,
|
|
|
|
on_rightclick = on_rightclick,
|
|
|
|
on_timer = node_timer,
|
2020-01-26 01:15:44 +03:00
|
|
|
after_place_node = after_place_node,
|
|
|
|
after_dig_node = after_dig_node,
|
2021-06-07 23:00:52 +03:00
|
|
|
get_generator_data = get_generator_data,
|
2021-06-27 19:29:46 +03:00
|
|
|
ta3_formspec = techage.generator_settings("ta3", PWR_PERF),
|
2020-01-26 01:15:44 +03:00
|
|
|
|
2019-04-27 18:42:27 +03:00
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {cracky=2, crumbly=2, choppy=2},
|
|
|
|
on_rotate = screwdriver.disallow,
|
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_wood_defaults(),
|
|
|
|
})
|
|
|
|
|
2019-05-05 23:29:00 +03:00
|
|
|
minetest.register_node("techage:generator_on", {
|
2019-07-02 22:33:12 +03:00
|
|
|
description = S("TA3 Generator"),
|
2019-04-27 18:42:27 +03:00
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
2019-05-06 22:09:13 +03:00
|
|
|
"techage_filling_ta3.png^techage_frame_ta3.png",
|
|
|
|
"techage_filling_ta3.png^techage_frame_ta3.png",
|
|
|
|
"techage_filling_ta3.png^techage_appl_hole_electric.png^techage_frame_ta3.png",
|
|
|
|
"techage_filling_ta3.png^techage_appl_open.png^techage_frame_ta3.png",
|
2019-04-27 18:42:27 +03:00
|
|
|
{
|
2019-05-06 22:09:13 +03:00
|
|
|
image = "techage_filling4_ta3.png^techage_appl_generator4.png^techage_frame4_ta3.png",
|
2019-04-27 18:42:27 +03:00
|
|
|
backface_culling = false,
|
|
|
|
animation = {
|
|
|
|
type = "vertical_frames",
|
|
|
|
aspect_w = 32,
|
|
|
|
aspect_h = 32,
|
2019-05-06 22:09:13 +03:00
|
|
|
length = 0.3,
|
2019-04-27 18:42:27 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-06-17 22:24:48 +03:00
|
|
|
image = "techage_filling4_ta3.png^techage_appl_generator4.png^[transformFX]^techage_frame4_ta3.png",
|
2019-04-27 18:42:27 +03:00
|
|
|
backface_culling = false,
|
|
|
|
animation = {
|
|
|
|
type = "vertical_frames",
|
|
|
|
aspect_w = 32,
|
|
|
|
aspect_h = 32,
|
2019-05-06 22:09:13 +03:00
|
|
|
length = 0.3,
|
2019-04-27 18:42:27 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
on_receive_fields = on_receive_fields,
|
|
|
|
on_rightclick = on_rightclick,
|
|
|
|
on_timer = node_timer,
|
2020-01-26 01:15:44 +03:00
|
|
|
after_place_node = after_place_node,
|
|
|
|
after_dig_node = after_dig_node,
|
2021-06-07 23:00:52 +03:00
|
|
|
get_generator_data = get_generator_data,
|
2021-06-27 19:29:46 +03:00
|
|
|
ta3_formspec = techage.generator_settings("ta3", PWR_PERF),
|
2020-01-26 01:15:44 +03:00
|
|
|
|
2019-05-21 17:37:05 +03:00
|
|
|
drop = "",
|
2019-04-27 18:42:27 +03:00
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {not_in_creative_inventory=1},
|
|
|
|
diggable = false,
|
|
|
|
on_rotate = screwdriver.disallow,
|
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_wood_defaults(),
|
|
|
|
})
|
2019-05-05 23:29:00 +03:00
|
|
|
|
2021-06-07 23:00:52 +03:00
|
|
|
power.register_nodes({"techage:generator", "techage:generator_on"}, Cable, "gen", {"R"})
|
2019-05-05 23:29:00 +03:00
|
|
|
|
2020-02-06 22:51:30 +03:00
|
|
|
-- controlled by the turbine
|
2019-06-08 23:57:01 +03:00
|
|
|
techage.register_node({"techage:generator", "techage:generator_on"}, {
|
|
|
|
on_transfer = function(pos, in_dir, topic, payload)
|
2020-01-26 01:15:44 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
|
|
|
if topic == "trigger" then
|
|
|
|
nvm.firebox_trigger = 3
|
2021-06-27 19:29:46 +03:00
|
|
|
if techage.is_running(nvm) then
|
|
|
|
return math.max((nvm.provided or PWR_PERF) / PWR_PERF, 0.02)
|
2019-06-16 22:06:16 +03:00
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
2019-06-08 23:57:01 +03:00
|
|
|
end
|
2019-06-29 00:46:44 +03:00
|
|
|
end,
|
2019-08-25 19:08:30 +03:00
|
|
|
on_recv_message = function(pos, src, topic, payload)
|
2020-01-31 21:55:10 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
2020-03-02 23:51:18 +03:00
|
|
|
if topic == "delivered" then
|
2020-02-28 00:33:42 +03:00
|
|
|
return math.floor((nvm.provided or 0) + 0.5)
|
2019-06-29 00:46:44 +03:00
|
|
|
else
|
|
|
|
return State:on_receive_message(pos, topic, payload)
|
|
|
|
end
|
|
|
|
end,
|
2019-06-08 23:57:01 +03:00
|
|
|
})
|
|
|
|
|
2021-06-07 23:00:52 +03:00
|
|
|
-- used by power terminal
|
|
|
|
control.register_nodes({"techage:generator", "techage:generator_on"}, {
|
|
|
|
on_receive = function(pos, tlib2, topic, payload)
|
|
|
|
end,
|
|
|
|
on_request = function(pos, tlib2, topic)
|
|
|
|
if topic == "info" then
|
|
|
|
local nvm = techage.get_nvm(pos)
|
2021-06-09 23:09:59 +03:00
|
|
|
local meta = M(pos)
|
2021-06-07 23:00:52 +03:00
|
|
|
return {
|
|
|
|
type = S("TA3 Generator"),
|
2021-06-09 23:09:59 +03:00
|
|
|
number = meta:get_string("node_number") or "",
|
2021-06-27 19:29:46 +03:00
|
|
|
running = techage.is_running(nvm) or false,
|
|
|
|
available = PWR_PERF,
|
2021-06-07 23:00:52 +03:00
|
|
|
provided = nvm.provided or 0,
|
2021-06-09 23:09:59 +03:00
|
|
|
termpoint = meta:get_string("termpoint"),
|
2021-06-07 23:00:52 +03:00
|
|
|
}
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-01-26 01:15:44 +03:00
|
|
|
minetest.register_craft({
|
|
|
|
output = "techage:generator",
|
|
|
|
recipe = {
|
|
|
|
{"basic_materials:steel_bar", "dye:green", "default:wood"},
|
|
|
|
{"", "basic_materials:gear_steel", "techage:electric_cableS"},
|
|
|
|
{"default:wood", "techage:iron_ingot", "basic_materials:steel_bar"},
|
|
|
|
},
|
|
|
|
})
|