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.settings:get("item_entity_ttl")) or 900
18 local gravity = tonumber(core.settings:get("movement_gravity")) or 9.81
21 core.register_entity(":__builtin:item", {
22 initial_properties = {
25 collide_with_objects = false,
26 collisionbox = {-0.3, -0.3, -0.3, 0.3, 0.3, 0.3},
28 visual_size = {x = 0.4, y = 0.4},
35 physical_state = true,
38 -- Pushing item out of solid nodes
40 force_out_start = nil,
42 set_item = function(self, item)
43 local stack = ItemStack(item or self.itemstring)
44 self.itemstring = stack:to_string()
45 if self.itemstring == "" then
50 -- Backwards compatibility: old clients use the texture
51 -- to get the type of the item
52 local itemname = stack:is_known() and stack:get_name() or "unknown"
54 local max_count = stack:get_stack_max()
55 local count = math.min(stack:get_count(), max_count)
56 local size = 0.2 + 0.1 * (count / max_count) ^ (1 / 3)
57 local def = core.registered_items[itemname]
58 local glow = def and def.light_source and
59 math.floor(def.light_source / 2 + 0.5)
61 local size_bias = 1e-3 * math.random() -- small random bias to counter Z-fighting
62 local c = {-size, -size, -size, size, size, size}
63 self.object:set_properties({
66 textures = {itemname},
67 visual_size = {x = size + size_bias, y = size + size_bias},
69 automatic_rotate = math.pi * 0.5 * 0.2 / size,
70 wield_item = self.itemstring,
74 -- cache for usage in on_step
75 self._collisionbox = c
78 get_staticdata = function(self)
79 return core.serialize({
80 itemstring = self.itemstring,
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.age = (data.age or 0) + dtime_s
92 self.dropped_by = data.dropped_by
95 self.itemstring = staticdata
97 self.object:set_armor_groups({immortal = 1})
98 self.object:set_velocity({x = 0, y = 2, z = 0})
99 self.object:set_acceleration({x = 0, y = -gravity, z = 0})
100 self._collisionbox = self.initial_properties.collisionbox
104 try_merge_with = function(self, own_stack, object, entity)
105 if self.age == entity.age then
106 -- Can not merge with itself
110 local stack = ItemStack(entity.itemstring)
111 local name = stack:get_name()
112 if own_stack:get_name() ~= name or
113 own_stack:get_meta() ~= stack:get_meta() or
114 own_stack:get_wear() ~= stack:get_wear() or
115 own_stack:get_free_space() == 0 then
116 -- Can not merge different or full stack
120 local count = own_stack:get_count()
121 local total_count = stack:get_count() + count
122 local max_count = stack:get_stack_max()
124 if total_count > max_count then
127 -- Merge the remote stack into this one
129 local pos = object:get_pos()
130 pos.y = pos.y + ((total_count - count) / max_count) * 0.15
131 self.object:move_to(pos)
133 self.age = 0 -- Handle as new entity
134 own_stack:set_count(total_count)
135 self:set_item(own_stack)
137 entity.itemstring = ""
142 enable_physics = function(self)
143 if not self.physical_state then
144 self.physical_state = true
145 self.object:set_properties({physical = true})
146 self.object:set_velocity({x=0, y=0, z=0})
147 self.object:set_acceleration({x=0, y=-gravity, z=0})
151 disable_physics = function(self)
152 if self.physical_state then
153 self.physical_state = false
154 self.object:set_properties({physical = false})
155 self.object:set_velocity({x=0, y=0, z=0})
156 self.object:set_acceleration({x=0, y=0, z=0})
160 on_step = function(self, dtime, moveresult)
161 self.age = self.age + dtime
162 if time_to_live > 0 and self.age > time_to_live then
168 local pos = self.object:get_pos()
169 local node = core.get_node_or_nil({
171 y = pos.y + self._collisionbox[2] - 0.05,
174 -- Delete in 'ignore' nodes
175 if node and node.name == "ignore" then
181 if self.force_out then
182 -- This code runs after the entity got a push from the is_stuck code.
183 -- It makes sure the entity is entirely outside the solid node
184 local c = self._collisionbox
185 local s = self.force_out_start
186 local f = self.force_out
187 local ok = (f.x > 0 and pos.x + c[1] > s.x + 0.5) or
188 (f.y > 0 and pos.y + c[2] > s.y + 0.5) or
189 (f.z > 0 and pos.z + c[3] > s.z + 0.5) or
190 (f.x < 0 and pos.x + c[4] < s.x - 0.5) or
191 (f.z < 0 and pos.z + c[6] < s.z - 0.5)
193 -- Item was successfully forced out
195 self:enable_physics()
200 if not self.physical_state then
201 return -- Don't do anything
205 "Collision info missing, this is caused by an out-of-date/buggy mod or game")
207 if not moveresult.collides then
208 -- future TODO: items should probably decelerate in air
212 -- Push item out when stuck inside solid node
213 local is_stuck = false
214 local snode = core.get_node_or_nil(pos)
216 local sdef = core.registered_nodes[snode.name] or {}
217 is_stuck = (sdef.walkable == nil or sdef.walkable == true)
218 and (sdef.collision_box == nil or sdef.collision_box.type == "regular")
219 and (sdef.node_box == nil or sdef.node_box.type == "regular")
225 {x=1, y=0, z=0}, {x=-1, y=0, z= 0},
226 {x=0, y=0, z=1}, {x= 0, y=0, z=-1},
229 -- Check which one of the 4 sides is free
231 local cnode = core.get_node(vector.add(pos, order[o])).name
232 local cdef = core.registered_nodes[cnode] or {}
233 if cnode ~= "ignore" and cdef.walkable == false then
238 -- If none of the 4 sides is free, check upwards
240 shootdir = {x=0, y=1, z=0}
241 local cnode = core.get_node(vector.add(pos, shootdir)).name
242 if cnode == "ignore" then
243 shootdir = nil -- Do not push into ignore
248 -- Set new item moving speed accordingly
249 local newv = vector.multiply(shootdir, 3)
250 self:disable_physics()
251 self.object:set_velocity(newv)
253 self.force_out = newv
254 self.force_out_start = vector.round(pos)
259 node = nil -- ground node we're colliding with
260 if moveresult.touching_ground then
261 for _, info in ipairs(moveresult.collisions) do
262 if info.axis == "y" then
263 node = core.get_node(info.node_pos)
269 -- Slide on slippery nodes
270 local def = node and core.registered_nodes[node.name]
271 local keep_movement = false
274 local slippery = core.get_item_group(node.name, "slippery")
275 local vel = self.object:get_velocity()
276 if slippery ~= 0 and (math.abs(vel.x) > 0.1 or math.abs(vel.z) > 0.1) then
277 -- Horizontal deceleration
278 local factor = math.min(4 / (slippery + 4) * dtime, 1)
279 self.object:set_velocity({
280 x = vel.x * (1 - factor),
282 z = vel.z * (1 - factor)
288 if not keep_movement then
289 self.object:set_velocity({x=0, y=0, z=0})
292 if self.moving_state == keep_movement then
293 -- Do not update anything until the moving state changes
296 self.moving_state = keep_movement
298 -- Only collect items if not moving
299 if self.moving_state then
302 -- Collect the items around to merge with
303 local own_stack = ItemStack(self.itemstring)
304 if own_stack:get_free_space() == 0 then
307 local objects = core.get_objects_inside_radius(pos, 1.0)
308 for k, obj in pairs(objects) do
309 local entity = obj:get_luaentity()
310 if entity and entity.name == "__builtin:item" then
311 if self:try_merge_with(own_stack, obj, entity) then
312 own_stack = ItemStack(self.itemstring)
313 if own_stack:get_free_space() == 0 then
321 on_punch = function(self, hitter)
322 local inv = hitter:get_inventory()
323 if inv and self.itemstring ~= "" then
324 local left = inv:add_item("main", self.itemstring)
325 if left and not left:is_empty() then