From 8e0b2a2b119e41c8ebd187fa33cd35b9abd57e66 Mon Sep 17 00:00:00 2001 From: Jeija Date: Thu, 29 Mar 2012 11:35:23 +0200 Subject: [PATCH] Simplify rules System --- mesecons/init.lua | 225 ++++++++++++++++----------------------- mesecons_button/init.lua | 51 ++++++--- mesecons_torch/init.lua | 64 +++++------ 3 files changed, 157 insertions(+), 183 deletions(-) diff --git a/mesecons/init.lua b/mesecons/init.lua index aa9dc4c..2d7e3a7 100644 --- a/mesecons/init.lua +++ b/mesecons/init.lua @@ -63,13 +63,13 @@ -- INCLUDE SETTINGS dofile(minetest.get_modpath("mesecons").."/settings.lua") - -- PUBLIC VARIABLES 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.pwr_srcs={} -- this is public for now -mesecon.pwr_srcs_off={} -- this is public for now +mesecon.pwr_srcs={} +mesecon.pwr_srcs_off={} +mesecon.rules={} -- MESECONS @@ -136,7 +136,7 @@ end function mesecon:turnon(p, x, y, z, firstcall, rules) if rules==nil then - rules="default" + rules=mesecon:get_rules("default") end local lpos = {} lpos.x=p.x+x @@ -152,10 +152,9 @@ function mesecon:turnon(p, x, y, z, firstcall, rules) nodeupdate(lpos) end if node.name == "mesecons:mesecon_off" or firstcall then - local rules=mesecon:get_rules(rules) local i=1 while rules[i]~=nil do - mesecon:turnon(lpos, rules[i].x, rules[i].y, rules[i].z, false, "default") + mesecon:turnon(lpos, rules[i].x, rules[i].y, rules[i].z, false) i=i+1 end end @@ -163,7 +162,7 @@ end function mesecon:turnoff(pos, x, y, z, firstcall, rules) if rules==nil then - rules="default" + rules=mesecon:get_rules("default") end local lpos = {} lpos.x=pos.x+x @@ -191,10 +190,9 @@ function mesecon:turnoff(pos, x, y, z, firstcall, rules) if node.name == "mesecons:mesecon_on" or firstcall then if connected == 0 then - local rules=mesecon:get_rules(rules) local i=1 while rules[i]~=nil do - mesecon:turnoff(lpos, rules[i].x, rules[i].y, rules[i].z, false, "default") + mesecon:turnoff(lpos, rules[i].x, rules[i].y, rules[i].z, false) i=i+1 end end @@ -250,7 +248,7 @@ function mesecon:check_if_turnon(pos) local getactivated=0 local rules=mesecon:get_rules("default") local i=1 - while rules[i]~=nil do + while rules[i]~=nil do getactivated=getactivated+mesecon:is_power_on(pos, rules[i].x, rules[i].y, rules[i].z) i=i+1 end @@ -388,137 +386,94 @@ mesecon:register_on_signal_off(function(pos, node) end end) --- mesecon rules -function mesecon:get_rules(name) - local rules={} - rules[0]="dummy" - if name=="default" then - table.insert(rules, {x=0, y=0, z=-1}) - table.insert(rules, {x=1, y=0, z=0}) - table.insert(rules, {x=-1, y=0, z=0}) - table.insert(rules, {x=0, y=0, z=1}) - table.insert(rules, {x=1, y=1, z=0}) - table.insert(rules, {x=1, y=-1, z=0}) - table.insert(rules, {x=-1, y=1, z=0}) - table.insert(rules, {x=-1, y=-1, z=0}) - table.insert(rules, {x=0, y=1, z=1}) - table.insert(rules, {x=0, y=-1, z=1}) - table.insert(rules, {x=0, y=1, z=-1}) - table.insert(rules, {x=0, y=-1, z=-1}) - end - if name=="movestone" then - table.insert(rules, {x=0, y=1, z=-1}) - table.insert(rules, {x=0, y=0, z=-1}) - table.insert(rules, {x=0, y=-1, z=-1}) - table.insert(rules, {x=0, y=1, z=1}) - table.insert(rules, {x=0, y=-1, z=1}) - table.insert(rules, {x=0, y=0, z=1}) - table.insert(rules, {x=1, y=0, z=0}) - table.insert(rules, {x=1, y=1, z=0}) - table.insert(rules, {x=1, y=-1, z=0}) - table.insert(rules, {x=-1, y=1, z=0}) - table.insert(rules, {x=-1, y=-1, z=0}) - table.insert(rules, {x=-1, y=0, z=0}) - end - if name=="piston" then - table.insert(rules, {x=0, y=1, z=0}) - table.insert(rules, {x=0, y=-1, z=0}) - table.insert(rules, {x=0, y=1, z=-1}) - table.insert(rules, {x=0, y=0, z=-1}) - table.insert(rules, {x=0, y=-1, z=-1}) - table.insert(rules, {x=0, y=1, z=1}) - table.insert(rules, {x=0, y=-1, z=1}) - table.insert(rules, {x=0, y=0, z=1}) - table.insert(rules, {x=1, y=0, z=0}) - table.insert(rules, {x=1, y=1, z=0}) - table.insert(rules, {x=1, y=-1, z=0}) - table.insert(rules, {x=-1, y=1, z=0}) - table.insert(rules, {x=-1, y=-1, z=0}) - table.insert(rules, {x=-1, y=0, z=0}) - end - if name=="pressureplate" then - table.insert(rules, {x=0, y=1, z=-1}) - table.insert(rules, {x=0, y=0, z=-1}) - table.insert(rules, {x=0, y=-1, z=-1}) - table.insert(rules, {x=0, y=1, z=1}) - table.insert(rules, {x=0, y=-1, z=1}) - table.insert(rules, {x=0, y=0, z=1}) - table.insert(rules, {x=1, y=0, z=0}) - table.insert(rules, {x=1, y=1, z=0}) - table.insert(rules, {x=1, y=-1, z=0}) - table.insert(rules, {x=-1, y=1, z=0}) - table.insert(rules, {x=-1, y=-1, z=0}) - table.insert(rules, {x=-1, y=0, z=0}) - table.insert(rules, {x=0, y=-1, z=0}) - table.insert(rules, {x=0, y=1, z=0}) - end - if name=="mesecontorch_x-" then - table.insert(rules, {x=1, y=0, z=0}) - table.insert(rules, {x=0, y=0, z=1}) - table.insert(rules, {x=0, y=0, z=-1}) - end - if name=="mesecontorch_x+" then - table.insert(rules, {x=-1, y=0, z=0}) - table.insert(rules, {x=0, y=0, z=1}) - table.insert(rules, {x=0, y=0, z=-1}) - end - if name=="mesecontorch_z-" then - table.insert(rules, {x=0, y=0, z=1}) - table.insert(rules, {x=1, y=0, z=0}) - table.insert(rules, {x=-1, y=0, z=0}) - end - if name=="mesecontorch_z+" then - table.insert(rules, {x=0, y=0, z=-1}) - table.insert(rules, {x=1, y=0, z=0}) - table.insert(rules, {x=-1, y=0, z=0}) - end - if name=="mesecontorch_y-" then - table.insert(rules, {x=0, y=1, z=0}) - table.insert(rules, {x=1, y=1, z=0}) - table.insert(rules, {x=-1, y=1, z=0}) - table.insert(rules, {x=0, y=1, z=1}) - table.insert(rules, {x=0, y=1, z=-1}) - end - if name=="mesecontorch_y+" then - table.insert(rules, {x=0, y=-1, z=0}) - table.insert(rules, {x=1, y=-1, z=0}) - table.insert(rules, {x=-1, y=-1, z=0}) - table.insert(rules, {x=0, y=-1, z=1}) - table.insert(rules, {x=0, y=-1, z=-1}) +function mesecon:add_rules(name, rules) + local i=0 + while mesecon.rules[i]~=nil do + i=i+1 end + mesecon.rules[i]={} + mesecon.rules[i].name=name + mesecon.rules[i].rules=rules +end - if name=="button_x+" or name=="button_x-" - or name=="button_z-" or name=="button_z+" then --Is any button -table.insert(rules, {x=0, y=0, z=-1}) - table.insert(rules, {x=1, y=0, z=0}) - table.insert(rules, {x=-1, y=0, z=0}) - table.insert(rules, {x=0, y=0, z=1}) - table.insert(rules, {x=1, y=1, z=0}) - table.insert(rules, {x=1, y=-1, z=0}) - table.insert(rules, {x=-1, y=1, z=0}) - table.insert(rules, {x=-1, y=-1, z=0}) - table.insert(rules, {x=0, y=1, z=1}) - table.insert(rules, {x=0, y=-1, z=1}) - table.insert(rules, {x=0, y=1, z=-1}) - table.insert(rules, {x=0, y=-1, z=-1}) - table.insert(rules, {x=0, y=-1, z=0}) +function mesecon:get_rules(name) + local i=0 + while mesecon.rules[i]~=nil do + if mesecon.rules[i].name==name then + return mesecon.rules[i].rules + end + i=i+1 end - if name=="button_x+" then - table.insert(rules, {x=-2, y=0, z=0}) - end - if name=="button_x-" then - table.insert(rules, {x=2, y=0, z=0}) - end - if name=="button_z+" then - table.insert(rules, {x=0, y=0, z=-2}) - end - if name=="button_z-" then - table.insert(rules, {x=0, y=0, z=2}) - end - return 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}}) + + +--Rules rotation Functions: +function mesecon:rotate_rules_right(rules) + local i=1 + local nr={}; + while rules[i]~=nil do + nr[i]={} + nr[i].z=rules[i].x + nr[i].x=-rules[i].z + nr[i].y=rules[i].y + i=i+1 + end + return nr +end + +function mesecon:rotate_rules_left(rules) + local i=1 + local nr={}; + while rules[i]~=nil do + nr[i]={} + nr[i].z=-rules[i].x + nr[i].x=rules[i].z + nr[i].y=rules[i].y + i=i+1 + end + return nr +end + +function mesecon:rotate_rules_down(rules) + local i=1 + local nr={}; + while rules[i]~=nil do + nr[i]={} + nr[i].y=rules[i].x + nr[i].x=-rules[i].y + nr[i].z=rules[i].z + i=i+1 + end + return nr +end + +function mesecon:rotate_rules_up(rules) + local i=1 + local nr={}; + while rules[i]~=nil do + nr[i]={} + nr[i].y=-rules[i].x + nr[i].x=rules[i].y + nr[i].z=rules[i].z + i=i+1 + end + return nr +end print("[MESEcons] Loaded!") diff --git a/mesecons_button/init.lua b/mesecons_button/init.lua index 8cb2c9f..3b76bbb 100644 --- a/mesecons_button/init.lua +++ b/mesecons_button/init.lua @@ -37,50 +37,67 @@ minetest.register_on_dignode( 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_string="" + + local rules=mesecon:get_rules("button") if node.param2 == 5 then - rules_string="button_z+" + rules=mesecon:rotate_rules_left(rules) end if node.param2 == 3 then - rules_string="button_x+" + rules=mesecon:rotate_rules_right(mesecon:rotate_rules_right(rules)) end if node.param2 == 4 then - rules_string="button_z-" + rules=mesecon:rotate_rules_right(rules) end - if node.param2 == 2 then - rules_string="button_x-" - end - mesecon:receptor_on(pos, rules_string) + mesecon:receptor_on(pos, rules) end end) + minetest.register_abm({ nodenames = {"mesecons_button:button_on"}, - interval = 0.1, + 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}) - local rules_string="" + local rules=mesecon:get_rules("button") + print (rules[1].x) if node.param2 == 5 then - rules_string="button_z+" + rules=mesecon:rotate_rules_left(rules) end + print (rules[1].x) if node.param2 == 3 then - rules_string="button_x+" + rules=mesecon:rotate_rules_right(mesecon:rotate_rules_right(rules)) end + print (rules[1].x) if node.param2 == 4 then - rules_string="button_z-" + rules=mesecon:rotate_rules_right(rules) end - if node.param2 == 2 then - rules_string="button_x-" - end - mesecon:receptor_off(pos, rules_string) + print (rules[1].x) + mesecon:receptor_off(pos, rules) end }) + minetest.register_craft({ output = '"mesecons_button:button_off" 2', recipe = { {'"mesecons:mesecon_off"','"default:stone"'}, } }) + +mesecon:add_rules("button", { +{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}, +{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") diff --git a/mesecons_torch/init.lua b/mesecons_torch/init.lua index dfed5c6..ba3ac65 100644 --- a/mesecons_torch/init.lua +++ b/mesecons_torch/init.lua @@ -46,34 +46,33 @@ end)]] minetest.register_abm({ nodenames = {"mesecons_torch:mesecon_torch_off","mesecons_torch:mesecon_torch_on"}, - interval = 0.1, + interval = 1, 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_string="" + local rules=mesecon:get_rules("mesecontorch") if node.param2 == 4 then - pa.z = -2 - rules_string="mesecontorch_z+" + pa.z = -2 + rules=mesecon:rotate_rules_right(rules) elseif node.param2 == 2 then - pa.x = -2 - rules_string="mesecontorch_x+" + pa.x = -2 + rules=mesecon:rotate_rules_right(mesecon:rotate_rules_right(rules)) --180 degrees elseif node.param2 == 5 then - pa.z = 2 - rules_string="mesecontorch_z-" + pa.z = 2 + rules=mesecon:rotate_rules_left(rules) elseif node.param2 == 3 then - pa.x = 2 - rules_string="mesecontorch_x-" + pa.x = 2 elseif node.param2 == 1 then - pa.y = 2 - rules_string="mesecontorch_y-" + pa.y = 2 + rules=mesecon:rotate_rules_down(rules) elseif node.param2 == 0 then - pa.y = -2 - rules_string="mesecontorch_y+" + 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} - --print("Checking at "..dump(postc).." with "..rules_string) if mesecon:is_power_on(postc,0,0,0)==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}) @@ -98,26 +97,29 @@ minetest.register_on_dignode( minetest.register_on_placenode(function(pos, node, placer) if node.name == "mesecons_torch:mesecon_torch_on" then - local rules_string="" - + local rules=mesecon:get_rules("mesecontorch") if node.param2 == 4 then - rules_string="mesecontorch_z+" - elseif node.param2 == 2 then - rules_string="mesecontorch_x+" - elseif node.param2 == 5 then - rules_string="mesecontorch_z-" - elseif node.param2 == 3 then - rules_string="mesecontorch_x-" - elseif node.param2 == 1 then - rules_string="mesecontorch_y-" - elseif node.param2 == 0 then - rules_string="mesecontorch_y+" - end - - mesecon:receptor_on(pos, rules_string) + 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 + mesecon:receptor_on(pos, rules) end end) +mesecon:add_rules("mesecontorch", +{{x=1, y=0, z=0}, +{x=0, y=0, z=1}, +{x=0, y=0, z=-1}, +{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")