Crystal Cavern: a maze chase game

games/cavern: an original maze-chase game in C for the Z-80 - a miner
gathers crystals while four bats hunt him, each choosing its target its own
way and switching between chasing and scattering to its corner; lanterns
scare them and their eyes fly home to the roost; nuggets and wrap-round
tunnels. gen.py builds the maze from a grid of junctions, removing links at
random while keeping it connected, mirrored and free of dead ends. The maze
is drawn into the graphics in 8 x 8 tiles; figures are sprites.
test-cavern.py has an autopilot gather every crystal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-29 21:07:33 -08:00
1 parent e1daf27891
commit 073db68754
11 files changed
+1447

No files matched your search

+1
View File
@@ -12,3 +12,4 @@ games/keep/out/
games/raid/out/
games/warden/out/
games/swarm/out/
games/cavern/out/
+10
View File
@@ -282,6 +282,15 @@ wiped out of the graphics and becomes a sprite until it flies back into its
place. Install it as `SWARMGAM` with its loader saved as `SWARM`;
`test-swarm.py --play` has an autopilot destroy a whole swarm.
**Crystal Cavern** (`games/cavern/`) is a maze chase: a miner gathers
crystals while four bats hunt him, each with its own way of choosing where to
go, switching between chasing and going to their corners; lanterns turn the
tables. `RUN "CAVERN"`. The maze is drawn into the graphics in 8 x 8 tiles;
`gen.py` builds it from a grid of junctions, removing links at random while
keeping it connected, mirrored and free of dead ends. Install it as `CAVEGAME`
with its loader saved as `CAVERN`; `test-cavern.py --play` has an autopilot
gather every crystal.
`tools/boardemu.py` models the board's game hardware (graphics, scrolling,
sprites, held keys, terminal) around `tools/z80emu.py` for both tests.
@@ -577,6 +586,7 @@ which Vivado needs on newer distros.
| `games/raid/` | Bastion Raid: game (C), art and level generator, loader, emulator test with autopilot |
| `games/warden/` | Star Warden: game (C), art and mountain generator, loader, emulator test with autopilot |
| `games/swarm/` | Star Swarm: game (C), art generator, loader, emulator test with autopilot |
| `games/cavern/` | Crystal Cavern: game (C), maze and art generator, loader, emulator test with autopilot |
| `tools/boardemu.py` | Model of the board's game hardware around `z80emu.py`, with screenshots |
| `basic/` | BASIC interpreter (C), start-up code, build script, `basic.mem` |
| `tools/z80emu.py`, `tools/cputest.py`, `scripts/cpusim.sh` | CPU test bench: reference emulator, random tests |
+29
View File
@@ -1228,6 +1228,35 @@ the 32 sprites, so the formation is drawn into the graphics, and its sway is
the hardware scroll; a diving alien is wiped from the graphics and flies as a
sprite until it is back in its place (`games/swarm/` on the PC).
### Crystal Cavern
A maze chase. You are a miner gathering the crystals in a cave, chased by four
bats; gather every crystal to reach the next, faster cave.
```
RUN "CAVERN"
```
- **The cursor keys** or **W A S D** steer the miner. Hold a direction before
a junction and it turns there; it can turn back at any time.
- **The bats** each hunt their own way: the red one comes straight for you,
the magenta one heads you off, the green one works with the red, and the
orange one keeps its distance. Now and then they all break off and go to
their corners for a while.
- **Lanterns** (the four yellow lights) scare the bats blue for a few seconds:
catch them then, and their eyes fly back to the roost in the middle. They
flash white just before they recover.
- **The side tunnels** wrap round to the other side; bats are slow in them.
- **Score:** 10 a crystal, 50 a lantern, 200, 400, 800 and 1,600 for bats
caught on one lantern, and a gold nugget (500 a cave) that appears twice a
cave where you started. An extra life every 10,000.
- **P** pauses; **Esc** gives up, and Esc at the title goes back to BASIC.
`CAVERN` loads the memory file `CAVEGAME` at 8600h. The maze was made by
`gen.py`, which joins up a grid of junctions and takes links away at random
while keeping the cave connected, mirrored and free of dead ends
(`games/cavern/` on the PC).
## 15. Limits at a glance
| Item | Limit |
Binary file not shown.
+25
View File
@@ -0,0 +1,25 @@
#!/bin/bash
# Build Crystal Cavern: cavern.ihx, Intel HEX for tools/send-hex.py.
# The program runs at 8600h with its variables at D000h, inside BASIC's
# program area (8482h-D6D5h) and clear of the CAVERN loader program at its start.
set -e
cd "$(dirname "$0")"
mkdir -p out
python3 gen.py out/data.h
sdasz80 -plosgff out/crt0.rel crt0.s
sdcc -mz80 --opt-code-size --max-allocs-per-node 20000 --std-sdcc11 -Iout -c -o out/cavern.rel cavern.c
sdcc -mz80 --no-std-crt0 --code-loc 0x8620 --data-loc 0xD000 -o out/cavern.ihx out/crt0.rel out/cavern.rel
python3 - <<'EOF'
import re
m = open("out/cavern.map").read()
def sym(n):
return int(re.search(r"([0-9A-F]{8})\s+" + n + r"\b", m).group(1), 16)
code_end = sym("s__CODE") + sym("l__CODE")
data_end = sym("s__DATA") + sym("l__DATA")
init = sym("l__INITIALIZED") + sym("l__INITIALIZER")
print("code 8600-%04X (%d bytes), variables D000-%04X" % (code_end - 1, code_end - 0x8600, data_end - 1))
assert code_end <= 0xD000, "code runs into the variables"
assert data_end <= 0xD6D6, "variables run past BASIC's program area"
assert init == 0, "initialised variables are not supported"
EOF
cp out/cavern.ihx cavern.ihx
+4
View File
@@ -0,0 +1,4 @@
10 REM CRYSTAL CAVERN: LOAD THE GAME (MEMORY FILE CAVEGAME) AND PLAY IT
20 REM THE GAME RUNS AT 8600H, PAST THIS PROGRAM, AND COMES BACK HERE
30 BLOAD "CAVEGAME": CALL 34304
40 CLS
+648
View File
@@ -0,0 +1,648 @@
/* Crystal Cavern: a maze chase game for the Basys 3 Z-80.
You are a miner gathering crystals in a cave, chased by four bats. Each
hunts its own way: the red one comes straight for you, the magenta one
heads you off, the green one works with the red, and the orange one keeps
its distance. A lantern scares them for a while: catch them then and their
eyes fly back to the roost. Gold nuggets turn up for bonus points, and the
side tunnels wrap round. Gather every crystal to reach the next cave.
The maze is drawn into the graphics in 8 x 8 tiles; the miner, the bats and
the nugget are sprites. It runs at 8600h, loaded by the CAVERN program in
BASIC. */
typedef unsigned char u8;
typedef signed char i8;
typedef unsigned int u16;
typedef int i16;
__sfr __at 0x00 con_stat;
__sfr __at 0x01 con_data;
__sfr __at 0x30 gx_lo;
__sfr __at 0x31 gx_hi;
__sfr __at 0x32 gy_p;
__sfr __at 0x33 gpix;
__sfr __at 0x34 gfill;
__sfr __at 0x70 sp_sel;
__sfr __at 0x71 sp_xlo;
__sfr __at 0x72 sp_xhi;
__sfr __at 0x73 sp_y;
__sfr __at 0x74 sp_shape;
__sfr __at 0x75 sp_ctl;
__sfr __at 0x76 sp_load;
__sfr __at 0x77 sp_data;
__sfr __at 0x78 frame_ctr;
__sfr __at 0x88 joy;
#include "data.h" /* out/data.h, made by gen.py */
#define J_UP 1
#define J_DOWN 2
#define J_LEFT 4
#define J_RIGHT 8
#define J_FIRE 16
#define J_ENTER 32
#define J_ESC 128
#define MX0 48 /* the maze's top left on the screen */
#define MY0 8
#define PW (MW * 8) /* maze width in pixels */
#define T_OPEN 0
#define T_WALL 1
#define T_CRYS 2
#define T_LAMP 3
#define T_DOOR 4
#define T_ROOST 5
#define T_TUNNEL 6
#define D_RIGHT 0
#define D_DOWN 1
#define D_LEFT 2
#define D_UP 3
#define D_NONE 4
static const i8 ddx[5] = { 1, 0, -1, 0, 0 };
static const i8 ddy[5] = { 0, 1, 0, -1, 0 };
/* bats */
#define NB 4
#define B_ROOST 0
#define B_LEAVE 1
#define B_HUNT 2
#define B_EYES 3
#define B_HOME 4 /* eyes back in the roost, going down */
#define DOOR_X 108 /* column 13's centre: the way in and out of the roost */
#define OUT_Y 76 /* row 9's centre, just above the door */
#define ROOM_Y 108 /* the roost's middle */
#define SP_MINER 0
#define SP_BAT 1 /* 4 */
#define SP_NUGGET 5
#define SP_BOOM 6
static const u8 bat_shape[NB] = { S_BAT_R, S_BAT_M, S_BAT_G, S_BAT_O };
static const i8 corner_x[NB] = { 26, 1, 26, 1 };
static const i8 corner_y[NB] = { -2, -2, 30, 30 };
static const u8 roost_x[NB] = { DOOR_X, DOOR_X, 92, 124 };
/* scatter and chase, in seconds: then chase for good */
static const u8 mode_secs[6] = { 7, 20, 7, 20, 5, 255 };
/* ---------------------------------------------------------------- state */
/* not static: test-cavern.py reads these from the link map */
u8 maze[MH][MW];
i16 mx, my; /* miner, maze pixels (tile centre = 8t + 4) */
u8 lives, level, dead, shield, left;
u16 score;
u8 bstate[NB];
i16 bx[NB], by[NB];
static u8 mdir, mwant, macc, manim;
static u8 bdir[NB], bacc[NB], bwait[NB];
static u8 fright, fright_flash, combo;
static u16 fright_time, mode_time;
static u8 mode, mode_i, eaten;
static u16 nugget_time;
static u8 nugget_on, boom_time, dead_time, lamp_blink;
static u16 next_extra;
static u8 last_frame, quit, hud_dirty;
static u16 rnd_state;
static const char *msg;
static u8 msg_time;
/* ----------------------------------------------------------------- text */
static void putch(char c) { while (!(con_stat & 2)); con_data = c; }
static void puts_(const char *s) { while (*s) putch(*s++); }
static void putn(u16 n, u8 width)
{
char b[6];
u8 i = 0;
do { b[i++] = '0' + n % 10; n /= 10; } while (n);
while (width > i) { putch('0'); width--; }
while (i) putch(b[--i]);
}
static void at(u8 r, u8 c) { putch(27); putch('['); putn(r, 0); putch(';'); putn(c, 0); putch('H'); }
static void ink(const char *s) { putch(27); putch('['); puts_(s); putch('m'); }
static void centre(u8 r, const char *s)
{
u8 n = 0;
while (s[n]) n++;
at(r, 41 - n / 2);
puts_(s);
}
static u16 rnd(void)
{
rnd_state ^= rnd_state << 7;
rnd_state ^= rnd_state >> 9;
rnd_state ^= rnd_state << 8;
return rnd_state;
}
static void wait_frame(void)
{
while (frame_ctr == last_frame);
last_frame = frame_ctr;
}
static u8 drain_keys(void)
{
u8 c, p = 0;
while (con_stat & 1) {
c = con_data;
if (c == 'p' || c == 'P') p = 1;
}
return p;
}
static void say(const char *s) { msg = s; msg_time = 120; hud_dirty = 1; }
static i16 absd(i16 v) { return v < 0 ? -v : v; }
/* ------------------------------------------------------------ the maze */
static u8 tile(i16 tx, i16 ty)
{
if (ty < 0 || ty >= MH) return T_WALL;
if (tx < 0) tx += MW;
if (tx >= MW) tx -= MW;
return maze[ty][tx];
}
static u8 is_open(u8 t) { return t == T_OPEN || t == T_CRYS || t == T_LAMP || t == T_TUNNEL; }
static void plot_row(u16 x, u8 y, const u8 *c)
{
u8 i;
gx_lo = (u8)x; gx_hi = (u8)(x >> 8); gy_p = y;
for (i = 0; i < 8; i++) gpix = c[i];
}
static u8 wall_ink;
/* Draw tile (tx, ty): walls are outlined where they meet a passage. */
static void draw_tile(u8 tx, u8 ty)
{
u8 t = maze[ty][tx], r, c, row[8], up, dn, lf, rt;
u16 x = MX0 + tx * 8;
u8 y = MY0 + ty * 8;
for (r = 0; r < 8; r++) {
for (c = 0; c < 8; c++) row[c] = 0;
if (t == T_WALL) {
up = !(tile(tx, ty - 1) == T_WALL);
dn = !(tile(tx, ty + 1) == T_WALL);
lf = !(tile(tx - 1, ty) == T_WALL) && tx > 0;
rt = !(tile(tx + 1, ty) == T_WALL) && tx < MW - 1;
for (c = 0; c < 8; c++)
if ((r == 1 && up) || (r == 6 && dn) || (c == 1 && lf) || (c == 6 && rt))
row[c] = wall_ink;
/* tidy the corners where two outlines meet */
if ((r < 1 && up) || (r > 6 && dn)) for (c = 0; c < 8; c++) row[c] = 0;
if (lf) row[0] = 0;
if (rt) row[7] = 0;
} else if (t == T_DOOR) {
if (r == 3 || r == 4) for (c = 0; c < 8; c++) row[c] = 13;
} else if (t == T_CRYS) {
if (r == 3 || r == 4) row[3] = row[4] = 14;
} else if (t == T_LAMP && lamp_blink) {
if (r >= 1 && r <= 6) for (c = (r == 1 || r == 6) ? 2 : 1; c < ((r == 1 || r == 6) ? 6 : 7); c++) row[c] = 11;
if (r == 3 || r == 4) row[3] = row[4] = 15;
}
plot_row(x, y + r, row);
}
}
static void draw_maze(void)
{
u8 x, y;
for (y = 0; y < MH; y++)
for (x = 0; x < MW; x++)
draw_tile(x, y);
}
static void draw_lamps(void)
{
u8 x, y;
for (y = 0; y < MH; y++)
for (x = 0; x < MW; x++)
if (maze[y][x] == T_LAMP) draw_tile(x, y);
}
static void reset_maze(void)
{
u8 x, y;
left = 0;
for (y = 0; y < MH; y++)
for (x = 0; x < MW; x++) {
maze[y][x] = maze_start[y][x];
if (maze[y][x] == T_CRYS || maze[y][x] == T_LAMP) left++;
}
}
/* -------------------------------------------------------------- sprites */
static void spr_at(u8 n, i16 x, i16 y, u8 shape, u8 ctl)
{
sp_sel = n;
if (x < -15 || x > 319 || y < -15 || y > 239) { sp_ctl = 0; return; }
sp_xlo = (u8)x; sp_xhi = (u8)(x >> 8) & 1; sp_y = (u8)y;
sp_shape = shape; sp_ctl = ctl;
}
static void spr_off(u8 n) { sp_sel = n; sp_ctl = 0; }
static void all_off(void) { u8 i; for (i = 0; i < 32; i++) spr_off(i); }
/* a figure at maze pixel (x, y); hidden in the tunnels beyond the maze */
static void figure(u8 n, i16 x, i16 y, u8 shape, u8 ctl)
{
if (x < 4 || x > PW - 4) { spr_off(n); return; }
spr_at(n, MX0 + x - 8, MY0 + y - 8, shape, ctl);
}
static void load_shapes(void)
{
const u8 *p = shape_data;
u8 s, i;
for (s = 0; s < N_SHAPES; s++) {
sp_load = s;
for (i = 0; i < 128; i++) sp_data = *p++;
}
}
/* ------------------------------------------------------------------ HUD */
static void hud(void)
{
hud_dirty = 0;
at(1, 1); ink("0;93"); puts_("CRYSTAL CAVERN ");
ink("0;37"); puts_("SCORE "); ink("0;97"); putn(score, 6);
ink("0;37"); puts_(" LIVES "); ink("0;97"); putn(lives, 0);
ink("0;37"); puts_(" CAVE "); ink("0;97"); putn(level, 0);
if (msg && msg_time) { ink("0;96"); puts_(" "); puts_(msg); }
puts_("\033[K");
ink("0");
}
static void add_score(u16 n)
{
score += n;
if (score >= next_extra) { next_extra += 10000; lives++; say("EXTRA LIFE"); }
hud_dirty = 1;
}
/* --------------------------------------------------------------- moving */
static u8 at_centre(i16 x, i16 y) { return (x & 7) == 4 && (y & 7) == 4; }
static void wrap(i16 *x)
{
if (*x < -8) *x += PW + 16;
else if (*x >= PW + 8) *x -= PW + 16;
}
static u8 can_go(i16 x, i16 y, u8 d)
{
return is_open(tile((x >> 3) + ddx[d], (y >> 3) + ddy[d]));
}
/* ---------------------------------------------------------------- miner */
static void miner_step(void)
{
u8 t;
i16 tx, ty;
if (mwant != D_NONE && mwant == ((mdir + 2) & 3)) mdir = mwant; /* turning back: any time */
if (at_centre(mx, my)) {
tx = mx >> 3; ty = my >> 3;
t = tile(tx, ty);
if (t == T_CRYS || t == T_LAMP) {
if (tx >= 0 && tx < MW) {
maze[ty][tx] = T_OPEN;
draw_tile((u8)tx, (u8)ty);
}
left--;
if (t == T_CRYS) add_score(10);
else {
add_score(50);
fright = 1; combo = 0;
fright_time = level < 7 ? 360 - 40 * level : 100;
{ u8 b; for (b = 0; b < NB; b++) if (bstate[b] == B_HUNT) bdir[b] = (bdir[b] + 2) & 3; }
}
eaten++;
}
if (mwant != D_NONE && can_go(mx, my, mwant)) mdir = mwant;
if (mdir == D_NONE || !can_go(mx, my, mdir)) return; /* against a wall */
}
if (mdir == D_NONE) return;
mx += ddx[mdir]; my += ddy[mdir];
wrap(&mx);
manim++;
}
static void miner(u8 j)
{
u8 spd;
if (j & J_LEFT) mwant = D_LEFT;
else if (j & J_RIGHT) mwant = D_RIGHT;
else if (j & J_UP) mwant = D_UP;
else if (j & J_DOWN) mwant = D_DOWN;
spd = 16 + (level < 5 ? level : 5);
macc += spd;
while (macc >= 16) { macc -= 16; miner_step(); }
figure(SP_MINER, mx, my, S_MINER + ((manim >> 2) & 1), mdir == D_LEFT ? 3 : 1);
}
/* ----------------------------------------------------------------- bats */
static void bat_target(u8 b, i16 *tx, i16 *ty)
{
i16 mtx = mx >> 3, mty = my >> 3, d;
u8 md = mdir == D_NONE ? D_LEFT : mdir;
if (bstate[b] == B_EYES) { *tx = 13; *ty = 9; return; }
if (!mode) { *tx = corner_x[b]; *ty = corner_y[b]; return; } /* scatter */
switch (b) {
case 0: *tx = mtx; *ty = mty; break;
case 1: *tx = mtx + 4 * ddx[md]; *ty = mty + 4 * ddy[md]; break;
case 2:
*tx = 2 * (mtx + 2 * ddx[md]) - (bx[0] >> 3);
*ty = 2 * (mty + 2 * ddy[md]) - (by[0] >> 3);
break;
default:
d = absd((bx[b] >> 3) - mtx) + absd((by[b] >> 3) - mty);
if (d > 8) { *tx = mtx; *ty = mty; }
else { *tx = corner_x[b]; *ty = corner_y[b]; }
}
}
/* At a junction: the way (not back) that ends nearest the target, or a
random one when frightened. */
static void bat_choose(u8 b)
{
i16 tx, ty, nx, ny, d, best = 32767;
u8 k, back = (bdir[b] + 2) & 3, pick = back, start;
if (fright && bstate[b] == B_HUNT) {
start = rnd() & 3;
for (k = 0; k < 4; k++) {
u8 dd = (start + k) & 3;
if (dd != back && can_go(bx[b], by[b], dd)) { bdir[b] = dd; return; }
}
bdir[b] = back;
return;
}
bat_target(b, &tx, &ty);
for (k = 0; k < 4; k++) {
if (k == back || !can_go(bx[b], by[b], k)) continue;
nx = (bx[b] >> 3) + ddx[k] - tx;
ny = (by[b] >> 3) + ddy[k] - ty;
d = absd(nx) + absd(ny);
/* break ties in the order up, left, down, right */
if (d < best || (d == best && (k == D_UP || (k == D_LEFT && pick != D_UP)))) { best = d; pick = k; }
}
bdir[b] = pick;
}
static void bat_step(u8 b)
{
switch (bstate[b]) {
case B_ROOST:
by[b] += (bwait[b] & 8) ? 1 : -1;
if (by[b] < ROOM_Y - 3) by[b] = ROOM_Y - 3;
if (by[b] > ROOM_Y + 3) by[b] = ROOM_Y + 3;
return;
case B_LEAVE:
if (bx[b] < DOOR_X) bx[b]++;
else if (bx[b] > DOOR_X) bx[b]--;
else if (by[b] > OUT_Y) by[b]--;
else { bstate[b] = B_HUNT; bdir[b] = D_LEFT; }
return;
case B_HOME:
if (by[b] < ROOM_Y) by[b]++;
else bstate[b] = B_LEAVE;
return;
}
if (at_centre(bx[b], by[b])) {
if (bstate[b] == B_EYES && bx[b] == DOOR_X && by[b] == OUT_Y) { bstate[b] = B_HOME; return; }
bat_choose(b);
}
bx[b] += ddx[bdir[b]]; by[b] += ddy[bdir[b]];
wrap(&bx[b]);
}
static void kill_miner(void)
{
if (dead || shield) return;
dead = 1; dead_time = 100;
boom_time = 60;
say("THE BATS GOT YOU");
}
static void bats(void)
{
u8 b, spd, s, n;
for (b = 0; b < NB; b++) {
bwait[b]++;
if (bstate[b] == B_ROOST) {
/* let them out by crystals eaten or by time */
if ((b == 1) || (b == 2 && (eaten >= 30 || bwait[b] > 240)) ||
(b == 3 && (eaten >= 60 || bwait[b] > 250 && eaten >= 20)))
bstate[b] = B_LEAVE;
}
if (bstate[b] == B_EYES || bstate[b] == B_HOME) spd = 40;
else if (bstate[b] == B_HUNT && fright) spd = 8;
else if (bstate[b] == B_HUNT && (by[b] >> 3) == TUNNEL_Y && ((bx[b] >> 3) < 6 || (bx[b] >> 3) > MW - 7)) spd = 8;
else spd = 14 + (level < 5 ? level : 5);
bacc[b] += spd;
while (bacc[b] >= 16) { bacc[b] -= 16; bat_step(b); }
if (!dead && bstate[b] == B_HUNT && absd(bx[b] - mx) < 6 && absd(by[b] - my) < 6) {
if (fright) {
bstate[b] = B_EYES;
add_score(200 << combo);
if (combo < 3) combo++;
} else kill_miner();
}
n = (bwait[b] >> 3) & 1;
if (bstate[b] == B_EYES || bstate[b] == B_HOME) s = S_EYES;
else if (fright && bstate[b] != B_ROOST && bstate[b] != B_LEAVE)
s = (fright_time < 120 && (fright_time & 8)) ? S_FLASH + n : S_SCARED + n;
else s = bat_shape[b] + n;
figure(SP_BAT + b, bx[b], by[b], s, 1);
}
}
static void modes(void)
{
if (fright) {
if (!--fright_time) fright = 0;
return;
}
if (mode_secs[mode_i] == 255) return;
if (++mode_time >= (u16)mode_secs[mode_i] * 60) {
u8 b;
mode_time = 0; mode_i++; mode ^= 1;
for (b = 0; b < NB; b++) if (bstate[b] == B_HUNT) bdir[b] = (bdir[b] + 2) & 3;
}
}
static void nugget(void)
{
if (!nugget_on && (eaten == 70 || eaten == 170) && nugget_time == 0) {
nugget_on = 1; nugget_time = 540;
}
if (nugget_time) {
nugget_time--;
if (!nugget_time) nugget_on = 0;
}
if (nugget_on && !dead && absd(mx - (START_X * 8 + 8)) < 6 && absd(my - (START_Y * 8 + 4)) < 6) {
nugget_on = 0; nugget_time = 1;
add_score(500 * level);
say("GOLD!");
}
if (nugget_on) figure(SP_NUGGET, START_X * 8 + 8, START_Y * 8 + 4, S_NUGGET, 1);
else spr_off(SP_NUGGET);
}
static void place_all(void)
{
u8 b;
mx = START_X * 8 + 8; my = START_Y * 8 + 4; /* between columns 13 and 14 */
mdir = D_LEFT; mwant = D_LEFT; macc = 0;
for (b = 0; b < NB; b++) {
bx[b] = roost_x[b]; by[b] = ROOM_Y; bacc[b] = 0; bwait[b] = b * 40;
bstate[b] = B_ROOST; bdir[b] = D_UP;
}
bx[0] = DOOR_X; by[0] = OUT_Y; bstate[0] = B_HUNT; bdir[0] = D_LEFT;
fright = 0; mode = 0; mode_i = 0; mode_time = 0;
nugget_on = 0; nugget_time = 0;
boom_time = 0; spr_off(SP_BOOM);
}
/* ------------------------------------------------------------- screens */
static void clear_all(void)
{
gfill = 0;
while (gfill & 1);
all_off();
}
static u8 wait_fire(void)
{
u8 j;
while (joy & (J_FIRE | J_ENTER | J_ESC)) { wait_frame(); drain_keys(); }
for (;;) {
wait_frame(); drain_keys(); rnd();
j = joy;
if (j & J_ESC) return 0;
if (j & (J_FIRE | J_ENTER)) return 1;
}
}
static void get_ready(void)
{
u8 i;
ink("1;93"); at(19, 36); puts_("GET READY"); ink("0");
figure(SP_MINER, mx, my, S_MINER, 3); /* everyone waits where they are */
for (i = 0; i < NB; i++) figure(SP_BAT + i, bx[i], by[i], bat_shape[i], 1);
for (i = 0; i < 100; i++) { wait_frame(); drain_keys(); }
at(19, 36); puts_(" ");
}
static u8 title(void)
{
u8 b;
clear_all();
puts_("\033[0m\033[2J");
reset_maze();
wall_ink = 12; lamp_blink = 1;
draw_maze();
place_all();
figure(SP_MINER, mx, my, S_MINER, 1);
for (b = 0; b < NB; b++) figure(SP_BAT + b, bx[b], by[b], bat_shape[b], 1);
ink("1;93"); centre(5, " C R Y S T A L C A V E R N ");
ink("0;97");
centre(8, " Gather every crystal in the cave, and keep away from the bats. ");
centre(9, " A lantern scares them for a while: then you can catch them. ");
centre(11, " ARROWS or W A S D move P pause ESC give up ");
ink("1;96"); centre(25, " PRESS SPACE TO BEGIN ");
ink("0");
return wait_fire();
}
static void play(void)
{
u8 j, i;
lives = 3; score = 0; level = 1; next_extra = 10000;
clear_all();
puts_("\033[2J");
for (;;) { /* each cave */
reset_maze();
wall_ink = 12; lamp_blink = 1;
draw_maze();
eaten = 0;
place_all();
dead = 0;
msg = 0; hud();
get_ready();
for (;;) { /* each frame */
wait_frame();
if (drain_keys()) {
at(1, 60); ink("0;93"); puts_("PAUSED - P TO GO ON\033[K"); ink("0");
for (;;) {
wait_frame();
if (joy & J_ESC) { quit = 1; return; }
if (drain_keys()) break;
}
hud_dirty = 1;
}
j = joy;
if (j & J_ESC) return;
if (!(last_frame & 15)) { lamp_blink ^= 1; draw_lamps(); }
if (!dead) {
miner(j);
modes();
bats();
nugget();
} else {
spr_at(SP_BOOM, MX0 + mx - 8, MY0 + my - 8, S_BOOM, (boom_time & 4) ? 1 : 0);
spr_off(SP_MINER);
if (boom_time) boom_time--;
if (!--dead_time) {
if (!--lives) {
hud();
ink("0;44;97");
centre(14, " ");
centre(15, " GAME OVER ");
centre(16, " ");
ink("0");
for (i = 0; i < 150; i++) { wait_frame(); drain_keys(); }
return;
}
place_all();
dead = 0;
hud_dirty = 1;
hud();
get_ready();
}
}
if (msg_time && !--msg_time) hud_dirty = 1;
if (hud_dirty) hud();
if (!left) break; /* every crystal gathered */
}
say("CAVE CLEARED");
hud();
for (i = 0; i < 8; i++) { /* the walls flash */
wall_ink = (i & 1) ? 12 : 15;
draw_maze();
for (j = 0; j < 12; j++) { wait_frame(); drain_keys(); }
}
all_off();
level++;
}
}
void main(void)
{
rnd_state = 0x1980 ^ frame_ctr;
last_frame = frame_ctr;
puts_("\033[?3l\033[?25l");
load_shapes();
quit = 0;
while (!quit) {
if (!title()) break;
play();
}
clear_all();
puts_("\033[0m\033[2J\033[H\033[?25h");
while (con_stat & 1) (void)con_data;
}
+311
View File
@@ -0,0 +1,311 @@
:198600002100D001760378B1280C36000B78B12805545D13EDB0C306AC2C
:208620004FDB00CB4F28FA79D301C9010101010101010101010101010101010101010101A9
:208640000101010101010101020202020202020202010102020202010102020202020202E6
:208660000202010102010101020101010201010201010201010201010102010101020101D0
:208680000201010102010101020101020101020101020101010201010102010102010101B1
:2086A00002010101020101020101020101020101010201010102010103010101020202028D
:2086C000020202020101020202020202020201010103010102010101020101010101010269
:2086E000010102010101010101020101010201010201010102010101010101020101020153
:20870000010101010102010101020101020101010201010101010102010102010101010133
:20872000010201010102010102010101020202020201010000000001010202020202010110
:208740000102010102010101010101010201010100000101010201010101010101020101F6
:208760000201010101010101020101010404010101020101010101010102010102010101CE
:2087800001010101020105050505050501020101010101010102010102010101010101019D
:2087A000020105050505050501020101010101010102010606010101060202020201050563
:2087C00005050505010202020206010101060601020101010201010102010505050505053B
:2087E000010201010102010101020101020101010201010102010101010101010102010152
:20880000010201010102010102010101020101010201010101010101010201010102010131
:20882000010201010201010102010101020101010101010101020101010201010102010111
:208840000202020202010101020202020202020202020101010202020202010101010101E4
:208860000101010101010102010102010101010101010101010101010101010101010101D6
:208880000101010201010201010101010101010101010101010101010101010101010102B5
:2088A00001010201010101010101010101010101030202020202020202020202000002028A
:2088C000020202020202020202030101020101010201010102010102010102010102010167
:2088E00001020101010201010201010102010101020101020101020101020101010201014F
:2089000001020101020101010201010102010102010102010102010101020101010201012E
:20892000020202020201010102020202010102020202010101020202020201010101010105
:20894000010101010101010101010101010101010101010101010100000000000000000000
:20896000000BBBBBB000000000B3333333F00000033333333FF00000099999999900000052
:208980000990990999000000099999999900000000999009900000000CCCCCCCCC00000098
:2089A000CCCCCCCCCC9000009CCCCCCCCC00000004444444440000000444000444000000BB
:2089C0000770000077000000000000000000000000000000000000000000000000000000A9
:2089E000000BBBBBB000000000B3333333F00000033333333FF000000999999999000000D2
:208A00000990990999000000099999999900000000999009900000000CCCCCCCCC00000017
:208A2000CCCCCCCCCC9000009CCCCCCCCC00000004444444440000000044404440000000C2
:208A4000000770770000000000000000000000000000000000000000000000000000000028
:208A6000000000000000000900000000000090099000000000099009990099990099900925
:208A80009999999999999000999F9999F999000009999999999000000090999909000000EC
:208AA000000909909000000000000000000000000000000000000000000000000000000084
:208AC000000000000000000000000000000000000000000000000000000000000000000096
:208AE00000000000000000000000000000000000000000000000000000009999000000093B
:208B00000999999999909009999F9999F99990099999999999999009909999999909900965
:208B20000000999900009000000900009000000000000000000000000000000000000000DA
:208B4000000000000000000000000000000000000000000000000000000000000000000015
:208B6000000000000000000D000000000000D00DD0000000000DD00DDD00DDDD00DDD00D00
:208B8000DDDDDDDDDDDDD000DDDFDDDDFDDD00000DDDDDDDDDD0000000D0DDDD0D0000009F
:208BA000000D0DD0D0000000000000000000000000000000000000000000000000000000FB
:208BC000000000000000000000000000000000000000000000000000000000000000000095
:208BE0000000000000000000000000000000000000000000000000000000DDDD0000000DAE
:208C00000DDDDDDDDDD0D00DDDDFDDDDFDDDD00DDDDDDDDDDDDDD00DD0DDDDDDDD0DD00DC0
:208C20000000DDDD0000D000000D0000D000000000000000000000000000000000000000CD
:208C4000000000000000000000000000000000000000000000000000000000000000000014
:208C6000000000000000000A000000000000A00AA0000000000AA00AAA00AAAA00AAA00A9A
:208C8000AAAAAAAAAAAAA000AAAFAAAAFAAA00000AAAAAAAAAA0000000A0AAAA0A00000097
:208CA000000A0AA0A000000000000000000000000000000000000000000000000000000060
:208CC000000000000000000000000000000000000000000000000000000000000000000094
:208CE0000000000000000000000000000000000000000000000000000000AAAA0000000A16
:208D00000AAAAAAAAAA0A00AAAAFAAAAFAAAA00AAAAAAAAAAAAAA00AA0AAAAAAAA0AA00ABA
:208D20000000AAAA0000A000000A0000A00000000000000000000000000000000000000095
:208D4000000000000000000000000000000000000000000000000000000000000000000013
:208D6000000000000000000300000000000030033000000000033003330033330033300358
:208D80003333333333333000333F3333F333000003333333333000000030333303000000DB
:208DA00000030330300000000000000000000000000000000000000000000000000000004D
:208DC000000000000000000000000000000000000000000000000000000000000000000093
:208DE00000000000000000000000000000000000000000000000000000003333000000030A
:208E00000333333333303003333F3333F33330033333333333333003303333333303300358
:208E2000000033330000300000030000300000000000000000000000000000000000000069
:208E4000000000000000000000000000000000000000000000000000000000000000000012
:208E60000000000000000004000000000000400440000000000440044400444400444004CE
:208E80004444444444444000444F4444F44400000444444444400000004044440400000087
:208EA00000040440400000000000000000000000000000000000000000000000000000002A
:208EC000000000000000000000000000000000000000000000000000000000000000000092
:208EE0000000000000000000000000000000000000000000000000000000444400000004E6
:208F00000444444444404004444F4444F444400444444444444440044044444444044004AE
:208F2000000044440000400000040000400000000000000000000000000000000000000025
:208F4000000000000000000000000000000000000000000000000000000000000000000011
:208F6000000000000000000F000000000000F00FF0000000000FF00FFF00FFFF00FFF00FEA
:208F8000FFFFFFFFFFFFF000FFFFFFFFFFFF00000FFFFFFFFFF0000000F0FFFF0F000000F5
:208FA000000F0FF0F0000000000000000000000000000000000000000000000000000000B3
:208FC000000000000000000000000000000000000000000000000000000000000000000091
:208FE0000000000000000000000000000000000000000000000000000000FFFF0000000F64
:209000000FFFFFFFFFF0F00FFFFFFFFFFFFFF00FFFFFFFFFFFFFF00FF0FFFFFFFF0FF00F6A
:209020000000FFFF0000F000000F0000F00000000000000000000000000000000000000043
:20904000000000000000000000000000000000000000000000000000000000000000000010
:209060000000000000000000000000000000000000000000000000000000000000000000F0
:20908000000000000000000000FF0000FF00000000F40000F40000000000000000000000EA
:2090A0000000000000000000000000000000000000000000000000000000000000000000B0
:2090C000000000000000000000000000000000000000000000000000000000000000000090
:2090E0000000000000000000000000000000000000000000000000000000BBB00000000005
:2091000000BBBFBBB00000000BBFBBBBBB0000000BBBBBB3BB00000000BB3BBBB00000001F
:20912000000BBBB000000000000000000000000000000000000000000000000000000000B9
:2091400000000000000000000000000000000000000000000000000000000000000000000F
:209160000F00000000F0000000F00BB00F000000000F0BB0F00000000000BFFB0000000BB7
:20918000BB0BFFFFB0BBB0000000BFFB00000000000F0BB0F000000000F00BB00F000000C2
:2091A0000F00000000F00000000000000000000000000000000000000000000000000000B0
:2091C00000000000000000000000000000000000000000000000000100FF0000000100FF8F
:2091E00000020406081A011A01FEFE1E1E6C6C5C7C0714071405FF7EB7C823E5CD2086E1AA
:2092000018F5DDE5DD210000DD39F5F5F53B4D44DD36FF00DD5EFF16002100003919DD34DA
:20922000FFE5C5110A006960CDACACC1E17BC63077110A006960CD6CAC4B4278B120D5DDA1
:209240004EFFDD46047990300AC53E30CD2086C10518F279B728110D591600210000391989
:209260007EC5CD2086C118EBDDF9DDE1E133E967E53E1BCD20863E5BCD2086E14C2600E587
:20928000AFF53369CD02923E3BCD2086E1AF67F533CD02923E48C32086E53E1BCD20863EE3
:2092A0005BCD2086E1CDF7913E6DC32086470E00692600197EB728030C18F52E00CB2DCB2F
:2092C000193E2991D56F78CD6F92D1EBC3F7912A70D3292929292929293A70D3AD3270D3C1
:2092E0003A71D3AC3271D3CB3F4F3A70D3A93270D32171D3AE3271D3ED5B70D3C9DB782189
:209300006DD39628F8DB78326DD3C90E00DB000F300EDB01FE702804D65020F10E0118EDD2
:2093200079C92272D32174D33678216FD33601C9CB7C2807AF955F9F9457C9EBC94D44CB8F
:209340007A20087BD61D7ADE0038033E01C9CB782806211C00094D4479D61C78173F1FDEE4
:2093600080380879C6E44F78CEFF476B622919291929291100D019097EC9B7280EFE02282D
:209380000AFE032806D6062802AFC93E01C97DD3307CD331210200397ED3320E00FD210300
:2093A00000FD39FD5E00FD66016B0600097ED3330C79D60838E7E1F133E9DDE5DD2100008A
:2093C000DD39FD21DEFFFD39FDF9DD77FEDD75FD0100D0DD5EFD16006B62291929192929ED
:2093E00009DD5EFE1600194EDD7EFEDD77E8DD36E900DD6EE826002929297DC630DD77EA98
:209400007CCE00DD77EBDD7EFD878787C608DD77ECDD7EE8C6FFDD77ED3E00CEFFDD77EE32
:20942000DD7EE8C601DD77EF3E00CE00DD77F0DD7EFED61B3E0017DD77F1793D3E0128018D
:20944000AFDD77F279D6043E012801AFDD77F379D6023E012801AFDD77F479D6033E0128FD
:2094600001AFDD77F5DD36FF000E00591600210200391936000C79D60838F0DD7EFF3D3E59
:20948000012801AFDD77F6DD7EFFD6063E012801AFDD77F7DD7EFFD6013E00174F3E06DD1B
:2094A00096FF3E0017DD77F8DD7EF2B7CABC95DD7EFDDD77DEDD36DF00D1D51BC5DD6EE822
:2094C0002600CD3D93C13D3E012801AFEE01DD77F9D1D513C5DD6EE82600CD3D93C13D3EC8
:2094E000012801AFEE01DD77FA6960D1D5E5DD6EEDDD66EECD3D93C13D2806DD7EFEB720A0
:2095000003AF18023E01DD77FB6960D1D5E5DD6EEFDD66F0CD3D93C13D2806DDCBF1462068
:2095200003AF18023E01DD77FCDD5EF7DD56F606007AB72806DD7EF9B7201F7BB72806DD89
:209540007EFAB72015783D2006DD7EFBB7200B78D6062014DD7EFCB7280ED5581600210460
:20956000003919D13A75D3770478D60838C379B72806DD7EF9B7200CDD7EF8B72826DD7E32
:20958000FAB72820DD36FA00DD5EFA16002102003919DD75F8DD74F93600DD34FADD7EFADB
:2095A000D60838E4DD7EFBB72804DD36E000DD7EFCB7CA7696DD36E700C37696DD7EFFD6A8
:2095C000033E012801AF47DD7EFFD6043E012801AF5FDD7EF3B7281D78B72004B3CA76965A
:2095E0000E005916002102003919360D0C79D60838F0C37696DD7EF4B7281178B72003B398
:209600002874DD36E40EDD36E30E186ADD7EF5B728643A6AD3B7285ECB41204BDD4EF8CB77
:20962000412044DD7EF6DD77FCDDCBFC462007DD4EF7CB4128040E0218020E01DD7EF7DD11
:2096400077FBDDCBFC462006DDCBFB4628041606180216077992300FD55916002104003934
:2096600019D1360B0C18DB78B72003B32808DD36E40FDD36E30FDD7EECDD86FF210200397B
:20968000E5F533DD6EEADD66EBCD8E93DD34FFDD7EFFD608DA6994DDF9DDE1C92E0026009C
:2096A000E57CCDBA93E1247CD61C38F42C7DD61D38ECC90E0006006960290929092929EB89
:2096C0002100D019EB0600682600197ED6032009C5D56978CDBA93D1C10478D61C38E80CA7
:2096E00079D61D38D0C9CD9BACF5F5AF3234D30E0006006960290929092929EB2100D019BF
:20970000E3212B8619EB0600DD7EFC80DD77FEDD7EFDCE00DD77FF682600197EDD6EFEDD9D
:2097200066FF77D602280BDD6EFEDD66FF7ED60320042134D3340478D61C38CC0C79D61DF6
:2097400038AFDDF9DDE1C9D3707BD6F17A173F1FDE7F38353E3FBB3E019AE25F97EE80FA36
:209760008997FD210200FD39FD7E00D6F1FD7E01173F1FDE7F38123EEFFDBE003E00FD9ED8
:2097800001E28697EE80F28E97AFD37518237BD3717AE601D372FD210200FD39FD7E00D309
:2097A00073FD7E02FD23FD23D374FD7E01FD23D375E1F1F1E9D370AFD375C90E00C579CD86
:2097C000B597C10C79D62038F4C94F7BD6047A173F1FDE8038083EDCBB3E009A300679CDB2
:2097E000B597182521280019457CFD210500FD39FD6600E533FD66FFFD2BE533FD6EFEFDE1
:2098000066FFE5585779CD4797E1F1F1E90157891E007BD37616000AD37703147AD6803833
:20982000F61C7BD61138EBC9AF326FD33E016FCD6F9221C198CD999221C698CDF79121D8EA
:2098400098CD999221DD98CDF79121E498CD99923E06F5332A35D3CD029221D898CD999270
:2098600021E998CDF79121E498CD99923A30D36FAF67F533CD029221D898CD999221F398DC
:20988000CDF79121E498CD99923A31D36FAF67F533CD02923A73D32172D3B628183A74D335
:2098A000B7281221FC98CD9992210199CDF7912A72D3CDF791210599CDF791210999C39903
:2098C00092303B3933004352595354414C2043415645524E20202000303B33370053434F04
:2098E00052452000303B3937002020204C495645532000202020434156452000303B39365A
:2099000000202020001B5B4B003000EB2135D37E8377237E8A77216BD33A35D396233A36FE
:20992000D39E3816216BD37EC61077237ECE27772130D334214099CD2293216FD33601C9FA
:209940004558545241204C494645007DE6074F060079D604B0200C7BE6074F060079D60445
:20996000B02802AFC93E01C94E23462B79D6F878173F1FDE7F300B79C6F04F78CE00712387
:2099800077C979D6E878173F1FDE80D879C6104F78CEFF712377C9DDE5DD210000DD39069A
:2099A00003CB2ACB1B10FA01DC9179DD86044F3001040A4F079F477B815F7A88570603CB1F
:2099C0002CCB1D10FA01D79179DD86044F3001040A4F079F4709CD3D93CD7A93DDE1E13304
:2099E000E9CD9BACF53B3A4CD3D604281F3A4BD306004F030379E6034F06003A4CD36F2668
:209A000000BFED4220063A4CD3324BD3ED5B2ED32A2CD3CD4B99B7CA2B9B3A2CD3DD77FD95
:209A20003A2DD3DD77FE0603DDCBFE2EDDCBFD1E10F6ED4B2ED33E03CB28CB193D20F9C588
:209A40005950DD6EFDDD66FECD3D935FC17BD6023E012801AFDD77FFB720067BD603C2F374
:209A60009ADDCBFE7E2024DD7EFDD61CDD7EFEDE0030181100D0696029092909292919D1D1
:209A8000D5193600DD7EFD69CDBA932134D335DD7EFFB72808210A00CD0B991852213200D0
:209AA000CD0B99215BD33601AF325DD33A31D3D60730173A31D34F06006960292909292933
:209AC00029EB216801BFED521803216400225ED30E002137D30600097ED602200C214FD3EA
:209AE0000600097EC602E603770C79D60438E32164D3343A4CD3D60428183A4CD3F533EDCA
:209B00005B2ED32A2CD3CD9799B728063A4CD3324BD33A4BD3D60428553A4BD3F533ED5BBE
:209B20002ED32A2CD3CD9799B728433A4BD3D604283C21D7913A4BD35F1600197E4F079F64
:209B40005F79212CD38677237B8E7721DC913A4BD35F1600197E4F079F5F79212ED386778F
:209B6000237B8E77212CD3CD6899214ED334DDF9DDE1C9CB572807214CD33602181ECB5F58
:209B80002806AF324CD31814CB472807214CD336031809CB4F2805214CD336013A31D3D6BE
:209BA0000530053A31D318023E05C610214DD386773A4DD3D610380E3A4DD3214DD3C6F0E5
:209BC00077CDE19918EB3A4BD3D60220041603180216013A4ED30F0FE6015FD52A2ED3E582
:209BE000ED5B2CD3AFCDCA97C9CD9BAC21F7FF39F9DD77FFDD73FDDD72FEED5B2CD30603DE
:209C0000CB2ACB1B10FA3A2ED3DD77F73A2FD3DD77F80603DDCBF82EDDCBF71E10F63A4B32
:209C2000D3D60420043E0218033A4BD3DD77FC0137D3DD6EFF2600097EDD4E04DD4605D621
:209C4000032014DD6EFDDD66FE360D2336003E090203AF02C3009E3A62D3B7202D11E59150
:209C6000DD6EFF2600197E5F079F57DD6EFDDD66FE73237211E991DD6EFF2600197E5F07F8
:209C80009F577B02037A02C3009EDD7EFFB72810DD7EFF3D281FDD7EFFD6022850C34A9DF6
:209CA000DD6EFDDD66FE732372DD7EF70203DD7EF802C3009EDD7EFCC6D76F3E00CE91679F
:209CC0007E6F079F67292919EBDD6EFDDD66FE73237211DC91DD6EFC2600197E6F079F67A5
:209CE0002929D1D5197D02037C02C3009EDD7EFCC6D76F3E00CE91677E6F079F6729192926
:209D0000EB2A3BD33E03CB2CCB1D3D20F97B955F7A9C57DD6EFDDD66FE73237211DC91DDE2
:209D20006EFC2600197E6F079F6729D1D51929EB2A43D33E03CB2CCB1D3D20F97B955F7AE5
:209D40009C577B02037A02C3009EDD6EFF260029DD75F9DD74FA3E3BDD86F96F3ED3DD8EC4
:209D6000FA677E23666F3E03CB2CCB1D3D20F9BFED52C5D5CD3093DD73FBDD72FCD1C1DD69
:209D80007EF9C6436FDD7EFACED3677E23666F3E03CB2CCB1D3D20F97DDD96F76F7CDD9E3E
:209DA000F8C5D567CD3093EBD1C17DDD86FB6F7CDD8EFC673E08BD3E009CE2BF9DEE80F28E
:209DC000D69DDD6EFDDD66FE732372DD7EF70203DD7EF802182A11E591DD6EFF2600197E03
:209DE0005F079F57DD6EFDDD66FE73237211E991DD6EFF2600197E5F079F577B02037A028C
:209E0000DDF9DDE1E1F1E9CD9BAC21EFFF39F9DD77FEDD36F8FFDD36F97FDD7EFEC64FDD6C
:209E200077F33E00CED3DD77F4DD6EF3DD66F47EC602E603DD77F5DD77FFDD7EFEDD77FCA8
:209E4000DD36FD00DD7EFCDD77F6DD36F700DDCBF626DDCBF7163A5BD3B7CA209F3E37DDD4
:209E600086FEDD77FC3ED3CE00DD77FDDD6EFCDD66FD7ED602C2209FCDCF92DD73FCDD72B7
:209E8000FDDD7EFCDD77FFE603DD77F8DD36FF00DD7EFFDD86F8DD77FDE603DD77F9DD7E3D
:209EA000F5DD96F928623E43DD86F6DD77FC3ED3DD8EF7DD77FDDD6EFCDD66FD7EDD77FA76
:209EC000237EDD77FB3E3BDD86F6DD77FC3ED3DD8EF7DD77FDDD6EFCDD66FD7EDD77FC2334
:209EE0007EDD77FDDD7EF9F533DD5EFADD56FBDD6EFCDD66FDCD9799B7280DDD6EF3DD66C3
:209F0000F4DD7EF977C31BA0DD34FFDD7EFFD604DA909EDD6EF3DD66F4DD7EF577C31BA0FE
:209F200021020039E521020039EBDD7EFECDE99B0E00DD7EF591CA0AA03E43DD86F6DD775E
:209F4000FA3ED3DD8EF7DD77FBDD6EFADD66FB5E23563E3BDD86F66F3ED3DD8EF7677E232F
:209F6000462BE5C5E561E3336F60CD9799C1E1B7CA0AA05E23560603CB2ACB1B10FA21D714
:209F8000910600097E6F079F67197DDD96EFDD77FC7CDD9EF0DD77FDDD6EFADD66FB5E23A3
:209FA000560603CB2ACB1B10FA21DC910600097E6F079F67197DDD96F16F7CDD9EF267E528
:209FC000C5DD6EFCDD66FDCD3093C1E1C5D5CD3093EBD1C119EB7BDD96F87ADD9EF9E2E3BF
:209FE0009FEE80FA01A0DD6EF8DD66F9BFED52201979FE03280BD6022010DD7EFFD60328F3
:20A0000009DD73F8DD72F9DD71FF0C79D604DA329FDD6EF3DD66F4DD7EFF77DDF9DDE1C9AD
:20A02000CD9BAC21F3FF39F9DD77FFC637DD77FB3E00CED3DD77FCDD6EFBDD66FC7EDD77A2
:20A04000FADD6EFF260029DD75FDDD74FEDD7EFAB72811DD7EFA3D2873DD7EFAD604CA4B19
:20A06000A1C386A1DD7EFDC6434FDD7EFECED3470ADD77FD030ADD77FE0B1157D3DD6EFF1A
:20A08000260019CB5E2805110100180311FFFFDD7EFD835FDD7EFE8A5769607323727BD659
:20A0A000697A173F1FDE8030076960366923360069605E23563E6FBB3E009AE2C0A0EE8062
:20A0C000F2B2A23E6F0203AF02C3B2A2DD7EFDC63BDD77F9DD7EFECED3DD77FADD6EF9DDB1
:20A0E00066FA4E234679D66C78173F1FDE80300D03DD6EF9DD66FA712370C3B2A23E6CB904
:20A100003E0098300D0BDD6EF9DD66FA712370C3B2A20143D3DD6EFDDD66FE094E23462BFA
:20A120003E4CB93E0098E22BA1EE80F235A10B712370C3B2A2DD6EFBDD66FC3602014FD31C
:20A14000DD6EFF2600093602C3B2A20143D3DD6EFDDD66FE097EDD77FD237EDD77FE2BDDC4
:20A160007EFDD66CDD7EFE173F1FDE80300DDD4EFDDD46FE03712370C3B2A2DD6EFBDD66C9
:20A18000FC3601C3B2A23E43DD86FDDD77F33ED3DD8EFEDD77F4E1E55E23563E3BDD86FD15
:20A1A000DD77F53ED3DD8EFEDD77F6C1E1E5C57E23666FCD4B99B72834DD6EFBDD66FC7E09
:20A1C000D6032023D1E1E5D54E234679D66CB02016E1E54E234679D64CB0200BDD6EFBDD89
:20A1E00066FC3604C3B2A2DD7EFFCD079EC1E1E5C57EDD77F7237EDD77F83E4FDD86FFDD12
:20A2000077F93ED3CE00DD77FADD6EF9DD66FA7EC6D7DD77FD3E00CE91DD77FEDD6EFDDDA0
:20A2200066FE7EDD77FEDD77FB079FDD77FCDD7EFBDD86F7DD77FDDD7EFCDD8EF8DD77FE98
:20A24000D1E1E5D5DD7EFD7723DD7EFE77E1E57EDD77FC237EDD77FDDD6EF9DD66FA7EDD6E
:20A2600077FEC6DCDD77FA3E00CE91DD77FBDD6EFADD66FB7EDD77FEDD77FA079FDD77FBF7
:20A28000DD7EFCDD86FADD77F8DD7EFDDD8EFBDD77F9E1E5DD7EF87723DD7EF977DD7EF5E5
:20A2A000DD77FDDD7EF6DD77FEDD6EFDDD66FECD6899DDF9DDE1C93A32D3B7C03A33D3B749
:20A2C000C02132D336012169D336642168D3363C21D6A2C3229354484520424154532047F9
:20A2E0004F5420594F5500CD9BAC21F5FF39F9DD36FF00DD7EFFC657DD77F53E00CED3DDB5
:20A3000077F6E1E57E3CE1E5773E37DD86FFDD77F73ED3CE00DD77F8C1E1E5C57EB7203DE8
:20A32000DD7EFF3D2831DD7EFFD602200E3A64D3D61E3023E1E57EFEF1301CDD7EFFD60363
:20A34000201B3A64D3D63C300EE1E57EFEFB380D3A64D3D6143806D1E1E5D53601C1E1E5BC
:20A36000C57EDD77FEDD6EFF260029DD75F9DD74FADD7EFED6032807DD7EFED6042007DD81
:20A3800036FE28C304A4DD7EFED6023E012801AF4FB7280C3A5BD3B72806DD36FE08186492
:20A3A00079B7284D3E43DD86F96F3ED3DD8EFA674E235E0603CB2BCB1910FA79D60EB320E3
:20A3C000303E3BDD86F96F3ED3DD8EFA674E235E0603CB2BCB1910FA79D6067B173F1FDE4D
:20A3E0008038083E15B93E009B3006DD36FE0818133A31D3D60530053A31D318023E05C68F
:20A400000EDD77FE3E53DD86FF6F3ED3CE00677EDD86FE777EFE10380DC6F077E5DD7EFFA1
:20A42000CD20A0E118EE3A32D3B7C2EDA4C1E1E5C57ED602C2EDA43E3BDD86F9DD77FD3E06
:20A44000D3DD8EFADD77FEDD6EFDDD66FE7EDD77FB237EDD77FC212CD3DD7EFB96DD77FDCE
:20A46000DD7EFC239EDD77FEDD6EFDDD66FECD30937BD6067A173F1FDE8030713E43DD8630
:20A48000F9DD77FD3ED3DD8EFADD77FEDD6EFDDD66FE7EDD77FB237EDD77FC212ED3DD7EE6
:20A4A000FB96DD77FDDD7EFC239EDD77FEDD6EFDDD66FECD30937BD6067A173F1FDE803063
:20A4C0002C3A5BD3B72823D1E1E5D536033A5DD34721C8000418012910FDCD0B993A5DD379
:20A4E000D6033009215DD3341803CDB7A2E1E57E0F0F0FE6014FD1E1E5D54678FE03280486
:20A50000D60420040E0E183B3A5BD3B7282778B728230528201178002A5ED3BFED52300F78
:20A520003A5ED30F0F0FE601280579C60C180379C60A4F180E3EE1DD86FF6F3E91CE006757
:20A540007E814FDD6EF926001143D3195E2356DD7EF9C63B6F3E00CED3677E23666FDD7E51
:20A56000FF3C0601C53341C533EBE5CDCA97DD34FFDD7EFFD604DAF3A2DDF9DDE1C93A5BC5
:20A58000D3B728132A5ED32B225ED33A5FD3215ED3B6C0325BD3C901F1913A63D36F260038
:20A5A000094E793CC82A60D3232260D3060069602909290929092929EB2A60D3AFED52D82D
:20A5C0002100002260D32163D3343A62D3EE013262D30E002137D30600097ED602200C21CA
:20A5E0004FD30600097EC602E603770C79D60438E3C93A67D3B720223A64D3FE462804D61D
:20A60000AA20173A66D3FD2165D3FDB600200B2167D33601211C022265D33A66D3FD216591
:20A62000D3FDB60028132A65D32B2265D33A66D3FDB6002004AF3267D33A67D3B728643A1C
:20A6400032D3B7205E3A2CD3C6905F3A2DD3CEFF57EBCD30937BD6067A173F1FDE803043E2
:20A660003A2ED3C6445F3A2FD3CEFF57EBCD30937BD6067A173F1FDE803028AF3267D32123
:20A6800001002265D33A31D34F0600696029092909290929092929092929CD0B9921BFA697
:20A6A000CD22933A67D3B72811210F01E521BC00E51170003E05CDCA97C93E05C3B5974788
:20A6C0004F4C442100217000222CD32EBC222ED33E02324BD3324CD3AF324DD30E0059165C
:20A6E00000EB29EB213BD319FD21ED91C50600FD09C1FD7E0006007723702143D319366C68
:20A700002336002153D306000936003E57815F3ED3CE005779878781878787122137D306C9
:20A7200000093600214FD306000936030C79D60438AC216C00223BD32E4C2243D32137D372
:20A740003602214FD33602AF325BD33262D3AF3263D32100002260D3AF3267D32265D3AF1F
:20A760003268D33E06C3B5973E00D334DB340FD2BB9718F8DB88E6B02808CDFD92CD0B9392
:20A7800018F2CDFD92CD0B93CDCF92DB88CB7F2802AFC9E63028EB3E01C9CD9BACF52125EB
:20A7A000A8CD99922E243E13CD6F92212AA8CDF7912134A8CD9992210003E52A2ED3E5ED45
:20A7C0005B2CD3AFCDCA970E0069260011E1911946591600EB29EBFD2143D3FD19FD7E0090
:20A7E000DD77FEFD7E01DD77FF213BD3195E2356793CC5260168E5DD6EFEDD66FFE5CDCA24
:20A8000097C10C79D60438C10664C5CDFD92CD0B93C110F62E243E13CD6F922136A8CDF797
:20A8200091F1DDE1C9313B3933004745542052454144590030002020202020202020200072
:20A84000CD9BACF5CD68A721F2A8CDF791CDE6962175D3360C216AD33601CD9C96CDC5A643
:20A86000210001E52A2ED3E5ED5B2CD3AFCDCA970E0021E19106000946591600EB29EB2118
:20A8800043D3197EDD77FE237EDD77FF213BD3195E2356793CC5260168E5DD6EFEDD66FF2D
:20A8A000E5CDCA97C10C79D60438C721FBA8CD99921100A93E05CDAD92211EA9CD999211B0
:20A8C00023A93E08CDAD921164A93E09CDAD9211A2A93E0BCDAD9221D8A9CD999211DDA907
:20A8E0003E19CDAD9221F4A9CD9992CD74A7C1DDE1C91B5B306D1B5B324A00313B39330032
:20A90000204320522059205320542041204C202020432041205620452052204E2000303BEB
:20A9200039370020476174686572206576657279206372797374616C20696E207468652047
:20A94000636176652C20616E64206B65657020617761792066726F6D2074686520626174B6
:20A96000732E20002041206C616E7465726E20736361726573207468656D20666F7220614A
:20A98000207768696C653A207468656E20796F752063616E206361746368207468656D2E87
:20A9A0002000204152524F5753206F72205720412053204420206D6F7665202020202050E2
:20A9C000207061757365202020202045534320676976652075702000313B39360020505231
:20A9E00045535320535041434520544F20424547494E200030002130D33603210000223573
:20AA0000D32131D33601211027226BD3CD68A72190ABCDF791CDE6962175D3360C216AD370
:20AA20003601CD9C96AF3264D3CDC5A6AF3232D32100002272D3CD2898CD9AA7CDFD92CD5E
:20AA40000B93B728322E3C3E01CD6F922195ABCD9992219AABCDF79121B1ABCD9992CDFD7D
:20AA600092DB88073006216ED33601C9CD0B93B728EC216FD33601DB884FCB79C03A6DD342
:20AA8000E60F200D3A6AD3EE01326AD3C5CDB396C13A32D3B7201079CD739BCD7EA5CDE705
:20AAA000A2CDF2A5C334AB3A68D3CB5728040601180206003A2CD3C6285F3A2DD3CE00571F
:20AAC0000E10C52A2ED3E53E06CD4797AFCDB5973A68D3B728042168D3352169D3353A69AE
:20AAE000D3B720502130D3353A30D3B72034CD289821B3ABCD999211BBAB3E0ECDAD9211D7
:20AB0000DAAB3E0FCDAD9211BBAB3E10CDAD9221B1ABCD99920696C5CDFD92CD0B93C11018
:20AB2000F6C9CDC5A6AF3232D3216FD33601CD2898CD9AA73A74D3B7280F2174D3353A7449
:20AB4000D3B72005216FD336013A6FD3B72803CD28983A34D3B7C23CAA21F9ABCD2293CD0D
:20AB600028980E00CB413E0C20023E0F3275D3C5CD9C96C1060CC5CDFD92CD0B93C110F6DE
:20AB80000C79D60838DECDBB972131D334C315AA1B5B324A00303B393300504155534544B7
:20ABA000202D205020544F20474F204F4E1B5B4B003000303B34343B393700202020202093
:20ABC000202020202020202020202020202020202020202020202020200020202020202095
:20ABE0002020202047414D45204F5645522020202020202020202020004341564520434C31
:20AC0000454152454400DB780E00EE803270D379EE193271D3DB78326DD32149ACCDF79169
:20AC2000CD0D98AF326ED33A6ED3B7200BCD40A8B72805CDF6A918EFCD68A72155ACCDF755
:20AC400091DB000FD0DB0118F81B5B3F336C1B5B3F32356C001B5B306D1B5B324A1B5B481E
:20AC60001B5B3F323568005D6F2600547BE680B220110610ED6A17933001833FED6A10F6DF
:20AC80005FEBC906097D6C2600CB1DED6AED523001193F1710F5CB10505FC9E1DDE5DD216C
:11ACA0000000DD39E95D6FCD69ACEBC9CD6CACEBC9A9
:00000001FF
+37
View File
@@ -0,0 +1,37 @@
; Start-up for a machine-code program that BASIC CALLs at 8600h: clear the
; variables, run main, and return to BASIC. The program has no initialised
; variables (gen.py's tables are const, so they stay in the code).
.module crt0
.globl _main
.globl s__DATA, l__DATA
.area _HEADER (ABS)
.org 0x8600
start:
ld hl, #s__DATA
ld bc, #l__DATA
ld a, b
or c
jr z, go
ld (hl), #0
dec bc
ld a, b
or c
jr z, go
ld d, h
ld e, l
inc de
ldir
go:
jp _main ; main's RET goes back to BASIC
.area _HOME
.area _CODE
.area _INITIALIZER
.area _GSINIT
.area _GSFINAL
.area _DATA
.area _INITIALIZED
.area _BSEG
.area _BSS
.area _HEAP
+250
View File
@@ -0,0 +1,250 @@
#!/usr/bin/env python3
"""Make data.h for cavern.c: the maze and the sprite shapes.
The maze is 28 x 29 tiles of 8 x 8 pixels, mirrored left to right. It is
built from a grid of junctions joined by straight corridors: every link is
made, then links are taken away at random as long as every junction keeps at
least two, the whole cave stays connected and the fixed parts (the crossings
at the roost and the start, the side tunnels) stay. So there are no dead ends,
which a chase game can't have. The bats' roost sits in the middle.
Tiles: 0 open, 1 wall, 2 crystal, 3 lantern, 4 roost door, 5 roost, 6 tunnel.
"""
import random
import sys
from collections import deque
W, H = 28, 29
NX = [1, 5, 9, 12] # junction columns on the left (12 mirrors to 15)
NY = [1, 5, 9, 14, 19, 23, 27] # junction rows
ROOST = (10, 11, 17, 16) # cols 10-17, rows 11-16 (x0, y0, x1, y1)
TUNNEL_Y = 14
START = (13, 23) # the miner starts between 13 and 14 on row 23
LANTERNS = [(1, 5), (1, 23)] # and their mirror images
def mirror(x):
return W - 1 - x
def build(seed):
rnd = random.Random(seed)
nodes = {(x, y) for x in NX for y in NY}
nodes.discard((12, 14)) # inside the roost
edges = set()
for y in NY:
row = [x for x in NX if (x, y) in nodes]
for a, b in zip(row, row[1:]):
if not (y == 14 and b == 12):
edges.add(((a, y), (b, y)))
if (12, y) in nodes:
edges.add(((12, y), (15, y))) # across the middle
for x in NX:
col = [y for y in NY if (x, y) in nodes]
for a, b in zip(col, col[1:]):
if x == 12 and a < ROOST[3] and b > ROOST[1]:
continue # would run through the roost
edges.add(((x, a), (x, b)))
fixed = {((12, 9), (15, 9)), ((12, 23), (15, 23))}
def full(es):
g = {}
for (a, b) in es:
for p, q in ((a, b), (b, a)):
g.setdefault(p, set()).add(q)
if a[0] == 12 and b[0] == 15:
continue
ma, mb = (mirror(a[0]), a[1]), (mirror(b[0]), b[1])
g.setdefault(ma, set()).add(mb)
g.setdefault(mb, set()).add(ma)
return g
def ok(es):
g = full(es)
# the tunnel ends count as a link for (1, 14)
deg = {n: len(v) + (1 if n[1] == TUNNEL_Y and n[0] in (1, mirror(1)) else 0) for n, v in g.items()}
if any(d < 2 for d in deg.values()):
return False
start = next(iter(g))
seen, q = {start}, deque([start])
while q:
for m in g[q.popleft()]:
if m not in seen:
seen.add(m); q.append(m)
return len(seen) == len(g)
order = sorted(edges - fixed)
rnd.shuffle(order)
for e in order:
if rnd.random() < 0.45 and ok(edges - {e}):
edges.discard(e)
assert ok(edges)
grid = [[1] * W for _ in range(H)]
def carve(x, y, t=2):
for xx in (x, mirror(x)):
if grid[y][xx] == 1:
grid[y][xx] = t
for (a, b) in edges:
if a[1] == b[1]:
for x in range(min(a[0], b[0]), max(a[0], b[0]) + 1):
carve(x, a[1])
else:
for y in range(min(a[1], b[1]), max(a[1], b[1]) + 1):
carve(a[0], y)
# the roost: walls round a room, a door at the top, a way up to row 9
x0, y0, x1, y1 = ROOST
for y in range(y0, y1 + 1):
for x in range(x0, x1 + 1):
grid[y][x] = 1 if y in (y0, y1) or x in (x0, x1) else 5
grid[y0][13] = grid[y0][14] = 4
grid[10][13] = grid[10][14] = 0
# tunnels: from the side junctions out through the wall
for x in (0,):
carve(x, TUNNEL_Y, 6)
for x in range(1, NX[0] + 1):
if grid[TUNNEL_Y][x] == 1:
carve(x, TUNNEL_Y, 6)
# no crystals in the tunnel, by the roost door, or where the miner starts
for x in range(0, 6):
for xx in (x, mirror(x)):
if grid[TUNNEL_Y][xx] == 2:
grid[TUNNEL_Y][xx] = 6
for x in (12, 13, 14, 15):
if grid[9][x] == 2:
grid[9][x] = 0
for x in (13, 14):
grid[START[1]][x] = 0
for (x, y) in LANTERNS:
assert grid[y][x] == 2, (x, y)
grid[y][x] = grid[y][mirror(x)] = 3
return grid
def check(grid):
open_t = lambda t: t in (0, 2, 3, 6)
for y in range(H):
for x in range(W):
if not open_t(grid[y][x]):
continue
n = 0
for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nx, ny = (x + dx) % W, y + dy
if 0 <= ny < H and (open_t(grid[ny][nx]) or grid[ny][nx] == 4):
n += 1
assert n >= 2, "dead end at %d,%d" % (x, y)
for y in range(H):
assert grid[y] == grid[y][::-1], "row %d is not mirrored" % y
# ---------------------------------------------------------------- shapes
Z = "0" * 16
MINER_A = [Z,
"00000BBBBBB00000",
"0000B3333333F000", # helmet with a lamp
"00033333333FF000",
"0009999999990000",
"0009909909990000",
"0009999999990000",
"0000999009900000",
"000CCCCCCCCC0000",
"00CCCCCCCCCC9000",
"009CCCCCCCCC0000",
"000444444444000",
"0004440004440000",
"0007700000770000",
] + [Z] * 2
MINER_A[11] = MINER_A[11] + "0"
MINER_B = MINER_A[:11] + [
"0004444444440000",
"0000444044400000",
"0000077077000000",
] + [Z] * 2
def bat(c, frame):
if frame == 0:
rows = [Z, Z,
"0C000000000000C0",
"0CC0000000000CC0",
"0CCC00CCCC00CCC0",
"0CCCCCCCCCCCCCC0",
"00CCCFCCCCFCCC00",
"000CCCCCCCCCC000",
"0000C0CCCC0C0000",
"00000C0CC0C00000",
"000000000000000" + "0"] + [Z] * 5
else:
rows = [Z, Z, Z, Z,
"000000CCCC000000",
"0C0CCCCCCCCCC0C0",
"0CCCCFCCCCFCCCC0",
"0CCCCCCCCCCCCCC0",
"0CC0CCCCCCCC0CC0",
"0C0000CCCC0000C0",
"00000C0000C00000"] + [Z] * 5
return [r.replace("C", c) for r in rows]
EYES = [Z] * 6 + ["0000FF0000FF0000", "0000F40000F40000"] + [Z] * 8
NUGGET = [Z] * 4 + [
"000000BBB0000000",
"0000BBBFBBB00000",
"000BBFBBBBBB0000",
"000BBBBBB3BB0000",
"0000BB3BBBB00000",
"00000BBBB0000000",
] + [Z] * 6
BOOM = [Z,
"000F00000000F000",
"0000F00BB00F0000",
"00000F0BB0F00000",
"000000BFFB000000",
"0BBB0BFFFFB0BBB0",
"000000BFFB000000",
"00000F0BB0F00000",
"0000F00BB00F0000",
"000F00000000F000",
] + [Z] * 6
SHAPES = [("S_MINER", MINER_A), ("S_MINER2", MINER_B),
("S_BAT_R", bat("9", 0)), ("S_BAT_R2", bat("9", 1)),
("S_BAT_M", bat("D", 0)), ("S_BAT_M2", bat("D", 1)),
("S_BAT_G", bat("A", 0)), ("S_BAT_G2", bat("A", 1)),
("S_BAT_O", bat("3", 0)), ("S_BAT_O2", bat("3", 1)),
("S_SCARED", bat("4", 0)), ("S_SCARED2", bat("4", 1)),
("S_FLASH", bat("F", 0)), ("S_FLASH2", bat("F", 1)),
("S_EYES", EYES), ("S_NUGGET", NUGGET), ("S_BOOM", BOOM)]
def main(out, seed=11):
grid = build(seed)
check(grid)
crystals = sum(r.count(2) + r.count(3) for r in grid)
L = ["/* made by gen.py - edit that instead */", ""]
L.append("#define MW %d\n#define MH %d" % (W, H))
L.append("#define START_X %d\n#define START_Y %d\n#define TUNNEL_Y %d" % (START[0], START[1], TUNNEL_Y))
L.append("#define CRYSTALS %d" % crystals)
L.append("static const u8 maze_start[MH][MW] = {")
for row in grid:
L.append(" {" + ",".join(str(t) for t in row) + "},")
L.append("};")
for i, (name, img) in enumerate(SHAPES):
assert len(img) == 16 and all(len(r) == 16 for r in img), name
L.append("#define %s %d" % (name, i))
L.append("#define N_SHAPES %d" % len(SHAPES))
L.append("static const u8 shape_data[N_SHAPES * 128] = {")
for name, img in SHAPES:
b = [int(r[x], 16) << 4 | int(r[x + 1], 16) for r in img for x in range(0, 16, 2)]
L.append(" " + ",".join(str(v) for v in b) + ", /* %s */" % name)
L.append("};")
open(out, "w").write("\n".join(L) + "\n")
print("data.h: maze %d x %d, %d crystals, %d shapes" % (W, H, crystals, len(SHAPES)))
return grid
if __name__ == "__main__":
g = main(sys.argv[1] if len(sys.argv) > 1 else "data.h", int(sys.argv[2]) if len(sys.argv) > 2 and sys.argv[2].isdigit() else 11)
if "--show" in sys.argv:
for row in g:
print("".join(" ##..oo==-- "[t * 2:t * 2 + 2][0] if False else " #.o=- "[t] for t in row))
+132
View File
@@ -0,0 +1,132 @@
#!/usr/bin/env python3
"""Run Crystal Cavern in tools/z80emu.py on a model of the board
(tools/boardemu.py) and take screenshots.
python3 test-cavern.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 walls stop the miner,
that crystals score and that the bats leave the roost, and with --play lets
an autopilot gather every crystal in the cave by breadth-first search. The
autopilot can't be caught (it keeps the miner shielded), so this checks the
maze and the rules, not its skill.
"""
import argparse
import os
import sys
from collections import deque
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, ESC = 1, 2, 4, 8, 16, 128
MW, MH = 28, 29
OPEN = (0, 2, 3, 6)
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 cave")
a = ap.parse_args()
mem, hi = load_ihx(os.path.join(HERE, "out", "cavern.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", "cavern.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 maze(x, y):
return b.m[sym["_maze"] + y * MW + (x % MW)] if 0 <= y < MH else 1
def screen():
return b.screen_text().display
b.run_frames(5)
check("title screen", "C R Y S T A L C A V E R 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(110) # GET READY
check("game starts", "CAVE 1" in screen()[0] and "LIVES 3" in screen()[0])
b.m[sym["_shield"]] = 1
left0 = u8("_left")
c0 = b.steps
worst = 0
for f in range(120):
c1 = b.steps
b.run_frames(1, LEFT)
worst = max(worst, b.steps - c1)
print("%.0f instructions a frame on average, %d at most (%.0f%%)" %
((b.steps - c0) / 120, worst, worst / 5500))
check("every frame fits in a 60th of a second", worst < 550000)
check("crystals are gathered and score", u8("_left") < left0 and s16("_score") > 0)
x0 = s16("_mx")
b.run_frames(20, LEFT)
check("a wall stops the miner", s16("_mx") == x0 or (s16("_mx") & 7) == 4)
check("the bats leave the roost", sum(1 for k in range(4) if u8("_bstate", k) == 2) >= 2)
if a.shots: b.shot(os.path.join(a.shots, "2-chase.png"), font)
if a.play:
frame = 0
want = LEFT
while frame < 30000 and b.pc != 0:
b.m[sym["_shield"]] = 1
mx, my = s16("_mx"), s16("_my")
if (mx & 7) == 4 and (my & 7) == 4:
start = (mx >> 3, my >> 3)
prev = {start: None}
q = deque([start])
goal = None
while q:
p = q.popleft()
if maze(*p) in (2, 3) and p != start:
goal = p
break
for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
n = ((p[0] + dx) % MW, p[1] + dy)
if n not in prev and maze(*n) in OPEN:
prev[n] = p; q.append(n)
if goal:
while prev[goal] != start:
goal = prev[goal]
dx = (goal[0] - start[0] + MW // 2) % MW - MW // 2
dy = goal[1] - start[1]
want = RIGHT if dx > 0 else LEFT if dx < 0 else DOWN if dy > 0 else UP
b.run_frames(1, want)
frame += 1
if frame == 900 and a.shots: b.shot(os.path.join(a.shots, "3-midgame.png"), font)
if "CAVE 2" in screen()[0]:
break
print(screen()[0])
check("autopilot gathered every crystal in %d frames" % frame, "CAVE 2" in screen()[0])
b.run_frames(110) # the next cave's GET READY
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()