MapGen: find optimization. #37

This commit is contained in:
Van
2026-03-13 21:57:05 +03:00
parent 145f65d4af
commit a34822ed54
3 changed files with 98 additions and 41 deletions

View File

@@ -1,9 +1,12 @@
local mathAbs, mathRound, mathMin, mathMax, id, posToString local mathAbs, mathRound, mathMin, mathMax, posToString
= math.abs, math.round, math.min, math.max, core.get_content_id, core.pos_to_string = math.abs, math.round, math.min, math.max, core.pos_to_string
local v = vector.new
local id = core.get_content_id
local getInstance = Class.getInstance local getInstance = Class.getInstance
local LOG_PROFILE = false local LOG_PROFILE = true
local function calculateAverage(t) local function calculateAverage(t)
if #t == 0 then return 0 end -- Защита от деления на ноль if #t == 0 then return 0 end -- Защита от деления на ноль
@@ -155,52 +158,63 @@ function MapGen:getPeakByPos(layer, category, pos)
return layer:getPeakByPos(category, pos) return layer:getPeakByPos(category, pos)
end end
---Returns `true` if the point is within the triangle's region. ---Checks if a point is inside a triangle using the vector product method.
---The barycentric coordinate method is used. ---
---@param px integer ---If the point is inside the triangle, it will return the triangle itself;
---@param pz integer ---if it is outside, its neighbor in the direction of the point will be returned.
---
---**Note**: works only with coordinates X-Z.
---@param triangle MapGen.Triangle ---@param triangle MapGen.Triangle
local function pointInTriangle2D(px, pz, triangle) ---@param pointPos vector
local posP = vector.new(px, 0, pz) ---@return boolean, MapGen.Triangle?
local pos1 = triangle.p1.getPeakPos() function MapGen:isPointInTriangle(triangle, pointPos)
local pos2 = triangle.p2.getPeakPos() local e1, e2, e3 = triangle.e1, triangle.e2, triangle.e3
local pos3 = triangle.p3.getPeakPos()
local v0 = pos3 - pos1 local y1 = e1.vec:cross(pointPos - e1.p1:getPeakPos()).y
local v1 = pos2 - pos1 if y1 > 0 then
local v2 = posP - pos1 return false, triangle.neighbours[3]
end
local dot00 = vector.dot(v0, v0) local y2 = e2.vec:cross(pointPos - e2.p1:getPeakPos()).y
local dot01 = vector.dot(v0, v1) if y2 > 0 then
local dot02 = vector.dot(v0, v2) return false, triangle.neighbours[1]
local dot11 = vector.dot(v1, v1) end
local dot12 = vector.dot(v1, v2)
local invDenom = 1 / (dot00 * dot11 - dot01 * dot01) local y3 = e3.vec:cross(pointPos - e3.p1:getPeakPos()).y
local u = (dot11 * dot02 - dot01 * dot12) * invDenom if y3 > 0 then
local v = (dot00 * dot12 - dot01 * dot02) * invDenom return false, triangle.neighbours[2]
end
return (u >= 0) and (v >= 0) and (u + v <= 1) return true, triangle
end end
---Returns the `MapGen.Triangle` at the coordinates. If there is no triangle at these coordinates, it returns `nil`. ---@param layer MapGen.Layer The layer of the layer the triangle is in.
--- ---@param category MapGen.Layer.Category The category in which the triangle is in.
---**Important:** works only in the XZ plane. ---@param x integer
---@param layer string|MapGen.Layer The layer or name of the layer the triangle is in. ---@param z integer
---@param category MapGen.Layer.Category The category in which the triangle is in. ---@param triangle MapGen.Triangle?
---@param pos MapPosition Coordinate of the desired peak. ---@return MapGen.Triangle?
---@return MapGen.Triangle? function MapGen:findTriangle(layer, category, x, z, triangle)
function MapGen:getTriangleByPos2D(layer, category, pos) if triangle == nil then
if type(layer) == 'string' then triangle = layer.trianglesList[category][1]
local layerName = layer
layer = self:getLayerByName(layerName)
end end
for _, triangle in ipairs(layer.trianglesList[category]) do local iterationCount = 1000000
if pointInTriangle2D(pos.x, pos.z, triangle) then local isFound = false
return triangle while not isFound do
if iterationCount < 0 then
error('The number of triangle search iterations'..
'has significantly exceeded the number of triangles'..
'in the list. Most likely, the loop has become infinite')
end end
iterationCount = iterationCount - 1
assert(triangle)
isFound, triangle = self:isPointInTriangle(triangle, v(x, 0, z))
end end
return triangle
end end
---Checks if a point is inside a region. ---Checks if a point is inside a region.
@@ -766,6 +780,47 @@ function MapGen:getNoises2dValue(category, layer, x, y, z)
return pastCalc.noiseValue return pastCalc.noiseValue
end end
if not self:pointInRegion2D(layer, category..'_boundary', {x=x, y=y, z=z}) then
return nil
end
if pastCalc.triangle ~= nil then
---@type number?
local noiseValue
local triangles = {
pastCalc.triangle,
pastCalc.triangle.neighbours[1],
pastCalc.triangle.neighbours[2],
pastCalc.triangle.neighbours[3],
}
---@param t MapGen.Triangle
for _, t in ipairs(triangles) do
if self:isPointInTriangle(t, v(x, 0, z)) then
noiseValue = calcNoise2dValue(category, layer, t, x, y, z)
pastCalc.x = x
pastCalc.z = z
pastCalc.noiseValue = noiseValue
return noiseValue
end
end
end
local triangle = self:findTriangle(layer, category, x, z, pastCalc.triangle)
assert(triangle ~= nil)
local noiseValue = calcNoise2dValue(category, layer, triangle, x, y, z)
pastCalc.triangle = triangle
pastCalc.x = x
pastCalc.z = z
pastCalc.noiseValue = noiseValue
return noiseValue
--[[
if pastCalc.triangle ~= nil then if pastCalc.triangle ~= nil then
---@type number? ---@type number?
local noiseValue local noiseValue
@@ -807,7 +862,7 @@ function MapGen:getNoises2dValue(category, layer, x, y, z)
return noiseValue return noiseValue
end end
end end--]]
-- If the point does not fall within the triangle, it is impossible to calculate the height. -- If the point does not fall within the triangle, it is impossible to calculate the height.
-- For debugging purposes only, may slow down generation: -- For debugging purposes only, may slow down generation:

