Add Cobble Walls to Moss Growth ABM
[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.12}
44         table.dug = table.dug or
45                         {name = "default_sand_footstep", gain = 0.24}
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_gravel_defaults(table)
53         table = table or {}
54         table.footstep = table.footstep or
55                         {name = "default_gravel_footstep", gain = 0.5}
56         table.dug = table.dug or
57                         {name = "default_gravel_footstep", gain = 1.0}
58         table.place = table.place or
59                         {name = "default_place_node", gain = 1.0}
60         default.node_sound_defaults(table)
61         return table
62 end
63
64 function default.node_sound_wood_defaults(table)
65         table = table or {}
66         table.footstep = table.footstep or
67                         {name = "default_wood_footstep", gain = 0.5}
68         table.dug = table.dug or
69                         {name = "default_wood_footstep", gain = 1.0}
70         default.node_sound_defaults(table)
71         return table
72 end
73
74 function default.node_sound_leaves_defaults(table)
75         table = table or {}
76         table.footstep = table.footstep or
77                         {name = "default_grass_footstep", gain = 0.35}
78         table.dug = table.dug or
79                         {name = "default_grass_footstep", gain = 0.7}
80         table.dig = table.dig or
81                         {name = "default_dig_crumbly", gain = 0.4}
82         table.place = table.place or
83                         {name = "default_place_node", gain = 1.0}
84         default.node_sound_defaults(table)
85         return table
86 end
87
88 function default.node_sound_glass_defaults(table)
89         table = table or {}
90         table.footstep = table.footstep or
91                         {name = "default_glass_footstep", gain = 0.5}
92         table.dug = table.dug or
93                         {name = "default_break_glass", gain = 1.0}
94         default.node_sound_defaults(table)
95         return table
96 end
97
98 function default.node_sound_metal_defaults(table)
99         table = table or {}
100         table.footstep = table.footstep or
101                         {name = "default_metal_footstep", gain = 0.5}
102         table.dig = table.dig or
103                         {name = "default_dig_metal", gain = 0.5}
104         table.dug = table.dug or
105                         {name = "default_dug_metal", gain = 0.5}
106         table.place = table.place or
107                         {name = "default_place_node_metal", gain = 0.5}
108         default.node_sound_defaults(table)
109         return table
110 end
111
112 function default.node_sound_water_defaults(table)
113         table = table or {}
114         table.footstep = table.footstep or
115                         {name = "default_water_footstep", gain = 0.2}
116         default.node_sound_defaults(table)
117         return table
118 end
119
120 --
121 -- Lavacooling
122 --
123
124 default.cool_lava = function(pos, node)
125         if node.name == "default:lava_source" then
126                 minetest.set_node(pos, {name = "default:obsidian"})
127         else -- Lava flowing
128                 minetest.set_node(pos, {name = "default:stone"})
129         end
130         minetest.sound_play("default_cool_lava",
131                 {pos = pos, max_hear_distance = 16, gain = 0.25})
132 end
133
134 minetest.register_abm({
135         label = "Lava cooling",
136         nodenames = {"default:lava_source", "default:lava_flowing"},
137         neighbors = {"group:cools_lava", "group:water"},
138         interval = 1,
139         chance = 1,
140         catch_up = false,
141         action = function(...)
142                 default.cool_lava(...)
143         end,
144 })
145
146
147 --
148 -- optimized helper to put all items in an inventory into a drops list
149 --
150
151 function default.get_inventory_drops(pos, inventory, drops)
152         local inv = minetest.get_meta(pos):get_inventory()
153         local n = #drops
154         for i = 1, inv:get_size(inventory) do
155                 local stack = inv:get_stack(inventory, i)
156                 if stack:get_count() > 0 then
157                         drops[n+1] = stack:to_table()
158                         n = n + 1
159                 end
160         end
161 end
162
163 --
164 -- Papyrus and cactus growing
165 --
166
167 -- wrapping the functions in abm action is necessary to make overriding them possible
168
169 function default.grow_cactus(pos, node)
170         if node.param2 >= 4 then
171                 return
172         end
173         pos.y = pos.y - 1
174         if minetest.get_item_group(minetest.get_node(pos).name, "sand") == 0 then
175                 return
176         end
177         pos.y = pos.y + 1
178         local height = 0
179         while node.name == "default:cactus" and height < 4 do
180                 height = height + 1
181                 pos.y = pos.y + 1
182                 node = minetest.get_node(pos)
183         end
184         if height == 4 or node.name ~= "air" then
185                 return
186         end
187         minetest.set_node(pos, {name = "default:cactus"})
188         return true
189 end
190
191 function default.grow_papyrus(pos, node)
192         pos.y = pos.y - 1
193         local name = minetest.get_node(pos).name
194         if name ~= "default:dirt_with_grass" and name ~= "default:dirt" then
195                 return
196         end
197         if not minetest.find_node_near(pos, 3, {"group:water"}) then
198                 return
199         end
200         pos.y = pos.y + 1
201         local height = 0
202         while node.name == "default:papyrus" and height < 4 do
203                 height = height + 1
204                 pos.y = pos.y + 1
205                 node = minetest.get_node(pos)
206         end
207         if height == 4 or node.name ~= "air" then
208                 return
209         end
210         minetest.set_node(pos, {name = "default:papyrus"})
211         return true
212 end
213
214 minetest.register_abm({
215         label = "Grow cactus",
216         nodenames = {"default:cactus"},
217         neighbors = {"group:sand"},
218         interval = 12,
219         chance = 83,
220         action = function(...)
221                 default.grow_cactus(...)
222         end
223 })
224
225 minetest.register_abm({
226         label = "Grow papyrus",
227         nodenames = {"default:papyrus"},
228         neighbors = {"default:dirt", "default:dirt_with_grass"},
229         interval = 14,
230         chance = 71,
231         action = function(...)
232                 default.grow_papyrus(...)
233         end
234 })
235
236
237 --
238 -- dig upwards
239 --
240
241 function default.dig_up(pos, node, digger)
242         if digger == nil then return end
243         local np = {x = pos.x, y = pos.y + 1, z = pos.z}
244         local nn = minetest.get_node(np)
245         if nn.name == node.name then
246                 minetest.node_dig(np, nn, digger)
247         end
248 end
249
250
251 --
252 -- Fence registration helper
253 --
254
255 function default.register_fence(name, def)
256         minetest.register_craft({
257                 output = name .. " 4",
258                 recipe = {
259                         { def.material, 'group:stick', def.material },
260                         { def.material, 'group:stick', def.material },
261                 }
262         })
263
264         local fence_texture = "default_fence_overlay.png^" .. def.texture ..
265                         "^default_fence_overlay.png^[makealpha:255,126,126"
266         -- Allow almost everything to be overridden
267         local default_fields = {
268                 paramtype = "light",
269                 drawtype = "nodebox",
270                 node_box = {
271                         type = "connected",
272                         fixed = {{-1/8, -1/2, -1/8, 1/8, 1/2, 1/8}},
273                         -- connect_top =
274                         -- connect_bottom =
275                         connect_front = {{-1/16,3/16,-1/2,1/16,5/16,-1/8},
276                                 {-1/16,-5/16,-1/2,1/16,-3/16,-1/8}},
277                         connect_left = {{-1/2,3/16,-1/16,-1/8,5/16,1/16},
278                                 {-1/2,-5/16,-1/16,-1/8,-3/16,1/16}},
279                         connect_back = {{-1/16,3/16,1/8,1/16,5/16,1/2},
280                                 {-1/16,-5/16,1/8,1/16,-3/16,1/2}},
281                         connect_right = {{1/8,3/16,-1/16,1/2,5/16,1/16},
282                                 {1/8,-5/16,-1/16,1/2,-3/16,1/16}},
283                 },
284                 connects_to = {"group:fence", "group:wood", "group:tree"},
285                 inventory_image = fence_texture,
286                 wield_image = fence_texture,
287                 tiles = {def.texture},
288                 sunlight_propagates = true,
289                 is_ground_content = false,
290                 groups = {},
291         }
292         for k, v in pairs(default_fields) do
293                 if not def[k] then
294                         def[k] = v
295                 end
296         end
297
298         -- Always add to the fence group, even if no group provided
299         def.groups.fence = 1
300
301         def.texture = nil
302         def.material = nil
303
304         minetest.register_node(name, def)
305 end
306
307
308 --
309 -- Leafdecay
310 --
311
312 -- Prevent decay of placed leaves
313
314 default.after_place_leaves = function(pos, placer, itemstack, pointed_thing)
315         if placer and not placer:get_player_control().sneak then
316                 local node = minetest.get_node(pos)
317                 node.param2 = 1
318                 minetest.set_node(pos, node)
319         end
320 end
321
322 -- Leafdecay ABM
323
324 minetest.register_abm({
325         label = "Leaf decay",
326         nodenames = {"group:leafdecay"},
327         neighbors = {"air"},
328         interval = 2,
329         chance = 10,
330         catch_up = false,
331
332         action = function(pos, node, _, _)
333                 -- Check if leaf is placed
334                 if node.param2 ~= 0 then
335                         return
336                 end
337
338                 local rad = minetest.registered_nodes[node.name].groups.leafdecay
339                 -- Assume ignore is a trunk, to make this
340                 -- work at the border of a loaded area
341                 if minetest.find_node_near(pos, rad, {"ignore", "group:tree"}) then
342                         return
343                 end
344                 -- Drop stuff
345                 local itemstacks = minetest.get_node_drops(node.name)
346                 for _, itemname in ipairs(itemstacks) do
347                         if itemname ~= node.name or
348                                         minetest.get_item_group(node.name, "leafdecay_drop") ~= 0 then
349                                 local p_drop = {
350                                         x = pos.x - 0.5 + math.random(),
351                                         y = pos.y - 0.5 + math.random(),
352                                         z = pos.z - 0.5 + math.random(),
353                                 }
354                                 minetest.add_item(p_drop, itemname)
355                         end
356                 end
357                 -- Remove node
358                 minetest.remove_node(pos)
359                 minetest.check_for_falling(pos)
360         end
361 })
362
363
364 --
365 -- Convert dirt to something that fits the environment
366 --
367
368 minetest.register_abm({
369         label = "Grass spread",
370         nodenames = {"default:dirt"},
371         neighbors = {
372                 "air",
373                 "group:grass",
374                 "group:dry_grass",
375                 "default:snow",
376         },
377         interval = 6,
378         chance = 50,
379         catch_up = false,
380         action = function(pos, node)
381                 -- Check for darkness: night, shadow or under a light-blocking node
382                 -- Returns if ignore above
383                 local above = {x = pos.x, y = pos.y + 1, z = pos.z}
384                 if (minetest.get_node_light(above) or 0) < 13 then
385                         return
386                 end
387
388                 -- Look for spreading dirt-type neighbours
389                 local p2 = minetest.find_node_near(pos, 1, "group:spreading_dirt_type")
390                 if p2 then
391                         local n3 = minetest.get_node(p2)
392                         minetest.set_node(pos, {name = n3.name})
393                         return
394                 end
395
396                 -- Else, any seeding nodes on top?
397                 local name = minetest.get_node(above).name
398                 -- Snow check is cheapest, so comes first
399                 if name == "default:snow" then
400                         minetest.set_node(pos, {name = "default:dirt_with_snow"})
401                 -- Most likely case first
402                 elseif minetest.get_item_group(name, "grass") ~= 0 then
403                         minetest.set_node(pos, {name = "default:dirt_with_grass"})
404                 elseif minetest.get_item_group(name, "dry_grass") ~= 0 then
405                         minetest.set_node(pos, {name = "default:dirt_with_dry_grass"})
406                 end
407         end
408 })
409
410
411 --
412 -- Grass and dry grass removed in darkness
413 --
414
415 minetest.register_abm({
416         label = "Grass covered",
417         nodenames = {"group:spreading_dirt_type"},
418         interval = 8,
419         chance = 50,
420         catch_up = false,
421         action = function(pos, node)
422                 local above = {x = pos.x, y = pos.y + 1, z = pos.z}
423                 local name = minetest.get_node(above).name
424                 local nodedef = minetest.registered_nodes[name]
425                 if name ~= "ignore" and nodedef and not ((nodedef.sunlight_propagates or
426                                 nodedef.paramtype == "light") and
427                                 nodedef.liquidtype == "none") then
428                         minetest.set_node(pos, {name = "default:dirt"})
429                 end
430         end
431 })
432
433
434 --
435 -- Moss growth on cobble near water
436 --
437
438 minetest.register_abm({
439         label = "Moss growth",
440         nodenames = {"default:cobble", "stairs:slab_cobble", "stairs:stair_cobble", "walls:cobble"},
441         neighbors = {"group:water"},
442         interval = 16,
443         chance = 200,
444         catch_up = false,
445         action = function(pos, node)
446                 if node.name == "default:cobble" then
447                         minetest.set_node(pos, {name = "default:mossycobble"})
448                 elseif node.name == "stairs:slab_cobble" then
449                         minetest.set_node(pos, {name = "stairs:slab_mossycobble", param2 = node.param2})
450                 elseif node.name == "stairs:stair_cobble" then
451                         minetest.set_node(pos, {name = "stairs:stair_mossycobble", param2 = node.param2})
452                 elseif node.name == "walls:cobble" then
453                         minetest.set_node(pos, {name = "walls:mossycobble", param2 = node.param2})
454                 end
455         end
456 })
457
458
459 --
460 -- Checks if specified volume intersects a protected volume
461 --
462
463 function default.intersects_protection(minp, maxp, player_name, interval)
464         -- 'interval' is the largest allowed interval for the 3D lattice of checks
465
466         -- Compute the optimal float step 'd' for each axis so that all corners and
467         -- borders are checked. 'd' will be smaller or equal to 'interval'.
468         -- Subtracting 1e-4 ensures that the max co-ordinate will be reached by the
469         -- for loop (which might otherwise not be the case due to rounding errors).
470         local d = {}
471         for _, c in pairs({"x", "y", "z"}) do
472                 if maxp[c] > minp[c] then
473                         d[c] = (maxp[c] - minp[c]) / math.ceil((maxp[c] - minp[c]) / interval) - 1e-4
474                 elseif maxp[c] == minp[c] then
475                         d[c] = 1 -- Any value larger than 0 to avoid division by zero
476                 else -- maxp[c] < minp[c], print error and treat as protection intersected
477                         minetest.log("error", "maxp < minp in 'default.intersects_protection()'")
478                         return true
479                 end
480         end
481
482         for zf = minp.z, maxp.z, d.z do
483                 local z = math.floor(zf + 0.5)
484                 for yf = minp.y, maxp.y, d.y do
485                         local y = math.floor(yf + 0.5)
486                         for xf = minp.x, maxp.x, d.x do
487                                 local x = math.floor(xf + 0.5)
488                                 if minetest.is_protected({x = x, y = y, z = z}, player_name) then
489                                         return true
490                                 end
491                         end
492                 end
493         end
494
495         return false
496 end
497
498
499 --
500 -- Coral death near air
501 --
502
503 minetest.register_abm({
504         nodenames = {"default:coral_brown", "default:coral_orange"},
505         neighbors = {"air"},
506         interval = 17,
507         chance = 5,
508         catch_up = false,
509         action = function(pos, node)
510                 minetest.set_node(pos, {name = "default:coral_skeleton"})
511         end,
512 })