]> git.lizzy.rs Git - minetest.git/blob - builtin/game/item_entity.lua
a184677643194c95fc5c167401ec98d61820a524
[minetest.git] / builtin / game / item_entity.lua
1 -- Minetest: builtin/item_entity.lua
2
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.
8         if obj then
9                 obj:get_luaentity():set_item(stack:to_string())
10         end
11         return obj
12 end
13
14 -- If item_entity_ttl is not set, enity will have default life time
15 -- Setting it to -1 disables the feature
16
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
19
20
21 core.register_entity(":__builtin:item", {
22         initial_properties = {
23                 hp_max = 1,
24                 physical = true,
25                 collide_with_objects = false,
26                 collisionbox = {-0.3, -0.3, -0.3, 0.3, 0.3, 0.3},
27                 visual = "wielditem",
28                 visual_size = {x = 0.4, y = 0.4},
29                 textures = {""},
30                 spritediv = {x = 1, y = 1},
31                 initial_sprite_basepos = {x = 0, y = 0},
32                 is_visible = false,
33         },
34
35         itemstring = "",
36         moving_state = true,
37         slippery_state = false,
38         age = 0,
39
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
44                         -- item not yet known
45                         return
46                 end
47
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"
51
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
56
57                 self.object:set_properties({
58                         is_visible = true,
59                         visual = "wielditem",
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,
67                 })
68
69         end,
70
71         get_staticdata = function(self)
72                 return core.serialize({
73                         itemstring = self.itemstring,
74                         age = self.age,
75                         dropped_by = self.dropped_by
76                 })
77         end,
78
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
86                         end
87                 else
88                         self.itemstring = staticdata
89                 end
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})
93                 self:set_item()
94         end,
95
96         try_merge_with = function(self, own_stack, object, entity)
97                 if self.age == entity.age then
98                         -- Can not merge with itself
99                         return false
100                 end
101
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
109                         return false
110                 end
111
112                 local count = own_stack:get_count()
113                 local total_count = stack:get_count() + count
114                 local max_count = stack:get_stack_max()
115
116                 if total_count > max_count then
117                         return false
118                 end
119                 -- Merge the remote stack into this one
120
121                 local pos = object:get_pos()
122                 pos.y = pos.y + ((total_count - count) / max_count) * 0.15
123                 self.object:move_to(pos)
124
125                 self.age = 0 -- Handle as new entity
126                 own_stack:set_count(total_count)
127                 self:set_item(own_stack)
128
129                 entity.itemstring = ""
130                 object:remove()
131                 return true
132         end,
133
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
137                         self.itemstring = ""
138                         self.object:remove()
139                         return
140                 end
141
142                 local pos = self.object:get_pos()
143                 local node = core.get_node_or_nil({
144                         x = pos.x,
145                         y = pos.y + self.object:get_properties().collisionbox[2] - 0.05,
146                         z = pos.z
147                 })
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
154
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,
163                                         y = 0,
164                                         z = -vel.z * slip_factor
165                                 })
166                         elseif vel.y == 0 then
167                                 is_moving = false
168                         end
169                 end
170
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
174                         return
175                 end
176
177                 self.moving_state = is_moving
178                 self.slippery_state = is_slippery
179
180                 if is_moving then
181                         self.object:set_acceleration({x = 0, y = -gravity, z = 0})
182                 else
183                         self.object:set_acceleration({x = 0, y = 0, z = 0})
184                         self.object:set_velocity({x = 0, y = 0, z = 0})
185                 end
186
187                 --Only collect items if not moving
188                 if is_moving then
189                         return
190                 end
191                 -- Collect the items around to merge with
192                 local own_stack = ItemStack(self.itemstring)
193                 if own_stack:get_free_space() == 0 then
194                         return
195                 end
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
203                                                 return
204                                         end
205                                 end
206                         end
207                 end
208         end,
209
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
215                                 self:set_item(left)
216                                 return
217                         end
218                 end
219                 self.itemstring = ""
220                 self.object:remove()
221         end,
222 })