2012-12-15 18:45:51 +01:00
-- Internal.lua - The core of mesecons
--
2014-11-23 09:43:24 +01:00
-- For more practical developer resources see http://mesecons.net/developers.php
2012-12-15 18:45:51 +01:00
--
-- Function overview
2014-11-23 09:43:24 +01:00
-- mesecon.get_effector(nodename) --> Returns the mesecons.effector -specifictation in the nodedef by the nodename
-- mesecon.get_receptor(nodename) --> Returns the mesecons.receptor -specifictation in the nodedef by the nodename
-- mesecon.get_conductor(nodename) --> Returns the mesecons.conductor-specifictation in the nodedef by the nodename
-- mesecon.get_any_inputrules (node) --> Returns the rules of a node if it is a conductor or an effector
-- mesecon.get_any_outputrules (node) --> Returns the rules of a node if it is a conductor or a receptor
2012-12-15 18:45:51 +01:00
-- RECEPTORS
2014-11-23 09:43:24 +01:00
-- mesecon.is_receptor(nodename) --> Returns true if nodename is a receptor
2021-03-25 16:53:51 +01:00
-- mesecon.is_receptor_on(nodename) --> Returns true if nodename is an receptor with state = mesecon.state.on
2014-11-23 09:43:24 +01:00
-- mesecon.is_receptor_off(nodename) --> Returns true if nodename is an receptor with state = mesecon.state.off
-- mesecon.receptor_get_rules(node) --> Returns the rules of the receptor (mesecon.rules.default if none specified)
2012-12-15 18:45:51 +01:00
-- EFFECTORS
2014-11-23 09:43:24 +01:00
-- mesecon.is_effector(nodename) --> Returns true if nodename is an effector
-- mesecon.is_effector_on(nodename) --> Returns true if nodename is an effector with nodedef.mesecons.effector.action_off
-- mesecon.is_effector_off(nodename) --> Returns true if nodename is an effector with nodedef.mesecons.effector.action_on
-- mesecon.effector_get_rules(node) --> Returns the input rules of the effector (mesecon.rules.default if none specified)
2012-12-15 18:45:51 +01:00
-- SIGNALS
2014-11-23 09:43:24 +01:00
-- mesecon.activate(pos, node, depth) --> Activates the effector node at the specific pos (calls nodedef.mesecons.effector.action_on), higher depths are executed later
-- mesecon.deactivate(pos, node, depth) --> Deactivates the effector node at the specific pos (calls nodedef.mesecons.effector.action_off), higher depths are executed later
2014-11-22 15:42:22 +01:00
-- mesecon.changesignal(pos, node, rulename, newstate, depth) --> Changes the effector node at the specific pos (calls nodedef.mesecons.effector.action_change), higher depths are executed later
2012-12-15 18:45:51 +01:00
-- CONDUCTORS
2014-11-23 09:43:24 +01:00
-- mesecon.is_conductor(nodename) --> Returns true if nodename is a conductor
-- mesecon.is_conductor_on(node --> Returns true if node is a conductor with state = mesecon.state.on
-- mesecon.is_conductor_off(node) --> Returns true if node is a conductor with state = mesecon.state.off
-- mesecon.get_conductor_on(node_off) --> Returns the onstate nodename of the conductor
-- mesecon.get_conductor_off(node_on) --> Returns the offstate nodename of the conductor
-- mesecon.conductor_get_rules(node) --> Returns the input+output rules of a conductor (mesecon.rules.default if none specified)
2012-12-15 18:45:51 +01:00
-- HIGH-LEVEL Internals
2014-11-23 09:43:24 +01:00
-- mesecon.is_power_on(pos) --> Returns true if pos emits power in any way
-- mesecon.is_power_off(pos) --> Returns true if pos does not emit power in any way
-- mesecon.is_powered(pos) --> Returns true if pos is powered by a receptor or a conductor
-- RULES ROTATION helpers
2014-11-22 15:42:22 +01:00
-- mesecon.rotate_rules_right(rules)
-- mesecon.rotate_rules_left(rules)
-- mesecon.rotate_rules_up(rules)
-- mesecon.rotate_rules_down(rules)
2012-12-15 18:45:51 +01:00
-- These functions return rules that have been rotated in the specific direction
2022-04-01 17:33:41 -04:00
-- See fifo_queue.lua for documentation.
mesecon.fifo_queue = dofile ( minetest.get_modpath ( " mesecons " ) .. " /fifo_queue.lua " )
2021-03-25 16:53:51 +01:00
2012-12-15 18:45:51 +01:00
-- General
2014-11-22 15:42:22 +01:00
function mesecon . get_effector ( nodename )
2023-12-27 10:20:04 +01:00
local def = minetest.registered_nodes [ nodename ]
return def and def.mesecons and def.mesecons . effector
2012-12-15 18:45:51 +01:00
end
2012-08-13 11:58:04 +02:00
2014-11-22 15:42:22 +01:00
function mesecon . get_receptor ( nodename )
2023-12-27 10:20:04 +01:00
local def = minetest.registered_nodes [ nodename ]
return def and def.mesecons and def.mesecons . receptor
2012-12-15 18:45:51 +01:00
end
2014-11-22 15:42:22 +01:00
function mesecon . get_conductor ( nodename )
2023-12-27 10:20:04 +01:00
local def = minetest.registered_nodes [ nodename ]
return def and def.mesecons and def.mesecons . conductor
2012-12-15 18:45:51 +01:00
end
2016-07-25 10:01:43 +02:00
function mesecon . get_any_outputrules ( node )
2016-02-19 12:11:38 +01:00
if not node then return nil end
2014-11-22 15:42:22 +01:00
if mesecon.is_conductor ( node.name ) then
return mesecon.conductor_get_rules ( node )
elseif mesecon.is_receptor ( node.name ) then
return mesecon.receptor_get_rules ( node )
2012-08-13 11:58:04 +02:00
end
end
2016-07-25 10:01:43 +02:00
function mesecon . get_any_inputrules ( node )
2016-02-19 12:11:38 +01:00
if not node then return nil end
2014-11-22 15:42:22 +01:00
if mesecon.is_conductor ( node.name ) then
return mesecon.conductor_get_rules ( node )
elseif mesecon.is_effector ( node.name ) then
return mesecon.effector_get_rules ( node )
2012-12-15 18:45:51 +01:00
end
2014-11-23 09:43:24 +01:00
end
2016-07-25 10:01:43 +02:00
function mesecon . get_any_rules ( node )
2020-12-19 23:12:00 +03:00
return mesecon.merge_rule_sets ( mesecon.get_any_inputrules ( node ) ,
mesecon.get_any_outputrules ( node ) )
2012-12-15 18:45:51 +01:00
end
-- Receptors
-- Nodes that can power mesecons
2014-11-22 15:42:22 +01:00
function mesecon . is_receptor_on ( nodename )
local receptor = mesecon.get_receptor ( nodename )
2012-12-15 18:45:51 +01:00
if receptor and receptor.state == mesecon.state . on then
2012-12-08 14:14:04 +01:00
return true
end
2012-12-15 18:45:51 +01:00
return false
end
2014-11-22 15:42:22 +01:00
function mesecon . is_receptor_off ( nodename )
local receptor = mesecon.get_receptor ( nodename )
2012-12-15 18:45:51 +01:00
if receptor and receptor.state == mesecon.state . off then
return true
end
return false
end
2014-11-22 15:42:22 +01:00
function mesecon . is_receptor ( nodename )
local receptor = mesecon.get_receptor ( nodename )
2012-12-15 18:45:51 +01:00
if receptor then
return true
2012-08-13 11:58:04 +02:00
end
return false
end
2014-11-22 15:42:22 +01:00
function mesecon . receptor_get_rules ( node )
local receptor = mesecon.get_receptor ( node.name )
2012-12-15 18:45:51 +01:00
if receptor then
local rules = receptor.rules
2012-12-08 21:56:09 +01:00
if type ( rules ) == ' function ' then
2012-12-08 14:14:04 +01:00
return rules ( node )
2012-12-08 21:56:09 +01:00
elseif rules then
2012-12-08 14:14:04 +01:00
return rules
end
end
2012-12-21 16:22:25 +01:00
2012-12-08 21:56:09 +01:00
return mesecon.rules . default
2012-08-13 11:58:04 +02:00
end
2012-08-13 20:17:45 +02:00
-- Effectors
2012-12-08 21:56:09 +01:00
-- Nodes that can be powered by mesecons
2014-11-22 15:42:22 +01:00
function mesecon . is_effector_on ( nodename )
local effector = mesecon.get_effector ( nodename )
2012-12-15 18:45:51 +01:00
if effector and effector.action_off then
2012-12-08 14:14:04 +01:00
return true
end
2012-08-13 20:17:45 +02:00
return false
end
2014-11-22 15:42:22 +01:00
function mesecon . is_effector_off ( nodename )
local effector = mesecon.get_effector ( nodename )
2012-12-15 18:45:51 +01:00
if effector and effector.action_on then
2012-12-08 14:14:04 +01:00
return true
end
2012-08-13 20:17:45 +02:00
return false
end
2014-11-22 15:42:22 +01:00
function mesecon . is_effector ( nodename )
local effector = mesecon.get_effector ( nodename )
2012-12-15 18:45:51 +01:00
if effector then
2012-12-08 14:14:04 +01:00
return true
end
2012-12-15 18:45:51 +01:00
return false
2012-08-13 20:17:45 +02:00
end
2014-11-22 15:42:22 +01:00
function mesecon . effector_get_rules ( node )
local effector = mesecon.get_effector ( node.name )
2012-12-15 18:45:51 +01:00
if effector then
local rules = effector.rules
2012-12-08 21:56:09 +01:00
if type ( rules ) == ' function ' then
2012-12-08 14:14:04 +01:00
return rules ( node )
2012-12-08 21:56:09 +01:00
elseif rules then
2012-12-08 14:14:04 +01:00
return rules
end
end
2012-12-08 21:56:09 +01:00
return mesecon.rules . default
2012-08-13 16:49:14 +02:00
end
2014-01-10 22:29:18 +01:00
-- #######################
-- # Signals (effectors) #
-- #######################
-- Activation:
mesecon.queue : add_function ( " activate " , function ( pos , rulename )
2016-02-19 12:11:38 +01:00
local node = mesecon.get_node_force ( pos )
if not node then return end
2014-11-22 17:12:48 +01:00
local effector = mesecon.get_effector ( node.name )
2014-01-10 22:29:18 +01:00
if effector and effector.action_on then
effector.action_on ( pos , node , rulename )
end
end )
2012-08-13 11:58:04 +02:00
2014-11-22 15:42:22 +01:00
function mesecon . activate ( pos , node , rulename , depth )
2014-01-10 22:29:18 +01:00
if rulename == nil then
2014-11-22 15:42:22 +01:00
for _ , rule in ipairs ( mesecon.effector_get_rules ( node ) ) do
mesecon.activate ( pos , node , rule , depth + 1 )
2013-06-05 06:40:53 +02:00
end
2014-01-10 22:29:18 +01:00
return
2012-09-05 23:52:09 +02:00
end
2014-11-22 14:47:18 +01:00
mesecon.queue : add_action ( pos , " activate " , { rulename } , nil , rulename , 1 / depth )
2012-08-13 11:58:04 +02:00
end
2014-01-10 22:29:18 +01:00
-- Deactivation
mesecon.queue : add_function ( " deactivate " , function ( pos , rulename )
2016-02-19 12:11:38 +01:00
local node = mesecon.get_node_force ( pos )
if not node then return end
2014-11-23 09:43:24 +01:00
local effector = mesecon.get_effector ( node.name )
2014-01-10 22:29:18 +01:00
if effector and effector.action_off then
effector.action_off ( pos , node , rulename )
2012-09-05 23:52:09 +02:00
end
2014-01-10 22:29:18 +01:00
end )
2012-08-13 11:58:04 +02:00
2014-11-22 15:42:22 +01:00
function mesecon . deactivate ( pos , node , rulename , depth )
2014-01-10 22:29:18 +01:00
if rulename == nil then
2014-11-22 15:42:22 +01:00
for _ , rule in ipairs ( mesecon.effector_get_rules ( node ) ) do
mesecon.deactivate ( pos , node , rule , depth + 1 )
2013-06-05 06:40:53 +02:00
end
2014-01-10 22:29:18 +01:00
return
2013-06-05 06:40:53 +02:00
end
2014-11-22 14:47:18 +01:00
mesecon.queue : add_action ( pos , " deactivate " , { rulename } , nil , rulename , 1 / depth )
2013-06-05 06:40:53 +02:00
end
2014-01-10 22:29:18 +01:00
-- Change
mesecon.queue : add_function ( " change " , function ( pos , rulename , changetype )
2016-02-19 12:11:38 +01:00
local node = mesecon.get_node_force ( pos )
if not node then return end
2014-11-22 15:42:22 +01:00
local effector = mesecon.get_effector ( node.name )
2014-01-10 22:29:18 +01:00
if effector and effector.action_change then
effector.action_change ( pos , node , rulename , changetype )
2013-06-05 06:40:53 +02:00
end
2014-01-10 22:29:18 +01:00
end )
2013-06-05 06:40:53 +02:00
2014-11-22 15:42:22 +01:00
function mesecon . changesignal ( pos , node , rulename , newstate , depth )
2014-01-10 22:29:18 +01:00
if rulename == nil then
2014-11-22 15:42:22 +01:00
for _ , rule in ipairs ( mesecon.effector_get_rules ( node ) ) do
mesecon.changesignal ( pos , node , rule , newstate , depth + 1 )
2013-06-05 06:40:53 +02:00
end
2014-01-10 22:29:18 +01:00
return
2012-09-05 23:52:09 +02:00
end
2014-01-10 22:29:18 +01:00
2014-11-29 15:08:37 +01:00
-- Include "change" in overwritecheck so that it cannot be overwritten
-- by "active" / "deactivate" that will be called upon the node at the same time.
local overwritecheck = { " change " , rulename }
mesecon.queue : add_action ( pos , " change " , { rulename , newstate } , nil , overwritecheck , 1 / depth )
2012-08-13 11:58:04 +02:00
end
2012-12-08 21:56:09 +01:00
-- Conductors
2012-08-13 11:58:04 +02:00
2014-11-22 15:42:22 +01:00
function mesecon . is_conductor_on ( node , rulename )
2016-02-19 12:11:38 +01:00
if not node then return false end
2014-11-22 15:42:22 +01:00
local conductor = mesecon.get_conductor ( node.name )
2013-06-19 19:38:34 -07:00
if conductor then
if conductor.state then
return conductor.state == mesecon.state . on
end
if conductor.states then
if not rulename then
2014-11-22 15:42:22 +01:00
return mesecon.getstate ( node.name , conductor.states ) ~= 1
2013-06-19 19:38:34 -07:00
end
2014-11-22 15:42:22 +01:00
local bit = mesecon.rule2bit ( rulename , mesecon.conductor_get_rules ( node ) )
local binstate = mesecon.getbinstate ( node.name , conductor.states )
return mesecon.get_bit ( binstate , bit )
2013-06-19 19:38:34 -07:00
end
2012-08-13 11:58:04 +02:00
end
2016-02-19 12:11:38 +01:00
2012-08-13 11:58:04 +02:00
return false
end
2014-11-22 15:42:22 +01:00
function mesecon . is_conductor_off ( node , rulename )
2016-02-19 12:11:38 +01:00
if not node then return false end
2014-11-22 15:42:22 +01:00
local conductor = mesecon.get_conductor ( node.name )
2013-06-19 19:38:34 -07:00
if conductor then
if conductor.state then
return conductor.state == mesecon.state . off
end
if conductor.states then
if not rulename then
2014-11-22 15:42:22 +01:00
return mesecon.getstate ( node.name , conductor.states ) == 1
2013-06-19 19:38:34 -07:00
end
2014-11-22 15:42:22 +01:00
local bit = mesecon.rule2bit ( rulename , mesecon.conductor_get_rules ( node ) )
local binstate = mesecon.getbinstate ( node.name , conductor.states )
return not mesecon.get_bit ( binstate , bit )
2013-06-19 19:38:34 -07:00
end
2012-08-13 11:58:04 +02:00
end
2016-02-19 12:11:38 +01:00
2012-08-13 11:58:04 +02:00
return false
end
2014-11-22 15:42:22 +01:00
function mesecon . is_conductor ( nodename )
local conductor = mesecon.get_conductor ( nodename )
2012-12-15 18:45:51 +01:00
if conductor then
2012-12-08 14:14:04 +01:00
return true
end
2012-08-13 11:58:04 +02:00
return false
end
2014-11-22 15:42:22 +01:00
function mesecon . get_conductor_on ( node_off , rulename )
local conductor = mesecon.get_conductor ( node_off.name )
2012-12-15 18:45:51 +01:00
if conductor then
2013-06-19 19:38:34 -07:00
if conductor.onstate then
return conductor.onstate
end
if conductor.states then
2014-11-22 15:42:22 +01:00
local bit = mesecon.rule2bit ( rulename , mesecon.conductor_get_rules ( node_off ) )
local binstate = mesecon.getbinstate ( node_off.name , conductor.states )
binstate = mesecon.set_bit ( binstate , bit , " 1 " )
2013-06-19 19:38:34 -07:00
return conductor.states [ tonumber ( binstate , 2 ) + 1 ]
end
2012-08-13 11:58:04 +02:00
end
2022-02-12 14:12:12 -05:00
return nil
2012-08-13 11:58:04 +02:00
end
2014-11-22 15:42:22 +01:00
function mesecon . get_conductor_off ( node_on , rulename )
local conductor = mesecon.get_conductor ( node_on.name )
2012-12-15 18:45:51 +01:00
if conductor then
2013-06-19 19:38:34 -07:00
if conductor.offstate then
return conductor.offstate
end
if conductor.states then
2014-11-22 15:42:22 +01:00
local bit = mesecon.rule2bit ( rulename , mesecon.conductor_get_rules ( node_on ) )
local binstate = mesecon.getbinstate ( node_on.name , conductor.states )
binstate = mesecon.set_bit ( binstate , bit , " 0 " )
2013-06-19 19:38:34 -07:00
return conductor.states [ tonumber ( binstate , 2 ) + 1 ]
end
2012-12-15 18:45:51 +01:00
end
2022-02-12 14:12:12 -05:00
return nil
2012-08-16 21:24:06 +02:00
end
2014-11-22 15:42:22 +01:00
function mesecon . conductor_get_rules ( node )
local conductor = mesecon.get_conductor ( node.name )
2012-12-15 18:45:51 +01:00
if conductor then
local rules = conductor.rules
2012-12-08 21:56:09 +01:00
if type ( rules ) == ' function ' then
2012-12-08 14:14:04 +01:00
return rules ( node )
2012-12-08 21:56:09 +01:00
elseif rules then
2012-12-08 14:14:04 +01:00
return rules
end
end
2012-12-08 21:56:09 +01:00
return mesecon.rules . default
2012-08-16 21:24:06 +02:00
end
2012-12-15 18:45:51 +01:00
-- some more general high-level stuff
2014-11-22 15:42:22 +01:00
function mesecon . is_power_on ( pos , rulename )
2016-02-19 12:11:38 +01:00
local node = mesecon.get_node_force ( pos )
2016-02-22 15:47:18 -08:00
if node and ( mesecon.is_conductor_on ( node , rulename ) or mesecon.is_receptor_on ( node.name ) ) then
2012-08-13 11:58:04 +02:00
return true
end
return false
end
2014-11-22 15:42:22 +01:00
function mesecon . is_power_off ( pos , rulename )
2016-02-19 12:11:38 +01:00
local node = mesecon.get_node_force ( pos )
2016-02-22 15:47:18 -08:00
if node and ( mesecon.is_conductor_off ( node , rulename ) or mesecon.is_receptor_off ( node.name ) ) then
2012-08-13 20:17:45 +02:00
return true
2012-08-13 11:58:04 +02:00
end
2012-08-13 20:17:45 +02:00
return false
2012-08-13 11:58:04 +02:00
end
2021-11-27 10:28:13 -05:00
-- The set of conductor states which require light updates when they change.
local light_update_conductors
-- Calculate the contents of the above set if they have not been calculated.
local function find_light_update_conductors ( )
-- The expensive calculation is only done the first time.
if light_update_conductors then return end
light_update_conductors = { }
-- Find conductors whose lighting characteristics change depending on their state.
local checked = { }
for name , def in pairs ( minetest.registered_nodes ) do
local conductor = mesecon.get_conductor ( name )
if conductor and not checked [ name ] then
-- Find the other states of the conductor besides the current one.
local other_states
if conductor.onstate then
other_states = { conductor.onstate }
elseif conductor.offstate then
other_states = { conductor.offstate }
else
other_states = conductor.states
end
-- Check the conductor. Other states are marked as checked.
for _ , other_state in ipairs ( other_states ) do
local other_def = minetest.registered_nodes [ other_state ]
if ( def.paramtype == " light " ) ~= ( other_def.paramtype == " light " )
or def.sunlight_propagates ~= other_def.sunlight_propagates
or def.light_source ~= other_def.light_source then
-- The light characteristics change depending on the state.
-- The states are added to the set.
light_update_conductors [ name ] = true
for _ , other_state in ipairs ( other_states ) do
light_update_conductors [ other_state ] = true
checked [ other_state ] = true
end
break
end
checked [ other_state ] = true
end
end
end
end
2016-08-30 19:12:09 +02:00
-- Turn off an equipotential section starting at `pos`, which outputs in the direction of `link`.
-- Breadth-first search. Map is abstracted away in a voxelmanip.
-- Follow all all conductor paths replacing conductors that were already
-- looked at, activating / changing all effectors along the way.
2014-11-22 15:42:22 +01:00
function mesecon . turnon ( pos , link )
2021-11-27 10:28:13 -05:00
find_light_update_conductors ( )
2022-04-01 17:33:41 -04:00
local frontiers = mesecon.fifo_queue . new ( )
2021-03-25 16:53:51 +01:00
frontiers : add ( { pos = pos , link = link } )
local pos_can_be_skipped = { }
2013-06-19 19:38:34 -07:00
2014-11-22 14:47:18 +01:00
local depth = 1
2021-03-25 16:53:51 +01:00
for f in frontiers : iter ( ) do
2016-02-19 12:11:38 +01:00
local node = mesecon.get_node_force ( f.pos )
2013-06-19 19:38:34 -07:00
2014-11-22 14:47:18 +01:00
if not node then
2016-08-21 10:28:10 -07:00
-- Area does not exist; do nothing
2021-03-25 16:53:51 +01:00
pos_can_be_skipped [ minetest.hash_node_position ( f.pos ) ] = true
2021-07-28 14:22:57 -04:00
elseif mesecon.is_conductor ( node.name ) then
2014-11-22 15:42:22 +01:00
local rules = mesecon.conductor_get_rules ( node )
2014-11-22 14:47:18 +01:00
2021-07-28 14:22:57 -04:00
if mesecon.is_conductor_off ( node , f.link ) then
-- Call turnon on neighbors
for _ , r in ipairs ( mesecon.rule2meta ( f.link , rules ) ) do
local np = vector.add ( f.pos , r )
if not pos_can_be_skipped [ minetest.hash_node_position ( np ) ] then
for _ , l in ipairs ( mesecon.rules_link_rule_all ( f.pos , r ) ) do
frontiers : add ( { pos = np , link = l } )
end
2021-03-25 16:53:51 +01:00
end
2016-08-30 18:24:08 +02:00
end
2021-07-28 14:22:57 -04:00
2022-01-29 19:23:53 -05:00
mesecon.swap_node_force ( f.pos , mesecon.get_conductor_on ( node , f.link ) , light_update_conductors [ node.name ] ~= nil )
2012-08-16 21:24:06 +02:00
end
2016-07-25 10:01:43 +02:00
2021-07-28 14:22:57 -04:00
-- Only conductors with flat rules can be reliably skipped later
if not rules [ 1 ] or rules [ 1 ] . x then
pos_can_be_skipped [ minetest.hash_node_position ( f.pos ) ] = true
end
2014-11-22 15:42:22 +01:00
elseif mesecon.is_effector ( node.name ) then
mesecon.changesignal ( f.pos , node , f.link , mesecon.state . on , depth )
if mesecon.is_effector_off ( node.name ) then
mesecon.activate ( f.pos , node , f.link , depth )
2014-11-22 14:47:18 +01:00
end
2021-03-25 16:53:51 +01:00
else
pos_can_be_skipped [ minetest.hash_node_position ( f.pos ) ] = true
2012-08-13 11:58:04 +02:00
end
2014-11-22 14:47:18 +01:00
depth = depth + 1
2012-08-13 11:58:04 +02:00
end
end
2016-08-30 19:12:09 +02:00
-- Turn on an equipotential section starting at `pos`, which outputs in the direction of `link`.
-- Breadth-first search. Map is abstracted away in a voxelmanip.
-- Follow all all conductor paths replacing conductors that were already
-- looked at, deactivating / changing all effectors along the way.
-- In case an onstate receptor is discovered, abort the process by returning false, which will
-- cause `receptor_off` to discard all changes made in the voxelmanip.
-- Contrary to turnon, turnoff has to cache all change and deactivate signals so that they will only
-- be called in the very end when we can be sure that no conductor was found along the path.
--
-- Signal table entry structure:
-- {
-- pos = position of effector,
-- node = node descriptor (name, param1 and param2),
-- link = link the effector is connected to,
-- depth = indicates order in which signals wire fired, higher is later
-- }
2014-11-22 15:42:22 +01:00
function mesecon . turnoff ( pos , link )
2021-11-27 10:28:13 -05:00
find_light_update_conductors ( )
2022-04-01 17:33:41 -04:00
local frontiers = mesecon.fifo_queue . new ( )
2021-03-25 16:53:51 +01:00
frontiers : add ( { pos = pos , link = link } )
2016-08-30 19:12:09 +02:00
local signals = { }
2021-03-25 16:53:51 +01:00
local pos_can_be_skipped = { }
2012-08-13 11:58:04 +02:00
2014-11-22 14:47:18 +01:00
local depth = 1
2021-03-25 16:53:51 +01:00
for f in frontiers : iter ( ) do
2016-02-19 12:11:38 +01:00
local node = mesecon.get_node_force ( f.pos )
2014-01-11 16:46:27 +01:00
2014-11-22 14:47:18 +01:00
if not node then
2016-08-21 10:28:10 -07:00
-- Area does not exist; do nothing
2021-03-25 16:53:51 +01:00
pos_can_be_skipped [ minetest.hash_node_position ( f.pos ) ] = true
2021-07-28 14:22:57 -04:00
elseif mesecon.is_conductor ( node.name ) then
2014-11-22 15:42:22 +01:00
local rules = mesecon.conductor_get_rules ( node )
2021-07-28 14:22:57 -04:00
if mesecon.is_conductor_on ( node , f.link ) then
for _ , r in ipairs ( mesecon.rule2meta ( f.link , rules ) ) do
local np = vector.add ( f.pos , r )
if not pos_can_be_skipped [ minetest.hash_node_position ( np ) ] then
-- Check if an onstate receptor is connected. If that is the case,
-- abort this turnoff process by returning false. `receptor_off` will
-- discard all the changes that we made in the voxelmanip:
if mesecon.rules_link_rule_all_inverted ( f.pos , r ) [ 1 ] then
if mesecon.is_receptor_on ( mesecon.get_node_force ( np ) . name ) then
return false
end
2021-03-25 18:54:21 +01:00
end
2016-08-30 19:12:09 +02:00
2021-07-28 14:22:57 -04:00
-- Call turnoff on neighbors
for _ , l in ipairs ( mesecon.rules_link_rule_all ( f.pos , r ) ) do
frontiers : add ( { pos = np , link = l } )
end
2021-03-25 16:53:51 +01:00
end
2016-08-30 18:24:08 +02:00
end
2021-07-28 14:22:57 -04:00
2022-01-29 19:23:53 -05:00
mesecon.swap_node_force ( f.pos , mesecon.get_conductor_off ( node , f.link ) , light_update_conductors [ node.name ] ~= nil )
2016-07-25 10:01:43 +02:00
end
2021-07-28 14:22:57 -04:00
-- Only conductors with flat rules can be reliably skipped later
if not rules [ 1 ] or rules [ 1 ] . x then
pos_can_be_skipped [ minetest.hash_node_position ( f.pos ) ] = true
end
2014-11-22 15:42:22 +01:00
elseif mesecon.is_effector ( node.name ) then
2016-08-30 19:12:09 +02:00
table.insert ( signals , {
pos = f.pos ,
node = node ,
link = f.link ,
depth = depth
} )
2021-03-25 16:53:51 +01:00
else
pos_can_be_skipped [ minetest.hash_node_position ( f.pos ) ] = true
2012-08-13 11:58:04 +02:00
end
2014-11-22 14:47:18 +01:00
depth = depth + 1
2012-08-13 11:58:04 +02:00
end
2012-12-27 09:28:04 +01:00
2016-08-30 19:12:09 +02:00
for _ , sig in ipairs ( signals ) do
2022-12-07 07:15:23 -05:00
-- If sig.depth is 1, it has not yet been checked that the power source is actually off.
if sig.depth > 1 or not mesecon.is_powered ( sig.pos , sig.link ) then
mesecon.changesignal ( sig.pos , sig.node , sig.link , mesecon.state . off , sig.depth )
if mesecon.is_effector_on ( sig.node . name ) and not mesecon.is_powered ( sig.pos ) then
mesecon.deactivate ( sig.pos , sig.node , sig.link , sig.depth )
end
2012-12-27 09:28:04 +01:00
end
end
2016-08-30 19:12:09 +02:00
return true
2012-08-13 11:58:04 +02:00
end
2016-08-30 19:32:11 +02:00
-- Get all linking inputrules of inputnode (effector or conductor) that is connected to
-- outputnode (receptor or conductor) at position `output` and has an output in direction `rule`
2014-11-22 15:42:22 +01:00
function mesecon . rules_link_rule_all ( output , rule )
2016-02-14 20:55:50 +01:00
local input = vector.add ( output , rule )
2016-02-19 12:11:38 +01:00
local inputnode = mesecon.get_node_force ( input )
2016-07-25 10:01:43 +02:00
local inputrules = mesecon.get_any_inputrules ( inputnode )
2014-01-04 15:52:52 +01:00
if not inputrules then
2014-01-04 17:22:04 +01:00
return { }
2014-01-04 15:52:52 +01:00
end
2014-01-04 17:22:04 +01:00
local rules = { }
2015-10-04 13:30:34 +02:00
2014-11-22 15:42:22 +01:00
for _ , inputrule in ipairs ( mesecon.flattenrules ( inputrules ) ) do
2014-01-04 15:52:52 +01:00
-- Check if input accepts from output
2016-02-14 20:55:50 +01:00
if vector.equals ( vector.add ( input , inputrule ) , output ) then
2014-11-29 15:08:37 +01:00
table.insert ( rules , inputrule )
2014-01-04 15:52:52 +01:00
end
end
2016-02-19 12:11:38 +01:00
2014-01-04 17:22:04 +01:00
return rules
2014-01-04 15:52:52 +01:00
end
2016-08-30 19:32:11 +02:00
-- Get all linking outputnodes of outputnode (receptor or conductor) that is connected to
-- inputnode (effector or conductor) at position `input` and has an input in direction `rule`
2014-11-22 15:42:22 +01:00
function mesecon . rules_link_rule_all_inverted ( input , rule )
2016-02-14 20:55:50 +01:00
local output = vector.add ( input , rule )
2016-02-19 12:11:38 +01:00
local outputnode = mesecon.get_node_force ( output )
2016-07-25 10:01:43 +02:00
local outputrules = mesecon.get_any_outputrules ( outputnode )
2014-01-05 13:51:09 +01:00
if not outputrules then
return { }
end
local rules = { }
2015-10-04 13:30:34 +02:00
2014-11-22 15:42:22 +01:00
for _ , outputrule in ipairs ( mesecon.flattenrules ( outputrules ) ) do
2016-02-14 20:55:50 +01:00
if vector.equals ( vector.add ( output , outputrule ) , input ) then
2014-11-29 15:08:37 +01:00
table.insert ( rules , mesecon.invertRule ( outputrule ) )
2014-01-05 13:51:09 +01:00
end
end
return rules
end
2014-11-22 15:42:22 +01:00
function mesecon . is_powered ( pos , rule )
2016-02-19 12:11:38 +01:00
local node = mesecon.get_node_force ( pos )
2014-11-22 15:42:22 +01:00
local rules = mesecon.get_any_inputrules ( node )
2012-12-15 18:45:51 +01:00
if not rules then return false end
2012-08-16 21:24:06 +02:00
2014-11-21 22:04:39 +01:00
-- List of nodes that send out power to pos
local sourcepos = { }
2013-06-19 19:38:34 -07:00
if not rule then
2014-11-22 15:42:22 +01:00
for _ , rule in ipairs ( mesecon.flattenrules ( rules ) ) do
local rulenames = mesecon.rules_link_rule_all_inverted ( pos , rule )
2014-01-05 13:51:09 +01:00
for _ , rname in ipairs ( rulenames ) do
2016-02-14 20:55:50 +01:00
local np = vector.add ( pos , rname )
2016-02-19 12:11:38 +01:00
local nn = mesecon.get_node_force ( np )
if ( mesecon.is_conductor_on ( nn , mesecon.invertRule ( rname ) )
or mesecon.is_receptor_on ( nn.name ) ) then
2014-11-21 22:04:39 +01:00
table.insert ( sourcepos , np )
2014-01-05 13:51:09 +01:00
end
2013-06-19 19:38:34 -07:00
end
end
else
2014-11-22 15:42:22 +01:00
local rulenames = mesecon.rules_link_rule_all_inverted ( pos , rule )
2014-01-05 13:51:09 +01:00
for _ , rname in ipairs ( rulenames ) do
2016-02-14 20:55:50 +01:00
local np = vector.add ( pos , rname )
2016-02-19 12:11:38 +01:00
local nn = mesecon.get_node_force ( np )
2014-11-22 15:42:22 +01:00
if ( mesecon.is_conductor_on ( nn , mesecon.invertRule ( rname ) )
or mesecon.is_receptor_on ( nn.name ) ) then
2014-11-29 10:56:09 +01:00
table.insert ( sourcepos , np )
2014-01-05 13:51:09 +01:00
end
2012-08-13 11:58:04 +02:00
end
end
2014-11-21 22:04:39 +01:00
-- Return FALSE if not powered, return list of sources if is powered
if ( # sourcepos == 0 ) then return false
else return sourcepos end
2012-08-16 21:24:06 +02:00
end