Translated using Weblate (German)
[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                 physical = true,
10                 collide_with_objects = false,
11                 collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
12                 visual = "wielditem",
13                 textures = {},
14                 visual_size = {x=0.667, y=0.667},
15         },
16
17         node = {},
18
19         set_node = function(self, node)
20                 self.node = node
21                 local prop = {
22                         is_visible = true,
23                         textures = {node.name},
24                 }
25                 self.object:set_properties(prop)
26         end,
27
28         get_staticdata = function(self)
29                 return self.node.name
30         end,
31
32         on_activate = function(self, staticdata)
33                 self.object:set_armor_groups({immortal=1})
34                 if staticdata then
35                         self:set_node({name=staticdata})
36                 end
37         end,
38
39         on_step = function(self, dtime)
40                 -- Set gravity
41                 self.object:setacceleration({x=0, y=-10, z=0})
42                 -- Turn to actual sand when collides to ground or just move
43                 local pos = self.object:getpos()
44                 local bcp = {x=pos.x, y=pos.y-0.7, z=pos.z} -- Position of bottom center point
45                 local bcn = core.get_node(bcp)
46                 local bcd = core.registered_nodes[bcn.name]
47                 -- Note: walkable is in the node definition, not in item groups
48                 if not bcd or
49                                 (bcd.walkable or
50                                 (core.get_item_group(self.node.name, "float") ~= 0 and
51                                 bcd.liquidtype ~= "none")) then
52                         if bcd and bcd.leveled and
53                                         bcn.name == self.node.name then
54                                 local addlevel = self.node.level
55                                 if addlevel == nil or addlevel <= 0 then
56                                         addlevel = bcd.leveled
57                                 end
58                                 if core.add_node_level(bcp, addlevel) == 0 then
59                                         self.object:remove()
60                                         return
61                                 end
62                         elseif bcd and bcd.buildable_to and
63                                         (core.get_item_group(self.node.name, "float") == 0 or
64                                         bcd.liquidtype == "none") then
65                                 core.remove_node(bcp)
66                                 return
67                         end
68                         local np = {x=bcp.x, y=bcp.y+1, z=bcp.z}
69                         -- Check what's here
70                         local n2 = core.get_node(np)
71                         -- If it's not air or liquid, remove node and replace it with
72                         -- it's drops
73                         if n2.name ~= "air" and (not core.registered_nodes[n2.name] or
74                                         core.registered_nodes[n2.name].liquidtype == "none") then
75                                 core.remove_node(np)
76                                 if core.registered_nodes[n2.name].buildable_to == false then
77                                         -- Add dropped items
78                                         local drops = core.get_node_drops(n2.name, "")
79                                         local _, dropped_item
80                                         for _, dropped_item in ipairs(drops) do
81                                                 core.add_item(np, dropped_item)
82                                         end
83                                 end
84                                 -- Run script hook
85                                 local _, callback
86                                 for _, callback in ipairs(core.registered_on_dignodes) do
87                                         callback(np, n2, nil)
88                                 end
89                         end
90                         -- Create node and remove entity
91                         core.add_node(np, self.node)
92                         self.object:remove()
93                         nodeupdate(np)
94                         return
95                 end
96                 local vel = self.object:getvelocity()
97                 if vector.equals(vel, {x=0,y=0,z=0}) then
98                         local npos = self.object:getpos()
99                         self.object:setpos(vector.round(npos))
100                 end
101         end
102 })
103
104 function spawn_falling_node(p, node)
105         local obj = core.add_entity(p, "__builtin:falling_node")
106         obj:get_luaentity():set_node(node)
107 end
108
109 function drop_attached_node(p)
110         local nn = core.get_node(p).name
111         core.remove_node(p)
112         for _,item in ipairs(core.get_node_drops(nn, "")) do
113                 local pos = {
114                         x = p.x + math.random()/2 - 0.25,
115                         y = p.y + math.random()/2 - 0.25,
116                         z = p.z + math.random()/2 - 0.25,
117                 }
118                 core.add_item(pos, item)
119         end
120 end
121
122 function check_attached_node(p, n)
123         local def = core.registered_nodes[n.name]
124         local d = {x=0, y=0, z=0}
125         if def.paramtype2 == "wallmounted" then
126                 if n.param2 == 0 then
127                         d.y = 1
128                 elseif n.param2 == 1 then
129                         d.y = -1
130                 elseif n.param2 == 2 then
131                         d.x = 1
132                 elseif n.param2 == 3 then
133                         d.x = -1
134                 elseif n.param2 == 4 then
135                         d.z = 1
136                 elseif n.param2 == 5 then
137                         d.z = -1
138                 end
139         else
140                 d.y = -1
141         end
142         local p2 = {x=p.x+d.x, y=p.y+d.y, z=p.z+d.z}
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 nodeupdate_single(p, delay)
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                 local n_bottom = core.get_node(p_bottom)
160                 -- Note: walkable is in the node definition, not in item groups
161                 if core.registered_nodes[n_bottom.name] and
162                                 (core.get_item_group(n.name, "float") == 0 or
163                                         core.registered_nodes[n_bottom.name].liquidtype == "none") and
164                                 (n.name ~= n_bottom.name or (core.registered_nodes[n_bottom.name].leveled and
165                                         core.get_node_level(p_bottom) < core.get_node_max_level(p_bottom))) and
166                                 (not core.registered_nodes[n_bottom.name].walkable or
167                                         core.registered_nodes[n_bottom.name].buildable_to) then
168                         if delay then
169                                 core.after(0.1, nodeupdate_single, {x=p.x, y=p.y, z=p.z}, false)
170                         else
171                                 n.level = core.get_node_level(p)
172                                 core.remove_node(p)
173                                 spawn_falling_node(p, n)
174                                 nodeupdate(p)
175                         end
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                         nodeupdate(p)
183                 end
184         end
185 end
186
187 function nodeupdate(p, delay)
188         -- Round p to prevent falling entities to get stuck
189         p.x = math.floor(p.x+0.5)
190         p.y = math.floor(p.y+0.5)
191         p.z = math.floor(p.z+0.5)
192
193         for x = -1,1 do
194         for y = -1,1 do
195         for z = -1,1 do
196                 nodeupdate_single({x=p.x+x, y=p.y+y, z=p.z+z}, delay or not (x==0 and y==0 and z==0))
197         end
198         end
199         end
200 end
201
202 --
203 -- Global callbacks
204 --
205
206 function on_placenode(p, node)
207         nodeupdate(p)
208 end
209 core.register_on_placenode(on_placenode)
210
211 function on_dignode(p, node)
212         nodeupdate(p)
213 end
214 core.register_on_dignode(on_dignode)