create grass mod.

create a new mod that implements basic grass growth and death.
This commit is contained in:
trans_soup 2023-10-12 08:26:37 +02:00
parent 850d397a97
commit 7b86727e4b
8 changed files with 60 additions and 0 deletions

View file

@ -10,3 +10,4 @@ load_file("utils_node")
load_file("utils_stringify")
load_file("utils_random")
load_file("utils_vector")
load_file("wrapper_abm")

View file

@ -8,3 +8,19 @@ function blockgame.reg_simple_node (name, desc, groups)
groups = groups,
})
end
-- probably temporary; will probably use groups for this later.
local air_flowable = {
"air",
"tree:leaves",
"tree:leaves_alive",
}
function blockgame.air_flows_through (pos, node)
node = node or minetest.get_node(pos)
local node_name = node.name
for _, name in pairs(air_flowable) do
if name == node_name then return true end
end
return false
end

View file

@ -1,6 +1,8 @@
blockgame.vector = blockgame.vector or {}
local api = blockgame.vector
-- TODO: replace direct sharing of api variables such as `sides` with functions that return them, to prevent other mods from mutating the shared data.
api.horizontal_directions = {
west = vector.new(-1, 0, 0),
east = vector.new(1, 0, 0),

View file

@ -0,0 +1,3 @@
function blockgame.register_abm (...)
return minetest.register_abm(...)
end

10
mods/bg_grass/death.lua Normal file
View file

@ -0,0 +1,10 @@
blockgame.register_abm({
nodenames = {"core:grass"},
interval = 15,
chance = 4,
action = function (pos, node)
if blockgame.air_flows_through(pos + blockgame.vector.dirs.up) then return false end
minetest.set_node(pos, {name = "core:dirt"})
return true
end,
})

23
mods/bg_grass/growth.lua Normal file
View file

@ -0,0 +1,23 @@
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
for _, side in pairs(sides) do
if attempt_spread(pos + side) then return true end
end
return false
end,
})
function attempt_spread (pos)
if minetest.get_node(pos).name ~= "core: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 = "core:grass"})
return true
end

2
mods/bg_grass/init.lua Normal file
View file

@ -0,0 +1,2 @@
load_file("growth")
load_file("death")

3
mods/bg_grass/mod.conf Normal file
View file

@ -0,0 +1,3 @@
name = grass
description = adds grass spreading and related mechanics to blockgame.
depends = core, api