blockgame/mods/bg_grass/grow.lua
trans_soup 7755583fa4 rename mods and add hacky cleanup thing.
rename mods; add `bg_` prefix.

add hacky cleanup ABM (couldn't get LBM:s to work), that converts old
nodes into new ones according to this rename.

the cleanup doesn't do anything for items, though.
2023-10-15 18:22:13 +02:00

56 lines
1.3 KiB
Lua

-- TODO: move these to the general api.
local function listify_table (tab)
local copy = {}
for _, item in pairs(tab) do
table.insert(copy, item)
end
return copy
end
local function shuffle (tab)
tab = listify_table(tab)
local shuffled = {}
repeat
local index = math.random(1, #tab)
local value = tab[index]
table.insert(shuffled, value)
table.remove(tab, index)
until #tab == 0
return shuffled
end
blockgame.register_abm({
nodenames = {"bg_terrain:grass"},
neighbors = {"bg_terrain:dirt"},
interval = 15,
chance = 8,
action = function (pos, node)
local sides = listify_table(blockgame.vector.sides)
local diagonally = {}
for _, side in pairs(sides) do
table.insert(diagonally, side + blockgame.vector.dirs.down)
table.insert(diagonally, side + blockgame.vector.dirs.up)
end
sides = shuffle(sides)
diagonally = shuffle(diagonally)
for _, offset in pairs(sides) do
if attempt_spread(pos + offset) then return true end
end
for _, offset in pairs(diagonally) do
if attempt_spread(pos + offset) then return true end
end
return false
end,
})
function attempt_spread (pos)
if minetest.get_node(pos).name ~= "bg_terrain:dirt" then return false end
if not blockgame.air_flows_through(pos + blockgame.vector.dirs.up) then return false end
minetest.set_node(pos, {name = "bg_terrain:grass"})
return true
end