diff --git a/mods/CORE/MapGen/src/MapGen.lua b/mods/CORE/MapGen/src/MapGen.lua index 87931b9..7bd793b 100644 --- a/mods/CORE/MapGen/src/MapGen.lua +++ b/mods/CORE/MapGen/src/MapGen.lua @@ -33,6 +33,9 @@ local Triangulation = require('MapGen.Triangulation') ---@type MapGen.Cavern local Cavern = require('MapGen.Cavern') +---@type MapGen.Region +local Region = require('MapGen.Region') + local ff2luat = require('MapGen.ff2luat') ---@class MapGen @@ -365,6 +368,7 @@ end function MapGen:registerBiome(layerName, biomeName, def) local layer = self.layersByName[layerName] assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName)) + ---TODO: перенести проверку на уникальность в конструктор assert(layer.biomesByName[biomeName] == nil, ('A biome named `%s` already exists in the `%s` layer.'):format(biomeName, layerName)) local biome = Biome:new(biomeName, def) @@ -379,6 +383,7 @@ end function MapGen:registerCavern(layerName, cavernName, def) local layer = self.layersByName[layerName] assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName)) + ---TODO: перенести проверку на уникальность в конструктор assert(layer.cavernsByName[cavernName] == nil, ('A cavern named `%s` already exists in the `%s` layer.'):format(cavernName, layerName)) local cavern = Cavern:new(cavernName, def) @@ -386,6 +391,19 @@ function MapGen:registerCavern(layerName, cavernName, def) layer:addCavern(cavernName, cavern) end +---Designate the world region between the vertices as `MapGen.Region` and give it a name. +---@param layerName string The name of the layer in which the new region will be included. +---@param regionName string The name to be assigned to the region. The name must be unique for each region. +---@param def MapGen.RegionDef Region description table, see details `MapGen.RegionDef` +function MapGen:registerRegion(layerName, regionName, def) + local layer = self.layersByName[layerName] + assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName)) + assert(layer.regionsByName[regionName] == nil, ('A region named `%s` already exists in the `%s` layer.'):format(regionName, layerName)) + + local region = Region:new(regionName, def) + + layer:addRegion(regionName, region) +end -- --- INITIALIZATION METHODS --- diff --git a/mods/CORE/MapGen/src/MapGen/Layer.lua b/mods/CORE/MapGen/src/MapGen/Layer.lua index 24b7843..2712547 100644 --- a/mods/CORE/MapGen/src/MapGen/Layer.lua +++ b/mods/CORE/MapGen/src/MapGen/Layer.lua @@ -21,6 +21,7 @@ local getInstance = Class.getInstance ---@field biomesVerticalScattering integer ---@field cavernsByName table ---@field cavernsList MapGen.Cavern[] +---@field regionsByName table ---@field trianglesList {['landscape']:MapGen.Triangle[],['temp']:MapGen.Triangle[],['humidity']:MapGen.Triangle[]} ---@field tetrahedronsList MapGen.Tetrahedron[] **Not supported** local Layer = { @@ -44,6 +45,7 @@ local Layer = { biomesDiagram = {}, cavernsByName = {}, cavernsList = {}, + regionsByName = {}, trianglesList = { landscape = {}, temp = {}, @@ -177,6 +179,7 @@ end ---@param biomeName string ---@param biome MapGen.Biome function Layer:addBiome(biomeName, biome) + -- TODO: перенести сюда проверку на уникальность self.biomesByName[biomeName] = biome table.insert(self.biomesList, biome) end @@ -185,10 +188,17 @@ end ---@param cavernName string ---@param cavern MapGen.Cavern function Layer:addCavern(cavernName, cavern) + -- TODO: перенести сюда проверку на уникальность self.cavernsByName[cavernName] = cavern table.insert(self.cavernsList, cavern) end +function Layer:addRegion(regionName, region) + -- TODO: перенести сюда проверку на уникальность + self.regionsByName[regionName] = region + --table.insert(self.regionsList, region) +end + ---@param height number ---@param biomesList MapGen.Biome[] ---@return string[], MapGen.Biome[] diff --git a/mods/CORE/MapGen/src/MapGen/Region.lua b/mods/CORE/MapGen/src/MapGen/Region.lua new file mode 100644 index 0000000..925798b --- /dev/null +++ b/mods/CORE/MapGen/src/MapGen/Region.lua @@ -0,0 +1,112 @@ +local mathClamp, mathHuge, mathSqrt = math.clamp, math.huge, math.sqrt + +local getInstance = Class.getInstance + +---@class MapGen.Region +---@field name string +---@field vertices Position[] +local Region = {} + +---@class MapGen.RegionDef +---@field vertices Position[] + +---@param def MapGen.RegionDef +---@return MapGen.Region +function Region:new(name, def) + local instance = getInstance(self, { + name = name, + vertices = def.vertices + }) + + return instance +end + +---Calculate squared distance from point to line segment. +---@param px integer +---@param pz integer +---@param x1 integer +---@param z1 integer +---@param x2 integer +---@param z2 integer +---@return number +local function calcDistanceSq(px, pz, x1, z1, x2, z2) + local dx = x2 - x1 + local dz = z2 - z1 + local len_sq = dx * dx + dz * dz + + -- Degenerate segment (a single point): + if len_sq == 0 then + return (px - x1)^2 + (pz - z1)^2 + end + + -- Project point onto line, clamped to segment [0, 1] + local param = mathClamp(((px - x1) * dx + (pz - z1) * dz) / len_sq, 0, 1) + + -- Closest point on segment + local closest_x = x1 + param * dx + local closest_z = z1 + param * dz + + return (px - closest_x)^2 + (pz - closest_z)^2 +end + +---Checks if a point is inside a `MapGen.Region` (2D check using x and z coordinates). +---@param point Position +---@return boolean, number? +function Region:pointInRegion(point) + local vertices = self.vertices + local px, pz = point.x, point.z + + local n = #vertices + if n < 3 then + return false + end + + -- Calculate minimum squared distance to any edge + local minDistanceSq = mathHuge + for i = 1, n do + local j = (i % n) + 1 + local v1, v2 = vertices[i], vertices[j] + + if v1 == nil or v2 == nil then error() end + + local dist_sq = calcDistanceSq(px, pz, v1.x, v1.z, v2.x, v2.z) + + if dist_sq < minDistanceSq then + minDistanceSq = dist_sq + end + end + + local minDistance = mathSqrt(minDistanceSq) + + -- Check if point is on edge (within small epsilon) + if minDistance <= 1e-9 then + return true, minDistance + end + + -- Ray casting algorithm: count edge crossings + local inside = false + + for i = 1, n do + local j = (i % n) + 1 + local vi, vj = vertices[i], vertices[j] + + if vi == nil or vj == nil then error() end + + local xi, zi = vi.x, vi.z + local xj, zj = vj.x, vj.z + + -- Check if horizontal ray from point (going +x) crosses this edge + if ((zi > pz) ~= (zj > pz)) and + (px < (xj - xi) * (pz - zi) / (zj - zi) + xi) then + inside = not inside + end + end + + if inside then + return true, minDistance + else + return false + end +end + +return Region \ No newline at end of file diff --git a/mods/ENVIRONMENT/mapGenerator/src/mapGenerator.lua b/mods/ENVIRONMENT/mapGenerator/src/mapGenerator.lua index 2df9756..a046067 100644 --- a/mods/ENVIRONMENT/mapGenerator/src/mapGenerator.lua +++ b/mods/ENVIRONMENT/mapGenerator/src/mapGenerator.lua @@ -70,6 +70,16 @@ local land2 = { lacunarity = 2, } +local land3 = { + offset = 40, + scale = 20, + spread = {x = 45, y = 20, z = 45}, + seed = 472, + octaves = 3, + persistence = 0.3, + lacunarity = 2, +} + local climat1 = { offset = 80, scale = 10, @@ -137,6 +147,7 @@ mapGenerator:register2DPeaks('midgard', for _, c in ipairs({'landscape', 'temp', 'humidity'}) do mapGenerator:registerLayerNoiseColor('midgard', c, '#4978c6', land1) mapGenerator:registerLayerNoiseColor('midgard', c, '#77ea72', land2) + mapGenerator:registerLayerNoiseColor('midgard', c, '#46B331', land3) --[[ mapGenerator:register2DPeaks('midgard', c, { { @@ -158,11 +169,11 @@ for _, c in ipairs({'landscape', 'temp', 'humidity'}) do })--]] end -mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 100,{x=0, y=0}, 'landscape') +mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 32,{x=0, y=0}, 'landscape') mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 150,{x=0, y=0}, 'temp') mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 200,{x=0, y=0}, 'humidity') --- [[ +--[[ mapGenerator:registerCavern('midgard', 'cavern1', { minY = -48, maxY = 50, diff --git a/mods/ENVIRONMENT/mapGenerator/textures/maps/map.ff b/mods/ENVIRONMENT/mapGenerator/textures/maps/map.ff index 89e2f7f..4295421 100644 Binary files a/mods/ENVIRONMENT/mapGenerator/textures/maps/map.ff and b/mods/ENVIRONMENT/mapGenerator/textures/maps/map.ff differ