summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/LivingVillage.Desktop.Tests/MapGenTests.fs63
-rw-r--r--src/LivingVillage.Desktop/MapGen.fs35
2 files changed, 93 insertions, 5 deletions
diff --git a/src/LivingVillage.Desktop.Tests/MapGenTests.fs b/src/LivingVillage.Desktop.Tests/MapGenTests.fs
index 4079ad0..4d11dd1 100644
--- a/src/LivingVillage.Desktop.Tests/MapGenTests.fs
+++ b/src/LivingVillage.Desktop.Tests/MapGenTests.fs
@@ -172,6 +172,69 @@ type MapGenTests () =
Assert.IsTrue(visited.[ay * map.Width + ax], "avatar spawn must sit in the reachable core")
Assert.IsTrue(ax >= map.Core.MinX && ax <= map.Core.MaxX && ay >= map.Core.MinY && ay <= map.Core.MaxY, "avatar spawn must sit in the core")
+ // ---- P23:512x384 扩程(尺寸自适应参数 + 连续性/可达性 + 默认图锁定) ----
+
+ [<TestMethod>]
+ member _.SizeAdaptiveParamsScaleRiversWithHeight () =
+ Assert.AreEqual<int>(2, (MapGen.paramsForSize 64 48 (uint64 1)).RiverCount)
+ Assert.AreEqual<int>(2, (MapGen.paramsForSize 256 192 (uint64 1)).RiverCount)
+ Assert.AreEqual<int>(4, (MapGen.paramsForSize 512 384 (uint64 1)).RiverCount)
+
+ [<TestMethod>]
+ member _.HugeMap512SameSeedIsByteIdentical () =
+ let a = MapGen.generateWithSize 512 384 (uint64 4242)
+ let b = MapGen.generateWithSize 512 384 (uint64 4242)
+ tilesEqual a b "512x384 same seed"
+ Assert.AreEqual<string>(MapGen.serialize a, MapGen.serialize b)
+ Assert.AreEqual<int>(512, a.Width)
+ Assert.AreEqual<int>(384, a.Height)
+ Assert.AreEqual<int>(30, a.Spawns.Length)
+ Assert.IsTrue(a.ReachabilityOk, "512x384 must keep the core reachable")
+ Assert.IsTrue(a.BridgeCrossingsOk, "512x384 bridges must connect both banks")
+
+ [<TestMethod>]
+ member _.HugeMap512DifferentSeedsDiverge () =
+ let a = MapGen.generateWithSize 512 384 (uint64 4242)
+ let b = MapGen.generateWithSize 512 384 (uint64 7)
+ Assert.AreNotEqual<int64>(checksum a, checksum b, "different seeds must differ at 512x384")
+ Assert.AreNotEqual<string>(MapGen.serialize a, MapGen.serialize b)
+
+ [<TestMethod>]
+ member _.HugeMap512RiversStayContinuousAcrossAllBands () =
+ let map = MapGen.generateWithSize 512 384 (uint64 4242)
+ Assert.AreEqual<int>(4, map.Rivers.Length)
+ let minWidth = map.Rivers |> List.map (fun river -> river.Width) |> List.min
+ Assert.IsTrue(minWidth >= 3, "river width lower bound holds at 512x384")
+ for x in 1 .. map.Width - 2 do
+ let runs = waterRunsInColumn map x
+ if runs.Length <> map.Rivers.Length then
+ Assert.Fail(sprintf "column %d must hold exactly %d contiguous runs, saw %d" x map.Rivers.Length runs.Length)
+ for run in runs do
+ Assert.IsTrue(run >= minWidth, sprintf "column %d has a run of %d < min width %d" x run minWidth)
+ for y in 0 .. map.Height - 1 do
+ for x in 0 .. map.Width - 1 do
+ if map.Tiles.[y * map.Width + x] = int MapGen.GroundTile.Water then
+ Assert.IsTrue(waterNeighbourCount map x y >= 2, sprintf "water tile (%d,%d) is isolated" x y)
+
+ [<TestMethod>]
+ member _.HugeMap512ThirtySpawnsReachCoreAndBothBanks () =
+ let map = MapGen.generateWithSize 512 384 (uint64 4242)
+ let startX, startY = map.Spawns.Head
+ let visited = MapGen.floodFill map startX startY
+ for (x, y) in map.Spawns do
+ Assert.IsTrue(visited.[y * map.Width + x], sprintf "spawn (%d,%d) unreachable at 512x384" x y)
+ for building in map.Buildings do
+ Assert.IsTrue(visited.[building.DoorY * map.Width + building.DoorX], "building door unreachable at 512x384")
+ for (x, y) in map.Bridges do
+ Assert.IsTrue(visited.[y * map.Width + x], sprintf "bridge tile (%d,%d) unreachable at 512x384" x y)
+ Assert.IsTrue(map.Buildings.Length >= map.Rivers.Length, "riverside building per band at 512x384")
+
+ /// 锁定当前默认尺寸的生成结果,防止 P23 参数适配无意改变 64x48 / 256x192。
+ [<TestMethod>]
+ member _.DefaultSizeChecksumsArePinned () =
+ Assert.AreEqual<int64>(6042052L, checksum (MapGen.generateWithSize 64 48 (uint64 4242)))
+ Assert.AreEqual<int64>(1284656962L, checksum (MapGen.generateWithSize 256 192 (uint64 4242)))
+
[<TestMethod>]
member _.VisibleRangeClampsAtLegacyMapEdges () =
let x0, y0, x1, y1 = MapGen.visibleTileRange 64 48 0 0 2048 1536
diff --git a/src/LivingVillage.Desktop/MapGen.fs b/src/LivingVillage.Desktop/MapGen.fs
index 7c7059a..ce9bccf 100644
--- a/src/LivingVillage.Desktop/MapGen.fs
+++ b/src/LivingVillage.Desktop/MapGen.fs
@@ -72,6 +72,15 @@ module MapGen =
SpawnColumns = 5
SpawnRows = 6 }
+ /// P23 尺寸自适应参数:河道数量随高度增长(约每 96 行一条),其余沿用默认。
+ /// 64x48 / 256x192 仍得到 2 条河道,与既有输出逐字节一致;512x384 得到 4 条。
+ let paramsForSize (width: int) (height: int) (seed: uint64) : Params =
+ let base_ = defaultParams seed
+ { base_ with
+ Width = width
+ Height = height
+ RiverCount = max base_.RiverCount (max 1 (height / 96)) }
+
// ---- splitmix64 与 value noise(无外部依赖、无时钟) ----
let private splitmix (state: uint64) : uint64 * uint64 =
@@ -104,6 +113,10 @@ module MapGen =
(v00 * (1.0f - sx) + v10 * sx) * (1.0f - sy) + (v01 * (1.0f - sx) + v11 * sx) * sy
/// 2-3 octave 叠加;特征尺度随地图宽度等比缩放,保证小图/大图观感一致。
+ /// P23:特征尺度封顶(<=4x),避免 512x384 退化成过于平滑的「放大图」。
+ let private noiseWidthScale (width: int) : float32 =
+ min 4.0f (float32 width / 64.0f)
+
let private multiOctave (widthScale: float32) (x: int) (y: int) (seed: uint64) : float32 =
let xf = float32 x
let yf = float32 y
@@ -138,7 +151,7 @@ module MapGen =
x >= core.MinX && x <= core.MaxX && y >= core.MinY && y <= core.MaxY
// 1) 多八度噪声:低洼草地转泥炭(跳过核心区)。
- let widthScale = float32 w / 64.0f
+ let widthScale = noiseWidthScale w
for y in 0 .. h - 1 do
for x in 0 .. w - 1 do
if multiOctave widthScale x y p.Seed < 0.18f && not (inCore x y) then
@@ -148,14 +161,26 @@ module MapGen =
let riverWidth = max 3 p.RiverWidth
let riverBands =
[ for i in 0 .. max 0 p.RiverCount - 1 do
- if i % 2 = 0 then
+ if i = 0 then
let centerY = max 1 (min (h - 1 - riverWidth) (min (core.MinY - riverWidth - 2) (h / 4)))
if centerY + riverWidth - 1 < core.MinY then
yield { CenterY = centerY; Width = riverWidth }
- else
+ elif i = 1 then
let centerY = max 1 (min (h - 1 - riverWidth) (max (core.MaxY + 2) (3 * h / 4)))
if centerY > core.MaxY then
- yield { CenterY = centerY; Width = riverWidth } ]
+ yield { CenterY = centerY; Width = riverWidth }
+ else
+ // P23:第 3 条起在核心上/下方逐层外扩堆叠,互不重叠、不压核心。
+ let layer = (i - 1) / 2
+ let offset = if i % 2 = 1 then layer - 1 else layer
+ if i % 2 = 0 then
+ let centerY = core.MinY - riverWidth - 2 - offset * (riverWidth + 3)
+ if centerY >= 1 then
+ yield { CenterY = centerY; Width = riverWidth }
+ else
+ let centerY = core.MaxY + 2 + offset * (riverWidth + 3)
+ if centerY + riverWidth - 1 <= h - 2 then
+ yield { CenterY = centerY; Width = riverWidth } ]
for river in riverBands do
for x in 1 .. w - 2 do
for dy in 0 .. river.Width - 1 do
@@ -310,7 +335,7 @@ module MapGen =
BridgeCrossingsOk = crossingsOk }
let generateWithSize (width: int) (height: int) (seed: uint64) : Result =
- generate { defaultParams seed with Width = width; Height = height }
+ generate (paramsForSize width height seed)
/// 生成结果的确定性序列化(同 seed 逐字节一致),供回归测试与跨机核对。
let serialize (map: Result) : string =