New branch: Move mesecon to node definition instead of functions like register_conductor; first steps: move everything to legacy

This commit is contained in:
Jeija 2012-12-08 14:14:04 +01:00
parent 12206ac190
commit e0aa5b1d3d
4 changed files with 189 additions and 101 deletions

View File

@ -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");
--Deprecated stuff
dofile(minetest.get_modpath("mesecons").."/legacy.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
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",
{{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");

View File

@ -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

View File

@ -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)

19
mesecons/presets.lua Normal file
View File

@ -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"