improve grass growth.

grass now grows up and down diagonally in the cardinal directions.
This commit is contained in:
trans_soup 2023-10-12 10:25:01 +02:00
parent 7b86727e4b
commit 3bd1e66e29

View file

@ -1,14 +1,47 @@
-- 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 = {"core:grass"},
neighbors = {"core:dirt"},
interval = 15,
chance = 8,
action = function (pos, node)
-- TODO: also check diagonally adjacent nodes?
-- TODO: shuffle sides; currently they grow in a specific order determined by the order of `blockgame.vector.sides`.
local sides = blockgame.vector.sides
local sides = listify_table(blockgame.vector.sides)
local diagonally = {}
for _, side in pairs(sides) do
if attempt_spread(pos + side) then return true end
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,