Minetest-WorldEditAdditions/worldeditadditions/lib/replacemix.lua

54 lines
2.4 KiB
Lua
Raw Normal View History

local wea_c = worldeditadditions_core
2022-09-19 01:18:03 +02:00
local Vector3 = wea_c.Vector3
2020-05-14 22:37:27 +02:00
--- Like //mix, but replaces a given node instead.
-- @module worldeditadditions.replacemix
2023-07-02 03:42:17 +02:00
-- TODO: Implement //replacesplat, which picks seeder nodes with a percentage chance, and then some growth passes with e.g. cellular automata? We should probably be pushing towards a release though round about now
2020-05-14 22:37:27 +02:00
-- ██████ ███████ ██████ ██ █████ ██████ ███████ ███ ███ ██ ██ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ████ ██ ██ ██
-- ██████ █████ ██████ ██ ███████ ██ █████ ██ ████ ██ ██ ███
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ██ ██ ███████ ██ ███████ ██ ██ ██████ ███████ ██ ██ ██ ██ ██
2023-07-02 03:42:17 +02:00
2020-05-14 22:37:27 +02:00
function worldeditadditions.replacemix(pos1, pos2, target_node, target_node_chance, replacements)
2022-09-19 01:18:03 +02:00
pos1, pos2 = Vector3.sort(pos1, pos2)
2020-05-14 22:37:27 +02:00
-- pos2 will always have the highest co-ordinates now
-- Fetch the nodes in the specified area
local manip, area = worldedit.manip_helpers.init(pos1, pos2)
local data = manip:get_data()
local node_id_target = minetest.get_content_id(target_node)
-- Initialise statistics
local changed = 0
local candidates = 0
local distribution = {}
-- Generate the list of node ids
2022-09-19 01:18:03 +02:00
local node_ids_replace, node_ids_replace_count = wea_c.make_weighted(replacements)
2020-05-14 22:37:27 +02:00
for node_name, weight in pairs(replacements) do
distribution[minetest.get_content_id(node_name)] = 0
2020-05-14 22:37:27 +02:00
end
for i in area:iterp(pos1, pos2) do
if data[i] == node_id_target then
candidates = candidates + 1
if math.random(target_node_chance) == 1 then
local next_id = node_ids_replace[math.random(node_ids_replace_count)]
data[i] = next_id
changed = changed + 1
-- We initialised this above
distribution[next_id] = distribution[next_id] + 1
end
end
end
-- Save the modified nodes back to disk & return
worldedit.manip_helpers.finish(manip, data)
return true, changed, candidates, distribution
end