]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/item_entity.lua
item_entity: Cache collisionbox for use in on_step
[dragonfireclient.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                 is_visible = false,
31         },
32
33         itemstring = "",
34         moving_state = true,
35         physical_state = true,
36         -- Item expiry
37         age = 0,
38         -- Pushing item out of solid nodes
39         force_out = nil,
40         force_out_start = nil,
41
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
46                         -- item not yet known
47                         return
48                 end
49
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"
53
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)
60
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({
64                         is_visible = true,
65                         visual = "wielditem",
66                         textures = {itemname},
67                         visual_size = {x = size + size_bias, y = size + size_bias},
68                         collisionbox = c,
69                         automatic_rotate = math.pi * 0.5 * 0.2 / size,
70                         wield_item = self.itemstring,
71                         glow = glow,
72                 })
73
74                 -- cache for usage in on_step
75                 self._collisionbox = c
76         end,
77
78         get_staticdata = function(self)
79                 return core.serialize({
80                         itemstring = self.itemstring,
81                         age = self.age,
82                         dropped_by = self.dropped_by
83                 })
84         end,
85
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
93                         end
94                 else
95                         self.itemstring = staticdata
96                 end
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
101                 self:set_item()
102         end,
103
104         try_merge_with = function(self, own_stack, object, entity)
105                 if self.age == entity.age then
106                         -- Can not merge with itself
107                         return false
108                 end
109
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
117                         return false
118                 end
119
120                 local count = own_stack:get_count()
121                 local total_count = stack:get_count() + count
122                 local max_count = stack:get_stack_max()
123
124                 if total_count > max_count then
125                         return false
126                 end
127                 -- Merge the remote stack into this one
128
129                 local pos = object:get_pos()
130                 pos.y = pos.y + ((total_count - count) / max_count) * 0.15
131                 self.object:move_to(pos)
132
133                 self.age = 0 -- Handle as new entity
134                 own_stack:set_count(total_count)
135                 self:set_item(own_stack)
136
137                 entity.itemstring = ""
138                 object:remove()
139                 return true
140         end,
141
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})
148                 end
149         end,
150
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})
157                 end
158         end,
159
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
163                         self.itemstring = ""
164                         self.object:remove()
165                         return
166                 end
167
168                 local pos = self.object:get_pos()
169                 local node = core.get_node_or_nil({
170                         x = pos.x,
171                         y = pos.y + self._collisionbox[2] - 0.05,
172                         z = pos.z
173                 })
174                 -- Delete in 'ignore' nodes
175                 if node and node.name == "ignore" then
176                         self.itemstring = ""
177                         self.object:remove()
178                         return
179                 end
180
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)
192                         if ok then
193                                 -- Item was successfully forced out
194                                 self.force_out = nil
195                                 self:enable_physics()
196                                 return
197                         end
198                 end
199
200                 if not self.physical_state then
201                         return -- Don't do anything
202                 end
203
204                 assert(moveresult,
205                         "Collision info missing, this is caused by an out-of-date/buggy mod or game")
206
207                 if not moveresult.collides then
208                         -- future TODO: items should probably decelerate in air
209                         return
210                 end
211
212                 -- Push item out when stuck inside solid node
213                 local is_stuck = false
214                 local snode = core.get_node_or_nil(pos)
215                 if snode then
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")
220                 end
221
222                 if is_stuck then
223                         local shootdir
224                         local order = {
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},
227                         }
228
229                         -- Check which one of the 4 sides is free
230                         for o = 1, #order do
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
234                                         shootdir = order[o]
235                                         break
236                                 end
237                         end
238                         -- If none of the 4 sides is free, check upwards
239                         if not shootdir then
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
244                                 end
245                         end
246
247                         if shootdir then
248                                 -- Set new item moving speed accordingly
249                                 local newv = vector.multiply(shootdir, 3)
250                                 self:disable_physics()
251                                 self.object:set_velocity(newv)
252
253                                 self.force_out = newv
254                                 self.force_out_start = vector.round(pos)
255                                 return
256                         end
257                 end
258
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)
264                                         break
265                                 end
266                         end
267                 end
268
269                 -- Slide on slippery nodes
270                 local def = node and core.registered_nodes[node.name]
271                 local keep_movement = false
272
273                 if def then
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),
281                                         y = 0,
282                                         z = vel.z * (1 - factor)
283                                 })
284                                 keep_movement = true
285                         end
286                 end
287
288                 if not keep_movement then
289                         self.object:set_velocity({x=0, y=0, z=0})
290                 end
291
292                 if self.moving_state == keep_movement then
293                         -- Do not update anything until the moving state changes
294                         return
295                 end
296                 self.moving_state = keep_movement
297
298                 -- Only collect items if not moving
299                 if self.moving_state then
300                         return
301                 end
302                 -- Collect the items around to merge with
303                 local own_stack = ItemStack(self.itemstring)
304                 if own_stack:get_free_space() == 0 then
305                         return
306                 end
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
314                                                 return
315                                         end
316                                 end
317                         end
318                 end
319         end,
320
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
326                                 self:set_item(left)
327                                 return
328                         end
329                 end
330                 self.itemstring = ""
331                 self.object:remove()
332         end,
333 })