]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/falling.lua
Merge pull request #447 from sapier/add_lua_log_parameter_check
[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                 collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
11                 visual = "wielditem",
12                 textures = {},
13                 visual_size = {x=0.667, y=0.667},
14         },
15
16         nodename = "",
17
18         set_node = function(self, nodename)
19                 self.nodename = nodename
20                 local stack = ItemStack(nodename)
21                 local itemtable = stack:to_table()
22                 local itemname = nil
23                 if itemtable then
24                         itemname = stack:to_table().name
25                 end
26                 local item_texture = nil
27                 local item_type = ""
28                 if minetest.registered_items[itemname] then
29                         item_texture = minetest.registered_items[itemname].inventory_image
30                         item_type = minetest.registered_items[itemname].type
31                 end
32                 prop = {
33                         is_visible = true,
34                         textures = {nodename},
35                 }
36                 self.object:set_properties(prop)
37         end,
38
39         get_staticdata = function(self)
40                 return self.nodename
41         end,
42
43         on_activate = function(self, staticdata)
44                 self.nodename = staticdata
45                 self.object:set_armor_groups({immortal=1})
46                 --self.object:setacceleration({x=0, y=-10, z=0})
47                 self:set_node(self.nodename)
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.env: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 then
60                         local np = {x=bcp.x, y=bcp.y+1, z=bcp.z}
61                         -- Check what's here
62                         local n2 = minetest.env:get_node(np)
63                         -- If it's not air or liquid, remove node and replace it with
64                         -- it's drops
65                         if n2.name ~= "air" and (not minetest.registered_nodes[n2.name] or
66                                         minetest.registered_nodes[n2.name].liquidtype == "none") then
67                                 local drops = minetest.get_node_drops(n2.name, "")
68                                 minetest.env:remove_node(np)
69                                 -- Add dropped items
70                                 local _, dropped_item
71                                 for _, dropped_item in ipairs(drops) do
72                                         minetest.env:add_item(np, dropped_item)
73                                 end
74                                 -- Run script hook
75                                 local _, callback
76                                 for _, callback in ipairs(minetest.registered_on_dignodes) do
77                                         callback(np, n2, nil)
78                                 end
79                         end
80                         -- Create node and remove entity
81                         minetest.env:add_node(np, {name=self.nodename})
82                         self.object:remove()
83                 else
84                         -- Do nothing
85                 end
86         end
87 })
88
89 function spawn_falling_node(p, nodename)
90         obj = minetest.env:add_entity(p, "__builtin:falling_node")
91         obj:get_luaentity():set_node(nodename)
92 end
93
94 function drop_attached_node(p)
95         local nn = minetest.env:get_node(p).name
96         minetest.env:remove_node(p)
97         for _,item in ipairs(minetest.get_node_drops(nn, "")) do
98                 local pos = {
99                         x = p.x + math.random()/2 - 0.25,
100                         y = p.y + math.random()/2 - 0.25,
101                         z = p.z + math.random()/2 - 0.25,
102                 }
103                 minetest.env:add_item(pos, item)
104         end
105 end
106
107 function check_attached_node(p, n)
108         local def = minetest.registered_nodes[n.name]
109         local d = {x=0, y=0, z=0}
110         if def.paramtype2 == "wallmounted" then
111                 if n.param2 == 0 then
112                         d.y = 1
113                 elseif n.param2 == 1 then
114                         d.y = -1
115                 elseif n.param2 == 2 then
116                         d.x = 1
117                 elseif n.param2 == 3 then
118                         d.x = -1
119                 elseif n.param2 == 4 then
120                         d.z = 1
121                 elseif n.param2 == 5 then
122                         d.z = -1
123                 end
124         else
125                 d.y = -1
126         end
127         local p2 = {x=p.x+d.x, y=p.y+d.y, z=p.z+d.z}
128         local nn = minetest.env:get_node(p2).name
129         local def2 = minetest.registered_nodes[nn]
130         if def2 and not def2.walkable then
131                 return false
132         end
133         return true
134 end
135
136 --
137 -- Some common functions
138 --
139
140 function nodeupdate_single(p)
141         n = minetest.env:get_node(p)
142         if minetest.get_node_group(n.name, "falling_node") ~= 0 then
143                 p_bottom = {x=p.x, y=p.y-1, z=p.z}
144                 n_bottom = minetest.env:get_node(p_bottom)
145                 -- Note: walkable is in the node definition, not in item groups
146                 if minetest.registered_nodes[n_bottom.name] and
147                                 not minetest.registered_nodes[n_bottom.name].walkable then
148                         minetest.env:remove_node(p)
149                         spawn_falling_node(p, n.name)
150                         nodeupdate(p)
151                 end
152         end
153         
154         if minetest.get_node_group(n.name, "attached_node") ~= 0 then
155                 if not check_attached_node(p, n) then
156                         drop_attached_node(p)
157                         nodeupdate(p)
158                 end
159         end
160 end
161
162 function nodeupdate(p)
163         -- Round p to prevent falling entities to get stuck
164         p.x = math.floor(p.x+0.5)
165         p.y = math.floor(p.y+0.5)
166         p.z = math.floor(p.z+0.5)
167         
168         for x = -1,1 do
169         for y = -1,1 do
170         for z = -1,1 do
171                 p2 = {x=p.x+x, y=p.y+y, z=p.z+z}
172                 nodeupdate_single(p2)
173         end
174         end
175         end
176 end
177
178 --
179 -- Global callbacks
180 --
181
182 function on_placenode(p, node)
183         nodeupdate(p)
184 end
185 minetest.register_on_placenode(on_placenode)
186
187 function on_dignode(p, node)
188         nodeupdate(p)
189 end
190 minetest.register_on_dignode(on_dignode)