/* The Iron Keep: a top-down escape game for the Basys 3 Z-80. You are a prisoner in a castle of twelve rooms. Steal the crown from the throne room and get out down the stairs. Guards patrol, and shoot when they can see you along a row or a column; some come after you. Keys open locked doors, chests hold gold, crates hold ammunition. It runs at 8600h, loaded by the KEEP program in BASIC, and returns to BASIC when you quit. It uses the hardware sprites (ports 70h-78h), the graphics layer (30h-34h), the held-key port (88h) and the terminal for text. Build with build.sh; gen.py makes data.h from the art and the map. */ typedef unsigned char u8; typedef signed char i8; typedef unsigned int u16; typedef int i16; __sfr __at 0x00 con_stat; /* bit 0: key waiting, bit 1: can send */ __sfr __at 0x01 con_data; __sfr __at 0x30 gx_lo; __sfr __at 0x31 gx_hi; __sfr __at 0x32 gy_p; __sfr __at 0x33 gpix; /* plot, then X + 1 */ __sfr __at 0x34 gfill; /* fill the graphics (and turn sprites off) */ __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 /* directions */ #define D_DOWN 0 #define D_UP 1 #define D_LEFT 2 #define D_RIGHT 3 static const i8 dir_dx[4] = { 0, 0, -1, 1 }; static const i8 dir_dy[4] = { 1, -1, 0, 0 }; /* tiles */ #define T_FLOOR 0 #define T_WALL 1 #define T_DOOR 2 #define T_STAIRS 3 #define T_OUT 0xFF /* sprite numbers: lower numbers are in front */ #define SP_PLAYER 0 #define SP_PSHOT 1 /* 2 */ #define SP_GUARD 3 /* 6 */ #define SP_GSHOT 9 /* 4 */ #define SP_ITEM 13 /* 8 */ #define SP_PUFF 21 #define NG 6 #define NI 8 #define NPS 2 #define NGS 4 /* the hit box inside a 16 x 16 figure */ #define HB_X0 3 #define HB_X1 12 #define HB_Y0 2 #define HB_Y1 14 /* -------------------------------------------------------------- state */ static u8 map[14][20]; /* not static: test-keep.py finds these in the link map */ u8 room; /* 0-11: row * 4 + column */ static u16 gone[12]; /* objects taken or guards beaten, by index */ static u8 opened[12]; /* doors opened, by order in the room */ i16 px, py; /* player */ static i16 ex, ey; /* where it entered the room */ static u8 pdir, panim, fire_cool, fire_held; u8 inv, lives, keys, ammo, crown; static u16 gold, kills; static i16 gx[NG], gy[NG]; static u8 gdir[NG], gon[NG], gkind[NG], gcool[NG], gsee[NG], gobj[NG]; static i16 ix[NI], iy[NI]; static u8 itype[NI], ion[NI], iobj[NI]; static i16 psx[NPS], psy[NPS], gsx[NGS], gsy[NGS]; static u8 psd[NPS], pson[NPS], gsd[NGS], gson[NGS]; static u8 puff; static i16 puff_x, puff_y; static u8 last_frame, quit, status_dirty, msg_time; u8 over, won; static const char *msg; static u16 rnd_state; /* -------------------------------------------------------------- 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) { char b[6]; u8 i = 0; do { b[i++] = '0' + n % 10; n /= 10; } while (n); while (i) putch(b[--i]); } static void at(u8 r, u8 c) { putch(27); putch('['); putn(r); putch(';'); putn(c); 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; } /* Keys also arrive as characters; throw them away (P pauses). */ 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; } /* ----------------------------------------------------------- graphics */ static void clear_gfx(void) { gfill = 0; /* also turns every sprite off */ while (gfill & 1); } static void draw_tile(u8 tx, u8 ty, u8 t) { const u8 *p = tile_data + ((u16)t << 8); u16 x = tx << 4; u8 y = 16 + (ty << 4), r, i; for (r = 0; r < 16; r++) { gx_lo = x; gx_hi = x >> 8; gy_p = y + r; for (i = 0; i < 16; i++) gpix = *p++; } } static void spr_at(u8 n, i16 x, i16 y) { sp_sel = n; sp_xlo = x; sp_xhi = (x >> 8) & 1; sp_y = y; } static void spr_show(u8 n, u8 shape, u8 ctl) { sp_sel = n; sp_shape = shape; sp_ctl = ctl; } static void spr_off(u8 n) { sp_sel = n; sp_ctl = 0; } 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++; } } /* --------------------------------------------------------------- map */ static u8 tile_at(i16 x, i16 y) { if (x < 0 || x > 319 || y < 16 || y > 239) return T_OUT; return map[(u8)((y - 16) >> 4)][(u8)(x >> 4)]; } static u8 hit_tx, hit_ty; /* What stops a figure at (x, y): 0 nothing, T_WALL or T_DOOR (hit_tx/ty say which tile). Off the room is open for the player, a wall for guards. */ static u8 blocked(i16 x, i16 y, u8 guard) { static const u8 cx[4] = { HB_X0, HB_X1, HB_X0, HB_X1 }; static const u8 cy[4] = { HB_Y0, HB_Y0, HB_Y1, HB_Y1 }; u8 i, t; for (i = 0; i < 4; i++) { t = tile_at(x + cx[i], y + cy[i]); if (t == T_WALL || t == T_DOOR) { hit_tx = (u8)((x + cx[i]) >> 4); hit_ty = (u8)((y + cy[i] - 16) >> 4); return t; } if (t == T_OUT && guard) return T_WALL; } return 0; } static u8 door_index(u8 tx, u8 ty) { const u8 *p = room_tiles[room]; u8 n = 0, i, t; u16 k, want = ty * 20 + tx; for (k = 0; k < want; k++) { i = p[k >> 1]; t = (k & 1) ? i >> 4 : i & 15; if (t == T_DOOR) n++; } return n; } static void say(const char *s) { msg = s; msg_time = 180; status_dirty = 1; } /* ------------------------------------------------------------- status */ static void status(void) { status_dirty = 0; at(1, 1); ink("0;93"); puts_("THE IRON KEEP"); ink("0;37"); puts_(" "); puts_(room_names[room]); puts_("\033[K"); if (msg && msg_time) { at(1, 40); ink("0;96"); puts_(msg); } at(2, 1); ink("0;37"); puts_("LIVES "); ink("0;97"); putn(lives); ink("0;37"); puts_(" KEYS "); ink("0;97"); putn(keys); ink("0;37"); puts_(" AMMO "); ink("0;97"); putn(ammo); ink("0;37"); puts_(" GOLD "); ink("0;97"); putn(gold); ink("0;37"); if (crown) { puts_(" "); ink("0;93"); puts_("CROWN"); } puts_("\033[K"); ink("0"); } /* --------------------------------------------------------------- rooms */ static void enter_room(void) { const u8 *p = room_tiles[room], *o = room_objs[room]; u8 x, y, i, t, d = 0, ng = 0, ni = 0, k = 0; clear_gfx(); for (y = 0; y < 14; y++) for (x = 0; x < 20; x += 2) { i = *p++; map[y][x] = i & 15; map[y][x + 1] = i >> 4; } for (y = 0; y < 14; y++) for (x = 0; x < 20; x++) { t = map[y][x]; if (t == T_DOOR && (opened[room] & (1 << d++))) t = map[y][x] = T_FLOOR; if (t != T_FLOOR) draw_tile(x, y, t); } for (i = 0; i < NG; i++) gon[i] = 0; for (i = 0; i < NI; i++) ion[i] = 0; for (; *o; o += 3, k++) { if (gone[room] & (1 << k)) continue; t = o[0]; if (t == 'h' || t == 'v' || t == 'c') { gx[ng] = o[1] << 4; gy[ng] = 16 + (o[2] << 4); gkind[ng] = t; gon[ng] = 1; gsee[ng] = 0; gobj[ng] = k; gdir[ng] = t == 'v' ? D_DOWN : D_RIGHT; gcool[ng] = 30 + (rnd() & 31); ng++; } else if (t != 'P') { ix[ni] = o[1] << 4; iy[ni] = 16 + (o[2] << 4); itype[ni] = t; ion[ni] = 1; iobj[ni] = k; spr_at(SP_ITEM + ni, ix[ni], iy[ni]); spr_show(SP_ITEM + ni, t == 'K' ? S_KEY : t == 'G' ? S_CHEST : t == 'A' ? S_AMMO : S_CROWN, 1); ni++; } } for (i = 0; i < NPS; i++) pson[i] = 0; for (i = 0; i < NGS; i++) gson[i] = 0; puff = 0; ex = px; ey = py; status_dirty = 1; } /* Leave by an edge into the next room. */ static void edges(void) { if (px + HB_X0 < 0) { room--; px = 304; } else if (px + HB_X1 > 319) { room++; px = 0; } else if (py + HB_Y0 < 16) { room -= 4; py = 224; } else if (py + HB_Y1 > 239) { room += 4; py = 16; } else return; enter_room(); } /* ------------------------------------------------------------- player */ static void open_door(void) { if (!keys) { say("LOCKED. YOU NEED A KEY."); return; } keys--; opened[room] |= 1 << door_index(hit_tx, hit_ty); map[hit_ty][hit_tx] = T_FLOOR; draw_tile(hit_tx, hit_ty, T_FLOOR); say("THE KEY TURNS. THE DOOR OPENS."); } static void move_player(u8 j) { i16 n; u8 t, moved = 0; if (j & (J_LEFT | J_RIGHT)) { pdir = (j & J_LEFT) ? D_LEFT : D_RIGHT; n = px + ((j & J_LEFT) ? -2 : 2); t = blocked(n, py, 0); if (!t) { px = n; moved = 1; } else if (t == T_DOOR) open_door(); } if (j & (J_UP | J_DOWN)) { if (!(j & (J_LEFT | J_RIGHT))) pdir = (j & J_UP) ? D_UP : D_DOWN; n = py + ((j & J_UP) ? -2 : 2); t = blocked(px, n, 0); if (!t) { py = n; moved = 1; } else if (t == T_DOOR) open_door(); } if (moved) panim++; } static void fire(u8 j) { u8 i; if (!(j & J_FIRE)) { fire_held = 0; return; } if (fire_held || fire_cool) return; fire_held = 1; if (!ammo) { say("OUT OF AMMUNITION!"); return; } for (i = 0; i < NPS; i++) if (!pson[i]) { pson[i] = 1; psd[i] = pdir; psx[i] = px + 8 + dir_dx[pdir] * 6; psy[i] = py + 8 + dir_dy[pdir] * 6; ammo--; fire_cool = 8; status_dirty = 1; spr_show(SP_PSHOT + i, S_SHOT, 1); return; } } static void player_hit(void) { u8 i; if (inv || over) return; puff = 30; puff_x = px; puff_y = py; if (!--lives) { over = 1; won = 0; return; } say("YOU ARE HIT!"); px = ex; py = ey; inv = 120; for (i = 0; i < NGS; i++) { gson[i] = 0; spr_off(SP_GSHOT + i); } } static u8 overlap(i16 ax, i16 ay, i16 bx, i16 by) { return ax + HB_X0 <= bx + HB_X1 && bx + HB_X0 <= ax + HB_X1 && ay + HB_Y0 <= by + HB_Y1 && by + HB_Y0 <= ay + HB_Y1; } static void pick_up(void) { u8 i; for (i = 0; i < NI; i++) { if (!ion[i] || !overlap(px, py, ix[i], iy[i])) continue; ion[i] = 0; gone[room] |= 1 << iobj[i]; spr_off(SP_ITEM + i); switch (itype[i]) { case 'K': keys++; say("A KEY!"); break; case 'G': gold += 100; say("GOLD! 100 CROWNS."); break; case 'A': ammo += 5; say("FIVE MORE SHOTS."); break; case 'C': crown = 1; say("THE CROWN! NOW FIND THE STAIRS."); break; } } if (tile_at(px + 8, py + 8) == T_STAIRS) { if (crown) { over = 1; won = 1; } else if (msg_time < 120) say("NOT WITHOUT THE CROWN."); } } /* -------------------------------------------------------------- guards */ /* Can guard i see the player along a row or a column? Returns the direction to look in, plus 1, or 0. */ static u8 sees(u8 i) { i16 gcx = gx[i] + 8, gcy = gy[i] + 8, pcx = px + 8, pcy = py + 8, d; u8 a, b, t, k; d = gcy - pcy; if (d > -8 && d < 8) { k = (u8)((gcy - 16) >> 4); a = (u8)(gcx >> 4); b = (u8)(pcx >> 4); if (a > b) { t = a; a = b; b = t; } for (; a <= b; a++) if (map[k][a] == T_WALL || map[k][a] == T_DOOR) return 0; return (pcx < gcx ? D_LEFT : D_RIGHT) + 1; } d = gcx - pcx; if (d > -8 && d < 8) { k = (u8)(gcx >> 4); a = (u8)((gcy - 16) >> 4); b = (u8)((pcy - 16) >> 4); if (a > b) { t = a; a = b; b = t; } for (; a <= b; a++) if (map[a][k] == T_WALL || map[a][k] == T_DOOR) return 0; return (pcy < gcy ? D_UP : D_DOWN) + 1; } return 0; } static void guard_shoots(u8 i) { u8 k; for (k = 0; k < NGS; k++) if (!gson[k]) { gson[k] = 1; gsd[k] = gdir[i]; gsx[k] = gx[i] + 8 + dir_dx[gdir[i]] * 6; gsy[k] = gy[i] + 8 + dir_dy[gdir[i]] * 6; spr_show(SP_GSHOT + k, S_GSHOT, 1); gcool[i] = 45 + (rnd() & 63); return; } } static void guards(void) { u8 i, s, d; i16 nx, ny; for (i = 0; i < NG; i++) { if (!gon[i]) continue; if (gcool[i]) gcool[i]--; s = inv ? 0 : sees(i); if (s) { gdir[i] = s - 1; if (!gsee[i]) { gsee[i] = 1; if (gcool[i] < 25) gcool[i] = 25; } if (!gcool[i]) guard_shoots(i); if (gkind[i] != 'c') goto shown; /* the others stand and shoot */ } else gsee[i] = 0; d = gdir[i]; nx = gx[i] + dir_dx[d]; ny = gy[i] + dir_dy[d]; if (blocked(nx, ny, 1)) { /* turn round, or for a chaser, try the other way across */ if (gkind[i] == 'c' && !s) gdir[i] = (rnd() & 3); else if (!s) gdir[i] = d ^ 1; } else { gx[i] = nx; gy[i] = ny; } if (overlap(gx[i], gy[i], px, py)) player_hit(); shown: d = gdir[i]; spr_at(SP_GUARD + i, gx[i], gy[i]); spr_show(SP_GUARD + i, (d == D_DOWN ? S_GDOWN : d == D_UP ? S_GUP : S_GSIDE) + (((gx[i] + gy[i]) >> 3) & 1), d == D_LEFT ? 3 : 1); } } /* ------------------------------------------------------------- bullets */ static void shots(void) { u8 i, k; for (i = 0; i < NPS; i++) { if (!pson[i]) continue; psx[i] += dir_dx[psd[i]] * 5; psy[i] += dir_dy[psd[i]] * 5; k = tile_at(psx[i], psy[i]); if (k == T_WALL || k == T_DOOR || k == T_OUT) { pson[i] = 0; spr_off(SP_PSHOT + i); continue; } for (k = 0; k < NG; k++) if (gon[k] && psx[i] >= gx[k] + HB_X0 && psx[i] <= gx[k] + HB_X1 && psy[i] >= gy[k] + HB_Y0 && psy[i] <= gy[k] + HB_Y1) { gon[k] = 0; gone[room] |= 1 << gobj[k]; spr_off(SP_GUARD + k); pson[i] = 0; spr_off(SP_PSHOT + i); puff = 20; puff_x = gx[k]; puff_y = gy[k]; kills++; break; } if (pson[i]) spr_at(SP_PSHOT + i, psx[i] - 8, psy[i] - 8); } for (i = 0; i < NGS; i++) { if (!gson[i]) continue; gsx[i] += dir_dx[gsd[i]] * 3; gsy[i] += dir_dy[gsd[i]] * 3; k = tile_at(gsx[i], gsy[i]); if (k == T_WALL || k == T_DOOR || k == T_OUT) { gson[i] = 0; spr_off(SP_GSHOT + i); continue; } if (gsx[i] >= px + HB_X0 && gsx[i] <= px + HB_X1 && gsy[i] >= py + HB_Y0 && gsy[i] <= py + HB_Y1) { gson[i] = 0; spr_off(SP_GSHOT + i); player_hit(); continue; } spr_at(SP_GSHOT + i, gsx[i] - 8, gsy[i] - 8); } } static void draw_player(void) { u8 s = pdir == D_DOWN ? S_PDOWN : pdir == D_UP ? S_PUP : S_PSIDE; spr_at(SP_PLAYER, px, py); spr_show(SP_PLAYER, s + ((panim >> 3) & 1), (inv & 4) ? 0 : pdir == D_LEFT ? 3 : 1); if (puff) { spr_at(SP_PUFF, puff_x, puff_y); spr_show(SP_PUFF, S_PUFF, --puff ? 1 : 0); } } /* -------------------------------------------------------------- screens */ 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 u8 title(void) { u8 i; clear_gfx(); puts_("\033[0m\033[2J"); for (i = 0; i < 20; i++) { draw_tile(i, 0, T_WALL); draw_tile(i, 13, T_WALL); } for (i = 1; i < 13; i++) { draw_tile(0, i, T_WALL); draw_tile(19, i, T_WALL); } draw_tile(9, 13, T_DOOR); draw_tile(10, 13, T_DOOR); spr_at(0, 120, 168); spr_show(0, S_PDOWN, 1); spr_at(1, 152, 168); spr_show(1, S_CROWN, 1); spr_at(2, 184, 168); spr_show(2, S_GSIDE, 3); spr_at(3, 72, 168); spr_show(3, S_GSIDE, 1); ink("1;93"); centre(7, "T H E I R O N K E E P"); ink("0;37"); centre(10, "You are a prisoner in the Iron Keep. Steal the king's crown"); centre(11, "from the throne room, and escape down the stairs."); centre(14, "ARROWS or W A S D move SPACE or CTRL shoot"); centre(15, "Keys open locked doors. Chests hold gold, crates hold shots."); centre(16, "Guards see along rows and columns - and they shoot."); centre(17, "P pauses. ESC gives up."); ink("1;96"); centre(25, "PRESS SPACE TO BEGIN"); ink("0"); return wait_fire(); } static u8 end_screen(void) { u16 score = gold + kills * 50; u8 j; if (won) score += 1000 + 500 + lives * 200; else if (crown) score += 1000; clear_gfx(); /* a clean box: no walls or figures behind it */ ink("0;44;97"); centre(12, " "); centre(13, won ? " YOU ESCAPED WITH THE CROWN! " : " THE GUARDS HAVE TAKEN YOU. "); centre(14, " "); at(15, 21); puts_(" SCORE "); putn(score); for (j = 10 + (score > 9) + (score > 99) + (score > 999) + (score > 9999); j < 40; j++) putch(' '); centre(16, " "); centre(17, " SPACE TO PLAY AGAIN, ESC TO STOP "); centre(18, " "); ink("0"); return wait_fire(); } /* ----------------------------------------------------------------- game */ static void play(void) { u8 j, i; const u8 *o = room_objs[0]; for (i = 0; i < 12; i++) { gone[i] = 0; opened[i] = 0; } lives = 3; keys = 0; ammo = 10; gold = 0; kills = 0; crown = 0; over = 0; won = 0; inv = 0; fire_cool = 0; fire_held = 1; panim = 0; msg = 0; msg_time = 0; pdir = D_DOWN; room = 0; for (; *o; o += 3) if (o[0] == 'P') { px = o[1] << 4; py = 16 + (o[2] << 4); } puts_("\033[2J"); enter_room(); say("FIND THE CROWN. MIND THE GUARDS."); while (!over) { wait_frame(); if (drain_keys()) { /* P: pause until P again */ at(1, 40); ink("0;96"); puts_("PAUSED - P TO GO ON\033[K"); ink("0"); for (;;) { wait_frame(); if (joy & J_ESC) { quit = 1; return; } if (drain_keys()) break; } status_dirty = 1; } j = joy; if (j & J_ESC) { over = 1; won = 0; break; } move_player(j); edges(); fire(j); shots(); guards(); pick_up(); draw_player(); if (inv) inv--; if (fire_cool) fire_cool--; if (msg_time && !--msg_time) status_dirty = 1; if (status_dirty) status(); } status(); for (i = 0; i < 60; i++) { wait_frame(); drain_keys(); draw_player(); } } void main(void) { rnd_state = 0xACE1 ^ frame_ctr; last_frame = frame_ctr; puts_("\033[?3l\033[?25l"); /* 80 columns (this clears), no cursor */ load_shapes(); quit = 0; while (!quit) { if (!title()) break; play(); if (quit) break; if (!end_screen()) break; } clear_gfx(); puts_("\033[0m\033[2J\033[H\033[?25h"); while (con_stat & 1) (void)con_data; }