2 Commits

7 changed files with 108 additions and 52 deletions

View File

@@ -1,28 +1,18 @@
local mathAbs, mathRound, mathMin, mathMax, id local mathAbs, mathRound, mathMin, mathMax, id
= math.abs, math.round, math.min, math.max, core.get_content_id = math.abs, math.round, math.min, math.max, core.get_content_id
---@class MapGen.PastCalcNoiseValue local modInfo = Mod.getInfo('smc__core__map_gen')
local pastCalcNoiseValue = { local require = modInfo.require
---@type MapGen.Triangle?
triangle = nil, ---@alias MapGen.PastCalcNoiseValue {triangle:MapGen.Triangle?, x:integer?, z:integer?, noiseValue:number?}
---@type integer?
x = nil,
---@type integer?
z = nil,
---@type number?
noiseValue = nil,
}
---@type table<MapGen.Layer.Category, MapGen.PastCalcNoiseValue> ---@type table<MapGen.Layer.Category, MapGen.PastCalcNoiseValue>
local pastNoise2DCalc = { local pastNoise2DCalc = {
landscape = pastCalcNoiseValue, landscape = {},
temp = pastCalcNoiseValue, temp = {},
humidity = pastCalcNoiseValue humidity = {}
} }
local modInfo = Mod.getInfo('smc__core__map_gen')
local require = modInfo.require
---@type MapGen.Layer ---@type MapGen.Layer
local Layer = require('MapGen.Layer') local Layer = require('MapGen.Layer')
@@ -262,7 +252,7 @@ function MapGen:register2DPeaksFromFile(layerName, map, scale,zeroPos, category,
for _, peakPos in ipairs(peaksPos) do for _, peakPos in ipairs(peaksPos) do
---@type MapGen.PeakDef ---@type MapGen.PeakDef
local def = { local def = {
pos = vector.new((peakPos.x - zeroPos.x) * scale, 0, (peakPos.y - zeroPos.y) * scale), pos = vector.new((peakPos.x - zeroPos.x - 1) * scale, 0, (peakPos.y - zeroPos.y - 1) * scale),
color = color, color = color,
groups = groups, groups = groups,
} }
@@ -574,19 +564,34 @@ function MapGen:getNoises2dValue(category, layer, x, y, z)
return pastCalc.noiseValue return pastCalc.noiseValue
end end
if pastCalc.triangle ~= nil and pointInTriangle(x, z, pastCalc.triangle) then if pastCalc.triangle ~= nil then
local noiseValue = calcNoise2dValue(category, layer, pastCalc.triangle, x, y, z) ---@type number?
local noiseValue
pastCalc.x = x local triangles = {
pastCalc.z = z pastCalc.triangle,
pastCalc.noiseValue = noiseValue pastCalc.triangle.neighbours.t1,
pastCalc.triangle.neighbours.t2,
pastCalc.triangle.neighbours.t3,
}
return noiseValue ---@param t MapGen.Triangle
for _, t in ipairs(triangles) do
if pointInTriangle(x, z, t) then
noiseValue = calcNoise2dValue(category, layer, t, x, y, z)
pastCalc.x = x
pastCalc.z = z
pastCalc.noiseValue = noiseValue
return noiseValue
end
end
end end
---@param triangle MapGen.Triangle ---@param triangle MapGen.Triangle
for _, triangle in ipairs(layer.trianglesList[category]) do for _, triangle in ipairs(layer.trianglesList[category]) do
if pointInTriangle(x, z,triangle) then if pointInTriangle(x, z, triangle) then
local noiseValue = calcNoise2dValue(category, layer, triangle, x, y, z) local noiseValue = calcNoise2dValue(category, layer, triangle, x, y, z)
pastCalc.triangle = triangle pastCalc.triangle = triangle
@@ -717,7 +722,6 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
return return
end end
-- Calculating landscape height
-- If the value of the noise is `nil`, -- If the value of the noise is `nil`,
-- then the point does not fall within any of the triangles. -- then the point does not fall within any of the triangles.
local noiseHeightValue = mapGenerator:getNoises2dValue('landscape', layer, x, y, z) or 0.0 local noiseHeightValue = mapGenerator:getNoises2dValue('landscape', layer, x, y, z) or 0.0

View File

@@ -54,6 +54,12 @@ end
-- --- Class registration --- -- --- Class registration ---
---Adjacent triangles:
--- * `t1` with a common side p1-p2
--- * `t2` with a common side p2-p3
--- * `t3` with a common side p3-p1
---@alias MapGen.Triangle.Neighbours {t1:MapGen.Triangle?,t2:MapGen.Triangle?,t3:MapGen.Triangle?}
---@class MapGen.Triangle ---@class MapGen.Triangle
---@field p1 MapGen.Peak ---@field p1 MapGen.Peak
---@field p2 MapGen.Peak ---@field p2 MapGen.Peak
@@ -64,13 +70,16 @@ end
---@field h1 MapGen.Triangulation.Edge ---@field h1 MapGen.Triangulation.Edge
---@field h2 MapGen.Triangulation.Edge ---@field h2 MapGen.Triangulation.Edge
---@field h3 MapGen.Triangulation.Edge ---@field h3 MapGen.Triangulation.Edge
local Triangle = {} ---@field neighbours MapGen.Triangle.Neighbours
local Triangle = {
neighbours = {}
}
---@param p1 MapGen.Peak ---@param p1 MapGen.Peak
---@param p2 MapGen.Peak ---@param p2 MapGen.Peak
---@param p3 MapGen.Peak ---@param p3 MapGen.Peak
---@return MapGen.Triangle ---@return MapGen.Triangle
function Triangle:new( p1, p2, p3 ) function Triangle:new(p1, p2, p3)
assert(not isFlatAngle(p1, p2, p3), ("angle (p1, p2, p3) is flat:\n %s\n %s\n %s") assert(not isFlatAngle(p1, p2, p3), ("angle (p1, p2, p3) is flat:\n %s\n %s\n %s")
:format(tostring(p1), tostring(p2), tostring(p3))) :format(tostring(p1), tostring(p2), tostring(p3)))

View File

@@ -28,6 +28,22 @@ local Triangulation = {
convexMultiplier = 1e3, convexMultiplier = 1e3,
} }
---Finds and writes adjacent triangles into triangles.
---@param triangles MapGen.Triangle[]
local function findTrianglesNeighbours(triangles)
for _, triangle in ipairs(triangles) do
for _, t in ipairs(triangles) do
if triangle.e1 == t.e1 and triangle.p3 ~= t.p3 then
triangle.neighbours.t1 = t
elseif triangle.e2 == t.e2 and triangle.p3 ~= t.p1 then
triangle.neighbours.t2 = t
elseif triangle.e3 == t.e3 and triangle.p3 ~= t.p2 then
triangle.neighbours.t3 = t
end
end
end
end
---The Delaunay method divides a plane into triangles based on a list of their vertices. ---The Delaunay method divides a plane into triangles based on a list of their vertices.
--- ---
---Doesn't take into account the Y coordinate of the vertices. ---Doesn't take into account the Y coordinate of the vertices.
@@ -140,6 +156,8 @@ function Triangulation.triangulate( vertices )
tableRemove( vertices ) tableRemove( vertices )
end end
findTrianglesNeighbours(triangles)
return triangles return triangles
end end

