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