Cleanup and improve piston code

This commit is contained in:
Jeija 2012-12-16 11:58:43 +01:00
parent ae4bd1e21c
commit a1852204fb
5 changed files with 69 additions and 47 deletions

@ -297,9 +297,7 @@ function mesecon:turnon(pos)
mesecon:turnon(np) mesecon:turnon(np)
end end
end end
end elseif mesecon:is_effector(node.name) then
if mesecon:is_effector(node.name) then
mesecon:changesignal(pos, node) mesecon:changesignal(pos, node)
if mesecon:is_effector_off(node.name) then if mesecon:is_effector_off(node.name) then
mesecon:activate(pos, node) mesecon:activate(pos, node)
@ -321,9 +319,7 @@ function mesecon:turnoff(pos)
mesecon:turnoff(np) mesecon:turnoff(np)
end end
end end
end elseif mesecon:is_effector(node.name) then
if mesecon:is_effector(node.name) then
mesecon:changesignal(pos, node) mesecon:changesignal(pos, node)
if mesecon:is_effector_on(node.name) if mesecon:is_effector_on(node.name)
and not mesecon:is_powered(pos) then and not mesecon:is_powered(pos) then

@ -8,6 +8,10 @@ mesecon.on_placenode = function (pos, node)
mesecon:changesignal(pos, node) mesecon:changesignal(pos, node)
mesecon:activate(pos, node) mesecon:activate(pos, node)
end end
elseif mesecon:is_conductor_on(node.name) then
mesecon:swap_node(pos, mesecon:get_conductor_off(node.name))
elseif mesecon:is_effector_on (node.name) then
mesecon:deactivate(pos, node)
end end
end end

@ -6,6 +6,15 @@ function mesecon:swap_node(pos, name)
minetest.env:get_meta(pos):from_table(data) minetest.env:get_meta(pos):from_table(data)
end end
function mesecon:move_node(pos, newpos)
local node = minetest.env:get_node(pos)
local meta = minetest.env:get_meta(pos):to_table()
minetest.env:remove_node(pos)
minetest.env:add_node(newpos, node)
minetest.env:get_meta(pos):from_table(meta)
end
function mesecon:addPosRule(p, r) function mesecon:addPosRule(p, r)
return {x = p.x + r.x, y = p.y + r.y, z = p.z + r.z} return {x = p.x + r.x, y = p.y + r.y, z = p.z + r.z}
end end

@ -216,10 +216,10 @@ function mesecon:update_autoconnect(pos, secondcall, replace_old)
if mesecon:rules_link_anydir(pos, zpympos) then zp = 1 end if mesecon:rules_link_anydir(pos, zpympos) then zp = 1 end
if mesecon:rules_link_anydir(pos, zmympos) then zm = 1 end if mesecon:rules_link_anydir(pos, zmympos) then zm = 1 end
if mesecon:rules_link(pos, xpypos) then xpy = 1 else xpy = 0 end if mesecon:rules_link_anydir(pos, xpypos) then xpy = 1 else xpy = 0 end
if mesecon:rules_link(pos, zpypos) then zpy = 1 else zpy = 0 end if mesecon:rules_link_anydir(pos, zpypos) then zpy = 1 else zpy = 0 end
if mesecon:rules_link(pos, xmypos) then xmy = 1 else xmy = 0 end if mesecon:rules_link_anydir(pos, xmypos) then xmy = 1 else xmy = 0 end
if mesecon:rules_link(pos, zmypos) then zmy = 1 else zmy = 0 end if mesecon:rules_link_anydir(pos, zmypos) then zmy = 1 else zmy = 0 end
if xpy == 1 then xp = 1 end if xpy == 1 then xp = 1 end
if zpy == 1 then zp = 1 end if zpy == 1 then zp = 1 end

