Dematerialize, annihilate and abolish MapGen and MapGen2

This commit is contained in:
2025-09-25 22:00:35 +07:00
parent e17b2d522f
commit 3ce1e3786c
16 changed files with 0 additions and 683 deletions

View File

@@ -1,7 +0,0 @@
local MapGen = Mod:new()
local require = MapGen.require
MapGen.Class = require('MapGen')
MapGen.Class:enable()
Api.addModToGlobalSpace(MapGen, 'Core.MapGen')

View File

@@ -1,2 +0,0 @@
name = smc__core__map_gen
depends = smc__api__system

View File

@@ -1,274 +0,0 @@
local core, table, math = core, table, math
local ipairs = pairs, ipairs
local modInfo = Mod.getInfo()
local require = modInfo.require
---@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 = {}
---@return MapGen
function MapGen:new()
local instance = setmetatable({
layersByName = {},
layersList = {},
registeredRegions = {},
materials = {
impassableWater = core.get_content_id("impassable_water:water"),
impassableSeabed = core.get_content_id("impassable_water:seabed"),
air = core.get_content_id("air")
},
}, {__index = self})
return instance
end
---@param name string
---@param minY number
---@param maxY number
---@return MapGen.Layer
function MapGen:registerLayer(name, minY, maxY)
---@type MapGen.Layer
local layer = Layer:new(name, minY, maxY)
self.layersByName[name] = layer
table.insert(self.layersList, layer)
return layer
end
---@param params table
function MapGen:register2DRegion(params)
---@type MapGen.Region.Region2D
local region = Region2D:new(params)
---@type MapGen.Layer
local layer = self.layersByName[params.layerName]
if not layer then
error("Invalid layer: " .. params.layerName)
end
layer:add2DRegion(region)
-- TODO: тут не надо возвращать регион?
end
---@param params table
function MapGen:register3DRegion(params)
---@type MapGen.Region.Region3D
local region = Region3D:new(params)
---@type MapGen.Layer
local layer = self.layersByName[params.layerName]
if not layer then
error("Invalid layer: " .. params.layerName)
end
layer:add3DRegion(region)
-- TODO: тут не надо возвращать регион?
end
function MapGen:enable()
core.register_on_generated(function(...)
self:onMapGenerated(...)
end)
end
--- Main generation logic
---@param minp table TODO: заменить тип на класс/вектор координат
---@param maxp table TODO: заменить тип на класс/вектор координат
---@param seed number
function MapGen:onMapGenerated(minp, maxp, seed)
local vm, emin, emax = core.get_mapgen_object("voxelmanip")
local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
local data = vm:get_data()
-- Sort layers by maxY descending for processing priority
table.sort(self.layersList, function(a, b)
return a.maxY > b.maxY
end)
local terrainCache = self:process2DRegions(minp, maxp)
self:process3DRegions(minp, maxp, data, area, terrainCache)
vm:set_data(data)
vm:calc_lighting()
vm:write_to_map()
end
---@param minp table TODO: заменить тип на класс/вектор координат
---@param maxp table TODO: заменить тип на класс/вектор координат
---@return table
function MapGen:process2DRegions(minp, maxp)
local cache = {}
local width = maxp.x - minp.x + 1
local zSize = maxp.z - minp.z + 1
-- Precompute noise for all 2D regions
local regionNoiseCache = {}
for _, layer in ipairs(self.layersList) do
for _, region in ipairs(layer.regions2D) do
local noiseCache = {
height = {},
temp = {},
humid = {},
}
for x = minp.x, maxp.x do
noiseCache.temp[x] = {}
noiseCache.humid[x] = {}
noiseCache.height[x] = {}
for z = minp.z, maxp.z do
noiseCache.temp[x][z] = region.noiseTemp:get2d({x=x, y=z})
noiseCache.humid[x][z] = region.noiseHumid:get2d({x=x, y=z})
noiseCache.height[x][z] = region.noiseHeight:get2d({x=x, y=z})
end
end
regionNoiseCache[region] = noiseCache
end
end
for x = minp.x, maxp.x do
cache[x] = {}
for z = minp.z, maxp.z do
cache[x][z] = {}
for _, layer in ipairs(self.layersList) do
local bestHeight, bestWeight = nil, 0.0
for _, region in ipairs(layer.regions2D) do
local noiseCache = regionNoiseCache[region]
local temp = noiseCache.temp[x][z]
local humid = noiseCache.humid[x][z]
if MapGenUtils.isInRange(temp, region.tempRange) and MapGenUtils.isInRange(humid, region.humidRange) then
local tempWeight = MapGenUtils.calculateWeight(temp, region.tempRange.min, region.tempRange.max)
local heightWeight = MapGenUtils.calculateWeight(humid, region.humidRange.min, region.humidRange.max)
local weight = tempWeight * heightWeight
if weight > bestWeight then
local heightNoise = noiseCache.height[x][z]
bestHeight = math.lerp(
region.heightRange.min,
region.heightRange.max,
heightNoise
)
bestWeight = weight
end
end
end
if bestHeight then
cache[x][z][layer] = math.floor(math.clamp(bestHeight, layer.minY, layer.maxY))
end
end
end
end
return cache
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)
-- Precompute 3D noise for regions
local region3DNoise = {}
for _, layer in ipairs(self.layersList) do
for _, region in ipairs(layer.regions3D) do
region3DNoise[region] = {
temp = {},
humid = {},
cave = {}
}
for y = minp.y, maxp.y do
region3DNoise[region].temp[y] = {}
region3DNoise[region].humid[y] = {}
region3DNoise[region].cave[y] = {}
for x = minp.x, maxp.x do
region3DNoise[region].temp[y][x] = {}
region3DNoise[region].humid[y][x] = {}
region3DNoise[region].cave[y][x] = {}
for z = minp.z, maxp.z do
region3DNoise[region].temp[y][x][z] = region.noiseTemp:get3d({x=x, y=y, z=z})
region3DNoise[region].humid[y][x][z] = region.noiseHumid:get3d({x=x, y=y, z=z})
region3DNoise[region].cave[y][x][z] = region.noiseCave:get3d({x=x, y=y, z=z})
end
end
end
end
end
for x = minp.x, maxp.x do
for z = minp.z, maxp.z do
local columnCache = terrainCache[x][z]
local baseIndex = area:index(x, minp.y, z)
local yStride = area.ystride
for y = minp.y, maxp.y do
local idx = baseIndex + (y - minp.y) * yStride
local processed = false
for _, layer in ipairs(self.layersList) do
if y >= layer.minY and y <= layer.maxY then
local surfaceY = columnCache[layer]
if not surfaceY then
data[idx] = y == layer.minY and
self.materials.impassableSeabed or
self.materials.impassableWater
else
-- Set default based on position relative to surface
local default = (y <= surfaceY) and
self.materials.impassableSeabed or
self.materials.air
data[idx] = default
-- Apply 3D regions (caves/structures)
for _, region in ipairs(layer.regions3D) do
local temp = region3DNoise[region].temp[y][x][z]
local humid = region3DNoise[region].humid[y][x][z]
local cave = region3DNoise[region].cave[y][x][z]
if MapGenUtils.isInRange(temp, region.tempRange) and
MapGenUtils.isInRange(humid, region.humidRange) and cave > region.caveThreshold then
data[idx] = self.materials.air
break -- Only first matching region applies
end
end
end
processed = true
break -- Only process one layer per Y position
end
end
if not processed then
data[idx] = self.materials.air
end
end
end
end
end
return MapGen

