Skip to content

Commit 178f536

Browse files
committedMay 14, 2015
Item entity merging refactor
Don't ident too much, and add a comment.
1 parent b70e67d commit 178f536

File tree

1 file changed

+56
-45
lines changed

1 file changed

+56
-45
lines changed
 

Diff for: ‎builtin/game/item_entity.lua

+56-45
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,56 @@ core.register_entity(":__builtin:item", {
9696
self:set_item(self.itemstring)
9797
end,
9898

99+
try_merge_with = function(self, own_stack, object, obj)
100+
local stack = ItemStack(obj.itemstring)
101+
if own_stack:get_name() == stack:get_name() and stack:get_free_space() > 0 then
102+
local overflow = false
103+
local count = stack:get_count() + own_stack:get_count()
104+
local max_count = stack:get_stack_max()
105+
if count > max_count then
106+
overflow = true
107+
count = count - max_count
108+
else
109+
self.itemstring = ''
110+
end
111+
local pos = object:getpos()
112+
pos.y = pos.y + (count - stack:get_count()) / max_count * 0.15
113+
object:moveto(pos, false)
114+
local s, c
115+
local max_count = stack:get_stack_max()
116+
local name = stack:get_name()
117+
if not overflow then
118+
obj.itemstring = name .. " " .. count
119+
s = 0.2 + 0.1 * (count / max_count)
120+
c = s
121+
object:set_properties({
122+
visual_size = {x = s, y = s},
123+
collisionbox = {-c, -c, -c, c, c, c}
124+
})
125+
self.object:remove()
126+
-- merging succeeded
127+
return true
128+
else
129+
s = 0.4
130+
c = 0.3
131+
object:set_properties({
132+
visual_size = {x = s, y = s},
133+
collisionbox = {-c, -c, -c, c, c, c}
134+
})
135+
obj.itemstring = name .. " " .. max_count
136+
s = 0.2 + 0.1 * (count / max_count)
137+
c = s
138+
self.object:set_properties({
139+
visual_size = {x = s, y = s},
140+
collisionbox = {-c, -c, -c, c, c, c}
141+
})
142+
self.itemstring = name .. " " .. count
143+
end
144+
end
145+
-- merging didn't succeed
146+
return false
147+
end,
148+
99149
on_step = function(self, dtime)
100150
self.age = self.age + dtime
101151
if time_to_live > 0 and self.age > time_to_live then
@@ -111,52 +161,13 @@ core.register_entity(":__builtin:item", {
111161
if not core.registered_nodes[nn] or core.registered_nodes[nn].walkable and v.y == 0 then
112162
if self.physical_state then
113163
local own_stack = ItemStack(self.object:get_luaentity().itemstring)
114-
for _,object in ipairs(core.get_objects_inside_radius(p, 0.8)) do
164+
-- Merge with close entities of the same item
165+
for _, object in ipairs(core.get_objects_inside_radius(p, 0.8)) do
115166
local obj = object:get_luaentity()
116-
if obj and obj.name == "__builtin:item" and obj.physical_state == false then
117-
local stack = ItemStack(obj.itemstring)
118-
if own_stack:get_name() == stack:get_name() and stack:get_free_space() > 0 then
119-
local overflow = false
120-
local count = stack:get_count() + own_stack:get_count()
121-
local max_count = stack:get_stack_max()
122-
if count>max_count then
123-
overflow = true
124-
count = count - max_count
125-
else
126-
self.itemstring = ''
127-
end
128-
local pos=object:getpos()
129-
pos.y = pos.y + (count - stack:get_count()) / max_count * 0.15
130-
object:moveto(pos, false)
131-
local s, c
132-
local max_count = stack:get_stack_max()
133-
local name = stack:get_name()
134-
if not overflow then
135-
obj.itemstring = name.." "..count
136-
s = 0.2 + 0.1 * (count / max_count)
137-
c = s
138-
object:set_properties({
139-
visual_size = {x = s, y = s},
140-
collisionbox = {-c, -c, -c, c, c, c}
141-
})
142-
self.object:remove()
143-
return
144-
else
145-
s = 0.4
146-
c = 0.3
147-
object:set_properties({
148-
visual_size = {x = s, y = s},
149-
collisionbox = {-c, -c, -c, c, c, c}
150-
})
151-
obj.itemstring = name.." "..max_count
152-
s = 0.2 + 0.1 * (count / max_count)
153-
c = s
154-
self.object:set_properties({
155-
visual_size = {x = s, y = s},
156-
collisionbox = {-c, -c, -c, c, c, c}
157-
})
158-
self.itemstring = name.." "..count
159-
end
167+
if obj and obj.name == "__builtin:item"
168+
and obj.physical_state == false then
169+
if self:try_merge_with(own_stack, object, obj) then
170+
return
160171
end
161172
end
162173
end

0 commit comments

Comments
 (0)
Please sign in to comment.