renew/waterlily.lua

78 lines
2.9 KiB
Lua

-- Renew mod for Minetest
-- Copyright © 2018 Alex Yst <https://y.st./>
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
-- This software is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- Lesser General Public License for more details.
-- You should have received a copy of the GNU Lesser General Public
-- License along with this program. If not, see
-- <https://www.gnu.org./licenses/>.
minetest.register_abm({
label = "Water Lily Spread",
nodenames = {"flowers:waterlily_waving"},
interval = 11,
chance = 50,
action = function(pos)
-- Water lilies won't propagate in darkness.
local light = minetest.get_node_light(pos)
if not light or light < 13 then
return
end
-- Water lilies need shallow water with soil under it to spread. Using
-- groups instead of hard-coded node names allows for better
-- integration with third-party mods.
local should_be_water = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z})
local should_be_dirt = minetest.get_node({x=pos.x, y=pos.y-2, z=pos.z})
if minetest.get_item_group(should_be_water.name, "water") == 0
or minetest.registered_nodes[should_be_water.name].liquidtype ~= "source"
or minetest.get_item_group(should_be_dirt.name, "soil") == 0 then
return
end
local pos0 = vector.subtract(pos, 4)
local pos1 = vector.add(pos, 4)
local waters = minetest.find_nodes_in_area_under_air(
pos0, pos1, "group:water")
if #waters > 0 then
local seedling = waters[math.random(#waters)]
local seedling_above =
{x = seedling.x, y = seedling.y + 1, z = seedling.z}
local light = minetest.get_node_light(seedling_above)
local should_be_dirt = minetest.get_node({x=seedling.x, y=seedling.y-1, z=seedling.z})
local seedling_node = minetest.get_node(seedling)
local nearby_lilies = #minetest.find_nodes_in_area({
x=seedling_above.x-1,
y=seedling_above.y,
z=seedling_above.z-1,
}, {
x=seedling_above.x+1,
y=seedling_above.y,
z=seedling_above.z+1,
}, "flowers:waterlily_waving")
-- The new location has the same requirements as the old location, but
-- with the added requirement that a new water lily will not grow where
-- it would be touching five other water lilies. Water lilies grow
-- closer together than other flowers, but even they have their density
-- limit.
if not light or light < 13
or minetest.registered_nodes[seedling_node.name].liquidtype ~= "source"
or minetest.get_item_group(should_be_dirt.name, "soil") == 0
or nearby_lilies > 4 then
return
end
minetest.set_node(seedling_above, {name = "flowers:waterlily_waving", param2=math.random(0, 3)})
end
end
})