Minetest-WorldEditAdditions/worldeditadditions/lib/sculpt/parse_static.lua
2023-08-03 02:42:46 +01:00

55 lines
1.5 KiB
Lua

local wea = worldeditadditions
local wea_c = worldeditadditions_core
local Vector3 = wea_c.Vector3
---
-- @module worldeditadditions.sculpt
--- Parses a static brush definition.
-- @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.
local values = wea_c.table.map(
wea_c.split(source, "\n", false),
function(line)
local row = wea_c.split(line, "%s+", false)
width = math.max(width, #row)
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