Add .is_sapling() and new forest command.

It's not hooked up yet and we haven't implemented the chat command, so 
it's obviously untested.
This commit is contained in:
Starbeamrainbowlabs 2020-09-13 15:43:49 +01:00
parent 4c0c6c1b00
commit 44efb9bb38
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 95 additions and 1 deletions

View file

@ -0,0 +1,82 @@
--- Overlap command. Places a specified node on top of each column.
-- @module worldeditadditions.layers
function worldeditadditions.forest(pos1, pos2, sapling_weights)
pos1, pos2 = worldedit.sort_pos(pos1, pos2)
-- This command requires the bonemeal mod to be installed
-- We check here too because other mods might call this function directly and bypass the chat command system
if not minetest.get_modpath("bonemeal") then
return false, "Bonemeal mod not loaded"
end
worldeditadditions.overlay(pos1, pos2, sapling_weights)
-- Fetch the nodes in the specified area
local manip, area = worldedit.manip_helpers.init(pos1, pos2)
local data = manip:get_data()
local id_air = minetest.get_content_id("air")
local group_cache = {}
local stats = { attempts = {}, failures = 0 }
for z = pos2.z, pos1.z, -1 do
for x = pos2.x, pos1.x, -1 do
for y = pos2.y, pos1.y, -1 do
local i = area:index(x, y, z)
if not groups_cache[data[i]] then
group_cache[data[i]] = worldeditadditions.is_sapling(data[i])
end
if group_cache[data[i]] then
local did_grow = false
for i=1,100 do
bonemeal:on_use(
{ x = x, y = y, z = z },
4,
nil
)
local new_id_at_pos = minetest.get_content_id(minetest.get_node({ z = z, y = y, x = x }).name)
if not groups_cache[new_id_at_pos] then
group_cache[new_id_at_pos] = worldeditadditions.is_sapling(new_id_at_pos)
end
if not group_cache[new_id_at_pos] then
did_grow = true
table.insert(stats.attempts, i)
break
end
end
if not did_grow then
-- We can't set it to air here because then when we save back we would overwrite all the newly grown trees
stats.failures = stats.failures + 1
end
end
end
end
end
-- Re-fetch the Voxel Manipulator to accountn for the new trees
manip, area = worldedit.manip_helpers.init(pos1, pos2)
data = manip:get_data()
for z = pos2.z, pos1.z, -1 do
for x = pos2.x, pos1.x, -1 do
for y = pos2.y, pos1.y, -1 do
local i = area:index(x, y, z)
if not groups_cache[data[i]] then
group_cache[data[i]] = worldeditadditions.is_sapling(data[i])
end
if group_cache[data[i]] then
data[i] = air
end
end
end
end
-- Save the modified nodes back to disk & return
worldedit.manip_helpers.finish(manip, data)
return true, stats
end

View file

@ -1,5 +1,5 @@
--- Overlap command. Places a specified node on top of each column.
-- @module worldeditadditions.overlay
-- @module worldeditadditions.layers
function worldeditadditions.layers(pos1, pos2, node_weights)
pos1, pos2 = worldedit.sort_pos(pos1, pos2)

View file

@ -32,6 +32,7 @@ local node_id_air = minetest.get_content_id("air")
local node_id_ignore = minetest.get_content_id("ignore")
--- Determines whether the given node/content id is an airlike node or not.
-- It is recommended that the result of this function be cached.
-- @param id number The content/node id to check.
-- @return bool Whether the given node/content id is an airlike node or not.
function worldeditadditions.is_airlike(id)
@ -62,6 +63,7 @@ function worldeditadditions.is_airlike(id)
end
--- Determines whether the given node/content id is a liquid-ish node or not.
-- It is recommended that the result of this function be cached.
-- @param id number The content/node id to check.
-- @return bool Whether the given node/content id is a liquid-ish node or not.
function worldeditadditions.is_liquidlike(id)
@ -78,3 +80,13 @@ function worldeditadditions.is_liquidlike(id)
-- If it's not none, then it has to be a liquid as the only other values are source and flowing
return true
end
--- Determines whether the given node/content id is a sapling or not.
-- Nodes with the "sapling" group are considered saplings.
-- It is recommended that the result of this function be cached.
-- @param id number The content/node id to check.
-- @return bool Whther the given node/content id is a sapxling or not.
function worldeditadditions.is_sapling(id)
local node_name = minetest.get_name_from_content_id(data[i])
return minetest.get_item_group(node_name, "sapling") ~= 0
end