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