Simplify rules System

This commit is contained in:
Jeija 2012-03-29 11:35:23 +02:00
parent d6c54cf76b
commit 8e0b2a2b11
3 changed files with 157 additions and 183 deletions

View File

@ -63,13 +63,13 @@
-- INCLUDE SETTINGS -- INCLUDE SETTINGS
dofile(minetest.get_modpath("mesecons").."/settings.lua") dofile(minetest.get_modpath("mesecons").."/settings.lua")
-- PUBLIC VARIABLES -- PUBLIC VARIABLES
mesecon={} -- contains all functions and all global variables mesecon={} -- contains all functions and all global variables
mesecon.actions_on={} -- Saves registered function callbacks for mesecon on mesecon.actions_on={} -- Saves registered function callbacks for mesecon on
mesecon.actions_off={} -- Saves registered function callbacks for mesecon off mesecon.actions_off={} -- Saves registered function callbacks for mesecon off
mesecon.pwr_srcs={} -- this is public for now mesecon.pwr_srcs={}
mesecon.pwr_srcs_off={} -- this is public for now mesecon.pwr_srcs_off={}
mesecon.rules={}
-- MESECONS -- MESECONS
@ -136,7 +136,7 @@ end
function mesecon:turnon(p, x, y, z, firstcall, rules) function mesecon:turnon(p, x, y, z, firstcall, rules)
if rules==nil then if rules==nil then
rules="default" rules=mesecon:get_rules("default")
end end
local lpos = {} local lpos = {}
lpos.x=p.x+x lpos.x=p.x+x
@ -152,10 +152,9 @@ function mesecon:turnon(p, x, y, z, firstcall, rules)
nodeupdate(lpos) nodeupdate(lpos)
end end
if node.name == "mesecons:mesecon_off" or firstcall then if node.name == "mesecons:mesecon_off" or firstcall then
local rules=mesecon:get_rules(rules)
local i=1 local i=1
while rules[i]~=nil do 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 i=i+1
end end
end end
@ -163,7 +162,7 @@ end
function mesecon:turnoff(pos, x, y, z, firstcall, rules) function mesecon:turnoff(pos, x, y, z, firstcall, rules)
if rules==nil then if rules==nil then
rules="default" rules=mesecon:get_rules("default")
end end
local lpos = {} local lpos = {}
lpos.x=pos.x+x 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 node.name == "mesecons:mesecon_on" or firstcall then
if connected == 0 then if connected == 0 then
local rules=mesecon:get_rules(rules)
local i=1 local i=1
while rules[i]~=nil do 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 i=i+1
end end
end end
@ -250,7 +248,7 @@ function mesecon:check_if_turnon(pos)
local getactivated=0 local getactivated=0
local rules=mesecon:get_rules("default") local rules=mesecon:get_rules("default")
local i=1 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) getactivated=getactivated+mesecon:is_power_on(pos, rules[i].x, rules[i].y, rules[i].z)
i=i+1 i=i+1
end end
@ -388,137 +386,94 @@ mesecon:register_on_signal_off(function(pos, node)
end end
end) end)
-- mesecon rules function mesecon:add_rules(name, rules)
function mesecon:get_rules(name) local i=0
local rules={} while mesecon.rules[i]~=nil do
rules[0]="dummy" i=i+1
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})
end end
mesecon.rules[i]={}
mesecon.rules[i].name=name
mesecon.rules[i].rules=rules
end
if name=="button_x+" or name=="button_x-" function mesecon:get_rules(name)
or name=="button_z-" or name=="button_z+" then --Is any button local i=0
table.insert(rules, {x=0, y=0, z=-1}) while mesecon.rules[i]~=nil do
table.insert(rules, {x=1, y=0, z=0}) if mesecon.rules[i].name==name then
table.insert(rules, {x=-1, y=0, z=0}) return mesecon.rules[i].rules
table.insert(rules, {x=0, y=0, z=1}) end
table.insert(rules, {x=1, y=1, z=0}) i=i+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=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})
end 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 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!") print("[MESEcons] Loaded!")

View File

