1 -- Minetest: builtin/item_entity.lua
3 function core.spawn_item(pos, item)
4 -- Take item in any format
5 local stack = ItemStack(item)
6 local obj = core.add_entity(pos, "__builtin:item")
7 -- Don't use obj if it couldn't be added to the map.
9 obj:get_luaentity():set_item(stack:to_string())
14 -- If item_entity_ttl is not set, enity will have default life time
15 -- Setting it to -1 disables the feature
17 local time_to_live = tonumber(core.setting_get("item_entity_ttl"))
18 if not time_to_live then
22 core.register_entity(":__builtin:item", {
23 initial_properties = {
26 collide_with_objects = false,
27 collisionbox = {-0.3, -0.3, -0.3, 0.3, 0.3, 0.3},
29 visual_size = {x = 0.4, y = 0.4},
31 spritediv = {x = 1, y = 1},
32 initial_sprite_basepos = {x = 0, y = 0},
38 physical_state = true,
41 set_item = function(self, itemstring)
42 self.itemstring = itemstring
43 local stack = ItemStack(itemstring)
44 local count = stack:get_count()
45 local max_count = stack:get_stack_max()
46 if count > max_count then
48 self.itemstring = stack:get_name().." "..max_count
50 local s = 0.2 + 0.1 * (count / max_count)
52 local itemtable = stack:to_table()
54 local description = ""
56 itemname = stack:to_table().name
58 local item_texture = nil
60 if core.registered_items[itemname] then
61 item_texture = core.registered_items[itemname].inventory_image
62 item_type = core.registered_items[itemname].type
63 description = core.registered_items[itemname].description
68 textures = {itemname},
69 visual_size = {x = s, y = s},
70 collisionbox = {-c, -c, -c, c, c, c},
71 automatic_rotate = math.pi * 0.5,
72 infotext = description,
74 self.object:set_properties(prop)
77 get_staticdata = function(self)
78 return core.serialize({
79 itemstring = self.itemstring,
80 always_collect = self.always_collect,
82 dropped_by = self.dropped_by
86 on_activate = function(self, staticdata, dtime_s)
87 if string.sub(staticdata, 1, string.len("return")) == "return" then
88 local data = core.deserialize(staticdata)
89 if data and type(data) == "table" then
90 self.itemstring = data.itemstring
91 self.always_collect = data.always_collect
93 self.age = data.age + dtime_s
97 self.dropped_by = data.dropped_by
100 self.itemstring = staticdata
102 self.object:set_armor_groups({immortal = 1})
103 self.object:setvelocity({x = 0, y = 2, z = 0})
104 self.object:setacceleration({x = 0, y = -10, z = 0})
105 self:set_item(self.itemstring)
108 try_merge_with = function(self, own_stack, object, obj)
109 local stack = ItemStack(obj.itemstring)
110 if own_stack:get_name() == stack:get_name() and stack:get_free_space() > 0 then
111 local overflow = false
112 local count = stack:get_count() + own_stack:get_count()
113 local max_count = stack:get_stack_max()
114 if count > max_count then
116 count = count - max_count
120 local pos = object:getpos()
121 pos.y = pos.y + (count - stack:get_count()) / max_count * 0.15
122 object:moveto(pos, false)
124 local max_count = stack:get_stack_max()
125 local name = stack:get_name()
127 obj.itemstring = name .. " " .. count
128 s = 0.2 + 0.1 * (count / max_count)
130 object:set_properties({
131 visual_size = {x = s, y = s},
132 collisionbox = {-c, -c, -c, c, c, c}
140 object:set_properties({
141 visual_size = {x = s, y = s},
142 collisionbox = {-c, -c, -c, c, c, c}
144 obj.itemstring = name .. " " .. max_count
145 s = 0.2 + 0.1 * (count / max_count)
147 self.object:set_properties({
148 visual_size = {x = s, y = s},
149 collisionbox = {-c, -c, -c, c, c, c}
151 self.itemstring = name .. " " .. count
154 -- merging didn't succeed
158 on_step = function(self, dtime)
159 self.age = self.age + dtime
160 if time_to_live > 0 and self.age > time_to_live then
165 local p = self.object:getpos()
167 local node = core.get_node_or_nil(p)
168 local in_unloaded = (node == nil)
170 -- Don't infinetly fall into unloaded map
171 self.object:setvelocity({x = 0, y = 0, z = 0})
172 self.object:setacceleration({x = 0, y = 0, z = 0})
173 self.physical_state = false
174 self.object:set_properties({physical = false})
178 -- If node is not registered or node is walkably solid and resting on nodebox
179 local v = self.object:getvelocity()
180 if not core.registered_nodes[nn] or core.registered_nodes[nn].walkable and v.y == 0 then
181 if self.physical_state then
182 local own_stack = ItemStack(self.object:get_luaentity().itemstring)
183 -- Merge with close entities of the same item
184 for _, object in ipairs(core.get_objects_inside_radius(p, 0.8)) do
185 local obj = object:get_luaentity()
186 if obj and obj.name == "__builtin:item"
187 and obj.physical_state == false then
188 if self:try_merge_with(own_stack, object, obj) then
193 self.object:setvelocity({x = 0, y = 0, z = 0})
194 self.object:setacceleration({x = 0, y = 0, z = 0})
195 self.physical_state = false
196 self.object:set_properties({physical = false})
199 if not self.physical_state then
200 self.object:setvelocity({x = 0, y = 0, z = 0})
201 self.object:setacceleration({x = 0, y = -10, z = 0})
202 self.physical_state = true
203 self.object:set_properties({physical = true})
208 on_punch = function(self, hitter)
209 local inv = hitter:get_inventory()
210 if inv and self.itemstring ~= '' then
211 local left = inv:add_item("main", self.itemstring)
212 if left and not left:is_empty() then
213 self.itemstring = left:to_string()