Skip to content

Commit 4301f02

Browse files
authored
Luacontroller: Add overheat factor for deferred tasks (minetest-mods#715)
1 parent e2fc73d commit 4301f02

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

mesecons_luacontroller/init.lua

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
-- ports = get_real_port_states(pos): gets if inputs are powered from outside
1313
-- newport = merge_port_states(state1, state2): just does result = state1 or state2 for every port
1414
-- set_port(pos, rule, state): activates/deactivates the mesecons according to the port states
15-
-- set_port_states(pos, ports): Applies new port states to a Luacontroller at pos
15+
-- set_port_states(pos, ports, ignore_overheat): Applies new port states to a Luacontroller at pos
1616
-- run_inner(pos, code, event): runs code on the controller at pos and event
1717
-- reset_formspec(pos, code, errmsg): installs new code and prints error messages, without resetting LCID
1818
-- reset_meta(pos, code, errmsg): performs a software-reset, installs new code and prints error message
1919
-- run(pos, event): a wrapper for run_inner which gets code & handles errors via reset_meta
20-
-- resetn(pos): performs a hardware reset, turns off all ports
20+
-- reset(pos): performs a hardware reset, turns off all ports
2121
--
2222
-- The Sandbox
2323
-- The whole code of the controller runs in a sandbox,
@@ -129,10 +129,15 @@ local function clean_port_states(ports)
129129
ports.d = ports.d and true or false
130130
end
131131

132+
local is_controller_burnt
132133

133-
local function set_port_states(pos, ports)
134+
local function set_port_states(pos, ports, ignore_overheat)
134135
local node = minetest.get_node(pos)
135136
local name = node.name
137+
if not ignore_overheat and is_controller_burnt(name) then
138+
return -- Avoid swapping back to a non-burnt node
139+
end
140+
136141
clean_port_states(ports)
137142
local vports = minetest.registered_nodes[name].virtual_portstates
138143
local new_name = generate_name(ports)
@@ -168,6 +173,11 @@ end
168173
-----------------
169174
-- Overheating --
170175
-----------------
176+
177+
is_controller_burnt = function(node_name)
178+
return node_name == (BASENAME .. "_burnt")
179+
end
180+
171181
local function burn_controller(pos)
172182
local node = minetest.get_node(pos)
173183
node.name = BASENAME.."_burnt"
@@ -178,6 +188,10 @@ local function burn_controller(pos)
178188
end
179189

180190
local function overheat(pos)
191+
if is_controller_burnt(core.get_node(pos).name) then
192+
-- Avoid spamming "Node overheats" log messages.
193+
return true
194+
end
181195
if mesecon.do_overheat(pos) then -- If too hot
182196
burn_controller(pos)
183197
return true
@@ -688,9 +702,10 @@ local function run_inner(pos, code, event)
688702
end
689703

690704
local function reset_formspec(meta, code, errmsg)
705+
code = code or ""
691706
meta:set_string("code", code)
692707
meta:mark_as_private("code")
693-
code = minetest.formspec_escape(code or "")
708+
code = minetest.formspec_escape(code)
694709
errmsg = minetest.formspec_escape(tostring(errmsg or ""))
695710
meta:set_string("formspec", "size[12,10]"
696711
.."style_type[label,textarea;font=mono]"
@@ -722,11 +737,11 @@ local function run(pos, event)
722737
end
723738

724739
local function reset(pos)
725-
set_port_states(pos, {a=false, b=false, c=false, d=false})
740+
set_port_states(pos, {a=false, b=false, c=false, d=false}, true)
726741
end
727742

728743
local function node_timer(pos)
729-
if minetest.registered_nodes[minetest.get_node(pos).name].is_burnt then
744+
if is_controller_burnt(core.get_node(pos).name) then
730745
return false
731746
end
732747
run(pos, {type="interrupt"})
@@ -740,15 +755,16 @@ end
740755
mesecon.queue:add_function("lc_interrupt", function (pos, luac_id, iid)
741756
-- There is no luacontroller anymore / it has been reprogrammed / replaced / burnt
742757
if (minetest.get_meta(pos):get_int("luac_id") ~= luac_id) then return end
743-
if (minetest.registered_nodes[minetest.get_node(pos).name].is_burnt) then return end
758+
if is_controller_burnt(core.get_node(pos).name) then return end
744759
run(pos, {type="interrupt", iid = iid})
745760
end)
746761

747762
mesecon.queue:add_function("lc_digiline_relay", function (pos, channel, luac_id, msg)
748763
if not digiline then return end
749764
-- This check is only really necessary because in case of server crash, old actions can be thrown into the future
750765
if (minetest.get_meta(pos):get_int("luac_id") ~= luac_id) then return end
751-
if (minetest.registered_nodes[minetest.get_node(pos).name].is_burnt) then return end
766+
-- This escapes the sandbox, thus give a harsh penalty.
767+
if overheat(pos) then return end
752768
-- The actual work
753769
digiline:receptor_send(pos, digiline.rules.default, channel, msg)
754770
end)
@@ -929,7 +945,6 @@ minetest.register_node(BASENAME .. "_burnt", {
929945
"jeija_microcontroller_sides.png"
930946
},
931947
inventory_image = "jeija_luacontroller_burnt_top.png",
932-
is_burnt = true,
933948
paramtype = "light",
934949
is_ground_content = false,
935950
groups = {dig_immediate=2, not_in_creative_inventory=1},

0 commit comments

Comments
 (0)