|
- <#
- .SYNOPSIS
- Verifies generated Land Map 64 worlds (R1).
- .DESCRIPTION
- Without -Runs: checks the world captured by tools/smoke.ps1 (build/smoke/c64/world.bin).
- With -Runs N: runs the program headless in VICE N times (VICE randomises the autostart
- delay, so the timer seed and the world differ per run), captures each world at
- smoke_ready, and checks every one. With 5 or more worlds it also checks that the
- terrain mix differs between worlds (R1.6).
-
- Checks per world:
- - 80x50 tile IDs, all known (src/tiles.inc order)
- - every terrain type present: land, grass, trees, stone, water (R1.2)
- - no road or bridge tiles: roads are not generated (M5.1); paths are worn in later by
- the people (M5.2)
- - water: no strip one cell thin, and every water cell has the shore tile its
- neighbours call for (outside the world and bridges count as water) (R1.4)
- - spread: every 40x20-cell view contains at least three of land, grass and trees,
- water and stone, with at least 8 cells each (R1.5)
- - with -Runs: the map is ready (smoke_ready) within 15 seconds of NTSC time after
- autostart (R1.1); also reports how long generation took (generate_world to smoke_ready)
- Exit 0 when every world passes, 1 on a failure, 2 when a tool is missing.
- #>
- [CmdletBinding()]
- param(
- [string]$Path,
- [int]$Runs = 0,
- [switch]$ShowMap,
- [switch]$NoBuild,
- [int]$TimeoutSeconds = 120
- )
- $ErrorActionPreference = 'Stop'
- . (Join-Path $PSScriptRoot 'lib.ps1')
-
- $width = 80; $height = 50
- # Tile IDs from src/tiles.inc.
- $LAND = 0; $GRASS = 1; $TREE = 2; $STONE = 3; $WATER = 4
- $WATER_N = 5; $WATER_S = 6; $WATER_W = 7; $WATER_E = 8
- $WATER_NW = 9; $WATER_NE = 10; $WATER_SW = 11; $WATER_SE = 12
- $ROAD_H = 13; $ROAD_V = 14; $ROAD_X = 19; $PATH_LAND = 20; $PATH_TREE = 22
- $BRIDGE_H = 23; $BRIDGE_V = 24; $BRIDGE_X = 25
- $TILE_COUNT = 26 # src/tiles.inc enum; no road, path or bridge tile at generation
- $glyphs = '.gT#~~~~~~~~~-|+++++:::=!*'
- # Shore tile by [row part][column part]: row 0 = open to the north, 2 = open to the south.
- $shore = @(@($WATER_NW, $WATER_N, $WATER_NE), @($WATER_W, $WATER, $WATER_E), @($WATER_SW, $WATER_S, $WATER_SE))
- $NTSC_HZ = 1022727
- $maxCycles = 15 * $NTSC_HZ
-
- function Test-WaterFamily([int]$t) { $t -ge $WATER -and $t -le $WATER_SE }
- function Test-Bridge([int]$t) { $t -ge $BRIDGE_H -and $t -le $BRIDGE_X }
-
- # Class of a tile for the spread check: 0 land, 1 grass/trees, 2 water, 3 stone, -1 road/bridge.
- function Get-TileClass([int]$t) {
- if ($t -eq $LAND) { 0 } elseif ($t -eq $GRASS -or $t -eq $TREE) { 1 }
- elseif (Test-WaterFamily $t) { 2 } elseif ($t -eq $STONE) { 3 } else { -1 }
- }
-
- function Test-World([byte[]]$tiles, [string]$name) {
- $fail = [System.Collections.Generic.List[string]]::new()
- if ($tiles.Length -ne $width * $height) { $fail.Add("expected 4000 bytes, got $($tiles.Length)"); return @{ failures = $fail } }
- $at = { param($x, $y) $tiles[$y * $width + $x] }
-
- # Tile IDs and terrain types present.
- $counts = [int[]]::new(256)
- foreach ($t in $tiles) { $counts[$t]++ }
- for ($t = $TILE_COUNT; $t -lt 256; $t++) { if ($counts[$t]) { $fail.Add("unknown tile ID $t ($($counts[$t]) cells)") } }
- $waterCells = 0; for ($t = $WATER; $t -le $WATER_SE; $t++) { $waterCells += $counts[$t] }
- foreach ($p in @(@('land', $counts[$LAND]), @('grass', $counts[$GRASS]), @('trees', $counts[$TREE]), @('stone', $counts[$STONE]), @('water', $waterCells))) {
- if ($p[1] -eq 0) { $fail.Add("no $($p[0]) anywhere") }
- }
-
- # No roads, paths or bridges at generation time (paths are worn in later).
- $roadCells = 0; for ($t = $ROAD_H; $t -lt $TILE_COUNT; $t++) { $roadCells += $counts[$t] }
- if ($roadCells -gt 0) { $fail.Add("$roadCells road, path or bridge cells: the generator makes none") }
-
- # Water: neighbours count as water if water family, bridge, or outside the world.
- $isWet = { param($x, $y)
- if ($x -lt 0 -or $y -lt 0 -or $x -ge $width -or $y -ge $height) { return $true }
- $t = & $at $x $y; (Test-WaterFamily $t) -or (Test-Bridge $t) }
- for ($y = 0; $y -lt $height; $y++) {
- for ($x = 0; $x -lt $width; $x++) {
- $t = & $at $x $y
- if (Test-WaterFamily $t) {
- $n = & $isWet $x ($y - 1); $s = & $isWet $x ($y + 1); $w = & $isWet ($x - 1) $y; $e = & $isWet ($x + 1) $y
- if (-not $n -and -not $s) { $fail.Add("water one cell thin (north-south) at ($x,$y)"); continue }
- if (-not $w -and -not $e) { $fail.Add("water one cell thin (west-east) at ($x,$y)"); continue }
- $r = if (-not $n) { 0 } elseif (-not $s) { 2 } else { 1 }
- $c = if (-not $w) { 0 } elseif (-not $e) { 2 } else { 1 }
- if ($t -ne $shore[$r][$c]) { $fail.Add("water at ($x,$y) is tile $t, its neighbours call for $($shore[$r][$c])") }
- } elseif ($t -eq $BRIDGE_H -and -not ((& $isWet $x ($y - 1)) -or (& $isWet $x ($y + 1)))) {
- $fail.Add("bridge at ($x,$y) has no water north or south of it")
- } elseif ($t -eq $BRIDGE_V -and -not ((& $isWet ($x - 1) $y) -or (& $isWet ($x + 1) $y))) {
- $fail.Add("bridge at ($x,$y) has no water west or east of it")
- }
- }
- }
-
- # Spread: prefix sums per class, then every 40x20 window.
- $sum = 0..3 | ForEach-Object { , ([int[]]::new(($width + 1) * ($height + 1))) }
- for ($y = 0; $y -lt $height; $y++) {
- for ($x = 0; $x -lt $width; $x++) {
- $k = Get-TileClass (& $at $x $y)
- for ($c = 0; $c -lt 4; $c++) {
- $v = if ($k -eq $c) { 1 } else { 0 }
- $i = ($y + 1) * ($width + 1) + ($x + 1)
- $sum[$c][$i] = $v + $sum[$c][$i - 1] + $sum[$c][$i - ($width + 1)] - $sum[$c][$i - ($width + 1) - 1]
- }
- }
- }
- $worst = 4; $worstAt = ''
- for ($y = 0; $y -le $height - 20; $y++) {
- for ($x = 0; $x -le $width - 40; $x++) {
- $present = 0
- for ($c = 0; $c -lt 4; $c++) {
- $a = $sum[$c]; $W = $width + 1
- $n = $a[($y + 20) * $W + $x + 40] - $a[$y * $W + $x + 40] - $a[($y + 20) * $W + $x] + $a[$y * $W + $x]
- if ($n -ge 8) { $present++ }
- }
- if ($present -lt $worst) { $worst = $present; $worstAt = "($x,$y)" }
- }
- }
- if ($worst -lt 3) { $fail.Add("the 40x20 view at $worstAt has only $worst terrain types with 8+ cells") }
-
- $total = 0; $share = 0..3 | ForEach-Object { $sum[$_][$height * ($width + 1) + $width] }
- foreach ($v in $share) { $total += $v }
- @{ failures = $fail; share = @($share | ForEach-Object { [math]::Round(100 * $_ / $total) }); worstView = $worst }
- }
-
- function Show-World([byte[]]$tiles) {
- for ($y = 0; $y -lt $height; $y++) {
- -join ($tiles[($y * $width)..($y * $width + $width - 1)] | ForEach-Object { if ($_ -lt $glyphs.Length) { $glyphs[$_] } else { '?' } })
- }
- }
-
- # ---------------------------------------------------------------- collect worlds
- $worlds = [System.Collections.Generic.List[hashtable]]::new()
- if ($Runs -le 0) {
- if (-not $Path) { $Path = Join-Path $PSScriptRoot '../build/smoke/c64/world.bin' }
- $worlds.Add(@{ name = 'smoke world'; tiles = [IO.File]::ReadAllBytes((Resolve-Path -LiteralPath $Path).Path); cycles = $null })
- } else {
- try {
- $project = Get-ProjectConfig
- $target = Get-ProjectTargets $project 'c64'
- if (-not $NoBuild) { Invoke-TargetBuild $project $target @{} }
- $emu = Get-ToolPath $project $target.emulator.binary
- } catch [MissingToolException] {
- Write-Host $_.Exception.Message -ForegroundColor Yellow
- exit 2
- }
- $labels = @{}
- foreach ($line in Get-Content -LiteralPath (Get-RepoPath $target.build.labels)) {
- $parts = $line -split '\s+'
- if ($parts.Count -ge 3) { $labels[$parts[2].TrimStart('.')] = [Convert]::ToInt32($parts[1], 16) }
- }
- $root = Get-RepoPath 'build/check-world'
- if (Test-Path -LiteralPath $root) { Remove-Item -LiteralPath $root -Recurse -Force }
- for ($run = 1; $run -le $Runs; $run++) {
- $dir = Join-Path $root "run$run"
- New-Item -ItemType Directory -Force -Path $dir | Out-Null
- $mon = @('logname "monitor.log"', 'log on',
- ('trace exec ${0:x4}' -f $labels.smoke_ready),
- ('command 1 "bsave \"world.bin\" 0 {0:x4} {1:x4}"' -f $labels.world_tiles, ($labels.world_tiles + $width * $height - 1)),
- ('trace exec ${0:x4}' -f $labels.generate_world))
- $n = 2
- # No key may reach the program: keys typed on the PC while VICE has focus would.
- foreach ($row in 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space', 'key_row_p') {
- $n++; $mon += ('trace exec ${0:x4}' -f $labels[$row]); $mon += ('command {0} "r a = ff"' -f $n)
- }
- Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $mon
- Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value ''
- $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $target)
- $vargs += @('-limitcycles', [string]($maxCycles + 2000000), '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target))
- $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } }
- $p = Start-Process -FilePath $emu -ArgumentList $quoted -WorkingDirectory $dir -PassThru `
- -RedirectStandardOutput (Join-Path $dir 'vice.log') -RedirectStandardError (Join-Path $dir 'vice.err')
- if (-not $p.WaitForExit($TimeoutSeconds * 1000)) { $p.Kill($true); Write-Host "[FAIL] run $run`: VICE did not exit in time." -ForegroundColor Red; exit 1 }
- $file = Join-Path $dir 'world.bin'
- if (-not (Test-Path -LiteralPath $file)) { Write-Host "[FAIL] run $run`: smoke_ready not reached within 15 s of NTSC time (R1.1)." -ForegroundColor Red; exit 1 }
- # The trace's instruction line ends with the CPU clock.
- $clock = @{}
- foreach ($log in 'monitor.log', 'vice.log') {
- $lf = Join-Path $dir $log
- if (-not (Test-Path -LiteralPath $lf)) { continue }
- $lines = Get-Content -LiteralPath $lf
- for ($i = 0; $i -lt $lines.Count; $i++) {
- if ($lines[$i] -match '^#([12]) \(Trace\s+exec' -and -not $clock.ContainsKey($Matches[1])) {
- $id = $Matches[1]
- for ($j = $i + 1; $j -lt [math]::Min($i + 4, $lines.Count); $j++) { if ($lines[$j] -match '^\.C:.*\s(\d+)\s*$') { $clock[$id] = [long]$Matches[1]; break } }
- }
- }
- if ($clock.Count -eq 2) { break }
- }
- $gen = if ($clock.ContainsKey('1') -and $clock.ContainsKey('2')) { $clock['1'] - $clock['2'] } else { $null }
- $worlds.Add(@{ name = "run $run"; tiles = [IO.File]::ReadAllBytes($file); cycles = $clock['1']; genCycles = $gen })
- }
- }
-
- # ---------------------------------------------------------------- check
- $failed = $false
- $shares = [System.Collections.Generic.List[int[]]]::new()
- foreach ($w in $worlds) {
- $r = Test-World $w.tiles $w.name
- $time = if ($null -ne $w.cycles) { ', ready {0:N1} s after power-on' -f ($w.cycles / $NTSC_HZ) } else { '' }
- if ($null -ne $w.genCycles) { $time += ' (generation {0:N1} s)' -f ($w.genCycles / $NTSC_HZ) }
- if ($null -ne $w.cycles -and $w.cycles -gt $maxCycles) { $r.failures.Add('generation took longer than 15 s (R1.1)') }
- $mix = if ($r.share) { 'land {0}% grass/trees {1}% water {2}% stone {3}%' -f $r.share[0], $r.share[1], $r.share[2], $r.share[3] } else { '' }
- if ($r.failures.Count) {
- $failed = $true
- Write-Host "[FAIL] $($w.name): $mix$time" -ForegroundColor Red
- $r.failures | Select-Object -First 10 | ForEach-Object { Write-Host " $_" }
- if ($r.failures.Count -gt 10) { Write-Host " ... $($r.failures.Count - 10) more" }
- } else {
- Write-Host "[PASS] $($w.name): $mix; every 40x20 view has $($r.worstView)+ terrain types$time"
- $shares.Add([int[]]$r.share)
- }
- if ($ShowMap) { Show-World $w.tiles }
- }
- if ($worlds.Count -ge 5 -and -not $failed) {
- $names = 'land', 'grass/trees', 'water', 'stone'
- $spread = 0..3 | ForEach-Object { $c = $_; $v = $shares | ForEach-Object { $_[$c] }; ($v | Measure-Object -Maximum).Maximum - ($v | Measure-Object -Minimum).Minimum }
- $line = (0..3 | ForEach-Object { '{0} {1}' -f $names[$_], $spread[$_] }) -join ', '
- if (($spread | Where-Object { $_ -ge 10 }).Count -lt 2) {
- Write-Host "[FAIL] the terrain mix hardly varies between worlds (percentage-point spread: $line); R1.6 needs 10+ for at least two types" -ForegroundColor Red
- $failed = $true
- } else {
- Write-Host "[PASS] the terrain mix varies between worlds (percentage-point spread: $line)"
- }
- }
- if ($failed) { exit 1 }
- Write-Output "[PASS] $($worlds.Count) world(s) checked."
|