Consolidate ABMs
[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 --
88 -- Lavacooling
89 --
90
91 default.cool_lava = function(pos, node)
92         if node.name == "default:lava_source" then
93                 minetest.set_node(pos, {name = "default:obsidian"})
94         else -- Lava flowing
95                 minetest.set_node(pos, {name = "default:stone"})
96         end
97         minetest.sound_play("default_cool_lava",
98                 {pos = pos, max_hear_distance = 16, gain = 0.25})
99 end
100
101 minetest.register_abm({
102         nodenames = {"default:lava_source", "default:lava_flowing"},
103         neighbors = {"group:water"},
104         interval = 1,
105         chance = 1,
106         catch_up = false,
107         action = function(...)
108                 default.cool_lava(...)
109         end,
110 })
111
112
113 --
114 -- Papyrus and cactus growing
115 --
116
117 -- wrapping the functions in abm action is necessary to make overriding them possible
118
119 function default.grow_cactus(pos, node)
120         if node.param2 >= 4 then
121                 return
122         end
123         pos.y = pos.y - 1
124         if minetest.get_item_group(minetest.get_node(pos).name, "sand") == 0 then
125                 return
126         end
127         pos.y = pos.y + 1
128         local height = 0
129         while node.name == "default:cactus" and height < 4 do
130                 height = height + 1
131                 pos.y = pos.y + 1
132                 node = minetest.get_node(pos)
133         end
134         if height == 4 or node.name ~= "air" then
135                 return
136         end
137         minetest.set_node(pos, {name = "default:cactus"})
138         return true
139 end
140
141 function default.grow_papyrus(pos, node)
142         pos.y = pos.y - 1
143         local name = minetest.get_node(pos).name
144         if name ~= "default:dirt_with_grass" and name ~= "default:dirt" then
145                 return
146         end
147         if not minetest.find_node_near(pos, 3, {"group:water"}) then
148                 return
149         end
150         pos.y = pos.y + 1
151         local height = 0
152         while node.name == "default:papyrus" and height < 4 do
153                 height = height + 1
154                 pos.y = pos.y + 1
155                 node = minetest.get_node(pos)
156         end
157         if height == 4 or node.name ~= "air" then
158                 return
159         end
160         minetest.set_node(pos, {name = "default:papyrus"})
161         return true
162 end
163
164 minetest.register_abm({
165         nodenames = {"default:cactus"},
166         neighbors = {"group:sand"},
167         interval = 12,
168         chance = 83,
169         action = function(...)
170                 default.grow_cactus(...)
171         end
172 })
173
174 minetest.register_abm({
175         nodenames = {"default:papyrus"},
176         neighbors = {"default:dirt", "default:dirt_with_grass"},
177         interval = 14,
178         chance = 71,
179         action = function(...)
180                 default.grow_papyrus(...)
181         end
182 })
183
184
185 --
186 -- dig upwards
187 --
188
189 function default.dig_up(pos, node, digger)
190         if digger == nil then return end
191         local np = {x = pos.x, y = pos.y + 1, z = pos.z}
192         local nn = minetest.get_node(np)
193         if nn.name == node.name then
194                 minetest.node_dig(np, nn, digger)
195         end
196 end
197
198
199 --
200 -- Fence registration helper
201 --
202 function default.register_fence(name, def)
203         minetest.register_craft({
204                 output = name .. " 4",
205                 recipe = {
206                         { def.material, 'group:stick', def.material },
207                         { def.material, 'group:stick', def.material },
208                 }
209         })
210
211         local fence_texture = "default_fence_overlay.png^" .. def.texture ..
212                         "^default_fence_overlay.png^[makealpha:255,126,126"
213         -- Allow almost everything to be overridden
214         local default_fields = {
215                 paramtype = "light",
216                 drawtype = "fencelike",
217                 inventory_image = fence_texture,
218                 wield_image = fence_texture,
219                 tiles = { def.texture },
220                 sunlight_propagates = true,
221                 is_ground_content = false,
222                 selection_box = {
223                         type = "fixed",
224                         fixed = {-1/7, -1/2, -1/7, 1/7, 1/2, 1/7},
225                 },
226                 groups = {},
227         }
228         for k, v in pairs(default_fields) do
229                 if not def[k] then
230                         def[k] = v
231                 end
232         end
233
234         -- Always add to the fence group, even if no group provided
235         def.groups.fence = 1
236
237         def.texture = nil
238         def.material = nil
239
240         minetest.register_node(name, def)
241 end
242
243
244 --
245 -- Leafdecay
246 --
247
248 default.leafdecay_trunk_cache = {}
249 default.leafdecay_enable_cache = true
250 -- Spread the load of finding trunks
251 default.leafdecay_trunk_find_allow_accumulator = 0
252
253 minetest.register_globalstep(function(dtime)
254         local finds_per_second = 5000
255         default.leafdecay_trunk_find_allow_accumulator =
256                         math.floor(dtime * finds_per_second)
257 end)
258
259 default.after_place_leaves = function(pos, placer, itemstack, pointed_thing)
260         local node = minetest.get_node(pos)
261         node.param2 = 1
262         minetest.set_node(pos, node)
263 end
264
265 minetest.register_abm({
266         nodenames = {"group:leafdecay"},
267         neighbors = {"air", "group:liquid"},
268         -- A low interval and a high inverse chance spreads the load
269         interval = 2,
270         chance = 5,
271
272         action = function(p0, node, _, _)
273                 --print("leafdecay ABM at "..p0.x..", "..p0.y..", "..p0.z..")")
274                 local do_preserve = false
275                 local d = minetest.registered_nodes[node.name].groups.leafdecay
276                 if not d or d == 0 then
277                         --print("not groups.leafdecay")
278                         return
279                 end
280                 local n0 = minetest.get_node(p0)
281                 if n0.param2 ~= 0 then
282                         --print("param2 ~= 0")
283                         return
284                 end
285                 local p0_hash = nil
286                 if default.leafdecay_enable_cache then
287                         p0_hash = minetest.hash_node_position(p0)
288                         local trunkp = default.leafdecay_trunk_cache[p0_hash]
289                         if trunkp then
290                                 local n = minetest.get_node(trunkp)
291                                 local reg = minetest.registered_nodes[n.name]
292                                 -- Assume ignore is a trunk, to make the thing
293                                 -- work at the border of the active area
294                                 if n.name == "ignore" or (reg and reg.groups.tree and
295                                                 reg.groups.tree ~= 0) then
296                                         --print("cached trunk still exists")
297                                         return
298                                 end
299                                 --print("cached trunk is invalid")
300                                 -- Cache is invalid
301                                 table.remove(default.leafdecay_trunk_cache, p0_hash)
302                         end
303                 end
304                 if default.leafdecay_trunk_find_allow_accumulator <= 0 then
305                         return
306                 end
307                 default.leafdecay_trunk_find_allow_accumulator =
308                                 default.leafdecay_trunk_find_allow_accumulator - 1
309                 -- Assume ignore is a trunk, to make the thing
310                 -- work at the border of the active area
311                 local p1 = minetest.find_node_near(p0, d, {"ignore", "group:tree"})
312                 if p1 then
313                         do_preserve = true
314                         if default.leafdecay_enable_cache then
315                                 --print("caching trunk")
316                                 -- Cache the trunk
317                                 default.leafdecay_trunk_cache[p0_hash] = p1
318                         end
319                 end
320                 if not do_preserve then
321                         -- Drop stuff other than the node itself
322                         local itemstacks = minetest.get_node_drops(n0.name)
323                         for _, itemname in ipairs(itemstacks) do
324                                 if minetest.get_item_group(n0.name, "leafdecay_drop") ~= 0 or
325                                                 itemname ~= n0.name then
326                                         local p_drop = {
327                                                 x = p0.x - 0.5 + math.random(),
328                                                 y = p0.y - 0.5 + math.random(),
329                                                 z = p0.z - 0.5 + math.random(),
330                                         }
331                                         minetest.add_item(p_drop, itemname)
332                                 end
333                         end
334                         -- Remove node
335                         minetest.remove_node(p0)
336                         nodeupdate(p0)
337                 end
338         end
339 })
340
341
342 --
343 -- Grass growing on well-lit dirt
344 --
345
346 minetest.register_abm({
347         nodenames = {"default:dirt"},
348         neighbors = {"air"},
349         interval = 6,
350         chance = 67,
351         catch_up = false,
352         action = function(pos, node)
353                 local above = {x = pos.x, y = pos.y + 1, z = pos.z}
354                 local name = minetest.get_node(above).name
355                 local nodedef = minetest.registered_nodes[name]
356                 if nodedef and (nodedef.sunlight_propagates or nodedef.paramtype == "light") and
357                                 nodedef.liquidtype == "none" and
358                                 (minetest.get_node_light(above) or 0) >= 13 then
359                         if name == "default:snow" or name == "default:snowblock" then
360                                 minetest.set_node(pos, {name = "default:dirt_with_snow"})
361                         else
362                                 minetest.set_node(pos, {name = "default:dirt_with_grass"})
363                         end
364                 end
365         end
366 })
367
368
369 --
370 -- Grass and dry grass removed in darkness
371 --
372
373 minetest.register_abm({
374         nodenames = {"default:dirt_with_grass", "default:dirt_with_dry_grass"},
375         interval = 8,
376         chance = 50,
377         catch_up = false,
378         action = function(pos, node)
379                 local above = {x = pos.x, y = pos.y + 1, z = pos.z}
380                 local name = minetest.get_node(above).name
381                 local nodedef = minetest.registered_nodes[name]
382                 if name ~= "ignore" and nodedef and not ((nodedef.sunlight_propagates or
383                                 nodedef.paramtype == "light") and
384                                 nodedef.liquidtype == "none") then
385                         minetest.set_node(pos, {name = "default:dirt"})
386                 end
387         end
388 })
389
390
391 --
392 -- Moss growth on cobble near water
393 --
394
395 minetest.register_abm({
396         nodenames = {"default:cobble"},
397         neighbors = {"group:water"},
398         interval = 16,
399         chance = 200,
400         catch_up = false,
401         action = function(pos, node)
402                 minetest.set_node(pos, {name = "default:mossycobble"})
403         end
404 })