Skip to content

Commit

Permalink
Improve overheating (#334)
Browse files Browse the repository at this point in the history
New overheating system that doesn’t use the meta.
  • Loading branch information
numberZero committed Apr 14, 2017
1 parent 2554164 commit 54b9eaf
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
67 changes: 50 additions & 17 deletions mesecons/services.lua
Expand Up @@ -66,30 +66,63 @@ minetest.register_on_placenode(mesecon.on_placenode)
minetest.register_on_dignode(mesecon.on_dignode)

-- Overheating service for fast circuits
local OVERHEAT_MAX = mesecon.setting("overheat_max", 20)
local COOLDOWN_TIME = mesecon.setting("cooldown_time", 2.0)
local COOLDOWN_STEP = mesecon.setting("cooldown_granularity", 0.5)
local COOLDOWN_MULTIPLIER = OVERHEAT_MAX / COOLDOWN_TIME
local cooldown_timer = 0.0
local object_heat = {}

-- returns true if heat is too high
mesecon.do_overheat = function(pos)
local meta = minetest.get_meta(pos)
local heat = meta:get_int("heat") or 0

heat = heat + 1
meta:set_int("heat", heat)

if heat < mesecon.setting("overheat_max", 20) then
mesecon.queue:add_action(pos, "cooldown", {}, 1, nil, 0)
else
function mesecon.do_overheat(pos)
local id = minetest.hash_node_position(pos)
local heat = (object_heat[id] or 0) + 1
object_heat[id] = heat
if heat >= OVERHEAT_MAX then
minetest.log("action", "Node overheats at " .. minetest.pos_to_string(pos))
object_heat[id] = nil
return true
end

return false
end

function mesecon.do_cooldown(pos)
local id = minetest.hash_node_position(pos)
object_heat[id] = nil
end

mesecon.queue:add_function("cooldown", function (pos)
local meta = minetest.get_meta(pos)
local heat = meta:get_int("heat")
function mesecon.get_heat(pos)
local id = minetest.hash_node_position(pos)
return object_heat[id] or 0
end

if (heat > 0) then
meta:set_int("heat", heat - 1)
function mesecon.move_hot_nodes(moved_nodes)
local new_heat = {}
for _, n in ipairs(moved_nodes) do
local old_id = minetest.hash_node_position(n.oldpos)
local new_id = minetest.hash_node_position(n.pos)
new_heat[new_id] = object_heat[old_id]
object_heat[old_id] = nil
end
for id, heat in pairs(new_heat) do
object_heat[id] = heat
end
end)
end

local function global_cooldown(dtime)
cooldown_timer = cooldown_timer + dtime
if cooldown_timer < COOLDOWN_STEP then
return -- don't overload the CPU
end
local cooldown = COOLDOWN_MULTIPLIER * cooldown_timer
cooldown_timer = 0
for id, heat in pairs(object_heat) do
heat = heat - cooldown
if heat <= 0 then
object_heat[id] = nil -- free some RAM
else
object_heat[id] = heat
end
end
end
minetest.register_globalstep(global_cooldown)
3 changes: 2 additions & 1 deletion mesecons_gates/init.lua
Expand Up @@ -74,7 +74,8 @@ local function register_gate(name, inputnumber, assess, recipe, description)
assess = assess,
onstate = basename.."_on",
offstate = basename.."_off",
inputnumber = inputnumber
inputnumber = inputnumber,
after_dig_node = mesecon.do_cooldown,
},{
tiles = {"jeija_microcontroller_bottom.png^".."jeija_gate_off.png^"..
"jeija_gate_"..name..".png"},
Expand Down
4 changes: 2 additions & 2 deletions mesecons_luacontroller/init.lua
Expand Up @@ -315,7 +315,7 @@ local function create_environment(pos, mem, event)
port = vports_copy,
event = event,
mem = mem,
heat = minetest.get_meta(pos):get_int("heat"),
heat = mesecon.get_heat(pos),
heat_max = mesecon.setting("overheat_max", 20),
print = safe_print,
interrupt = get_interrupt(pos),
Expand Down Expand Up @@ -485,7 +485,6 @@ local function reset_meta(pos, code, errmsg)
"image_button[3.75,6;2.5,1;jeija_luac_runbutton.png;program;]"..
"image_button_exit[9.72,-0.25;0.425,0.4;jeija_close_window.png;exit;]"..
"label[0.1,5;"..errmsg.."]")
meta:set_int("heat", 0)
meta:set_int("luac_id", math.random(1, 65535))
end

Expand Down Expand Up @@ -626,6 +625,7 @@ for d = 0, 1 do
d = d == 1,
},
after_dig_node = function (pos, node)
mesecon.do_cooldown(pos)
mesecon.receptor_off(pos, output_rules)
end,
is_luacontroller = true,
Expand Down
1 change: 1 addition & 0 deletions mesecons_mvps/init.lua
Expand Up @@ -246,3 +246,4 @@ mesecon.register_mvps_stopper("doors:door_steel_t_1")
mesecon.register_mvps_stopper("doors:door_steel_b_2")
mesecon.register_mvps_stopper("doors:door_steel_t_2")
mesecon.register_mvps_stopper("default:chest_locked")
mesecon.register_on_mvps_move(mesecon.move_hot_nodes)

0 comments on commit 54b9eaf

Please sign in to comment.