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_wood_defaults(table)
54 table.footstep = table.footstep or
55 {name = "default_wood_footstep", gain = 0.5}
56 table.dug = table.dug or
57 {name = "default_wood_footstep", gain = 1.0}
58 default.node_sound_defaults(table)
62 function default.node_sound_leaves_defaults(table)
64 table.footstep = table.footstep or
65 {name = "default_grass_footstep", gain = 0.35}
66 table.dug = table.dug or
67 {name = "default_grass_footstep", gain = 0.7}
68 table.dig = table.dig or
69 {name = "default_dig_crumbly", gain = 0.4}
70 table.place = table.place or
71 {name = "default_place_node", gain = 1.0}
72 default.node_sound_defaults(table)
76 function default.node_sound_glass_defaults(table)
78 table.footstep = table.footstep or
79 {name = "default_glass_footstep", gain = 0.5}
80 table.dug = table.dug or
81 {name = "default_break_glass", gain = 1.0}
82 default.node_sound_defaults(table)
91 default.cool_lava = function(pos, node)
92 if node.name == "default:lava_source" then
93 minetest.set_node(pos, {name = "default:obsidian"})
95 minetest.set_node(pos, {name = "default:stone"})
97 minetest.sound_play("default_cool_lava",
98 {pos = pos, max_hear_distance = 16, gain = 0.25})
101 minetest.register_abm({
102 nodenames = {"default:lava_source", "default:lava_flowing"},
103 neighbors = {"group:water"},
107 action = function(...)
108 default.cool_lava(...)
114 -- optimized helper to put all items in an inventory into a drops list
116 function default.get_inventory_drops(pos, inventory, drops)
117 local inv = minetest.get_meta(pos):get_inventory()
119 for i = 1, inv:get_size(inventory) do
120 local stack = inv:get_stack(inventory, i)
121 if stack:get_count() > 0 then
122 drops[n+1] = stack:to_table()
129 -- Papyrus and cactus growing
132 -- wrapping the functions in abm action is necessary to make overriding them possible
134 function default.grow_cactus(pos, node)
135 if node.param2 >= 4 then
139 if minetest.get_item_group(minetest.get_node(pos).name, "sand") == 0 then
144 while node.name == "default:cactus" and height < 4 do
147 node = minetest.get_node(pos)
149 if height == 4 or node.name ~= "air" then
152 minetest.set_node(pos, {name = "default:cactus"})
156 function default.grow_papyrus(pos, node)
158 local name = minetest.get_node(pos).name
159 if name ~= "default:dirt_with_grass" and name ~= "default:dirt" then
162 if not minetest.find_node_near(pos, 3, {"group:water"}) then
167 while node.name == "default:papyrus" and height < 4 do
170 node = minetest.get_node(pos)
172 if height == 4 or node.name ~= "air" then
175 minetest.set_node(pos, {name = "default:papyrus"})
179 minetest.register_abm({
180 nodenames = {"default:cactus"},
181 neighbors = {"group:sand"},
184 action = function(...)
185 default.grow_cactus(...)
189 minetest.register_abm({
190 nodenames = {"default:papyrus"},
191 neighbors = {"default:dirt", "default:dirt_with_grass"},
194 action = function(...)
195 default.grow_papyrus(...)
204 function default.dig_up(pos, node, digger)
205 if digger == nil then return end
206 local np = {x = pos.x, y = pos.y + 1, z = pos.z}
207 local nn = minetest.get_node(np)
208 if nn.name == node.name then
209 minetest.node_dig(np, nn, digger)
215 -- Fence registration helper
217 function default.register_fence(name, def)
218 minetest.register_craft({
219 output = name .. " 4",
221 { def.material, 'group:stick', def.material },
222 { def.material, 'group:stick', def.material },
226 local fence_texture = "default_fence_overlay.png^" .. def.texture ..
227 "^default_fence_overlay.png^[makealpha:255,126,126"
228 -- Allow almost everything to be overridden
229 local default_fields = {
231 drawtype = "nodebox",
234 fixed = {{-1/8, -1/2, -1/8, 1/8, 1/2, 1/8}},
237 connect_front = {{-1/16,3/16,-1/2,1/16,5/16,-1/8},
238 {-1/16,-5/16,-1/2,1/16,-3/16,-1/8}},
239 connect_left = {{-1/2,3/16,-1/16,-1/8,5/16,1/16},
240 {-1/2,-5/16,-1/16,-1/8,-3/16,1/16}},
241 connect_back = {{-1/16,3/16,1/8,1/16,5/16,1/2},
242 {-1/16,-5/16,1/8,1/16,-3/16,1/2}},
243 connect_right = {{1/8,3/16,-1/16,1/2,5/16,1/16},
244 {1/8,-5/16,-1/16,1/2,-3/16,1/16}},
246 connects_to = {"group:fence", "group:wood", "group:tree"},
247 inventory_image = fence_texture,
248 wield_image = fence_texture,
249 tiles = {def.texture},
250 sunlight_propagates = true,
251 is_ground_content = false,
254 for k, v in pairs(default_fields) do
260 -- Always add to the fence group, even if no group provided
266 minetest.register_node(name, def)
274 default.leafdecay_trunk_cache = {}
275 default.leafdecay_enable_cache = true
276 -- Spread the load of finding trunks
277 default.leafdecay_trunk_find_allow_accumulator = 0
279 minetest.register_globalstep(function(dtime)
280 local finds_per_second = 5000
281 default.leafdecay_trunk_find_allow_accumulator =
282 math.floor(dtime * finds_per_second)
285 default.after_place_leaves = function(pos, placer, itemstack, pointed_thing)
286 if placer and not placer:get_player_control().sneak then
287 local node = minetest.get_node(pos)
289 minetest.set_node(pos, node)
293 minetest.register_abm({
294 nodenames = {"group:leafdecay"},
295 neighbors = {"air", "group:liquid"},
296 -- A low interval and a high inverse chance spreads the load
300 action = function(p0, node, _, _)
301 --print("leafdecay ABM at "..p0.x..", "..p0.y..", "..p0.z..")")
302 local do_preserve = false
303 local d = minetest.registered_nodes[node.name].groups.leafdecay
304 if not d or d == 0 then
305 --print("not groups.leafdecay")
308 local n0 = minetest.get_node(p0)
309 if n0.param2 ~= 0 then
310 --print("param2 ~= 0")
314 if default.leafdecay_enable_cache then
315 p0_hash = minetest.hash_node_position(p0)
316 local trunkp = default.leafdecay_trunk_cache[p0_hash]
318 local n = minetest.get_node(trunkp)
319 local reg = minetest.registered_nodes[n.name]
320 -- Assume ignore is a trunk, to make the thing
321 -- work at the border of the active area
322 if n.name == "ignore" or (reg and reg.groups.tree and
323 reg.groups.tree ~= 0) then
324 --print("cached trunk still exists")
327 --print("cached trunk is invalid")
329 table.remove(default.leafdecay_trunk_cache, p0_hash)
332 if default.leafdecay_trunk_find_allow_accumulator <= 0 then
335 default.leafdecay_trunk_find_allow_accumulator =
336 default.leafdecay_trunk_find_allow_accumulator - 1
337 -- Assume ignore is a trunk, to make the thing
338 -- work at the border of the active area
339 local p1 = minetest.find_node_near(p0, d, {"ignore", "group:tree"})
342 if default.leafdecay_enable_cache then
343 --print("caching trunk")
345 default.leafdecay_trunk_cache[p0_hash] = p1
348 if not do_preserve then
349 -- Drop stuff other than the node itself
350 local itemstacks = minetest.get_node_drops(n0.name)
351 for _, itemname in ipairs(itemstacks) do
352 if minetest.get_item_group(n0.name, "leafdecay_drop") ~= 0 or
353 itemname ~= n0.name then
355 x = p0.x - 0.5 + math.random(),
356 y = p0.y - 0.5 + math.random(),
357 z = p0.z - 0.5 + math.random(),
359 minetest.add_item(p_drop, itemname)
363 minetest.remove_node(p0)
371 -- Convert dirt to something that fits the environment
374 minetest.register_abm({
375 nodenames = {"default:dirt"},
377 "default:dirt_with_grass",
378 "default:dirt_with_dry_grass",
379 "default:dirt_with_snow",
387 action = function(pos, node)
388 -- Most likely case, half the time it's too dark for this.
389 local above = {x = pos.x, y = pos.y + 1, z = pos.z}
390 if (minetest.get_node_light(above) or 0) < 13 then
394 -- Look for likely neighbors.
395 local p2 = minetest.find_node_near(pos, 1, {"default:dirt_with_grass",
396 "default:dirt_with_dry_grass", "default:dirt_with_snow"})
398 -- But the node needs to be under air in this case.
399 local n2 = minetest.get_node(above)
400 if n2 and n2.name == "air" then
401 local n3 = minetest.get_node(p2)
402 minetest.set_node(pos, {name = n3.name})
408 local n2 = minetest.get_node(above)
414 -- Snow check is cheapest, so comes first.
415 if name == "default:snow" then
416 minetest.set_node(pos, {name = "default:dirt_with_snow"})
417 -- Most likely case first.
418 elseif minetest.get_item_group(name, "grass") ~= 0 then
419 minetest.set_node(pos, {name = "default:dirt_with_grass"})
420 elseif minetest.get_item_group(name, "dry_grass") ~= 0 then
421 minetest.set_node(pos, {name = "default:dirt_with_dry_grass"})
427 -- Grass and dry grass removed in darkness
430 minetest.register_abm({
432 "default:dirt_with_grass",
433 "default:dirt_with_dry_grass",
434 "default:dirt_with_snow",
439 action = function(pos, node)
440 local above = {x = pos.x, y = pos.y + 1, z = pos.z}
441 local name = minetest.get_node(above).name
442 local nodedef = minetest.registered_nodes[name]
443 if name ~= "ignore" and nodedef and not ((nodedef.sunlight_propagates or
444 nodedef.paramtype == "light") and
445 nodedef.liquidtype == "none") then
446 minetest.set_node(pos, {name = "default:dirt"})
453 -- Moss growth on cobble near water
456 minetest.register_abm({
457 nodenames = {"default:cobble"},
458 neighbors = {"group:water"},
462 action = function(pos, node)
463 minetest.set_node(pos, {name = "default:mossycobble"})