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},
30 spritediv = {x = 1, y = 1},
31 initial_sprite_basepos = {x = 0, y = 0},
37 slippery_state = false,
38 physical_state = true,
41 -- Pushing item out of solid nodes
43 force_out_start = nil,
45 set_item = function(self, item)
46 local stack = ItemStack(item or self.itemstring)
47 self.itemstring = stack:to_string()
48 if self.itemstring == "" then
53 -- Backwards compatibility: old clients use the texture
54 -- to get the type of the item
55 local itemname = stack:is_known() and stack:get_name() or "unknown"
57 local max_count = stack:get_stack_max()
58 local count = math.min(stack:get_count(), max_count)
59 local size = 0.2 + 0.1 * (count / max_count) ^ (1 / 3)
60 local coll_height = size * 0.75
62 self.object:set_properties({
65 textures = {itemname},
66 visual_size = {x = size, y = size},
67 collisionbox = {-size, -coll_height, -size,
68 size, coll_height, size},
69 selectionbox = {-size, -size, -size, size, size, size},
70 automatic_rotate = math.pi * 0.5 * 0.2 / size,
71 wield_item = self.itemstring,
76 get_staticdata = function(self)
77 return core.serialize({
78 itemstring = self.itemstring,
80 dropped_by = self.dropped_by
84 on_activate = function(self, staticdata, dtime_s)
85 if string.sub(staticdata, 1, string.len("return")) == "return" then
86 local data = core.deserialize(staticdata)
87 if data and type(data) == "table" then
88 self.itemstring = data.itemstring
89 self.age = (data.age or 0) + dtime_s
90 self.dropped_by = data.dropped_by
93 self.itemstring = staticdata
95 self.object:set_armor_groups({immortal = 1})
96 self.object:set_velocity({x = 0, y = 2, z = 0})
97 self.object:set_acceleration({x = 0, y = -gravity, z = 0})
101 try_merge_with = function(self, own_stack, object, entity)
102 if self.age == entity.age then
103 -- Can not merge with itself
107 local stack = ItemStack(entity.itemstring)
108 local name = stack:get_name()
109 if own_stack:get_name() ~= name or
110 own_stack:get_meta() ~= stack:get_meta() or
111 own_stack:get_wear() ~= stack:get_wear() or
112 own_stack:get_free_space() == 0 then
113 -- Can not merge different or full stack
117 local count = own_stack:get_count()
118 local total_count = stack:get_count() + count
119 local max_count = stack:get_stack_max()
121 if total_count > max_count then
124 -- Merge the remote stack into this one
126 local pos = object:get_pos()
127 pos.y = pos.y + ((total_count - count) / max_count) * 0.15
128 self.object:move_to(pos)
130 self.age = 0 -- Handle as new entity
131 own_stack:set_count(total_count)
132 self:set_item(own_stack)
134 entity.itemstring = ""
139 enable_physics = function(self)
140 if not self.physical_state then
141 self.physical_state = true
142 self.object:set_properties({physical = true})
143 self.object:set_velocity({x=0, y=0, z=0})
144 self.object:set_acceleration({x=0, y=-gravity, z=0})
148 disable_physics = function(self)
149 if self.physical_state then
150 self.physical_state = false
151 self.object:set_properties({physical = false})
152 self.object:set_velocity({x=0, y=0, z=0})
153 self.object:set_acceleration({x=0, y=0, z=0})
157 on_step = function(self, dtime)
158 self.age = self.age + dtime
159 if time_to_live > 0 and self.age > time_to_live then
165 local pos = self.object:get_pos()
166 local node = core.get_node_or_nil({
168 y = pos.y + self.object:get_properties().collisionbox[2] - 0.05,
171 -- Delete in 'ignore' nodes
172 if node and node.name == "ignore" then
178 local is_stuck = false
179 local snode = core.get_node_or_nil(pos)
181 local sdef = core.registered_nodes[snode.name] or {}
182 is_stuck = (sdef.walkable == nil or sdef.walkable == true)
183 and (sdef.collision_box == nil or sdef.collision_box.type == "regular")
184 and (sdef.node_box == nil or sdef.node_box.type == "regular")
187 -- Push item out when stuck inside solid node
191 {x=1, y=0, z=0}, {x=-1, y=0, z= 0},
192 {x=0, y=0, z=1}, {x= 0, y=0, z=-1},
195 -- Check which one of the 4 sides is free
197 local cnode = core.get_node(vector.add(pos, order[o])).name
198 local cdef = core.registered_nodes[cnode] or {}
199 if cnode ~= "ignore" and cdef.walkable == false then
204 -- If none of the 4 sides is free, check upwards
206 shootdir = {x=0, y=1, z=0}
207 local cnode = core.get_node(vector.add(pos, shootdir)).name
208 if cnode == "ignore" then
209 shootdir = nil -- Do not push into ignore
214 -- Set new item moving speed accordingly
215 local newv = vector.multiply(shootdir, 3)
216 self:disable_physics()
217 self.object:set_velocity(newv)
219 self.force_out = newv
220 self.force_out_start = vector.round(pos)
223 elseif self.force_out then
224 -- This code runs after the entity got a push from the above code.
225 -- It makes sure the entity is entirely outside the solid node
226 local c = self.object:get_properties().collisionbox
227 local s = self.force_out_start
228 local f = self.force_out
229 local ok = (f.x > 0 and pos.x + c[1] > s.x + 0.5) or
230 (f.y > 0 and pos.y + c[2] > s.y + 0.5) or
231 (f.z > 0 and pos.z + c[3] > s.z + 0.5) or
232 (f.x < 0 and pos.x + c[4] < s.x - 0.5) or
233 (f.z < 0 and pos.z + c[6] < s.z - 0.5)
235 -- Item was successfully forced out
237 self:enable_physics()
241 if not self.physical_state then
242 return -- Don't do anything
245 -- Slide on slippery nodes
246 local vel = self.object:get_velocity()
247 local def = node and core.registered_nodes[node.name]
248 local is_moving = (def and not def.walkable) or
249 vel.x ~= 0 or vel.y ~= 0 or vel.z ~= 0
250 local is_slippery = false
252 if def and def.walkable then
253 local slippery = core.get_item_group(node.name, "slippery")
254 is_slippery = slippery ~= 0
255 if is_slippery and (math.abs(vel.x) > 0.2 or math.abs(vel.z) > 0.2) then
256 -- Horizontal deceleration
257 local slip_factor = 4.0 / (slippery + 4)
258 self.object:set_acceleration({
259 x = -vel.x * slip_factor,
261 z = -vel.z * slip_factor
263 elseif vel.y == 0 then
268 if self.moving_state == is_moving and
269 self.slippery_state == is_slippery then
270 -- Do not update anything until the moving state changes
274 self.moving_state = is_moving
275 self.slippery_state = is_slippery
278 self.object:set_acceleration({x = 0, y = -gravity, z = 0})
280 self.object:set_acceleration({x = 0, y = 0, z = 0})
281 self.object:set_velocity({x = 0, y = 0, z = 0})
284 --Only collect items if not moving
288 -- Collect the items around to merge with
289 local own_stack = ItemStack(self.itemstring)
290 if own_stack:get_free_space() == 0 then
293 local objects = core.get_objects_inside_radius(pos, 1.0)
294 for k, obj in pairs(objects) do
295 local entity = obj:get_luaentity()
296 if entity and entity.name == "__builtin:item" then
297 if self:try_merge_with(own_stack, obj, entity) then
298 own_stack = ItemStack(self.itemstring)
299 if own_stack:get_free_space() == 0 then
307 on_punch = function(self, hitter)
308 local inv = hitter:get_inventory()
309 if inv and self.itemstring ~= "" then
310 local left = inv:add_item("main", self.itemstring)
311 if left and not left:is_empty() then