namespace LivingVillage.Desktop.Tests open Microsoft.VisualStudio.TestTools.UnitTesting open LivingVillage.Kernel open LivingVillage.Desktop /// P20 第一步回归:确定性生成器 + 尺寸参数化 + 30 spawn 核心可达 + 视口裁剪。 [] type MapGenTests () = let tilesEqual (a: MapGen.Result) (b: MapGen.Result) (message: string) = if a.Tiles.Length <> b.Tiles.Length then Assert.Fail(sprintf "%s: tile count %d <> %d" message a.Tiles.Length b.Tiles.Length) for i in 0 .. a.Tiles.Length - 1 do if a.Tiles.[i] <> b.Tiles.[i] then Assert.Fail(sprintf "%s: tile %d = %d, expected %d" message i a.Tiles.[i] b.Tiles.[i]) let checksum (map: MapGen.Result) : int64 = map.Tiles |> Array.mapi (fun i v -> int64 i * int64 (v + 1)) |> Array.sum let waterRunsInColumn (map: MapGen.Result) (x: int) : int list = let runs = ResizeArray() let mutable run = 0 for y in 0 .. map.Height - 1 do if map.Tiles.[y * map.Width + x] = int MapGen.GroundTile.Water then run <- run + 1 elif run > 0 then runs.Add run run <- 0 if run > 0 then runs.Add run List.ofSeq runs let waterNeighbourCount (map: MapGen.Result) (x: int) (y: int) : int = [ (1, 0); (-1, 0); (0, 1); (0, -1) ] |> List.sumBy (fun (dx, dy) -> let nx, ny = x + dx, y + dy if nx >= 0 && nx < map.Width && ny >= 0 && ny < map.Height && map.Tiles.[ny * map.Width + nx] = int MapGen.GroundTile.Water then 1 else 0) [] member _.SameSeedGeneratesByteIdenticalTerrain () = let a = MapGen.generateWithSize 64 48 (uint64 4242) let b = MapGen.generateWithSize 64 48 (uint64 4242) tilesEqual a b "64x48 same seed" Assert.AreEqual(a.Seed, b.Seed) Assert.AreEqual(checksum a, checksum b) let large1 = MapGen.generateWithSize 256 192 (uint64 7) let large2 = MapGen.generateWithSize 256 192 (uint64 7) tilesEqual large1 large2 "256x192 same seed" Assert.AreEqual(checksum large1, checksum large2) [] member _.DifferentSeedsProduceDifferentTerrain () = let a = MapGen.generateWithSize 256 192 (uint64 1) let b = MapGen.generateWithSize 256 192 (uint64 2) Assert.AreNotEqual(checksum a, checksum b, "different seeds must differ") [] member _.LargeMapThirtySpawnsAllReachCore () = let map = MapGen.generateWithSize 256 192 (uint64 4242) Assert.AreEqual(256, map.Width) Assert.AreEqual(192, map.Height) Assert.AreEqual(30, map.Spawns.Length) Assert.IsTrue(map.ReachabilityOk, "all 30 spawns must reach the core") Assert.IsTrue(map.ReachableTiles > 30, "core region must be walkable") for (x, y) in map.Spawns do Assert.AreNotEqual(int MapGen.GroundTile.Water, map.Tiles.[y * map.Width + x], sprintf "spawn (%d,%d) must be walkable" x y) [] member _.DefaultSizeMatchesLegacyBounds () = let map = MapGen.generate (MapGen.defaultParams (uint64 4242)) Assert.AreEqual(64, map.Width) Assert.AreEqual(48, map.Height) Assert.AreEqual(64 * 48, map.Tiles.Length) Assert.IsTrue(map.ReachabilityOk) Assert.AreEqual(30, map.Spawns.Length) [] member _.VisibleRangeCullsLargeMapToViewport () = let total = 256 * 192 let x0, y0, x1, y1 = MapGen.visibleTileRange 256 192 1000 1000 1280 720 let count = MapGen.visibleTileCount 256 192 1000 1000 1280 720 Assert.AreEqual((x1 - x0 + 1) * (y1 - y0 + 1), count) Assert.IsTrue(count < total, "viewport culling must draw strictly fewer tiles than the whole map") let ox0, oy0, ox1, oy1 = MapGen.visibleTileRange 256 192 0 0 1280 720 Assert.AreEqual(0, ox0) Assert.AreEqual(0, oy0) Assert.AreEqual(1280 / Sim.tilePixels + 1, ox1) Assert.AreEqual(720 / Sim.tilePixels + 1, oy1) // ---- P20 第二步:河道连续性 + 桥/路/建筑 + 交通可达 ---- [] member _.RiversAreContinuousWithoutGapsOrIsolatedTiles () = let map = MapGen.generateWithSize 256 192 (uint64 4242) Assert.IsTrue(map.Rivers.Length >= 1, "at least one river band") let minWidth = map.Rivers |> List.map (fun river -> river.Width) |> List.min Assert.IsTrue(minWidth >= 3, "river width lower bound") 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) [] member _.StructuresContainBridgesRoadsAndBuildingsAlongRivers () = let map = MapGen.generateWithSize 256 192 (uint64 4242) Assert.IsTrue(map.Bridges.Length >= map.Rivers.Length, "at least one bridge per river") Assert.IsTrue(map.Paths.Length > 0, "stone road network exists") Assert.IsTrue(map.Buildings.Length >= 4, "at least four riverside building blocks") for building in map.Buildings do Assert.IsTrue(MapGen.isWalkable map building.DoorX building.DoorY, sprintf "door (%d,%d) must be walkable" building.DoorX building.DoorY) let nearWater = [ for y in max 0 (building.Top - 12) .. min (map.Height - 1) (building.Top + building.Height + 12) do for x in max 0 (building.Left - 12) .. min (map.Width - 1) (building.Left + building.Width + 12) -> map.Tiles.[y * map.Width + x] = int MapGen.GroundTile.Water ] |> List.exists id Assert.IsTrue(nearWater, "building block must sit along a river") [] member _.BridgesKeepBothRiverBanksReachableFromSpawns () = let map = MapGen.generateWithSize 256 192 (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" x y) for building in map.Buildings do Assert.IsTrue(visited.[building.DoorY * map.Width + building.DoorX], "building door unreachable via bridge/road") for (x, y) in map.Bridges do Assert.IsTrue(visited.[y * map.Width + x], sprintf "bridge tile (%d,%d) unreachable" x y) Assert.IsTrue(map.BridgeCrossingsOk, "each bridge must connect both river banks") [] member _.StructuresAreDeterministicAcrossRuns () = let a = MapGen.generateWithSize 256 192 (uint64 4242) let b = MapGen.generateWithSize 256 192 (uint64 4242) Assert.IsTrue(a.Rivers = b.Rivers) Assert.IsTrue(a.Bridges = b.Bridges) Assert.IsTrue(a.Paths = b.Paths) Assert.IsTrue(a.Buildings = b.Buildings) let c = MapGen.generateWithSize 256 192 (uint64 7) Assert.IsTrue(a.Buildings <> c.Buildings || a.Bridges <> c.Bridges, "different seeds must relocate structures") // ---- P20 第三步:序列化确定性 + 默认可玩世界接入生成器站位 ---- [] member _.SameSeedSerializationIsByteIdentical () = let a = MapGen.generateWithSize 256 192 (uint64 4242) let b = MapGen.generateWithSize 256 192 (uint64 4242) Assert.AreEqual(MapGen.serialize a, MapGen.serialize b) let c = MapGen.generateWithSize 256 192 (uint64 7) Assert.AreNotEqual(MapGen.serialize a, MapGen.serialize c) [] member _.MapWorldPlacementUsesReachableCoreSpawns () = let map = MapGen.generateWithSize 256 192 (uint64 4242) let world = WorldBootstrap.initialWorldInMap false 42UL Sim.npcCount map Assert.AreEqual(Sim.npcCount, world.Npcs.Length) let visited = MapGen.floodFill map (fst map.Spawns.Head) (snd map.Spawns.Head) for npc in world.Npcs do let tx = int npc.Pos.X / Sim.tilePixels let ty = int npc.Pos.Y / Sim.tilePixels Assert.IsTrue(visited.[ty * map.Width + tx], sprintf "npc tile (%d,%d) must be reachable from the core" tx ty) let ax = int world.Avatar.Pos.X / Sim.tilePixels let ay = int world.Avatar.Pos.Y / Sim.tilePixels 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 扩程(尺寸自适应参数 + 连续性/可达性 + 默认图锁定) ---- [] member _.SizeAdaptiveParamsScaleRiversWithHeight () = Assert.AreEqual(2, (MapGen.paramsForSize 64 48 (uint64 1)).RiverCount) Assert.AreEqual(2, (MapGen.paramsForSize 256 192 (uint64 1)).RiverCount) Assert.AreEqual(4, (MapGen.paramsForSize 512 384 (uint64 1)).RiverCount) [] 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(MapGen.serialize a, MapGen.serialize b) Assert.AreEqual(512, a.Width) Assert.AreEqual(384, a.Height) Assert.AreEqual(30, a.Spawns.Length) Assert.IsTrue(a.ReachabilityOk, "512x384 must keep the core reachable") Assert.IsTrue(a.BridgeCrossingsOk, "512x384 bridges must connect both banks") [] member _.HugeMap512DifferentSeedsDiverge () = let a = MapGen.generateWithSize 512 384 (uint64 4242) let b = MapGen.generateWithSize 512 384 (uint64 7) Assert.AreNotEqual(checksum a, checksum b, "different seeds must differ at 512x384") Assert.AreNotEqual(MapGen.serialize a, MapGen.serialize b) [] member _.HugeMap512RiversStayContinuousAcrossAllBands () = let map = MapGen.generateWithSize 512 384 (uint64 4242) Assert.AreEqual(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) [] 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") // ---- P24:512x384 沿河民居加密(默认图不受影响) ---- [] member _.ExtraHouseDensityOnlyEnabledForLargeMaps () = Assert.AreEqual(0, (MapGen.paramsForSize 64 48 (uint64 1)).ExtraRiversideHouses) Assert.AreEqual(0, (MapGen.paramsForSize 256 192 (uint64 1)).ExtraRiversideHouses) Assert.IsTrue((MapGen.paramsForSize 512 384 (uint64 1)).ExtraRiversideHouses > 0) [] member _.LargeMapHasAtLeastTwelveHousesWithReachableDoors () = let map = MapGen.generateWithSize 512 384 (uint64 4242) Assert.IsTrue(map.Buildings.Length >= 12, sprintf "512x384 must place >=12 houses, saw %d" map.Buildings.Length) let startX, startY = map.Spawns.Head let visited = MapGen.floodFill map startX startY for building in map.Buildings do Assert.IsTrue(MapGen.isWalkable map building.DoorX building.DoorY, sprintf "door (%d,%d) must be walkable" building.DoorX building.DoorY) Assert.IsTrue(visited.[building.DoorY * map.Width + building.DoorX], sprintf "door (%d,%d) unreachable" building.DoorX building.DoorY) Assert.IsTrue(map.ReachabilityOk, "512x384 reachability must hold with extra houses") [] member _.LargeMapHousePlacementIsDeterministic () = let a = MapGen.generateWithSize 512 384 (uint64 4242) let b = MapGen.generateWithSize 512 384 (uint64 4242) Assert.AreEqual(a.Buildings.Length, b.Buildings.Length) Assert.IsTrue(a.Buildings = b.Buildings) Assert.AreEqual(MapGen.serialize a, MapGen.serialize b) /// P28:512 沿河民居由 10 -> 16 增密(+6),并出现 2/3/4 三档宽度(附属小筑/民居/大宅), /// 形成聚落层次;默认图仍为 0,不消耗 RNG,故 64/256 checksum 锁定不变。 [] member _.LargeMapHouseDensityHierarchyOnlyGrowsLargeMaps () = Assert.AreEqual(16, (MapGen.paramsForSize 512 384 (uint64 1)).ExtraRiversideHouses) Assert.AreEqual(0, (MapGen.paramsForSize 64 48 (uint64 1)).ExtraRiversideHouses) Assert.AreEqual(0, (MapGen.paramsForSize 256 192 (uint64 1)).ExtraRiversideHouses) let map = MapGen.generateWithSize 512 384 (uint64 4242) Assert.IsTrue(map.Buildings.Length >= 22, sprintf "P28 512 must place >=22 houses, saw %d" map.Buildings.Length) let widths = map.Buildings |> List.map (fun b -> b.Width) |> Set.ofList Assert.IsTrue(widths.Contains 2, "outbuilding width 2 must appear") Assert.IsTrue(widths.Contains 3, "house width 3 must appear") Assert.IsTrue(widths.Contains 4, "hall width 4 must appear") Assert.AreEqual(24, map.Buildings.Length) [] member _.DefaultSizeHouseCountUnchanged () = Assert.AreEqual(4, (MapGen.generateWithSize 64 48 (uint64 4242)).Buildings.Length) Assert.AreEqual(4, (MapGen.generateWithSize 256 192 (uint64 4242)).Buildings.Length) // ---- P25:大图不产孤立石板 tile(装饰石块默认开启仅限小图) ---- [] member _.DecorativeStonesDisabledOnLargeMaps () = Assert.IsTrue((MapGen.paramsForSize 64 48 (uint64 1)).DecorativeStones) Assert.IsTrue((MapGen.paramsForSize 256 192 (uint64 1)).DecorativeStones) Assert.IsFalse((MapGen.paramsForSize 512 384 (uint64 1)).DecorativeStones) [] member _.LargeMapHasNoIsolatedStoneTiles () = let map = MapGen.generateWithSize 512 384 (uint64 4242) let pathSet = System.Collections.Generic.HashSet(map.Paths |> List.map (fun (x, y) -> y * map.Width + x)) let stone x y = map.Tiles.[y * map.Width + x] = int MapGen.GroundTile.Stone let mutable isolated = 0 for y in 0 .. map.Height - 1 do for x in 0 .. map.Width - 1 do if stone x y && not (pathSet.Contains(y * map.Width + x)) then let neighbours = [ (x + 1, y); (x - 1, y); (x, y + 1); (x, y - 1) ] |> List.filter (fun (nx, ny) -> nx >= 0 && nx < map.Width && ny >= 0 && ny < map.Height) |> List.filter (fun (nx, ny) -> stone nx ny) |> List.length if neighbours = 0 then isolated <- isolated + 1 Assert.AreEqual(0, isolated) // 装饰关闭后,大图所有石板都应属于路网。 let stonesOutsidePaths = [ for y in 0 .. map.Height - 1 do for x in 0 .. map.Width - 1 do if stone x y && not (pathSet.Contains(y * map.Width + x)) then yield (x, y) ] Assert.AreEqual(0, stonesOutsidePaths.Length) /// 锁定当前默认尺寸的生成结果,防止 P23 参数适配无意改变 64x48 / 256x192。 [] member _.DefaultSizeChecksumsArePinned () = Assert.AreEqual(6042052L, checksum (MapGen.generateWithSize 64 48 (uint64 4242))) Assert.AreEqual(1284656962L, checksum (MapGen.generateWithSize 256 192 (uint64 4242))) // ---- P26:河岸装饰(护岸石垒/芦苇/垂柳)仅大图、落在岸格且不挡通行 ---- [] member _.RiverDecorationsOnlyEnabledOnLargeMaps () = Assert.IsFalse((MapGen.paramsForSize 64 48 (uint64 1)).RiverDecorations) Assert.IsFalse((MapGen.paramsForSize 256 192 (uint64 1)).RiverDecorations) Assert.IsTrue((MapGen.paramsForSize 512 384 (uint64 1)).RiverDecorations) Assert.AreEqual(0, (MapGen.generateWithSize 64 48 (uint64 4242)).Decorations.Length) Assert.AreEqual(0, (MapGen.generateWithSize 256 192 (uint64 4242)).Decorations.Length) [] member _.RiverbankDecorationsSitOnBanksAndNeverBlockPathsOrBridges () = let map = MapGen.generateWithSize 512 384 (uint64 4242) Assert.IsTrue(map.Decorations.Length > 0, "512x384 must place riverbank decorations") let pathSet = System.Collections.Generic.HashSet(map.Paths |> List.map (fun (x, y) -> y * map.Width + x)) let bridgeSet = System.Collections.Generic.HashSet(map.Bridges |> List.map (fun (x, y) -> y * map.Width + x)) let water x y = map.Tiles.[y * map.Width + x] = int MapGen.GroundTile.Water let inCore x y = x >= map.Core.MinX && x <= map.Core.MaxX && y >= map.Core.MinY && y <= map.Core.MaxY for decoration in map.Decorations do let footprint = match decoration.Kind with | MapGen.Willow -> [ decoration.Y; decoration.Y + 1 ] | _ -> [ decoration.Y ] for y in footprint do let i = y * map.Width + decoration.X Assert.AreNotEqual(int MapGen.GroundTile.Water, map.Tiles.[i], "decoration must sit on land") Assert.IsFalse(pathSet.Contains i, "decoration must not block a path tile") Assert.IsFalse(bridgeSet.Contains i, "decoration must not block a bridge tile") Assert.IsFalse(inCore decoration.X y, "decoration must stay outside the core") // Bank adjacency: at least one footprint tile must touch the river orthogonally. let touchesWater = footprint |> List.exists (fun y -> [ (decoration.X + 1, y); (decoration.X - 1, y); (decoration.X, y + 1); (decoration.X, y - 1) ] |> List.exists (fun (nx, ny) -> nx >= 0 && nx < map.Width && ny >= 0 && ny < map.Height && water nx ny)) Assert.IsTrue(touchesWater, "decoration must line the riverbank, not float inland") [] member _.LargeMapRiverDecorationsAreDeterministic () = let a = MapGen.generateWithSize 512 384 (uint64 4242) let b = MapGen.generateWithSize 512 384 (uint64 4242) Assert.IsTrue(a.Decorations = b.Decorations) Assert.AreEqual(MapGen.serialize a, MapGen.serialize b) let c = MapGen.generateWithSize 512 384 (uint64 7) Assert.AreNotEqual(MapGen.serialize a, MapGen.serialize c) [] member _.VisibleRangeClampsAtLegacyMapEdges () = let x0, y0, x1, y1 = MapGen.visibleTileRange 64 48 0 0 2048 1536 Assert.AreEqual(0, x0) Assert.AreEqual(0, y0) Assert.AreEqual(63, x1) Assert.AreEqual(47, y1) Assert.AreEqual(64 * 48, MapGen.visibleTileCount 64 48 0 0 2048 1536)