2020-06-07 21:46:46 +02:00
|
|
|
--- Makes an associative table of node_name => weight into a list of node ids.
|
|
|
|
-- Node names with a heigher weight are repeated more times.
|
|
|
|
function worldeditadditions.make_weighted(tbl)
|
|
|
|
local result = {}
|
|
|
|
for node_name, weight in pairs(tbl) do
|
|
|
|
local next_id = minetest.get_content_id(node_name)
|
2020-09-14 02:19:15 +02:00
|
|
|
-- print("[make_weighted] seen "..node_name.." @ weight "..weight.." → id "..next_id)
|
2020-06-07 21:46:46 +02:00
|
|
|
for i = 1, weight do
|
2020-06-11 01:38:16 +02:00
|
|
|
table.insert(result, next_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return result, #result
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Unwinds a list of { node = string, weight = number } tables into a list of node ids.
|
|
|
|
-- The node ids will be repeated multiple times according to their weights
|
|
|
|
-- (e.g. an entry with a weight of 2 will be repeated twice).
|
|
|
|
-- @param list table[] The list to unwind.
|
|
|
|
-- @return number[],number The unwound list of node ids, follows by the number of node ids in total.
|
|
|
|
function worldeditadditions.unwind_node_list(list)
|
|
|
|
local result = {}
|
|
|
|
for i,item in ipairs(list) do
|
|
|
|
local node_id = minetest.get_content_id(item.node)
|
|
|
|
for i = 1, item.weight do
|
|
|
|
table.insert(result, node_id)
|
2020-06-07 21:46:46 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return result, #result
|
|
|
|
end
|
2020-06-09 21:43:29 +02:00
|
|
|
|
2020-09-13 21:32:55 +02:00
|
|
|
function worldeditadditions.registered_nodes_by_group(group)
|
|
|
|
local result = {}
|
|
|
|
for name, def in pairs(minetest.registered_nodes) do
|
|
|
|
if def.groups[group] then
|
|
|
|
result[#result+1] = name
|
|
|
|
end
|
2020-06-11 01:38:16 +02:00
|
|
|
end
|
2020-09-13 21:32:55 +02:00
|
|
|
return result
|
2020-09-13 16:43:49 +02:00
|
|
|
end
|
2020-09-14 02:19:15 +02:00
|
|
|
|
|
|
|
--- Turns a node_name → weight table into a list of { node_name, weight } tables.
|
|
|
|
function worldeditadditions.weighted_to_list(node_weights)
|
|
|
|
local result = {}
|
|
|
|
for node_name, weight in pairs(node_weights) do
|
|
|
|
table.insert(result, { node_name, weight })
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|