Files
T
brad ArantandClaude Opus 5.5 0b16e1dce0 Star Warden: a side-scrolling planet defence game
games/warden: an original Defender-style game in C for the Z-80 - landers
snatch humanoids from a planet that wraps round (1,280 pixels, exactly
four graphics widths, so world column x lives in buffer column x mod 320
and only columns scrolling into view are drawn), mutants, bombers and
mines, a laser, smart bombs, hyperspace and a long-range scanner in text.
test-warden.py checks scrolling and the scanner and has an autopilot
clear a wave; manual and README describe it.

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

815 lines
24 KiB
C

/* Star Warden: a side-scrolling planet defence game for the Basys 3 Z-80.
Landers come down to snatch the humanoids walking on the planet and carry
them off; one that reaches the top turns into a mutant that hunts you.
Shoot them, catch the humanoids they drop, and stay clear of the bombers'
mines. The long-range scanner at the top shows the whole planet. A wave
ends when every enemy is gone, with a bonus for each humanoid still alive.
The planet is 1,280 pixels round, exactly four buffers wide, so world
column x always lives in buffer column x mod 320. The hardware scrolls the
graphics (ports 35h-37h) with its edge border on, and the game draws the
columns that come into view in the hidden border. Everything that moves is
a sprite; the scanner and the score are text. It runs at 8600h, loaded by
the WARDEN 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 0x35 scr_xlo;
__sfr __at 0x36 scr_xhi;
__sfr __at 0x37 scr_y;
__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_SHIFT 64
#define J_ESC 128
#define TOP_Y 34 /* the play area: below the scanner */
#define GROUND_Y 210 /* a humanoid's sprite stands here */
#define BOT_Y 214 /* lowest the ship goes */
/* sprites: lower numbers in front */
#define SP_SHIP 0
#define SP_LASER 1 /* 4 */
#define SP_SHOT 5 /* 4 */
#define SP_POOL 9 /* 23: enemies, humanoids, mines, explosions */
#define SP_END 32
#define NE 16 /* enemies */
#define NH 8 /* humanoids */
#define NM 6 /* mines */
#define NL 4 /* lasers */
#define NS 4 /* enemy shots */
#define NX 5 /* explosions */
#define E_NONE 0
#define E_LANDER 1
#define E_MUTANT 2
#define E_BOMBER 3
#define L_SEEK 0
#define L_DOWN 1
#define L_CARRY 2
#define H_WALK 0
#define H_HELD 1
#define H_FALL 2
#define H_GONE 3
/* ---------------------------------------------------------------- state */
/* not static: test-warden.py reads these from the link map */
u16 cam; /* world column at the screen's left */
u16 px; /* ship, world pixels */
u8 py;
u8 lives, bombs, wave, dead, inv, shield;
u16 score;
u8 etype[NE];
u16 ex[NE]; /* world x in 1/16 pixels */
u8 ey[NE];
static i16 vx; /* 1/16 pixels a frame */
static u8 pxf; /* sub-pixel */
static u8 facing; /* 0 right, 1 left */
static i16 off; /* ship's screen x */
static u8 dead_time, fire_cool, bomb_held, hyper_held;
static u16 next_extra;
static u8 estate[NE], etgt[NE], ecool[NE];
static i8 evx[NE], evy[NE];
static u8 ebase[NE];
static u16 hx[NH];
static u8 hy[NH], hstate[NH], hdir[NH], hvy[NH], hfrom[NH];
static u16 mx[NM];
static u8 my[NM];
static u16 mlife[NM];
static u16 lx[NL];
static u8 ly[NL], ldir[NL], llife[NL];
static u16 sx16[NS], sy16[NS];
static i16 svx[NS], svy[NS];
static u8 slife[NS];
static u16 xx[NX];
static u8 xy[NX], xt[NX];
static u8 pool_used, pool_last;
static u8 last_frame, quit, hud_dirty, scan_timer;
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; }
/* ---------------------------------------------------------- the planet */
static u16 wrapx(i16 x)
{
while (x < 0) x += WORLD;
while (x >= WORLD) x -= WORLD;
return (u16)x;
}
/* shortest signed distance from a to b round the planet */
static i16 dist(u16 a, u16 b)
{
i16 d = (i16)b - (i16)a;
if (d > WORLD / 2) d -= WORLD;
else if (d < -WORLD / 2) d += WORLD;
return d;
}
static i16 absd(i16 v) { return v < 0 ? -v : v; }
/* move a world x in 1/16 pixels by d, round the planet */
static u16 step16(u16 x, i16 d)
{
i16 v = (i16)x + d;
if (v < 0) v += WORLD * 16;
else if (v >= WORLD * 16) v -= WORLD * 16;
return (u16)v;
}
/* Draw world column x into its buffer column. */
static void draw_col(u16 x)
{
u16 bx = x;
u8 y, a, b, lo, hi, star_y, star_c = 0, c;
while (bx >= 320) bx -= 320;
a = mountain[x];
b = mountain[x + 1 < WORLD ? x + 1 : 0];
lo = a < b ? a : b;
hi = a < b ? b : a;
star_y = 0;
if (!((x * 37) & 3)) {
star_y = TOP_Y + (u8)((x * 71) % 110);
star_c = (x & 8) ? 15 : (x & 16) ? 14 : 7;
}
for (y = 0; y < 240; y++) {
c = 0;
if (y >= lo && y <= hi) c = 3;
else if (y == star_y) c = star_c;
else if (y == 236) c = 4; /* the planet's surface line */
gx_lo = (u8)bx; gx_hi = (u8)(bx >> 8); gy_p = y;
gpix = c;
}
}
static void set_scroll(void)
{
u16 b = cam;
while (b >= 320) b -= 320;
scr_xlo = (u8)b; scr_xhi = (u8)(b >> 8) | 2; scr_y = 0;
}
static void draw_all(void)
{
u16 i;
for (i = 0; i < 320; i++) draw_col(wrapx(cam + i));
}
/* Move the camera by d columns (at most 4 either way) and draw what comes
into view: those columns are in the hidden border until next frame. */
static void scroll_by(i16 d)
{
i16 k;
if (d > 0)
for (k = 0; k < d; k++) draw_col(wrapx(cam + 320 + k));
else
for (k = 0; k < -d; k++) draw_col(wrapx((i16)cam - 1 - k));
cam = wrapx((i16)cam + d);
}
/* -------------------------------------------------------------- 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; }
/* A world object at (wx, y) on screen gets the next pool sprite. */
static void pool_put(u16 wx, i16 y, u8 shape, u8 ctl)
{
i16 s = dist(cam, wx);
if (s < -15 || s > 319 || pool_used == SP_END - SP_POOL) return;
spr_at(SP_POOL + pool_used, s - 8, y - 8, shape, ctl);
pool_used++;
}
static void pool_end(void)
{
u8 i;
for (i = pool_used; i < pool_last; i++) spr_off(SP_POOL + i);
pool_last = pool_used;
pool_used = 0;
}
static void all_off(void) { u8 i; for (i = 0; i < 32; i++) spr_off(i); pool_last = 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++;
}
}
static u8 on_screen(u16 wx)
{
i16 s = dist(cam, wx);
return s >= 0 && s < 320;
}
static void boom(u16 wx, u8 y)
{
u8 i, best = 0;
for (i = 0; i < NX; i++) if (xt[i] < xt[best]) best = i;
xx[best] = wx; xy[best] = y; xt[best] = 20;
}
/* ------------------------------------------------------------------ HUD */
static void hud(void)
{
hud_dirty = 0;
at(1, 1); ink("0;93"); puts_("STAR WARDEN ");
ink("0;37"); puts_("SCORE "); ink("0;97"); putn(score, 6);
ink("0;37"); puts_(" SHIPS "); ink("0;97"); putn(lives, 0);
ink("0;37"); puts_(" BOMBS "); ink("0;97"); putn(bombs, 0);
ink("0;37"); puts_(" WAVE "); ink("0;97"); putn(wave, 0);
if (msg && msg_time) { ink("0;96"); puts_(" "); puts_(msg); }
puts_("\033[K");
ink("0");
}
/* The scanner: the whole planet in 80 columns and 3 rows, the ship in the
middle. */
static u8 scan_col(u16 wx)
{
i16 d = dist(px, wx) + WORLD / 2;
d >>= 4;
return 1 + (u8)(d > 79 ? 79 : d);
}
static u8 scan_row(u8 y) { return 2 + (y < 100 ? 0 : y < 160 ? 1 : 2); }
static void scanner(void)
{
u8 i;
for (i = 2; i <= 4; i++) { at(i, 1); puts_("\033[2K"); }
ink("0;34");
at(2, 33); putch('['); at(4, 33); putch('[');
at(2, 48); putch(']'); at(4, 48); putch(']');
ink("0;95");
for (i = 0; i < NH; i++)
if (hstate[i] != H_GONE) { at(scan_row(hy[i]), scan_col(hx[i])); putch('.'); }
for (i = 0; i < NE; i++) {
if (!etype[i]) continue;
ink(etype[i] == E_LANDER ? "0;92" : etype[i] == E_MUTANT ? "0;95" : "0;94");
at(scan_row(ey[i]), scan_col(ex[i] >> 4));
putch(etype[i] == E_BOMBER ? '#' : 'o');
}
if (!dead) { ink("0;93"); at(scan_row(py), scan_col(px)); putch(facing ? '<' : '>'); }
ink("0");
}
/* --------------------------------------------------------------- waves */
static u8 humans_left(void)
{
u8 i, n = 0;
for (i = 0; i < NH; i++) if (hstate[i] != H_GONE) n++;
return n;
}
static void place_humans(void)
{
u8 i;
for (i = 0; i < NH; i++) {
hx[i] = wrapx(i * (WORLD / NH) + (rnd() & 63));
hy[i] = GROUND_Y; hstate[i] = H_WALK; hdir[i] = rnd() & 1;
}
}
static void spawn(u8 type)
{
u8 i;
u16 x;
for (i = 0; i < NE && etype[i]; i++);
if (i == NE) return;
do x = rnd() % WORLD; while (absd(dist(px, x)) < 260);
etype[i] = type; ex[i] = x << 4;
ey[i] = TOP_Y + 6 + (rnd() & 63);
estate[i] = L_SEEK; etgt[i] = 255;
ecool[i] = 60 + (rnd() & 127);
evx[i] = (rnd() & 1) ? 8 : -8;
evy[i] = 0;
ebase[i] = ey[i];
}
static void start_wave(void)
{
u8 i, n;
for (i = 0; i < NE; i++) etype[i] = E_NONE;
for (i = 0; i < NM; i++) mlife[i] = 0;
for (i = 0; i < NS; i++) { slife[i] = 0; spr_off(SP_SHOT + i); }
if (wave % 5 == 1) place_humans(); /* the planet is restored */
n = 4 + 2 * wave; if (n > 12) n = 12;
for (i = 0; i < n; i++) spawn(E_LANDER);
n = wave < 4 ? wave - 1 : 3;
for (i = 0; i < n; i++) spawn(E_BOMBER);
if (!humans_left())
for (i = 0; i < NE; i++) if (etype[i] == E_LANDER) etype[i] = E_MUTANT;
}
static u8 enemies_left(void)
{
u8 i, n = 0;
for (i = 0; i < NE; i++) if (etype[i]) n++;
return n;
}
/* --------------------------------------------------------------- rules */
static void add_score(u16 n)
{
score += n;
if (score >= next_extra) {
next_extra += 10000;
lives++; bombs++;
say("BONUS SHIP");
}
hud_dirty = 1;
}
static void kill_ship(void)
{
u8 i;
if (dead || inv || shield) return;
dead = 1; dead_time = 100;
boom(px, py);
spr_off(SP_SHIP);
for (i = 0; i < NS; i++) { slife[i] = 0; spr_off(SP_SHOT + i); }
}
static void kill_enemy(u8 i)
{
u8 h;
if (etype[i] == E_LANDER && estate[i] == L_CARRY && etgt[i] < NH) {
h = etgt[i];
hstate[h] = H_FALL; hvy[h] = 0; hfrom[h] = hy[h];
}
add_score(etype[i] == E_BOMBER ? 250 : 150);
boom(ex[i] >> 4, ey[i]);
etype[i] = E_NONE;
}
static void enemy_fire(u8 i, u8 often)
{
u8 k;
i16 dx, dy, m;
if (ecool[i]) { ecool[i]--; return; }
ecool[i] = often + (rnd() & 63);
if (dead || !on_screen(ex[i] >> 4)) return;
for (k = 0; k < NS && slife[k]; k++);
if (k == NS) return;
dx = dist(ex[i] >> 4, px);
dy = (i16)py - ey[i];
m = absd(dx) > absd(dy) ? absd(dx) : absd(dy);
if (!m) return;
sx16[k] = ex[i]; sy16[k] = (u16)ey[i] << 4;
svx[k] = dx * 32 / m; svy[k] = dy * 32 / m;
slife[k] = 90;
}
static void enemies(void)
{
u8 i, h, best;
i16 d, bd;
u16 x;
for (i = 0; i < NE; i++) {
if (!etype[i]) continue;
x = ex[i] >> 4;
switch (etype[i]) {
case E_LANDER:
if (estate[i] == L_SEEK) {
ex[i] = step16(ex[i], evx[i]);
if (!(last_frame & 31)) ey[i] = ebase[i] + (rnd() & 15);
/* pick a humanoid nobody is after, and go for it when above it */
if (etgt[i] == 255) {
best = 255; bd = 2000;
for (h = 0; h < NH; h++) {
if (hstate[h] != H_WALK) continue;
d = absd(dist(x, hx[h]));
if (d < bd) { bd = d; best = h; }
}
etgt[i] = best;
} else if (hstate[etgt[i]] != H_WALK) etgt[i] = 255;
else if (absd(dist(x, hx[etgt[i]])) < 3) estate[i] = L_DOWN;
enemy_fire(i, 90);
} else if (estate[i] == L_DOWN) {
h = etgt[i];
if (h >= NH || hstate[h] != H_WALK) { estate[i] = L_SEEK; etgt[i] = 255; break; }
ex[i] = (u16)hx[h] << 4;
if (ey[i] + 12 < hy[h]) ey[i]++;
else { estate[i] = L_CARRY; hstate[h] = H_HELD; }
enemy_fire(i, 120);
} else {
h = etgt[i];
if (!(last_frame & 1)) ey[i]--;
hx[h] = ex[i] >> 4; hy[h] = ey[i] + 12;
if (ey[i] <= TOP_Y) {
hstate[h] = H_GONE;
etype[i] = E_MUTANT;
say(humans_left() ? "A HUMANOID IS LOST" : "THE PLANET IS LOST");
if (!humans_left())
for (h = 0; h < NE; h++) if (etype[h] == E_LANDER) etype[h] = E_MUTANT;
}
}
break;
case E_MUTANT:
d = dist(x, px);
ex[i] = step16(ex[i], d > 0 ? 22 : -22);
if (ey[i] < py) ey[i]++; else if (ey[i] > py) ey[i]--;
if (!(rnd() & 3)) ey[i] += (rnd() & 1) ? 2 : -2;
if (ey[i] < TOP_Y) ey[i] = TOP_Y;
if (ey[i] > BOT_Y) ey[i] = BOT_Y;
enemy_fire(i, 40);
break;
case E_BOMBER:
ex[i] = step16(ex[i], evx[i]);
ey[i] = ebase[i] + ((last_frame >> 2) & 15);
if (ecool[i]) ecool[i]--;
else {
ecool[i] = 70 + (rnd() & 63);
for (h = 0; h < NM && mlife[h]; h++);
if (h < NM) { mx[h] = x; my[h] = ey[i]; mlife[h] = 400; }
}
break;
}
if (!etype[i]) continue;
x = ex[i] >> 4;
if (!dead && absd(dist(x, px)) < 12 && absd((i16)ey[i] - py) < 8) {
kill_enemy(i);
kill_ship();
continue;
}
pool_put(x, ey[i], etype[i] == E_LANDER ? S_LANDER : etype[i] == E_MUTANT ? S_MUTANT : S_BOMBER, 1);
}
}
static void humans(void)
{
u8 i;
for (i = 0; i < NH; i++) {
switch (hstate[i]) {
case H_WALK:
if (!(last_frame & 7)) {
hx[i] = wrapx((i16)hx[i] + (hdir[i] ? 1 : -1));
if (!(rnd() & 31)) hdir[i] ^= 1;
}
break;
case H_FALL:
if (!(last_frame & 3) && hvy[i] < 4) hvy[i]++;
hy[i] += hvy[i];
if (!dead && absd(dist(hx[i], px)) < 12 && absd((i16)hy[i] - py) < 12) {
hstate[i] = H_WALK; hy[i] = GROUND_Y;
add_score(500); say("HUMANOID SAVED");
break;
}
if (hy[i] >= GROUND_Y) {
hy[i] = GROUND_Y;
if (GROUND_Y - hfrom[i] > 90) { hstate[i] = H_GONE; boom(hx[i], GROUND_Y); say("A HUMANOID FELL"); }
else { hstate[i] = H_WALK; add_score(250); }
}
break;
}
if (hstate[i] != H_GONE) pool_put(hx[i], hy[i], S_HUMAN, 1);
}
}
static void mines_and_booms(void)
{
u8 i;
for (i = 0; i < NM; i++) {
if (!mlife[i]) continue;
mlife[i]--;
if (!dead && absd(dist(mx[i], px)) < 8 && absd((i16)my[i] - py) < 6) {
mlife[i] = 0; boom(mx[i], my[i]); kill_ship(); continue;
}
pool_put(mx[i], my[i], S_MINE, 1);
}
for (i = 0; i < NX; i++) {
if (!xt[i]) continue;
xt[i]--;
pool_put(xx[i], xy[i], dead && xx[i] == px ? S_SHIPBOOM : (xt[i] & 4) ? S_BOOM1 : S_BOOM2, 1);
}
}
static void shots(void)
{
u8 i;
i16 s;
for (i = 0; i < NS; i++) {
if (!slife[i]) continue;
slife[i]--;
sx16[i] = step16(sx16[i], svx[i]);
sy16[i] += svy[i];
if ((sy16[i] >> 4) < TOP_Y || (sy16[i] >> 4) > 236) slife[i] = 0;
if (!dead && slife[i] && absd(dist(sx16[i] >> 4, px)) < 7 && absd((i16)(sy16[i] >> 4) - py) < 5) {
slife[i] = 0; kill_ship();
}
if (!slife[i]) { spr_off(SP_SHOT + i); continue; }
s = dist(cam, sx16[i] >> 4);
spr_at(SP_SHOT + i, s - 8, (i16)(sy16[i] >> 4) - 8, S_SHOT, 1);
}
}
static void lasers(u8 j)
{
u8 i, k;
i16 s;
if ((j & J_FIRE) && !fire_cool && !dead) {
for (i = 0; i < NL && llife[i]; i++);
if (i < NL) {
lx[i] = wrapx((i16)px + (facing ? -14 : 14)); ly[i] = py + 1; ldir[i] = facing; llife[i] = 26;
fire_cool = 7;
}
}
if (fire_cool) fire_cool--;
for (i = 0; i < NL; i++) {
if (!llife[i]) continue;
llife[i]--;
lx[i] = wrapx((i16)lx[i] + (ldir[i] ? -12 : 12));
for (k = 0; k < NE; k++)
if (etype[k] && absd(dist(lx[i], ex[k] >> 4)) < 12 && absd((i16)ey[k] - ly[i]) < 7) {
kill_enemy(k); llife[i] = 0; break;
}
for (k = 0; k < NM && llife[i]; k++)
if (mlife[k] && absd(dist(lx[i], mx[k])) < 10 && absd((i16)my[k] - ly[i]) < 5) {
mlife[k] = 0; boom(mx[k], my[k]); add_score(50); llife[i] = 0;
}
if (!llife[i] || !on_screen(lx[i])) { llife[i] = 0; spr_off(SP_LASER + i); continue; }
s = dist(cam, lx[i]);
spr_at(SP_LASER + i, s - 8, (i16)ly[i] - 8, S_LASER, 1);
}
}
static void smart_bomb(void)
{
u8 i;
if (!bombs || dead) return;
bombs--;
for (i = 0; i < NE; i++) if (etype[i] && on_screen(ex[i] >> 4)) kill_enemy(i);
for (i = 0; i < NM; i++) if (mlife[i] && on_screen(mx[i])) { mlife[i] = 0; boom(mx[i], my[i]); }
for (i = 0; i < NS; i++) { slife[i] = 0; spr_off(SP_SHOT + i); }
say("SMART BOMB");
}
static void hyperspace(void)
{
if (dead) return;
px = rnd() % WORLD;
py = TOP_Y + 10 + (rnd() % (BOT_Y - TOP_Y - 20));
vx = 0;
off = facing ? 208 : 104;
cam = wrapx((i16)px - off);
set_scroll();
draw_all();
inv = 30;
}
static void fly(u8 j)
{
i16 target, want, d;
if (dead) return;
if (j & J_LEFT) facing = 1;
if (j & J_RIGHT) facing = 0;
if (j & (J_LEFT | J_RIGHT)) {
vx += facing ? -5 : 5;
if (vx > 64) vx = 64;
if (vx < -64) vx = -64;
} else if (vx > 0) vx -= vx > 2 ? 2 : vx;
else if (vx < 0) vx += vx < -2 ? 2 : -vx;
if ((j & J_UP) && py > TOP_Y + 4) py -= 2;
if ((j & J_DOWN) && py < BOT_Y) py += 2;
/* move the ship */
d = vx + pxf;
px = wrapx((i16)px + (d >> 4));
pxf = d & 15;
/* the camera keeps the ship a third of the way in from behind, and
swings across when it turns, 4 columns a frame at most */
target = facing ? 208 : 104;
if (off < target) off += off + 3 < target ? 3 : target - off;
else if (off > target) off -= off - 3 > target ? 3 : off - target;
want = (i16)px - off;
d = dist(cam, wrapx(want));
if (d > 4) d = 4;
if (d < -4) d = -4;
scroll_by(d);
off = dist(cam, px);
spr_at(SP_SHIP, off - 8, (i16)py - 8, S_SHIP, ((inv & 4) ? 0 : 1) | (facing ? 2 : 0));
}
/* ------------------------------------------------------------- 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 u8 title(void)
{
clear_all();
puts_("\033[0m\033[2J");
cam = 0;
set_scroll();
draw_all();
spr_at(0, 150, 150, S_SHIP, 1);
spr_at(1, 196, 120, S_LANDER, 1);
spr_at(2, 230, 140, S_MUTANT, 1);
spr_at(3, 110, 120, S_BOMBER, 1);
spr_at(4, 196, 202, S_HUMAN, 1);
ink("1;93"); centre(7, "S T A R W A R D E N");
ink("0;97");
centre(10, "Landers are stealing the humanoids from the planet.");
centre(11, "Shoot them, and catch the humanoids they drop.");
centre(12, "A lander that carries one to the top becomes a mutant.");
centre(15, "LEFT / RIGHT fly UP / DOWN climb and dive SPACE fire");
centre(16, "SHIFT smart bomb ENTER hyperspace P pause ESC give up");
ink("1;96"); centre(29, "PRESS SPACE TO BEGIN");
ink("0");
return wait_fire();
}
static void play(void)
{
u8 j, i;
lives = 3; bombs = 3; score = 0; wave = 1; next_extra = 10000;
for (i = 0; i < NX; i++) xt[i] = 0;
for (i = 0; i < NL; i++) llife[i] = 0;
px = 160; py = 120; vx = 0; pxf = 0; facing = 0; off = 104;
clear_all();
puts_("\033[2J");
cam = wrapx((i16)px - off);
set_scroll();
draw_all();
start_wave();
dead = 0; inv = 60; fire_cool = 0; bomb_held = 1; hyper_held = 1;
msg = 0; say("DEFEND THE HUMANOIDS");
hud();
for (;;) {
wait_frame();
set_scroll();
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 (j & J_SHIFT) { if (!bomb_held) smart_bomb(); bomb_held = 1; } else bomb_held = 0;
if (j & J_ENTER) { if (!hyper_held) hyperspace(); hyper_held = 1; } else hyper_held = 0;
fly(j);
lasers(j);
enemies();
humans();
mines_and_booms();
shots();
pool_end();
if (inv) inv--;
if (msg_time && !--msg_time) hud_dirty = 1;
if (hud_dirty) hud();
if (!scan_timer--) { scan_timer = 9; scanner(); }
if (dead && !--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;
}
dead = 0; inv = 120; vx = 0;
hud_dirty = 1;
}
if (!enemies_left() && !dead) {
i = humans_left();
add_score((u16)i * 100 * (wave < 5 ? wave : 5));
say("WAVE CLEARED");
hud();
for (j = 0; j < 120; j++) { wait_frame(); set_scroll(); drain_keys(); }
wave++;
start_wave();
}
}
}
void main(void)
{
rnd_state = 0x2084 ^ 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;
}