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)
103 default.cool_lava = function(pos, node)
104 if node.name == "default:lava_source" then
105 minetest.set_node(pos, {name = "default:obsidian"})
107 minetest.set_node(pos, {name = "default:stone"})
109 minetest.sound_play("default_cool_lava",
110 {pos = pos, max_hear_distance = 16, gain = 0.25})
113 minetest.register_abm({
114 label = "Lava cooling",
115 nodenames = {"default:lava_source", "default:lava_flowing"},
116 neighbors = {"group:water"},
120 action = function(...)
121 default.cool_lava(...)
127 -- optimized helper to put all items in an inventory into a drops list
130 function default.get_inventory_drops(pos, inventory, drops)
131 local inv = minetest.get_meta(pos):get_inventory()
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()
143 -- Papyrus and cactus growing
146 -- wrapping the functions in abm action is necessary to make overriding them possible
148 function default.grow_cactus(pos, node)
149 if node.param2 >= 4 then
153 if minetest.get_item_group(minetest.get_node(pos).name, "sand") == 0 then
158 while node.name == "default:cactus" and height < 4 do
161 node = minetest.get_node(pos)
163 if height == 4 or node.name ~= "air" then
166 minetest.set_node(pos, {name = "default:cactus"})
170 function default.grow_papyrus(pos, node)
172 local name = minetest.get_node(pos).name
173 if name ~= "default:dirt_with_grass" and name ~= "default:dirt" then
176 if not minetest.find_node_near(pos, 3, {"group:water"}) then
181 while node.name == "default:papyrus" and height < 4 do
184 node = minetest.get_node(pos)
186 if height == 4 or node.name ~= "air" then
189 minetest.set_node(pos, {name = "default:papyrus"})
193 minetest.register_abm({
194 label = "Grow cactus",
195 nodenames = {"default:cactus"},
196 neighbors = {"group:sand"},
199 action = function(...)
200 default.grow_cactus(...)
204 minetest.register_abm({
205 label = "Grow papyrus",
206 nodenames = {"default:papyrus"},
207 neighbors = {"default:dirt", "default:dirt_with_grass"},
210 action = function(...)
211 default.grow_papyrus(...)
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)
231 -- Fence registration helper
234 function default.register_fence(name, def)
235 minetest.register_craft({
236 output = name .. " 4",
238 { def.material, 'group:stick', def.material },
239 { def.material, 'group:stick', def.material },
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 = {
248 drawtype = "nodebox",
251 fixed = {{-1/8, -1/2, -1/8, 1/8, 1/2, 1/8}},
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}},
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,
271 for k, v in pairs(default_fields) do
277 -- Always add to the fence group, even if no group provided
283 minetest.register_node(name, def)
291 -- Prevent decay of placed leaves
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)
297 minetest.set_node(pos, node)
303 minetest.register_abm({
304 label = "Leaf decay",
305 nodenames = {"group:leafdecay"},
311 action = function(pos, node, _, _)
312 -- Check if leaf is placed
313 if node.param2 ~= 0 then
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
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
329 x = pos.x - 0.5 + math.random(),
330 y = pos.y - 0.5 + math.random(),
331 z = pos.z - 0.5 + math.random(),
333 minetest.add_item(p_drop, itemname)
337 minetest.remove_node(pos)
344 -- Convert dirt to something that fits the environment
347 minetest.register_abm({
348 label = "Grass spread",
349 nodenames = {"default:dirt"},
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
367 -- Look for spreading dirt-type neighbours
368 local p2 = minetest.find_node_near(pos, 1, "group:spreading_dirt_type")
370 local n3 = minetest.get_node(p2)
371 minetest.set_node(pos, {name = n3.name})
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"})
391 -- Grass and dry grass removed in darkness
394 minetest.register_abm({
395 label = "Grass covered",
396 nodenames = {"group:spreading_dirt_type"},
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"})
414 -- Moss growth on cobble near water
417 minetest.register_abm({
418 label = "Moss growth",
419 nodenames = {"default:cobble", "stairs:slab_cobble", "stairs:stair_cobble"},
420 neighbors = {"group:water"},
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})
437 -- Checks if specified volume intersects a protected volume
440 function default.intersects_protection(minp, maxp, player_name, interval)
441 -- 'interval' is the largest allowed interval for the 3D lattice of checks
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).
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()'")
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
477 -- Coral death near air
480 minetest.register_abm({
481 nodenames = {"default:coral_brown", "default:coral_orange"},
486 action = function(pos, node)
487 minetest.set_node(pos, {name = "default:coral_skeleton"})