Minetest-WorldEditAdditions/worldeditadditions/lib/sculpt/parse_static.lua

52 lines
1.4 KiB
Lua
Raw Normal View History

local wea = worldeditadditions
2022-09-19 02:16:22 +02:00
local wea_c = worldeditadditions_core
local Vector3 = wea_c.Vector3
--- Parses a static brush definition.
2023-07-02 21:05:46 +02:00
-- @name parse_static
-- @internal
-- @param source string The source string that contains the static brush, formatted as TSV.
-- @returns true,table,Vector3|false,string A success boolean, followed either by an error message as a string or the brush (as a table) and it's size (as an X/Y Vector3)
return function(source)
local width = -1
local height
local maxvalue, minvalue, range
-- Parse out the TSV into a table of tables, while also parsing values as numbers
-- Also keeps track of the maximum/minimum values found for rescaling later.
2022-09-19 02:16:22 +02:00
local values = wea_c.table.map(
wea_c.split(source, "\n", false),
function(line)
2022-09-19 02:16:22 +02:00
local row = wea_c.split(line, "%s+", false)
width = math.max(width, #row)
2022-09-19 02:16:22 +02:00
return wea_c.table.map(
row,
function(pixel)
local value = tonumber(pixel)
if not value then value = 0 end
if maxvalue == nil or value > maxvalue then
maxvalue = value
end
if minvalue == nil or value < minvalue then
minvalue = value
end
return value
end
)
end
)
height = #values
range = maxvalue - minvalue
local brush = {}
for y,row in ipairs(values) do
for x,value in ipairs(row) do
local i = (y-1)*width + (x-1)
brush[i] = (value - minvalue) / range
end
end
return true, brush, Vector3.new(width, height, 0)
end