Files
T
brad ArantandClaude Opus 5.5 073db68754 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>
2026-11-29 21:07:33 -08:00

649 lines
20 KiB
C

/* 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;
}