3 Commits

4 changed files with 22 additions and 19 deletions

View File

@@ -3,5 +3,4 @@ Api = {}
local modInfo = Mod.getInfo('smc__api__api_loader')
local require = modInfo.require
require('Api.ff2lua')
require('Api.globalStep')

View File

@@ -41,6 +41,8 @@ local Triangulation = require('MapGen.Triangulation')
---@type MapGen.Cavern
local Cavern = require('MapGen.Cavern')
local ff2luat = require('MapGen.ff2luat')
---@class MapGen
---@field layersByName table<string, MapGen.Layer>
---@field layersList MapGen.Layer[]
@@ -250,7 +252,7 @@ end
---@param groups? table<string, number>
function MapGen:register2DPeaksFromFile(layerName, map, scale,zeroPos, category, groups)
local modPath = core.get_modpath(core.get_current_modname())
local peaks = Api.ff2luat(modPath .. '/textures/maps/' .. map)
local peaks = ff2luat(modPath .. '/textures/maps/' .. map)
local mapSize = peaks.size
peaks.size = nil
@@ -901,10 +903,6 @@ function MapGen:run()
self:initLayersTriangultaion()
MapGen.isRunning = true
for _, peak in ipairs(self.layersList[1].peaksList["landscape"]) do
print(tostring(peak))
end
end
return MapGen

View File

@@ -53,29 +53,37 @@ end
---@param filepath string image in farbfeld format
---@param backgroundColor string? ColorString, will be ignored while processing image. Default: `#000000FF`
---@return ColoredDotsTable
function Api.ff2luat(filepath, backgroundColor)
local function ff2luat(filepath, backgroundColor)
local file = io.open(filepath, 'rb')
if not file then error('Can\'t open farbfeld image: ' .. dump(filepath)) end
if not file:read(8) == 'farbfeld' then error('Not farbfeld image: ' .. dump(filepath)) end
local ff = file:read('*a') -- binary!
file:close()
-- TODO: make it decompressing gzip format -> https://en.wikipedia.org/wiki/Gzip
-- I should skip 10 bytes in a beginning, read how many extra fields after it, skip them too, and skip last 8 bytes
-- (read wiki for information).
-- if filepath:sub(-3) == '.gz' then ff = core.decompress(ff, 'deflate') end
if not ff:sub(1,8) == 'farbfeld' then error('Not farbfeld image: ' .. dump(filepath)) end
if not backgroundColor then backgroundColor = '#000000FF' end
--HACK: normalizing colorstring for future string comparison ('#000' -> '#000000FF')
backgroundColor = core.colorspec_to_colorstring(core.colorspec_to_table(backgroundColor))
---@type ColoredDotsTable
local dots = { size = { w = uintb2num(file:read(4)), h = uintb2num(file:read(4)) } }
local dots = { size = { w = uintb2num(ff:sub(9,12)), h = uintb2num(ff:sub(13,16)) } }
local cursor
for i = 1,dots.size.h do
for j = 1,dots.size.w do
local color_t = {
r = uint16ToUint8(uintb2num(file:read(2))),
g = uint16ToUint8(uintb2num(file:read(2))),
b = uint16ToUint8(uintb2num(file:read(2))),
a = uint16ToUint8(uintb2num(file:read(2)))
cursor = 16 + ((i-1)*dots.size.w + j-1)*8 + 1
local colorT = {
r = uint16ToUint8(uintb2num(ff:sub(cursor, cursor+1))),
g = uint16ToUint8(uintb2num(ff:sub(cursor+2,cursor+3))),
b = uint16ToUint8(uintb2num(ff:sub(cursor+4,cursor+5))),
a = uint16ToUint8(uintb2num(ff:sub(cursor+6,cursor+7)))
}
---@type string @ColorString
local color = core.colorspec_to_colorstring(color_t)
local color = core.colorspec_to_colorstring(colorT)
if color ~= backgroundColor then
if not dots[color] then
@@ -87,7 +95,7 @@ function Api.ff2luat(filepath, backgroundColor)
end
end
file:close()
return dots
end
return ff2luat

View File

@@ -37,8 +37,6 @@ dofile(core.get_modpath('smc__api__table')..'/init.lua')
-- TODO: пока что закоментировано, поскольку вызывает ошибку из-за global_step
-- Api loader
-- dofile(core.get_modpath('smc__api__api_loader')..'/init.lua')
Api = {}
dofile(core.get_modpath('smc__api__api_loader')..'/src/Api/ff2lua.lua')
---@type MapGen
local mapGenerator = dofile(core.get_modpath('smc__core__map_generator')..'/src/mapGenerator.lua')