Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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

Powered by TurnKey Linux.