View File

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

View File

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

View File

@@ -1,23 +0,0 @@
local core, setmetatable = core, setmetatable
local modInfo = Mod.getInfo()
---@type MapGen.Region
local Region = modInfo.require("Region")
---@class MapGen.Region.Region2D : MapGen.Region
local Region2D = Mod:getClassExtended(Region, {})
---@param params table
---@return MapGen.Region.Region2D
function Region2D:new(params)
local instance = Region.new(self, params)
instance.type = "2d"
return setmetatable(instance, {__index = Region2D})
end
function Region2D:initNoises()
self.noiseHeight = core.get_value_noise(self.noiseParamsHeight)
self.noiseTemp = core.get_value_noise(self.noiseParamsTemp)
self.noiseHumid = core.get_value_noise(self.noiseParamsHumid)
end

View File

@@ -1,23 +0,0 @@
local core, setmetatable = core, setmetatable
local modInfo = Mod.getInfo()
---@type MapGen.Region
local Region = modInfo.require("Region")
---@class MapGen.Region.Region3D : MapGen.Region
local Region3D = Mod:getClassExtended(Region, {})
---@param params table
---@return MapGen.Region.Region3D
function Region3D:new(params)
local instance = Region:new(params)
instance.type = "3d"
return setmetatable(instance, {__index = Region3D})
end
function Region3D:initNoises()
self.noiseCave = core.get_value_noise(self.noiseParamsCave)
self.noiseTemp = core.get_value_noise(self.noiseParamsTemp)
self.noiseHumid = core.get_value_noise(self.noiseParamsHumid)
end