@ -1,5 +1,25 @@
--PISTONS --PISTONS
-- Get mesecon rules of pistons
piston_rules =
{{x=0, y=0, z=1}, --everything apart from z- (pusher side)
{x=1, y=0, z=0},
{x=-1, y=0, z=0},
{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}}
local piston_get_rules = function (node)
local rules = piston_rules
for i = 1, node.param2 do
rules = mesecon:rotate_rules_left(rules)
end
return rules
end
--starts the timer to make the piston update to its new state --starts the timer to make the piston update to its new state
local update = function(pos, node) local update = function(pos, node)
local timer = minetest.env:get_node_timer(pos) local timer = minetest.env:get_node_timer(pos)
@ -44,11 +64,12 @@ end
function mesecon:piston_push(pos) function mesecon:piston_push(pos)
local node = minetest.env:get_node(pos) local node = minetest.env:get_node(pos)
local dir = mesecon:piston_get_direction(node) local dir = mesecon:piston_get_direction(node)
pos = {x=pos.x + dir.x, y=pos.y + dir.y, z=pos.z + dir.z} --move to first node being pushed pos = addPosRule(pos, dir) --move to first node being pushed
--determine the number of nodes that need to be pushed --determine the number of nodes that need to be pushed
local count = 0 local count = 0
local checkpos = {x=pos.x, y=pos.y, z=pos.z} --first node being pushed local checkpos = {x=pos.x, y=pos.y, z=pos.z} --first node being pushed
while true do while true do
local checknode = minetest.env:get_node(checkpos) local checknode = minetest.env:get_node(checkpos)
@ -69,10 +90,13 @@ function mesecon:piston_push(pos)
return return
end end
checkpos.x, checkpos.y, checkpos.z = checkpos.x + dir.x, checkpos.y + dir.y, checkpos.z + dir.z checkpos = addPosRule(checkpos, dir)
end end
local checknode = minetest.env:get_node(pos) local thisnode = minetest.env:get_node(pos)
minetest.env:remove_node(pos)
mesecon.on_dignode(pos, thisnode)
local nextnode
--add pusher --add pusher
if node.name == "mesecons_pistons:piston_normal" then if node.name == "mesecons_pistons:piston_normal" then
@ -91,25 +115,15 @@ function mesecon:piston_push(pos)
--move nodes forward --move nodes forward
for i = 1, count do for i = 1, count do
pos.x, pos.y, pos.z = pos.x + dir.x, pos.y + dir.y, pos.z + dir.z --move to the next node pos = addPosRule(pos, dir) --move to the next node
--check for conductor --wip: not sure if still needed nextnode = minetest.env:get_node(pos)
if mesecon:is_conductor_on(checknode.name) then minetest.env:remove_node(pos)
checknode.name = mesecon:get_conductor_off(checknode.name) mesecon.on_dignode(pos, thisnode)
end minetest.env:add_node(pos, thisnode)
mesecon.on_placenode(pos, thisnode)
--move the node forward thisnode = nextnode
local nextnode = minetest.env:get_node(pos)
minetest.env:add_node(pos, checknode)
checknode = nextnode
end
--update nodes
for i = 1, count do
mesecon:updatenode(pos)
nodeupdate(pos) nodeupdate(pos)
pos.x, pos.y, pos.z = pos.x - dir.x, pos.y - dir.y, pos.z - dir.z --move to the previous node
end end
end end
@ -140,23 +154,17 @@ function mesecon:piston_pull(pos)
--retract piston --retract piston
minetest.env:remove_node(pos) --remove pusher minetest.env:remove_node(pos) --remove pusher
if node.name == "mesecons_pistons:piston_sticky" if minetest.registered_nodes[node.name].is_sticky_piston then --retract block if piston is sticky
or node.name == "mesecons_pistons:piston_up_sticky" local retractpos = addPosRule(pos, dir) --move to the node to be retracted
or node.name == "mesecons_pistons:piston_down_sticky" then --retract block if piston is sticky local retractnode = minetest.env:get_node(retractpos)
local checkpos = {x=pos.x + dir.x, y=pos.y + dir.y, z=pos.z + dir.z} --move to the node to be retracted if minetest.registered_nodes[retractnode.name].liquidtype == "none"
checknode = minetest.env:get_node(checkpos) and not mesecon:is_mvps_stopper(retractnode.name) then
if checknode.name ~= "air" mesecon:move_node(retractpos, pos)
and checknode.name ~= "ignore" mesecon.on_dignode(retractpos, retractnode)
and minetest.registered_nodes[checknode.name].liquidtype == "none" mesecon.on_placenode(pos, retractnode)
and not mesecon:is_mvps_stopper(checknode.name) then nodeupdate(pos)
minetest.env:add_node(pos, checknode)
minetest.env:remove_node(checkpos)
mesecon:updatenode(checkpos)
nodeupdate(checkpos)
end end
end end
mesecon:updatenode(pos)
nodeupdate(pos)
end end
--push direction of a piston --push direction of a piston
@ -208,7 +216,8 @@ minetest.register_node("mesecons_pistons:piston_normal", {
return minetest.item_place(itemstack, placer, pointed_thing) --place piston normally return minetest.item_place(itemstack, placer, pointed_thing) --place piston normally
end, end,
mesecons = {effector={ mesecons = {effector={
action_change = update action_change = update,
rules = piston_get_rules
}} }}
}) })
@ -219,6 +228,7 @@ minetest.register_node("mesecons_pistons:piston_sticky", {
paramtype2 = "facedir", paramtype2 = "facedir",
after_destruct = destruct, after_destruct = destruct,
on_timer = timer, on_timer = timer,
is_sticky_piston = true,
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
if pointed_thing.type ~= "node" then --can be placed only on nodes if pointed_thing.type ~= "node" then --can be placed only on nodes
return itemstack return itemstack
@ -243,7 +253,8 @@ minetest.register_node("mesecons_pistons:piston_sticky", {
return minetest.item_place(itemstack, placer, pointed_thing) --place piston normally return minetest.item_place(itemstack, placer, pointed_thing) --place piston normally
end, end,
mesecons = {effector={ mesecons = {effector={
action_change = update action_change = update,
rules = piston_get_rules
}} }}
}) })
@ -307,6 +318,7 @@ minetest.register_node("mesecons_pistons:piston_up_normal", {
groups = {cracky=3, mesecon=2}, groups = {cracky=3, mesecon=2},
after_destruct = destruct, after_destruct = destruct,
on_timer = timer, on_timer = timer,
is_sticky_piston = true,
mesecons = {effector={ mesecons = {effector={
action_change = update action_change = update
}}, }},
@ -393,6 +405,7 @@ minetest.register_node("mesecons_pistons:piston_down_sticky", {
groups = {cracky=3, mesecon=2}, groups = {cracky=3, mesecon=2},
after_destruct = destruct, after_destruct = destruct,
on_timer = timer, on_timer = timer,
is_sticky_piston = true,
mesecons = {effector={ mesecons = {effector={
action_change = update action_change = update
}}, }},