Item entity merging refactor

Don't ident too much, and add a comment.
This commit is contained in:
est31 2015-05-15 00:03:19 +02:00
parent b70e67df8c
commit 178f536f08

@ -96,24 +96,7 @@ core.register_entity(":__builtin:item", {
self:set_item(self.itemstring) self:set_item(self.itemstring)
end, end,
on_step = function(self, dtime) try_merge_with = function(self, own_stack, object, obj)
self.age = self.age + dtime
if time_to_live > 0 and self.age > time_to_live then
self.itemstring = ''
self.object:remove()
return
end
local p = self.object:getpos()
p.y = p.y - 0.5
local nn = core.get_node(p).name
-- If node is not registered or node is walkably solid and resting on nodebox
local v = self.object:getvelocity()
if not core.registered_nodes[nn] or core.registered_nodes[nn].walkable and v.y == 0 then
if self.physical_state then
local own_stack = ItemStack(self.object:get_luaentity().itemstring)
for _,object in ipairs(core.get_objects_inside_radius(p, 0.8)) do
local obj = object:get_luaentity()
if obj and obj.name == "__builtin:item" and obj.physical_state == false then
local stack = ItemStack(obj.itemstring) local stack = ItemStack(obj.itemstring)
if own_stack:get_name() == stack:get_name() and stack:get_free_space() > 0 then if own_stack:get_name() == stack:get_name() and stack:get_free_space() > 0 then
local overflow = false local overflow = false
@ -140,7 +123,8 @@ core.register_entity(":__builtin:item", {
collisionbox = {-c, -c, -c, c, c, c} collisionbox = {-c, -c, -c, c, c, c}
}) })
self.object:remove() self.object:remove()
return -- merging succeeded
return true
else else
s = 0.4 s = 0.4
c = 0.3 c = 0.3
@ -158,6 +142,33 @@ core.register_entity(":__builtin:item", {
self.itemstring = name .. " " .. count self.itemstring = name .. " " .. count
end end
end end
-- merging didn't succeed
return false
end,
on_step = function(self, dtime)
self.age = self.age + dtime
if time_to_live > 0 and self.age > time_to_live then
self.itemstring = ''
self.object:remove()
return
end
local p = self.object:getpos()
p.y = p.y - 0.5
local nn = core.get_node(p).name
-- If node is not registered or node is walkably solid and resting on nodebox
local v = self.object:getvelocity()
if not core.registered_nodes[nn] or core.registered_nodes[nn].walkable and v.y == 0 then
if self.physical_state then
local own_stack = ItemStack(self.object:get_luaentity().itemstring)
-- Merge with close entities of the same item
for _, object in ipairs(core.get_objects_inside_radius(p, 0.8)) do
local obj = object:get_luaentity()
if obj and obj.name == "__builtin:item"
and obj.physical_state == false then
if self:try_merge_with(own_stack, object, obj) then
return
end
end end
end end
self.object:setvelocity({x = 0, y = 0, z = 0}) self.object:setvelocity({x = 0, y = 0, z = 0})