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,
40 set_item = function(self, item)
41 local stack = ItemStack(item or self.itemstring)
42 self.itemstring = stack:to_string()
43 if self.itemstring == "" then
48 -- Backwards compatibility: old clients use the texture
49 -- to get the type of the item
50 local itemname = stack:is_known() and stack:get_name() or "unknown"
52 local max_count = stack:get_stack_max()
53 local count = math.min(stack:get_count(), max_count)
54 local size = 0.2 + 0.1 * (count / max_count) ^ (1 / 3)
55 local coll_height = size * 0.75
57 self.object:set_properties({
60 textures = {itemname},
61 visual_size = {x = size, y = size},
62 collisionbox = {-size, -coll_height, -size,
63 size, coll_height, size},
64 selectionbox = {-size, -size, -size, size, size, size},
65 automatic_rotate = math.pi * 0.5 * 0.2 / size,
66 wield_item = self.itemstring,
71 get_staticdata = function(self)
72 return core.serialize({
73 itemstring = self.itemstring,
75 dropped_by = self.dropped_by
79 on_activate = function(self, staticdata, dtime_s)
80 if string.sub(staticdata, 1, string.len("return")) == "return" then
81 local data = core.deserialize(staticdata)
82 if data and type(data) == "table" then
83 self.itemstring = data.itemstring
84 self.age = (data.age or 0) + dtime_s
85 self.dropped_by = data.dropped_by
88 self.itemstring = staticdata
90 self.object:set_armor_groups({immortal = 1})
91 self.object:set_velocity({x = 0, y = 2, z = 0})
92 self.object:set_acceleration({x = 0, y = -gravity, z = 0})
96 try_merge_with = function(self, own_stack, object, entity)
97 if self.age == entity.age then
98 -- Can not merge with itself
102 local stack = ItemStack(entity.itemstring)
103 local name = stack:get_name()
104 if own_stack:get_name() ~= name or
105 own_stack:get_meta() ~= stack:get_meta() or
106 own_stack:get_wear() ~= stack:get_wear() or
107 own_stack:get_free_space() == 0 then
108 -- Can not merge different or full stack
112 local count = own_stack:get_count()
113 local total_count = stack:get_count() + count
114 local max_count = stack:get_stack_max()
116 if total_count > max_count then
119 -- Merge the remote stack into this one
121 local pos = object:get_pos()
122 pos.y = pos.y + ((total_count - count) / max_count) * 0.15
123 self.object:move_to(pos)
125 self.age = 0 -- Handle as new entity
126 own_stack:set_count(total_count)
127 self:set_item(own_stack)
129 entity.itemstring = ""
134 on_step = function(self, dtime)
135 self.age = self.age + dtime
136 if time_to_live > 0 and self.age > time_to_live then
142 local pos = self.object:get_pos()
143 local node = core.get_node_or_nil({
145 y = pos.y + self.object:get_properties().collisionbox[2] - 0.05,
148 local vel = self.object:getvelocity()
149 local def = node and core.registered_nodes[node.name]
150 -- Avoid entity falling into ignore nodes or unloaded areas
151 local is_moving = node and node.name ~= "ignore" and
152 ((def and not def.walkable) or vel.x ~= 0 or vel.y ~= 0 or vel.z ~= 0)
153 local is_slippery = false
155 if def and def.walkable then
156 local slippery = core.get_item_group(node.name, "slippery")
157 is_slippery = slippery ~= 0
158 if is_slippery and (math.abs(vel.x) > 0.2 or math.abs(vel.z) > 0.2) then
159 -- Horizontal deceleration
160 local slip_factor = 4.0 / (slippery + 4)
161 self.object:set_acceleration({
162 x = -vel.x * slip_factor,
164 z = -vel.z * slip_factor
166 elseif vel.y == 0 then
171 if self.moving_state == is_moving and
172 self.slippery_state == is_slippery then
173 -- Do not update anything until the moving state changes
177 self.moving_state = is_moving
178 self.slippery_state = is_slippery
181 self.object:set_acceleration({x = 0, y = -gravity, z = 0})
183 self.object:set_acceleration({x = 0, y = 0, z = 0})
184 self.object:set_velocity({x = 0, y = 0, z = 0})
187 --Only collect items if not moving
191 -- Collect the items around to merge with
192 local own_stack = ItemStack(self.itemstring)
193 if own_stack:get_free_space() == 0 then
196 local objects = core.get_objects_inside_radius(pos, 1.0)
197 for k, obj in pairs(objects) do
198 local entity = obj:get_luaentity()
199 if entity and entity.name == "__builtin:item" then
200 if self:try_merge_with(own_stack, obj, entity) then
201 own_stack = ItemStack(self.itemstring)
202 if own_stack:get_free_space() == 0 then
210 on_punch = function(self, hitter)
211 local inv = hitter:get_inventory()
212 if inv and self.itemstring ~= "" then
213 local left = inv:add_item("main", self.itemstring)
214 if left and not left:is_empty() then