diff options
-rw-r--r-- | mesecons/init.lua | 29 | ||||
-rw-r--r-- | mesecons/internal.lua (renamed from mesecons/internal_api.lua) | 127 | ||||
-rw-r--r-- | mesecons/services.lua | 19 |
3 files changed, 84 insertions, 91 deletions
diff --git a/mesecons/init.lua b/mesecons/init.lua index 6715a2f..9ecd007 100644 --- a/mesecons/init.lua +++ b/mesecons/init.lua @@ -41,9 +41,6 @@ --rules=mesecon:rotate_rules_right/left/up/down(rules) -- -- ---!! If a receptor node is removed, the circuit should be recalculated. This means you have to ---send an mesecon:receptor_off signal to the api when the node is dug, using the ---after_dig_node node property. -- --EFFECTORS -- @@ -86,8 +83,8 @@ mesecon={} -- contains all functions and all global variables mesecon.actions_on={} -- Saves registered function callbacks for mesecon on mesecon.actions_off={} -- Saves registered function callbacks for mesecon off mesecon.actions_change={} -- Saves registered function callbacks for mesecon change -mesecon.pwr_srcs={} -mesecon.pwr_srcs_off={} +mesecon.receptors={} +mesecon.receptors_off={} mesecon.effectors={} mesecon.rules={} mesecon.conductors={} @@ -96,38 +93,38 @@ mesecon.conductors={} dofile(minetest.get_modpath("mesecons").."/settings.lua") --Internal API -dofile(minetest.get_modpath("mesecons").."/internal_api.lua"); +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:add_receptor_node(nodename, rules, get_rules) --rules table is optional; if rules depend on param2 pass (nodename, nil, function get_rules) local i=1 repeat - if mesecon.pwr_srcs[i]==nil then break end + if mesecon.receptors[i]==nil then break end i=i+1 until false if get_rules==nil and rules==nil then rules=mesecon:get_rules("default") end - mesecon.pwr_srcs[i]={} - mesecon.pwr_srcs[i].name=nodename - mesecon.pwr_srcs[i].rules=rules - mesecon.pwr_srcs[i].get_rules=get_rules + mesecon.receptors[i]={} + mesecon.receptors[i].name=nodename + mesecon.receptors[i].rules=rules + mesecon.receptors[i].get_rules=get_rules end function mesecon:add_receptor_node_off(nodename, rules, get_rules) local i=1 repeat - if mesecon.pwr_srcs_off[i]==nil then break end + if mesecon.receptors_off[i]==nil then break end i=i+1 until false if get_rules==nil and rules==nil then rules=mesecon:get_rules("default") end - mesecon.pwr_srcs_off[i]={} - mesecon.pwr_srcs_off[i].name=nodename - mesecon.pwr_srcs_off[i].rules=rules - mesecon.pwr_srcs_off[i].get_rules=get_rules + mesecon.receptors_off[i]={} + mesecon.receptors_off[i].name=nodename + mesecon.receptors_off[i].rules=rules + mesecon.receptors_off[i].get_rules=get_rules end function mesecon:receptor_on(pos, rules) diff --git a/mesecons/internal_api.lua b/mesecons/internal.lua index 7570091..d47c25c 100644 --- a/mesecons/internal_api.lua +++ b/mesecons/internal.lua @@ -1,65 +1,55 @@ -- INTERNAL API -function mesecon:is_receptor_node(nodename, pos, ownpos) --ownpos must be position of the effector/mesecon NOT of the receptor node; pos is the receptor position - local i=1 - local j=1 - repeat - if mesecon.pwr_srcs[i].name==nodename then - if pos==nil and ownpos==nil then --old usage still possible - return true - end - local rules = mesecon.pwr_srcs[i].rules - local get_rules = mesecon.pwr_srcs[i].get_rules - local node = minetest.env:get_node(pos) - - if get_rules~=nil then --get_rules preferred - rules = get_rules(node.param2) - end - - j=1 - while rules[j]~=nil do --Check if dest. position is specified in the receptor's rules - if pos.x+rules[j].x==ownpos.x - and pos.y+rules[j].y==ownpos.y - and pos.z+rules[j].z==ownpos.z then - return true - end - j=j+1 - end +function mesecon:is_receptor_node(nodename) + local i = 1 + while mesecon.receptors[i] ~= nil do + if mesecon.receptors[i].name == nodename then + return true end - i=i+1 - until mesecon.pwr_srcs[i]==nil + i = i + 1 + end return false end -function mesecon:is_receptor_node_off(nodename, pos, ownpos) --ownpos must be position of the effector/mesecon NOT of the receptor node; pos is the receptor position - local i=1 - local j=1 - repeat - if mesecon.pwr_srcs_off[i].name==nodename then - if pos==nil and ownpos==nil then --old usage still possible - return true - end - local rules = mesecon.pwr_srcs_off[i].rules - local rules = mesecon.pwr_srcs_off[i].get_rules - local node = minetest.env:get_node(pos) +function mesecon:is_receptor_node_off(nodename, pos, ownpos) + local i = 1 + while mesecon.receptors_off[i] ~= nil do + if mesecon.receptors_off[i].name == nodename then + return true + end + i = i + 1 + end + return false +end - if get_rules ~= nil then - rules = get_rules(node.param2) +function mesecon:receptor_get_rules(node) + local i = 1 + while(mesecon.receptors[i] ~= nil) do + if mesecon.receptors[i].name == node.name then + if mesecon.receptors[i].get_rules ~= nil then + return mesecon.receptors[i].get_rules(node.param2) + elseif mesecon.receptors[i].rules ~=nil then + return mesecon.receptors[i].rules + else + return mesecon:get_rules("default") end + end + i = i + 1 + end - j=1 - while rules[j]~=nil do - if pos.x+rules[j].x==ownpos.x - and pos.y+rules[j].y==ownpos.y - and pos.z+rules[j].z==ownpos.z then - return true - end - j=j+1 + while(mesecon.receptors_off[i] ~= nil) do + if mesecon.receptors_off[i].name == node.name then + if mesecon.receptors_off[i].get_rules ~= nil then + return mesecon.receptors_off[i].get_rules(node.param2) + elseif mesecon.receptors_off[i].rules ~=nil then + return mesecon.receptors_off[i].rules + else + return mesecon:get_rules("default") end end - i=i+1 - until mesecon.pwr_srcs_off[i]==nil - return false + i = i + 1 + end + return nil end --Signals @@ -295,7 +285,7 @@ function mesecon:connected_to_pw_src(pos, checked) local i = 1 while checked[i] ~= nil do --find out if node has already been checked - if checked[i].x == pos.x and checked[i].y == pos.y and checked[i].z == pos.z then + if compare_pos(checked[i], pos) then return false, checked end i = i + 1 @@ -357,8 +347,17 @@ function mesecon:is_powered_by_receptor(pos) if not pos_checked then table.insert(rcpt_checked, rcpt_pos) rcpt = minetest.env:get_node(rcpt_pos) - if mesecon:is_receptor_node(rcpt.name, rcpt_pos, pos) then - return true + + if mesecon:is_receptor_node(rcpt.name) then + rules = mesecon:receptor_get_rules(rcpt) + while rules[j] ~= nil do + if pos.x + rules[j].x == rcpt_pos.x + and pos.y + rules[j].y == rcpt_pos.y + and pos.z + rules[j].z == rcpt_pos.z then + return true + end + j=j+1 + end end end j=j+1 @@ -393,26 +392,6 @@ function mesecon:updatenode(pos) end end -minetest.register_on_placenode(function(pos, newnode, placer) - if mesecon:is_powered(pos) then - if mesecon:is_conductor_off(newnode.name) then - mesecon:turnon(pos) - else - mesecon:changesignal(pos) - mesecon:activate(pos) - end - end -end) - -minetest.register_on_dignode( - function(pos, oldnode, digger) - if mesecon:is_conductor_on(oldnode.name) then - local i = 1 - mesecon:receptor_off(pos) - end - end -) - function compare_pos(pos1, pos2) return pos1.x == pos2.x and pos1.y == pos2.y and pos1.z == pos2.z end diff --git a/mesecons/services.lua b/mesecons/services.lua index cf3b78b..4cf615e 100644 --- a/mesecons/services.lua +++ b/mesecons/services.lua @@ -6,7 +6,24 @@ minetest.register_on_dignode( end if mesecon:is_receptor_node(oldnode.name) then - mesecon:receptor_off(pos) + mesecon:receptor_off(pos, mesecon:receptor_get_rules(oldnode)) + end + end +) + +minetest.register_on_placenode( + function (pos, node) + if mesecon:is_receptor_node(node.name) then + mesecon:receptor_on(pos, mesecon:receptor_get_rules(node)) + end + + if mesecon:is_powered(pos) then + if mesecon:is_conductor_off(node.name) then + mesecon:turnon(pos) + else + mesecon:changesignal(pos) + mesecon:activate(pos) + end end end ) |