8 Commits

Author SHA1 Message Date
8fb3235937 Added TODO: for gzip decompressing 2026-01-11 11:42:08 +03:00
83e22f192b Rewrote ff2luat func for using string:sub (not file:read) 2026-01-11 11:42:08 +03:00
52630eb85a Add test map image parsing 2026-01-10 12:04:25 +03:00
Van
7658d74387 Merge pull request #2 from ssdaniel24/mapgen-img-fix
Fixed uintb2num func for normal conversion...
2026-01-09 21:08:49 +03:00
4c143b2055 Fixed uintb2num func for normal conversion... 2026-01-09 21:02:52 +03:00
Van
54184fc32a Merge pull request #1 from ssdaniel24/mapgen-img-fix
Fixed unsigned int bytes to number conversion
2026-01-09 19:19:16 +03:00
108c0c8817 Fixed unsigned int bytes to number conversion 2026-01-09 19:14:52 +03:00
a97d806f6b Mapgen: add peaks register from image 2026-01-09 18:44:26 +03:00
6 changed files with 168 additions and 6 deletions

View File

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

View File

@@ -0,0 +1,99 @@
-- 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<string,table>
---@param uintb string unsigned int bytes
---@return number
local function uintb2num(uintb)
local num = 0
local len = string.len(uintb)
for i = 1,len do
num = num*256 + string.byte(uintb, i)
end
return num
end
---Converts (scaling) 16-bit unsigned integer to 8-bit unsigned integer.
---Why? Because hex codes of colors are 8-bit: #ffffff -> ff = 256 (8 bit). Not 65536 (16 bit).
---@param num number 16-bit number
---@return number 8-bit number
local function uint16ToUint8(num)
return math.floor(num / 256)
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)
local file = io.open(filepath, 'rb')
if not file then error('Can\'t open 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(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
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(colorT)
if color ~= backgroundColor then
if not dots[color] then
dots[color] = {}
end
table.insert(dots[color], { x=j, y=i })
end
end
end
return dots
end

View File

@@ -232,6 +232,31 @@ function MapGen:register2DPeaks(layerName, multinoise, peakPoses, groups)
end end
end end
---TODO: описание
---@alias PeakColors table<ColorString, {noise:MapGen.Peak.MultinoiseParams, groups:table<string, number>?}>
---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
peaks.size = nil
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`. ---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 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. ---@param biomeName string The name to be assigned to the biome. The name must be unique for each biome.

View File

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

View File

@@ -61,7 +61,7 @@ local land1 = {
} }
local land2 = { local land2 = {
offset = 20, offset = 10,
scale = 5, scale = 5,
spread = {x = 30, y = 30, z = 30}, spread = {x = 30, y = 30, z = 30},
seed = 47, seed = 47,
@@ -101,7 +101,7 @@ local climat0 = {
} }
local v = vector.new local v = vector.new
--[[
mapGenerator:register2DPeaks('midgard', mapGenerator:register2DPeaks('midgard',
{ {
landscapeNoise = land1, landscapeNoise = land1,
@@ -132,8 +132,43 @@ mapGenerator:register2DPeaks('midgard',
},{ },{
v(200, 0, 100), v(200, 0, 100),
v(-300, 0, -200), 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,
}
}
})--]]
local N = 32
for x = -10, 10 do
for z = - 10, 10 do
local land
if math.random(0,1) == 0 then
land = land1
else
land = land2
end
print(x)
mapGenerator:register2DPeak('midgard', vector.new(x*N, 0, z*N), {
landscapeNoise = land,
tempNoise = climat2,
humidityNoise = climat2,
})
end
end
--[[
mapGenerator:registerCavern('midgard', 'cavern1', { mapGenerator:registerCavern('midgard', 'cavern1', {
minY = -48, minY = -48,
maxY = 0, maxY = 0,
@@ -148,7 +183,7 @@ mapGenerator:registerCavern('midgard', 'cavern1', {
persistence = 0.5, persistence = 0.5,
lacunarity = 2.0 lacunarity = 2.0
}, },
}) })--]]
-- --- Biomes --- -- --- Biomes ---