Files
Basys3Terminal/games/swarm/test-swarm.py
T
brad ArantandClaude Opus 5.5 e1daf27891 Star Swarm: a swooping alien shooter; boardemu frame counter fix
games/swarm: an original Galaxian-style game in C for the Z-80 - a
46-alien formation sways overhead and peels off in looping, firing dives,
flagships bring escorts. With more aliens than sprites, the formation is
drawn into the graphics and swayed by the hardware X scroll; a diver is
wiped out of the graphics and becomes a sprite until it flies back.
test-swarm.py has an autopilot destroy a whole swarm.

tools/boardemu.py: the frame counter now moves on once per wait, not on
every read (it had been running two frames per wait, so anything done on
odd frames never happened in tests). The other games' tests still pass.

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

127 lines
4.6 KiB
Python

#!/usr/bin/env python3
"""Run Star Swarm in tools/z80emu.py on a model of the board (tools/boardemu.py)
and take screenshots.
python3 test-swarm.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), checks that the formation sways
and that aliens dive, shoots one out of the formation, and with --play lets an
autopilot clear the whole swarm. 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
LEFT, RIGHT, FIRE, ESC = 4, 8, 16, 128
FX0, CW = 60, 20
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", "swarm.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", "swarm.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 s16(n, k=0):
v = b.m[sym[n] + 2 * k] | b.m[sym[n] + 2 * k + 1] << 8
return v - 65536 if v & 0x8000 else v
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 S W A R M" 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])
check("46 aliens in formation", sum(1 for k in range(60) if b.m[sym["_cell"] + k] == 1) == 46)
s0 = b.scroll_x
c0 = b.steps
worst = 0
for f in range(240):
c1 = b.steps
b.run_frames(1)
worst = max(worst, b.steps - c1)
per = (b.steps - c0) / 240
print("%.0f instructions a frame on average, %d at most (%.0f%%)" % (per, worst, worst / 5500))
check("every frame fits in a 60th of a second", worst < 550000)
check("the formation sways", b.scroll_x != s0)
check("aliens dive", any(u8("_dv_on", k) for k in range(8)))
if a.shots: b.shot(os.path.join(a.shots, "2-dive.png"), font)
sc = s16("_score")
b.m[sym["_shield"]] = 1
b.run_frames(2, RIGHT) # off the gap between two columns
b.run_frames(3, FIRE)
b.run_frames(40)
check("a shot into the formation scores", s16("_score") > sc)
if a.play:
frame = 0
while frame < 20000 and b.pc != 0:
b.m[sym["_shield"]] = 1
sx = s16("_ship_x") + 8
# aim at a diver low down if there is one, else the nearest column
target = None
for k in range(8):
if u8("_dv_on", k) and s16("_dv_y", k) >> 4 > 60:
target = (s16("_dv_x", k) >> 4) + 8
break
if target is None:
sway = (320 - b.scroll_x) % 320
if sway > 160: sway -= 320
best = None
for c in range(10):
if any(b.m[sym["_cell"] + r * 10 + c] == 1 for r in range(6)):
x = FX0 + c * CW + 2 + 8 + sway
if best is None or abs(x - sx) < abs(best - sx):
best = x
target = best if best is not None else sx
j = FIRE if frame % 2 else 0
if target > sx + 1: j |= RIGHT
elif target < sx - 1: j |= LEFT
b.run_frames(1, j)
frame += 1
if frame == 600 and a.shots: b.shot(os.path.join(a.shots, "3-battle.png"), font)
if "WAVE 2" in screen()[0]:
break
print(screen()[0])
check("autopilot cleared the swarm in %d frames" % frame, "WAVE 2" in screen()[0])
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()