Improve rules and rule handling (#370)

This commit is contained in:
DS 2017-10-08 01:39:02 +02:00 committed by Vitaliy
parent 905260c8db
commit 37eb7f00e8
5 changed files with 141 additions and 110 deletions

View File

@ -548,52 +548,3 @@ function mesecon.is_powered(pos, rule)
if (#sourcepos == 0) then return false if (#sourcepos == 0) then return false
else return sourcepos end else return sourcepos end
end end
--Rules rotation Functions:
function mesecon.rotate_rules_right(rules)
local nr = {}
for i, rule in ipairs(rules) do
table.insert(nr, {
x = -rule.z,
y = rule.y,
z = rule.x,
name = rule.name})
end
return nr
end
function mesecon.rotate_rules_left(rules)
local nr = {}
for i, rule in ipairs(rules) do
table.insert(nr, {
x = rule.z,
y = rule.y,
z = -rule.x,
name = rule.name})
end
return nr
end
function mesecon.rotate_rules_down(rules)
local nr = {}
for i, rule in ipairs(rules) do
table.insert(nr, {
x = -rule.y,
y = rule.x,
z = rule.z,
name = rule.name})
end
return nr
end
function mesecon.rotate_rules_up(rules)
local nr = {}
for i, rule in ipairs(rules) do
table.insert(nr, {
x = rule.y,
y = -rule.x,
z = rule.z,
name = rule.name})
end
return nr
end

View File

@ -1,61 +1,85 @@
mesecon.rules = {} mesecon.rules = {}
mesecon.state = {} mesecon.state = {}
mesecon.rules.default = mesecon.rules.default = {
{{x=0, y=0, z=-1}, {x = 0, y = 0, z = -1},
{x=1, y=0, z=0}, {x = 1, y = 0, z = 0},
{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}}
mesecon.rules.pplate = mesecon.mergetable(mesecon.rules.default, {{x=0, y=-2, z=0}})
mesecon.rules.buttonlike =
{{x = 1, y = 0, z = 0},
{x = 1, y = 1, z = 0},
{x = 1, y =-1, z = 0},
{x = 1, y =-1, z = 1},
{x = 1, y =-1, z =-1},
{x = 2, y = 0, z = 0}}
mesecon.rules.flat =
{{x = 1, y = 0, z = 0},
{x =-1, y = 0, z = 0},
{x = 0, y = 0, z = 1}, {x = 0, y = 0, z = 1},
{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},
}
mesecon.rules.alldirs = mesecon.rules.floor = mesecon.mergetable(mesecon.rules.default, {{x = 0, y = -1, z = 0}})
{{x= 1, y= 0, z= 0},
{x=-1, y= 0, z= 0}, mesecon.rules.pplate = mesecon.mergetable(mesecon.rules.floor, {{x = 0, y = -2, z = 0}})
{x= 0, y= 1, z= 0},
{x= 0, y=-1, z= 0}, mesecon.rules.buttonlike = {
{x= 0, y= 0, z= 1}, {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 = 1},
{x = 1, y = -1, z = -1},
{x = 2, y = 0, z = 0},
}
mesecon.rules.flat = {
{x = 1, y = 0, z = 0},
{x = -1, y = 0, z = 0},
{x = 0, y = 0, z = 1},
{x = 0, y = 0, z = -1},
}
mesecon.rules.alldirs = {
{x = 1, y = 0, z = 0},
{x = -1, y = 0, z = 0},
{x = 0, y = 1, z = 0},
{x = 0, y = -1, z = 0},
{x = 0, y = 0, z = 1},
{x = 0, y = 0, z = -1},
}
local rules_wallmounted = {
xp = mesecon.rotate_rules_down(mesecon.rules.floor),
xn = mesecon.rotate_rules_up(mesecon.rules.floor),
yp = mesecon.rotate_rules_up(mesecon.rotate_rules_up(mesecon.rules.floor)),
yn = mesecon.rules.floor,
zp = mesecon.rotate_rules_left(mesecon.rotate_rules_up(mesecon.rules.floor)),
zn = mesecon.rotate_rules_right(mesecon.rotate_rules_up(mesecon.rules.floor)),
}
local rules_buttonlike = {
xp = mesecon.rules.buttonlike,
xn = mesecon.rotate_rules_right(mesecon.rotate_rules_right(mesecon.rules.buttonlike)),
yp = mesecon.rotate_rules_down(mesecon.rules.buttonlike),
yn = mesecon.rotate_rules_up(mesecon.rules.buttonlike),
zp = mesecon.rotate_rules_right(mesecon.rules.buttonlike),
zn = mesecon.rotate_rules_left(mesecon.rules.buttonlike),
}
local function rules_from_dir(ruleset, dir)
if dir.x == 1 then return ruleset.xp end
if dir.y == 1 then return ruleset.yp end
if dir.z == 1 then return ruleset.zp end
if dir.x == -1 then return ruleset.xn end
if dir.y == -1 then return ruleset.yn end
if dir.Z == -1 then return ruleset.zn end
end
mesecon.rules.wallmounted_get = function(node)
local dir = minetest.wallmounted_to_dir(node.param2)
return rules_from_dir(rules_wallmounted, dir)
end
mesecon.rules.buttonlike_get = function(node) mesecon.rules.buttonlike_get = function(node)
local rules = mesecon.rules.buttonlike
local dir = minetest.facedir_to_dir(node.param2) local dir = minetest.facedir_to_dir(node.param2)
if dir.x == 1 then return rules_from_dir(rules_buttonlike, dir)
-- No action needed
elseif dir.z == -1 then
rules=mesecon.rotate_rules_left(rules)
elseif dir.x == -1 then
rules=mesecon.rotate_rules_right(mesecon.rotate_rules_right(rules))
elseif dir.z == 1 then
rules=mesecon.rotate_rules_right(rules)
elseif dir.y == -1 then
rules=mesecon.rotate_rules_up(rules)
elseif dir.y == 1 then
rules=mesecon.rotate_rules_down(rules)
end
return rules
end end
mesecon.state.on = "on" mesecon.state.on = "on"

View File

@ -6,6 +6,56 @@ function mesecon.move_node(pos, newpos)
minetest.get_meta(pos):from_table(meta) minetest.get_meta(pos):from_table(meta)
end end
-- Rules rotation Functions:
function mesecon.rotate_rules_right(rules)
local nr = {}
for i, rule in ipairs(rules) do
table.insert(nr, {
x = -rule.z,
y = rule.y,
z = rule.x,
name = rule.name})
end
return nr
end
function mesecon.rotate_rules_left(rules)
local nr = {}
for i, rule in ipairs(rules) do
table.insert(nr, {
x = rule.z,
y = rule.y,
z = -rule.x,
name = rule.name})
end
return nr
end
function mesecon.rotate_rules_down(rules)
local nr = {}
for i, rule in ipairs(rules) do
table.insert(nr, {
x = -rule.y,
y = rule.x,
z = rule.z,
name = rule.name})
end
return nr
end
function mesecon.rotate_rules_up(rules)
local nr = {}
for i, rule in ipairs(rules) do
table.insert(nr, {
x = rule.y,
y = -rule.x,
z = rule.z,
name = rule.name})
end
return nr
end
--
function mesecon.flattenrules(allrules) function mesecon.flattenrules(allrules)
--[[ --[[
{ {

View File

@ -17,16 +17,17 @@ minetest.register_node("mesecons_lamp:lamp_on", {
legacy_wallmounted = true, legacy_wallmounted = true,
sunlight_propagates = true, sunlight_propagates = true,
walkable = true, walkable = true,
light_source = default.LIGHT_MAX, light_source = minetest.LIGHT_MAX,
node_box = mesecon_lamp_box, node_box = mesecon_lamp_box,
selection_box = mesecon_lamp_box, selection_box = mesecon_lamp_box,
groups = {dig_immediate=3,not_in_creative_inventory=1, mesecon_effector_on = 1}, groups = {dig_immediate = 3,not_in_creative_inventory = 1, mesecon_effector_on = 1},
drop="mesecons_lamp:lamp_off 1", drop = "mesecons_lamp:lamp_off 1",
sounds = default.node_sound_glass_defaults(), sounds = default.node_sound_glass_defaults(),
mesecons = {effector = { mesecons = {effector = {
action_off = function (pos, node) action_off = function (pos, node)
minetest.swap_node(pos, {name = "mesecons_lamp:lamp_off", param2 = node.param2}) minetest.swap_node(pos, {name = "mesecons_lamp:lamp_off", param2 = node.param2})
end end,
rules = mesecon.rules.wallmounted_get,
}}, }},
on_blast = mesecon.on_blastnode, on_blast = mesecon.on_blastnode,
}) })
@ -43,12 +44,13 @@ minetest.register_node("mesecons_lamp:lamp_off", {
node_box = mesecon_lamp_box, node_box = mesecon_lamp_box,
selection_box = mesecon_lamp_box, selection_box = mesecon_lamp_box,
groups = {dig_immediate=3, mesecon_receptor_off = 1, mesecon_effector_off = 1}, groups = {dig_immediate=3, mesecon_receptor_off = 1, mesecon_effector_off = 1},
description="Mesecon Lamp", description = "Mesecon Lamp",
sounds = default.node_sound_glass_defaults(), sounds = default.node_sound_glass_defaults(),
mesecons = {effector = { mesecons = {effector = {
action_on = function (pos, node) action_on = function (pos, node)
minetest.swap_node(pos, {name = "mesecons_lamp:lamp_on", param2 = node.param2}) minetest.swap_node(pos, {name = "mesecons_lamp:lamp_on", param2 = node.param2})
end end,
rules = mesecon.rules.wallmounted_get,
}}, }},
on_blast = mesecon.on_blastnode, on_blast = mesecon.on_blastnode,
}) })

View File

@ -24,7 +24,8 @@ minetest.register_node("mesecons_solarpanel:solar_panel_on", {
groups = {dig_immediate=3, not_in_creative_inventory = 1}, groups = {dig_immediate=3, not_in_creative_inventory = 1},
sounds = default.node_sound_glass_defaults(), sounds = default.node_sound_glass_defaults(),
mesecons = {receptor = { mesecons = {receptor = {
state = mesecon.state.on state = mesecon.state.on,
rules = mesecon.rules.wallmounted_get,
}}, }},
on_blast = mesecon.on_blastnode, on_blast = mesecon.on_blastnode,
}) })
@ -52,10 +53,11 @@ minetest.register_node("mesecons_solarpanel:solar_panel_off", {
wall_side = { -8/16, -7/16, -7/16, -7/16, 7/16, 7/16 }, wall_side = { -8/16, -7/16, -7/16, -7/16, 7/16, 7/16 },
}, },
groups = {dig_immediate=3}, groups = {dig_immediate=3},
description="Solar Panel", description = "Solar Panel",
sounds = default.node_sound_glass_defaults(), sounds = default.node_sound_glass_defaults(),
mesecons = {receptor = { mesecons = {receptor = {
state = mesecon.state.off state = mesecon.state.off,
rules = mesecon.rules.wallmounted_get,
}}, }},
on_blast = mesecon.on_blastnode, on_blast = mesecon.on_blastnode,
}) })
@ -76,8 +78,9 @@ minetest.register_abm(
local light = minetest.get_node_light(pos, nil) local light = minetest.get_node_light(pos, nil)
if light >= 12 then if light >= 12 then
minetest.set_node(pos, {name="mesecons_solarpanel:solar_panel_on", param2=node.param2}) node.name = "mesecons_solarpanel:solar_panel_on"
mesecon.receptor_on(pos) minetest.swap_node(pos, node)
mesecon.receptor_on(pos, mesecon.rules.wallmounted_get(node))
end end
end, end,
}) })
@ -90,8 +93,9 @@ minetest.register_abm(
local light = minetest.get_node_light(pos, nil) local light = minetest.get_node_light(pos, nil)
if light < 12 then if light < 12 then
minetest.set_node(pos, {name="mesecons_solarpanel:solar_panel_off", param2=node.param2}) node.name = "mesecons_solarpanel:solar_panel_off"
mesecon.receptor_off(pos) minetest.swap_node(pos, node)
mesecon.receptor_off(pos, mesecon.rules.wallmounted_get(node))
end end
end, end,
}) })