View File

@@ -3,6 +3,7 @@ local getInstance = Class.getInstance
---@class MapGen.Triangulation.Edge ---@class MapGen.Triangulation.Edge
---@field p1 MapGen.Peak ---@field p1 MapGen.Peak
---@field p2 MapGen.Peak ---@field p2 MapGen.Peak
---@field vec vector
local Edge = {} local Edge = {}
---@param p1 MapGen.Peak ---@param p1 MapGen.Peak
@@ -12,7 +13,8 @@ function Edge:new( p1, p2 )
---@type MapGen.Triangulation.Edge ---@type MapGen.Triangulation.Edge
local instance = getInstance(self, { local instance = getInstance(self, {
p1 = p1, p1 = p1,
p2 = p2 p2 = p2,
vec = p2:getPeakPos() - p1:getPeakPos()
}, },
{ {
__tostring = self.toString, __tostring = self.toString,

View File

@@ -1,7 +1,7 @@
-- World generation occurs in a separate thread (MapGen Environment). -- World generation occurs in a separate thread (MapGen Environment).
-- But for the main thread, the mapgenerator object is also initialized. -- But for the main thread, the mapgenerator object is also initialized.
core.set_mapgen_setting('chunksize', '{x=1,y=5,z=1}', true) core.set_mapgen_setting('chunksize', '{x=2,y=10,z=2}', true)
local mapGeneneratorRequire = Mod.getInfo('smc__core__map_generator').require local mapGeneneratorRequire = Mod.getInfo('smc__core__map_generator').require