View File

@@ -1,31 +0,0 @@
local math = math
--- Utility class with static methods
---@class MapGen.Utils
local Utils = {}
---@static
---@param value number
---@param min number
---@param max number
---@return number
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
return false
end
return Utils

View File

@@ -1,7 +0,0 @@
local MapGen = Mod:new()
local require = MapGen.require
MapGen.Class = require('MapGen')
MapGen.Class:enable()
Api.addModToGlobalSpace(MapGen, 'Core.MapGen')

View File

@@ -1,2 +0,0 @@
name = smc__core__map_gen
depends = smc__api__system

View File

@@ -1,40 +0,0 @@
local MapGen = {}
MapGen.__index = MapGen
function MapGen:new()
local instance = setmetatable({}, self)
instance.layers = {}
return instance
end
function MapGen:RegisterLayer(layer)
Ensure.argNotNil(layer, 1, "MapGen:RegisterLayer")
Ensure.argType(layer.minHeight, "number", 1, "MapGen:RegisterLayer")
Ensure.argType(layer.maxHeight, "number", 2, "MapGen:RegisterLayer")
assert(layer.minHeight < layer.maxHeight, "minHeight must be less than maxHeight")
table.insert(self.layers, layer)
end
function MapGen:GenerateMap()
for _, layer in ipairs(self.layers) do
self:GenerateLayer(layer)
end
end
function MapGen:GenerateLayer(layer)
-- Implement layer generation logic here
-- This will include processing 2D and 3D regions
end
function MapGen:RegisterRegion2D(region)
Ensure.argNotNil(region, 1, "MapGen:RegisterRegion2D")
-- Additional validation and registration logic for 2D regions
end
function MapGen:RegisterRegion3D(region)
Ensure.argNotNil(region, 1, "MapGen:RegisterRegion3D")
-- Additional validation and registration logic for 3D regions
end
return MapGen

View File

@@ -1,42 +0,0 @@
local Layer = {}
Layer.__index = Layer
function Layer:new(minHeight, maxHeight)
Ensure.argType(minHeight, 'number', 1, 'Layer:new')
Ensure.argType(maxHeight, 'number', 2, 'Layer:new')
Ensure.argNotNil(minHeight, 1, 'Layer:new')
Ensure.argNotNil(maxHeight, 2, 'Layer:new')
assert(minHeight < maxHeight, 'minHeight must be less than maxHeight')
local layer = {
minHeight = minHeight,
maxHeight = maxHeight,
region2DList = {},
region3DList = {}
}
setmetatable(layer, self)
return layer
end
function Layer:RegisterRegion2D(region2D)
Ensure.argNotNil(region2D, 1, 'Layer:RegisterRegion2D')
assert(region2D.minHeight >= self.minHeight and region2D.maxHeight <= self.maxHeight, 'Region2D height must be within layer boundaries')
table.insert(self.region2DList, region2D)
end
function Layer:RegisterRegion3D(region3D)
Ensure.argNotNil(region3D, 1, 'Layer:RegisterRegion3D')
assert(region3D.minHeight >= self.minHeight and region3D.maxHeight <= self.maxHeight, 'Region3D height must be within layer boundaries')
table.insert(self.region3DList, region3D)
end
function Layer:GetRegions2D()
return self.region2DList
end
function Layer:GetRegions3D()
return self.region3DList
end
return Layer

View File

@@ -1,34 +0,0 @@
local Region = {}
Region.__index = Region
function Region:new(name, layer, allowedBiomes)
Ensure.stringArgNotEmpty(name, 1, "Region:new")
Ensure.argNotNil(layer, 2, "Region:new")
Ensure.argNotNil(allowedBiomes, 3, "Region:new")
local instance = setmetatable({}, self)
instance.name = name
instance.layer = layer
instance.allowedBiomes = allowedBiomes
return instance
end
function Region:GetName()
return self.name
end
function Region:GetLayer()
return self.layer
end
function Region:GetAllowedBiomes()
return self.allowedBiomes
end
function Region:IsBiomeAllowed(biome)
Ensure.stringArgNotEmpty(biome, 1, "Region:IsBiomeAllowed")
return self.allowedBiomes[biome] == true
end
return Region

