Files
Basys3Terminal/games/warden/test-warden.py
T
brad ArantandClaude Opus 5.5 0b16e1dce0 Star Warden: a side-scrolling planet defence game
games/warden: an original Defender-style game in C for the Z-80 - landers
snatch humanoids from a planet that wraps round (1,280 pixels, exactly
four graphics widths, so world column x lives in buffer column x mod 320
and only columns scrolling into view are drawn), mutants, bombers and
mines, a laser, smart bombs, hyperspace and a long-range scanner in text.
test-warden.py checks scrolling and the scanner and has an autopilot
clear a wave; manual and README describe it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 20:01:06 -08:00

122 lines
4.5 KiB
Python

#!/usr/bin/env python3
"""Run Star Warden in tools/z80emu.py on a model of the board (tools/boardemu.py)
and take screenshots.
python3 test-warden.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 along the planet checking
that the picture scrolls and the scanner fills in, and with --play lets an
autopilot hunt down every enemy until the wave is cleared. The autopilot
can't be killed (it keeps the ship shielded), so this checks the rules and the
waves, 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"))
from boardemu import Board, load_ihx, load_font # noqa: E402
UP, DOWN, LEFT, RIGHT, FIRE, ENTER, SHIFT, ESC = 1, 2, 4, 8, 16, 32, 64, 128
WORLD = 1280
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--shots", help="write PNG screenshots here")
ap.add_argument("--play", action="store_true", help="let the autopilot clear a wave")
a = ap.parse_args()
mem, hi = load_ihx(os.path.join(HERE, "out", "warden.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", "warden.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 u16(n, k=0):
return b.m[sym[n] + 2 * k] | b.m[sym[n] + 2 * k + 1] << 8
def u8(n, k=0):
return b.m[sym[n] + k]
def screen():
return b.screen_text().display
b.run_frames(5)
check("title screen", "S T A R W A R D E N" in "\n".join(screen()))
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", "WAVE 1" in screen()[0] and "SHIPS 3" in screen()[0])
c0 = b.steps
b.run_frames(60, RIGHT)
per = (b.steps - c0) / 60
print("%.0f instructions a frame while flying (%.0f%% of a 60 Hz frame)" % (per, per / 5500))
check("a frame fits in a 60th of a second", per < 500000)
check("flying right scrolls the planet", b.scroll_x != 0)
check("the scanner shows enemies", sum(r.count("o") for r in screen()[1:4]) >= 4)
if a.shots: b.shot(os.path.join(a.shots, "2-flying.png"), font)
s0 = b.scroll_x
b.run_frames(80, LEFT)
check("turning round scrolls the other way", b.scroll_x != s0)
if a.shots: b.shot(os.path.join(a.shots, "3-turned.png"), font)
if a.play:
b.m[sym["_shield"]] = 1
worst = 0
frame = 0
while frame < 9000 and b.pc != 0:
b.m[sym["_shield"]] = 1
px, py = u16("_px"), u8("_py")
best, bd = None, 9999
for k in range(16):
if u8("_etype", k):
ex = u16("_ex", k) >> 4
d = (ex - px + WORLD // 2) % WORLD - WORLD // 2
if abs(d) < abs(bd):
bd, best = d, k
j = 0
if best is not None:
ey = u8("_ey", best)
if ey < py - 1: j |= UP
elif ey > py + 1: j |= DOWN
if abs(bd) > 90 or (bd > 0) != (b.spr[0][3] & 2 == 0):
j |= RIGHT if bd > 0 else LEFT
if abs(ey - py) < 6 and abs(bd) < 260:
j |= FIRE if frame % 2 else 0
c0 = b.steps
b.run_frames(1, j)
worst = max(worst, b.steps - c0)
frame += 1
if frame == 120 and a.shots: b.shot(os.path.join(a.shots, "4-battle.png"), font)
if "WAVE 2" in screen()[0]:
break
print(screen()[0])
check("autopilot cleared wave 1 in %d frames" % frame, "WAVE 2" in screen()[0])
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)
b.run_frames(5, ESC)
b.run_frames(5)
check("Esc gives up the game", "PRESS SPACE TO BEGIN" in "\n".join(screen()))
b.run_frames(5, ESC)
b.run_frames(5)
check("Esc returns to BASIC", b.pc == 0)
sys.exit(1 if errors else 0)
if __name__ == "__main__":
main()