d0164cd6ac5c227f219877875582839595ea746d
[oweals/minetest_game.git] / mods / default / functions.lua
1 -- mods/default/functions.lua
2
3 --
4 -- Sounds
5 --
6
7 function default.node_sound_defaults(table)
8         table = table or {}
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}
15         return table
16 end
17
18 function default.node_sound_stone_defaults(table)
19         table = table or {}
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)
25         return table
26 end
27
28 function default.node_sound_dirt_defaults(table)
29         table = table or {}
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)
37         return table
38 end
39
40 function default.node_sound_sand_defaults(table)
41         table = table or {}
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)
49         return table
50 end
51
52 function default.node_sound_wood_defaults(table)
53         table = table or {}
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)
59         return table
60 end
61
62 function default.node_sound_leaves_defaults(table)
63         table = table or {}
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)
73         return table
74 end
75
76 function default.node_sound_glass_defaults(table)
77         table = table or {}
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)
83         return table
84 end
85
86
87 --
88 -- Lavacooling
89 --
90
91 default.cool_lava = function(pos, node)
92         if node.name == "default:lava_source" then
93                 minetest.set_node(pos, {name = "default:obsidian"})
94         else -- Lava flowing
95                 minetest.set_node(pos, {name = "default:stone"})
96         end
97         minetest.sound_play("default_cool_lava",
98                 {pos = pos, max_hear_distance = 16, gain = 0.25})
99 end
100
101 minetest.register_abm({
102         nodenames = {"default:lava_source", "default:lava_flowing"},
103         neighbors = {"group:water"},
104         interval = 1,
105         chance = 1,
106         catch_up = false,
107         action = function(...)
108                 default.cool_lava(...)
109         end,
110 })
111
112
113 --
114 -- optimized helper to put all items in an inventory into a drops list
115 --
116 function default.get_inventory_drops(pos, inventory, drops)
117         local inv = minetest.get_meta(pos):get_inventory()
118         local n = #drops
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()
123                         n = n + 1
124                 end
125         end
126 end
127
128 --
129 -- Papyrus and cactus growing
130 --
131
132 -- wrapping the functions in abm action is necessary to make overriding them possible
133
134 function default.grow_cactus(pos, node)
135         if node.param2 >= 4 then
136                 return
137         end
138         pos.y = pos.y - 1
139         if minetest.get_item_group(minetest.get_node(pos).name, "sand") == 0 then
140                 return
141         end
142         pos.y = pos.y + 1
143         local height = 0
144         while node.name == "default:cactus" and height < 4 do
145                 height = height + 1
146                 pos.y = pos.y + 1
147                 node = minetest.get_node(pos)
148         end
149         if height == 4 or node.name ~= "air" then
150                 return
151         end
152         minetest.set_node(pos, {name = "default:cactus"})
153         return true
154 end
155
156 function default.grow_papyrus(pos, node)
157         pos.y = pos.y - 1
158         local name = minetest.get_node(pos).name
159         if name ~= "default:dirt_with_grass" and name ~= "default:dirt" then
160                 return
161         end
162         if not minetest.find_node_near(pos, 3, {"group:water"}) then
163                 return
164         end
165         pos.y = pos.y + 1
166         local height = 0
167         while node.name == "default:papyrus" and height < 4 do
168                 height = height + 1
169                 pos.y = pos.y + 1
170                 node = minetest.get_node(pos)
171         end
172         if height == 4 or node.name ~= "air" then
173                 return
174         end
175         minetest.set_node(pos, {name = "default:papyrus"})
176         return true
177 end
178
179 minetest.register_abm({
180         nodenames = {"default:cactus"},
181         neighbors = {"group:sand"},
182         interval = 12,
183         chance = 83,
184         action = function(...)
185                 default.grow_cactus(...)
186         end
187 })
188
189 minetest.register_abm({
190         nodenames = {"default:papyrus"},
191         neighbors = {"default:dirt", "default:dirt_with_grass"},
192         interval = 14,
193         chance = 71,
194         action = function(...)
195                 default.grow_papyrus(...)
196         end
197 })
198
199
200 --
201 -- dig upwards
202 --
203
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)
210         end
211 end
212
213
214 --
215 -- Fence registration helper
216 --
217 function default.register_fence(name, def)
218         minetest.register_craft({
219                 output = name .. " 4",
220                 recipe = {
221                         { def.material, 'group:stick', def.material },
222                         { def.material, 'group:stick', def.material },
223                 }
224         })
225
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 = {
230                 paramtype = "light",
231                 drawtype = "nodebox",
232                 node_box = {
233                         type = "connected",
234                         fixed = {{-1/8, -1/2, -1/8, 1/8, 1/2, 1/8}},
235                         -- connect_top =
236                         -- connect_bottom =
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}},
245                 },
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,
252                 groups = {},
253         }
254         for k, v in pairs(default_fields) do
255                 if not def[k] then
256                         def[k] = v
257                 end
258         end
259
260         -- Always add to the fence group, even if no group provided
261         def.groups.fence = 1
262
263         def.texture = nil
264         def.material = nil
265
266         minetest.register_node(name, def)
267 end
268
269
270 --
271 -- Leafdecay
272 --
273
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
278
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)
283 end)
284
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)
288                 node.param2 = 1
289                 minetest.set_node(pos, node)
290         end
291 end
292
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
297         interval = 2,
298         chance = 5,
299
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")
306                         return
307                 end
308                 local n0 = minetest.get_node(p0)
309                 if n0.param2 ~= 0 then
310                         --print("param2 ~= 0")
311                         return
312                 end
313                 local p0_hash = nil
314                 if default.leafdecay_enable_cache then
315                         p0_hash = minetest.hash_node_position(p0)
316                         local trunkp = default.leafdecay_trunk_cache[p0_hash]
317                         if trunkp then
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")
325                                         return
326                                 end
327                                 --print("cached trunk is invalid")
328                                 -- Cache is invalid
329                                 table.remove(default.leafdecay_trunk_cache, p0_hash)
330                         end
331                 end
332                 if default.leafdecay_trunk_find_allow_accumulator <= 0 then
333                         return
334                 end
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"})
340                 if p1 then
341                         do_preserve = true
342                         if default.leafdecay_enable_cache then
343                                 --print("caching trunk")
344                                 -- Cache the trunk
345                                 default.leafdecay_trunk_cache[p0_hash] = p1
346                         end
347                 end
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
354                                         local p_drop = {
355                                                 x = p0.x - 0.5 + math.random(),
356                                                 y = p0.y - 0.5 + math.random(),
357                                                 z = p0.z - 0.5 + math.random(),
358                                         }
359                                         minetest.add_item(p_drop, itemname)
360                                 end
361                         end
362                         -- Remove node
363                         minetest.remove_node(p0)
364                         nodeupdate(p0)
365                 end
366         end
367 })
368
369
370 --
371 -- Convert dirt to something that fits the environment
372 --
373
374 minetest.register_abm({
375         nodenames = {"default:dirt"},
376         neighbors = {
377                 "default:dirt_with_grass",
378                 "default:dirt_with_dry_grass",
379                 "default:dirt_with_snow",
380                 "group:grass",
381                 "group:dry_grass",
382                 "default:snow",
383         },
384         interval = 6,
385         chance = 67,
386         catch_up = false,
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
391                         return
392                 end
393
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"})
397                 if p2 then
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})
403                                 return
404                         end
405                 end
406
407                 -- Anything on top?
408                 local n2 = minetest.get_node(above)
409                 if not n2 then
410                         return
411                 end
412
413                 local name = n2.name
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"})
422                 end
423         end
424 })
425
426 --
427 -- Grass and dry grass removed in darkness
428 --
429
430 minetest.register_abm({
431         nodenames = {
432                 "default:dirt_with_grass",
433                 "default:dirt_with_dry_grass",
434                 "default:dirt_with_snow",
435         },
436         interval = 8,
437         chance = 50,
438         catch_up = false,
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"})
447                 end
448         end
449 })
450
451
452 --
453 -- Moss growth on cobble near water
454 --
455
456 minetest.register_abm({
457         nodenames = {"default:cobble"},
458         neighbors = {"group:water"},
459         interval = 16,
460         chance = 200,
461         catch_up = false,
462         action = function(pos, node)
463                 minetest.set_node(pos, {name = "default:mossycobble"})
464         end
465 })