1
0
mirror of https://github.com/moparisthebest/minetest synced 2024-12-22 23:58:48 -05:00

Item entity stacks merge on the ground.

Add TTL to item entities.
This commit is contained in:
RealBadAngel 2014-05-17 11:25:25 +02:00
parent 94dba66c16
commit 18fe277d94
2 changed files with 103 additions and 34 deletions

View File

@ -8,26 +8,43 @@ function core.spawn_item(pos, item)
return obj return obj
end end
-- If item_entity_ttl is not set, enity will have default life time
-- Setting it to -1 disables the feature
local time_to_live = tonumber(core.setting_get("item_entity_ttl"))
if not time_to_live then
time_to_live = 900
end
core.register_entity(":__builtin:item", { core.register_entity(":__builtin:item", {
initial_properties = { initial_properties = {
hp_max = 1, hp_max = 1,
physical = true, physical = true,
collide_with_objects = false, collide_with_objects = false,
collisionbox = {-0.17,-0.17,-0.17, 0.17,0.17,0.17}, collisionbox = {-0.24, -0.24, -0.24, 0.24, 0.24, 0.24},
visual = "sprite", visual = "wielditem",
visual_size = {x=0.5, y=0.5}, visual_size = {x = 0.3, y = 0.3},
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,
}, },
itemstring = '', itemstring = '',
physical_state = true, physical_state = true,
age = 0,
set_item = function(self, itemstring) set_item = function(self, itemstring)
self.itemstring = itemstring self.itemstring = itemstring
local stack = ItemStack(itemstring) local stack = ItemStack(itemstring)
local count = stack:get_count()
local max_count = stack:get_stack_max()
if count > max_count then
count = max_count
self.itemstring = stack:get_name().." "..max_count
end
local s = 0.15 + 0.15 * (count / max_count)
local c = 0.8 * s
local itemtable = stack:to_table() local itemtable = stack:to_table()
local itemname = nil local itemname = nil
if itemtable then if itemtable then
@ -41,47 +58,51 @@ core.register_entity(":__builtin:item", {
end end
prop = { prop = {
is_visible = true, is_visible = true,
visual = "sprite", visual = "wielditem",
textures = {"unknown_item.png"} textures = {itemname},
visual_size = {x = s, y = s},
collisionbox = {-c, -c, -c, c, c, c},
automatic_rotate = math.pi * 0.2,
} }
if item_texture and item_texture ~= "" then
prop.visual = "sprite"
prop.textures = {item_texture}
prop.visual_size = {x=0.50, y=0.50}
else
prop.visual = "wielditem"
prop.textures = {itemname}
prop.visual_size = {x=0.20, y=0.20}
prop.automatic_rotate = math.pi * 0.25
end
self.object:set_properties(prop) self.object:set_properties(prop)
end, end,
get_staticdata = function(self) get_staticdata = function(self)
--return self.itemstring
return core.serialize({ return core.serialize({
itemstring = self.itemstring, itemstring = self.itemstring,
always_collect = self.always_collect, always_collect = self.always_collect,
age = self.age
}) })
end, end,
on_activate = function(self, staticdata) on_activate = function(self, staticdata, dtime_s)
if string.sub(staticdata, 1, string.len("return")) == "return" then if string.sub(staticdata, 1, string.len("return")) == "return" then
local data = core.deserialize(staticdata) local data = core.deserialize(staticdata)
if data and type(data) == "table" then if data and type(data) == "table" then
self.itemstring = data.itemstring self.itemstring = data.itemstring
self.always_collect = data.always_collect self.always_collect = data.always_collect
if data.age then
self.age = data.age + dtime_s
else
self.age = dtime_s
end
end end
else else
self.itemstring = staticdata self.itemstring = staticdata
end end
self.object:set_armor_groups({immortal=1}) self.object:set_armor_groups({immortal = 1})
self.object:setvelocity({x=0, y=2, z=0}) self.object:setvelocity({x = 0, y = 2, z = 0})
self.object:setacceleration({x=0, y=-10, z=0}) self.object:setacceleration({x = 0, y = -10, z = 0})
self:set_item(self.itemstring) self:set_item(self.itemstring)
end, end,
on_step = function(self, dtime) 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() local p = self.object:getpos()
p.y = p.y - 0.3 p.y = p.y - 0.3
local nn = core.get_node(p).name local nn = core.get_node(p).name
@ -89,21 +110,67 @@ core.register_entity(":__builtin:item", {
local v = self.object:getvelocity() local v = self.object:getvelocity()
if not core.registered_nodes[nn] or core.registered_nodes[nn].walkable and v.y == 0 then if not core.registered_nodes[nn] or core.registered_nodes[nn].walkable and v.y == 0 then
if self.physical_state then if self.physical_state then
self.object:setvelocity({x=0,y=0,z=0}) local own_stack = ItemStack(self.object:get_luaentity().itemstring)
self.object:setacceleration({x=0, y=0, z=0}) 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)
if own_stack:get_name() == stack:get_name() and stack:get_free_space() > 0 then
local overflow = false
local count = stack:get_count() + own_stack:get_count()
local max_count = stack:get_stack_max()
if count>max_count then
overflow = true
count = count - max_count
else
self.itemstring = ''
end
local pos=object:getpos()
pos.y = pos.y + (count - stack:get_count()) / max_count * 0.15
object:moveto(pos, false)
local s, c
local max_count = stack:get_stack_max()
local name = stack:get_name()
if not overflow then
obj.itemstring = name.." "..count
s = 0.15 + 0.15 * (count / max_count)
c = 0.8 * s
object:set_properties({
visual_size = {x = s, y = s},
collisionbox = {-c, -c, -c, c, c, c}
})
self.object:remove()
return
else
s = 0.3
c = 0.24
object:set_properties({
visual_size = {x = s, y = s},
collisionbox = {-c, -c, -c, c, c, c}
})
obj.itemstring = name.." "..max_count
s = 0.15 + 0.15 * (count / max_count)
c = 0.8 * s
self.object:set_properties({
visual_size = {x = s, y = s},
collisionbox = {-c, -c, -c, c, c, c}
})
self.itemstring = name.." "..count
end
end
end
end
self.object:setvelocity({x = 0, y = 0, z = 0})
self.object:setacceleration({x = 0, y = 0, z = 0})
self.physical_state = false self.physical_state = false
self.object:set_properties({ self.object:set_properties({physical = false})
physical = false
})
end end
else else
if not self.physical_state then if not self.physical_state then
self.object:setvelocity({x=0,y=0,z=0}) self.object:setvelocity({x = 0, y = 0, z = 0})
self.object:setacceleration({x=0, y=-10, z=0}) self.object:setacceleration({x = 0, y = -10, z = 0})
self.physical_state = true self.physical_state = true
self.object:set_properties({ self.object:set_properties({physical = true})
physical = true
})
end end
end end
end, end,
@ -120,4 +187,3 @@ core.register_entity(":__builtin:item", {
self.object:remove() self.object:remove()
end, end,
}) })

View File

@ -264,6 +264,9 @@
#max_users = 15 #max_users = 15
# Set to true to disallow old clients from connecting # Set to true to disallow old clients from connecting
#strict_protocol_version_checking = false #strict_protocol_version_checking = false
# Time in seconds for item entity to live. Default value: 900s
# Setting it to -1 disables the feature
#item_entity_ttl = 900
# Set to true to enable creative mode (unlimited inventory) # Set to true to enable creative mode (unlimited inventory)
#creative_mode = false #creative_mode = false
# Enable players getting damage and dying # Enable players getting damage and dying