MapGen: rewrite & add annotations

This commit is contained in:
Van
2025-08-30 14:12:19 +00:00
parent 8a54d3c48f
commit 704db1517d
6 changed files with 186 additions and 77 deletions

View File

@@ -1,19 +1,40 @@
local modInfo = Mod.getInfo()
local require = modInfo.require
local MapGenUtils = require("MapGen.Utils")
local Layer = require("MapGen.Layer")
local Region2D = require("MapGen.Region.Region2D")
local Region3D = require("MapGen.Region.Region3D")
local core, table, math = core, table, math local core, table, math = core, table, math
local ipairs = pairs, ipairs local ipairs = pairs, ipairs
-- Main MapGen class local modInfo = Mod.getInfo()
local MapGen = {} local require = modInfo.require
MapGen.__index = MapGen
---@type MapGen.Utils
local MapGenUtils = require("MapGen.Utils")
---@type MapGen.Layer
local Layer = require("MapGen.Layer")
---@type MapGen.Region.Region2D
local Region2D = require("MapGen.Region.Region2D")
---@type MapGen.Region.Region3D
local Region3D = require("MapGen.Region.Region3D")
---@class MapGen
---@field layersByName table
---@field layersList table
---@field registeredRegions table
---@field material table<string, number>
local MapGen = {
layersByName = nil,
layersList = nil,
registeredRegions = nil,
materials = {
impassableWater = nil,
impassableSeabed = nil,
air = nil,
},
}
---@return MapGen
function MapGen:new() function MapGen:new()
return setmetatable({ local instance = setmetatable({
layersByName = {}, layersByName = {},
layersList = {}, layersList = {},
registeredRegions = {}, registeredRegions = {},
@@ -21,29 +42,58 @@ function MapGen:new()
impassableWater = core.get_content_id("impassable_water:water"), impassableWater = core.get_content_id("impassable_water:water"),
impassableSeabed = core.get_content_id("impassable_water:seabed"), impassableSeabed = core.get_content_id("impassable_water:seabed"),
air = core.get_content_id("air") air = core.get_content_id("air")
} },
}, self) }, {__index = self})
return instance
end end
---@param name string
---@param minY number
---@param maxY number
---@return MapGen.Layer
function MapGen:registerLayer(name, minY, maxY) function MapGen:registerLayer(name, minY, maxY)
---@type MapGen.Layer
local layer = Layer:new(name, minY, maxY) local layer = Layer:new(name, minY, maxY)
self.layersByName[name] = layer self.layersByName[name] = layer
table.insert(self.layersList, layer) table.insert(self.layersList, layer)
return layer return layer
end end
---@param params table
function MapGen:register2DRegion(params) function MapGen:register2DRegion(params)
---@type MapGen.Region.Region2D
local region = Region2D:new(params) local region = Region2D:new(params)
---@type MapGen.Layer
local layer = self.layersByName[params.layerName] local layer = self.layersByName[params.layerName]
if not layer then error("Invalid layer: " .. params.layerName) end
if not layer then
error("Invalid layer: " .. params.layerName)
end
layer:add2DRegion(region) layer:add2DRegion(region)
-- TODO: тут не надо возвращать регион?
end end
---@param params table
function MapGen:register3DRegion(params) function MapGen:register3DRegion(params)
---@type MapGen.Region.Region3D
local region = Region3D:new(params) local region = Region3D:new(params)
---@type MapGen.Layer
local layer = self.layersByName[params.layerName] local layer = self.layersByName[params.layerName]
if not layer then error("Invalid layer: " .. params.layerName) end
if not layer then
error("Invalid layer: " .. params.layerName)
end
layer:add3DRegion(region) layer:add3DRegion(region)
-- TODO: тут не надо возвращать регион?
end end
function MapGen:enable() function MapGen:enable()
@@ -52,7 +102,10 @@ function MapGen:enable()
end) end)
end end
-- Main generation logic --- Main generation logic
---@param minp table TODO: заменить тип на класс/вектор координат
---@param maxp table TODO: заменить тип на класс/вектор координат
---@param seed number
function MapGen:onMapGenerated(minp, maxp, seed) function MapGen:onMapGenerated(minp, maxp, seed)
local vm, emin, emax = core.get_mapgen_object("voxelmanip") local vm, emin, emax = core.get_mapgen_object("voxelmanip")
local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax} local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
@@ -71,11 +124,14 @@ function MapGen:onMapGenerated(minp, maxp, seed)
vm:write_to_map() vm:write_to_map()
end end
---@param minp table TODO: заменить тип на класс/вектор координат
---@param maxp table TODO: заменить тип на класс/вектор координат
---@return table
function MapGen:process2DRegions(minp, maxp) function MapGen:process2DRegions(minp, maxp)
local cache = {} local cache = {}
local width = maxp.x - minp.x + 1 local width = maxp.x - minp.x + 1
local zSize = maxp.z - minp.z + 1 local zSize = maxp.z - minp.z + 1
-- Precompute noise for all 2D regions -- Precompute noise for all 2D regions
local regionNoiseCache = {} local regionNoiseCache = {}
for _, layer in ipairs(self.layersList) do for _, layer in ipairs(self.layersList) do
@@ -103,7 +159,7 @@ function MapGen:process2DRegions(minp, maxp)
cache[x] = {} cache[x] = {}
for z = minp.z, maxp.z do for z = minp.z, maxp.z do
cache[x][z] = {} cache[x][z] = {}
for _, layer in ipairs(self.layersList) do for _, layer in ipairs(self.layersList) do
local bestHeight, bestWeight = nil, 0.0 local bestHeight, bestWeight = nil, 0.0
@@ -139,6 +195,11 @@ function MapGen:process2DRegions(minp, maxp)
return cache return cache
end end
---@param minp table TODO: заменить тип на класс/вектор координат
---@param maxp table TODO: заменить тип на класс/вектор координат
---@param data void TODO: какой тип?
---@param area void
---@param terrainCache void
function MapGen:process3DRegions(minp, maxp, data, area, terrainCache) function MapGen:process3DRegions(minp, maxp, data, area, terrainCache)
-- Precompute 3D noise for regions -- Precompute 3D noise for regions
local region3DNoise = {} local region3DNoise = {}
@@ -172,32 +233,32 @@ function MapGen:process3DRegions(minp, maxp, data, area, terrainCache)
local columnCache = terrainCache[x][z] local columnCache = terrainCache[x][z]
local baseIndex = area:index(x, minp.y, z) local baseIndex = area:index(x, minp.y, z)
local yStride = area.ystride local yStride = area.ystride
for y = minp.y, maxp.y do for y = minp.y, maxp.y do
local idx = baseIndex + (y - minp.y) * yStride local idx = baseIndex + (y - minp.y) * yStride
local processed = false local processed = false
for _, layer in ipairs(self.layersList) do for _, layer in ipairs(self.layersList) do
if y >= layer.minY and y <= layer.maxY then if y >= layer.minY and y <= layer.maxY then
local surfaceY = columnCache[layer] local surfaceY = columnCache[layer]
if not surfaceY then if not surfaceY then
data[idx] = y == layer.minY and data[idx] = y == layer.minY and
self.materials.impassableSeabed or self.materials.impassableSeabed or
self.materials.impassableWater self.materials.impassableWater
else else
-- Set default based on position relative to surface -- Set default based on position relative to surface
local default = (y <= surfaceY) and local default = (y <= surfaceY) and
self.materials.impassableSeabed or self.materials.impassableSeabed or
self.materials.air self.materials.air
data[idx] = default data[idx] = default
-- Apply 3D regions (caves/structures) -- Apply 3D regions (caves/structures)
for _, region in ipairs(layer.regions3D) do for _, region in ipairs(layer.regions3D) do
local temp = region3DNoise[region].temp[y][x][z] local temp = region3DNoise[region].temp[y][x][z]
local humid = region3DNoise[region].humid[y][x][z] local humid = region3DNoise[region].humid[y][x][z]
local cave = region3DNoise[region].cave[y][x][z] local cave = region3DNoise[region].cave[y][x][z]
if MapGenUtils.isInRange(temp, region.tempRange) and if MapGenUtils.isInRange(temp, region.tempRange) and
MapGenUtils.isInRange(humid, region.humidRange) and cave > region.caveThreshold then MapGenUtils.isInRange(humid, region.humidRange) and cave > region.caveThreshold then
data[idx] = self.materials.air data[idx] = self.materials.air
@@ -205,12 +266,12 @@ function MapGen:process3DRegions(minp, maxp, data, area, terrainCache)
end end
end end
end end
processed = true processed = true
break -- Only process one layer per Y position break -- Only process one layer per Y position
end end
end end
if not processed then if not processed then
data[idx] = self.materials.air data[idx] = self.materials.air
end end

