2022-09-19 01:18:03 +02:00
|
|
|
local wea_c = worldeditadditions_core
|
|
|
|
local Vector3 = wea_c.Vector3
|
|
|
|
|
2024-01-02 17:59:51 +01:00
|
|
|
---
|
|
|
|
-- @module worldeditadditions
|
|
|
|
|
2020-05-12 01:38:42 +02:00
|
|
|
-- ██████ ██████ ██ ██ ███ ██ ████████
|
|
|
|
-- ██ ██ ██ ██ ██ ████ ██ ██
|
|
|
|
-- ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
-- ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
-- ██████ ██████ ██████ ██ ████ ██
|
2023-07-04 23:45:02 +02:00
|
|
|
|
|
|
|
--- Counts the nodes in a given area.
|
|
|
|
-- @param pos1 Vector3 pos1 of the defined region to count nodes in.
|
|
|
|
-- @param pos2 Vector3 pos2 of the defined region to count nodes in.
|
|
|
|
-- @param do_human_counts bool Whether to return human-readable counts (as a string) instead of the raw numbers.
|
|
|
|
-- @returns bool,table<number,number>,number 1. Whether the operation was successful or not.
|
|
|
|
-- 2. A table mapping node ids to the number of that node id seen.
|
|
|
|
-- 3. The total number of nodes counted.
|
2021-01-31 18:43:08 +01:00
|
|
|
function worldeditadditions.count(pos1, pos2, do_human_counts)
|
2022-09-19 01:18:03 +02:00
|
|
|
pos1, pos2 = Vector3.sort(pos1, pos2)
|
2020-05-12 01:38:42 +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()
|
|
|
|
|
|
|
|
-- z y x is the preferred loop order (because CPU cache I'd guess, since then we're iterating linearly through the data array)
|
|
|
|
local counts = {}
|
2020-09-20 18:53:55 +02:00
|
|
|
for i in area:iterp(pos1, pos2) do
|
|
|
|
if not counts[data[i]] then
|
|
|
|
counts[data[i]] = 0
|
2020-05-12 01:38:42 +02:00
|
|
|
end
|
2020-09-20 18:53:55 +02:00
|
|
|
counts[data[i]] = counts[data[i]] + 1
|
2020-05-12 01:38:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local total = worldedit.volume(pos1, pos2)
|
|
|
|
|
|
|
|
local results = {}
|
|
|
|
for node_name, count in pairs(counts) do
|
|
|
|
table.insert(results, {
|
|
|
|
count,
|
2022-09-19 01:18:03 +02:00
|
|
|
tostring(wea_c.round((count / total) * 100, 2)).."%",
|
2020-05-12 01:38:42 +02:00
|
|
|
minetest.get_name_from_content_id(node_name)
|
|
|
|
})
|
|
|
|
end
|
|
|
|
table.sort(results, function(a, b) return a[1] < b[1] end)
|
|
|
|
|
2021-01-31 18:43:08 +01:00
|
|
|
if do_human_counts then
|
|
|
|
for key,item in pairs(results) do
|
2022-09-19 01:18:03 +02:00
|
|
|
item[1] = wea_c.format.human_size(item[1], 2)
|
2021-01-31 18:43:08 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-12 01:38:42 +02:00
|
|
|
-- Save the modified nodes back to disk & return
|
|
|
|
-- No need to save - this function doesn't actually change anything
|
|
|
|
-- worldedit.manip_helpers.finish(manip, data)
|
|
|
|
|
|
|
|
return true, results, total
|
|
|
|
end
|