diff --git a/mods/API/ApiLoader/src/Api.lua b/mods/API/ApiLoader/src/Api.lua index 11d3d79..21e2ad4 100644 --- a/mods/API/ApiLoader/src/Api.lua +++ b/mods/API/ApiLoader/src/Api.lua @@ -3,4 +3,5 @@ Api = {} local modInfo = Mod.getInfo('smc__api__api_loader') local require = modInfo.require -require('Api.globalStep') \ No newline at end of file +require('Api.ff2lua') +require('Api.globalStep') diff --git a/mods/API/ApiLoader/src/Api/ff2lua.lua b/mods/API/ApiLoader/src/Api/ff2lua.lua new file mode 100644 index 0000000..e3a86ee --- /dev/null +++ b/mods/API/ApiLoader/src/Api/ff2lua.lua @@ -0,0 +1,95 @@ +-- A method for converting an image with colored dots (pixels) into a Lua table grouped by color. +-- Author: @ssdaniel24 (github) + +---Table with color as key and value is table with coordinates of dots. Note: coords begin from 1,1 (not 0,0). +---Example of table: +---```lua +---{ +--- 'size': { h=256, w=256 }, +--- '#fff': { {x=1,y=1}, {x=2,y=2}, ... }, +--- '#ff0': { {x=3,y=3}, {x=4,y=4}, ... }, +--- ... +---} +---``` +---@alias ColoredDotsTable table + +---@param uintb string unsigned int bytes +---@return number +local function uint2num(uintb) + local num = 0 + local len = string.len(uintb) + + for i = 1,len do + local b = string.byte(uintb, i) + num = num + b*(len+1-i) + end + + return num +end + +---Converts given farbfeld image to ColoredDotsTable. +---Farbfeld format: +---``` +---╔════════╤═════════════════════════════════════════════════════════╗ +---║ Bytes │ Description ║ +---╠════════╪═════════════════════════════════════════════════════════╣ +---║ 8 │ 'farbfeld' magic value ║ +---╟────────┼─────────────────────────────────────────────────────────╢ +---║ 4 │ 32-Bit BE unsigned integer (width) ║ +---╟────────┼─────────────────────────────────────────────────────────╢ +---║ 4 │ 32-Bit BE unsigned integer (height) ║ +---╟────────┼─────────────────────────────────────────────────────────╢ +---║ [2222] │ 4x16-Bit BE unsigned integers [RGBA] / pixel, row-major ║ +---╚════════╧═════════════════════════════════════════════════════════╝ +---``` +---[tools/farbfeld | suckless.org software that sucks less](https://tools.suckless.org/farbfeld/) +---@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) + if backgroundColor == nil then + backgroundColor = '#000000FF' + end + + 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 + + --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 = uint2num(file:read(4)), h = uint2num(file:read(4)) } } + + for i = 1,dots.size.h do + for j = 1,dots.size.w do + local color_t = { + r = uint2num(file:read(2)), + g = uint2num(file:read(2)), + b = uint2num(file:read(2)), + a = uint2num(file:read(2)) + } + + ---@type string @ColorString + local color = core.colorspec_to_colorstring(color_t) + + if color ~= backgroundColor then + if not dots[color] then + dots[color] = {} + end + + table.insert(dots[color], { x=j, y=i }) + end + end + end + + file:close() + + return dots +end diff --git a/mods/API/ApiLoader/src/Api/globalStep.lua b/mods/API/ApiLoader/src/Api/globalStep.lua index 1554710..2378054 100644 --- a/mods/API/ApiLoader/src/Api/globalStep.lua +++ b/mods/API/ApiLoader/src/Api/globalStep.lua @@ -72,4 +72,4 @@ core.register_globalstep(function(deltaTime) end end end -end) \ No newline at end of file +end) diff --git a/mods/CORE/MapGen/src/MapGen.lua b/mods/CORE/MapGen/src/MapGen.lua index 281c97e..4ba93ca 100644 --- a/mods/CORE/MapGen/src/MapGen.lua +++ b/mods/CORE/MapGen/src/MapGen.lua @@ -232,6 +232,30 @@ function MapGen:register2DPeaks(layerName, multinoise, peakPoses, groups) end end +---TODO: описание +---@alias PeakColors table?}> + +---TODO: описание +---@param layerName string The name of the layer in which the new peaks will be included. +---@param map string TODO +---@param zeroPos Position2d +---@param peakColors PeakColors TODO +function MapGen:register2DPeaksFromFile(layerName, map, zeroPos,peakColors) + local modPath = core.get_modpath(core.get_current_modname()) + local peaks = Api.ff2luat(modPath .. '/textures/maps/' .. map) + local mapSize = peaks.size + + print(dump(peaks)) + for color, peaksPos in pairs(peaks) do + local peakParams = peakColors[color] + assert(peakParams ~= nil, ('There are no generation settings declared for the color `%s`'):format(color)) + + for _, peakPos in ipairs(peaksPos) do + self:register2DPeak(layerName, vector.new(peakPos.x - zeroPos.x, 0, peakPos.y - zeroPos.y), peakParams.noise, peakParams.groups) + end + end +end + ---Mark a point on the biomes diagram and constrain the world area by height to mark it as `MapGen.Biome`. ---@param layerName string The name of the layer in which the new biome will be included. ---@param biomeName string The name to be assigned to the biome. The name must be unique for each biome. diff --git a/mods/ENVIRONMENT/mapGenerator/src/mapGenerator.lua b/mods/ENVIRONMENT/mapGenerator/src/mapGenerator.lua index 9bfdec6..763b5eb 100644 --- a/mods/ENVIRONMENT/mapGenerator/src/mapGenerator.lua +++ b/mods/ENVIRONMENT/mapGenerator/src/mapGenerator.lua @@ -101,7 +101,7 @@ local climat0 = { } local v = vector.new - +--[[ mapGenerator:register2DPeaks('midgard', { landscapeNoise = land1, @@ -132,6 +132,21 @@ mapGenerator:register2DPeaks('midgard', },{ v(200, 0, 100), v(-300, 0, -200), +})--]] + +mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', {x=250, y=250}, { + ['#396fffff'] = {noise = { + landscapeNoise = land1, + tempNoise = climat2, + humidityNoise = climat2, + } + }, + ['3fff26ff'] = {noise = { + landscapeNoise = land2, + tempNoise = climat2, + humidityNoise = climat2, + } + } }) mapGenerator:registerCavern('midgard', 'cavern1', { diff --git a/mods/ENVIRONMENT/mapGenerator/textures/maps/map.ff b/mods/ENVIRONMENT/mapGenerator/textures/maps/map.ff new file mode 100644 index 0000000..01cc1d9 Binary files /dev/null and b/mods/ENVIRONMENT/mapGenerator/textures/maps/map.ff differ