View File

@@ -37,6 +37,8 @@ dofile(core.get_modpath('smc__system__table')..'/init.lua')
-- TODO: пока что закоментировано, поскольку вызывает ошибку из-за global_step -- TODO: пока что закоментировано, поскольку вызывает ошибку из-за global_step
-- Smc lib loader -- Smc lib loader
-- dofile(core.get_modpath('smc__system__helper_lib')..'/init.lua') -- dofile(core.get_modpath('smc__system__helper_lib')..'/init.lua')
Smc = {}
dofile(core.get_modpath('smc__system__helper_lib')..'/src/Lib/color.lua')
---@type MapGen ---@type MapGen
local mapGenerator = dofile(core.get_modpath('smc__core__map_generator')..'/src/mapGenerator.lua') local mapGenerator = dofile(core.get_modpath('smc__core__map_generator')..'/src/mapGenerator.lua')

View File

@@ -52,7 +52,7 @@ mapGenerator:registerLayer('midgard', {
local land1 = { local land1 = {
offset = -20, offset = -20,
scale = 5, scale = 0,
spread = {x = 10, y = 10, z = 10}, spread = {x = 10, y = 10, z = 10},
seed = 47, seed = 47,
octaves = 3, octaves = 3,
@@ -62,7 +62,7 @@ local land1 = {
local land2 = { local land2 = {
offset = 10, offset = 10,
scale = 5, scale = 0,
spread = {x = 30, y = 30, z = 30}, spread = {x = 30, y = 30, z = 30},
seed = 47, seed = 47,
octaves = 3, octaves = 3,
@@ -137,11 +137,34 @@ mapGenerator:register2DPeaks('midgard',
for _, c in ipairs({'landscape', 'temp', 'humidity'}) do for _, c in ipairs({'landscape', 'temp', 'humidity'}) do
mapGenerator:registerLayerNoiseColor('midgard', c, '#4978c6', land1) mapGenerator:registerLayerNoiseColor('midgard', c, '#4978c6', land1)
mapGenerator:registerLayerNoiseColor('midgard', c, '#77ea72', land2) mapGenerator:registerLayerNoiseColor('midgard', c, '#77ea72', land2)
--[[
mapGenerator:register2DPeaks('midgard', c, {
{
color = '#77ea72',
pos = vector.new(512, 0, 512)
},
{
color = '#77ea72',
pos = vector.new(512, 0, -512)
},
{
color = '#77ea72',
pos = vector.new(-512, 0, -512)
},
{
color = '#77ea72',
pos = vector.new(-512, 0, 512)
},
{
color = '#4978c6',
pos = vector.new(0, 0, 0)
},
})--]]
end end
mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 64,{x=8, y=8}, 'landscape') mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 100,{x=0, y=0}, 'landscape')
mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 100,{x=8, y=8}, 'temp') mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 150,{x=0, y=0}, 'temp')
mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 100,{x=8, y=8}, 'humidity') mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 200,{x=0, y=0}, 'humidity')
--[[ --[[
mapGenerator:registerCavern('midgard', 'cavern1', { mapGenerator:registerCavern('midgard', 'cavern1', {

View File

@@ -3,23 +3,5 @@ Smc = {}
local modInfo = Mod.getInfo('smc__system__helper_lib') local modInfo = Mod.getInfo('smc__system__helper_lib')
local require = modInfo.require local require = modInfo.require
---Converts a colorstring into a single representation so that similar colors can match.
---@param color ColorString
---@return ColorString
function Smc.fixColorString(color)
local colorTable = core.colorspec_to_table(color)
if colorTable == nil then
return ''
end
local colorString = core.colorspec_to_colorstring(colorTable)
if colorString == nil then
return ''
end
return colorString
end
require('Lib.globalStep') require('Lib.globalStep')
require('Lib.color')

View File

@@ -0,0 +1,18 @@
---Converts a colorstring into a single representation so that similar colors can match.
---@param color ColorString
---@return ColorString
function Smc.fixColorString(color)
local colorTable = core.colorspec_to_table(color)
if colorTable == nil then
return ''
end
local colorString = core.colorspec_to_colorstring(colorTable)
if colorString == nil then
return ''
end
return colorString
end