View File

@@ -1,41 +0,0 @@
local modInfo = Mod.getInfo()
local require = modInfo.require
local Ensure = require("MapGen.Utils.Ensure")
local Region2D = {}
Region2D.__index = Region2D
function Region2D:new(name, layer, allowedBiomes, heightNoise, temperatureNoise, humidityNoise, heightVariance, temperatureVariance, humidityVariance)
Ensure.stringArgNotEmpty(name, 1, "Region2D:new")
Ensure.argNotNil(layer, 2, "Region2D:new")
Ensure.argType(allowedBiomes, "table", 3, "Region2D:new")
Ensure.argType(heightNoise, "table", 4, "Region2D:new")
Ensure.argType(temperatureNoise, "table", 5, "Region2D:new")
Ensure.argType(humidityNoise, "table", 6, "Region2D:new")
Ensure.argType(heightVariance, "table", 7, "Region2D:new")
Ensure.argType(temperatureVariance, "table", 8, "Region2D:new")
Ensure.argType(humidityVariance, "table", 9, "Region2D:new")
local instance = setmetatable({}, self)
instance.name = name
instance.layer = layer
instance.allowedBiomes = allowedBiomes
instance.heightNoise = heightNoise
instance.temperatureNoise = temperatureNoise
instance.humidityNoise = humidityNoise
instance.heightVariance = heightVariance
instance.temperatureVariance = temperatureVariance
instance.humidityVariance = humidityVariance
return instance
end
function Region2D:GenerateTerrain(x, z)
-- Implement terrain generation logic based on noise and variances
end
function Region2D:CheckBounds(y)
-- Implement boundary checking logic for the layer
end
return Region2D

View File

@@ -1,46 +0,0 @@
local Ensure = require("MapGen.Utils.Ensure")
local Region3D = {}
Region3D.__index = Region3D
function Region3D:new(name, layer, allowedBiomes, caveNoise, temperatureNoise, humidityNoise, temperatureVariance, humidityVariance)
Ensure.stringArgNotEmpty(name, 1, "Region3D:new")
Ensure.argNotNil(layer, 2, "Region3D:new")
Ensure.argType(allowedBiomes, "table", 3, "Region3D:new")
Ensure.argType(caveNoise, "table", 4, "Region3D:new")
Ensure.argType(temperatureNoise, "table", 5, "Region3D:new")
Ensure.argType(humidityNoise, "table", 6, "Region3D:new")
Ensure.argType(temperatureVariance, "table", 7, "Region3D:new")
Ensure.argType(humidityVariance, "table", 8, "Region3D:new")
local instance = setmetatable({
name = name,
layer = layer,
allowedBiomes = allowedBiomes,
caveNoise = caveNoise,
temperatureNoise = temperatureNoise,
humidityNoise = humidityNoise,
temperatureVariance = temperatureVariance,
humidityVariance = humidityVariance,
}, self)
return instance
end
function Region3D:Register()
-- Logic to register the 3D region within the specified layer
end
function Region3D:ValidateBounds(minHeight, maxHeight)
-- Logic to validate that the region does not exceed the layer boundaries
end
function Region3D:GenerateCaves()
-- Logic to generate caves based on the cave noise
end
function Region3D:GenerateContents()
-- Logic to populate the region with contents based on temperature and humidity
end
return Region3D

View File

@@ -1,31 +0,0 @@
local Utils = {}
function Utils.Clamp(value, min, max)
if value < min then
return min
elseif value > max then
return max
else
return value
end
end
function Utils.Lerp(a, b, t)
return a + (b - a) * t
end
function Utils.GenerateNoise(seed, x, y, scale)
local noise = core.get_perlin(seed, scale, 1, 0)
return noise:get2d({x, y})
end
function Utils.ValidateBiome(biome, allowedBiomes)
for _, allowedBiome in ipairs(allowedBiomes) do
if biome == allowedBiome then
return true
end
end
return false
end
return Utils