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

View File

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

View File

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