From db26adca250a4a99304a363553e7a09c2b3a4702 Mon Sep 17 00:00:00 2001 From: Anthony Zhang Date: Sat, 16 Jun 2012 12:53:14 -0400 Subject: [PATCH] Fix crash in sticky movestones (thanks MarkTraceur), cleanup movestoen code significantly. --- mesecons_movestones/init.lua | 150 ++++++++++++++--------------------- 1 file changed, 61 insertions(+), 89 deletions(-) diff --git a/mesecons_movestones/init.lua b/mesecons_movestones/init.lua index 8d82f40..0e497d2 100644 --- a/mesecons_movestones/init.lua +++ b/mesecons_movestones/init.lua @@ -2,41 +2,35 @@ function mesecon:get_movestone_direction(pos) getactivated=0 - local direction = {x=0, y=0, z=0} local lpos - local getactivated=0 local rules=mesecon:get_rules("movestone") lpos={x=pos.x+1, y=pos.y, z=pos.z} for n=1, 3 do if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then - direction.z=-1 - return direction + return {x=0, y=0, z=-1} end end lpos={x=pos.x-1, y=pos.y, z=pos.z} for n=4, 6 do if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then - direction.z=1 - return direction + return {x=0, y=0, z=1} end end lpos={x=pos.x, y=pos.y, z=pos.z+1} for n=7, 9 do if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then - direction.x=-1 - return direction + return {x=-1, y=0, z=0} end end lpos={x=pos.x, y=pos.y, z=pos.z-1} for n=10, 12 do if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then - direction.x=1 - return direction + return {x=1, y=0, z=0} end end end @@ -67,49 +61,29 @@ minetest.register_entity("mesecons_movestones:movestone_entity", { on_step = function(self, dtime) local pos = self.object:getpos() - local colp = pos - local velocity={} - local direction=mesecon:get_movestone_direction(colp) + local direction=mesecon:get_movestone_direction(pos) - --colp.x=colp.x-(direction.x/2.01) - --colp.y=colp.y-direction.y - --colp.z=colp.z-(direction.z/2.01) - - if not direction - or (minetest.env:get_node_or_nil(pos).name ~="air" - and minetest.env:get_node_or_nil(pos).name ~= nil) then + if not direction then minetest.env:add_node(pos, {name="mesecons_movestones:movestone"}) self.object:remove() return - end - --if not mesecon:check_if_turnon(colp) then - -- minetest.env:add_node(pos, {name="mesecons_movestones:movestone"}) - -- self.object:remove() - -- return - --end + end - velocity.x=direction.x*3 - velocity.y=direction.y*3 - velocity.z=direction.z*3 + self.object:setvelocity({x=direction.x*3, y=direction.y*3, z=direction.z*3}) - self.object:setvelocity(velocity) - - local np = {x=pos.x+direction.x, y=pos.y+direction.y, z=pos.z+direction.z} - local coln = minetest.env:get_node(np) - if coln.name ~= "air" and coln.name ~="water" then - local thisp= {x=pos.x, y=pos.y, z=pos.z} - local thisnode=minetest.env:get_node(thisp) - local nextnode={} - minetest.env:dig_node(thisp) + local lnode = minetest.env:get_node(pos) + if lnode.name ~= "ignore" and lnode.name ~= "air" and lnode.name ~= "default:water" and lnode.name ~= "default:water_flowing" then + local newnode repeat - thisp.x=thisp.x+direction.x - thisp.y=thisp.y+direction.y - thisp.z=thisp.z+direction.z - nextnode=minetest.env:get_node(thisp) - minetest.env:place_node(thisp, {name=thisnode.name}) - nodeupdate(thisp) - thisnode=nextnode - until thisnode.name=="air" or thisnode.name=="ignore" or thisnode.name=="default:water" or thisnode.name=="default:water_flowing" + minetest.env:remove_node(pos) + pos.x=pos.x+direction.x + pos.y=pos.y+direction.y + pos.z=pos.z+direction.z + newnode = {name=lnode.name} + lnode = minetest.env:get_node(pos) + minetest.env:add_node(pos, newnode) + nodeupdate(pos) + until lnode.name == "ignore" or lnode.name == "air" or lnode.name == "default:water" or lnode.name == "default:water_flowing" end end }) @@ -125,7 +99,7 @@ minetest.register_craft({ mesecon:register_on_signal_on(function (pos, node) if node.name=="mesecons_movestones:movestone" then - local direction=mesecon:get_movestone_direction({x=pos.x, y=pos.y, z=pos.z}) + local direction=mesecon:get_movestone_direction(pos) if not direction then return end local checknode={} local collpos={x=pos.x, y=pos.y, z=pos.z} @@ -182,7 +156,6 @@ minetest.register_entity("mesecons_movestones:sticky_movestone_entity", { local pos = self.object:getpos() local colp = pos local direction=mesecon:get_movestone_direction(colp) - local velocity={x=direction.x*3, y=direction.y*3, z=direction.z*3} if not direction then --or (minetest.env:get_node_or_nil(pos).name ~="air" @@ -190,57 +163,56 @@ minetest.register_entity("mesecons_movestones:sticky_movestone_entity", { minetest.env:add_node(pos, {name="mesecons_movestones:sticky_movestone"}) self.object:remove() return - end - - self.object:setvelocity(velocity) - - local np = {x=pos.x+direction.x, y=pos.y+direction.y, z=pos.z+direction.z} - local coln = minetest.env:get_node(np) - if coln.name ~= "air" and coln.name ~="water" then - local thisp= {x=pos.x, y=pos.y, z=pos.z} - local thisnode=minetest.env:get_node(thisp) - local nextnode={} - minetest.env:dig_node(thisp) - repeat - thisp.x=thisp.x+direction.x - thisp.y=thisp.y+direction.y - thisp.z=thisp.z+direction.z - nextnode=minetest.env:get_node(thisp) - minetest.env:place_node(thisp, {name=thisnode.name}) - nodeupdate(thisp) - thisnode=nextnode - until thisnode.name=="air" or thisnode.name=="ignore" or thisnode.name=="default:water" or thisnode.name=="default:water_flowing" end - --STICKY: - local np1 = {x=pos.x-direction.x*0.5, y=pos.y-direction.y*0.5, z=pos.z-direction.z*0.5} -- 1 away - local coln1 = minetest.env:get_node(np1) - local np2 = {x=pos.x-direction.x*1.5, y=pos.y-direction.y*1.5, z=pos.z-direction.z*1.5} -- 2 away - local coln2 = minetest.env:get_node(np2) + self.object:setvelocity({x=direction.x*3, y=direction.y*3, z=direction.z*3}) - if (coln1.name == "air" or coln1.name =="water") and (coln2.name~="air" and coln2.name ~= water) then - thisp= np2 - local newpos={} - local oldpos={} + pos.x=pos.x+direction.x + pos.y=pos.y+direction.y + pos.z=pos.z+direction.z + + local lpos = {x=pos.x, y=pos.y, z=pos.z} + local lnode = minetest.env:get_node(lpos) + if lnode.name ~= "ignore" and lnode.name ~= "air" and lnode.name ~= "default:water" and lnode.name ~= "default:water_flowing" then + local newnode repeat - newpos.x=thisp.x+direction.x - newpos.y=thisp.y+direction.y - newpos.z=thisp.z+direction.z - minetest.env:place_node(newpos, {name=minetest.env:get_node(thisp).name}) - nodeupdate(newpos) - oldpos={x=thisp.x, y=thisp.y, z=thisp.z} - thisp.x=thisp.x-direction.x - thisp.y=thisp.y-direction.y - thisp.z=thisp.z-direction.z - until minetest.env:get_node(thisp).name=="air" or minetest.env:get_node(thisp).name=="ignore" or minetest.env:get_node(thisp).name=="default:water" or minetest.env:get_node(thisp).name=="default:water_flowing" - minetest.env:dig_node(oldpos) + minetest.env:remove_node(lpos) + lpos.x=lpos.x+direction.x + lpos.y=lpos.y+direction.y + lpos.z=lpos.z+direction.z + newnode = {name=lnode.name} + lnode = minetest.env:get_node(lpos) + minetest.env:add_node(lpos, newnode) + nodeupdate(lpos) + until lnode.name == "ignore" or lnode.name == "air" or lnode.name == "default:water" or lnode.name == "default:water_flowing" end + + --STICKY + local lpos = {x=pos.x-direction.x, y=pos.y-direction.y, z=pos.z-direction.z} -- 1 away + local lnode = minetest.env:get_node(lpos) + local lpos2 = {x=pos.x-direction.x*2, y=pos.y-direction.y*2, z=pos.z-direction.z*2} -- 2 away + local lnode2 = minetest.env:get_node(lpos2) + + if lnode.name ~= "ignore" and lnode.name ~= "air" and lnode.name ~= "default:water" and lnode.name ~= "default:water_flowing" then return end + if lnode2.name == "ignore" or lnode2.name == "air" or lnode2.name == "default:water" or lnode2.name == "default:water_flowing" then return end + + local oldpos = {x=lpos2.x+direction.x, y=lpos2.y+direction.y, z=lpos2.z+direction.z} + repeat + minetest.env:add_node(oldpos, {name=minetest.env:get_node(lpos2).name}) + nodeupdate(oldpos) + oldpos = {x=lpos2.x, y=lpos2.y, z=lpos2.z} + lpos2.x = lpos2.x-direction.x + lpos2.y = lpos2.y-direction.y + lpos2.z = lpos2.z-direction.z + lnode = minetest.env:get_node(lpos2) + until lnode.name=="air" or lnode.name=="ignore" or lnode.name=="default:water" or lnode.name=="default:water_flowing" + minetest.env:remove_node(oldpos) end }) mesecon:register_on_signal_on(function (pos, node) if node.name=="mesecons_movestones:sticky_movestone" then - local direction=mesecon:get_movestone_direction({x=pos.x, y=pos.y, z=pos.z}) + local direction=mesecon:get_movestone_direction(pos) if not direction then return end local checknode={} local collpos={x=pos.x, y=pos.y, z=pos.z}