Default: Create 'grass', 'dry_grass' groups, use in dirt conversion ABM
[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 -- Papyrus and cactus growing
115 --
116
117 -- wrapping the functions in abm action is necessary to make overriding them possible
118
119 function default.grow_cactus(pos, node)
120         if node.param2 >= 4 then
121                 return
122         end
123         pos.y = pos.y - 1
124         if minetest.get_item_group(minetest.get_node(pos).name, "sand") == 0 then
125                 return
126         end
127         pos.y = pos.y + 1
128         local height = 0
129         while node.name == "default:cactus" and height < 4 do
130                 height = height + 1
131                 pos.y = pos.y + 1
132                 node = minetest.get_node(pos)
133         end
134         if height == 4 or node.name ~= "air" then
135                 return
136         end
137         minetest.set_node(pos, {name = "default:cactus"})
138         return true
139 end
140
141 function default.grow_papyrus(pos, node)
142         pos.y = pos.y - 1
143         local name = minetest.get_node(pos).name
144         if name ~= "default:dirt_with_grass" and name ~= "default:dirt" then
145                 return
146         end
147         if not minetest.find_node_near(pos, 3, {"group:water"}) then
148                 return
149         end
150         pos.y = pos.y + 1
151         local height = 0
152         while node.name == "default:papyrus" and height < 4 do
153                 height = height + 1
154                 pos.y = pos.y + 1
155                 node = minetest.get_node(pos)
156         end
157         if height == 4 or node.name ~= "air" then
158                 return
159         end
160         minetest.set_node(pos, {name = "default:papyrus"})
161         return true
162 end
163
164 minetest.register_abm({
165         nodenames = {"default:cactus"},
166         neighbors = {"group:sand"},
167         interval = 12,
168         chance = 83,
169         action = function(...)
170                 default.grow_cactus(...)
171         end
172 })
173
174 minetest.register_abm({
175         nodenames = {"default:papyrus"},
176         neighbors = {"default:dirt", "default:dirt_with_grass"},
177         interval = 14,
178         chance = 71,
179         action = function(...)
180                 default.grow_papyrus(...)
181         end
182 })
183
184
185 --
186 -- dig upwards
187 --
188
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)
195         end
196 end
197
198
199 --
200 -- Fence registration helper
201 --
202 function default.register_fence(name, def)
203         minetest.register_craft({
204                 output = name .. " 4",
205                 recipe = {
206                         { def.material, 'group:stick', def.material },
207                         { def.material, 'group:stick', def.material },
208                 }
209         })
210
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 = {
215                 paramtype = "light",
216                 drawtype = "nodebox",
217                 node_box = {
218                         type = "connected",
219                         fixed = {{-1/8, -1/2, -1/8, 1/8, 1/2, 1/8}},
220                         -- connect_top =
221                         -- connect_bottom =
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}},
230                 },
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,
237                 groups = {},
238         }
239         for k, v in pairs(default_fields) do
240                 if not def[k] then
241                         def[k] = v
242                 end
243         end
244
245         -- Always add to the fence group, even if no group provided
246         def.groups.fence = 1
247
248         def.texture = nil
249         def.material = nil
250
251         minetest.register_node(name, def)
252 end
253
254
255 --
256 -- Leafdecay
257 --
258
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
263
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)
268 end)
269
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)
273                 node.param2 = 1
274                 minetest.set_node(pos, node)
275         end
276 end
277
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
282         interval = 2,
283         chance = 5,
284
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")
291                         return
292                 end
293                 local n0 = minetest.get_node(p0)
294                 if n0.param2 ~= 0 then
295                         --print("param2 ~= 0")
296                         return
297                 end
298                 local p0_hash = nil
299                 if default.leafdecay_enable_cache then
300                         p0_hash = minetest.hash_node_position(p0)
301                         local trunkp = default.leafdecay_trunk_cache[p0_hash]
302                         if trunkp then
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")
310                                         return
311                                 end
312                                 --print("cached trunk is invalid")
313                                 -- Cache is invalid
314                                 table.remove(default.leafdecay_trunk_cache, p0_hash)
315                         end
316                 end
317                 if default.leafdecay_trunk_find_allow_accumulator <= 0 then
318                         return
319                 end
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"})
325                 if p1 then
326                         do_preserve = true
327                         if default.leafdecay_enable_cache then
328                                 --print("caching trunk")
329                                 -- Cache the trunk
330                                 default.leafdecay_trunk_cache[p0_hash] = p1
331                         end
332                 end
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
339                                         local p_drop = {
340                                                 x = p0.x - 0.5 + math.random(),
341                                                 y = p0.y - 0.5 + math.random(),
342                                                 z = p0.z - 0.5 + math.random(),
343                                         }
344                                         minetest.add_item(p_drop, itemname)
345                                 end
346                         end
347                         -- Remove node
348                         minetest.remove_node(p0)
349                         nodeupdate(p0)
350                 end
351         end
352 })
353
354
355 --
356 -- Convert dirt to something that fits the environment
357 --
358
359 minetest.register_abm({
360         nodenames = {"default:dirt"},
361         neighbors = {
362                 "default:dirt_with_grass",
363                 "default:dirt_with_dry_grass",
364                 "default:dirt_with_snow",
365                 "group:grass",
366                 "group:dry_grass",
367                 "default:snow",
368         },
369         interval = 6,
370         chance = 67,
371         catch_up = false,
372         action = function(pos, node)
373                 -- Most likely case, half the time it's too dark for this.
374                 local above = {x = pos.x, y = pos.y + 1, z = pos.z}
375                 if (minetest.get_node_light(above) or 0) < 13 then
376                         return
377                 end
378
379                 -- Look for likely neighbors.
380                 local p2 = minetest.find_node_near(pos, 1, {"default:dirt_with_grass",
381                                 "default:dirt_with_dry_grass", "default:dirt_with_snow"})
382                 if p2 then
383                         -- But the node needs to be under air in this case.
384                         local n2 = minetest.get_node(above)
385                         if n2 and n2.name == "air" then
386                                 local n3 = minetest.get_node(p2)
387                                 minetest.set_node(pos, {name = n3.name})
388                                 return
389                         end
390                 end
391
392                 -- Anything on top?
393                 local n2 = minetest.get_node(above)
394                 if not n2 then
395                         return
396                 end
397
398                 local name = n2.name
399                 -- Snow check is cheapest, so comes first.
400                 if name == "default:snow" then
401                         minetest.set_node(pos, {name = "default:dirt_with_snow"})
402                 -- Most likely case first.
403                 elseif minetest.get_item_group(name, "grass") ~= 0 then
404                         minetest.set_node(pos, {name = "default:dirt_with_grass"})
405                 elseif minetest.get_item_group(name, "dry_grass") ~= 0 then
406                         minetest.set_node(pos, {name = "default:dirt_with_dry_grass"})
407                 end
408         end
409 })
410
411 --
412 -- Grass and dry grass removed in darkness
413 --
414
415 minetest.register_abm({
416         nodenames = {
417                 "default:dirt_with_grass",
418                 "default:dirt_with_dry_grass",
419                 "default:dirt_with_snow",
420         },
421         interval = 8,
422         chance = 50,
423         catch_up = false,
424         action = function(pos, node)
425                 local above = {x = pos.x, y = pos.y + 1, z = pos.z}
426                 local name = minetest.get_node(above).name
427                 local nodedef = minetest.registered_nodes[name]
428                 if name ~= "ignore" and nodedef and not ((nodedef.sunlight_propagates or
429                                 nodedef.paramtype == "light") and
430                                 nodedef.liquidtype == "none") then
431                         minetest.set_node(pos, {name = "default:dirt"})
432                 end
433         end
434 })
435
436
437 --
438 -- Moss growth on cobble near water
439 --
440
441 minetest.register_abm({
442         nodenames = {"default:cobble"},
443         neighbors = {"group:water"},
444         interval = 16,
445         chance = 200,
446         catch_up = false,
447         action = function(pos, node)
448                 minetest.set_node(pos, {name = "default:mossycobble"})
449         end
450 })