/* Star Swarm: a swooping alien shooter for the Basys 3 RISC-V computer. An alien swarm sways across the top of the screen in formation: drones, warriors, escorts and two flagships. They peel off in looping dives, firing as they come, and loop back into place if they miss. A flagship brings its escorts; shoot the escorts first for a bonus. Your ship has one shot in the air at a time. There are 46 aliens and only 32 sprites, so the formation is drawn into the graphics layer and sways by the hardware X scroll (ports 35h-36h). An alien that dives is wiped from the graphics and becomes a sprite; when it gets back to its place it is drawn in again. The stars are sprites too, a couple of dots each, falling down the screen. It runs at A200h, loaded by the SWARM program in BASIC. */ 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_LEFT 4 #define J_RIGHT 8 #define J_FIRE 16 #define J_ENTER 32 #define J_ESC 128 #define COLS 10 #define ROWS 6 #define FX0 60 /* buffer x of column 0 */ #define CW 20 #define FY0 36 /* top row */ #define RH 16 #define SHIP_Y 214 /* sprites: lower numbers in front */ #define SP_SHIP 0 #define SP_SHOT 1 #define SP_DIVER 2 /* 8 */ #define SP_BUL 10 /* 6 */ #define SP_BOOM 16 /* 4 */ #define SP_STAR 20 /* 10 */ #define ND 8 #define NB 6 #define NX 4 #define NST 10 #define C_EMPTY 0 #define C_HOME 1 #define C_AWAY 2 #define P_LOOP 0 #define P_ATTACK 1 #define P_RETURN 2 /* which alien is where: 0 none, else 1 drone, 2 warrior, 3 escort, 4 flagship */ static const u8 layout[ROWS][COLS] = { { 0, 0, 0, 4, 0, 0, 4, 0, 0, 0 }, { 0, 0, 3, 3, 3, 3, 3, 3, 0, 0 }, { 0, 2, 2, 2, 2, 2, 2, 2, 2, 0 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }; static const u8 kind_shape[5] = { 0, S_DRONE, S_WARRIOR, S_ESCORT, S_FLAG }; static const u8 home_score[5] = { 0, 30, 40, 50, 60 }; static const u8 dive_score[5] = { 0, 60, 80, 100, 150 }; /* ---------------------------------------------------------------- state */ /* not static: test-swarm.py reads these from the link map */ u8 cell[ROWS][COLS]; i16 ship_x; u8 lives, wave, dead, shield; u16 score; u8 dv_on[ND]; i16 dv_x[ND], dv_y[ND]; /* 1/16 pixels, screen */ static u8 dv_row[ND], dv_col[ND], dv_ang[ND], dv_spd[ND], dv_phase[ND], dv_cool[ND], dv_shots[ND]; static i8 dv_turn[ND]; static u8 dv_lead[ND]; /* escorts: the flagship's diver, else 255 */ static u8 dv_escorts[ND], dv_esc_hit[ND]; static i16 shot_x, shot_y; static u8 shot_on; static i16 bx[NB], by[NB]; static i8 bvx[NB]; static u8 b_on[NB]; static i16 xx[NX], xy[NX]; static u8 xt[NX], xship[NX]; static u8 st_x[NST], st_y[NST], st_v[NST]; static i16 sway; static i8 sway_dir; static u8 flap, flap_timer, dive_timer, attacks, dead_time, fire_held; 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; } /* ------------------------------------------------------------ graphics */ /* Draw shape (or nothing) into the graphics with its top left at buffer (x, y). */ static void blit(u16 x, u8 y, u8 shape, u8 erase) { const u8 *p = shape_data + ((u16)shape << 7); u8 r, i, b; for (r = 0; r < 16; r++) { gx_lo = (u8)x; gx_hi = (u8)(x >> 8); gy_p = y + r; for (i = 0; i < 8; i++) { b = erase ? 0 : *p++; gpix = b >> 4; gpix = b & 15; } } } static u16 cell_bx(u8 c) { return FX0 + c * CW + 2; } static u8 cell_by(u8 r) { return FY0 + r * RH; } static void draw_cell(u8 r, u8 c) { blit(cell_bx(c), cell_by(r), kind_shape[layout[r][c]] + flap, cell[r][c] != C_HOME); } static void draw_formation(void) { u8 r, c; for (r = 0; r < ROWS; r++) for (c = 0; c < COLS; c++) if (layout[r][c]) draw_cell(r, c); } static void set_scroll(void) { /* the picture moves right by sway: show buffer column -sway at the left */ i16 s = -sway; if (s < 0) s += 320; scr_xlo = (u8)s; scr_xhi = (u8)(s >> 8); scr_y = 0; } /* -------------------------------------------------------------- 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); } 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 void boom(i16 x, i16 y, u8 ship) { u8 i, best = 0; for (i = 0; i < NX; i++) if (xt[i] < xt[best]) best = i; xx[best] = x; xy[best] = y; xt[best] = ship ? 60 : 18; xship[best] = ship; } /* ------------------------------------------------------------------ HUD */ static void hud(void) { hud_dirty = 0; at(1, 1); ink("0;93"); puts_("STAR SWARM "); 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_(" WAVE "); ink("0;97"); putn(wave, 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 += 7000; lives++; say("BONUS SHIP"); } hud_dirty = 1; } /* --------------------------------------------------------------- stars */ static void stars(void) { u8 i; for (i = 0; i < NST; i++) { st_y[i] += st_v[i]; if (st_y[i] >= 240) { st_y[i] -= 240; st_x[i] = rnd() % 304; } spr_at(SP_STAR + i, st_x[i], st_y[i], S_STARS1 + i % 3, 1); } } /* -------------------------------------------------------------- divers */ static void dive(u8 r, u8 c, u8 lead, i8 turn) { u8 d; for (d = 0; d < ND && dv_on[d]; d++); if (d == ND) return; dv_on[d] = 1; dv_row[d] = r; dv_col[d] = c; dv_x[d] = (i16)(cell_bx(c) + sway) << 4; dv_y[d] = (i16)cell_by(r) << 4; dv_ang[d] = 48; /* straight up */ dv_turn[d] = turn; dv_spd[d] = 22; dv_phase[d] = P_LOOP; dv_cool[d] = 20 + (rnd() & 31); dv_shots[d] = wave < 3 ? 2 : 3; dv_lead[d] = lead; dv_escorts[d] = 0; dv_esc_hit[d] = 0; if (lead != 255) dv_escorts[lead]++; cell[r][c] = C_AWAY; draw_cell(r, c); } static u8 divers_out(void) { u8 d, n = 0; for (d = 0; d < ND; d++) if (dv_on[d]) n++; return n; } static void launch(void) { u8 r, c, k, side, lead; i8 turn; u8 most = 2 + wave; if (most > ND) most = ND; if (divers_out() >= most) return; attacks++; /* every fourth attack, a flagship with the escorts beside it */ if ((attacks & 3) == 0) { c = (rnd() & 1) ? 3 : 6; if (cell[0][c] != C_HOME) c = 9 - c; if (cell[0][c] == C_HOME) { turn = c < 5 ? 1 : -1; dive(0, c, 255, turn); for (lead = 0; lead < ND; lead++) if (dv_on[lead] && dv_row[lead] == 0 && dv_col[lead] == c) break; if (lead < ND) for (r = c - 1; r <= c + 1 && dv_escorts[lead] < 2; r++) if (cell[1][r] == C_HOME) dive(1, r, lead, turn); return; } } /* otherwise one from the end of a row, from a random side */ side = rnd() & 1; for (k = 0; k < COLS; k++) { c = side ? COLS - 1 - k : k; for (r = ROWS; r-- > 1;) { if (cell[r][c] == C_HOME && !(rnd() & 1)) { dive(r, c, 255, c < 5 ? 1 : -1); return; } } } } static void bullet_from(i16 x, i16 y) { u8 b; for (b = 0; b < NB && b_on[b]; b++); if (b == NB) return; b_on[b] = 1; bx[b] = x << 4; by[b] = y << 4; bvx[b] = ship_x + 8 > x ? 6 : -6; if (absd(ship_x + 8 - x) < 20) bvx[b] = 0; } static void kill_diver(u8 d) { u8 r = dv_row[d], c = dv_col[d], k = layout[r][c], lead = dv_lead[d]; u16 pts = dive_score[k]; if (k == 4) { if (dv_escorts[d] && dv_esc_hit[d] == dv_escorts[d]) { pts = 800; say("FLAGSHIP AND ESCORTS: 800"); } else if (dv_escorts[d]) pts = 300; } if (lead != 255 && dv_on[lead]) dv_esc_hit[lead]++; add_score(pts); boom((dv_x[d] >> 4), dv_y[d] >> 4, 0); dv_on[d] = 0; cell[r][c] = C_EMPTY; spr_off(SP_DIVER + d); } static void kill_ship(void) { u8 d, b; if (dead || shield) return; dead = 1; dead_time = 130; boom(ship_x, SHIP_Y, 1); spr_off(SP_SHIP); for (b = 0; b < NB; b++) { b_on[b] = 0; spr_off(SP_BUL + b); } for (d = 0; d < ND; d++) if (dv_on[d] && dv_phase[d] != P_RETURN) dv_phase[d] = P_ATTACK; } static void divers(void) { u8 d, r, c, s; i16 x, y, tx; for (d = 0; d < ND; d++) { if (!dv_on[d]) continue; r = dv_row[d]; c = dv_col[d]; switch (dv_phase[d]) { case P_LOOP: /* up and over, out to the side */ dv_ang[d] = (dv_ang[d] - dv_turn[d]) & 63; if (dv_ang[d] == 16) { dv_phase[d] = P_ATTACK; dv_spd[d] = 30 + 2 * (wave < 8 ? wave : 8); } break; case P_ATTACK: /* down at the ship, firing */ if (!(last_frame & 3) && !dead) { tx = ship_x + 8 - (dv_x[d] >> 4); if (tx > 6 && dv_ang[d] > 9) dv_ang[d]--; else if (tx < -6 && dv_ang[d] < 23) dv_ang[d]++; } y = dv_y[d] >> 4; if (dv_cool[d]) dv_cool[d]--; else if (dv_shots[d] && y > 70 && y < 170 && !dead) { bullet_from((dv_x[d] >> 4) + 8, y + 12); dv_shots[d]--; dv_cool[d] = 16 + (rnd() & 15); } if (y > 244) { /* gone past: come back in at the top */ dv_phase[d] = P_RETURN; dv_y[d] = -16 << 4; dv_spd[d] = 24; } break; case P_RETURN: /* drop back into place */ dv_x[d] = (i16)(cell_bx(c) + sway) << 4; dv_y[d] += dv_spd[d]; if (dv_y[d] >= (i16)cell_by(r) << 4) { dv_on[d] = 0; cell[r][c] = C_HOME; draw_cell(r, c); spr_off(SP_DIVER + d); continue; } break; } if (dv_phase[d] != P_RETURN) { dv_x[d] += (sin64[(dv_ang[d] + 16) & 63] * dv_spd[d]) >> 6; dv_y[d] += (sin64[dv_ang[d]] * dv_spd[d]) >> 6; } x = dv_x[d] >> 4; y = dv_y[d] >> 4; if (!dead && absd(x - ship_x) < 11 && absd(y - SHIP_Y) < 10) { kill_diver(d); kill_ship(); continue; } if (shot_on && absd(shot_x - (x + 8)) < 8 && absd(shot_y - (y + 7)) < 8) { shot_on = 0; spr_off(SP_SHOT); kill_diver(d); continue; } s = kind_shape[layout[r][c]] + ((last_frame >> 3) & 1); spr_at(SP_DIVER + d, x, y, s, 1); } } static void bullets(void) { u8 b; i16 x, y; for (b = 0; b < NB; b++) { if (!b_on[b]) continue; bx[b] += bvx[b]; by[b] += 48; x = bx[b] >> 4; y = by[b] >> 4; if (y > 240) { b_on[b] = 0; spr_off(SP_BUL + b); continue; } if (!dead && absd(x - (ship_x + 8)) < 7 && y > SHIP_Y + 2 && y < SHIP_Y + 13) { b_on[b] = 0; spr_off(SP_BUL + b); kill_ship(); continue; } spr_at(SP_BUL + b, x - 8, y - 8, S_BULLET, 1); } } /* The ship's shot against the formation: which cell is it in? */ static void shot_vs_formation(void) { i16 bxp = shot_x - sway - FX0, byp = shot_y - FY0; u8 r, c, ix, iy; if (bxp < 0 || byp < 0) return; c = (u8)(bxp / CW); ix = (u8)(bxp % CW); r = (u8)(byp / RH); iy = (u8)(byp % RH); if (c >= COLS || r >= ROWS || ix < 3 || ix > 16 || iy < 2 || iy > 11) return; if (cell[r][c] != C_HOME) return; cell[r][c] = C_EMPTY; draw_cell(r, c); add_score(home_score[layout[r][c]]); boom(cell_bx(c) + sway, cell_by(r), 0); shot_on = 0; spr_off(SP_SHOT); } static void ship(u8 j) { if (dead) return; if ((j & J_LEFT) && ship_x > 6) ship_x -= 2; if ((j & J_RIGHT) && ship_x < 298) ship_x += 2; if (j & J_FIRE) { if (!shot_on && !fire_held) { shot_on = 1; shot_x = ship_x + 8; shot_y = SHIP_Y; } fire_held = 1; } else fire_held = 0; spr_at(SP_SHIP, ship_x, SHIP_Y, S_SHIP, 1); } static void shot(void) { if (!shot_on) return; shot_y -= 6; if (shot_y < 16) { shot_on = 0; spr_off(SP_SHOT); return; } shot_vs_formation(); if (shot_on) spr_at(SP_SHOT, shot_x - 8, shot_y - 8, S_SHOT, 1); } static void booms(void) { u8 i; for (i = 0; i < NX; i++) { if (!xt[i]) { spr_off(SP_BOOM + i); continue; } xt[i]--; spr_at(SP_BOOM + i, xx[i], xy[i], xship[i] ? S_SHIPBOOM : (xt[i] & 4) ? S_BOOM1 : S_BOOM2, xt[i] ? 1 : 0); } } static u8 aliens_left(void) { u8 r, c, n = 0; for (r = 0; r < ROWS; r++) for (c = 0; c < COLS; c++) if (cell[r][c]) n++; return n; } static void new_wave(void) { u8 r, c, i; for (r = 0; r < ROWS; r++) for (c = 0; c < COLS; c++) cell[r][c] = layout[r][c] ? C_HOME : C_EMPTY; for (i = 0; i < ND; i++) { dv_on[i] = 0; spr_off(SP_DIVER + i); } for (i = 0; i < NB; i++) { b_on[i] = 0; spr_off(SP_BUL + i); } shot_on = 0; spr_off(SP_SHOT); sway = 0; sway_dir = 1; flap = 0; flap_timer = 24; dive_timer = 120; attacks = 0; draw_formation(); } /* ------------------------------------------------------------- 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(); stars(); } for (;;) { wait_frame(); drain_keys(); rnd(); stars(); 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"); sway = 0; set_scroll(); blit(112, 150, S_FLAG, 0); blit(136, 150, S_ESCORT, 0); blit(160, 150, S_WARRIOR, 0); blit(184, 150, S_DRONE, 0); spr_at(SP_SHIP, 152, 200, S_SHIP, 1); ink("1;93"); centre(7, "S T A R S W A R M"); ink("0;97"); centre(10, "The swarm sways overhead, and peels off to dive at you."); centre(11, "A flagship dives with its escorts: shoot them first for a bonus."); centre(13, "LEFT / RIGHT move SPACE fire P pause ESC give up"); ink("0;37"); centre(16, " 60 / 150 50 / 100 40 / 80 30 / 60 "); centre(17, " IN FORMATION / DIVING"); ink("1;96"); centre(28, "PRESS SPACE TO BEGIN"); ink("0"); return wait_fire(); } static void play(void) { u8 j, i; lives = 3; score = 0; wave = 1; next_extra = 7000; ship_x = 152; dead = 0; fire_held = 1; for (i = 0; i < NX; i++) xt[i] = 0; clear_all(); puts_("\033[2J"); new_wave(); msg = 0; say("HERE THEY COME"); 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; /* sway: a pixel every other frame, turning at the ends */ if (last_frame & 1) { sway += sway_dir; if (sway >= 44 || sway <= -44) sway_dir = -sway_dir; } if (!--flap_timer) { flap_timer = 24; flap ^= 1; draw_formation(); } if (!dead && !--dive_timer) { i = aliens_left(); dive_timer = (i < 8 ? 30 : 100 - (wave < 6 ? wave * 10 : 60)) + (rnd() & 31); launch(); } ship(j); shot(); divers(); bullets(); booms(); stars(); if (msg_time && !--msg_time) hud_dirty = 1; if (hud_dirty) hud(); 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(); stars(); } return; } dead = 0; ship_x = 152; fire_held = 1; hud_dirty = 1; } if (!aliens_left() && !dead) { say("SWARM DESTROYED"); hud(); for (i = 0; i < 120; i++) { wait_frame(); drain_keys(); stars(); booms(); } wave++; new_wave(); hud_dirty = 1; } } } int main(void) { u8 i; rnd_state = 0x1979 ^ frame_ctr; last_frame = frame_ctr; u8 was_wide = con_stat & 4; /* 132 columns before the game? */ puts_("\033[?3l\033[?25l"); load_shapes(); for (i = 0; i < NST; i++) { st_x[i] = rnd() % 304; st_y[i] = rnd() % 240; st_v[i] = 1 + (i & 1); } 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; }