]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/falling.lua
b1beb1ab0c47bd5f20ff03d67862f76f1a2c7c8f
[dragonfireclient.git] / builtin / game / falling.lua
1 -- Minetest: builtin/item.lua
2
3 local builtin_shared = ...
4
5 --
6 -- Falling stuff
7 --
8
9 core.register_entity(":__builtin:falling_node", {
10         initial_properties = {
11                 visual = "wielditem",
12                 visual_size = {x = 0.667, y = 0.667},
13                 textures = {},
14                 physical = true,
15                 is_visible = false,
16                 collide_with_objects = false,
17                 collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
18         },
19
20         node = {},
21         meta = {},
22
23         set_node = function(self, node, meta)
24                 self.node = node
25                 self.meta = meta or {}
26                 self.object:set_properties({
27                         is_visible = true,
28                         textures = {node.name},
29                 })
30         end,
31
32         get_staticdata = function(self)
33                 local ds = {
34                         node = self.node,
35                         meta = self.meta,
36                 }
37                 return core.serialize(ds)
38         end,
39
40         on_activate = function(self, staticdata)
41                 self.object:set_armor_groups({immortal = 1})
42                 
43                 local ds = core.deserialize(staticdata)
44                 if ds and ds.node then
45                         self:set_node(ds.node, ds.meta)
46                 elseif ds then
47                         self:set_node(ds)
48                 elseif staticdata ~= "" then
49                         self:set_node({name = staticdata})
50                 end
51         end,
52
53         on_step = function(self, dtime)
54                 -- Set gravity
55                 local acceleration = self.object:getacceleration()
56                 if not vector.equals(acceleration, {x = 0, y = -10, z = 0}) then
57                         self.object:setacceleration({x = 0, y = -10, z = 0})
58                 end
59                 -- Turn to actual node when colliding with ground, or continue to move
60                 local pos = self.object:getpos()
61                 -- Position of bottom center point
62                 local bcp = {x = pos.x, y = pos.y - 0.7, z = pos.z}
63                 -- Avoid bugs caused by an unloaded node below
64                 local bcn = core.get_node_or_nil(bcp)
65                 local bcd = bcn and core.registered_nodes[bcn.name]
66                 if bcn and
67                                 (not bcd or bcd.walkable or
68                                 (core.get_item_group(self.node.name, "float") ~= 0 and
69                                 bcd.liquidtype ~= "none")) then
70                         if bcd and bcd.leveled and
71                                         bcn.name == self.node.name then
72                                 local addlevel = self.node.level
73                                 if not addlevel or addlevel <= 0 then
74                                         addlevel = bcd.leveled
75                                 end
76                                 if core.add_node_level(bcp, addlevel) == 0 then
77                                         self.object:remove()
78                                         return
79                                 end
80                         elseif bcd and bcd.buildable_to and
81                                         (core.get_item_group(self.node.name, "float") == 0 or
82                                         bcd.liquidtype == "none") then
83                                 core.remove_node(bcp)
84                                 return
85                         end
86                         local np = {x = bcp.x, y = bcp.y + 1, z = bcp.z}
87                         -- Check what's here
88                         local n2 = core.get_node(np)
89                         local nd = core.registered_nodes[n2.name]
90                         -- If it's not air or liquid, remove node and replace it with
91                         -- it's drops
92                         if n2.name ~= "air" and (not nd or nd.liquidtype == "none") then
93                                 core.remove_node(np)
94                                 if nd and nd.buildable_to == false then
95                                         -- Add dropped items
96                                         local drops = core.get_node_drops(n2.name, "")
97                                         for _, dropped_item in pairs(drops) do
98                                                 core.add_item(np, dropped_item)
99                                         end
100                                 end
101                                 -- Run script hook
102                                 for _, callback in pairs(core.registered_on_dignodes) do
103                                         callback(np, n2)
104                                 end
105                         end
106                         -- Create node and remove entity
107                         if core.registered_nodes[self.node.name] then
108                                 core.add_node(np, self.node)
109                                 if self.meta then
110                                         local meta = core.get_meta(np)
111                                         meta:from_table(self.meta)
112                                 end
113                         end
114                         self.object:remove()
115                         core.check_for_falling(np)
116                         return
117                 end
118                 local vel = self.object:getvelocity()
119                 if vector.equals(vel, {x = 0, y = 0, z = 0}) then
120                         local npos = self.object:getpos()
121                         self.object:setpos(vector.round(npos))
122                 end
123         end
124 })
125
126 local function spawn_falling_node(p, node, meta)
127         local obj = core.add_entity(p, "__builtin:falling_node")
128         if obj then
129                 obj:get_luaentity():set_node(node, meta)
130         end
131 end
132
133 function core.spawn_falling_node(pos)
134         local node = core.get_node(pos)
135         if node.name == "air" or node.name == "ignore" then
136                 return false
137         end
138         local obj = core.add_entity(pos, "__builtin:falling_node")
139         if obj then
140                 obj:get_luaentity():set_node(node)
141                 core.remove_node(pos)
142                 return true
143         end
144         return false
145 end
146
147 local function drop_attached_node(p)
148         local nn = core.get_node(p).name
149         core.remove_node(p)
150         for _, item in pairs(core.get_node_drops(nn, "")) do
151                 local pos = {
152                         x = p.x + math.random()/2 - 0.25,
153                         y = p.y + math.random()/2 - 0.25,
154                         z = p.z + math.random()/2 - 0.25,
155                 }
156                 core.add_item(pos, item)
157         end
158 end
159
160 function builtin_shared.check_attached_node(p, n)
161         local def = core.registered_nodes[n.name]
162         local d = {x = 0, y = 0, z = 0}
163         if def.paramtype2 == "wallmounted" or
164                         def.paramtype2 == "colorwallmounted" then
165                 -- The fallback vector here is in case 'wallmounted to dir' is nil due
166                 -- to voxelmanip placing a wallmounted node without resetting a
167                 -- pre-existing param2 value that is out-of-range for wallmounted.
168                 -- The fallback vector corresponds to param2 = 0.
169                 d = core.wallmounted_to_dir(n.param2) or {x = 0, y = 1, z = 0}
170         else
171                 d.y = -1
172         end
173         local p2 = vector.add(p, d)
174         local nn = core.get_node(p2).name
175         local def2 = core.registered_nodes[nn]
176         if def2 and not def2.walkable then
177                 return false
178         end
179         return true
180 end
181
182 --
183 -- Some common functions
184 --
185
186 function core.check_single_for_falling(p)
187         local n = core.get_node(p)
188         if core.get_item_group(n.name, "falling_node") ~= 0 then
189                 local p_bottom = {x = p.x, y = p.y - 1, z = p.z}
190                 -- Only spawn falling node if node below is loaded
191                 local n_bottom = core.get_node_or_nil(p_bottom)
192                 local d_bottom = n_bottom and core.registered_nodes[n_bottom.name]
193                 if d_bottom and
194
195                                 (core.get_item_group(n.name, "float") == 0 or
196                                 d_bottom.liquidtype == "none") and
197
198                                 (n.name ~= n_bottom.name or (d_bottom.leveled and
199                                 core.get_node_level(p_bottom) <
200                                 core.get_node_max_level(p_bottom))) and
201
202                                 (not d_bottom.walkable or d_bottom.buildable_to) then
203                         n.level = core.get_node_level(p)
204                         local meta = core.get_meta(p)
205                         local metatable = {}
206                         if meta ~= nil then
207                                 metatable = meta:to_table()
208                         end
209                         core.remove_node(p)
210                         spawn_falling_node(p, n, metatable)
211                         return true
212                 end
213         end
214
215         if core.get_item_group(n.name, "attached_node") ~= 0 then
216                 if not builtin_shared.check_attached_node(p, n) then
217                         drop_attached_node(p)
218                         return true
219                 end
220         end
221
222         return false
223 end
224
225 -- This table is specifically ordered.
226 -- We don't walk diagonals, only our direct neighbors, and self.
227 -- Down first as likely case, but always before self. The same with sides.
228 -- Up must come last, so that things above self will also fall all at once.
229 local check_for_falling_neighbors = {
230         {x = -1, y = -1, z = 0},
231         {x = 1, y = -1, z = 0},
232         {x = 0, y = -1, z = -1},
233         {x = 0, y = -1, z = 1},
234         {x = 0, y = -1, z = 0},
235         {x = -1, y = 0, z = 0},
236         {x = 1, y = 0, z = 0},
237         {x = 0, y = 0, z = 1},
238         {x = 0, y = 0, z = -1},
239         {x = 0, y = 0, z = 0},
240         {x = 0, y = 1, z = 0},
241 }
242
243 function core.check_for_falling(p)
244         -- Round p to prevent falling entities to get stuck.
245         p = vector.round(p)
246
247         -- We make a stack, and manually maintain size for performance.
248         -- Stored in the stack, we will maintain tables with pos, and
249         -- last neighbor visited. This way, when we get back to each
250         -- node, we know which directions we have already walked, and
251         -- which direction is the next to walk.
252         local s = {}
253         local n = 0
254         -- The neighbor order we will visit from our table.
255         local v = 1
256
257         while true do
258                 -- Push current pos onto the stack.
259                 n = n + 1
260                 s[n] = {p = p, v = v}
261                 -- Select next node from neighbor list.
262                 p = vector.add(p, check_for_falling_neighbors[v])
263                 -- Now we check out the node. If it is in need of an update,
264                 -- it will let us know in the return value (true = updated).
265                 if not core.check_single_for_falling(p) then
266                         -- If we don't need to "recurse" (walk) to it then pop
267                         -- our previous pos off the stack and continue from there,
268                         -- with the v value we were at when we last were at that
269                         -- node
270                         repeat
271                                 local pop = s[n]
272                                 p = pop.p
273                                 v = pop.v
274                                 s[n] = nil
275                                 n = n - 1
276                                 -- If there's nothing left on the stack, and no
277                                 -- more sides to walk to, we're done and can exit
278                                 if n == 0 and v == 11 then
279                                         return
280                                 end
281                         until v < 11
282                         -- The next round walk the next neighbor in list.
283                         v = v + 1
284                 else
285                         -- If we did need to walk the neighbor, then
286                         -- start walking it from the walk order start (1),
287                         -- and not the order we just pushed up the stack.
288                         v = 1
289                 end
290         end
291 end
292
293 --
294 -- Global callbacks
295 --
296
297 local function on_placenode(p, node)
298         core.check_for_falling(p)
299 end
300 core.register_on_placenode(on_placenode)
301
302 local function on_dignode(p, node)
303         core.check_for_falling(p)
304 end
305 core.register_on_dignode(on_dignode)
306
307 local function on_punchnode(p, node)
308         core.check_for_falling(p)
309 end
310 core.register_on_punchnode(on_punchnode)
311
312 --
313 -- Globally exported functions
314 --
315
316 -- TODO remove this function after the 0.4.15 release
317 function nodeupdate(p)
318         core.log("deprecated", "nodeupdate: deprecated, please use core.check_for_falling instead")
319         core.check_for_falling(p)
320 end
321
322 -- TODO remove this function after the 0.4.15 release
323 function nodeupdate_single(p)
324         core.log("deprecated", "nodeupdate_single: deprecated, please use core.check_single_for_falling instead")
325         core.check_single_for_falling(p)
326 end