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