]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/item_entity.lua
Randomwalk caves: Extend beyond mapchunk vertically also (#9094)
[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                 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         physical_state = true,
39         -- Item expiry
40         age = 0,
41         -- Pushing item out of solid nodes
42         force_out = nil,
43         force_out_start = nil,
44
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
49                         -- item not yet known
50                         return
51                 end
52
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"
56
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
61
62                 self.object:set_properties({
63                         is_visible = true,
64                         visual = "wielditem",
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,
72                 })
73
74         end,
75
76         get_staticdata = function(self)
77                 return core.serialize({
78                         itemstring = self.itemstring,
79                         age = self.age,
80                         dropped_by = self.dropped_by
81                 })
82         end,
83
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
91                         end
92                 else
93                         self.itemstring = staticdata
94                 end
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})
98                 self:set_item()
99         end,
100
101         try_merge_with = function(self, own_stack, object, entity)
102                 if self.age == entity.age then
103                         -- Can not merge with itself
104                         return false
105                 end
106
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
114                         return false
115                 end
116
117                 local count = own_stack:get_count()
118                 local total_count = stack:get_count() + count
119                 local max_count = stack:get_stack_max()
120
121                 if total_count > max_count then
122                         return false
123                 end
124                 -- Merge the remote stack into this one
125
126                 local pos = object:get_pos()
127                 pos.y = pos.y + ((total_count - count) / max_count) * 0.15
128                 self.object:move_to(pos)
129
130                 self.age = 0 -- Handle as new entity
131                 own_stack:set_count(total_count)
132                 self:set_item(own_stack)
133
134                 entity.itemstring = ""
135                 object:remove()
136                 return true
137         end,
138
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})
145                 end
146         end,
147
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})
154                 end
155         end,
156
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
160                         self.itemstring = ""
161                         self.object:remove()
162                         return
163                 end
164
165                 local pos = self.object:get_pos()
166                 local node = core.get_node_or_nil({
167                         x = pos.x,
168                         y = pos.y + self.object:get_properties().collisionbox[2] - 0.05,
169                         z = pos.z
170                 })
171                 -- Delete in 'ignore' nodes
172                 if node and node.name == "ignore" then
173                         self.itemstring = ""
174                         self.object:remove()
175                         return
176                 end
177
178                 local is_stuck = false
179                 local snode = core.get_node_or_nil(pos)
180                 if snode then
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")
185                 end
186
187                 -- Push item out when stuck inside solid node
188                 if is_stuck then
189                         local shootdir
190                         local order = {
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},
193                         }
194
195                         -- Check which one of the 4 sides is free
196                         for o = 1, #order do
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
200                                         shootdir = order[o]
201                                         break
202                                 end
203                         end
204                         -- If none of the 4 sides is free, check upwards
205                         if not shootdir then
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
210                                 end
211                         end
212
213                         if shootdir then
214                                 -- Set new item moving speed accordingly
215                                 local newv = vector.multiply(shootdir, 3)
216                                 self:disable_physics()
217                                 self.object:set_velocity(newv)
218
219                                 self.force_out = newv
220                                 self.force_out_start = vector.round(pos)
221                                 return
222                         end
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)
234                         if ok then
235                                 -- Item was successfully forced out
236                                 self.force_out = nil
237                                 self:enable_physics()
238                         end
239                 end
240
241                 if not self.physical_state then
242                         return -- Don't do anything
243                 end
244
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
251
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,
260                                         y = 0,
261                                         z = -vel.z * slip_factor
262                                 })
263                         elseif vel.y == 0 then
264                                 is_moving = false
265                         end
266                 end
267
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
271                         return
272                 end
273
274                 self.moving_state = is_moving
275                 self.slippery_state = is_slippery
276
277                 if is_moving then
278                         self.object:set_acceleration({x = 0, y = -gravity, z = 0})
279                 else
280                         self.object:set_acceleration({x = 0, y = 0, z = 0})
281                         self.object:set_velocity({x = 0, y = 0, z = 0})
282                 end
283
284                 --Only collect items if not moving
285                 if is_moving then
286                         return
287                 end
288                 -- Collect the items around to merge with
289                 local own_stack = ItemStack(self.itemstring)
290                 if own_stack:get_free_space() == 0 then
291                         return
292                 end
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
300                                                 return
301                                         end
302                                 end
303                         end
304                 end
305         end,
306
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
312                                 self:set_item(left)
313                                 return
314                         end
315                 end
316                 self.itemstring = ""
317                 self.object:remove()
318         end,
319 })