Fix crash in sticky movestones (thanks MarkTraceur), cleanup movestoen code significantly.

This commit is contained in:
Anthony Zhang 2012-06-16 12:53:14 -04:00
parent 3c7d2b2e25
commit db26adca25

View File

@ -2,41 +2,35 @@
function mesecon:get_movestone_direction(pos) function mesecon:get_movestone_direction(pos)
getactivated=0 getactivated=0
local direction = {x=0, y=0, z=0}
local lpos local lpos
local getactivated=0 local getactivated=0
local rules=mesecon:get_rules("movestone") local rules=mesecon:get_rules("movestone")
lpos={x=pos.x+1, y=pos.y, z=pos.z} lpos={x=pos.x+1, y=pos.y, z=pos.z}
for n=1, 3 do for n=1, 3 do
if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then
direction.z=-1 return {x=0, y=0, z=-1}
return direction
end end
end end
lpos={x=pos.x-1, y=pos.y, z=pos.z} lpos={x=pos.x-1, y=pos.y, z=pos.z}
for n=4, 6 do for n=4, 6 do
if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then
direction.z=1 return {x=0, y=0, z=1}
return direction
end end
end end
lpos={x=pos.x, y=pos.y, z=pos.z+1} lpos={x=pos.x, y=pos.y, z=pos.z+1}
for n=7, 9 do for n=7, 9 do
if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then
direction.x=-1 return {x=-1, y=0, z=0}
return direction
end end
end end
lpos={x=pos.x, y=pos.y, z=pos.z-1} lpos={x=pos.x, y=pos.y, z=pos.z-1}
for n=10, 12 do for n=10, 12 do
if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then if mesecon:is_power_on(lpos, rules[n].x, rules[n].y, rules[n].z) then
direction.x=1 return {x=1, y=0, z=0}
return direction
end end
end end
end end
@ -67,49 +61,29 @@ minetest.register_entity("mesecons_movestones:movestone_entity", {
on_step = function(self, dtime) on_step = function(self, dtime)
local pos = self.object:getpos() local pos = self.object:getpos()
local colp = pos local direction=mesecon:get_movestone_direction(pos)
local velocity={}
local direction=mesecon:get_movestone_direction(colp)
--colp.x=colp.x-(direction.x/2.01) if not direction then
--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
minetest.env:add_node(pos, {name="mesecons_movestones:movestone"}) minetest.env:add_node(pos, {name="mesecons_movestones:movestone"})
self.object:remove() self.object:remove()
return return
end end
--if not mesecon:check_if_turnon(colp) then
-- minetest.env:add_node(pos, {name="mesecons_movestones:movestone"})
-- self.object:remove()
-- return
--end
velocity.x=direction.x*3 self.object:setvelocity({x=direction.x*3, y=direction.y*3, z=direction.z*3})
velocity.y=direction.y*3
velocity.z=direction.z*3
self.object:setvelocity(velocity) 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 np = {x=pos.x+direction.x, y=pos.y+direction.y, z=pos.z+direction.z} local newnode
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 repeat
thisp.x=thisp.x+direction.x minetest.env:remove_node(pos)
thisp.y=thisp.y+direction.y pos.x=pos.x+direction.x
thisp.z=thisp.z+direction.z pos.y=pos.y+direction.y
nextnode=minetest.env:get_node(thisp) pos.z=pos.z+direction.z
minetest.env:place_node(thisp, {name=thisnode.name}) newnode = {name=lnode.name}
nodeupdate(thisp) lnode = minetest.env:get_node(pos)
thisnode=nextnode minetest.env:add_node(pos, newnode)
until thisnode.name=="air" or thisnode.name=="ignore" or thisnode.name=="default:water" or thisnode.name=="default:water_flowing" nodeupdate(pos)
until lnode.name == "ignore" or lnode.name == "air" or lnode.name == "default:water" or lnode.name == "default:water_flowing"
end end
end end
}) })
@ -125,7 +99,7 @@ minetest.register_craft({
mesecon:register_on_signal_on(function (pos, node) mesecon:register_on_signal_on(function (pos, node)
if node.name=="mesecons_movestones:movestone" then 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 if not direction then return end
local checknode={} local checknode={}
local collpos={x=pos.x, y=pos.y, z=pos.z} 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 pos = self.object:getpos()
local colp = pos local colp = pos
local direction=mesecon:get_movestone_direction(colp) 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 if not direction then
--or (minetest.env:get_node_or_nil(pos).name ~="air" --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"}) minetest.env:add_node(pos, {name="mesecons_movestones:sticky_movestone"})
self.object:remove() self.object:remove()
return 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 end
--STICKY: self.object:setvelocity({x=direction.x*3, y=direction.y*3, z=direction.z*3})
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)
if (coln1.name == "air" or coln1.name =="water") and (coln2.name~="air" and coln2.name ~= water) then pos.x=pos.x+direction.x
thisp= np2 pos.y=pos.y+direction.y
local newpos={} pos.z=pos.z+direction.z
local oldpos={}
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 repeat
newpos.x=thisp.x+direction.x minetest.env:remove_node(lpos)
newpos.y=thisp.y+direction.y lpos.x=lpos.x+direction.x
newpos.z=thisp.z+direction.z lpos.y=lpos.y+direction.y
minetest.env:place_node(newpos, {name=minetest.env:get_node(thisp).name}) lpos.z=lpos.z+direction.z
nodeupdate(newpos) newnode = {name=lnode.name}
oldpos={x=thisp.x, y=thisp.y, z=thisp.z} lnode = minetest.env:get_node(lpos)
thisp.x=thisp.x-direction.x minetest.env:add_node(lpos, newnode)
thisp.y=thisp.y-direction.y nodeupdate(lpos)
thisp.z=thisp.z-direction.z until lnode.name == "ignore" or lnode.name == "air" or lnode.name == "default:water" or lnode.name == "default:water_flowing"
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)
end 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 end
}) })
mesecon:register_on_signal_on(function (pos, node) mesecon:register_on_signal_on(function (pos, node)
if node.name=="mesecons_movestones:sticky_movestone" then 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 if not direction then return end
local checknode={} local checknode={}
local collpos={x=pos.x, y=pos.y, z=pos.z} local collpos={x=pos.x, y=pos.y, z=pos.z}