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.

230 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. - no road or bridge tiles: roads are not generated (M5.1); paths are worn in later by
  14. the people (M5.2)
  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. # No roads or bridges at generation time.
  65. $roadCells = 0; for ($t = $ROAD_H; $t -lt $TILE_COUNT; $t++) { $roadCells += $counts[$t] }
  66. if ($roadCells -gt 0) { $fail.Add("$roadCells road or bridge cells: the generator must not make roads") }
  67. # Water: neighbours count as water if water family, bridge, or outside the world.
  68. $isWet = { param($x, $y)
  69. if ($x -lt 0 -or $y -lt 0 -or $x -ge $width -or $y -ge $height) { return $true }
  70. $t = & $at $x $y; (Test-WaterFamily $t) -or (Test-Bridge $t) }
  71. for ($y = 0; $y -lt $height; $y++) {
  72. for ($x = 0; $x -lt $width; $x++) {
  73. $t = & $at $x $y
  74. if (Test-WaterFamily $t) {
  75. $n = & $isWet $x ($y - 1); $s = & $isWet $x ($y + 1); $w = & $isWet ($x - 1) $y; $e = & $isWet ($x + 1) $y
  76. if (-not $n -and -not $s) { $fail.Add("water one cell thin (north-south) at ($x,$y)"); continue }
  77. if (-not $w -and -not $e) { $fail.Add("water one cell thin (west-east) at ($x,$y)"); continue }
  78. $r = if (-not $n) { 0 } elseif (-not $s) { 2 } else { 1 }
  79. $c = if (-not $w) { 0 } elseif (-not $e) { 2 } else { 1 }
  80. if ($t -ne $shore[$r][$c]) { $fail.Add("water at ($x,$y) is tile $t, its neighbours call for $($shore[$r][$c])") }
  81. } elseif ($t -eq $BRIDGE_H -and -not ((& $isWet $x ($y - 1)) -or (& $isWet $x ($y + 1)))) {
  82. $fail.Add("bridge at ($x,$y) has no water north or south of it")
  83. } elseif ($t -eq $BRIDGE_V -and -not ((& $isWet ($x - 1) $y) -or (& $isWet ($x + 1) $y))) {
  84. $fail.Add("bridge at ($x,$y) has no water west or east of it")
  85. }
  86. }
  87. }
  88. # Spread: prefix sums per class, then every 40x20 window.
  89. $sum = 0..3 | ForEach-Object { , ([int[]]::new(($width + 1) * ($height + 1))) }
  90. for ($y = 0; $y -lt $height; $y++) {
  91. for ($x = 0; $x -lt $width; $x++) {
  92. $k = Get-TileClass (& $at $x $y)
  93. for ($c = 0; $c -lt 4; $c++) {
  94. $v = if ($k -eq $c) { 1 } else { 0 }
  95. $i = ($y + 1) * ($width + 1) + ($x + 1)
  96. $sum[$c][$i] = $v + $sum[$c][$i - 1] + $sum[$c][$i - ($width + 1)] - $sum[$c][$i - ($width + 1) - 1]
  97. }
  98. }
  99. }
  100. $worst = 4; $worstAt = ''
  101. for ($y = 0; $y -le $height - 20; $y++) {
  102. for ($x = 0; $x -le $width - 40; $x++) {
  103. $present = 0
  104. for ($c = 0; $c -lt 4; $c++) {
  105. $a = $sum[$c]; $W = $width + 1
  106. $n = $a[($y + 20) * $W + $x + 40] - $a[$y * $W + $x + 40] - $a[($y + 20) * $W + $x] + $a[$y * $W + $x]
  107. if ($n -ge 8) { $present++ }
  108. }
  109. if ($present -lt $worst) { $worst = $present; $worstAt = "($x,$y)" }
  110. }
  111. }
  112. if ($worst -lt 3) { $fail.Add("the 40x20 view at $worstAt has only $worst terrain types with 8+ cells") }
  113. $total = 0; $share = 0..3 | ForEach-Object { $sum[$_][$height * ($width + 1) + $width] }
  114. foreach ($v in $share) { $total += $v }
  115. @{ failures = $fail; share = @($share | ForEach-Object { [math]::Round(100 * $_ / $total) }); worstView = $worst }
  116. }
  117. function Show-World([byte[]]$tiles) {
  118. for ($y = 0; $y -lt $height; $y++) {
  119. -join ($tiles[($y * $width)..($y * $width + $width - 1)] | ForEach-Object { if ($_ -lt $glyphs.Length) { $glyphs[$_] } else { '?' } })
  120. }
  121. }
  122. # ---------------------------------------------------------------- collect worlds
  123. $worlds = [System.Collections.Generic.List[hashtable]]::new()
  124. if ($Runs -le 0) {
  125. if (-not $Path) { $Path = Join-Path $PSScriptRoot '../build/smoke/c64/world.bin' }
  126. $worlds.Add(@{ name = 'smoke world'; tiles = [IO.File]::ReadAllBytes((Resolve-Path -LiteralPath $Path).Path); cycles = $null })
  127. } else {
  128. try {
  129. $project = Get-ProjectConfig
  130. $target = Get-ProjectTargets $project 'c64'
  131. if (-not $NoBuild) { Invoke-TargetBuild $project $target @{} }
  132. $emu = Get-ToolPath $project $target.emulator.binary
  133. } catch [MissingToolException] {
  134. Write-Host $_.Exception.Message -ForegroundColor Yellow
  135. exit 2
  136. }
  137. $labels = @{}
  138. foreach ($line in Get-Content -LiteralPath (Get-RepoPath $target.build.labels)) {
  139. $parts = $line -split '\s+'
  140. if ($parts.Count -ge 3) { $labels[$parts[2].TrimStart('.')] = [Convert]::ToInt32($parts[1], 16) }
  141. }
  142. $root = Get-RepoPath 'build/check-world'
  143. if (Test-Path -LiteralPath $root) { Remove-Item -LiteralPath $root -Recurse -Force }
  144. for ($run = 1; $run -le $Runs; $run++) {
  145. $dir = Join-Path $root "run$run"
  146. New-Item -ItemType Directory -Force -Path $dir | Out-Null
  147. $mon = @('logname "monitor.log"', 'log on',
  148. ('trace exec ${0:x4}' -f $labels.smoke_ready),
  149. ('command 1 "bsave \"world.bin\" 0 {0:x4} {1:x4}"' -f $labels.world_tiles, ($labels.world_tiles + $width * $height - 1)),
  150. ('trace exec ${0:x4}' -f $labels.generate_world))
  151. $n = 2
  152. # No key may reach the program: keys typed on the PC while VICE has focus would.
  153. foreach ($row in 'key_row_cursor', 'key_row_lshift', 'key_row_rshift', 'key_row_space') {
  154. $n++; $mon += ('trace exec ${0:x4}' -f $labels[$row]); $mon += ('command {0} "r a = ff"' -f $n)
  155. }
  156. Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $mon
  157. Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value ''
  158. $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $target)
  159. $vargs += @('-limitcycles', [string]($maxCycles + 2000000), '-moncommands', 'monitor.txt', '-autostartprgmode', '1', '-autostart', (Get-AutostartFile $target))
  160. $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + $_ + '"' } else { $_ } }
  161. $p = Start-Process -FilePath $emu -ArgumentList $quoted -WorkingDirectory $dir -PassThru `
  162. -RedirectStandardOutput (Join-Path $dir 'vice.log') -RedirectStandardError (Join-Path $dir 'vice.err')
  163. if (-not $p.WaitForExit($TimeoutSeconds * 1000)) { $p.Kill($true); Write-Host "[FAIL] run $run`: VICE did not exit in time." -ForegroundColor Red; exit 1 }
  164. $file = Join-Path $dir 'world.bin'
  165. 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 }
  166. # The trace's instruction line ends with the CPU clock.
  167. $clock = @{}
  168. foreach ($log in 'monitor.log', 'vice.log') {
  169. $lf = Join-Path $dir $log
  170. if (-not (Test-Path -LiteralPath $lf)) { continue }
  171. $lines = Get-Content -LiteralPath $lf
  172. for ($i = 0; $i -lt $lines.Count; $i++) {
  173. if ($lines[$i] -match '^#([12]) \(Trace\s+exec' -and -not $clock.ContainsKey($Matches[1])) {
  174. $id = $Matches[1]
  175. 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 } }
  176. }
  177. }
  178. if ($clock.Count -eq 2) { break }
  179. }
  180. $gen = if ($clock.ContainsKey('1') -and $clock.ContainsKey('2')) { $clock['1'] - $clock['2'] } else { $null }
  181. $worlds.Add(@{ name = "run $run"; tiles = [IO.File]::ReadAllBytes($file); cycles = $clock['1']; genCycles = $gen })
  182. }
  183. }
  184. # ---------------------------------------------------------------- check
  185. $failed = $false
  186. $shares = [System.Collections.Generic.List[int[]]]::new()
  187. foreach ($w in $worlds) {
  188. $r = Test-World $w.tiles $w.name
  189. $time = if ($null -ne $w.cycles) { ', ready {0:N1} s after power-on' -f ($w.cycles / $NTSC_HZ) } else { '' }
  190. if ($null -ne $w.genCycles) { $time += ' (generation {0:N1} s)' -f ($w.genCycles / $NTSC_HZ) }
  191. if ($null -ne $w.cycles -and $w.cycles -gt $maxCycles) { $r.failures.Add('generation took longer than 15 s (R1.1)') }
  192. $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 { '' }
  193. if ($r.failures.Count) {
  194. $failed = $true
  195. Write-Host "[FAIL] $($w.name): $mix$time" -ForegroundColor Red
  196. $r.failures | Select-Object -First 10 | ForEach-Object { Write-Host " $_" }
  197. if ($r.failures.Count -gt 10) { Write-Host " ... $($r.failures.Count - 10) more" }
  198. } else {
  199. Write-Host "[PASS] $($w.name): $mix; every 40x20 view has $($r.worstView)+ terrain types$time"
  200. $shares.Add([int[]]$r.share)
  201. }
  202. if ($ShowMap) { Show-World $w.tiles }
  203. }
  204. if ($worlds.Count -ge 5 -and -not $failed) {
  205. $names = 'land', 'grass/trees', 'water', 'stone'
  206. $spread = 0..3 | ForEach-Object { $c = $_; $v = $shares | ForEach-Object { $_[$c] }; ($v | Measure-Object -Maximum).Maximum - ($v | Measure-Object -Minimum).Minimum }
  207. $line = (0..3 | ForEach-Object { '{0} {1}' -f $names[$_], $spread[$_] }) -join ', '
  208. if (($spread | Where-Object { $_ -ge 10 }).Count -lt 2) {
  209. 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
  210. $failed = $true
  211. } else {
  212. Write-Host "[PASS] the terrain mix varies between worlds (percentage-point spread: $line)"
  213. }
  214. }
  215. if ($failed) { exit 1 }
  216. Write-Output "[PASS] $($worlds.Count) world(s) checked."

Powered by TurnKey Linux.