Builtin/../falling.lua: Code optimisation
[oweals/minetest.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 sand when collides to ground or just move
50                 local pos = self.object:getpos()
51                 local bcp = {x = pos.x, y = pos.y - 0.7, z = pos.z} -- Position of bottom center point
52                 local bcn = core.get_node(bcp)
53                 local bcd = core.registered_nodes[bcn.name]
54                 -- Note: walkable is in the node definition, not in item groups
55                 if not bcd or bcd.walkable or
56                                 (core.get_item_group(self.node.name, "float") ~= 0 and
57                                 bcd.liquidtype ~= "none") then
58                         if bcd and bcd.leveled and
59                                         bcn.name == self.node.name then
60                                 local addlevel = self.node.level
61                                 if not addlevel or addlevel <= 0 then
62                                         addlevel = bcd.leveled
63                                 end
64                                 if core.add_node_level(bcp, addlevel) == 0 then
65                                         self.object:remove()
66                                         return
67                                 end
68                         elseif bcd and bcd.buildable_to and
69                                         (core.get_item_group(self.node.name, "float") == 0 or
70                                         bcd.liquidtype == "none") then
71                                 core.remove_node(bcp)
72                                 return
73                         end
74                         local np = {x = bcp.x, y = bcp.y + 1, z = bcp.z}
75                         -- Check what's here
76                         local n2 = core.get_node(np)
77                         local nd = core.registered_nodes[n2.name]
78                         -- If it's not air or liquid, remove node and replace it with
79                         -- it's drops
80                         if n2.name ~= "air" and (not nd or nd.liquidtype == "none") then
81                                 core.remove_node(np)
82                                 if nd.buildable_to == false then
83                                         -- Add dropped items
84                                         local drops = core.get_node_drops(n2.name, "")
85                                         for _, dropped_item in pairs(drops) do
86                                                 core.add_item(np, dropped_item)
87                                         end
88                                 end
89                                 -- Run script hook
90                                 for _, callback in pairs(core.registered_on_dignodes) do
91                                         callback(np, n2)
92                                 end
93                         end
94                         -- Create node and remove entity
95                         if core.registered_nodes[self.node.name] then
96                                 core.add_node(np, self.node)
97                         end
98                         self.object:remove()
99                         nodeupdate(np)
100                         return
101                 end
102                 local vel = self.object:getvelocity()
103                 if vector.equals(vel, {x = 0, y = 0, z = 0}) then
104                         local npos = self.object:getpos()
105                         self.object:setpos(vector.round(npos))
106                 end
107         end
108 })
109
110 function spawn_falling_node(p, node)
111         local obj = core.add_entity(p, "__builtin:falling_node")
112         obj:get_luaentity():set_node(node)
113 end
114
115 function drop_attached_node(p)
116         local nn = core.get_node(p).name
117         core.remove_node(p)
118         for _, item in pairs(core.get_node_drops(nn, "")) do
119                 local pos = {
120                         x = p.x + math.random()/2 - 0.25,
121                         y = p.y + math.random()/2 - 0.25,
122                         z = p.z + math.random()/2 - 0.25,
123                 }
124                 core.add_item(pos, item)
125         end
126 end
127
128 function check_attached_node(p, n)
129         local def = core.registered_nodes[n.name]
130         local d = {x = 0, y = 0, z = 0}
131         if def.paramtype2 == "wallmounted" then
132                 -- The fallback vector here is in case 'wallmounted to dir' is nil due
133                 -- to voxelmanip placing a wallmounted node without resetting a
134                 -- pre-existing param2 value that is out-of-range for wallmounted.
135                 -- The fallback vector corresponds to param2 = 0.
136                 d = core.wallmounted_to_dir(n.param2) or {x = 0, y = 1, z = 0}
137         else
138                 d.y = -1
139         end
140         local p2 = vector.add(p, d)
141         local nn = core.get_node(p2).name
142         local def2 = core.registered_nodes[nn]
143         if def2 and not def2.walkable then
144                 return false
145         end
146         return true
147 end
148
149 --
150 -- Some common functions
151 --
152
153 function nodeupdate_single(p)
154         local n = core.get_node(p)
155         if core.get_item_group(n.name, "falling_node") ~= 0 then
156                 local p_bottom = {x = p.x, y = p.y - 1, z = p.z}
157                 local n_bottom = core.get_node(p_bottom)
158                 local d_bottom = core.registered_nodes[n_bottom.name]
159                 -- Note: walkable is in the node definition, not in item groups
160                 if d_bottom and
161                                 (core.get_item_group(n.name, "float") == 0 or
162                                         d_bottom.liquidtype == "none") and
163                                 (n.name ~= n_bottom.name or (d_bottom.leveled and
164                                         core.get_node_level(p_bottom) < core.get_node_max_level(p_bottom))) and
165                                 (not d_bottom.walkable or
166                                         d_bottom.buildable_to) then
167                         n.level = core.get_node_level(p)
168                         core.remove_node(p)
169                         spawn_falling_node(p, n)
170                         return true
171                 end
172         end
173
174         if core.get_item_group(n.name, "attached_node") ~= 0 then
175                 if not check_attached_node(p, n) then
176                         drop_attached_node(p)
177                         return true
178                 end
179         end
180
181         return false
182 end
183
184 -- This table is specifically ordered.
185 -- We don't walk diagonals, only our direct neighbors, and self.
186 -- Down first as likely case, but always before self. The same with sides.
187 -- Up must come last, so that things above self will also fall all at once.
188 local nodeupdate_neighbors = {
189         {x = -1, y = -1, z = 0},
190         {x = 1, y = -1, z = 0},
191         {x = 0, y = -1, z = -1},
192         {x = 0, y = -1, z = 1},
193         {x = 0, y = -1, z = 0},
194         {x = -1, y = 0, z = 0},
195         {x = 1, y = 0, z = 0},
196         {x = 0, y = 0, z = 1},
197         {x = 0, y = 0, z = -1},
198         {x = 0, y = 0, z = 0},
199         {x = 0, y = 1, z = 0},
200 }
201
202 function nodeupdate(p)
203         -- Round p to prevent falling entities to get stuck.
204         p = vector.round(p)
205
206         -- We make a stack, and manually maintain size for performance.
207         -- Stored in the stack, we will maintain tables with pos, and
208         -- last neighbor visited. This way, when we get back to each
209         -- node, we know which directions we have already walked, and
210         -- which direction is the next to walk.
211         local s = {}
212         local n = 0
213         -- The neighbor order we will visit from our table.
214         local v = 1
215
216         while true do
217                 -- Push current pos onto the stack.
218                 n = n + 1
219                 s[n] = {p = p, v = v}
220                 -- Select next node from neighbor list.
221                 p = vector.add(p, nodeupdate_neighbors[v])
222                 -- Now we check out the node. If it is in need of an update,
223                 -- it will let us know in the return value (true = updated).
224                 if not nodeupdate_single(p) then
225                         -- If we don't need to "recurse" (walk) to it then pop
226                         -- our previous pos off the stack and continue from there,
227                         -- with the v value we were at when we last were at that
228                         -- node
229                         repeat
230                                 local pop = s[n]
231                                 p = pop.p
232                                 v = pop.v
233                                 s[n] = nil
234                                 n = n - 1
235                                 -- If there's nothing left on the stack, and no
236                                 -- more sides to walk to, we're done and can exit
237                                 if n == 0 and v == 11 then
238                                         return
239                                 end
240                         until v < 11
241                         -- The next round walk the next neighbor in list.
242                         v = v + 1
243                 else
244                         -- If we did need to walk the neighbor, then
245                         -- start walking it from the walk order start (1),
246                         -- and not the order we just pushed up the stack.
247                         v = 1
248                 end
249         end
250 end
251
252 --
253 -- Global callbacks
254 --
255
256 function on_placenode(p, node)
257         nodeupdate(p)
258 end
259 core.register_on_placenode(on_placenode)
260
261 function on_dignode(p, node)
262         nodeupdate(p)
263 end
264 core.register_on_dignode(on_dignode)
265
266 function on_punchnode(p, node)
267         nodeupdate(p)
268 end
269 core.register_on_punchnode(on_punchnode)