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