2014-04-20 21:44:58 +02:00
|
|
|
-- Dig and place services
|
|
|
|
|
2016-08-23 07:20:46 +02:00
|
|
|
mesecon.on_placenode = function(pos, node)
|
|
|
|
mesecon.execute_autoconnect_hooks_now(pos, node)
|
2014-11-22 11:37:47 +01:00
|
|
|
|
2014-03-21 21:31:34 +01:00
|
|
|
-- Receptors: Send on signal when active
|
2014-11-22 15:42:22 +01:00
|
|
|
if mesecon.is_receptor_on(node.name) then
|
|
|
|
mesecon.receptor_on(pos, mesecon.receptor_get_rules(node))
|
2014-03-21 21:31:34 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Conductors: Send turnon signal when powered or replace by respective offstate conductor
|
|
|
|
-- if placed conductor is an onstate one
|
2014-11-22 15:42:22 +01:00
|
|
|
if mesecon.is_conductor(node.name) then
|
|
|
|
local sources = mesecon.is_powered(pos)
|
2014-11-21 22:04:39 +01:00
|
|
|
if sources then
|
2014-03-21 21:31:34 +01:00
|
|
|
-- also call receptor_on if itself is powered already, so that neighboring
|
|
|
|
-- conductors will be activated (when pushing an on-conductor with a piston)
|
2014-11-21 22:04:39 +01:00
|
|
|
for _, s in ipairs(sources) do
|
2021-07-24 18:40:43 +02:00
|
|
|
local rule = vector.subtract(s, pos)
|
2014-11-22 15:42:22 +01:00
|
|
|
mesecon.turnon(pos, rule)
|
2014-11-21 22:04:39 +01:00
|
|
|
end
|
2014-11-22 15:42:22 +01:00
|
|
|
--mesecon.receptor_on (pos, mesecon.conductor_get_rules(node))
|
|
|
|
elseif mesecon.is_conductor_on(node) then
|
2017-10-03 23:55:33 +02:00
|
|
|
node.name = mesecon.get_conductor_off(node)
|
|
|
|
minetest.swap_node(pos, node)
|
2014-03-21 21:31:34 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Effectors: Send changesignal and activate or deactivate
|
2014-11-22 15:42:22 +01:00
|
|
|
if mesecon.is_effector(node.name) then
|
2014-11-29 15:08:37 +01:00
|
|
|
local powered_rules = {}
|
2015-10-04 12:30:04 +02:00
|
|
|
local unpowered_rules = {}
|
2014-11-29 15:08:37 +01:00
|
|
|
|
|
|
|
-- for each input rule, check if powered
|
|
|
|
for _, r in ipairs(mesecon.effector_get_rules(node)) do
|
|
|
|
local powered = mesecon.is_powered(pos, r)
|
2015-10-04 12:30:04 +02:00
|
|
|
if powered then table.insert(powered_rules, r)
|
|
|
|
else table.insert(unpowered_rules, r) end
|
2014-11-29 15:08:37 +01:00
|
|
|
|
|
|
|
local state = powered and mesecon.state.on or mesecon.state.off
|
|
|
|
mesecon.changesignal(pos, node, r, state, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
if (#powered_rules > 0) then
|
|
|
|
for _, r in ipairs(powered_rules) do
|
|
|
|
mesecon.activate(pos, node, r, 1)
|
|
|
|
end
|
2015-10-04 12:30:04 +02:00
|
|
|
else
|
|
|
|
for _, r in ipairs(unpowered_rules) do
|
|
|
|
mesecon.deactivate(pos, node, r, 1)
|
|
|
|
end
|
2012-08-13 11:58:04 +02:00
|
|
|
end
|
|
|
|
end
|
2012-12-15 18:45:51 +01:00
|
|
|
end
|
2012-08-13 11:58:04 +02:00
|
|
|
|
2016-08-23 07:20:46 +02:00
|
|
|
mesecon.on_dignode = function(pos, node)
|
2014-11-22 15:42:22 +01:00
|
|
|
if mesecon.is_conductor_on(node) then
|
|
|
|
mesecon.receptor_off(pos, mesecon.conductor_get_rules(node))
|
|
|
|
elseif mesecon.is_receptor_on(node.name) then
|
|
|
|
mesecon.receptor_off(pos, mesecon.receptor_get_rules(node))
|
2012-08-13 08:50:10 +02:00
|
|
|
end
|
2012-12-15 18:45:51 +01:00
|
|
|
|
2016-08-23 07:20:46 +02:00
|
|
|
mesecon.execute_autoconnect_hooks_queue(pos, node)
|
|
|
|
end
|
2014-11-22 11:37:47 +01:00
|
|
|
|
2017-10-07 00:44:49 +02:00
|
|
|
function mesecon.on_blastnode(pos, intensity)
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
minetest.remove_node(pos)
|
|
|
|
mesecon.on_dignode(pos, node)
|
|
|
|
return minetest.get_node_drops(node.name, "")
|
|
|
|
end
|
|
|
|
|
2012-12-15 18:45:51 +01:00
|
|
|
minetest.register_on_placenode(mesecon.on_placenode)
|
|
|
|
minetest.register_on_dignode(mesecon.on_dignode)
|
2014-04-20 21:44:58 +02:00
|
|
|
|
|
|
|
-- Overheating service for fast circuits
|
2017-04-14 21:14:17 +02:00
|
|
|
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 = {}
|
2014-04-20 21:44:58 +02:00
|
|
|
|
|
|
|
-- returns true if heat is too high
|
2017-04-14 21:14:17 +02:00
|
|
|
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
|
2014-04-20 21:44:58 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2017-04-14 21:14:17 +02:00
|
|
|
function mesecon.do_cooldown(pos)
|
|
|
|
local id = minetest.hash_node_position(pos)
|
|
|
|
object_heat[id] = nil
|
|
|
|
end
|
2014-04-20 21:44:58 +02:00
|
|
|
|
2017-04-14 21:14:17 +02:00
|
|
|
function mesecon.get_heat(pos)
|
|
|
|
local id = minetest.hash_node_position(pos)
|
|
|
|
return object_heat[id] or 0
|
|
|
|
end
|
2014-04-20 21:44:58 +02:00
|
|
|
|
2017-04-14 21:14:17 +02:00
|
|
|
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
|
2014-04-20 21:44:58 +02:00
|
|
|
end
|
2017-04-14 21:14:17 +02:00
|
|
|
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)
|