From d069275d0927fda377bd55d408c78318781bf0bf Mon Sep 17 00:00:00 2001 From: Jeija Date: Thu, 29 Mar 2012 21:06:48 +0200 Subject: Rules + Receptor system rework --- mesecons/init.lua | 127 +++++++++++++++++++++++++++++++++++------------ mesecons_button/init.lua | 62 ++++++++++------------- mesecons_torch/init.lua | 55 ++++++++++---------- 3 files changed, 147 insertions(+), 97 deletions(-) diff --git a/mesecons/init.lua b/mesecons/init.lua index fe8e950..e2e3e92 100644 --- a/mesecons/init.lua +++ b/mesecons/init.lua @@ -115,24 +115,16 @@ minetest.register_craft({ } }) -function mesecon:is_power_on(p, x, y, z) - local lpos = {} - lpos.x=p.x+x - lpos.y=p.y+y - lpos.z=p.z+z - local node = minetest.env:get_node(lpos) +function mesecon:is_power_on(pos) + local node = minetest.env:get_node(pos) if node.name == "mesecons:mesecon_on" or mesecon:is_receptor_node(node.name) then - return 1 + return true end - return 0 + return false end -function mesecon:is_power_off(p, x, y, z) - local lpos = {} - lpos.x=p.x+x - lpos.y=p.y+y - lpos.z=p.z+z - local node = minetest.env:get_node(lpos) +function mesecon:is_power_off(pos) + local node = minetest.env:get_node(pos) if node.name == "mesecons:mesecon_off" or mesecon:is_receptor_node_off(node.name) then return 1 end @@ -229,7 +221,7 @@ function mesecon:connected_to_pw_src(pos, x, y, z, checked, firstcall) checked[i].y=lpos.y checked[i].z=lpos.z - if mesecon:is_receptor_node(node.name) == true then -- receptor nodes (power sources) can be added using mesecon:add_receptor_node + if mesecon:is_receptor_node(node.name, lpos, pos) == true then -- receptor nodes (power sources) can be added using mesecon:add_receptor_node return 1 end @@ -250,16 +242,34 @@ function mesecon:connected_to_pw_src(pos, x, y, z, checked, firstcall) end function mesecon:check_if_turnon(pos) - local getactivated=0 - local rules=mesecon:get_rules("default") local i=1 - while rules[i]~=nil do - getactivated=getactivated+mesecon:is_power_on(pos, rules[i].x, rules[i].y, rules[i].z) - i=i+1 + local j=1 + local k=1 + local rcpt + local rcpt_pos={} + local rules + + rules=mesecon:get_rules("default") + while rules[k]~=nil do + if minetest.env:get_node({x=pos.x+rules[k].x, y=pos.y+rules[k].y, z=pos.z+rules[k].z}).name=="mesecons:mesecon_on" then + return true + end + k=k+1 end - if getactivated > 0 then - return true + + while mesecon.rules[i]~=nil do + j=1 + while mesecon.rules[i].rules[j]~=nil do + rcpt_pos={x=pos.x-mesecon.rules[i].rules[j].x, y=pos.y-mesecon.rules[i].rules[j].y, z=pos.z-mesecon.rules[i].rules[j].z} + rcpt=minetest.env:get_node(rcpt_pos) + if mesecon:is_receptor_node(rcpt.name, rcpt_pos, pos) then + return true + end + j=j+1 + end + i=i+1 end + return false end @@ -283,22 +293,34 @@ minetest.register_on_dignode( -- API API API API API API API API API API API API API API API API API API -function mesecon:add_receptor_node(nodename) +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 - i=i+1 if mesecon.pwr_srcs[i]==nil then break end + i=i+1 until false - mesecon.pwr_srcs[i]=nodename + 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 end -function mesecon:add_receptor_node_off(nodename) +function mesecon:add_receptor_node_off(nodename, rules, get_rules) local i=1 repeat - i=i+1 if mesecon.pwr_srcs_off[i]==nil then break end + i=i+1 until false - mesecon.pwr_srcs_off[i]=nodename + 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 end function mesecon:receptor_on(pos, rules) @@ -332,20 +354,60 @@ end -- INTERNAL API -function mesecon:is_receptor_node(nodename) +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 node=minetest.env:get_node(pos) + if rules==nil then + rules=mesecon.pwr_srcs[i].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 + end i=i+1 - if mesecon.pwr_srcs[i]==nodename then return true end until mesecon.pwr_srcs[i]==nil return false end -function mesecon:is_receptor_node_off(nodename) +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 node=minetest.env:get_node(pos) + if rules==nil then + rules=mesecon.pwr_srcs_off[i].get_rules(node.param2) + 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 + end + end i=i+1 - if mesecon.pwr_srcs_off[i]==nodename then return true end until mesecon.pwr_srcs_off[i]==nil return false end @@ -411,7 +473,6 @@ function mesecon:get_rules(name) end end - mesecon:add_rules("default", {{x=0, y=0, z=-1}, {x=1, y=0, z=0}, diff --git a/mesecons_button/init.lua b/mesecons_button/init.lua index 3b76bbb..e0191f7 100644 --- a/mesecons_button/init.lua +++ b/mesecons_button/init.lua @@ -30,52 +30,40 @@ minetest.register_node("mesecons_button:button_on", { minetest.register_on_dignode( function(pos, oldnode, digger) if oldnode.name == "mesecons_button:button_on" then - mesecon:receptor_off(pos) + mesecon:receptor_off(pos, mesecon.button_get_rules(oldnode.param2)) end end ) minetest.register_on_punchnode(function(pos, node, puncher) if node.name == "mesecons_button:button_off" then minetest.env:add_node(pos, {name="mesecons_button:button_on",param2=node.param2}) - - local rules=mesecon:get_rules("button") - if node.param2 == 5 then - rules=mesecon:rotate_rules_left(rules) - end - if node.param2 == 3 then - rules=mesecon:rotate_rules_right(mesecon:rotate_rules_right(rules)) - end - if node.param2 == 4 then - rules=mesecon:rotate_rules_right(rules) - end + local rules=mesecon.button_get_rules(node.param2) mesecon:receptor_on(pos, rules) + minetest.after(1, mesecon.button_turnoff, {pos=pos, param2=node.param2}) end end) -minetest.register_abm({ - nodenames = {"mesecons_button:button_on"}, - interval = 1, - chance = 1, - action = function(pos, node, active_object_count, active_object_count_wider) - minetest.env:add_node(pos, {name="mesecons_button:button_off",param2=node.param2}) +mesecon.button_turnoff = function (params) + if minetest.env:get_node(params.pos).name=="mesecons_button:button_on" then + minetest.env:add_node(params.pos, {name="mesecons_button:button_off", param2=params.param2}) + local rules=mesecon.button_get_rules(param2) + mesecon:receptor_off(params.pos, rules) + end +end - local rules=mesecon:get_rules("button") - print (rules[1].x) - if node.param2 == 5 then - rules=mesecon:rotate_rules_left(rules) - end - print (rules[1].x) - if node.param2 == 3 then - rules=mesecon:rotate_rules_right(mesecon:rotate_rules_right(rules)) - end - print (rules[1].x) - if node.param2 == 4 then - rules=mesecon:rotate_rules_right(rules) - end - print (rules[1].x) - mesecon:receptor_off(pos, rules) +mesecon.button_get_rules = function(param2) + local rules=mesecon:get_rules("button") + if param2 == 5 then + rules=mesecon:rotate_rules_left(rules) end -}) + if param2 == 3 then + rules=mesecon:rotate_rules_right(mesecon:rotate_rules_right(rules)) + end + if param2 == 4 then + rules=mesecon:rotate_rules_right(rules) + end + return rules +end minetest.register_craft({ output = '"mesecons_button:button_off" 2', @@ -95,9 +83,11 @@ mesecon:add_rules("button", { {x=0, y=1, z=1}, {x=0, y=-1, z=1}, {x=0, y=1, z=-1}, +{x=0, y=0, z=-1}, {x=0, y=-1, z=-1}, {x=0, y=-1, z=0}, {x=2, y=0, z=0}}) -mesecon:add_receptor_node("mesecons_button:button") -mesecon:add_receptor_node_off("mesecons_button:button_off") +mesecon:add_receptor_node_off("mesecons_button:button_off", nil, mesecon.button_get_rules) +mesecon:add_receptor_node("mesecons_button:button_on", nil, mesecon.button_get_rules) + diff --git a/mesecons_torch/init.lua b/mesecons_torch/init.lua index ba3ac65..cfdac11 100644 --- a/mesecons_torch/init.lua +++ b/mesecons_torch/init.lua @@ -50,30 +50,24 @@ minetest.register_abm({ chance = 1, action = function(pos, node, active_object_count, active_object_count_wider) local pa = {x=0, y=0, z=0} - --pa.y = 1 - local rules=mesecon:get_rules("mesecontorch") + local rules=mesecon.torch_get_rules(minetest.env:get_node(pos).param2) - if node.param2 == 4 then + if node.param2 == 4 then pa.z = -2 - rules=mesecon:rotate_rules_right(rules) - elseif node.param2 == 2 then + elseif node.param2 == 2 then pa.x = -2 - rules=mesecon:rotate_rules_right(mesecon:rotate_rules_right(rules)) --180 degrees - elseif node.param2 == 5 then + elseif node.param2 == 5 then pa.z = 2 - rules=mesecon:rotate_rules_left(rules) - elseif node.param2 == 3 then + elseif node.param2 == 3 then pa.x = 2 - elseif node.param2 == 1 then + elseif node.param2 == 1 then pa.y = 2 - rules=mesecon:rotate_rules_down(rules) - elseif node.param2 == 0 then + elseif node.param2 == 0 then pa.y = -2 - rules=mesecon:rotate_rules_up(rules) end local postc = {x=pos.x-pa.x, y=pos.y-pa.y, z=pos.z-pa.z} - if mesecon:is_power_on(postc,0,0,0)==1 then + if mesecon:is_power_on(postc)==1 then if node.name ~= "mesecons_torch:mesecon_torch_off" then minetest.env:add_node(pos, {name="mesecons_torch:mesecon_torch_off",param2=node.param2}) mesecon:receptor_off(pos, rules_string) @@ -97,22 +91,27 @@ minetest.register_on_dignode( minetest.register_on_placenode(function(pos, node, placer) if node.name == "mesecons_torch:mesecon_torch_on" then - local rules=mesecon:get_rules("mesecontorch") - if node.param2 == 4 then - rules=mesecon:rotate_rules_right(rules) - elseif node.param2 == 2 then - rules=mesecon:rotate_rules_right(mesecon:rotate_rules_right(rules)) --180 degrees - elseif node.param2 == 5 then - rules=mesecon:rotate_rules_left(rules) - elseif node.param2 == 1 then - rules=mesecon:rotate_rules_down(rules) - elseif node.param2 == 0 then - rules=mesecon:rotate_rules_up(rules) - end + local rules=mesecon.torch_get_rules(minetest.env:get_node(pos).param2) mesecon:receptor_on(pos, rules) end end) +mesecon.torch_get_rules = function(param2) + local rules=mesecon:get_rules("mesecontorch") + if param2 == 5 then + rules=mesecon:rotate_rules_right(rules) + elseif param2 == 2 then + rules=mesecon:rotate_rules_right(mesecon:rotate_rules_right(rules)) --180 degrees + elseif param2 == 4 then + rules=mesecon:rotate_rules_left(rules) + elseif param2 == 1 then + rules=mesecon:rotate_rules_down(rules) + elseif param2 == 0 then + rules=mesecon:rotate_rules_up(rules) + end + return rules +end + mesecon:add_rules("mesecontorch", {{x=1, y=0, z=0}, {x=0, y=0, z=1}, @@ -120,8 +119,8 @@ mesecon:add_rules("mesecontorch", {x=0, y=1, z=0}, {x=0, y=-1, z=0}}) -mesecon:add_receptor_node("mesecons_torch:mesecon_torch_on") -mesecon:add_receptor_node_off("mesecons_torch:mesecon_torch_off") +mesecon:add_receptor_node("mesecons_torch:mesecon_torch_on", nil, mesecon.torch_get_rules) +mesecon:add_receptor_node_off("mesecons_torch:mesecon_torch_off", nil, mesecon.torch_get_rules) -- Param2 Table (Block Attached To) -- 5 = z-1 -- cgit v1.2.3