MapGen: add Region class and MapGen:pointInRegion2D. close #17
This commit is contained in:
@@ -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
|
||||
@@ -189,6 +192,20 @@ function MapGen:getTriangleByPos2D(layer, category, pos)
|
||||
end
|
||||
end
|
||||
|
||||
---Checks if a point is inside a region.
|
||||
---If the point is inside the region, it will also return the distance to the nearest border.
|
||||
---@param layer MapGen.Layer The name of the layer in which the region is located.
|
||||
---@param regionName string The name of the region in which the location of the point is checked.
|
||||
---@param point Position Point position.
|
||||
---@return boolean, number?
|
||||
function MapGen:pointInRegion2D(layer, regionName, point)
|
||||
local region = layer.regionsByName[regionName]
|
||||
|
||||
assert(layer ~= nil, ('There is no region named `%s` registered.'):format(regionName))
|
||||
|
||||
return region:pointInRegion(point)
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- --- REGISTRATION METHODS ---
|
||||
@@ -365,6 +382,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 +397,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 +405,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 ---
|
||||
|
||||
@@ -21,6 +21,7 @@ local getInstance = Class.getInstance
|
||||
---@field biomesVerticalScattering integer
|
||||
---@field cavernsByName table<string, MapGen.Cavern>
|
||||
---@field cavernsList MapGen.Cavern[]
|
||||
---@field regionsByName table<string, MapGen.Region>
|
||||
---@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[]
|
||||
|
||||
112
mods/CORE/MapGen/src/MapGen/Region.lua
Normal file
112
mods/CORE/MapGen/src/MapGen/Region.lua
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user