]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/falling.lua
Weather support
[dragonfireclient.git] / builtin / falling.lua
1 -- Minetest: builtin/item.lua
2
3 --
4 -- Falling stuff
5 --
6
7 minetest.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 minetest.registered_items[itemname] then
30                         item_texture = minetest.registered_items[itemname].inventory_image
31                         item_type = minetest.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 = minetest.get_node(bcp)
57                 -- Note: walkable is in the node definition, not in item groups
58                 if minetest.registered_nodes[bcn.name] and
59                                 minetest.registered_nodes[bcn.name].walkable or
60                                 (minetest.get_node_group(self.node.name, "float") ~= 0 and minetest.registered_nodes[bcn.name].liquidtype ~= "none")
61                         then
62                         if minetest.registered_nodes[bcn.name].buildable_to and (minetest.get_node_group(self.node.name, "float") == 0 or minetest.registered_nodes[bcn.name].liquidtype == "none") then
63                                 minetest.remove_node(bcp)
64                                 return
65                         end
66                         local np = {x=bcp.x, y=bcp.y+1, z=bcp.z}
67                         -- Check what's here
68                         local n2 = minetest.get_node(np)
69                         -- If it's not air or liquid, remove node and replace it with
70                         -- it's drops
71                         if n2.name ~= "air" and (not minetest.registered_nodes[n2.name] or
72                                         minetest.registered_nodes[n2.name].liquidtype == "none") then
73                                 local drops = minetest.get_node_drops(n2.name, "")
74                                 minetest.remove_node(np)
75                                 -- Add dropped items
76                                 local _, dropped_item
77                                 for _, dropped_item in ipairs(drops) do
78                                         minetest.add_item(np, dropped_item)
79                                 end
80                                 -- Run script hook
81                                 local _, callback
82                                 for _, callback in ipairs(minetest.registered_on_dignodes) do
83                                         callback(np, n2, nil)
84                                 end
85                         end
86                         -- Create node and remove entity
87                         minetest.add_node(np, self.node)
88                         self.object:remove()
89                         nodeupdate(np)
90                 else
91                         -- Do nothing
92                 end
93         end
94 })
95
96 function spawn_falling_node(p, node)
97         obj = minetest.add_entity(p, "__builtin:falling_node")
98         obj:get_luaentity():set_node(node)
99 end
100
101 function drop_attached_node(p)
102         local nn = minetest.get_node(p).name
103         minetest.remove_node(p)
104         for _,item in ipairs(minetest.get_node_drops(nn, "")) do
105                 local pos = {
106                         x = p.x + math.random()/2 - 0.25,
107                         y = p.y + math.random()/2 - 0.25,
108                         z = p.z + math.random()/2 - 0.25,
109                 }
110                 minetest.add_item(pos, item)
111         end
112 end
113
114 function check_attached_node(p, n)
115         local def = minetest.registered_nodes[n.name]
116         local d = {x=0, y=0, z=0}
117         if def.paramtype2 == "wallmounted" then
118                 if n.param2 == 0 then
119                         d.y = 1
120                 elseif n.param2 == 1 then
121                         d.y = -1
122                 elseif n.param2 == 2 then
123                         d.x = 1
124                 elseif n.param2 == 3 then
125                         d.x = -1
126                 elseif n.param2 == 4 then
127                         d.z = 1
128                 elseif n.param2 == 5 then
129                         d.z = -1
130                 end
131         else
132                 d.y = -1
133         end
134         local p2 = {x=p.x+d.x, y=p.y+d.y, z=p.z+d.z}
135         local nn = minetest.get_node(p2).name
136         local def2 = minetest.registered_nodes[nn]
137         if def2 and not def2.walkable then
138                 return false
139         end
140         return true
141 end
142
143 --
144 -- Some common functions
145 --
146
147 function nodeupdate_single(p, delay)
148         n = minetest.get_node(p)
149         if minetest.get_node_group(n.name, "falling_node") ~= 0 then
150                 p_bottom = {x=p.x, y=p.y-1, z=p.z}
151                 n_bottom = minetest.get_node(p_bottom)
152                 -- Note: walkable is in the node definition, not in item groups
153                 if minetest.registered_nodes[n_bottom.name] and
154                                 (minetest.get_node_group(n.name, "float") == 0 or minetest.registered_nodes[n_bottom.name].liquidtype == "none") and
155                                 (not minetest.registered_nodes[n_bottom.name].walkable or 
156                                         minetest.registered_nodes[n_bottom.name].buildable_to) then
157                         if delay then
158                                 minetest.after(0.1, nodeupdate_single, {x=p.x, y=p.y, z=p.z}, false)
159                         else
160                                 minetest.remove_node(p)
161                                 spawn_falling_node(p, n)
162                                 nodeupdate(p)
163                         end
164                 end
165         end
166         
167         if minetest.get_node_group(n.name, "attached_node") ~= 0 then
168                 if not check_attached_node(p, n) then
169                         drop_attached_node(p)
170                         nodeupdate(p)
171                 end
172         end
173 end
174
175 function nodeupdate(p, delay)
176         -- Round p to prevent falling entities to get stuck
177         p.x = math.floor(p.x+0.5)
178         p.y = math.floor(p.y+0.5)
179         p.z = math.floor(p.z+0.5)
180         
181         for x = -1,1 do
182         for y = -1,1 do
183         for z = -1,1 do
184                 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))
185         end
186         end
187         end
188 end
189
190 --
191 -- Global callbacks
192 --
193
194 function on_placenode(p, node)
195         nodeupdate(p)
196 end
197 minetest.register_on_placenode(on_placenode)
198
199 function on_dignode(p, node)
200         nodeupdate(p)
201 end
202 minetest.register_on_dignode(on_dignode)