Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

242 Zeilen
13KB

  1. <#
  2. .SYNOPSIS
  3. Verifies generated Land Map 64 worlds (R1).
  4. .DESCRIPTION
  5. Without -Runs: checks the world captured by tools/smoke.ps1 (build/smoke/c64/world.bin).
  6. With -Runs N: runs the program headless in VICE N times (VICE randomises the autostart
  7. delay, so the timer seed and the world differ per run), captures each world at
  8. smoke_ready, and checks every one. With 5 or more worlds it also checks that the
  9. terrain mix differs between worlds (R1.6).
  10. Checks per world:
  11. - 80x50 tile IDs, all known (src/tiles.inc order)
  12. - every terrain type present: land, grass, trees, stone, water (R1.2)
  13. - roads: exactly two full east-west rows and two full north-south columns, crossings
  14. where they meet, no road tile anywhere else; every bridge has water beside it (R1.3)
  15. - water: no strip one cell thin, and every water cell has the shore tile its
  16. neighbours call for (outside the world and bridges count as water) (R1.4)
  17. - spread: every 40x20-cell view contains at least three of land, grass and trees,
  18. water and stone, with at least 8 cells each (R1.5)
  19. - with -Runs: the map is ready (smoke_ready) within 15 seconds of NTSC time after
  20. autostart (R1.1); also reports how long generation took (generate_world to smoke_ready)
  21. Exit 0 when every world passes, 1 on a failure, 2 when a tool is missing.
  22. #>
  23. [CmdletBinding()]
  24. param(
  25. [string]$Path,
  26. [int]$Runs = 0,
  27. [switch]$ShowMap,
  28. [switch]$NoBuild,
  29. [int]$TimeoutSeconds = 120
  30. )
  31. $ErrorActionPreference = 'Stop'
  32. . (Join-Path $PSScriptRoot 'lib.ps1')
  33. $width = 80; $height = 50
  34. # Tile IDs from src/tiles.inc.
  35. $LAND = 0; $GRASS = 1; $TREE = 2; $STONE = 3; $WATER = 4
  36. $WATER_N = 5; $WATER_S = 6; $WATER_W = 7; $WATER_E = 8
  37. $WATER_NW = 9; $WATER_NE = 10; $WATER_SW = 11; $WATER_SE = 12
  38. $ROAD_H = 13; $ROAD_V = 14; $ROAD_X = 19; $BRIDGE_H = 20; $BRIDGE_V = 21; $BRIDGE_X = 22
  39. $TILE_COUNT = 23
  40. $glyphs = '.gT#~~~~~~~~~-|+++++=!*'
  41. # Shore tile by [row part][column part]: row 0 = open to the north, 2 = open to the south.
  42. $shore = @(@($WATER_NW, $WATER_N, $WATER_NE), @($WATER_W, $WATER, $WATER_E), @($WATER_SW, $WATER_S, $WATER_SE))
  43. $NTSC_HZ = 1022727
  44. $maxCycles = 15 * $NTSC_HZ
  45. function Test-WaterFamily([int]$t) { $t -ge $WATER -and $t -le $WATER_SE }
  46. function Test-Bridge([int]$t) { $t -ge $BRIDGE_H -and $t -le $BRIDGE_X }
  47. # Class of a tile for the spread check: 0 land, 1 grass/trees, 2 water, 3 stone, -1 road/bridge.
  48. function Get-TileClass([int]$t) {
  49. if ($t -eq $LAND) { 0 } elseif ($t -eq $GRASS -or $t -eq $TREE) { 1 }
  50. elseif (Test-WaterFamily $t) { 2 } elseif ($t -eq $STONE) { 3 } else { -1 }
  51. }
  52. function Test-World([byte[]]$tiles, [string]$name) {
  53. $fail = [System.Collections.Generic.List[string]]::new()
  54. if ($tiles.Length -ne $width * $height) { $fail.Add("expected 4000 bytes, got $($tiles.Length)"); return @{ failures = $fail } }
  55. $at = { param($x, $y) $tiles[$y * $width + $x] }
  56. # Tile IDs and terrain types present.
  57. $counts = [int[]]::new(256)
  58. foreach ($t in $tiles) { $counts[$t]++ }
  59. for ($t = $TILE_COUNT; $t -lt 256; $t++) { if ($counts[$t]) { $fail.Add("unknown tile ID $t ($($counts[$t]) cells)") } }
  60. $waterCells = 0; for ($t = $WATER; $t -le $WATER_SE; $t++) { $waterCells += $counts[$t] }
  61. foreach ($p in @(@('land', $counts[$LAND]), @('grass', $counts[$GRASS]), @('trees', $counts[$TREE]), @('stone', $counts[$STONE]), @('water', $waterCells))) {
  62. if ($p[1] -eq 0) { $fail.Add("no $($p[0]) anywhere") }
  63. }
  64. # Roads.
  65. $hSet = @($ROAD_H, $BRIDGE_H, $ROAD_X, $BRIDGE_X); $vSet = @($ROAD_V, $BRIDGE_V, $ROAD_X, $BRIDGE_X)
  66. $roadRows = @(0..($height - 1) | Where-Object { $y = $_; @(0..($width - 1) | Where-Object { (& $at $_ $y) -notin $hSet }).Count -eq 0 })
  67. $roadCols = @(0..($width - 1) | Where-Object { $x = $_; @(0..($height - 1) | Where-Object { (& $at $x $_) -notin $vSet }).Count -eq 0 })
  68. if ($roadRows.Count -ne 2) { $fail.Add("expected 2 full east-west roads, found $($roadRows.Count)") }
  69. if ($roadCols.Count -ne 2) { $fail.Add("expected 2 full north-south roads, found $($roadCols.Count)") }
  70. for ($y = 0; $y -lt $height; $y++) {
  71. for ($x = 0; $x -lt $width; $x++) {
  72. $t = & $at $x $y
  73. $onH = $y -in $roadRows; $onV = $x -in $roadCols
  74. $isRoad = $t -ge $ROAD_H
  75. if ($onH -and $onV) { if ($t -notin @($ROAD_X, $BRIDGE_X)) { $fail.Add("crossing at ($x,$y) is tile $t") } }
  76. elseif (-not ($onH -or $onV) -and $isRoad) { $fail.Add("road tile $t off the road grid at ($x,$y)") }
  77. }
  78. }
  79. # Water: neighbours count as water if water family, bridge, or outside the world.
  80. $isWet = { param($x, $y)
  81. if ($x -lt 0 -or $y -lt 0 -or $x -ge $width -or $y -ge $height) { return $true }
  82. $t = & $at $x $y; (Test-WaterFamily $t) -or (Test-Bridge $t) }
  83. for ($y = 0; $y -lt $height; $y++) {
  84. for ($x = 0; $x -lt $width; $x++) {
  85. $t = & $at $x $y
  86. if (Test-WaterFamily $t) {
  87. $n = & $isWet $x ($y - 1); $s = & $isWet $x ($y + 1); $w = & $isWet ($x - 1) $y; $e = & $isWet ($x + 1) $y
  88. if (-not $n -and -not $s) { $fail.Add("water one cell thin (north-south) at ($x,$y)"); continue }
  89. if (-not $w -and -not $e) { $fail.Add("water one cell thin (west-east) at ($x,$y)"); continue }
  90. $r = if (-not $n) { 0 } elseif (-not $s) { 2 } else { 1 }
  91. $c = if (-not $w) { 0 } elseif (-not $e) { 2 } else { 1 }
  92. if ($t -ne $shore[$r][$c]) { $fail.Add("water at ($x,$y) is tile $t, its neighbours call for $($shore[$r][$c])") }
  93. } elseif ($t -eq $BRIDGE_H -and -not ((& $isWet $x ($y - 1)) -or (& $isWet $x ($y + 1)))) {
  94. $fail.Add("bridge at ($x,$y) has no water north or south of it")
  95. } elseif ($t -eq $BRIDGE_V -and -not ((& $isWet ($x - 1) $y) -or (& $isWet ($x + 1) $y))) {
  96. $fail.Add("bridge at ($x,$y) has no water west or east of it")
  97. }
  98. }
  99. }
  100. # Spread: prefix sums per class, then every 40x20 window.
  101. $sum = 0..3 | ForEach-Object { , ([int[]]::new(($width + 1) * ($height + 1))) }
  102. for ($y = 0; $y -lt $height; $y++) {
  103. for ($x = 0; $x -lt $width; $x++) {
  104. $k = Get-TileClass (& $at $x $y)
  105. for ($c = 0; $c -lt 4; $c++) {
  106. $v = if ($k -eq $c) { 1 } else { 0 }
  107. $i = ($y + 1) * ($width + 1) + ($x + 1)
  108. $sum[$c][$i] = $v + $sum[$c][$i - 1] + $sum[$c][$i - ($width + 1)] - $sum[$c][$i - ($width + 1) - 1]
  109. }
  110. }
  111. }
  112. $worst = 4; $worstAt = ''
  113. for ($y = 0; $y -le $height - 20; $y++) {
  114. for ($x = 0; $x -le $width - 40; $x++) {
  115. $present = 0
  116. for ($c = 0; $c -lt 4; $c++) {
  117. $a = $sum[$c]; $W = $width + 1
  118. $n = $a[($y + 20) * $W + $x + 40] - $a[$y * $W + $x + 40] - $a[($y + 20) * $W + $x] + $a[$y * $W + $x]
  119. if ($n -ge 8) { $present++ }
  120. }
  121. if ($present -lt $worst) { $worst = $present; $worstAt = "($x,$y)" }
  122. }
  123. }
  124. if ($worst -lt 3) { $fail.Add("the 40x20 view at $worstAt has only $worst terrain types with 8+ cells") }
  125. $total = 0; $share = 0..3 | ForEach-Object { $sum[$_][$height * ($width + 1) + $width] }
  126. foreach ($v in $share) { $total += $v }
  127. @{ failures = $fail; share = @($share | ForEach-Object { [math]::Round(100 * $_ / $total) }); worstView = $worst }
  128. }
  129. function Show-World([byte[]]$tiles) {
  130. for ($y = 0; $y -lt $height; $y++) {
  131. -join ($tiles[($y * $width)..($y * $width + $width - 1)] | ForEach-Object { if ($_ -lt $glyphs.Length) { $glyphs[$_] } else { '?' } })
  132. }
  133. }
  134. # ---------------------------------------------------------------- collect worlds
  135. $worlds = [System.Collections.Generic.List[hashtable]]::new()
  136. if ($Runs -le 0) {
  137. if (-not $Path) { $Path = Join-Path $PSScriptRoot '../build/smoke/c64/world.bin' }
  138. $worlds.Add(@{ name = 'smoke world'; tiles = [IO.File]::ReadAllBytes((Resolve-Path -LiteralPath $Path).Path); cycles = $null })
  139. } else {
  140. try {
  141. $project = Get-ProjectConfig
  142. $target = Get-ProjectTargets $project 'c64'
  143. if (-not $NoBuild) { Invoke-TargetBuild $project $target @{} }
  144. $emu = Get-ToolPath $project $target.emulator.binary
  145. } catch [MissingToolException] {
  146. Write-Host $_.Exception.Message -ForegroundColor Yellow
  147. exit 2
  148. }
  149. $labels = @{}
  150. foreach ($line in Get-Content -LiteralPath (Get-RepoPath $target.build.labels)) {
  151. $parts = $line -split '\s+'
  152. if ($parts.Count -ge 3) { $labels[$parts[2].TrimStart('.')] = [Convert]::ToInt32($parts[1], 16) }
  153. }
  154. $root = Get-RepoPath 'build/check-world'
  155. if (Test-Path -LiteralPath $root) { Remove-Item -LiteralPath $root -Recurse -Force }
  156. for ($run = 1; $run -le $Runs; $run++) {
  157. $dir = Join-Path $root "run$run"
  158. New-Item -ItemType Directory -Force -Path $dir | Out-Null
  159. $mon = @('logname "monitor.log"', 'log on',
  160. ('trace exec ${0:x4}' -f $labels.smoke_ready),
  161. ('command 1 "bsave \"world.bin\" 0 {0:x4} {1:x4}"' -f $labels.world_tiles, ($labels.world_tiles + $width * $height - 1)),
  162. ('trace exec ${0:x4}' -f $labels.generate_world))
  163. $n = 2
  164. # No key may reach the program: keys typed on the PC while VICE has focus would.
  165. foreach ($row in 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space') {
  166. $n++; $mon += ('trace exec ${0:x4}' -f $labels[$row]); $mon += ('command {0} "r a = ff"' -f $n)
  167. }
  168. Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $mon
  169. Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value ''
  170. $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $target)
  171. $vargs += @('-limitcycles', [string]($maxCycles + 2000000), '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target))
  172. $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } }
  173. $p = Start-Process -FilePath $emu -ArgumentList $quoted -WorkingDirectory $dir -PassThru `
  174. -RedirectStandardOutput (Join-Path $dir 'vice.log') -RedirectStandardError (Join-Path $dir 'vice.err')
  175. if (-not $p.WaitForExit($TimeoutSeconds * 1000)) { $p.Kill($true); Write-Host "[FAIL] run $run`: VICE did not exit in time." -ForegroundColor Red; exit 1 }
  176. $file = Join-Path $dir 'world.bin'
  177. 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 }
  178. # The trace's instruction line ends with the CPU clock.
  179. $clock = @{}
  180. foreach ($log in 'monitor.log', 'vice.log') {
  181. $lf = Join-Path $dir $log
  182. if (-not (Test-Path -LiteralPath $lf)) { continue }
  183. $lines = Get-Content -LiteralPath $lf
  184. for ($i = 0; $i -lt $lines.Count; $i++) {
  185. if ($lines[$i] -match '^#([12]) \(Trace\s+exec' -and -not $clock.ContainsKey($Matches[1])) {
  186. $id = $Matches[1]
  187. 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 } }
  188. }
  189. }
  190. if ($clock.Count -eq 2) { break }
  191. }
  192. $gen = if ($clock.ContainsKey('1') -and $clock.ContainsKey('2')) { $clock['1'] - $clock['2'] } else { $null }
  193. $worlds.Add(@{ name = "run $run"; tiles = [IO.File]::ReadAllBytes($file); cycles = $clock['1']; genCycles = $gen })
  194. }
  195. }
  196. # ---------------------------------------------------------------- check
  197. $failed = $false
  198. $shares = [System.Collections.Generic.List[int[]]]::new()
  199. foreach ($w in $worlds) {
  200. $r = Test-World $w.tiles $w.name
  201. $time = if ($null -ne $w.cycles) { ', ready {0:N1} s after power-on' -f ($w.cycles / $NTSC_HZ) } else { '' }
  202. if ($null -ne $w.genCycles) { $time += ' (generation {0:N1} s)' -f ($w.genCycles / $NTSC_HZ) }
  203. if ($null -ne $w.cycles -and $w.cycles -gt $maxCycles) { $r.failures.Add('generation took longer than 15 s (R1.1)') }
  204. $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 { '' }
  205. if ($r.failures.Count) {
  206. $failed = $true
  207. Write-Host "[FAIL] $($w.name): $mix$time" -ForegroundColor Red
  208. $r.failures | Select-Object -First 10 | ForEach-Object { Write-Host " $_" }
  209. if ($r.failures.Count -gt 10) { Write-Host " ... $($r.failures.Count - 10) more" }
  210. } else {
  211. Write-Host "[PASS] $($w.name): $mix; every 40x20 view has $($r.worstView)+ terrain types$time"
  212. $shares.Add([int[]]$r.share)
  213. }
  214. if ($ShowMap) { Show-World $w.tiles }
  215. }
  216. if ($worlds.Count -ge 5 -and -not $failed) {
  217. $names = 'land', 'grass/trees', 'water', 'stone'
  218. $spread = 0..3 | ForEach-Object { $c = $_; $v = $shares | ForEach-Object { $_[$c] }; ($v | Measure-Object -Maximum).Maximum - ($v | Measure-Object -Minimum).Minimum }
  219. $line = (0..3 | ForEach-Object { '{0} {1}' -f $names[$_], $spread[$_] }) -join ', '
  220. if (($spread | Where-Object { $_ -ge 10 }).Count -lt 2) {
  221. 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
  222. $failed = $true
  223. } else {
  224. Write-Host "[PASS] the terrain mix varies between worlds (percentage-point spread: $line)"
  225. }
  226. }
  227. if ($failed) { exit 1 }
  228. Write-Output "[PASS] $($worlds.Count) world(s) checked."

Powered by TurnKey Linux.