Skip to content

Commit 7d93272

Browse files
sofarparamat
authored andcommittedApr 16, 2016
Change how dirt turns to dirt_with_(something)
This changes how dirt blocks turn to dirt_with -grass, -dry_grass or -snow. Previously, dirt that was sunlit would turn to dirt_with_grass no matter what, but this happened without any context, so you could get green patches of dirt_with_grass in the middle of a savannah or even desert. Dirt no longer turns to covered dirt unless it's within 1 node from another dirt_with_grass or dirt_with_dry_grass or dirt_with_snow. This makes dirt_with_grass "growback" a lot slower, since it now only happens on the edges, but it retains the context nicely now. If there is any dirt with a grass or dry grass plant, or snow on top, and enough light, we'll convert it sporadically to dirt_with_grass or dirt_with_dry_grass or dirt_with_snow. This allows us to plant grass of our choice in a large dirt patch, or in a region where otherwise that type of grass is not present. This used to be done by 2 abms, but I've combined them in to a single ABM that is ordered to run with maximum efficiency, solving for the most common outcome first before attempting more complex checks.
1 parent 64fe69f commit 7d93272

File tree

1 file changed

+49
-12
lines changed

1 file changed

+49
-12
lines changed
 

‎mods/default/functions.lua

+49-12
Original file line numberDiff line numberDiff line change
@@ -353,32 +353,69 @@ minetest.register_abm({
353353

354354

355355
--
356-
-- Grass growing on well-lit dirt
356+
-- Convert dirt to something that fits the environment
357357
--
358358

359359
minetest.register_abm({
360360
nodenames = {"default:dirt"},
361-
neighbors = {"air"},
361+
neighbors = {
362+
"default:dirt_with_grass",
363+
"default:dirt_with_dry_grass",
364+
"default:dirt_with_snow",
365+
"default:grass_1",
366+
"default:grass_2",
367+
"default:grass_3",
368+
"default:grass_4",
369+
"default:grass_5",
370+
"default:dry_grass_1",
371+
"default:dry_grass_2",
372+
"default:dry_grass_3",
373+
"default:dry_grass_4",
374+
"default:dry_grass_5",
375+
"default:snow",
376+
},
362377
interval = 6,
363378
chance = 67,
364379
catch_up = false,
365380
action = function(pos, node)
381+
-- Most likely case, half the time it's too dark for this.
366382
local above = {x = pos.x, y = pos.y + 1, z = pos.z}
367-
local name = minetest.get_node(above).name
368-
local nodedef = minetest.registered_nodes[name]
369-
if nodedef and (nodedef.sunlight_propagates or nodedef.paramtype == "light") and
370-
nodedef.liquidtype == "none" and
371-
(minetest.get_node_light(above) or 0) >= 13 then
372-
if name == "default:snow" or name == "default:snowblock" then
373-
minetest.set_node(pos, {name = "default:dirt_with_snow"})
374-
else
375-
minetest.set_node(pos, {name = "default:dirt_with_grass"})
383+
if (minetest.get_node_light(above) or 0) < 13 then
384+
return
385+
end
386+
387+
-- Look for likely neighbors.
388+
local p2 = minetest.find_node_near(pos, 1, {"default:dirt_with_grass",
389+
"default:dirt_with_dry_grass", "default:dirt_with_snow"})
390+
if p2 then
391+
-- But the node needs to be under air in this case.
392+
local n2 = minetest.get_node(above)
393+
if n2 and n2.name == "air" then
394+
local n3 = minetest.get_node(p2)
395+
minetest.set_node(pos, {name = n3.name})
396+
return
376397
end
377398
end
399+
400+
-- Anything on top?
401+
local n2 = minetest.get_node(above)
402+
if not n2 then
403+
return
404+
end
405+
406+
local name = n2.name
407+
-- Snow check is cheapest, so comes first.
408+
if name == "default:snow" then
409+
minetest.set_node(pos, {name = "default:dirt_with_snow"})
410+
-- Most likely case first.
411+
elseif name:sub(1, 13) == "default:grass" then
412+
minetest.set_node(pos, {name = "default:dirt_with_grass"})
413+
elseif name:sub(1, 17) == "default:dry_grass" then
414+
minetest.set_node(pos, {name = "default:dirt_with_dry_grass"})
415+
end
378416
end
379417
})
380418

381-
382419
--
383420
-- Grass and dry grass removed in darkness
384421
--

0 commit comments

Comments
 (0)
Please sign in to comment.