@ -37,50 +37,67 @@ minetest.register_on_dignode(
minetest.register_on_punchnode(function(pos, node, puncher) minetest.register_on_punchnode(function(pos, node, puncher)
if node.name == "mesecons_button:button_off" then if node.name == "mesecons_button:button_off" then
minetest.env:add_node(pos, {name="mesecons_button:button_on",param2=node.param2}) 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 if node.param2 == 5 then
rules_string="button_z+" rules=mesecon:rotate_rules_left(rules)
end end
if node.param2 == 3 then if node.param2 == 3 then
rules_string="button_x+" rules=mesecon:rotate_rules_right(mesecon:rotate_rules_right(rules))
end end
if node.param2 == 4 then if node.param2 == 4 then
rules_string="button_z-" rules=mesecon:rotate_rules_right(rules)
end end
if node.param2 == 2 then mesecon:receptor_on(pos, rules)
rules_string="button_x-"
end
mesecon:receptor_on(pos, rules_string)
end end
end) end)
minetest.register_abm({ minetest.register_abm({
nodenames = {"mesecons_button:button_on"}, nodenames = {"mesecons_button:button_on"},
interval = 0.1, interval = 1,
chance = 1, chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider) action = function(pos, node, active_object_count, active_object_count_wider)
minetest.env:add_node(pos, {name="mesecons_button:button_off",param2=node.param2}) 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 if node.param2 == 5 then
rules_string="button_z+" rules=mesecon:rotate_rules_left(rules)
end end
print (rules[1].x)
if node.param2 == 3 then if node.param2 == 3 then
rules_string="button_x+" rules=mesecon:rotate_rules_right(mesecon:rotate_rules_right(rules))
end end
print (rules[1].x)
if node.param2 == 4 then if node.param2 == 4 then
rules_string="button_z-" rules=mesecon:rotate_rules_right(rules)
end end
if node.param2 == 2 then print (rules[1].x)
rules_string="button_x-" mesecon:receptor_off(pos, rules)
end
mesecon:receptor_off(pos, rules_string)
end end
}) })
minetest.register_craft({ minetest.register_craft({
output = '"mesecons_button:button_off" 2', output = '"mesecons_button:button_off" 2',
recipe = { recipe = {
{'"mesecons:mesecon_off"','"default:stone"'}, {'"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("mesecons_button:button")
mesecon:add_receptor_node_off("mesecons_button:button_off") mesecon:add_receptor_node_off("mesecons_button:button_off")

View File

@ -46,34 +46,33 @@ end)]]
minetest.register_abm({ minetest.register_abm({
nodenames = {"mesecons_torch:mesecon_torch_off","mesecons_torch:mesecon_torch_on"}, nodenames = {"mesecons_torch:mesecon_torch_off","mesecons_torch:mesecon_torch_on"},
interval = 0.1, interval = 1,
chance = 1, chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider) action = function(pos, node, active_object_count, active_object_count_wider)
local pa = {x=0, y=0, z=0} local pa = {x=0, y=0, z=0}
--pa.y = 1 --pa.y = 1
local rules_string="" local rules=mesecon:get_rules("mesecontorch")
if node.param2 == 4 then if node.param2 == 4 then
pa.z = -2 pa.z = -2
rules_string="mesecontorch_z+" rules=mesecon:rotate_rules_right(rules)
elseif node.param2 == 2 then elseif node.param2 == 2 then
pa.x = -2 pa.x = -2
rules_string="mesecontorch_x+" 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 pa.z = 2
rules_string="mesecontorch_z-" rules=mesecon:rotate_rules_left(rules)
elseif node.param2 == 3 then elseif node.param2 == 3 then
pa.x = 2 pa.x = 2
rules_string="mesecontorch_x-"
elseif node.param2 == 1 then elseif node.param2 == 1 then
pa.y = 2 pa.y = 2
rules_string="mesecontorch_y-" rules=mesecon:rotate_rules_down(rules)
elseif node.param2 == 0 then elseif node.param2 == 0 then
pa.y = -2 pa.y = -2
rules_string="mesecontorch_y+" rules=mesecon:rotate_rules_up(rules)
end end
local postc = {x=pos.x-pa.x, y=pos.y-pa.y, z=pos.z-pa.z} 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 mesecon:is_power_on(postc,0,0,0)==1 then
if node.name ~= "mesecons_torch:mesecon_torch_off" then if node.name ~= "mesecons_torch:mesecon_torch_off" then
minetest.env:add_node(pos, {name="mesecons_torch:mesecon_torch_off",param2=node.param2}) 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) minetest.register_on_placenode(function(pos, node, placer)
if node.name == "mesecons_torch:mesecon_torch_on" then if node.name == "mesecons_torch:mesecon_torch_on" then
local rules_string="" local rules=mesecon:get_rules("mesecontorch")
if node.param2 == 4 then if node.param2 == 4 then
rules_string="mesecontorch_z+" rules=mesecon:rotate_rules_right(rules)
elseif node.param2 == 2 then elseif node.param2 == 2 then
rules_string="mesecontorch_x+" rules=mesecon:rotate_rules_right(mesecon:rotate_rules_right(rules)) --180 degrees
elseif node.param2 == 5 then elseif node.param2 == 5 then
rules_string="mesecontorch_z-" rules=mesecon:rotate_rules_left(rules)
elseif node.param2 == 3 then elseif node.param2 == 1 then
rules_string="mesecontorch_x-" rules=mesecon:rotate_rules_down(rules)
elseif node.param2 == 1 then elseif node.param2 == 0 then
rules_string="mesecontorch_y-" rules=mesecon:rotate_rules_up(rules)
elseif node.param2 == 0 then end
rules_string="mesecontorch_y+" mesecon:receptor_on(pos, rules)
end
mesecon:receptor_on(pos, rules_string)
end end
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("mesecons_torch:mesecon_torch_on")
mesecon:add_receptor_node_off("mesecons_torch:mesecon_torch_off") mesecon:add_receptor_node_off("mesecons_torch:mesecon_torch_off")