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},
37 physical_state = true,
40 set_item = function(self, itemstring)
41 self.itemstring = itemstring
42 local stack = ItemStack(itemstring)
43 local count = stack:get_count()
44 local max_count = stack:get_stack_max()
45 if count > max_count then
47 self.itemstring = stack:get_name().." "..max_count
49 local s = 0.2 + 0.1 * (count / max_count)
51 local itemtable = stack:to_table()
54 itemname = stack:to_table().name
56 local item_texture = nil
58 if core.registered_items[itemname] then
59 item_texture = core.registered_items[itemname].inventory_image
60 item_type = core.registered_items[itemname].type
65 textures = {itemname},
66 visual_size = {x = s, y = s},
67 collisionbox = {-c, -c, -c, c, c, c},
68 automatic_rotate = math.pi * 0.5,
70 self.object:set_properties(prop)
73 get_staticdata = function(self)
74 return core.serialize({
75 itemstring = self.itemstring,
76 always_collect = self.always_collect,
81 on_activate = function(self, staticdata, dtime_s)
82 if string.sub(staticdata, 1, string.len("return")) == "return" then
83 local data = core.deserialize(staticdata)
84 if data and type(data) == "table" then
85 self.itemstring = data.itemstring
86 self.always_collect = data.always_collect
88 self.age = data.age + dtime_s
94 self.itemstring = staticdata
96 self.object:set_armor_groups({immortal = 1})
97 self.object:setvelocity({x = 0, y = 2, z = 0})
98 self.object:setacceleration({x = 0, y = -10, z = 0})
99 self:set_item(self.itemstring)
102 try_merge_with = function(self, own_stack, object, obj)
103 local stack = ItemStack(obj.itemstring)
104 if own_stack:get_name() == stack:get_name() and stack:get_free_space() > 0 then
105 local overflow = false
106 local count = stack:get_count() + own_stack:get_count()
107 local max_count = stack:get_stack_max()
108 if count > max_count then
110 count = count - max_count
114 local pos = object:getpos()
115 pos.y = pos.y + (count - stack:get_count()) / max_count * 0.15
116 object:moveto(pos, false)
118 local max_count = stack:get_stack_max()
119 local name = stack:get_name()
121 obj.itemstring = name .. " " .. count
122 s = 0.2 + 0.1 * (count / max_count)
124 object:set_properties({
125 visual_size = {x = s, y = s},
126 collisionbox = {-c, -c, -c, c, c, c}
134 object:set_properties({
135 visual_size = {x = s, y = s},
136 collisionbox = {-c, -c, -c, c, c, c}
138 obj.itemstring = name .. " " .. max_count
139 s = 0.2 + 0.1 * (count / max_count)
141 self.object:set_properties({
142 visual_size = {x = s, y = s},
143 collisionbox = {-c, -c, -c, c, c, c}
145 self.itemstring = name .. " " .. count
148 -- merging didn't succeed
152 on_step = function(self, dtime)
153 self.age = self.age + dtime
154 if time_to_live > 0 and self.age > time_to_live then
159 local p = self.object:getpos()
161 local node = core.get_node_or_nil(p)
162 local in_unloaded = (node == nil)
164 -- Don't infinetly fall into unloaded map
165 self.object:setvelocity({x = 0, y = 0, z = 0})
166 self.object:setacceleration({x = 0, y = 0, z = 0})
167 self.physical_state = false
168 self.object:set_properties({physical = false})
172 -- If node is not registered or node is walkably solid and resting on nodebox
173 local v = self.object:getvelocity()
174 if not core.registered_nodes[nn] or core.registered_nodes[nn].walkable and v.y == 0 then
175 if self.physical_state then
176 local own_stack = ItemStack(self.object:get_luaentity().itemstring)
177 -- Merge with close entities of the same item
178 for _, object in ipairs(core.get_objects_inside_radius(p, 0.8)) do
179 local obj = object:get_luaentity()
180 if obj and obj.name == "__builtin:item"
181 and obj.physical_state == false then
182 if self:try_merge_with(own_stack, object, obj) then
187 self.object:setvelocity({x = 0, y = 0, z = 0})
188 self.object:setacceleration({x = 0, y = 0, z = 0})
189 self.physical_state = false
190 self.object:set_properties({physical = false})
193 if not self.physical_state then
194 self.object:setvelocity({x = 0, y = 0, z = 0})
195 self.object:setacceleration({x = 0, y = -10, z = 0})
196 self.physical_state = true
197 self.object:set_properties({physical = true})
202 on_punch = function(self, hitter)
203 local inv = hitter:get_inventory()
204 if inv and self.itemstring ~= '' then
205 local left = inv:add_item("main", self.itemstring)
206 if left and not left:is_empty() then
207 self.itemstring = left:to_string()