techage/fusion_reactor/controller.lua

289 lines
8.3 KiB
Lua
Raw Normal View History

2022-01-08 00:35:12 +03:00
--[[
TechAge
=======
Copyright (C) 2019-2022 Joachim Stolberg
AGPL v3
See LICENSE.txt for more information
TA5 Fusion Reactor Controller
]]--
-- for lazy programmers
local S2P = minetest.string_to_pos
local P2S = minetest.pos_to_string
local M = minetest.get_meta
local S = techage.S
local Cable = techage.ElectricCable
2022-01-18 23:38:57 +03:00
local sched = techage.scheduler
2022-01-08 00:35:12 +03:00
local power = networks.power
local control = networks.control
2022-01-10 23:39:36 +03:00
local CYCLE_TIME = 2
local STANDBY_TICKS = 1
2022-01-18 23:38:57 +03:00
local COUNTDOWN_TICKS = 1
local PWR_NEEDED = 400
local EXPECTED_PLASMA_NUM = 56
local EXPECTED_SHELL_NUM = 56
local EXPECTED_MAGNET_NUM = 56
local CALL_RATE1 = 16 -- 2s * 16 = 32s
local CALL_RATE2 = 8 -- 2s * 8 = 16s
local DESCRIPTION = S("TA5 Fusion Reactor Controller")
2022-01-21 21:12:22 +03:00
local EX_POINTS = 60
2022-01-10 23:39:36 +03:00
2022-01-18 23:38:57 +03:00
local function count_trues(t)
local cnt = 0
2022-01-10 23:39:36 +03:00
for _,v in ipairs(t) do
if v then
2022-01-18 23:38:57 +03:00
cnt = cnt + 1
2022-01-10 23:39:36 +03:00
end
end
2022-01-18 23:38:57 +03:00
return cnt
2022-01-10 23:39:36 +03:00
end
local function nucleus(t)
2022-01-30 13:31:58 +03:00
t = techage.tbl_filter(t, function(v, k, t) return type(v) == "table" end)
2022-01-10 23:39:36 +03:00
if #t == 4 then
if vector.equals(t[1], t[2]) and vector.equals(t[3], t[4]) then
2022-01-18 23:38:57 +03:00
return true
2022-01-10 23:39:36 +03:00
end
end
2022-01-18 23:38:57 +03:00
return S("Nucleus detection error")
2022-01-10 23:39:36 +03:00
end
2022-01-18 23:38:57 +03:00
local tSched = {}
sched.register(tSched, CALL_RATE1, 0, function(pos, outdir)
local resp = control.request(pos, Cable, outdir, "con", "connect")
local cnt = count_trues(resp)
if cnt ~= EXPECTED_MAGNET_NUM then
return S("Magnet detection error\n(@1% found / 100% expected)", math.floor(cnt* 100 / EXPECTED_MAGNET_NUM))
end
return true
end)
sched.register(tSched, CALL_RATE1, 1, function(pos, outdir)
2022-01-10 23:39:36 +03:00
local resp = control.request(pos, Cable, outdir, "con", "test_plasma")
2022-01-18 23:38:57 +03:00
local cnt = count_trues(resp)
if cnt ~= EXPECTED_PLASMA_NUM then
return S("Plasma ring shape error")
2022-01-18 23:38:57 +03:00
end
return true
end)
sched.register(tSched, CALL_RATE1, 2, function(pos, outdir)
2022-01-10 23:39:36 +03:00
local resp = control.request(pos, Cable, outdir, "con", "test_shell")
2022-01-18 23:38:57 +03:00
local cnt = count_trues(resp)
if cnt ~= EXPECTED_SHELL_NUM then
return S("Shell shape error\n(@1% found / 100% expected)", math.floor(cnt* 100 / EXPECTED_SHELL_NUM))
end
return true
end)
sched.register(tSched, CALL_RATE1, 3, function(pos, outdir)
2022-01-10 23:39:36 +03:00
local resp = control.request(pos, Cable, outdir, "con", "test_nucleus")
2022-01-18 23:38:57 +03:00
return nucleus(resp)
end)
sched.register(tSched, CALL_RATE2, 4, function(pos, outdir)
local resp = control.request(pos, Cable, outdir, "con", "inc_power")
local cnt = count_trues(resp)
--print("inc_power", cnt)
2022-01-18 23:38:57 +03:00
if cnt < 52 then
return S("Cooling failed")
end
return true
end)
local function can_start(pos, nvm)
local outdir = networks.side_to_outdir(pos, "L")
if not power.power_available(pos, Cable, outdir) then
return S("No power")
end
outdir = networks.side_to_outdir(pos, "R")
control.request(pos, Cable, outdir, "con", "rst_power")
for i = 0,4 do
local res = tSched[i](pos, outdir)
if res ~= true then return res end
end
return true
end
local function start_node(pos, nvm)
sched.init(pos)
local outdir = networks.side_to_outdir(pos, "R")
control.send(pos, Cable, outdir, "con", "on")
sched.init(pos)
end
local function stop_node(pos, nvm)
local outdir = networks.side_to_outdir(pos, "R")
control.send(pos, Cable, outdir, "con", "off")
end
local function formspec(self, pos, nvm)
return "size[5,3]"..
"box[0,-0.1;4.8,0.5;#c6e8ff]" ..
"label[0.2,-0.1;" .. minetest.colorize( "#000000", DESCRIPTION) .. "]" ..
"image_button[2,1.5;1,1;".. self:get_state_button_image(nvm) ..";state_button;]"..
"tooltip[2,1.5;1,1;"..self:get_state_tooltip(nvm).."]"
end
local State = techage.NodeStates:new({
node_name_passive = "techage:ta5_fr_controller_pas",
node_name_active = "techage:ta5_fr_controller_act",
cycle_time = CYCLE_TIME,
infotext_name = DESCRIPTION,
standby_ticks = STANDBY_TICKS,
can_start = can_start,
start_node = start_node,
stop_node = stop_node,
formspec_func = formspec,
})
2022-01-10 23:39:36 +03:00
local function after_place_node(pos, placer, itemstack)
local nvm = techage.get_nvm(pos)
local meta = M(pos)
local own_num = techage.add_node(pos, "techage:ta5_fr_controller_pas")
2022-01-18 23:38:57 +03:00
State:node_init(pos, nvm, own_num)
2022-01-10 23:39:36 +03:00
meta:set_string("owner", placer:get_player_name())
Cable:after_place_node(pos)
end
2022-01-18 23:38:57 +03:00
local function consume_power(pos, nvm, outdir)
if techage.needs_power(nvm) then
local taken = power.consume_power(pos, Cable, outdir, PWR_NEEDED)
if techage.is_running(nvm) then
if taken < PWR_NEEDED then
State:nopower(pos, nvm, "No power")
stop_node(pos, nvm)
else
return true -- keep running
end
end
end
end
2022-01-10 23:39:36 +03:00
local function node_timer(pos)
local nvm = techage.get_nvm(pos)
local outdir = networks.side_to_outdir(pos, "L")
2022-01-18 23:38:57 +03:00
if consume_power(pos, nvm, outdir) then
2022-01-21 20:55:32 +03:00
local resp = sched.get(pos, tSched, function()
2022-01-18 23:38:57 +03:00
return true end)(pos, networks.Flip[outdir])
if resp ~= true then
State:fault(pos, nvm, resp)
stop_node(pos, nvm)
else
State:keep_running(pos, nvm, COUNTDOWN_TICKS)
end
end
return State:is_active(nvm)
2022-01-10 23:39:36 +03:00
end
local function after_dig_node(pos, oldnode, oldmetadata)
Cable:after_dig_node(pos)
techage.remove_node(pos, oldnode, oldmetadata)
techage.del_mem(pos)
end
2022-01-18 23:38:57 +03:00
local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
2022-01-19 23:24:14 +03:00
if techage.get_expoints(player) >= EX_POINTS then
local nvm = techage.get_nvm(pos)
State:state_button_event(pos, nvm, fields)
--M(pos):set_string("formspec", formspec(State, pos, nvm))
end
2022-01-18 23:38:57 +03:00
end
2022-01-10 23:39:36 +03:00
minetest.register_node("techage:ta5_fr_controller_pas", {
description = S("TA5 Fusion Reactor Controller"),
2022-01-08 00:35:12 +03:00
tiles = {
-- up, down, right, left, back, front
2022-01-10 23:39:36 +03:00
"techage_filling_ta4.png^techage_frame_ta5_top.png^techage_appl_arrow.png",
2022-01-08 00:35:12 +03:00
"techage_filling_ta4.png^techage_frame_ta4_bottom.png",
"techage_filling_ta4.png^techage_frame_ta5.png^techage_appl_hole_electric.png",
"techage_filling_ta4.png^techage_frame_ta5.png^techage_appl_hole_electric.png",
2022-01-18 23:38:57 +03:00
"techage_filling_ta4.png^techage_appl_plasma.png^techage_frame_ta5.png",
2022-01-10 23:39:36 +03:00
"techage_filling_ta4.png^techage_appl_plasma.png^techage_frame_ta5.png",
},
after_place_node = after_place_node,
on_timer = node_timer,
after_dig_node = after_dig_node,
2022-01-18 23:38:57 +03:00
on_receive_fields = on_receive_fields,
2022-01-10 23:39:36 +03:00
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_metal_defaults(),
})
minetest.register_node("techage:ta5_fr_controller_act", {
description = S("TA5 Fusion Reactor Controller"),
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta4.png^techage_frame_ta5_top.png^techage_appl_arrow.png",
"techage_filling_ta4.png^techage_frame_ta4_bottom.png",
"techage_filling_ta4.png^techage_frame_ta5.png^techage_appl_hole_electric.png",
"techage_filling_ta4.png^techage_frame_ta5.png^techage_appl_hole_electric.png",
2022-01-18 23:38:57 +03:00
{
name = "techage_filling4_ta4.png^techage_appl_plasma4.png^techage_frame4_ta5.png",
2022-01-18 23:38:57 +03:00
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 64,
aspect_h = 64,
length = 0.5,
},
},
2022-01-10 23:39:36 +03:00
{
name = "techage_filling4_ta4.png^techage_appl_plasma4.png^techage_frame4_ta5.png",
2022-01-10 23:39:36 +03:00
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 64,
aspect_h = 64,
length = 0.5,
},
},
2022-01-08 00:35:12 +03:00
},
2022-01-10 23:39:36 +03:00
after_place_node = after_place_node,
on_timer = node_timer,
after_dig_node = after_dig_node,
2022-01-18 23:38:57 +03:00
on_receive_fields = on_receive_fields,
2022-01-08 00:35:12 +03:00
paramtype2 = "facedir",
2022-01-18 23:38:57 +03:00
groups = {choppy=2, cracky=2, crumbly=2, not_in_creative_inventory=1},
drop = "",
2022-01-08 00:35:12 +03:00
is_ground_content = false,
sounds = default.node_sound_metal_defaults(),
})
techage.register_node({"techage:ta5_fr_controller_pas", "techage:ta5_fr_controller_act"}, {
on_recv_message = function(pos, src, topic, payload)
return State:on_receive_message(pos, topic, payload)
end,
2022-06-05 22:20:43 +03:00
on_beduino_receive_cmnd = function(pos, src, topic, payload)
return State:on_beduino_receive_cmnd(pos, topic, payload)
end,
on_beduino_request_data = function(pos, src, topic, payload)
return State:on_beduino_request_data(pos, topic, payload)
end,
})
2022-01-10 23:39:36 +03:00
power.register_nodes({"techage:ta5_fr_controller_pas", "techage:ta5_fr_controller_act"}, Cable, "con", {"L", "R"})
2022-01-19 23:24:14 +03:00
minetest.register_craft({
output = "techage:ta5_fr_controller_pas",
recipe = {
{'techage:aluminum', 'basic_materials:gold_wire', 'default:steel_ingot'},
{'techage:electric_cableS', 'techage:ta5_aichip2', 'techage:electric_cableS'},
{'default:steel_ingot', 'default:diamond', 'techage:aluminum'},
},
})