1 -- mods/default/functions.lua
7 function default.node_sound_defaults(table)
9 table.footstep = table.footstep or
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_source = function(pos)
92 minetest.set_node(pos, {name="default:obsidian"})
93 minetest.sound_play("default_cool_lava", {pos = pos, gain = 0.25})
96 default.cool_lava_flowing = function(pos)
97 minetest.set_node(pos, {name="default:stone"})
98 minetest.sound_play("default_cool_lava", {pos = pos, gain = 0.25})
101 minetest.register_abm({
102 nodenames = {"default:lava_flowing"},
103 neighbors = {"group:water"},
106 action = function(...)
107 default.cool_lava_flowing(...)
111 minetest.register_abm({
112 nodenames = {"default:lava_source"},
113 neighbors = {"group:water"},
116 action = function(...)
117 default.cool_lava_source(...)
123 -- Papyrus and cactus growing
126 function default.grow_cactus(pos, node)
127 if node.param2 ~= 0 then
131 if minetest.get_item_group(minetest.get_node(pos).name, "sand") == 0 then
136 while node.name == "default:cactus" and height < 4 and node.param2 == 0 do
139 node = minetest.get_node(pos)
142 or node.name ~= "air" then
145 minetest.set_node(pos, {name="default:cactus"})
149 function default.grow_papyrus(pos, node)
151 local name = minetest.get_node(pos).name
152 if name ~= "default:dirt_with_grass"
153 and name ~= "default:dirt" then
156 if not minetest.find_node_near(pos, 3, {"group:water"}) then
161 while node.name == "default:papyrus" and height < 4 do
164 node = minetest.get_node(pos)
167 or node.name ~= "air" then
170 minetest.set_node(pos, {name="default:papyrus"})
174 -- wrapping the functions in abm action is necessary to make overriding them possible
175 minetest.register_abm({
176 nodenames = {"default:cactus"},
177 neighbors = {"group:sand"},
180 action = function(...)
181 default.grow_cactus(...)
185 minetest.register_abm({
186 nodenames = {"default:papyrus"},
187 neighbors = {"default:dirt", "default:dirt_with_grass"},
190 action = function(...)
191 default.grow_papyrus(...)
200 function default.dig_up(pos, node, digger)
201 if digger == nil then return end
202 local np = {x = pos.x, y = pos.y + 1, z = pos.z}
203 local nn = minetest.get_node(np)
204 if nn.name == node.name then
205 minetest.node_dig(np, nn, digger)
214 default.leafdecay_trunk_cache = {}
215 default.leafdecay_enable_cache = true
216 -- Spread the load of finding trunks
217 default.leafdecay_trunk_find_allow_accumulator = 0
219 minetest.register_globalstep(function(dtime)
220 local finds_per_second = 5000
221 default.leafdecay_trunk_find_allow_accumulator =
222 math.floor(dtime * finds_per_second)
225 default.after_place_leaves = function(pos, placer, itemstack, pointed_thing)
226 local node = minetest.get_node(pos)
228 minetest.set_node(pos, node)
231 minetest.register_abm({
232 nodenames = {"group:leafdecay"},
233 neighbors = {"air", "group:liquid"},
234 -- A low interval and a high inverse chance spreads the load
238 action = function(p0, node, _, _)
239 --print("leafdecay ABM at "..p0.x..", "..p0.y..", "..p0.z..")")
240 local do_preserve = false
241 local d = minetest.registered_nodes[node.name].groups.leafdecay
242 if not d or d == 0 then
243 --print("not groups.leafdecay")
246 local n0 = minetest.get_node(p0)
247 if n0.param2 ~= 0 then
248 --print("param2 ~= 0")
252 if default.leafdecay_enable_cache then
253 p0_hash = minetest.hash_node_position(p0)
254 local trunkp = default.leafdecay_trunk_cache[p0_hash]
256 local n = minetest.get_node(trunkp)
257 local reg = minetest.registered_nodes[n.name]
258 -- Assume ignore is a trunk, to make the thing work at the border of the active area
259 if n.name == "ignore" or (reg and reg.groups.tree and reg.groups.tree ~= 0) then
260 --print("cached trunk still exists")
263 --print("cached trunk is invalid")
265 table.remove(default.leafdecay_trunk_cache, p0_hash)
268 if default.leafdecay_trunk_find_allow_accumulator <= 0 then
271 default.leafdecay_trunk_find_allow_accumulator =
272 default.leafdecay_trunk_find_allow_accumulator - 1
273 -- Assume ignore is a trunk, to make the thing work at the border of the active area
274 local p1 = minetest.find_node_near(p0, d, {"ignore", "group:tree"})
277 if default.leafdecay_enable_cache then
278 --print("caching trunk")
280 default.leafdecay_trunk_cache[p0_hash] = p1
283 if not do_preserve then
284 -- Drop stuff other than the node itself
285 local itemstacks = minetest.get_node_drops(n0.name)
286 for _, itemname in ipairs(itemstacks) do
287 if minetest.get_item_group(n0.name, "leafdecay_drop") ~= 0 or
288 itemname ~= n0.name then
290 x = p0.x - 0.5 + math.random(),
291 y = p0.y - 0.5 + math.random(),
292 z = p0.z - 0.5 + math.random(),
294 minetest.add_item(p_drop, itemname)
298 minetest.remove_node(p0)
308 minetest.register_abm({
309 nodenames = {"default:dirt"},
312 action = function(pos, node)
313 local above = {x=pos.x, y=pos.y+1, z=pos.z}
314 local name = minetest.get_node(above).name
315 local nodedef = minetest.registered_nodes[name]
316 if nodedef and (nodedef.sunlight_propagates or nodedef.paramtype == "light")
317 and nodedef.liquidtype == "none"
318 and (minetest.get_node_light(above) or 0) >= 13 then
319 if name == "default:snow" or name == "default:snowblock" then
320 minetest.set_node(pos, {name = "default:dirt_with_snow"})
322 minetest.set_node(pos, {name = "default:dirt_with_grass"})
328 minetest.register_abm({
329 nodenames = {"default:dirt_with_grass"},
332 action = function(pos, node)
333 local above = {x=pos.x, y=pos.y+1, z=pos.z}
334 local name = minetest.get_node(above).name
335 local nodedef = minetest.registered_nodes[name]
336 if name ~= "ignore" and nodedef
337 and not ((nodedef.sunlight_propagates or nodedef.paramtype == "light")
338 and nodedef.liquidtype == "none") then
339 minetest.set_node(pos, {name = "default:dirt"})