]> git.lizzy.rs Git - minetest.git/blob - builtin/game/falling.lua
Fix wrong video_driver setting when changing in mainmenu
[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 stack = ItemStack(node.name)
22                 local itemtable = stack:to_table()
23                 local itemname = nil
24                 if itemtable then
25                         itemname = stack:to_table().name
26                 end
27                 local item_texture = nil
28                 local item_type = ""
29                 if core.registered_items[itemname] then
30                         item_texture = core.registered_items[itemname].inventory_image
31                         item_type = core.registered_items[itemname].type
32                 end
33                 prop = {
34                         is_visible = true,
35                         textures = {node.name},
36                 }
37                 self.object:set_properties(prop)
38         end,
39
40         get_staticdata = function(self)
41                 return self.node.name
42         end,
43
44         on_activate = function(self, staticdata)
45                 self.object:set_armor_groups({immortal=1})
46                 --self.object:setacceleration({x=0, y=-10, z=0})
47                 self:set_node({name=staticdata})
48         end,
49
50         on_step = function(self, dtime)
51                 -- Set gravity
52                 self.object:setacceleration({x=0, y=-10, z=0})
53                 -- Turn to actual sand when collides to ground or just move
54                 local pos = self.object:getpos()
55                 local bcp = {x=pos.x, y=pos.y-0.7, z=pos.z} -- Position of bottom center point
56                 local bcn = core.get_node(bcp)
57                 local bcd = core.registered_nodes[bcn.name]
58                 -- Note: walkable is in the node definition, not in item groups
59                 if not bcd or
60                                 (bcd.walkable or
61                                 (core.get_item_group(self.node.name, "float") ~= 0 and
62                                 bcd.liquidtype ~= "none")) then
63                         if bcd and bcd.leveled and
64                                         bcn.name == self.node.name then
65                                 local addlevel = self.node.level
66                                 if addlevel == nil or addlevel <= 0 then
67                                         addlevel = bcd.leveled
68                                 end
69                                 if core.add_node_level(bcp, addlevel) == 0 then
70                                         self.object:remove()
71                                         return
72                                 end
73                         elseif bcd and bcd.buildable_to and
74                                         (core.get_item_group(self.node.name, "float") == 0 or
75                                         bcd.liquidtype == "none") then
76                                 core.remove_node(bcp)
77                                 return
78                         end
79                         local np = {x=bcp.x, y=bcp.y+1, z=bcp.z}
80                         -- Check what's here
81                         local n2 = core.get_node(np)
82                         -- If it's not air or liquid, remove node and replace it with
83                         -- it's drops
84                         if n2.name ~= "air" and (not core.registered_nodes[n2.name] or
85                                         core.registered_nodes[n2.name].liquidtype == "none") then
86                                 core.remove_node(np)
87                                 if core.registered_nodes[n2.name].buildable_to == false then
88                                         -- Add dropped items
89                                         local drops = core.get_node_drops(n2.name, "")
90                                         local _, dropped_item
91                                         for _, dropped_item in ipairs(drops) do
92                                                 core.add_item(np, dropped_item)
93                                         end
94                                 end
95                                 -- Run script hook
96                                 local _, callback
97                                 for _, callback in ipairs(core.registered_on_dignodes) do
98                                         callback(np, n2, nil)
99                                 end
100                         end
101                         -- Create node and remove entity
102                         core.add_node(np, self.node)
103                         self.object:remove()
104                         nodeupdate(np)
105                 else
106                         -- Do nothing
107                 end
108         end
109 })
110
111 function spawn_falling_node(p, node)
112         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                 if n.param2 == 0 then
134                         d.y = 1
135                 elseif n.param2 == 1 then
136                         d.y = -1
137                 elseif n.param2 == 2 then
138                         d.x = 1
139                 elseif n.param2 == 3 then
140                         d.x = -1
141                 elseif n.param2 == 4 then
142                         d.z = 1
143                 elseif n.param2 == 5 then
144                         d.z = -1
145                 end
146         else
147                 d.y = -1
148         end
149         local p2 = {x=p.x+d.x, y=p.y+d.y, z=p.z+d.z}
150         local nn = core.get_node(p2).name
151         local def2 = core.registered_nodes[nn]
152         if def2 and not def2.walkable then
153                 return false
154         end
155         return true
156 end
157
158 --
159 -- Some common functions
160 --
161
162 function nodeupdate_single(p, delay)
163         n = core.get_node(p)
164         if core.get_item_group(n.name, "falling_node") ~= 0 then
165                 p_bottom = {x=p.x, y=p.y-1, z=p.z}
166                 n_bottom = core.get_node(p_bottom)
167                 -- Note: walkable is in the node definition, not in item groups
168                 if core.registered_nodes[n_bottom.name] and
169                                 (core.get_item_group(n.name, "float") == 0 or
170                                         core.registered_nodes[n_bottom.name].liquidtype == "none") and
171                                 (n.name ~= n_bottom.name or (core.registered_nodes[n_bottom.name].leveled and
172                                         core.get_node_level(p_bottom) < core.get_node_max_level(p_bottom))) and
173                                 (not core.registered_nodes[n_bottom.name].walkable or
174                                         core.registered_nodes[n_bottom.name].buildable_to) then
175                         if delay then
176                                 core.after(0.1, nodeupdate_single, {x=p.x, y=p.y, z=p.z}, false)
177                         else
178                                 n.level = core.env:get_node_level(p)
179                                 core.remove_node(p)
180                                 spawn_falling_node(p, n)
181                                 nodeupdate(p)
182                         end
183                 end
184         end
185
186         if core.get_item_group(n.name, "attached_node") ~= 0 then
187                 if not check_attached_node(p, n) then
188                         drop_attached_node(p)
189                         nodeupdate(p)
190                 end
191         end
192 end
193
194 function nodeupdate(p, delay)
195         -- Round p to prevent falling entities to get stuck
196         p.x = math.floor(p.x+0.5)
197         p.y = math.floor(p.y+0.5)
198         p.z = math.floor(p.z+0.5)
199
200         for x = -1,1 do
201         for y = -1,1 do
202         for z = -1,1 do
203                 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))
204         end
205         end
206         end
207 end
208
209 --
210 -- Global callbacks
211 --
212
213 function on_placenode(p, node)
214         nodeupdate(p)
215 end
216 core.register_on_placenode(on_placenode)
217
218 function on_dignode(p, node)
219         nodeupdate(p)
220 end
221 core.register_on_dignode(on_dignode)