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