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.5}
44 table.dug = table.dug or
45 {name="default_sand_footstep", gain=1.0}
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.85}
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)
90 function default.spawn_falling_node(p, nodename)
91 spawn_falling_node(p, nodename)
94 -- Horrible crap to support old code
95 -- Don't use this and never do what this does, it's completely wrong!
96 -- (More specifically, the client and the C++ code doesn't get the group)
97 function default.register_falling_node(nodename, texture)
98 minetest.log("error", debug.traceback())
99 minetest.log('error', "WARNING: default.register_falling_node is deprecated")
100 if minetest.registered_nodes[nodename] then
101 minetest.registered_nodes[nodename].groups.falling_node = 1
109 -- Global environment step function
110 function on_step(dtime)
113 minetest.register_globalstep(on_step)
115 function on_placenode(p, node)
116 --print("on_placenode")
118 minetest.register_on_placenode(on_placenode)
120 function on_dignode(p, node)
121 --print("on_dignode")
123 minetest.register_on_dignode(on_dignode)
125 function on_punchnode(p, node)
127 minetest.register_on_punchnode(on_punchnode)
133 default.cool_lava_source = function(pos)
134 minetest.set_node(pos, {name="default:obsidian"})
137 default.cool_lava_flowing = function(pos)
138 minetest.set_node(pos, {name="default:stone"})
141 minetest.register_abm({
142 nodenames = {"default:lava_flowing"},
143 neighbors = {"group:water"},
146 action = function(pos, node, active_object_count, active_object_count_wider)
147 default.cool_lava_flowing(pos, node, active_object_count, active_object_count_wider)
151 minetest.register_abm({
152 nodenames = {"default:lava_source"},
153 neighbors = {"group:water"},
156 action = function(pos, node, active_object_count, active_object_count_wider)
157 default.cool_lava_source(pos, node, active_object_count, active_object_count_wider)
162 -- Papyrus and cactus growing
165 minetest.register_abm({
166 nodenames = {"default:cactus"},
167 neighbors = {"group:sand"},
170 action = function(pos, node)
172 local name = minetest.get_node(pos).name
173 if minetest.get_item_group(name, "sand") ~= 0 then
176 while minetest.get_node(pos).name == "default:cactus" and height < 4 do
181 if minetest.get_node(pos).name == "air" then
182 minetest.set_node(pos, {name="default:cactus"})
189 minetest.register_abm({
190 nodenames = {"default:papyrus"},
191 neighbors = {"default:dirt", "default:dirt_with_grass"},
194 action = function(pos, node)
196 local name = minetest.get_node(pos).name
197 if name == "default:dirt" or name == "default:dirt_with_grass" then
198 if minetest.find_node_near(pos, 3, {"group:water"}) == nil then
203 while minetest.get_node(pos).name == "default:papyrus" and height < 4 do
208 if minetest.get_node(pos).name == "air" then
209 minetest.set_node(pos, {name="default:papyrus"})
220 -- To enable leaf decay for a node, add it to the "leafdecay" group.
222 -- The rating of the group determines how far from a node in the group "tree"
223 -- the node can be without decaying.
225 -- If param2 of the node is ~= 0, the node will always be preserved. Thus, if
226 -- the player places a node of that kind, you will want to set param2=1 or so.
228 -- If the node is in the leafdecay_drop group then the it will always be dropped
231 default.leafdecay_trunk_cache = {}
232 default.leafdecay_enable_cache = true
233 -- Spread the load of finding trunks
234 default.leafdecay_trunk_find_allow_accumulator = 0
236 minetest.register_globalstep(function(dtime)
237 local finds_per_second = 5000
238 default.leafdecay_trunk_find_allow_accumulator =
239 math.floor(dtime * finds_per_second)
242 minetest.register_abm({
243 nodenames = {"group:leafdecay"},
244 neighbors = {"air", "group:liquid"},
245 -- A low interval and a high inverse chance spreads the load
249 action = function(p0, node, _, _)
250 --print("leafdecay ABM at "..p0.x..", "..p0.y..", "..p0.z..")")
251 local do_preserve = false
252 local d = minetest.registered_nodes[node.name].groups.leafdecay
253 if not d or d == 0 then
254 --print("not groups.leafdecay")
257 local n0 = minetest.get_node(p0)
258 if n0.param2 ~= 0 then
259 --print("param2 ~= 0")
263 if default.leafdecay_enable_cache then
264 p0_hash = minetest.hash_node_position(p0)
265 local trunkp = default.leafdecay_trunk_cache[p0_hash]
267 local n = minetest.get_node(trunkp)
268 local reg = minetest.registered_nodes[n.name]
269 -- Assume ignore is a trunk, to make the thing work at the border of the active area
270 if n.name == "ignore" or (reg and reg.groups.tree and reg.groups.tree ~= 0) then
271 --print("cached trunk still exists")
274 --print("cached trunk is invalid")
276 table.remove(default.leafdecay_trunk_cache, p0_hash)
279 if default.leafdecay_trunk_find_allow_accumulator <= 0 then
282 default.leafdecay_trunk_find_allow_accumulator =
283 default.leafdecay_trunk_find_allow_accumulator - 1
284 -- Assume ignore is a trunk, to make the thing work at the border of the active area
285 local p1 = minetest.find_node_near(p0, d, {"ignore", "group:tree"})
288 if default.leafdecay_enable_cache then
289 --print("caching trunk")
291 default.leafdecay_trunk_cache[p0_hash] = p1
294 if not do_preserve then
295 -- Drop stuff other than the node itself
296 itemstacks = minetest.get_node_drops(n0.name)
297 for _, itemname in ipairs(itemstacks) do
298 if minetest.get_item_group(n0.name, "leafdecay_drop") ~= 0 or
299 itemname ~= n0.name then
301 x = p0.x - 0.5 + math.random(),
302 y = p0.y - 0.5 + math.random(),
303 z = p0.z - 0.5 + math.random(),
305 minetest.add_item(p_drop, itemname)
309 minetest.remove_node(p0)