The Iron Keep: a top-down escape game, held-key ports, monitor HEX loader

games/keep: an original game in C for the Z-80 using the sprites - twelve
rooms, guards that patrol, see along rows and columns and shoot, keys and
locked doors, gold, ammunition and a crown to steal. gen.py holds the art
and castle map and checks reachability; test-keep.py runs it in z80emu on
a model of the board's ports, takes screenshots and has a bot play it
through. keep.bas loads KEEPGAME at 8600h and CALLs it.

Ports 88h/89h read keys held down (ps2_keystate), the monitor writes
Intel HEX records typed at its prompt, and tools/send-hex.py sends a
whole file and saves it as a memory file. Testbench covers both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-29 09:21:02 -08:00
1 parent 13373244e8
commit 743071cff3
19 files changed
+3544 -1306

No files matched your search

+454
View File
@@ -0,0 +1,454 @@
#!/usr/bin/env python3
"""Turn the art and the castle map below into data.h for keep.c.
Shapes and tiles are 16 rows of 16 hex digits, one colour per pixel (the
terminal's 16 colours; 0 is see-through). The castle is 4 x 3 rooms; each room
is 18 x 12 tiles inside a wall border that gen.py adds, with gaps where two
rooms connect. Map characters:
# wall D locked door (a key opens it) X stairs out
. floor P where the player starts
K key G gold chest A ammunition C the crown
h guard patrolling left-right v guard patrolling up-down
c guard that chases you once it has seen you
The script checks that every room's objects fit the sprite budget and that
everything can be reached from the start, counting a key for every door.
"""
import sys
from collections import deque
# ------------------------------------------------------------------ shapes
PLAYER_DOWN_TOP = [
"0000033333300000",
"0000333333330000",
"0000399999930000",
"0000390990930000",
"0000099999900000",
"0000000990000000",
]
PLAYER_UP_TOP = [
"0000033333300000",
"0000333333330000",
"0000333333330000",
"0000333333330000",
"0000033333300000",
"0000000990000000",
]
BODY_FRONT = [
"000CCCCCCCCCC000",
"00CCCCCCCCCCCC00",
"009CCCCCCCCCC900",
"009CCCCCCCCCC900",
"0000CCCCCCCC0000",
"0000444444440000",
]
LEGS_FRONT_A = [
"0000444004440000",
"0000444004440000",
"0000777007770000",
"0000000000000000",
]
LEGS_FRONT_B = [
"0000044004400000",
"0000044004400000",
"0000077007700000",
"0000000000000000",
]
PLAYER_SIDE_TOP = [
"0000033333300000",
"0000333333330000",
"0000333399990000",
"0000333990990000",
"0000033999990000",
"0000000999000000",
"00000CCCCCC00000",
"0000CCCCCCCC0000",
"0000CCCCC9990000",
"0000CCCCCC000000",
"00000CCCCC000000",
"0000044444400000",
]
LEGS_SIDE_A = [
"0000044004400000",
"0000444000440000",
"0000777000770000",
"0000000000000000",
]
LEGS_SIDE_B = [
"0000004444000000",
"0000004444000000",
"0000007777000000",
"0000000000000000",
]
PLAYER = [
PLAYER_DOWN_TOP + BODY_FRONT + LEGS_FRONT_A,
PLAYER_DOWN_TOP + BODY_FRONT + LEGS_FRONT_B,
PLAYER_UP_TOP + BODY_FRONT + LEGS_FRONT_A,
PLAYER_UP_TOP + BODY_FRONT + LEGS_FRONT_B,
PLAYER_SIDE_TOP + LEGS_SIDE_A,
PLAYER_SIDE_TOP + LEGS_SIDE_B,
]
# guards: grey helmet, green tunic, dark grey trousers, brown boots
GUARD = [[r.translate(str.maketrans("3C47", "7283")) for r in s] for s in PLAYER]
def dot(c):
rows = ["0" * 16] * 16
for y in range(6, 10):
rows[y] = "000000" + c * 4 + "000000"
return rows
KEY = ["0" * 16] * 3 + [
"000BBB0000000000",
"00B000B000000000",
"00B000BBBBBBBBB0",
"00B000B0000B0B00",
"000BBB00000B0B00",
] + ["0" * 16] * 8
CHEST = ["0" * 16] * 3 + [
"0003333333333000",
"0033333333333300",
"003BBBBBBBBBB300",
"0033333BB3333300",
"0033333BB3333300",
"0033333333333300",
"0033333333333300",
"003BBBBBBBBBB300",
"0033333333333300",
] + ["0" * 16] * 4
AMMO = ["0" * 16] * 3 + [
"0022222222222200",
"0028888888888200",
"0028BB8BB8BB8200",
"0028BB8BB8BB8200",
"0028BB8BB8BB8200",
"0028888888888200",
"0022222222222200",
] + ["0" * 16] * 6
CROWN = ["0" * 16] * 3 + [
"000B000BB000B000",
"000BB0BBBB0BB000",
"000BBBBBBBBBB000",
"000BB9BBBB9BB000",
"000BBBBCCBBBB000",
"000BBBBBBBBBB000",
"0000000000000000",
"000BBBBBBBBBB000",
] + ["0" * 16] * 5
PUFF = [
"0000000000000000",
"000F00000000F000",
"0000F00BB00F0000",
"00000F0BB0F00000",
"000000BFFB000000",
"0BBB0BFFFFB0BBB0",
"000000BFFB000000",
"00000F0BB0F00000",
"0000F00BB00F0000",
"000F00000000F000",
] + ["0" * 16] * 6
SHAPES = PLAYER + GUARD + [dot("B"), dot("9"), KEY, CHEST, AMMO, CROWN, PUFF]
SHAPE_NAMES = ["S_PDOWN", "S_PDOWN2", "S_PUP", "S_PUP2", "S_PSIDE", "S_PSIDE2",
"S_GDOWN", "S_GDOWN2", "S_GUP", "S_GUP2", "S_GSIDE", "S_GSIDE2",
"S_SHOT", "S_GSHOT", "S_KEY", "S_CHEST", "S_AMMO", "S_CROWN", "S_PUFF"]
# ------------------------------------------------------------------- tiles
WALL = (["8888888888888888", "7777777877777778", "7777777877777778", "7777777877777778",
"8888888888888888", "7778777777787777", "7778777777787777", "7778777777787777"] * 2)
DOOR = (["8888888888888888"] + ["8333833383338338"] * 6 +
["8333833BB3338338"] * 2 + ["8333833383338338"] * 6 + ["8888888888888888"])
STAIRS = ["EEEEEEEEEEEEEEEE", "6666666666666666", "8888888888888888", "0000000000000000"] * 4
TILES = [["0" * 16] * 16, WALL, DOOR, STAIRS] # floor, wall, door, stairs
# -------------------------------------------------------------------- map
ROOMS = [[None] * 4 for _ in range(3)]
NAMES = [[None] * 4 for _ in range(3)]
def room(r, c, name, rows):
ROOMS[r][c] = rows
NAMES[r][c] = name
room(0, 0, "GATEHOUSE", [
"..................",
"..P...............",
"..................",
"....####....####..",
"....#..........#..",
"....#...A......#..",
"....#..........#..",
"....####....####..",
"..................",
"..................",
"..................",
"..................",
])
room(0, 1, "GUARD ROOM", [
"..................",
".h................",
"..................",
"..###.......###...",
"..#...........#...",
"..#.....K.....#...",
"..#...........#...",
"..###.......###...",
"..................",
"...........h......",
"..................",
"..................",
])
room(0, 2, "CHAPEL", [
"..................",
".v..........v.....",
"..................",
"......######......",
"......#G.G#.......",
"......#...D.......",
"......#####.......",
"..................",
"..................",
"....h.............",
"..................",
"..................",
])
room(0, 3, "ARMOURY", [
"..................",
"..A............A..",
"..................",
".####.......####..",
"..................",
"........h.........",
"..................",
".####.......####..",
"..................",
"..................",
".......c..........",
"..................",
])
room(1, 0, "CORRIDOR", [
"..................",
".v.......####.....",
".........#........",
".........#....h...",
"#######..#........",
"..................",
"..................",
"#######..########.",
".........#........",
"..A......#........",
".........#....v...",
"..................",
])
room(1, 1, "GREAT HALL", [
"..................",
"..G............G..",
"..................",
"....##......##....",
"....##..h...##....",
"..................",
"..................",
"....##......##....",
"....##...h..##....",
"..................",
".c...........v....",
"..................",
])
room(1, 2, "CELLARS", [
"...#.......#......",
"...#.#####.#.####.",
"...#.#.....#....#.",
".....#.#######..#.",
"######.#.....#..#.",
".......#..A..#....",
".#######.....###..",
".#.......###......",
".#.#######.#.####.",
".#.........#.#....",
".#####.#####.#.h..",
".......#.......#..",
])
room(1, 3, "LIBRARY", [
"..................",
".##.##.##.##.##.#.",
"..................",
".##.##.##.##.##.#.",
"..........h.......",
"..................",
".##.##.##.##.##.#.",
"..................",
".##.##.##.##.##.#.",
"........K.........",
"...v..............",
"..................",
])
room(2, 0, "STAIRWELL", [
"..................",
"..................",
"..######..######..",
"..#............#..",
"..#.....XX.....#..",
"..#.....XX.....#..",
"..#............#..",
"..######..######..",
"..................",
"....h.............",
"..................",
"..................",
])
room(2, 1, "DUNGEON", [
".#..#..#..#..#..#.",
".#..#..#..#..#..#.",
".#..#..#..#..#..#.",
".##.####..####.##.",
"..................",
"........v.........",
"..................",
".##.####..####.##.",
".#..#..#..#..#..#.",
".#K.#..#..#.G#..#.",
".#..#..#..#..#..#.",
"..................",
])
room(2, 2, "TREASURY", [
"..................",
"..h...............",
"..................",
".....##########...",
".....#G..G...G#...",
".....#........#...",
".....######D###...",
"..................",
"..................",
".........v........",
"..................",
"..................",
])
room(2, 3, "THRONE ROOM", [
"#######D##########",
"..................",
".c..............c.",
"..................",
"...####....####...",
"...#..........#...",
"...#....C.....#...",
"...#..........#...",
"...####....####...",
"..................",
".v..............v.",
"..................",
])
# links: (room, room) side by side or one above the other
H_LINKS = {(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1)} # (r, c) to (r, c+1)
V_LINKS = {(0, 0), (1, 0), (0, 3), (1, 3), (1, 1)} # (r, c) to (r+1, c)
W, H = 20, 14
GAP_ROWS = (6, 7) # side gaps, in room rows
GAP_COLS = (8, 9) # top/bottom gaps, in room columns
OBJ = "PKGAChvc"
def full_room(r, c):
inner = ROOMS[r][c]
assert len(inner) == 12 and all(len(x) == 18 for x in inner), NAMES[r][c]
g = [["#"] * W for _ in range(H)]
for y in range(12):
for x in range(18):
g[y + 1][x + 1] = inner[y][x]
if (r, c) in H_LINKS:
for y in GAP_ROWS: g[y][W - 1] = "."
if (r, c - 1) in H_LINKS:
for y in GAP_ROWS: g[y][0] = "."
if (r, c) in V_LINKS:
for x in GAP_COLS: g[H - 1][x] = "."
if (r - 1, c) in V_LINKS:
for x in GAP_COLS: g[0][x] = "."
return g
FULL = [[full_room(r, c) for c in range(4)] for r in range(3)]
# one big grid to check reachability across rooms
BIG = [[FULL[gy // H][gx // W][gy % H][gx % W] for gx in range(4 * W)] for gy in range(3 * H)]
def check():
start = [(x, y) for y in range(3 * H) for x in range(4 * W) if BIG[y][x] == "P"]
assert len(start) == 1
doors = sum(row.count("D") for row in BIG)
keys = sum(row.count("K") for row in BIG)
assert keys >= doors, f"{keys} keys for {doors} doors"
# with every door open, every non-wall tile must be reachable
seen = {start[0]}
q = deque(seen)
while q:
x, y = q.popleft()
for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nx, ny = x + dx, y + dy
if 0 <= nx < 4 * W and 0 <= ny < 3 * H and (nx, ny) not in seen and BIG[ny][nx] != "#":
seen.add((nx, ny)); q.append((nx, ny))
lost = [(x, y, BIG[y][x]) for y in range(3 * H) for x in range(4 * W)
if BIG[y][x] != "#" and (x, y) not in seen]
objs_lost = [t for t in lost if t[2] != "."]
assert not objs_lost, f"unreachable objects: {objs_lost}"
# rooms' sprite budgets: 6 guards, 8 items
for r in range(3):
for c in range(4):
s = "".join("".join(row) for row in FULL[r][c])
assert sum(s.count(ch) for ch in "hvc") <= 6, NAMES[r][c]
assert sum(s.count(ch) for ch in "KGAC") <= 8, NAMES[r][c]
assert s.count("D") <= 8, NAMES[r][c]
return len(lost)
def nib(c):
return int(c, 16)
def main(out):
unreach = check()
L = ["/* made by gen.py - edit that instead */", ""]
L.append("#define N_SHAPES %d" % len(SHAPES))
for i, n in enumerate(SHAPE_NAMES):
L.append("#define %s %d" % (n, i))
for i, s in enumerate(SHAPES):
assert len(s) == 16 and all(len(row) == 16 for row in s), SHAPE_NAMES[i]
L.append("static const u8 shape_data[N_SHAPES * 128] = {")
for s in SHAPES:
b = [nib(row[x]) << 4 | nib(row[x + 1]) for row in s for x in range(0, 16, 2)]
L.append(" " + ",".join(str(v) for v in b) + ",")
L.append("};")
L.append("static const u8 tile_data[4 * 256] = {")
for t in TILES:
assert len(t) == 16 and all(len(row) == 16 for row in t)
L.append(" " + ",".join(str(nib(ch)) for row in t for ch in row) + ",")
L.append("};")
# rooms: tiles packed two per byte (low nibble first), then object lists
code = {".": 0, "#": 1, "D": 2, "X": 3}
L.append("static const u8 room_tiles[12][140] = {")
objs = []
for r in range(3):
for c in range(4):
g = FULL[r][c]
t = [code.get(g[y][x], 0) for y in range(H) for x in range(W)]
L.append(" {" + ",".join(str(t[i] | t[i + 1] << 4) for i in range(0, W * H, 2)) + "},")
ol = [(g[y][x], x, y) for y in range(H) for x in range(W) if g[y][x] in OBJ]
objs.append(ol)
L.append("};")
L.append("/* objects: type, tile x, tile y; ends with 0 */")
L.append("static const u8 room_objs[12][3 * 16 + 1] = {")
for ol in objs:
assert len(ol) <= 16
L.append(" {" + ",".join("'%s',%d,%d" % o for o in ol) + (",0}," if ol else "0},"))
L.append("};")
L.append("static const char *const room_names[12] = {")
L.append(" " + ", ".join('"%s"' % NAMES[r][c] for r in range(3) for c in range(4)))
L.append("};")
open(out, "w").write("\n".join(L) + "\n")
print("data.h: %d shapes, 12 rooms (%d floor tiles only reachable through nothing)" %
(len(SHAPES), unreach))
if __name__ == "__main__":
main(sys.argv[1] if len(sys.argv) > 1 else "data.h")