stop falling.lua error
[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
56                                 (bcd.walkable or
57                                 (core.get_item_group(self.node.name, "float") ~= 0 and
58                                 bcd.liquidtype ~= "none")) then
59                         if bcd and bcd.leveled and
60                                         bcn.name == self.node.name then
61                                 local addlevel = self.node.level
62                                 if not addlevel or addlevel <= 0 then
63                                         addlevel = bcd.leveled
64                                 end
65                                 if core.add_node_level(bcp, addlevel) == 0 then
66                                         self.object:remove()
67                                         return
68                                 end
69                         elseif bcd and bcd.buildable_to and
70                                         (core.get_item_group(self.node.name, "float") == 0 or
71                                         bcd.liquidtype == "none") then
72                                 core.remove_node(bcp)
73                                 return
74                         end
75                         local np = {x = bcp.x, y = bcp.y + 1, z = bcp.z}
76                         -- Check what's here
77                         local n2 = core.get_node(np)
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 core.registered_nodes[n2.name] or
81                                         core.registered_nodes[n2.name].liquidtype == "none") then
82                                 core.remove_node(np)
83                                 if core.registered_nodes[n2.name].buildable_to == false then
84                                         -- Add dropped items
85                                         local drops = core.get_node_drops(n2.name, "")
86                                         for _, dropped_item in ipairs(drops) do
87                                                 core.add_item(np, dropped_item)
88                                         end
89                                 end
90                                 -- Run script hook
91                                 for _, callback in ipairs(core.registered_on_dignodes) do
92                                         callback(np, n2)
93                                 end
94                         end
95                         -- Create node and remove entity
96                         if core.registered_nodes[self.node.name] then
97                                 core.add_node(np, self.node)
98                         end
99                         self.object:remove()
100                         nodeupdate(np)
101                         return
102                 end
103                 local vel = self.object:getvelocity()
104                 if vector.equals(vel, {x = 0, y = 0, z = 0}) then
105                         local npos = self.object:getpos()
106                         self.object:setpos(vector.round(npos))
107                 end
108         end
109 })
110
111 function spawn_falling_node(p, node)
112         local obj = core.add_entity(p, "__builtin:falling_node")
113         obj:get_luaentity():set_node(node)
114 end
115
116 function drop_attached_node(p)
117         local nn = core.get_node(p).name
118         core.remove_node(p)
119         for _, item in ipairs(core.get_node_drops(nn, "")) do
120                 local pos = {
121                         x = p.x + math.random()/2 - 0.25,
122                         y = p.y + math.random()/2 - 0.25,
123                         z = p.z + math.random()/2 - 0.25,
124                 }
125                 core.add_item(pos, item)
126         end
127 end
128
129 function check_attached_node(p, n)
130         local def = core.registered_nodes[n.name]
131         local d = {x = 0, y = 0, z = 0}
132         if def.paramtype2 == "wallmounted" then
133                 d = core.wallmounted_to_dir(n.param2)
134         else
135                 d.y = -1
136         end
137         local p2 = vector.add(p, d)
138         local nn = core.get_node(p2).name
139         local def2 = core.registered_nodes[nn]
140         if def2 and not def2.walkable then
141                 return false
142         end
143         return true
144 end
145
146 --
147 -- Some common functions
148 --
149
150 function nodeupdate_single(p, delay)
151         local n = core.get_node(p)
152         if core.get_item_group(n.name, "falling_node") ~= 0 then
153                 local p_bottom = {x = p.x, y = p.y - 1, z = p.z}
154                 local n_bottom = core.get_node(p_bottom)
155                 -- Note: walkable is in the node definition, not in item groups
156                 if core.registered_nodes[n_bottom.name] and
157                                 (core.get_item_group(n.name, "float") == 0 or
158                                         core.registered_nodes[n_bottom.name].liquidtype == "none") and
159                                 (n.name ~= n_bottom.name or (core.registered_nodes[n_bottom.name].leveled and
160                                         core.get_node_level(p_bottom) < core.get_node_max_level(p_bottom))) and
161                                 (not core.registered_nodes[n_bottom.name].walkable or
162                                         core.registered_nodes[n_bottom.name].buildable_to) then
163                         if delay then
164                                 core.after(0.1, nodeupdate_single, p, false)
165                         else
166                                 n.level = core.get_node_level(p)
167                                 core.remove_node(p)
168                                 spawn_falling_node(p, n)
169                                 nodeupdate(p)
170                         end
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                         nodeupdate(p)
178                 end
179         end
180 end
181
182 function nodeupdate(p, delay)
183         -- Round p to prevent falling entities to get stuck
184         p = vector.round(p)
185
186         for x = -1, 1 do
187         for y = -1, 1 do
188         for z = -1, 1 do
189                 local d = vector.new(x, y, z)
190                 nodeupdate_single(vector.add(p, d), delay or not (x == 0 and y == 0 and z == 0))
191         end
192         end
193         end
194 end
195
196 --
197 -- Global callbacks
198 --
199
200 function on_placenode(p, node)
201         nodeupdate(p)
202 end
203 core.register_on_placenode(on_placenode)
204
205 function on_dignode(p, node)
206         nodeupdate(p)
207 end
208 core.register_on_dignode(on_dignode)