Try to fix some bugs that happen on server restart

This commit is contained in:
Novatux 2014-07-01 18:42:52 +02:00
parent d532e646f6
commit 99a902a8b8

View File

@ -353,15 +353,16 @@ minetest.register_entity("pipeworks:tubed_item", {
hp_max = 1, hp_max = 1,
physical = false, physical = false,
-- collisionbox = {0,0,0,0,0,0}, -- collisionbox = {0,0,0,0,0,0},
collisionbox = {0.1,0.1,0.1,0.1,0.1,0.1}, collisionbox = {0.1, 0.1, 0.1, 0.1, 0.1, 0.1},
visual = "sprite", visual = "sprite",
visual_size = {x=0.5, y=0.5}, visual_size = {x = 0.5, y = 0.5},
textures = {""}, textures = {""},
spritediv = {x=1, y=1}, spritediv = {x = 1, y = 1},
initial_sprite_basepos = {x=0, y=0}, initial_sprite_basepos = {x = 0, y = 0},
is_visible = false, is_visible = false,
start_pos={}, start_pos = {},
route={} route = {},
removed = false
}, },
itemstring = '', itemstring = '',
@ -389,23 +390,25 @@ minetest.register_entity("pipeworks:tubed_item", {
if item_texture and item_texture ~= "" then if item_texture and item_texture ~= "" then
prop.visual = "sprite" prop.visual = "sprite"
prop.textures = {item_texture} prop.textures = {item_texture}
prop.visual_size = {x=0.3, y=0.3} prop.visual_size = {x = 0.3, y = 0.3}
else else
prop.visual = "wielditem" prop.visual = "wielditem"
prop.textures = {itemname} prop.textures = {itemname}
prop.visual_size = {x=0.15, y=0.15} prop.visual_size = {x = 0.15, y = 0.15}
end end
self.object:set_properties(prop) self.object:set_properties(prop)
end, end,
get_staticdata = function(self) get_staticdata = function(self)
if self.start_pos==nil then return end if self.start_pos == nil or self.removed then
local velocity=self.object:getvelocity() return
end
local velocity = self.object:getvelocity()
self.object:setpos(self.start_pos) self.object:setpos(self.start_pos)
return minetest.serialize({ return minetest.serialize({
itemstring=self.itemstring, itemstring = self.itemstring,
velocity=velocity, velocity = velocity,
start_pos=self.start_pos start_pos = self.start_pos
}) })
end, end,
@ -428,10 +431,19 @@ minetest.register_entity("pipeworks:tubed_item", {
self:set_item(item.itemstring) self:set_item(item.itemstring)
end, end,
remove = function(self)
self.object:remove()
self.removed = true
self.itemstring = ''
end,
on_step = function(self, dtime) on_step = function(self, dtime)
if self.start_pos==nil then if self.removed then
return
end
if self.start_pos == nil then
local pos = self.object:getpos() local pos = self.object:getpos()
self.start_pos=roundpos(pos) self.start_pos = roundpos(pos)
end end
local pos = self.object:getpos() local pos = self.object:getpos()
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
@ -440,32 +452,32 @@ minetest.register_entity("pipeworks:tubed_item", {
local stack = ItemStack(self.itemstring) local stack = ItemStack(self.itemstring)
local drop_pos = nil local drop_pos = nil
local velocity=self.object:getvelocity() local velocity = self.object:getvelocity()
if velocity == nil then return end if velocity == nil then return end
local velocitycopy = {x = velocity.x, y = velocity.y, z = velocity.z} local velocitycopy = {x = velocity.x, y = velocity.y, z = velocity.z}
local moved = false local moved = false
local speed = math.abs(velocity.x+velocity.y+velocity.z) local speed = math.abs(velocity.x + velocity.y + velocity.z)
local vel = {x = velocity.x/speed, y = velocity.y/speed, z = velocity.z/speed, speed = speed} local vel = {x = velocity.x / speed, y = velocity.y / speed, z = velocity.z / speed, speed = speed}
if math.abs(vel.x) == 1 then if math.abs(vel.x) == 1 then
local next_node = math.abs(pos.x-self.start_pos.x) local next_node = math.abs(pos.x - self.start_pos.x)
if next_node >= 1 then if next_node >= 1 then
self.start_pos.x = self.start_pos.x+vel.x self.start_pos.x = self.start_pos.x + vel.x
moved = true moved = true
end end
elseif math.abs(vel.y) == 1 then elseif math.abs(vel.y) == 1 then
local next_node = math.abs(pos.y-self.start_pos.y) local next_node = math.abs(pos.y - self.start_pos.y)
if next_node >= 1 then
self.start_pos.y = self.start_pos.y+vel.y
moved = true
end
elseif math.abs(vel.z) == 1 then
local next_node = math.abs(pos.z-self.start_pos.z)
if next_node >= 1 then if next_node >= 1 then
self.start_pos.z = self.start_pos.z+vel.z self.start_pos.y = self.start_pos.y + vel.y
moved = true
end
elseif math.abs(vel.z) == 1 then
local next_node = math.abs(pos.z - self.start_pos.z)
if next_node >= 1 then
self.start_pos.z = self.start_pos.z + vel.z
moved = true moved = true
end end
end end
@ -481,7 +493,7 @@ minetest.register_entity("pipeworks:tubed_item", {
leftover = stack leftover = stack
end end
if leftover:is_empty() then if leftover:is_empty() then
self.object:remove() self:remove()
return return
end end
velocity.x = -velocity.x velocity.x = -velocity.x
@ -497,7 +509,7 @@ minetest.register_entity("pipeworks:tubed_item", {
drop_pos = minetest.find_node_near(vector.add(self.start_pos, velocity), 1, "air") drop_pos = minetest.find_node_near(vector.add(self.start_pos, velocity), 1, "air")
if drop_pos then if drop_pos then
minetest.item_drop(stack, "", drop_pos) minetest.item_drop(stack, "", drop_pos)
self.object:remove() self:remove()
end end
end end
end end