View File

@@ -1,42 +1,64 @@
local table, setmetatable = table, setmetatable local table, setmetatable = table, setmetatable
local Layer = {} ---@class MapGen.Layer
Layer.__index = Layer ---@field name string
---@field minY number
---@field maxY number
---@field regions2D MapGen.Region.Region2D[]
---@field regions3D MapGen.Region.Region3D[]
local Layer = {
name = nil,
minY = nil,
maxY = nil,
regions2D = nil,
regions3D = nil,
}
---@return MapGen.Layer
---@param name string
---@param minY number
---@param maxY number
---@return MapGen.Layer
function Layer:new(name, minY, maxY) function Layer:new(name, minY, maxY)
return setmetatable({ ---@type MapGen.Layer
local instance = setmetatable({
name = name, name = name,
minY = minY, minY = minY,
maxY = maxY, maxY = maxY,
regions2D = {}, regions2D = {},
regions3D = {} regions3D = {}
}, self) }, {__index = self})
return instance
end end
---@param region MapGen.Region.Region2D
function Layer:add2DRegion(region) function Layer:add2DRegion(region)
table.insert(self.regions2D, region) table.insert(self.regions2D, region)
end end
---@param region MapGen.Region.Region3D
function Layer:add3DRegion(region) function Layer:add3DRegion(region)
table.insert(self.regions3D, region) table.insert(self.regions3D, region)
end end
---@return boolean
function Layer:contains(x, y, z) function Layer:contains(x, y, z)
if y < self.minY or y > self.maxY then if y < self.minY or y > self.maxY then
return false return false
end end
for _, region in ipairs(self.regions2D) do for _, region in ipairs(self.regions2D) do
if region:contains2D(x, z) then if region:contains2D(x, z) then -- TODO: ТАКОГО МЕТОДА НИГДЕ НЕТ
return true return true
end end
end end
for _, region in ipairs(self.regions3D) do for _, region in ipairs(self.regions3D) do
if region:contains3D(x, y, z) then if region:contains3D(x, y, z) then -- TODO: ТАКОГО МЕТОДА НИГДЕ НЕТ
return true return true
end end
end end
return false return false
end end

