1 -- mods/default/functions.lua
7 function default.node_sound_defaults(table)
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}
18 function default.node_sound_stone_defaults(table)
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)
28 function default.node_sound_dirt_defaults(table)
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)
40 function default.node_sound_sand_defaults(table)
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)
52 function default.node_sound_gravel_defaults(table)
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)
64 function default.node_sound_wood_defaults(table)
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)
74 function default.node_sound_leaves_defaults(table)
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)
88 function default.node_sound_glass_defaults(table)
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)
98 function default.node_sound_metal_defaults(table)
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)
112 function default.node_sound_water_defaults(table)
114 table.footstep = table.footstep or
115 {name = "default_water_footstep", gain = 0.2}
116 default.node_sound_defaults(table)
124 default.cool_lava = function(pos, node)
125 if node.name == "default:lava_source" then
126 minetest.set_node(pos, {name = "default:obsidian"})
128 minetest.set_node(pos, {name = "default:stone"})
130 minetest.sound_play("default_cool_lava",
131 {pos = pos, max_hear_distance = 16, gain = 0.25})
134 minetest.register_abm({
135 label = "Lava cooling",
136 nodenames = {"default:lava_source", "default:lava_flowing"},
137 neighbors = {"group:cools_lava", "group:water"},
141 action = function(...)
142 default.cool_lava(...)
148 -- optimized helper to put all items in an inventory into a drops list
151 function default.get_inventory_drops(pos, inventory, drops)
152 local inv = minetest.get_meta(pos):get_inventory()
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()
164 -- Papyrus and cactus growing
167 -- wrapping the functions in abm action is necessary to make overriding them possible
169 function default.grow_cactus(pos, node)
170 if node.param2 >= 4 then
174 if minetest.get_item_group(minetest.get_node(pos).name, "sand") == 0 then
179 while node.name == "default:cactus" and height < 4 do
182 node = minetest.get_node(pos)
184 if height == 4 or node.name ~= "air" then
187 minetest.set_node(pos, {name = "default:cactus"})
191 function default.grow_papyrus(pos, node)
193 local name = minetest.get_node(pos).name
194 if name ~= "default:dirt_with_grass" and name ~= "default:dirt" then
197 if not minetest.find_node_near(pos, 3, {"group:water"}) then
202 while node.name == "default:papyrus" and height < 4 do
205 node = minetest.get_node(pos)
207 if height == 4 or node.name ~= "air" then
210 minetest.set_node(pos, {name = "default:papyrus"})
214 minetest.register_abm({
215 label = "Grow cactus",
216 nodenames = {"default:cactus"},
217 neighbors = {"group:sand"},
220 action = function(...)
221 default.grow_cactus(...)
225 minetest.register_abm({
226 label = "Grow papyrus",
227 nodenames = {"default:papyrus"},
228 neighbors = {"default:dirt", "default:dirt_with_grass"},
231 action = function(...)
232 default.grow_papyrus(...)
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)
252 -- Fence registration helper
255 function default.register_fence(name, def)
256 minetest.register_craft({
257 output = name .. " 4",
259 { def.material, 'group:stick', def.material },
260 { def.material, 'group:stick', def.material },
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 = {
269 drawtype = "nodebox",
272 fixed = {{-1/8, -1/2, -1/8, 1/8, 1/2, 1/8}},
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}},
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,
292 for k, v in pairs(default_fields) do
298 -- Always add to the fence group, even if no group provided
304 minetest.register_node(name, def)
312 -- Prevent decay of placed leaves
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)
318 minetest.set_node(pos, node)
324 minetest.register_abm({
325 label = "Leaf decay",
326 nodenames = {"group:leafdecay"},
332 action = function(pos, node, _, _)
333 -- Check if leaf is placed
334 if node.param2 ~= 0 then
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
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
350 x = pos.x - 0.5 + math.random(),
351 y = pos.y - 0.5 + math.random(),
352 z = pos.z - 0.5 + math.random(),
354 minetest.add_item(p_drop, itemname)
358 minetest.remove_node(pos)
359 minetest.check_for_falling(pos)
365 -- Convert dirt to something that fits the environment
368 minetest.register_abm({
369 label = "Grass spread",
370 nodenames = {"default:dirt"},
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
388 -- Look for spreading dirt-type neighbours
389 local p2 = minetest.find_node_near(pos, 1, "group:spreading_dirt_type")
391 local n3 = minetest.get_node(p2)
392 minetest.set_node(pos, {name = n3.name})
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"})
412 -- Grass and dry grass removed in darkness
415 minetest.register_abm({
416 label = "Grass covered",
417 nodenames = {"group:spreading_dirt_type"},
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"})
435 -- Moss growth on cobble near water
438 minetest.register_abm({
439 label = "Moss growth",
440 nodenames = {"default:cobble", "stairs:slab_cobble", "stairs:stair_cobble", "walls:cobble"},
441 neighbors = {"group:water"},
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})
460 -- Checks if specified volume intersects a protected volume
463 function default.intersects_protection(minp, maxp, player_name, interval)
464 -- 'interval' is the largest allowed interval for the 3D lattice of checks
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).
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()'")
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
500 -- Coral death near air
503 minetest.register_abm({
504 nodenames = {"default:coral_brown", "default:coral_orange"},
509 action = function(pos, node)
510 minetest.set_node(pos, {name = "default:coral_skeleton"})