]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/item_entity.lua
Builtin: Allow to revoke unknown privileges
[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                 self.object:set_properties({
63                         is_visible = true,
64                         visual = "wielditem",
65                         textures = {itemname},
66                         visual_size = {x = size + size_bias, y = size + size_bias},
67                         collisionbox = {-size, -size, -size, size, size, size},
68                         automatic_rotate = math.pi * 0.5 * 0.2 / size,
69                         wield_item = self.itemstring,
70                         glow = glow,
71                 })
72
73         end,
74
75         get_staticdata = function(self)
76                 return core.serialize({
77                         itemstring = self.itemstring,
78                         age = self.age,
79                         dropped_by = self.dropped_by
80                 })
81         end,
82
83         on_activate = function(self, staticdata, dtime_s)
84                 if string.sub(staticdata, 1, string.len("return")) == "return" then
85                         local data = core.deserialize(staticdata)
86                         if data and type(data) == "table" then
87                                 self.itemstring = data.itemstring
88                                 self.age = (data.age or 0) + dtime_s
89                                 self.dropped_by = data.dropped_by
90                         end
91                 else
92                         self.itemstring = staticdata
93                 end
94                 self.object:set_armor_groups({immortal = 1})
95                 self.object:set_velocity({x = 0, y = 2, z = 0})
96                 self.object:set_acceleration({x = 0, y = -gravity, z = 0})
97                 self:set_item()
98         end,
99
100         try_merge_with = function(self, own_stack, object, entity)
101                 if self.age == entity.age then
102                         -- Can not merge with itself
103                         return false
104                 end
105
106                 local stack = ItemStack(entity.itemstring)
107                 local name = stack:get_name()
108                 if own_stack:get_name() ~= name or
109                                 own_stack:get_meta() ~= stack:get_meta() or
110                                 own_stack:get_wear() ~= stack:get_wear() or
111                                 own_stack:get_free_space() == 0 then
112                         -- Can not merge different or full stack
113                         return false
114                 end
115
116                 local count = own_stack:get_count()
117                 local total_count = stack:get_count() + count
118                 local max_count = stack:get_stack_max()
119
120                 if total_count > max_count then
121                         return false
122                 end
123                 -- Merge the remote stack into this one
124
125                 local pos = object:get_pos()
126                 pos.y = pos.y + ((total_count - count) / max_count) * 0.15
127                 self.object:move_to(pos)
128
129                 self.age = 0 -- Handle as new entity
130                 own_stack:set_count(total_count)
131                 self:set_item(own_stack)
132
133                 entity.itemstring = ""
134                 object:remove()
135                 return true
136         end,
137
138         enable_physics = function(self)
139                 if not self.physical_state then
140                         self.physical_state = true
141                         self.object:set_properties({physical = true})
142                         self.object:set_velocity({x=0, y=0, z=0})
143                         self.object:set_acceleration({x=0, y=-gravity, z=0})
144                 end
145         end,
146
147         disable_physics = function(self)
148                 if self.physical_state then
149                         self.physical_state = false
150                         self.object:set_properties({physical = false})
151                         self.object:set_velocity({x=0, y=0, z=0})
152                         self.object:set_acceleration({x=0, y=0, z=0})
153                 end
154         end,
155
156         on_step = function(self, dtime, moveresult)
157                 self.age = self.age + dtime
158                 if time_to_live > 0 and self.age > time_to_live then
159                         self.itemstring = ""
160                         self.object:remove()
161                         return
162                 end
163
164                 local pos = self.object:get_pos()
165                 local node = core.get_node_or_nil({
166                         x = pos.x,
167                         y = pos.y + self.object:get_properties().collisionbox[2] - 0.05,
168                         z = pos.z
169                 })
170                 -- Delete in 'ignore' nodes
171                 if node and node.name == "ignore" then
172                         self.itemstring = ""
173                         self.object:remove()
174                         return
175                 end
176
177                 if self.force_out then
178                         -- This code runs after the entity got a push from the is_stuck code.
179                         -- It makes sure the entity is entirely outside the solid node
180                         local c = self.object:get_properties().collisionbox
181                         local s = self.force_out_start
182                         local f = self.force_out
183                         local ok = (f.x > 0 and pos.x + c[1] > s.x + 0.5) or
184                                 (f.y > 0 and pos.y + c[2] > s.y + 0.5) or
185                                 (f.z > 0 and pos.z + c[3] > s.z + 0.5) or
186                                 (f.x < 0 and pos.x + c[4] < s.x - 0.5) or
187                                 (f.z < 0 and pos.z + c[6] < s.z - 0.5)
188                         if ok then
189                                 -- Item was successfully forced out
190                                 self.force_out = nil
191                                 self:enable_physics()
192                                 return
193                         end
194                 end
195
196                 if not self.physical_state then
197                         return -- Don't do anything
198                 end
199
200                 assert(moveresult,
201                         "Collision info missing, this is caused by an out-of-date/buggy mod or game")
202
203                 if not moveresult.collides then
204                         -- future TODO: items should probably decelerate in air
205                         return
206                 end
207
208                 -- Push item out when stuck inside solid node
209                 local is_stuck = false
210                 local snode = core.get_node_or_nil(pos)
211                 if snode then
212                         local sdef = core.registered_nodes[snode.name] or {}
213                         is_stuck = (sdef.walkable == nil or sdef.walkable == true)
214                                 and (sdef.collision_box == nil or sdef.collision_box.type == "regular")
215                                 and (sdef.node_box == nil or sdef.node_box.type == "regular")
216                 end
217
218                 if is_stuck then
219                         local shootdir
220                         local order = {
221                                 {x=1, y=0, z=0}, {x=-1, y=0, z= 0},
222                                 {x=0, y=0, z=1}, {x= 0, y=0, z=-1},
223                         }
224
225                         -- Check which one of the 4 sides is free
226                         for o = 1, #order do
227                                 local cnode = core.get_node(vector.add(pos, order[o])).name
228                                 local cdef = core.registered_nodes[cnode] or {}
229                                 if cnode ~= "ignore" and cdef.walkable == false then
230                                         shootdir = order[o]
231                                         break
232                                 end
233                         end
234                         -- If none of the 4 sides is free, check upwards
235                         if not shootdir then
236                                 shootdir = {x=0, y=1, z=0}
237                                 local cnode = core.get_node(vector.add(pos, shootdir)).name
238                                 if cnode == "ignore" then
239                                         shootdir = nil -- Do not push into ignore
240                                 end
241                         end
242
243                         if shootdir then
244                                 -- Set new item moving speed accordingly
245                                 local newv = vector.multiply(shootdir, 3)
246                                 self:disable_physics()
247                                 self.object:set_velocity(newv)
248
249                                 self.force_out = newv
250                                 self.force_out_start = vector.round(pos)
251                                 return
252                         end
253                 end
254
255                 node = nil -- ground node we're colliding with
256                 if moveresult.touching_ground then
257                         for _, info in ipairs(moveresult.collisions) do
258                                 if info.axis == "y" then
259                                         node = core.get_node(info.node_pos)
260                                         break
261                                 end
262                         end
263                 end
264
265                 -- Slide on slippery nodes
266                 local def = node and core.registered_nodes[node.name]
267                 local keep_movement = false
268
269                 if def then
270                         local slippery = core.get_item_group(node.name, "slippery")
271                         local vel = self.object:get_velocity()
272                         if slippery ~= 0 and (math.abs(vel.x) > 0.1 or math.abs(vel.z) > 0.1) then
273                                 -- Horizontal deceleration
274                                 local factor = math.min(4 / (slippery + 4) * dtime, 1)
275                                 self.object:set_velocity({
276                                         x = vel.x * (1 - factor),
277                                         y = 0,
278                                         z = vel.z * (1 - factor)
279                                 })
280                                 keep_movement = true
281                         end
282                 end
283
284                 if not keep_movement then
285                         self.object:set_velocity({x=0, y=0, z=0})
286                 end
287
288                 if self.moving_state == keep_movement then
289                         -- Do not update anything until the moving state changes
290                         return
291                 end
292                 self.moving_state = keep_movement
293
294                 -- Only collect items if not moving
295                 if self.moving_state then
296                         return
297                 end
298                 -- Collect the items around to merge with
299                 local own_stack = ItemStack(self.itemstring)
300                 if own_stack:get_free_space() == 0 then
301                         return
302                 end
303                 local objects = core.get_objects_inside_radius(pos, 1.0)
304                 for k, obj in pairs(objects) do
305                         local entity = obj:get_luaentity()
306                         if entity and entity.name == "__builtin:item" then
307                                 if self:try_merge_with(own_stack, obj, entity) then
308                                         own_stack = ItemStack(self.itemstring)
309                                         if own_stack:get_free_space() == 0 then
310                                                 return
311                                         end
312                                 end
313                         end
314                 end
315         end,
316
317         on_punch = function(self, hitter)
318                 local inv = hitter:get_inventory()
319                 if inv and self.itemstring ~= "" then
320                         local left = inv:add_item("main", self.itemstring)
321                         if left and not left:is_empty() then
322                                 self:set_item(left)
323                                 return
324                         end
325                 end
326                 self.itemstring = ""
327                 self.object:remove()
328         end,
329 })