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 = "fencelike",
217 inventory_image = fence_texture,
218 wield_image = fence_texture,
219 tiles = { def.texture },
220 sunlight_propagates = true,
221 is_ground_content = false,
224 fixed = {-1/7, -1/2, -1/7, 1/7, 1/2, 1/7},
228 for k, v in pairs(default_fields) do
234 -- Always add to the fence group, even if no group provided
240 minetest.register_node(name, def)
248 default.leafdecay_trunk_cache = {}
249 default.leafdecay_enable_cache = true
250 -- Spread the load of finding trunks
251 default.leafdecay_trunk_find_allow_accumulator = 0
253 minetest.register_globalstep(function(dtime)
254 local finds_per_second = 5000
255 default.leafdecay_trunk_find_allow_accumulator =
256 math.floor(dtime * finds_per_second)
259 default.after_place_leaves = function(pos, placer, itemstack, pointed_thing)
260 local node = minetest.get_node(pos)
262 minetest.set_node(pos, node)
265 minetest.register_abm({
266 nodenames = {"group:leafdecay"},
267 neighbors = {"air", "group:liquid"},
268 -- A low interval and a high inverse chance spreads the load
272 action = function(p0, node, _, _)
273 --print("leafdecay ABM at "..p0.x..", "..p0.y..", "..p0.z..")")
274 local do_preserve = false
275 local d = minetest.registered_nodes[node.name].groups.leafdecay
276 if not d or d == 0 then
277 --print("not groups.leafdecay")
280 local n0 = minetest.get_node(p0)
281 if n0.param2 ~= 0 then
282 --print("param2 ~= 0")
286 if default.leafdecay_enable_cache then
287 p0_hash = minetest.hash_node_position(p0)
288 local trunkp = default.leafdecay_trunk_cache[p0_hash]
290 local n = minetest.get_node(trunkp)
291 local reg = minetest.registered_nodes[n.name]
292 -- Assume ignore is a trunk, to make the thing
293 -- work at the border of the active area
294 if n.name == "ignore" or (reg and reg.groups.tree and
295 reg.groups.tree ~= 0) then
296 --print("cached trunk still exists")
299 --print("cached trunk is invalid")
301 table.remove(default.leafdecay_trunk_cache, p0_hash)
304 if default.leafdecay_trunk_find_allow_accumulator <= 0 then
307 default.leafdecay_trunk_find_allow_accumulator =
308 default.leafdecay_trunk_find_allow_accumulator - 1
309 -- Assume ignore is a trunk, to make the thing
310 -- work at the border of the active area
311 local p1 = minetest.find_node_near(p0, d, {"ignore", "group:tree"})
314 if default.leafdecay_enable_cache then
315 --print("caching trunk")
317 default.leafdecay_trunk_cache[p0_hash] = p1
320 if not do_preserve then
321 -- Drop stuff other than the node itself
322 local itemstacks = minetest.get_node_drops(n0.name)
323 for _, itemname in ipairs(itemstacks) do
324 if minetest.get_item_group(n0.name, "leafdecay_drop") ~= 0 or
325 itemname ~= n0.name then
327 x = p0.x - 0.5 + math.random(),
328 y = p0.y - 0.5 + math.random(),
329 z = p0.z - 0.5 + math.random(),
331 minetest.add_item(p_drop, itemname)
335 minetest.remove_node(p0)
343 -- Grass growing on well-lit dirt
346 minetest.register_abm({
347 nodenames = {"default:dirt"},
352 action = function(pos, node)
353 local above = {x = pos.x, y = pos.y + 1, z = pos.z}
354 local name = minetest.get_node(above).name
355 local nodedef = minetest.registered_nodes[name]
356 if nodedef and (nodedef.sunlight_propagates or nodedef.paramtype == "light") and
357 nodedef.liquidtype == "none" and
358 (minetest.get_node_light(above) or 0) >= 13 then
359 if name == "default:snow" or name == "default:snowblock" then
360 minetest.set_node(pos, {name = "default:dirt_with_snow"})
362 minetest.set_node(pos, {name = "default:dirt_with_grass"})
370 -- Grass and dry grass removed in darkness
373 minetest.register_abm({
374 nodenames = {"default:dirt_with_grass", "default:dirt_with_dry_grass"},
378 action = function(pos, node)
379 local above = {x = pos.x, y = pos.y + 1, z = pos.z}
380 local name = minetest.get_node(above).name
381 local nodedef = minetest.registered_nodes[name]
382 if name ~= "ignore" and nodedef and not ((nodedef.sunlight_propagates or
383 nodedef.paramtype == "light") and
384 nodedef.liquidtype == "none") then
385 minetest.set_node(pos, {name = "default:dirt"})
392 -- Moss growth on cobble near water
395 minetest.register_abm({
396 nodenames = {"default:cobble"},
397 neighbors = {"group:water"},
401 action = function(pos, node)
402 minetest.set_node(pos, {name = "default:mossycobble"})