Skip to content

Commit c0de564

Browse files
committedOct 1, 2016
Default: Generalise, optimise and simplify grass spread function
Credit to tenplus1 for the suggestion to generalise for mod use. Mods can add mod nodes to 'group:spreading_dirt_type' enabling the function to work with mod nodes. Add some nodes to this group. Removing 'dirt_with_grass' etc. from 'neighbors' stops the ABM action running everywhere and constantly, on the dirt nodes immediately below the surface nodes. Now the action only runs in the rare case of a dirt node with neighbouring air, grass decorations or snow. Remove check for air above to allow grass to spread under light- transmitting nodes such as fences, walls, plants. This causes spread under slabs, stairs and glass, when near air, but seems worth it. Remove unnecessary check for nil node.
1 parent 5e4a6e8 commit c0de564

File tree

2 files changed

+15
-30
lines changed

2 files changed

+15
-30
lines changed
 

‎mods/default/functions.lua

+12-27
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,7 @@ minetest.register_abm({
348348
label = "Grass spread",
349349
nodenames = {"default:dirt"},
350350
neighbors = {
351-
"default:dirt_with_grass",
352-
"default:dirt_with_dry_grass",
353-
"default:dirt_with_snow",
351+
"air",
354352
"group:grass",
355353
"group:dry_grass",
356354
"default:snow",
@@ -359,36 +357,27 @@ minetest.register_abm({
359357
chance = 67,
360358
catch_up = false,
361359
action = function(pos, node)
362-
-- Most likely case, half the time it's too dark for this.
360+
-- Check for darkness: night, shadow or under a light-blocking node
361+
-- Returns if ignore above
363362
local above = {x = pos.x, y = pos.y + 1, z = pos.z}
364363
if (minetest.get_node_light(above) or 0) < 13 then
365364
return
366365
end
367366

368-
-- Look for likely neighbors.
369-
local p2 = minetest.find_node_near(pos, 1, {"default:dirt_with_grass",
370-
"default:dirt_with_dry_grass", "default:dirt_with_snow"})
367+
-- Look for spreading dirt-type neighbours
368+
local p2 = minetest.find_node_near(pos, 1, "group:spreading_dirt_type")
371369
if p2 then
372-
-- But the node needs to be under air in this case.
373-
local n2 = minetest.get_node(above)
374-
if n2 and n2.name == "air" then
375-
local n3 = minetest.get_node(p2)
376-
minetest.set_node(pos, {name = n3.name})
377-
return
378-
end
379-
end
380-
381-
-- Anything on top?
382-
local n2 = minetest.get_node(above)
383-
if not n2 then
370+
local n3 = minetest.get_node(p2)
371+
minetest.set_node(pos, {name = n3.name})
384372
return
385373
end
386374

387-
local name = n2.name
388-
-- Snow check is cheapest, so comes first.
375+
-- Else, any seeding nodes on top?
376+
local name = minetest.get_node(above).name
377+
-- Snow check is cheapest, so comes first
389378
if name == "default:snow" then
390379
minetest.set_node(pos, {name = "default:dirt_with_snow"})
391-
-- Most likely case first.
380+
-- Most likely case first
392381
elseif minetest.get_item_group(name, "grass") ~= 0 then
393382
minetest.set_node(pos, {name = "default:dirt_with_grass"})
394383
elseif minetest.get_item_group(name, "dry_grass") ~= 0 then
@@ -404,11 +393,7 @@ minetest.register_abm({
404393

405394
minetest.register_abm({
406395
label = "Grass covered",
407-
nodenames = {
408-
"default:dirt_with_grass",
409-
"default:dirt_with_dry_grass",
410-
"default:dirt_with_snow",
411-
},
396+
nodenames = {"group:spreading_dirt_type"},
412397
interval = 8,
413398
chance = 50,
414399
catch_up = false,

‎mods/default/nodes.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ minetest.register_node("default:dirt_with_grass", {
332332
tiles = {"default_grass.png", "default_dirt.png",
333333
{name = "default_dirt.png^default_grass_side.png",
334334
tileable_vertical = false}},
335-
groups = {crumbly = 3, soil = 1},
335+
groups = {crumbly = 3, soil = 1, spreading_dirt_type = 1},
336336
drop = 'default:dirt',
337337
sounds = default.node_sound_dirt_defaults({
338338
footstep = {name = "default_grass_footstep", gain = 0.25},
@@ -357,7 +357,7 @@ minetest.register_node("default:dirt_with_dry_grass", {
357357
"default_dirt.png",
358358
{name = "default_dirt.png^default_dry_grass_side.png",
359359
tileable_vertical = false}},
360-
groups = {crumbly = 3, soil = 1},
360+
groups = {crumbly = 3, soil = 1, spreading_dirt_type = 1},
361361
drop = 'default:dirt',
362362
sounds = default.node_sound_dirt_defaults({
363363
footstep = {name = "default_grass_footstep", gain = 0.4},
@@ -369,7 +369,7 @@ minetest.register_node("default:dirt_with_snow", {
369369
tiles = {"default_snow.png", "default_dirt.png",
370370
{name = "default_dirt.png^default_snow_side.png",
371371
tileable_vertical = false}},
372-
groups = {crumbly = 3, soil = 1},
372+
groups = {crumbly = 3, soil = 1, spreading_dirt_type = 1},
373373
drop = 'default:dirt',
374374
sounds = default.node_sound_dirt_defaults({
375375
footstep = {name = "default_snow_footstep", gain = 0.15},

0 commit comments

Comments
 (0)
Please sign in to comment.