From 3bd1e66e29a078ad970d03d0767f4f6134966eeb Mon Sep 17 00:00:00 2001 From: trans_soup <> Date: Thu, 12 Oct 2023 10:25:01 +0200 Subject: [PATCH] improve grass growth. grass now grows up and down diagonally in the cardinal directions. --- mods/bg_grass/growth.lua | 41 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/mods/bg_grass/growth.lua b/mods/bg_grass/growth.lua index 440e2b6..4b1cb3e 100644 --- a/mods/bg_grass/growth.lua +++ b/mods/bg_grass/growth.lua @@ -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,