The terminal comes up (and resets with ESC c) in DECCOLM's 132x64 mode; term_ctrl's WIDE_BOOT parameter selects 80x32 instead. Port 0 bit 2 tells software which width is showing: BASIC's line editor wraps at it, and the games, which play at 80 columns, put 132 back when they end. scripts/sim.sh wide checks the start-up; the other checks still start at 80x32. Manual, README and terminfo updated. send-hex.py is executable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
699 lines
22 KiB
C
699 lines
22 KiB
C
/* Bastion Raid: an isometric scrolling shooter for the Basys 3 RISC-V computer.
|
|
|
|
Fly over a fortress that scrolls diagonally towards you. Climb over low
|
|
walls, fly low through the doorways in tall ones, shoot turrets before they
|
|
shoot you, and shoot fuel tanks to fill up. Between fortresses, fighters
|
|
come at you through space. The reactor core waits at the far end. Your
|
|
shadow on the floor and the altimeter show how high you are.
|
|
|
|
The world is drawn into the graphics layer, which the hardware scrolls
|
|
(ports 35h-37h): each frame the picture moves two pixels left and one
|
|
down, and only the two new columns and the new row are drawn, pixel by
|
|
pixel from the world's description (world()). Everything that moves is a
|
|
sprite. It runs at A200h, loaded by the RAID program in BASIC.
|
|
|
|
World coordinates: a pixel (WX, WY) of the unscrolled world has
|
|
s = WX + 2 WY (across the flight path) and d = 2 WY - WX (along it, growing
|
|
towards the viewer). Tiles are 32 x 32 in (s, d): tile i = s >> 5,
|
|
j = d >> 5, and level row r = -j. The ship flies towards smaller d. */
|
|
|
|
typedef unsigned char u8;
|
|
typedef signed char i8;
|
|
typedef unsigned short u16;
|
|
typedef short i16;
|
|
|
|
#include "basys3.h"
|
|
|
|
#define con_stat IO8(0x00)
|
|
#define con_data IO8(0x01)
|
|
#define gx_lo IO8(0x30)
|
|
#define gx_hi IO8(0x31)
|
|
#define gy_p IO8(0x32)
|
|
#define gpix IO8(0x33)
|
|
#define gfill IO8(0x34)
|
|
#define scr_xlo IO8(0x35)
|
|
#define scr_xhi IO8(0x36)
|
|
#define scr_y IO8(0x37)
|
|
#define sp_sel IO8(0x70)
|
|
#define sp_xlo IO8(0x71)
|
|
#define sp_xhi IO8(0x72)
|
|
#define sp_y IO8(0x73)
|
|
#define sp_shape IO8(0x74)
|
|
#define sp_ctl IO8(0x75)
|
|
#define sp_load IO8(0x76)
|
|
#define sp_data IO8(0x77)
|
|
#define frame_ctr IO8(0x78)
|
|
#define joy IO8(0x88)
|
|
|
|
#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
|
|
|
|
/* sprites: lower numbers in front */
|
|
#define SP_SHIP 0
|
|
#define SP_SHOT 1 /* 3 */
|
|
#define SP_FIGHT 4 /* 4 */
|
|
#define SP_SHELL 8 /* 4 */
|
|
#define SP_BOOM 12 /* 3 */
|
|
#define SP_OBJ 15 /* 12 */
|
|
#define SP_SHADOW 27
|
|
|
|
#define NSHOT 3
|
|
#define NFIGHT 4
|
|
#define NSHELL 4
|
|
#define NBOOM 3
|
|
#define NOBJ 12
|
|
|
|
#define SHIP_D 128 /* the ship's ground point is 4 tiles nearer than the screen centre */
|
|
#define MAX_ALT 48
|
|
#define BACK_H 24 /* the fortress's back wall */
|
|
#define FRONT_H 20 /* and its front face, below the floor */
|
|
|
|
/* ---------------------------------------------------------------- state */
|
|
static i16 cx, cy; /* world pixel at the screen's top left */
|
|
static u16 fbx; /* the same, in the 320 x 240 buffer */
|
|
static u8 fby;
|
|
i16 cam_d; /* d at the screen's centre */
|
|
|
|
i16 ship_s, ship_d;
|
|
u8 alt, lives, fuel, level, inv, dead;
|
|
u8 shield; /* tests only: shells, fighters and objects can't hit */
|
|
static u8 fuel_tick, fire_held, dead_time;
|
|
static u16 score;
|
|
|
|
static i16 shot_s[NSHOT], shot_d[NSHOT];
|
|
static u8 shot_alt[NSHOT], shot_life[NSHOT];
|
|
static i16 fight_s[NFIGHT], fight_d[NFIGHT];
|
|
static u8 fight_alt[NFIGHT], fight_on[NFIGHT];
|
|
static i16 shell_s[NSHELL], shell_d[NSHELL];
|
|
static u8 shell_life[NSHELL];
|
|
static i16 boom_s[NBOOM], boom_d[NBOOM];
|
|
static u8 boom_alt[NBOOM], boom_t[NBOOM];
|
|
static i16 obj_s[NOBJ], obj_d[NOBJ];
|
|
static u8 obj_type[NOBJ], obj_on[NOBJ], obj_cool[NOBJ], obj_hits[NOBJ];
|
|
static u16 next_obj;
|
|
static u8 fight_timer;
|
|
|
|
static u8 last_frame, quit, hud_dirty;
|
|
static u8 shown_fuel, shown_alt;
|
|
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;
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- world */
|
|
static u8 row_info(i16 r)
|
|
{
|
|
return (r >= 0 && r < LEVEL_ROWS) ? level_rows[r] : 0;
|
|
}
|
|
|
|
/* The colour of world point (s, d). */
|
|
static u8 world(i16 s, i16 d)
|
|
{
|
|
i16 jw, jlo, k2, sb, db;
|
|
u8 ri, k, ib, g, h;
|
|
|
|
/* walls across the path: the face of the wall at the far edge of row
|
|
-jw covers the 2H units of d in front of the line d = 32 jw. The
|
|
nearest wall is tried first. */
|
|
jlo = (d >> 5) + 1;
|
|
for (jw = (d + 2 * TALL_H) >> 5; jw >= jlo; jw--) {
|
|
ri = row_info(-jw);
|
|
if (!(ri & R_WALL)) continue;
|
|
k2 = (jw << 5) - d;
|
|
h = (ri & R_DOOR) ? TALL_H : LOW_H;
|
|
k = (u8)(k2 >> 1);
|
|
if (k > h) continue;
|
|
sb = s + k2;
|
|
if (sb < 0 || sb >= 32 * BW) continue;
|
|
ib = (u8)(sb >> 5);
|
|
if (ri & R_DOOR) {
|
|
g = (ri >> 3) & 15;
|
|
if (ib >= g && ib < g + 2) {
|
|
if (k < DOOR_H) continue; /* the doorway */
|
|
if (k < DOOR_H + 3) return 14; /* its lintel */
|
|
}
|
|
}
|
|
if (k >= h - 1) return (ri & R_DOOR) ? 15 : 11; /* the top */
|
|
if (!(k & 7) || ((sb + ((k & 8) << 1)) & 31) < 2) return 8;
|
|
return (ri & R_DOOR) ? 7 : 3;
|
|
}
|
|
|
|
/* the fortress's back wall, rising from its far side (s = 0) */
|
|
if (s < 0 && s >= -2 * BACK_H) {
|
|
db = d - s;
|
|
if (row_info(-(db >> 5)) & R_FORT) {
|
|
k = (u8)((-s) >> 1);
|
|
if (k >= BACK_H - 1) return 7;
|
|
return (k & 3) ? 8 : 0;
|
|
}
|
|
}
|
|
/* its front face, below the near side (s = 32 BW) */
|
|
if (s >= 32 * BW && s < 32 * BW + 2 * FRONT_H) {
|
|
k2 = s - 32 * BW;
|
|
db = d - k2;
|
|
if (row_info(-(db >> 5)) & R_FORT)
|
|
return ((db & 31) < 2) ? 0 : 4;
|
|
}
|
|
/* the floor */
|
|
if (s >= 0 && s < 32 * BW && (row_info(-(d >> 5)) & R_FORT)) {
|
|
if ((s & 31) < 2 || (d & 31) < 2) return 0;
|
|
if ((((s >> 5) + (d >> 5)) & 3) == 0 && (s & 31) > 13 && (s & 31) < 18 &&
|
|
(d & 31) > 13 && (d & 31) < 18) return 7;
|
|
return 8;
|
|
}
|
|
/* space */
|
|
if (((s * 5 + d * 3) & 255) == 7 && !((s ^ d) & 0x60))
|
|
return (s & 16) ? 15 : 7;
|
|
return 0;
|
|
}
|
|
|
|
/* Draw screen columns xs and xs + 1 (xs even) from the world. */
|
|
static void draw_cols(u16 xs)
|
|
{
|
|
i16 wx = cx + xs, s = wx + 2 * cy, d = 2 * cy - wx;
|
|
u16 fx = fbx + xs;
|
|
u8 y, fy = fby, c0, c1, flo, fhi;
|
|
if (fx >= 320) fx -= 320;
|
|
flo = (u8)fx; fhi = (u8)(fx >> 8);
|
|
for (y = 0; y < 240; y++) {
|
|
c0 = world(s, d);
|
|
c1 = world(s + 1, d - 1);
|
|
gx_lo = flo; gx_hi = fhi; gy_p = fy;
|
|
gpix = c0; gpix = c1;
|
|
s += 2; d += 2;
|
|
if (++fy == 240) fy = 0;
|
|
}
|
|
}
|
|
|
|
/* Draw screen row ys from the world. */
|
|
static void draw_row(u8 ys)
|
|
{
|
|
i16 wy = cy + ys, s = cx + 2 * wy, d = 2 * wy - cx;
|
|
u16 x, fx = fbx;
|
|
u8 fy = fby + ys;
|
|
if (fy >= 240) fy -= 240;
|
|
gx_lo = (u8)fx; gx_hi = (u8)(fx >> 8); gy_p = fy;
|
|
for (x = 0; x < 320; x++) {
|
|
gpix = world(s, d);
|
|
s++; d--;
|
|
if (++fx == 320) { fx = 0; gx_lo = 0; gx_hi = 0; gy_p = fy; }
|
|
}
|
|
}
|
|
|
|
/* Show the camera's view. The border bit hides the screen's edges, where
|
|
advance() draws the next columns and row while this frame is on screen. */
|
|
static void set_scroll(void)
|
|
{
|
|
scr_xlo = (u8)fbx; scr_xhi = (u8)(fbx >> 8) | 2; scr_y = fby;
|
|
}
|
|
|
|
/* Put the camera at level row r (the screen centre), and draw it all. */
|
|
static void camera_at(i16 r)
|
|
{
|
|
u8 y;
|
|
/* centre: s = 16 BW (the fortress's middle), d = -32 r + 16 */
|
|
i16 sc = 16 * BW, dc = -32 * r + 16;
|
|
/* WX = (s - d) / 2, WY = (s + d) / 4, both even-friendly */
|
|
cx = ((sc - dc) >> 1) - 160;
|
|
cy = ((sc + dc) >> 2) - 120;
|
|
cx &= ~1;
|
|
cam_d = 2 * (cy + 120) - (cx + 160);
|
|
fbx = (u16)(cx % 320); if ((i16)fbx < 0) fbx += 320;
|
|
{ i16 t = cy % 240; if (t < 0) t += 240; fby = (u8)t; }
|
|
for (y = 0; y < 240; y++) draw_row(y);
|
|
}
|
|
|
|
/* Fly on: the camera moves 2 right and 1 up; draw what comes into view. */
|
|
static void advance(void)
|
|
{
|
|
cx += 2; cy -= 1;
|
|
cam_d -= 4;
|
|
fbx += 2; if (fbx >= 320) fbx -= 320;
|
|
fby = fby ? fby - 1 : 239;
|
|
draw_cols(318);
|
|
draw_row(0);
|
|
}
|
|
|
|
/* ------------------------------------------------------------- sprites */
|
|
static void spr_place(u8 n, i16 s, i16 d, u8 up, u8 shape, u8 ctl)
|
|
{
|
|
i16 x = ((s - d) >> 1) - cx - 8, y = ((s + d) >> 2) - cy - 8 - up;
|
|
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); }
|
|
|
|
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 i16 absd(i16 v) { return v < 0 ? -v : v; }
|
|
|
|
static void boom(i16 s, i16 d, u8 up)
|
|
{
|
|
u8 i, best = 0;
|
|
for (i = 0; i < NBOOM; i++) if (boom_t[i] < boom_t[best]) best = i;
|
|
boom_s[best] = s; boom_d[best] = d; boom_alt[best] = up; boom_t[best] = 24;
|
|
}
|
|
|
|
static void say(const char *s) { msg = s; msg_time = 120; hud_dirty = 1; }
|
|
|
|
/* ------------------------------------------------------------------ HUD */
|
|
static void bar(u8 n, u8 of)
|
|
{
|
|
u8 i;
|
|
putch('[');
|
|
for (i = 0; i < of; i++) putch(i < n ? '#' : '-');
|
|
putch(']');
|
|
}
|
|
|
|
static void hud(void)
|
|
{
|
|
hud_dirty = 0;
|
|
shown_fuel = fuel; shown_alt = alt / 6;
|
|
at(1, 1); ink("0;93"); puts_("BASTION RAID ");
|
|
ink("0;37"); puts_("SCORE "); ink("0;97"); putn(score, 6);
|
|
ink("0;37"); puts_(" LEVEL "); ink("0;97"); putn(level, 0);
|
|
ink("0;37"); puts_(" SHIPS "); ink("0;97"); putn(lives, 0);
|
|
puts_("\033[K");
|
|
at(2, 1); ink(fuel < 25 ? "0;91" : "0;37"); puts_("FUEL "); bar((fuel + 9) / 10, 10);
|
|
ink("0;37"); puts_(" ALT "); ink("0;96"); bar(shown_alt + 1, 9);
|
|
if (msg && msg_time) { ink("0;93"); puts_(" "); puts_(msg); }
|
|
puts_("\033[K");
|
|
ink("0");
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- rules */
|
|
/* Does the wall at the far edge of row r stop something at s, height a? */
|
|
static u8 wall_stops(i16 r, i16 s, u8 a, u8 half)
|
|
{
|
|
u8 ri = row_info(r), g;
|
|
if (!(ri & R_WALL) || s < -half || s >= 32 * BW + half) return 0;
|
|
if (ri & R_LOW) return a <= LOW_H + 2;
|
|
if (a > TALL_H) return 0;
|
|
g = (ri >> 3) & 15;
|
|
return !(s - half >= 32 * g && s + half < 32 * g + 64 && a + 8 < DOOR_H);
|
|
}
|
|
|
|
static void crash(u8 wall)
|
|
{
|
|
u8 i;
|
|
if (inv || dead || (shield && !wall)) return;
|
|
boom(ship_s, ship_d, alt);
|
|
spr_off(SP_SHIP); spr_off(SP_SHADOW);
|
|
dead = 1; dead_time = 90;
|
|
for (i = 0; i < NSHELL; i++) { shell_life[i] = 0; spr_off(SP_SHELL + i); }
|
|
}
|
|
|
|
static void objects_in(void)
|
|
{
|
|
const u8 *o;
|
|
i16 r, ahead = -((cam_d - 12 * 32) >> 5);
|
|
u8 k;
|
|
while (next_obj < N_OBJS) {
|
|
o = level_objs + next_obj * 4;
|
|
r = o[0] | o[1] << 8;
|
|
if (r > ahead) break;
|
|
for (k = 0; k < NOBJ && obj_on[k]; k++);
|
|
if (k == NOBJ) break;
|
|
obj_on[k] = 1; obj_type[k] = o[2];
|
|
obj_s[k] = 32 * o[3] + 16; obj_d[k] = -32 * r + 16;
|
|
obj_cool[k] = 40 + (rnd() & 63);
|
|
obj_hits[k] = o[2] == O_CORE ? 4 : 1;
|
|
next_obj++;
|
|
}
|
|
}
|
|
|
|
static void hit_object(u8 k)
|
|
{
|
|
if (--obj_hits[k]) { boom(obj_s[k], obj_d[k], 4); return; }
|
|
obj_on[k] = 0;
|
|
spr_off(SP_OBJ + k);
|
|
boom(obj_s[k], obj_d[k], 0);
|
|
switch (obj_type[k]) {
|
|
case O_TURRET: score += 100; break;
|
|
case O_TANK: score += 150; fuel = fuel > 75 ? 100 : fuel + 25; say("FUEL"); break;
|
|
case O_RADAR: score += 300; break;
|
|
case O_CORE: score += 2000; say("REACTOR DESTROYED!"); break;
|
|
}
|
|
hud_dirty = 1;
|
|
}
|
|
|
|
static void objects(void)
|
|
{
|
|
static const u8 shapes[5] = { 0, S_TURRET, S_TANK, S_RADAR, S_CORE };
|
|
u8 k, i;
|
|
for (k = 0; k < NOBJ; k++) {
|
|
if (!obj_on[k]) continue;
|
|
if (obj_d[k] > cam_d + 8 * 32) { obj_on[k] = 0; spr_off(SP_OBJ + k); continue; }
|
|
spr_place(SP_OBJ + k, obj_s[k], obj_d[k], 4, shapes[obj_type[k]], 1);
|
|
if (!dead && alt < 12 && absd(obj_s[k] - ship_s) < 16 && absd(obj_d[k] - ship_d) < 16) {
|
|
hit_object(k); crash(0); continue;
|
|
}
|
|
if (obj_type[k] == O_TURRET && !dead && obj_d[k] < ship_d && obj_d[k] > ship_d - 8 * 32) {
|
|
if (obj_cool[k]) obj_cool[k]--;
|
|
else {
|
|
for (i = 0; i < NSHELL && shell_life[i]; i++);
|
|
if (i < NSHELL) {
|
|
shell_s[i] = obj_s[k]; shell_d[i] = obj_d[k]; shell_life[i] = 70;
|
|
}
|
|
obj_cool[k] = (level > 3 ? 40 : 100 - 20 * level) + (rnd() & 63);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static void shells(void)
|
|
{
|
|
u8 i;
|
|
for (i = 0; i < NSHELL; i++) {
|
|
if (!shell_life[i]) continue;
|
|
shell_life[i]--;
|
|
shell_d[i] += 6;
|
|
if (shell_s[i] < ship_s) shell_s[i]++; else if (shell_s[i] > ship_s) shell_s[i]--;
|
|
/* moving towards the viewer, it crosses the far edge of row -(d >> 5) */
|
|
if (!shell_life[i] || (((shell_d[i] - 6) >> 5) != (shell_d[i] >> 5) &&
|
|
wall_stops(-(shell_d[i] >> 5), shell_s[i], 10, 2))) {
|
|
shell_life[i] = 0; spr_off(SP_SHELL + i); continue;
|
|
}
|
|
spr_place(SP_SHELL + i, shell_s[i], shell_d[i], 10, S_SHELL, 1);
|
|
if (!dead && absd(shell_s[i] - ship_s) < 12 && absd(shell_d[i] - ship_d) < 12 && alt < 22) {
|
|
shell_life[i] = 0; spr_off(SP_SHELL + i);
|
|
crash(0);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void fighters(void)
|
|
{
|
|
u8 i, k;
|
|
if (!dead && (row_info(-(cam_d >> 5) + 4) & R_FIGHTERS) && !--fight_timer) {
|
|
fight_timer = (level > 3 ? 25 : 55 - 10 * level) + (rnd() & 31);
|
|
for (i = 0; i < NFIGHT && fight_on[i]; i++);
|
|
if (i < NFIGHT) {
|
|
fight_on[i] = 1;
|
|
fight_s[i] = 24 + (rnd() % (32 * BW - 48));
|
|
fight_d[i] = cam_d - 7 * 32;
|
|
fight_alt[i] = 6 + (rnd() % 40);
|
|
}
|
|
}
|
|
for (i = 0; i < NFIGHT; i++) {
|
|
if (!fight_on[i]) continue;
|
|
fight_d[i] += 6;
|
|
if (!(last_frame & 3)) {
|
|
if (fight_alt[i] < alt) fight_alt[i]++; else if (fight_alt[i] > alt) fight_alt[i]--;
|
|
}
|
|
if (fight_d[i] > cam_d + 8 * 32) { fight_on[i] = 0; spr_off(SP_FIGHT + i); continue; }
|
|
spr_place(SP_FIGHT + i, fight_s[i], fight_d[i], fight_alt[i], S_FIGHTER, 1);
|
|
if (!dead && absd(fight_s[i] - ship_s) < 16 && absd(fight_d[i] - ship_d) < 16 &&
|
|
absd(fight_alt[i] - alt) < 10) {
|
|
fight_on[i] = 0; spr_off(SP_FIGHT + i);
|
|
boom(fight_s[i], fight_d[i], fight_alt[i]);
|
|
crash(0);
|
|
}
|
|
for (k = 0; k < NSHOT; k++)
|
|
if (shot_life[k] && absd(shot_s[k] - fight_s[i]) < 16 && absd(shot_d[k] - fight_d[i]) < 20 &&
|
|
absd(shot_alt[k] - fight_alt[i]) < 12) {
|
|
shot_life[k] = 0; spr_off(SP_SHOT + k);
|
|
fight_on[i] = 0; spr_off(SP_FIGHT + i);
|
|
boom(fight_s[i], fight_d[i], fight_alt[i]);
|
|
score += 200; hud_dirty = 1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void shots(u8 j)
|
|
{
|
|
u8 i, k;
|
|
i16 od;
|
|
if (!(j & J_FIRE)) fire_held = 0;
|
|
else if (!fire_held && !dead) {
|
|
fire_held = 1;
|
|
for (i = 0; i < NSHOT && shot_life[i]; i++);
|
|
if (i < NSHOT) {
|
|
shot_s[i] = ship_s; shot_d[i] = ship_d - 12; shot_alt[i] = alt; shot_life[i] = 28;
|
|
}
|
|
}
|
|
for (i = 0; i < NSHOT; i++) {
|
|
if (!shot_life[i]) continue;
|
|
od = shot_d[i];
|
|
shot_d[i] -= 12;
|
|
shot_life[i]--;
|
|
if ((od >> 5) != (shot_d[i] >> 5) && wall_stops(-(od >> 5), shot_s[i], shot_alt[i], 2)) {
|
|
shot_life[i] = 0;
|
|
boom(shot_s[i], (od >> 5) << 5, shot_alt[i]);
|
|
}
|
|
if (shot_alt[i] < 16)
|
|
for (k = 0; k < NOBJ && shot_life[i]; k++)
|
|
if (obj_on[k] && absd(obj_s[k] - shot_s[i]) < 16 && absd(obj_d[k] - shot_d[i]) < 16) {
|
|
shot_life[i] = 0;
|
|
hit_object(k);
|
|
}
|
|
if (!shot_life[i]) { spr_off(SP_SHOT + i); continue; }
|
|
spr_place(SP_SHOT + i, shot_s[i], shot_d[i], shot_alt[i], S_SHOT, 1);
|
|
}
|
|
}
|
|
|
|
static void booms(void)
|
|
{
|
|
u8 i;
|
|
for (i = 0; i < NBOOM; i++) {
|
|
if (!boom_t[i]) continue;
|
|
boom_t[i]--;
|
|
if (!boom_t[i]) { spr_off(SP_BOOM + i); continue; }
|
|
spr_place(SP_BOOM + i, boom_s[i], boom_d[i], boom_alt[i], (boom_t[i] & 4) ? S_BOOM1 : S_BOOM2, 1);
|
|
}
|
|
}
|
|
|
|
static void fly(u8 j)
|
|
{
|
|
i16 od = ship_d;
|
|
if (dead) return;
|
|
if (j & J_LEFT) ship_s -= 3;
|
|
if (j & J_RIGHT) ship_s += 3;
|
|
if (ship_s < 16) ship_s = 16;
|
|
if (ship_s > 32 * BW - 16) ship_s = 32 * BW - 16;
|
|
if ((j & J_UP) && alt < MAX_ALT) alt++;
|
|
if ((j & J_DOWN) && alt > 2) alt--;
|
|
ship_d = cam_d + SHIP_D;
|
|
if ((od >> 5) != (ship_d >> 5) && !inv && wall_stops(-(od >> 5), ship_s, alt, 10)) {
|
|
say("YOU HIT THE WALL");
|
|
crash(1);
|
|
return;
|
|
}
|
|
if (++fuel_tick >= 24) {
|
|
fuel_tick = 0;
|
|
if (fuel) fuel--;
|
|
if (!fuel) { say("OUT OF FUEL"); crash(0); return; }
|
|
if (fuel != shown_fuel) hud_dirty = 1;
|
|
}
|
|
if (alt / 6 != shown_alt) hud_dirty = 1;
|
|
spr_place(SP_SHIP, ship_s, ship_d, alt, S_SHIP, (inv & 4) ? 0 : 1);
|
|
if ((row_info(-(ship_d >> 5)) & R_FORT) && ship_s >= 0 && ship_s < 32 * BW)
|
|
spr_place(SP_SHADOW, ship_s, ship_d, 0, S_SHADOW, 1);
|
|
else
|
|
spr_off(SP_SHADOW);
|
|
}
|
|
|
|
/* ------------------------------------------------------------- screens */
|
|
static void clear_all(void)
|
|
{
|
|
gfill = 0; /* also sprites off, scroll to 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");
|
|
camera_at(12);
|
|
set_scroll();
|
|
ship_s = 16 * BW; ship_d = cam_d + SHIP_D; alt = 24;
|
|
spr_place(SP_SHIP, ship_s, ship_d, alt, S_SHIP, 1);
|
|
ink("1;93"); centre(5, "B A S T I O N R A I D");
|
|
ink("0;97");
|
|
centre(8, "Fly over the fortress and destroy its reactor core.");
|
|
centre(10, "UP / DOWN climb and dive LEFT / RIGHT steer SPACE fire");
|
|
centre(12, "Climb over low walls. Fly low through the doors in tall ones.");
|
|
centre(13, "Your shadow and the altimeter show your height.");
|
|
centre(14, "Shoot fuel tanks for fuel. Turrets and fighters shoot back.");
|
|
centre(16, "P pauses. ESC gives up.");
|
|
ink("1;96"); centre(28, "PRESS SPACE TO BEGIN");
|
|
ink("0");
|
|
return wait_fire();
|
|
}
|
|
|
|
static void reset_level(void)
|
|
{
|
|
u8 i;
|
|
for (i = 0; i < NSHOT; i++) shot_life[i] = 0;
|
|
for (i = 0; i < NFIGHT; i++) fight_on[i] = 0;
|
|
for (i = 0; i < NSHELL; i++) shell_life[i] = 0;
|
|
for (i = 0; i < NBOOM; i++) boom_t[i] = 0;
|
|
for (i = 0; i < NOBJ; i++) obj_on[i] = 0;
|
|
next_obj = 0;
|
|
fight_timer = 30;
|
|
clear_all();
|
|
camera_at(-6);
|
|
set_scroll();
|
|
ship_s = 16 * BW; alt = 24; ship_d = cam_d + SHIP_D;
|
|
inv = 90; dead = 0;
|
|
}
|
|
|
|
/* Play until the ships run out (or Esc); returns 1 to go on to the title. */
|
|
static void play(void)
|
|
{
|
|
u8 j, i;
|
|
lives = 3; score = 0; level = 1;
|
|
puts_("\033[2J");
|
|
for (;;) {
|
|
fuel = 100; fuel_tick = 0; fire_held = 1;
|
|
reset_level();
|
|
msg = 0; msg_time = 0;
|
|
say("LEVEL START");
|
|
hud();
|
|
for (;;) {
|
|
wait_frame();
|
|
set_scroll();
|
|
if (drain_keys()) {
|
|
at(2, 50); 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;
|
|
fly(j);
|
|
objects_in();
|
|
objects();
|
|
shots(j);
|
|
shells();
|
|
fighters();
|
|
booms();
|
|
if (inv) inv--;
|
|
if (msg_time && !--msg_time) hud_dirty = 1;
|
|
if (hud_dirty) hud();
|
|
if (dead) {
|
|
if (!--dead_time) {
|
|
if (!--lives) {
|
|
hud();
|
|
ink("0;44;97");
|
|
centre(13, " ");
|
|
centre(14, " GAME OVER ");
|
|
centre(15, " ");
|
|
ink("0");
|
|
for (i = 0; i < 120; i++) { wait_frame(); drain_keys(); }
|
|
return;
|
|
}
|
|
dead = 0; inv = 120;
|
|
if (fuel < 40) fuel = 40;
|
|
alt = 24;
|
|
hud_dirty = 1;
|
|
}
|
|
}
|
|
if (-(ship_d >> 5) >= LEVEL_ROWS) break; /* through the level */
|
|
advance();
|
|
}
|
|
score += 500;
|
|
level++;
|
|
say("LEVEL CLEARED");
|
|
hud();
|
|
for (i = 0; i < 90; i++) { wait_frame(); drain_keys(); }
|
|
}
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
rnd_state = 0xBEEF ^ frame_ctr;
|
|
last_frame = frame_ctr;
|
|
u8 was_wide = con_stat & 4; /* 132 columns before the game? */
|
|
puts_("\033[?3l\033[?25l");
|
|
load_shapes();
|
|
quit = 0;
|
|
while (!quit) {
|
|
if (!title()) break;
|
|
play();
|
|
}
|
|
clear_all();
|
|
puts_(was_wide ? "\033[0m\033[?3h\033[?25h" /* back to 132 columns (this clears) */
|
|
: "\033[0m\033[2J\033[H\033[?25h");
|
|
while (con_stat & 1) (void)con_data;
|
|
}
|