#!/usr/bin/env python3 """Run Bastion Raid in tools/z80emu.py on a model of the board (tools/boardemu.py) and take screenshots. python3 test-raid.py [--shots DIR] [--play] It checks the title screen, measures the Z-80 instructions each frame takes (a 60 Hz frame has about 550,000 at 90 MHz), flies into the fortress, and with --play lets an autopilot fly the whole level: it steers through every doorway at the right height, climbs over low walls and shoots ahead. The autopilot can't be hit by shells or fighters (it keeps the ship invulnerable), so this checks the level and the rules, not its skill. """ import argparse import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(HERE, "..", "..", "tools")) sys.path.insert(0, HERE) from boardemu import Board, load_ihx, load_font # noqa: E402 UP, DOWN, LEFT, RIGHT, FIRE, ENTER, ESC = 1, 2, 4, 8, 16, 32, 128 def main(): ap = argparse.ArgumentParser() ap.add_argument("--shots", help="write PNG screenshots here") ap.add_argument("--play", action="store_true", help="fly the level with the autopilot") a = ap.parse_args() mem, hi = load_ihx(os.path.join(HERE, "out", "raid.ihx")) b = Board(mem, code_end=hi) font = load_font() if a.shots else None if a.shots: os.makedirs(a.shots, exist_ok=True) sym = {} for line in open(os.path.join(HERE, "out", "raid.map")): f = line.split() if len(f) >= 2 and f[1].startswith("_"): try: sym[f[1]] = int(f[0], 16) except ValueError: pass errors = 0 def check(what, ok): nonlocal errors print(("PASS " if ok else "FAIL ") + what) errors += not ok def i16(n): v = b.m[sym[n]] | b.m[sym[n] + 1] << 8 return v - 65536 if v & 0x8000 else v def hud(): return "\n".join(b.screen_text().display[:2]) b.run_frames(5) check("title screen", "B A S T I O N R A I D" in "\n".join(b.screen_text().display)) if a.shots: b.shot(os.path.join(a.shots, "1-title.png"), font) b.run_frames(3, FIRE) b.run_frames(5) check("game starts", "LEVEL 1" in hud() and "SHIPS 3" in hud()) c0 = b.steps b.run_frames(60) per = (b.steps - c0) / 60 print("%.0f instructions a frame (%.0f%% of a 60 Hz frame at 90 MHz)" % (per, per / 5500)) check("a frame fits in a 60th of a second", per < 500000) if a.shots: b.shot(os.path.join(a.shots, "2-approach.png"), font) alt0 = b.m[sym["_alt"]] b.run_frames(10, UP) check("climbing raises the ship", b.m[sym["_alt"]] == alt0 + 10) b.run_frames(60) if a.shots: b.shot(os.path.join(a.shots, "3-fortress.png"), font) print(hud()) if a.play: sys.path.insert(0, HERE) import gen rows, objs = gen.level() frame = 0 best_row = 0 worst = 0 while frame < 6000 and b.pc != 0: b.m[sym["_shield"]] = 1 b.m[sym["_fuel"]] = 100 if b.m[sym["_dead"]]: r = -(i16("_ship_d") >> 5) print("autopilot crashed at row %d: ship s %d, height %d; rows here %s" % (r, i16("_ship_s"), b.m[sym["_alt"]], rows[r - 2:r + 3])) break ship_s, ship_d, alt = i16("_ship_s"), i16("_ship_d"), b.m[sym["_alt"]] row = -(ship_d >> 5) best_row = max(best_row, row) # the next wall within 6 rows decides where to be want_s, want_alt = 16 * gen.BW, 24 for r in range(row, min(row + 6, len(rows))): ri = rows[r] if ri & 4: g = (ri >> 3) & 15 want_s, want_alt = 32 * g + 32, 10 break if ri & 2: want_alt = 30 break j = 0 if ship_s < want_s - 2: j |= RIGHT elif ship_s > want_s + 2: j |= LEFT if alt < want_alt: j |= UP elif alt > want_alt: j |= DOWN if frame % 6 == 0: j |= FIRE c0 = b.steps b.run_frames(1, j) worst = max(worst, b.steps - c0) frame += 1 if frame == 700 and a.shots: b.shot(os.path.join(a.shots, "4-doorway.png"), font) if "LEVEL 2" in hud(): break check("autopilot flew the level (got to row %d of %d)" % (best_row, len(rows)), "LEVEL 2" in hud()) print("slowest frame: %d instructions (%.0f%% of a 60 Hz frame)" % (worst, worst / 5500)) check("every frame fits in a 60th of a second", worst < 550000) print(hud()) b.run_frames(120) # level 2 starts b.run_frames(5, ESC) # give up: back to the title b.run_frames(5) check("Esc gives up the game", "PRESS SPACE TO BEGIN" in "\n".join(b.screen_text().display)) b.run_frames(5, ESC) # Esc at the title: back to BASIC b.run_frames(5) check("Esc returns to BASIC", b.pc == 0) sys.exit(1 if errors else 0) if __name__ == "__main__": main()