View File

@@ -1,14 +1,21 @@
local Region = {}
Region.__index = Region
local pairs, setmetatable = pairs, setmetatable local pairs, setmetatable = pairs, setmetatable
---@class MapGen.Region
local Region = {}
---@param params table
---@return MapGen.Region
function Region:new(params) function Region:new(params)
local instance = setmetatable({}, self) ---@type MapGen.Region
local instance = setmetatable({}, {__index = self})
for k, v in pairs(params) do for k, v in pairs(params) do
instance[k] = v instance[k] = v
end end
instance:initNoises() instance:initNoises()
return instance return instance
end end

View File

@@ -1,11 +1,15 @@
local modInfo = Mod.getInfo()
local Region = modInfo.require()("Region")
local core, setmetatable = core, setmetatable local core, setmetatable = core, setmetatable
-- 2D Region implementation local modInfo = Mod.getInfo()
local Region2D = setmetatable({}, {__index = Region})
---@type MapGen.Region
local Region = modInfo.require("Region")
---@class MapGen.Region.Region2D : MapGen.Region
local Region2D = Api.getClassExtended(Region, {})
---@param params table
---@return MapGen.Region.Region2D
function Region2D:new(params) function Region2D:new(params)
local instance = Region.new(self, params) local instance = Region.new(self, params)
instance.type = "2d" instance.type = "2d"

View File

@@ -1,13 +1,17 @@
local modInfo = Mod.getInfo()
local Region = modInfo.require()("Region")
local core, setmetatable = core, setmetatable local core, setmetatable = core, setmetatable
-- 3D Region implementation local modInfo = Mod.getInfo()
local Region3D = setmetatable({}, {__index = Region})
---@type MapGen.Region
local Region = modInfo.require("Region")
---@class MapGen.Region.Region3D : MapGen.Region
local Region3D = Api.getClassExtended(Region, {})
---@param params table
---@return MapGen.Region.Region3D
function Region3D:new(params) function Region3D:new(params)
local instance = Region.new(self, params) local instance = Region:new(params)
instance.type = "3d" instance.type = "3d"
return setmetatable(instance, {__index = Region3D}) return setmetatable(instance, {__index = Region3D})
end end

View File

@@ -1,20 +1,31 @@
local math = math local math = math
-- Utility class with static methods --- Utility class with static methods
local MapGenUtils = { ---@class MapGen.Utils
calculateWeight = function(value, min, max) local Utils = {}
local center = (min + max) / 2
local span = (max - min) / 2
return 1 - math.abs((value - center) / span)
end,
---@return boolean ---@static
isInRange = function(value, range) ---@param value number
if value >= range.min and value <= range.max then ---@param min number
return true ---@param max number
end ---@return number
return false function Utils.calculateWeight(value, min, max)
local center = (min + max) / 2
local span = (max - min) / 2
return 1 - math.abs((value - center) / span)
end
---@static
---@param value number
---@param range number
---@return boolean
function Utils.isInRange(value, range)
if value >= range.min and value <= range.max then
return true
end end
}
return MapGenUtils return false
end
return Utils