diff options
-rw-r--r-- | mesecons/init.lua | 97 | ||||
-rw-r--r-- | mesecons/internal.lua | 94 | ||||
-rw-r--r-- | mesecons/legacy.lua | 80 | ||||
-rw-r--r-- | mesecons/presets.lua | 19 |
4 files changed, 189 insertions, 101 deletions
diff --git a/mesecons/init.lua b/mesecons/init.lua index 40aa7d9..d980419 100644 --- a/mesecons/init.lua +++ b/mesecons/init.lua @@ -3,7 +3,7 @@ -- | \/ | |___ ____ |___ | | | | \ | |____ -- | | | | | | | | | \ | | -- | | |___ ____| |___ |____ |____| | \| ____| --- by Jeija and Minerd247 +-- by Jeija, Uberi (Temperest), sfan5, VanessaE, -- -- -- @@ -21,110 +21,29 @@ mesecon.actions_off={} -- Saves registered function callbacks for mesecon off mesecon.actions_change={} -- Saves registered function callbacks for mesecon change mesecon.receptors={} mesecon.effectors={} -mesecon.rules={} mesecon.conductors={} -- INCLUDE SETTINGS dofile(minetest.get_modpath("mesecons").."/settings.lua") +--Presets (eg default rules) +dofile(minetest.get_modpath("mesecons").."/presets.lua"); + --Internal API dofile(minetest.get_modpath("mesecons").."/internal.lua"); --- API API API API API API API API API API API API API API API API API API - -function mesecon:register_receptor(onstate, offstate, rules, get_rules) - if get_rules == nil and rules == nil then - rules = mesecon:get_rules("default") - end - table.insert(mesecon.receptors, - {onstate = onstate, - offstate = offstate, - rules = rules, - get_rules = get_rules}) -end - -function mesecon:register_effector(onstate, offstate, input_rules, get_input_rules) - if get_input_rules==nil and input_rules==nil then - rules=mesecon:get_rules("default") - end - table.insert(mesecon.effectors, - {onstate = onstate, - offstate = offstate, - input_rules = input_rules, - get_input_rules = get_input_rules}) -end - -function mesecon:receptor_on(pos, rules) - if rules == nil then - rules = mesecon:get_rules("default") - end - - for i, rule in ipairs(rules) do - local np = { - x = pos.x + rule.x, - y = pos.y + rule.y, - z = pos.z + rule.z} - if mesecon:rules_link(pos, np, rules) then - mesecon:turnon(np, pos) - end - end -end - -function mesecon:receptor_off(pos, rules) - if rules == nil then - rules = mesecon:get_rules("default") - end - - for i, rule in ipairs(rules) do - local np = { - x = pos.x + rule.x, - y = pos.y + rule.y, - z = pos.z + rule.z} - if mesecon:rules_link(pos, np, rules) and not mesecon:connected_to_pw_src(np) then - mesecon:turnoff(np, pos) - end - end -end - -function mesecon:register_on_signal_on(action) - table.insert(mesecon.actions_on, action) -end +--Deprecated stuff +dofile(minetest.get_modpath("mesecons").."/legacy.lua"); -function mesecon:register_on_signal_off(action) - table.insert(mesecon.actions_off, action) -end +-- API API API API API API API API API API API API API API API API API API -function mesecon:register_on_signal_change(action) - table.insert(mesecon.actions_change, action) -end -function mesecon:register_conductor (onstate, offstate, rules, get_rules) - if rules == nil then - rules = mesecon:get_rules("default") - end - table.insert(mesecon.conductors, {onstate = onstate, offstate = offstate, rules = rules, get_rules = get_rules}) -end -mesecon:add_rules("default", -{{x=0, y=0, z=-1}, -{x=1, y=0, z=0}, -{x=-1, y=0, z=0}, -{x=0, y=0, z=1}, -{x=1, y=1, z=0}, -{x=1, y=-1, z=0}, -{x=-1, y=1, z=0}, -{x=-1, y=-1, z=0}, -{x=0, y=1, z=1}, -{x=0, y=-1, z=1}, -{x=0, y=1, z=-1}, -{x=0, y=-1, z=-1}}) -print("[MESEcons] Main mod Loaded!") +print("[OK] mesecons") --The actual wires dofile(minetest.get_modpath("mesecons").."/wires.lua"); --Services like turnoff receptor on dignode and so on dofile(minetest.get_modpath("mesecons").."/services.lua"); ---Deprecated stuff -dofile(minetest.get_modpath("mesecons").."/legacy.lua"); diff --git a/mesecons/internal.lua b/mesecons/internal.lua index 7a576d6..8c4ccaf 100644 --- a/mesecons/internal.lua +++ b/mesecons/internal.lua @@ -2,6 +2,11 @@ --Receptors function mesecon:is_receptor_node(nodename) + if minetest.registered_nodes[nodename].mesecons + and minetest.registered_nodes[nodename].mesecons.receptor + and minetest.registered_nodes[nodename].mesecons.receptor.state == mesecon.state.on then + return true + end for _, receptor in ipairs(mesecon.receptors) do if receptor.onstate == nodename then return true @@ -11,6 +16,11 @@ function mesecon:is_receptor_node(nodename) end function mesecon:is_receptor_node_off(nodename, pos, ownpos) + if minetest.registered_nodes[nodename].mesecons + and minetest.registered_nodes[nodename].mesecons.receptor + and minetest.registered_nodes[nodename].mesecons.receptor.state == mesecon.state.off then + return true + end for _, receptor in ipairs(mesecon.receptors) do if receptor.offstate == nodename then return true @@ -20,7 +30,16 @@ function mesecon:is_receptor_node_off(nodename, pos, ownpos) end function mesecon:receptor_get_rules(node) - for i, receptor in ipairs(mesecon.receptors) do + if minetest.registered_nodes[node.name].mesecons + and minetest.registered_nodes[node.name].mesecons.receptor then + local rules = minetest.registered_nodes[node.name].mesecons.receptor.rules + if type(rules) == 'function' then + return rules(node) + else + return rules + end + end + for i, receptor in ipairs(mesecon.receptors) do --TODO if receptor.onstate == node.name or receptor.offstate == node.name then if receptor.get_rules ~= nil then return receptor.get_rules(node.param2) @@ -36,28 +55,51 @@ end -- Effectors function mesecon:is_effector_on(nodename) - for i, effector in ipairs(mesecon.effectors) do + for i, effector in ipairs(mesecon.effectors) do --TODO if effector.onstate == nodename then return true end end + if minetest.registered_nodes[nodename].mesecons + and minetest.registered_nodes[nodename].mesecons.effector + and minetest.registered_nodes[nodename].mesecons.effector.state == mesecon.state.on then + return true + end return false end function mesecon:is_effector_off(nodename) - for i, effector in ipairs(mesecon.effectors) do + for i, effector in ipairs(mesecon.effectors) do --TODO if effector.offstate == nodename then return true end end + if minetest.registered_nodes[nodename].mesecons + and minetest.registered_nodes[nodename].mesecons.effector + and minetest.registered_nodes[nodename].mesecons.effector.state == mesecon.state.off then + return true + end return false end function mesecon:is_effector(nodename) - return mesecon:is_effector_on(nodename) or mesecon:is_effector_off(nodename) + if minetest.registered_nodes[nodename].mesecons + and minetest.registered_nodes[nodename].mesecons.effector then + return true + end + return mesecon:is_effector_on(nodename) or mesecon:is_effector_off(nodename) --TODO end function mesecon:effector_get_input_rules(node) + if minetest.registered_nodes[node.name].mesecons + and minetest.registered_nodes[node.name].mesecons.effector then + local rules = minetest.registered_nodes[node.name].mesecons.effector.rules + if type(rules) == 'function' then + return rules(node) + else + return rules + end + end for i, effector in ipairs(mesecon.effectors) do if effector.onstate == node.name or effector.offstate == node.name then @@ -74,21 +116,21 @@ end --Signals -function mesecon:activate(pos) +function mesecon:activate(pos) --TODO local node = minetest.env:get_node(pos) for i, action in ipairs(mesecon.actions_on) do action(pos, node) end end -function mesecon:deactivate(pos) +function mesecon:deactivate(pos) --TODO local node = minetest.env:get_node(pos) for i, action in ipairs(mesecon.actions_off) do action(pos, node) end end -function mesecon:changesignal(pos) +function mesecon:changesignal(pos) --TODO local node = minetest.env:get_node(pos) for i, action in ipairs(mesecon.actions_change) do action(pos, node) @@ -112,7 +154,11 @@ end --Conductor system stuff function mesecon:get_conductor_on(offstate) - for i, conductor in ipairs(mesecon.conductors) do + if minetest.registered_nodes[nodename].mesecons + and minetest.registered_nodes[nodename].mesecons.condcutor then + return minetest.registered_nodes[nodename].mesecons.conductor.onstate + end + for i, conductor in ipairs(mesecon.conductors) do --TODO if conductor.offstate == offstate then return conductor.onstate end @@ -121,7 +167,11 @@ function mesecon:get_conductor_on(offstate) end function mesecon:get_conductor_off(onstate) - for i, conductor in ipairs(mesecon.conductors) do + if minetest.registered_nodes[nodename].mesecons + and minetest.registered_nodes[nodename].mesecons.condcutor then + return minetest.registered_nodes[nodename].mesecons.conductor.offstate + end + for i, conductor in ipairs(mesecon.conductors) do --TODO if conductor.onstate == onstate then return conductor.offstate end @@ -130,7 +180,12 @@ function mesecon:get_conductor_off(onstate) end function mesecon:is_conductor_on(name) - for i, conductor in ipairs(mesecon.conductors) do + if minetest.registered_nodes[nodename].mesecons + and minetest.registered_nodes[nodename].mesecons.condcutor + and minetest.registered_nodes[nodename].mesecons.condcutor.state == mesecon.state.on then + return true + end + for i, conductor in ipairs(mesecon.conductors) do --TODO if conductor.onstate == name then return true end @@ -139,7 +194,12 @@ function mesecon:is_conductor_on(name) end function mesecon:is_conductor_off(name) - for i, conductor in ipairs(mesecon.conductors) do + if minetest.registered_nodes[nodename].mesecons + and minetest.registered_nodes[nodename].mesecons.condcutor + and minetest.registered_nodes[nodename].mesecons.condcutor.state == mesecon.state.off then + return true + end + for i, conductor in ipairs(mesecon.conductors) do --TODO if conductor.offstate == name then return true end @@ -148,11 +208,21 @@ function mesecon:is_conductor_off(name) end function mesecon:is_conductor(name) + --TODO: simplify return mesecon:is_conductor_on(name) or mesecon:is_conductor_off(name) end function mesecon:conductor_get_rules(node) - for i, conductor in ipairs(mesecon.conductors) do + if minetest.registered_nodes[node.name].mesecons + and minetest.registered_nodes[node.name].mesecons.condcutor then + local rules = minetest.registered_nodes[node.name].mesecons.condcutor.rules + if type(rules) == 'function' then + return rules(node) + else + return rules + end + end + for i, conductor in ipairs(mesecon.conductors) do --TODO if conductor.onstate == node.name or conductor.offstate == node.name then if conductor.get_rules ~= nil then diff --git a/mesecons/legacy.lua b/mesecons/legacy.lua index 4fd73dd..271221a 100644 --- a/mesecons/legacy.lua +++ b/mesecons/legacy.lua @@ -1,3 +1,5 @@ +--very old: + function mesecon:add_receptor_node(name, rules, get_rules) if get_rules==nil and rules==nil then rules=mesecon:get_rules("default") @@ -11,3 +13,81 @@ function mesecon:add_receptor_node_off(name, rules, get_rules) end table.insert(mesecon.receptors, {offstate = name, rules = rules, get_rules = get_rules}) end + +--old: + +function mesecon:register_receptor(onstate, offstate, rules, get_rules) + if get_rules == nil and rules == nil then + rules = mesecon:get_rules("default") + end + + table.insert(mesecon.receptors, + {onstate = onstate, + offstate = offstate, + rules = rules, + get_rules = get_rules}) +end + +function mesecon:register_effector(onstate, offstate, input_rules, get_input_rules) + if get_input_rules==nil and input_rules==nil then + rules=mesecon:get_rules("default") + end + table.insert(mesecon.effectors, + {onstate = onstate, + offstate = offstate, + input_rules = input_rules, + get_input_rules = get_input_rules}) +end + +function mesecon:receptor_on(pos, rules) + if rules == nil then + rules = mesecon:get_rules("default") + end + + for i, rule in ipairs(rules) do + local np = { + x = pos.x + rule.x, + y = pos.y + rule.y, + z = pos.z + rule.z} + if mesecon:rules_link(pos, np, rules) then + mesecon:turnon(np, pos) + end + end +end + +function mesecon:receptor_off(pos, rules) + if rules == nil then + rules = mesecon:get_rules("default") + end + + for i, rule in ipairs(rules) do + local np = { + x = pos.x + rule.x, + y = pos.y + rule.y, + z = pos.z + rule.z} + if mesecon:rules_link(pos, np, rules) and not mesecon:connected_to_pw_src(np) then + mesecon:turnoff(np, pos) + end + end +end + +function mesecon:register_on_signal_on(action) + table.insert(mesecon.actions_on, action) +end + +function mesecon:register_on_signal_off(action) + table.insert(mesecon.actions_off, action) +end + +function mesecon:register_on_signal_change(action) + table.insert(mesecon.actions_change, action) +end + +function mesecon:register_conductor (onstate, offstate, rules, get_rules) + if rules == nil then + rules = mesecon:get_rules("default") + end + table.insert(mesecon.conductors, {onstate = onstate, offstate = offstate, rules = rules, get_rules = get_rules}) +end + +mesecon:add_rules("default", mesecon.rules.default) diff --git a/mesecons/presets.lua b/mesecons/presets.lua new file mode 100644 index 0000000..450f039 --- /dev/null +++ b/mesecons/presets.lua @@ -0,0 +1,19 @@ +mesecon.rules={} +mesecon.state = {} + +mesecon.rules.default = +{{x=0, y=0, z=-1}, +{x=1, y=0, z=0}, +{x=-1, y=0, z=0}, +{x=0, y=0, z=1}, +{x=1, y=1, z=0}, +{x=1, y=-1, z=0}, +{x=-1, y=1, z=0}, +{x=-1, y=-1, z=0}, +{x=0, y=1, z=1}, +{x=0, y=-1, z=1}, +{x=0, y=1, z=-1}, +{x=0, y=-1, z=-1}} + +mesecon.state.on = "on" +mesecon.state.off = "off" |