586dfc45343616d8cbbd31d0cdfe74c97fe0a3c8
[oweals/minetest_game.git] / mods / default / functions.lua
1 -- mods/default/functions.lua
2
3 --
4 -- Sounds
5 --
6
7 function default.node_sound_defaults(table)
8         table = table or {}
9         table.footstep = table.footstep or
10                         {name="", gain=1.0}
11         table.dug = table.dug or
12                         {name="default_dug_node", gain=0.25}
13         table.place = table.place or
14                         {name="default_place_node_hard", gain=1.0}
15         return table
16 end
17
18 function default.node_sound_stone_defaults(table)
19         table = table or {}
20         table.footstep = table.footstep or
21                         {name="default_hard_footstep", gain=0.5}
22         table.dug = table.dug or
23                         {name="default_hard_footstep", gain=1.0}
24         default.node_sound_defaults(table)
25         return table
26 end
27
28 function default.node_sound_dirt_defaults(table)
29         table = table or {}
30         table.footstep = table.footstep or
31                         {name="default_dirt_footstep", gain=1.0}
32         table.dug = table.dug or
33                         {name="default_dirt_footstep", gain=1.5}
34         table.place = table.place or
35                         {name="default_place_node", gain=1.0}
36         default.node_sound_defaults(table)
37         return table
38 end
39
40 function default.node_sound_sand_defaults(table)
41         table = table or {}
42         table.footstep = table.footstep or
43                         {name="default_sand_footstep", gain=0.2}
44         table.dug = table.dug or
45                         {name="default_sand_footstep", gain=0.4}
46         table.place = table.place or
47                         {name="default_place_node", gain=1.0}
48         default.node_sound_defaults(table)
49         return table
50 end
51
52 function default.node_sound_wood_defaults(table)
53         table = table or {}
54         table.footstep = table.footstep or
55                         {name="default_wood_footstep", gain=0.5}
56         table.dug = table.dug or
57                         {name="default_wood_footstep", gain=1.0}
58         default.node_sound_defaults(table)
59         return table
60 end
61
62 function default.node_sound_leaves_defaults(table)
63         table = table or {}
64         table.footstep = table.footstep or
65                         {name="default_grass_footstep", gain=0.35}
66         table.dug = table.dug or
67                         {name="default_grass_footstep", gain=0.7}
68         table.dig = table.dig or
69                         {name="default_dig_crumbly", gain=0.4}
70         table.place = table.place or
71                         {name="default_place_node", gain=1.0}
72         default.node_sound_defaults(table)
73         return table
74 end
75
76 function default.node_sound_glass_defaults(table)
77         table = table or {}
78         table.footstep = table.footstep or
79                         {name="default_glass_footstep", gain=0.5}
80         table.dug = table.dug or
81                         {name="default_break_glass", gain=1.0}
82         default.node_sound_defaults(table)
83         return table
84 end
85
86 --
87 -- Legacy
88 --
89
90 function default.spawn_falling_node(p, nodename)
91         spawn_falling_node(p, nodename)
92 end
93
94 -- Horrible crap to support old code
95 -- Don't use this and never do what this does, it's completely wrong!
96 -- (More specifically, the client and the C++ code doesn't get the group)
97 function default.register_falling_node(nodename, texture)
98         minetest.log("error", debug.traceback())
99         minetest.log('error', "WARNING: default.register_falling_node is deprecated")
100         if minetest.registered_nodes[nodename] then
101                 minetest.registered_nodes[nodename].groups.falling_node = 1
102         end
103 end
104
105 --
106 -- Lavacooling
107 --
108
109 default.cool_lava_source = function(pos)
110         minetest.set_node(pos, {name="default:obsidian"})
111         minetest.sound_play("default_cool_lava", {pos = pos,  gain = 0.25})
112 end
113
114 default.cool_lava_flowing = function(pos)
115         minetest.set_node(pos, {name="default:stone"})
116         minetest.sound_play("default_cool_lava", {pos = pos,  gain = 0.25})
117 end
118
119 minetest.register_abm({
120         nodenames = {"default:lava_flowing"},
121         neighbors = {"group:water"},
122         interval = 1,
123         chance = 1,
124         action = function(pos, node, active_object_count, active_object_count_wider)
125                 default.cool_lava_flowing(pos, node, active_object_count, active_object_count_wider)
126         end,
127 })
128
129 minetest.register_abm({
130         nodenames = {"default:lava_source"},
131         neighbors = {"group:water"},
132         interval = 1,
133         chance = 1,
134         action = function(pos, node, active_object_count, active_object_count_wider)
135                 default.cool_lava_source(pos, node, active_object_count, active_object_count_wider)
136         end,
137 })
138
139 --
140 -- Papyrus and cactus growing
141 --
142
143 minetest.register_abm({
144         nodenames = {"default:cactus"},
145         neighbors = {"group:sand"},
146         interval = 50,
147         chance = 20,
148         action = function(pos, node)
149                 pos.y = pos.y-1
150                 local name = minetest.get_node(pos).name
151                 if minetest.get_item_group(name, "sand") ~= 0 then
152                         pos.y = pos.y+1
153                         local height = 0
154                         while minetest.get_node(pos).name == "default:cactus" and height < 4 do
155                                 height = height+1
156                                 pos.y = pos.y+1
157                         end
158                         if height < 4 then
159                                 if minetest.get_node(pos).name == "air" then
160                                         minetest.set_node(pos, {name="default:cactus"})
161                                 end
162                         end
163                 end
164         end,
165 })
166
167 minetest.register_abm({
168         nodenames = {"default:papyrus"},
169         neighbors = {"default:dirt", "default:dirt_with_grass"},
170         interval = 50,
171         chance = 20,
172         action = function(pos, node)
173                 pos.y = pos.y-1
174                 local name = minetest.get_node(pos).name
175                 if name == "default:dirt" or name == "default:dirt_with_grass" then
176                         if minetest.find_node_near(pos, 3, {"group:water"}) == nil then
177                                 return
178                         end
179                         pos.y = pos.y+1
180                         local height = 0
181                         while minetest.get_node(pos).name == "default:papyrus" and height < 4 do
182                                 height = height+1
183                                 pos.y = pos.y+1
184                         end
185                         if height < 4 then
186                                 if minetest.get_node(pos).name == "air" then
187                                         minetest.set_node(pos, {name="default:papyrus"})
188                                 end
189                         end
190                 end
191         end,
192 })
193
194 --
195 -- dig upwards
196 --
197
198 function default.dig_up(pos, node, digger)
199         if digger == nil then return end
200         local np = {x = pos.x, y = pos.y + 1, z = pos.z}
201         local nn = minetest.get_node(np)
202         if nn.name == node.name then
203                 minetest.node_dig(np, nn, digger)
204         end
205 end
206
207 --
208 -- Leafdecay
209 --
210
211 default.leafdecay_trunk_cache = {}
212 default.leafdecay_enable_cache = true
213 -- Spread the load of finding trunks
214 default.leafdecay_trunk_find_allow_accumulator = 0
215
216 minetest.register_globalstep(function(dtime)
217         local finds_per_second = 5000
218         default.leafdecay_trunk_find_allow_accumulator =
219                         math.floor(dtime * finds_per_second)
220 end)
221
222 default.after_place_leaves = function(pos, placer, itemstack, pointed_thing)
223         local node = minetest.get_node(pos)
224         node.param2 = 1
225         minetest.set_node(pos, node)
226 end
227
228 minetest.register_abm({
229         nodenames = {"group:leafdecay"},
230         neighbors = {"air", "group:liquid"},
231         -- A low interval and a high inverse chance spreads the load
232         interval = 2,
233         chance = 5,
234
235         action = function(p0, node, _, _)
236                 --print("leafdecay ABM at "..p0.x..", "..p0.y..", "..p0.z..")")
237                 local do_preserve = false
238                 local d = minetest.registered_nodes[node.name].groups.leafdecay
239                 if not d or d == 0 then
240                         --print("not groups.leafdecay")
241                         return
242                 end
243                 local n0 = minetest.get_node(p0)
244                 if n0.param2 ~= 0 then
245                         --print("param2 ~= 0")
246                         return
247                 end
248                 local p0_hash = nil
249                 if default.leafdecay_enable_cache then
250                         p0_hash = minetest.hash_node_position(p0)
251                         local trunkp = default.leafdecay_trunk_cache[p0_hash]
252                         if trunkp then
253                                 local n = minetest.get_node(trunkp)
254                                 local reg = minetest.registered_nodes[n.name]
255                                 -- Assume ignore is a trunk, to make the thing work at the border of the active area
256                                 if n.name == "ignore" or (reg and reg.groups.tree and reg.groups.tree ~= 0) then
257                                         --print("cached trunk still exists")
258                                         return
259                                 end
260                                 --print("cached trunk is invalid")
261                                 -- Cache is invalid
262                                 table.remove(default.leafdecay_trunk_cache, p0_hash)
263                         end
264                 end
265                 if default.leafdecay_trunk_find_allow_accumulator <= 0 then
266                         return
267                 end
268                 default.leafdecay_trunk_find_allow_accumulator =
269                                 default.leafdecay_trunk_find_allow_accumulator - 1
270                 -- Assume ignore is a trunk, to make the thing work at the border of the active area
271                 local p1 = minetest.find_node_near(p0, d, {"ignore", "group:tree"})
272                 if p1 then
273                         do_preserve = true
274                         if default.leafdecay_enable_cache then
275                                 --print("caching trunk")
276                                 -- Cache the trunk
277                                 default.leafdecay_trunk_cache[p0_hash] = p1
278                         end
279                 end
280                 if not do_preserve then
281                         -- Drop stuff other than the node itself
282                         local itemstacks = minetest.get_node_drops(n0.name)
283                         for _, itemname in ipairs(itemstacks) do
284                                 if minetest.get_item_group(n0.name, "leafdecay_drop") ~= 0 or
285                                                 itemname ~= n0.name then
286                                         local p_drop = {
287                                                 x = p0.x - 0.5 + math.random(),
288                                                 y = p0.y - 0.5 + math.random(),
289                                                 z = p0.z - 0.5 + math.random(),
290                                         }
291                                         minetest.add_item(p_drop, itemname)
292                                 end
293                         end
294                         -- Remove node
295                         minetest.remove_node(p0)
296                         nodeupdate(p0)
297                 end
298         end
299 })
300