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.2}
44 table.dug = table.dug or
45 {name = "default_sand_footstep", gain = 0.4}
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 -- Papyrus and cactus growing
117 -- wrapping the functions in abm action is necessary to make overriding them possible
119 function default.grow_cactus(pos, node)
120 if node.param2 >= 4 then
124 if minetest.get_item_group(minetest.get_node(pos).name, "sand") == 0 then
129 while node.name == "default:cactus" and height < 4 do
132 node = minetest.get_node(pos)
134 if height == 4 or node.name ~= "air" then
137 minetest.set_node(pos, {name = "default:cactus"})
141 function default.grow_papyrus(pos, node)
143 local name = minetest.get_node(pos).name
144 if name ~= "default:dirt_with_grass" and name ~= "default:dirt" then
147 if not minetest.find_node_near(pos, 3, {"group:water"}) then
152 while node.name == "default:papyrus" and height < 4 do
155 node = minetest.get_node(pos)
157 if height == 4 or node.name ~= "air" then
160 minetest.set_node(pos, {name = "default:papyrus"})
164 minetest.register_abm({
165 nodenames = {"default:cactus"},
166 neighbors = {"group:sand"},
169 action = function(...)
170 default.grow_cactus(...)
174 minetest.register_abm({
175 nodenames = {"default:papyrus"},
176 neighbors = {"default:dirt", "default:dirt_with_grass"},
179 action = function(...)
180 default.grow_papyrus(...)
189 function default.dig_up(pos, node, digger)
190 if digger == nil then return end
191 local np = {x = pos.x, y = pos.y + 1, z = pos.z}
192 local nn = minetest.get_node(np)
193 if nn.name == node.name then
194 minetest.node_dig(np, nn, digger)
200 -- Fence registration helper
202 function default.register_fence(name, def)
203 minetest.register_craft({
204 output = name .. " 4",
206 { def.material, 'group:stick', def.material },
207 { def.material, 'group:stick', def.material },
211 local fence_texture = "default_fence_overlay.png^" .. def.texture ..
212 "^default_fence_overlay.png^[makealpha:255,126,126"
213 -- Allow almost everything to be overridden
214 local default_fields = {
216 drawtype = "nodebox",
219 fixed = {{-1/8, -1/2, -1/8, 1/8, 1/2, 1/8}},
222 connect_front = {{-1/16,3/16,-1/2,1/16,5/16,-1/8},
223 {-1/16,-5/16,-1/2,1/16,-3/16,-1/8}},
224 connect_left = {{-1/2,3/16,-1/16,-1/8,5/16,1/16},
225 {-1/2,-5/16,-1/16,-1/8,-3/16,1/16}},
226 connect_back = {{-1/16,3/16,1/8,1/16,5/16,1/2},
227 {-1/16,-5/16,1/8,1/16,-3/16,1/2}},
228 connect_right = {{1/8,3/16,-1/16,1/2,5/16,1/16},
229 {1/8,-5/16,-1/16,1/2,-3/16,1/16}},
231 connects_to = {"group:fence", "group:wood", "group:tree"},
232 inventory_image = fence_texture,
233 wield_image = fence_texture,
234 tiles = {def.texture},
235 sunlight_propagates = true,
236 is_ground_content = false,
239 for k, v in pairs(default_fields) do
245 -- Always add to the fence group, even if no group provided
251 minetest.register_node(name, def)
259 default.leafdecay_trunk_cache = {}
260 default.leafdecay_enable_cache = true
261 -- Spread the load of finding trunks
262 default.leafdecay_trunk_find_allow_accumulator = 0
264 minetest.register_globalstep(function(dtime)
265 local finds_per_second = 5000
266 default.leafdecay_trunk_find_allow_accumulator =
267 math.floor(dtime * finds_per_second)
270 default.after_place_leaves = function(pos, placer, itemstack, pointed_thing)
271 if placer and not placer:get_player_control().sneak then
272 local node = minetest.get_node(pos)
274 minetest.set_node(pos, node)
278 minetest.register_abm({
279 nodenames = {"group:leafdecay"},
280 neighbors = {"air", "group:liquid"},
281 -- A low interval and a high inverse chance spreads the load
285 action = function(p0, node, _, _)
286 --print("leafdecay ABM at "..p0.x..", "..p0.y..", "..p0.z..")")
287 local do_preserve = false
288 local d = minetest.registered_nodes[node.name].groups.leafdecay
289 if not d or d == 0 then
290 --print("not groups.leafdecay")
293 local n0 = minetest.get_node(p0)
294 if n0.param2 ~= 0 then
295 --print("param2 ~= 0")
299 if default.leafdecay_enable_cache then
300 p0_hash = minetest.hash_node_position(p0)
301 local trunkp = default.leafdecay_trunk_cache[p0_hash]
303 local n = minetest.get_node(trunkp)
304 local reg = minetest.registered_nodes[n.name]
305 -- Assume ignore is a trunk, to make the thing
306 -- work at the border of the active area
307 if n.name == "ignore" or (reg and reg.groups.tree and
308 reg.groups.tree ~= 0) then
309 --print("cached trunk still exists")
312 --print("cached trunk is invalid")
314 table.remove(default.leafdecay_trunk_cache, p0_hash)
317 if default.leafdecay_trunk_find_allow_accumulator <= 0 then
320 default.leafdecay_trunk_find_allow_accumulator =
321 default.leafdecay_trunk_find_allow_accumulator - 1
322 -- Assume ignore is a trunk, to make the thing
323 -- work at the border of the active area
324 local p1 = minetest.find_node_near(p0, d, {"ignore", "group:tree"})
327 if default.leafdecay_enable_cache then
328 --print("caching trunk")
330 default.leafdecay_trunk_cache[p0_hash] = p1
333 if not do_preserve then
334 -- Drop stuff other than the node itself
335 local itemstacks = minetest.get_node_drops(n0.name)
336 for _, itemname in ipairs(itemstacks) do
337 if minetest.get_item_group(n0.name, "leafdecay_drop") ~= 0 or
338 itemname ~= n0.name then
340 x = p0.x - 0.5 + math.random(),
341 y = p0.y - 0.5 + math.random(),
342 z = p0.z - 0.5 + math.random(),
344 minetest.add_item(p_drop, itemname)
348 minetest.remove_node(p0)
356 -- Grass growing on well-lit dirt
359 minetest.register_abm({
360 nodenames = {"default:dirt"},
365 action = function(pos, node)
366 local above = {x = pos.x, y = pos.y + 1, z = pos.z}
367 local name = minetest.get_node(above).name
368 local nodedef = minetest.registered_nodes[name]
369 if nodedef and (nodedef.sunlight_propagates or nodedef.paramtype == "light") and
370 nodedef.liquidtype == "none" and
371 (minetest.get_node_light(above) or 0) >= 13 then
372 if name == "default:snow" or name == "default:snowblock" then
373 minetest.set_node(pos, {name = "default:dirt_with_snow"})
375 minetest.set_node(pos, {name = "default:dirt_with_grass"})
383 -- Grass and dry grass removed in darkness
386 minetest.register_abm({
387 nodenames = {"default:dirt_with_grass", "default:dirt_with_dry_grass"},
391 action = function(pos, node)
392 local above = {x = pos.x, y = pos.y + 1, z = pos.z}
393 local name = minetest.get_node(above).name
394 local nodedef = minetest.registered_nodes[name]
395 if name ~= "ignore" and nodedef and not ((nodedef.sunlight_propagates or
396 nodedef.paramtype == "light") and
397 nodedef.liquidtype == "none") then
398 minetest.set_node(pos, {name = "default:dirt"})
405 -- Moss growth on cobble near water
408 minetest.register_abm({
409 nodenames = {"default:cobble"},
410 neighbors = {"group:water"},
414 action = function(pos, node)
415 minetest.set_node(pos, {name = "default:mossycobble"})