Missile Command: arcade rules, crafts, trail system v2, emulator wave skip

Game (examples/MissileCommand):
- Trails rewritten as an analytic flight path (pos is a pure function of
  distance travelled): paint and erase replay the exact same pixels, so
  blinking trails leave zero residue. Smart bombs wiggle (S-curves) with
  deterministic paths; erase band widened to a symmetric 7px so the ±1px
  Bresenham slope jitter between paint/erase endpoints can never leak
  paint ink (verified zero strays across dense frame captures).
- 2600 look: bright steady missile head dot with a flashing trail
  (shared blink phase), trails vanish on death/impact; AA rockets stay
  moving dots.
- Arcade rules: bomber planes (100x mult) and satellites (250x mult)
  cross the sky dropping bombs; max 3 city kills per wave; bonus cities
  at every 10k kept in reserve and deployed between waves; battery
  silos rebuilt and replenished at each wave start (they no longer stay
  dead across levels); center battery fires fast rockets, sides slow.
- Wave-start attack alarm (four falling tones, once); volume U/V/M with
  OSD; sound task zeroes the I2S DMA ring when the queue drains so a
  finished sound's tail no longer loops forever.

Emulator:
- --wave N flag to skip straight to a wave for quick testing
- --dump-interval N rolling frame captures (seq%05d.bmp)
- shim: uxQueueMessagesWaiting added
This commit is contained in:
Dejvino
2026-11-10 23:42:01 +01:00
parent 45ffa87425
commit 0f85c75331
5 changed files with 390 additions and 72 deletions
+2
View File
@@ -5,4 +5,6 @@ script
test test
SX126x_Transmit SX126x_Transmit
config config
emulator/missilecommand-emu
emulator/seq*.bmp
+7
View File
@@ -63,9 +63,12 @@ int main(int argc, char **argv)
long maxFrames = -1; long maxFrames = -1;
const char *dumpPath = nullptr; const char *dumpPath = nullptr;
bool autoplay = false; bool autoplay = false;
int dumpEvery = 0;
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--frames") && i + 1 < argc) maxFrames = atol(argv[++i]); if (!strcmp(argv[i], "--frames") && i + 1 < argc) maxFrames = atol(argv[++i]);
else if (!strcmp(argv[i], "--dump") && i + 1 < argc) dumpPath = argv[++i]; else if (!strcmp(argv[i], "--dump") && i + 1 < argc) dumpPath = argv[++i];
else if (!strcmp(argv[i], "--dump-interval") && i + 1 < argc) dumpEvery = atoi(argv[++i]);
else if (!strcmp(argv[i], "--wave") && i + 1 < argc) emuStartWaveRequest = atoi(argv[++i]);
else if (!strcmp(argv[i], "--autoplay")) autoplay = true; else if (!strcmp(argv[i], "--autoplay")) autoplay = true;
} }
@@ -206,6 +209,10 @@ int main(int argc, char **argv)
if (maxFrames > 0 && --maxFrames == 0) { if (maxFrames > 0 && --maxFrames == 0) {
running = false; running = false;
if (dumpPath) saveBmp(dumpPath); if (dumpPath) saveBmp(dumpPath);
} else if (dumpEvery > 0 && frame % dumpEvery == 0) {
char path[256];
snprintf(path, sizeof(path), "seq%05d.bmp", frame / dumpEvery);
saveBmp(path);
} }
} }
+7
View File
@@ -139,6 +139,13 @@ inline int xQueueSend(QueueHandle_t q, const void *item, unsigned /*timeout*/)
return pdTRUE; return pdTRUE;
} }
inline unsigned uxQueueMessagesWaiting(QueueHandle_t q)
{
if (!q) return 0;
std::lock_guard<std::mutex> lk(q->m);
return (unsigned)q->items.size();
}
inline int xQueueReceive(QueueHandle_t q, void *out, unsigned timeout) inline int xQueueReceive(QueueHandle_t q, void *out, unsigned timeout)
{ {
if (!q) return pdFALSE; if (!q) return pdFALSE;
+366 -72
View File
@@ -52,6 +52,11 @@ static const float ROCKET_SPEED = 6.0f; // px / frame
static const float ROCKET_RADIUS = 24.0f; static const float ROCKET_RADIUS = 24.0f;
static const uint32_t BANNER_MS = 1500; static const uint32_t BANNER_MS = 1500;
static const uint32_t BONUS_MS = 3400; static const uint32_t BONUS_MS = 3400;
static const float ROCKET_SPEED_SIDE = 3.4f; // side batteries fire slow rockets;
// only the center battery is fast
static const int MAX_CRAFTS = 2; // bombers + satellites
static const float WIGGLE_AMP = 2.2f; // smart bomb wiggle amplitude (px)
static const float WIGGLE_FREQ = 0.07f; // wave number (wavelength ~90 px)
static const uint16_t EXPL_COLORS[3] = { TFT_WHITE, TFT_ORANGE, TFT_YELLOW }; static const uint16_t EXPL_COLORS[3] = { TFT_WHITE, TFT_ORANGE, TFT_YELLOW };
@@ -61,11 +66,14 @@ static const uint16_t EXPL_COLORS[3] = { TFT_WHITE, TFT_ORANGE, TFT_YELLOW };
struct Bomb { struct Bomb {
bool alive; bool alive;
float x, y; // current tip position float x, y; // current tip position
float hx, hy; // last head-dot position (for clean overpainting)
float d; // distance travelled along the flight path
float len; // total path length to the target
float sx, sy; // origin float sx, sy; // origin
float tx, ty; // target float tx, ty; // target
float dirx, diry; // unit direction float dirx, diry; // unit direction (basis of the path)
float speed; // px per frame float speed; // px per frame
float wob; // wiggle phase (smart bombs) float wob; // wiggle phase offset (smart bombs)
bool smart; bool smart;
}; };
@@ -84,9 +92,21 @@ struct Explosion {
int hold; int hold;
}; };
// Bomber planes and satellites cross the sky and drop bombs of their own
// (arcade rule). Shooting them down pays a bonus: plane 100x, satellite
// 250x the current multiplier.
struct Craft {
bool alive;
bool sat; // false: bomber plane, true: satellite
int x, y;
int dir; // -1: left, +1: right
uint32_t lastDrop;
};
static Bomb bombs[MAX_BOMBS]; static Bomb bombs[MAX_BOMBS];
static Rocket rockets[MAX_ROCKETS]; static Rocket rockets[MAX_ROCKETS];
static Explosion expls[MAX_EXPL]; static Explosion expls[MAX_EXPL];
static Craft crafts[MAX_CRAFTS];
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Game state // Game state
@@ -113,6 +133,13 @@ static uint32_t spawnIntervalMs = 1400;
static uint32_t lastSpawnMs = 0; static uint32_t lastSpawnMs = 0;
static int waveKills = 0; static int waveKills = 0;
static bool spawningStopped = false; static bool spawningStopped = false;
static int citiesLostWave = 0; // classic rule: max 3 city kills per wave
// Bonus cities: earned at preset score intervals (arcade: every 10,000),
// kept in reserve and automatically deployed between waves as needed.
static int reserveCities = 0;
static int nextCityAt = 10000;
static uint32_t cityOSDUntil = 0;
static struct { static struct {
int x, y; // current int x, y; // current
@@ -131,6 +158,9 @@ static void drawWorld();
static void drawHUD(); static void drawHUD();
static void drawAmmoPyramid(int bat); static void drawAmmoPyramid(int bat);
static void redrawGroundArea(float cx, float cy, float r); static void redrawGroundArea(float cx, float cy, float r);
static void spawnBombAt(float sx, float sy, bool smart, bool fromCraft);
static void resetGame();
static void startWave(int n);
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Sound helpers // Sound helpers
@@ -309,6 +339,23 @@ static int scoreMult()
return m > 6 ? 6 : m; return m > 6 ? 6 : m;
} }
// All scoring goes through here: at preset score intervals the player
// earns a bonus city that is kept in reserve (deployed between waves).
static void addScore(int pts)
{
score += pts;
while (score >= nextCityAt) {
reserveCities++;
nextCityAt += 10000;
cityOSDUntil = millis() + 1400;
drawHUD(); // show the new reserve count
sfxTone(1500, 60, 9000);
sfxTone(1900, 80, 9000);
}
if (score > highScore) highScore = score;
drawHUD(); // callers may go on drawing; HUD stays current
}
static void drawHUD() static void drawHUD()
{ {
tft.fillRect(0, 0, scrW, 16, TFT_BLACK); tft.fillRect(0, 0, scrW, 16, TFT_BLACK);
@@ -319,7 +366,9 @@ static void drawHUD()
tft.setTextDatum(TC_DATUM); tft.setTextDatum(TC_DATUM);
tft.drawString(String(score), scrW / 2, 2); tft.drawString(String(score), scrW / 2, 2);
tft.setTextDatum(TR_DATUM); tft.setTextDatum(TR_DATUM);
tft.drawString("x" + String(scoreMult()) + " HI " + String(highScore), scrW - 4, 2); String hud = "x" + String(scoreMult()) + " HI " + String(highScore);
if (reserveCities > 0) hud = hud + " +" + String(reserveCities);
tft.drawString(hud, scrW - 4, 2);
tft.setTextDatum(MC_DATUM); tft.setTextDatum(MC_DATUM);
} }
@@ -352,6 +401,25 @@ static void drawVolumeOSD()
} }
} }
// Brief notice when a bonus city is earned (like the original's award)
static void drawBonusCityOSD()
{
static bool shown = false;
bool want = millis() < cityOSDUntil;
int by = 20;
if (want) {
shown = true;
tft.setTextFont(2);
tft.setTextColor(TFT_GOLD, TFT_BLACK);
tft.setTextDatum(TL_DATUM);
tft.drawString("BONUS CITY EARNED", 4, by);
tft.setTextDatum(MC_DATUM);
} else if (shown) {
shown = false;
tft.fillRect(0, by - 2, 140, 18, TFT_BLACK);
}
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Crosshair // Crosshair
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -452,15 +520,15 @@ static int pickTargetX()
return tx; return tx;
} }
static void spawnBomb() static void spawnBombAt(float sx, float sy, bool smart, bool fromCraft)
{ {
for (int i = 0; i < MAX_BOMBS; i++) { for (int i = 0; i < MAX_BOMBS; i++) {
Bomb &b = bombs[i]; Bomb &b = bombs[i];
if (b.alive) continue; if (b.alive) continue;
b.smart = (random(100) < (wave >= 3 ? (2 + wave > 30 ? 30 : 2 + wave) : 0)); b.smart = smart;
b.sx = random(4, scrW - 4); b.sx = sx;
b.sy = 2; b.sy = sy;
b.tx = pickTargetX(); b.tx = pickTargetX();
b.ty = groundY; b.ty = groundY;
float dx = b.tx - b.sx; float dx = b.tx - b.sx;
@@ -468,16 +536,27 @@ static void spawnBomb()
float len = sqrtf(dx * dx + dy * dy); float len = sqrtf(dx * dx + dy * dy);
b.dirx = dx / len; b.dirx = dx / len;
b.diry = dy / len; b.diry = dy / len;
b.len = len;
b.d = 0;
b.speed = bombSpeed * (b.smart ? 1.25f : 1.0f); b.speed = bombSpeed * (b.smart ? 1.25f : 1.0f);
b.x = b.sx; b.x = b.sx;
b.y = b.sy; b.y = b.sy;
b.wob = random(0, 100) * 0.06f; b.hx = b.sx;
b.hy = b.sy;
b.wob = random(0, 628) * 0.01f;
b.alive = true; b.alive = true;
bombsSpawned++; if (!fromCraft) bombsSpawned++; // craft bombs don't use the quota
return; return;
} }
} }
static void spawnBomb()
{
// spawn below the HUD band so trails never touch the score text
bool smart = random(100) < (wave >= 3 ? (2 + wave > 28 ? 28 : 2 + wave) : 0);
spawnBombAt(random(4, scrW - 4), 22, smart, false);
}
static bool anyBombAlive() static bool anyBombAlive()
{ {
for (int i = 0; i < MAX_BOMBS; i++) if (bombs[i].alive) return true; for (int i = 0; i < MAX_BOMBS; i++) if (bombs[i].alive) return true;
@@ -526,7 +605,9 @@ static void launch(int bat, int tx, int ty)
if (len < 1) len = 1; if (len < 1) len = 1;
r.dirx = dx / len; r.dirx = dx / len;
r.diry = dy / len; r.diry = dy / len;
r.speed = ROCKET_SPEED; // classic rule: the center battery fires much faster rockets; only
// these can realistically catch smart bombs
r.speed = (bat == 1) ? ROCKET_SPEED : ROCKET_SPEED_SIDE;
r.alive = true; r.alive = true;
sfxFire(); sfxFire();
return; return;
@@ -555,11 +636,9 @@ static void fireAuto()
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Trail cleanup // Trails (the 2600 look: flashing streaks behind a bright steady dot)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Thick line: `w` parallel lines offset perpendicular to the dominant axis. // Thick line: `w` parallel lines offset perpendicular to the dominant axis.
// Missile trails are drawn 3px wide, so erase passes must be at least as
// wide or they leave residue.
static void drawThickLine(int x0, int y0, int x1, int y1, int w, uint16_t c) static void drawThickLine(int x0, int y0, int x1, int y1, int w, uint16_t c)
{ {
int dx = (x1 > x0) ? x1 - x0 : x0 - x1; int dx = (x1 > x0) ? x1 - x0 : x0 - x1;
@@ -577,7 +656,64 @@ static void drawThickLine(int x0, int y0, int x1, int y1, int w, uint16_t c)
} }
} }
#define TRAIL_W 3 // enemy trail thickness (px) // Deterministic bomb position along its flight path:
// pos(d) = start + dir*d (+ perp * A*sin(freq*d + phase) for smart bombs)
// Because the wiggle is a pure function of the distance travelled, the whole
// path can be replayed at any time; painting and erasing therefore cover
// exactly the same pixels.
static void bombPos(const Bomb &b, float d, float &px, float &py)
{
px = b.sx + b.dirx * d;
py = b.sy + b.diry * d;
if (!b.smart) return;
float p = WIGGLE_AMP * sinf(WIGGLE_FREQ * d + b.wob);
px += -b.diry * p;
py += b.dirx * p;
}
#define TRAIL_W 3 // trail band when painting (joint dots r=1)
#define TRAIL_ERASE 7 // erase band: symmetric ±3, strictly wider than paint.
// Why 7 and not 6: an even width (6) covers -3..+2, and
// the erase line's integer endpoints differ slightly
// from the paint line's (erase runs to a later d), so
// the Bresenham slope can shift the band by ±1px
// mid-line. The asymmetric +2 side then misses paint
// ink at +3. Odd width = symmetric, always covers.
// Blink pattern shared by all trails (the 2600 streaks shimmer in sync with
// the playfield clock): 6-frame cycle, trail dark on the last 2 frames.
#define BLINK_CYCLE 6
#define BLINK_OFF 4 // first "dark" frame of the cycle
// Paint (or erase) a bomb's whole trail by replaying its flight path.
// Normal bombs are straight single segments; smart bombs are walked in
// chords (the curve is gentle, so joint dots fill the chord gaps).
static void trailWalk(const Bomb &b, float dEnd, int w, uint16_t c)
{
int br = (c == TFT_BLACK) ? 3 : 1; // erase dots overshoot the paint dots
float px, py, qx, qy;
bombPos(b, 0, px, py);
// dot the path start too: the bright head dot paints one row/col beyond
// the first centerline pixel, and a near-vertical erase band (offsets
// perpendicular to a steep path) would miss it otherwise
tft.fillCircle((int)px, (int)py, br, c);
if (!b.smart) {
bombPos(b, dEnd, qx, qy);
drawThickLine((int)px, (int)py, (int)qx, (int)qy, w, c);
tft.fillCircle((int)qx, (int)qy, br, c);
return;
}
float step = (c == TFT_BLACK) ? 15.0f : 10.0f;
for (float d = step; ; d += step) {
if (d > dEnd) d = dEnd;
bombPos(b, d, qx, qy);
drawThickLine((int)px, (int)py, (int)qx, (int)qy, w, c);
tft.fillCircle((int)qx, (int)qy, br, c);
px = qx;
py = qy;
if (d >= dEnd) break;
}
}
// Does the segment (x0,y0)-(x1,y1) pass within `rad` of (px,py)? // Does the segment (x0,y0)-(x1,y1) pass within `rad` of (px,py)?
static bool linePassesNear(int x0, int y0, int x1, int y1, int px, int py, int rad) static bool linePassesNear(int x0, int y0, int x1, int y1, int px, int py, int rad)
@@ -597,24 +733,118 @@ static bool linePassesNear(int x0, int y0, int x1, int y1, int px, int py, int r
return ddx * ddx + ddy * ddy <= (long)rad * rad; return ddx * ddx + ddy * ddy <= (long)rad * rad;
} }
// Enemy trails persist while the bomb falls (classic look, drawn TRAIL_W px // after an erase pass: repair everything the erase band may have swept over
// thick with a 2px head dot), but vanish the moment the bomb is destroyed or static void trailRepair(const Bomb &b, float dEnd)
// reaches the ground, like the Atari 2600. The erase band is wider than the {
// draw: smart bombs wiggle sideways off the straight path, so their erase if (cross.drawn) {
// must sweep a wider corridor. float px, py;
bombPos(b, 0, px, py);
for (float d = 6.0f; d <= dEnd; d += 6.0f) {
float qx, qy;
bombPos(b, d, qx, qy);
if (linePassesNear((int)px, (int)py, (int)qx, (int)qy,
cross.dx, cross.dy, 8)) {
drawCrossAt(cross.dx, cross.dy, TFT_BLACK);
cross.drawn = false; // redrawn next frame
break;
}
px = qx;
py = qy;
}
}
if (dEnd >= b.len - 14) { // trail reached into the terrain strip
redrawGroundArea(b.tx, (float)groundY, 14.0f);
}
}
// Enemy trails persist while the bomb falls (classic look) but vanish the
// moment the bomb is destroyed or reaches the ground, like the Atari 2600.
static void eraseBombTrail(const Bomb &b) static void eraseBombTrail(const Bomb &b)
{ {
drawThickLine((int)b.sx, (int)b.sy, (int)b.x, (int)b.y, trailWalk(b, b.d, TRAIL_ERASE, TFT_BLACK);
b.smart ? 7 : 5, TFT_BLACK); trailRepair(b, b.d);
tft.fillCircle((int)b.x, (int)b.y, 2, TFT_BLACK); }
// repair anything the erase swept over
if (cross.drawn && linePassesNear((int)b.sx, (int)b.sy, (int)b.x, (int)b.y, // ---------------------------------------------------------------------------
cross.dx, cross.dy, 8)) { // Bomber planes & satellites
drawCrossAt(cross.dx, cross.dy, TFT_BLACK); // remove it now, // ---------------------------------------------------------------------------
cross.drawn = false; // redraw next frame static void drawCraftAt(const Craft &cr, uint16_t c)
{
if (cr.sat) {
tft.fillRect(cr.x - 5, cr.y - 1, 3, 3, c); // solar panels
tft.fillRect(cr.x + 3, cr.y - 1, 3, 3, c);
tft.drawFastHLine(cr.x - 2, cr.y, 5, c);
tft.fillCircle(cr.x, cr.y, 1, c); // body
} else {
tft.fillRect(cr.x - 5, cr.y - 1, 10, 2, c); // fuselage
tft.fillRect(cr.x - 4, cr.y - 3, 2, 5, c); // tail fin
tft.fillRect(cr.x, cr.y - 3, 3, 6, c); // wing
} }
redrawGroundArea(b.tx, (float)groundY, 14.0f); // target-end terrain }
drawHUD(); // trails start inside the HUD band
static void spawnCraft(bool sat)
{
for (int i = 0; i < MAX_CRAFTS; i++) {
Craft &cr = crafts[i];
if (cr.alive) continue;
cr.alive = true;
cr.sat = sat;
cr.dir = random(0, 2) ? 1 : -1;
cr.x = cr.dir > 0 ? -8 : scrW + 8;
cr.y = sat ? 26 + random(0, 10) : 44 + random(0, 18);
cr.lastDrop = millis() + 600;
if (sat) sfxSweep(900, 500, 350, 7000);
else sfxSweep(480, 290, 420, 7000);
return;
}
}
static void updateCrafts()
{
for (int k = 0; k < MAX_CRAFTS; k++) {
Craft &cr = crafts[k];
if (!cr.alive) continue;
// erase previous sprite, cross the screen, redraw
drawCraftAt(cr, TFT_BLACK);
// if the erase cut the crosshair, have it redrawn
if (cross.drawn &&
cross.dx > cr.x - 8 && cross.dx < cr.x + 8 &&
cross.dy > cr.y - 5 && cross.dy < cr.y + 5) {
drawCrossAt(cross.dx, cross.dy, TFT_BLACK);
cross.drawn = false;
}
cr.x += cr.dir * (cr.sat ? 1 : 2);
if (cr.x < -14 || cr.x > scrW + 14) {
cr.alive = false;
continue;
}
// satellites blink on and off as they move
if (cr.sat && (frameCount & 1)) continue;
drawCraftAt(cr, cr.sat ? TFT_ORANGE : TFT_WHITE);
// drop bombs on the defenses below
uint32_t now = millis();
uint32_t every = cr.sat ? 2200 : 1400;
if (now - cr.lastDrop >= every) {
cr.lastDrop = now;
spawnBombAt((float)cr.x, (float)cr.y + 6, false, true);
sfxTone(650, 45, 6000);
}
}
}
static void shootCraft(int k)
{
Craft &cr = crafts[k];
if (!cr.alive) return;
float cx = cr.x, cy = cr.y;
bool sat = cr.sat;
cr.alive = false;
drawCraftAt(cr, TFT_BLACK);
addScore((sat ? 250 : 100) * scoreMult());
spawnExpl(cx, cy, 16.0f, 1.5f);
sfxKill();
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -647,6 +877,10 @@ static void updateRockets()
static void killCity(int i) static void killCity(int i)
{ {
// classic rule: enemy weapons are only able to destroy three cities
// during one wave
if (!cityAlive[i] || citiesLostWave >= 3) return;
citiesLostWave++;
cityAlive[i] = false; cityAlive[i] = false;
drawCity(i); drawCity(i);
sfxCityBoom(); sfxCityBoom();
@@ -661,38 +895,49 @@ static void killBattery(int i)
static void updateBombs() static void updateBombs()
{ {
int phase = frameCount % BLINK_CYCLE;
bool dark = (phase >= BLINK_OFF); // trails are erased right now
bool cameOn = (phase == 0); // trails were just repainted
for (int i = 0; i < MAX_BOMBS; i++) { for (int i = 0; i < MAX_BOMBS; i++) {
Bomb &b = bombs[i]; Bomb &b = bombs[i];
if (!b.alive) continue; if (!b.alive) continue;
float edx = b.dirx;
float edy = b.diry;
if (b.smart) { // wiggle perpendicular to the flight path
b.wob += 0.55f;
float s = sinf(b.wob) * 0.4f;
edx = b.dirx - b.diry * s;
edy = b.diry + b.dirx * s;
float len = sqrtf(edx * edx + edy * edy);
edx /= len;
edy /= len;
}
// NOTE: keep float positions between frames; truncating to int and
// storing back would make slow missiles stall (0.5 px/frame rounds
// back to the same pixel forever).
float nxf = b.x + edx * b.speed;
float nyf = b.y + edy * b.speed;
uint16_t col = b.smart ? TFT_MAGENTA : TFT_RED; uint16_t col = b.smart ? TFT_MAGENTA : TFT_RED;
// Restore the previous head dot to trail color (so the path stays a // advance along the analytic path: position is exact for any d,
// continuous 3px line), extend the trail, then draw the new head: a // independent of the blink phase (no history, no drift)
// bright dot that flashes, like the 2600 version. float dOld = b.d;
tft.fillCircle((int)b.x, (int)b.y, 2, col); float dNew = dOld + b.speed;
drawThickLine((int)b.x, (int)b.y, (int)nxf, (int)nyf, TRAIL_W, col);
uint16_t headCol = (frameCount % 4) < 2 ? TFT_WHITE : col; if (cameOn) {
tft.fillCircle((int)nxf, (int)nyf, 2, headCol); // repaint the whole streak from the start (covers the head dot
b.x = nxf; // last drawn before the dark frames)
b.y = nyf; trailWalk(b, dNew, TRAIL_W, col);
} else if (!dark) {
// steady-on frame: clean the last bright head dot by
// overpainting it with trail color, then extend the streak
tft.fillCircle((int)b.hx, (int)b.hy, 1, col);
float px, py, qx, qy;
bombPos(b, dOld, px, py);
bombPos(b, dNew, qx, qy);
drawThickLine((int)px, (int)py, (int)qx, (int)qy, TRAIL_W, col);
} else if (phase == BLINK_OFF) {
// streak just went dark: erase it along its exact path
trailWalk(b, dNew, TRAIL_ERASE, TFT_BLACK);
trailRepair(b, dNew);
}
b.d = dNew;
if (b.d > b.len) b.d = b.len;
bombPos(b, b.d, b.x, b.y);
// the bright missile dot itself: steady while the trail flashes
if (!dark && b.d < b.len) {
tft.fillCircle((int)b.x, (int)b.y, 1, TFT_WHITE);
b.hx = b.x;
b.hy = b.y;
}
// caught by an explosion? // caught by an explosion?
for (int e = 0; e < MAX_EXPL; e++) { for (int e = 0; e < MAX_EXPL; e++) {
@@ -703,7 +948,7 @@ static void updateBombs()
if (ddx * ddx + ddy * ddy < ex.r * ex.r) { if (ddx * ddx + ddy * ddy < ex.r * ex.r) {
b.alive = false; b.alive = false;
waveKills++; waveKills++;
score += 25 * scoreMult(); addScore(25 * scoreMult());
eraseBombTrail(b); // also refreshes the HUD with the new score eraseBombTrail(b); // also refreshes the HUD with the new score
spawnExpl(b.x, b.y, 14.0f, 1.4f); spawnExpl(b.x, b.y, 14.0f, 1.4f);
sfxKill(); sfxKill();
@@ -712,11 +957,11 @@ static void updateBombs()
} }
if (!b.alive) continue; if (!b.alive) continue;
// reached the ground? // reached the target?
if (b.y >= b.ty) { if (b.d >= b.len) {
b.alive = false; b.alive = false;
eraseBombTrail(b); eraseBombTrail(b);
spawnExpl(b.tx, groundY - 4, 11.0f, 1.3f); spawnExpl(b.x, groundY - 4, 11.0f, 1.3f);
sfxBoom(); sfxBoom();
} }
} }
@@ -774,6 +1019,15 @@ static void updateExpls()
float dy = (groundY - 4) - e.y; float dy = (groundY - 4) - e.y;
if (dx * dx + dy * dy < e.r * e.r) killBattery(b); if (dx * dx + dy * dy < e.r * e.r) killBattery(b);
} }
// bombers / satellites caught in the fireball pay a bonus
for (int k = 0; k < MAX_CRAFTS; k++) {
Craft &cr = crafts[k];
if (!cr.alive) continue;
float dx = cr.x - e.x;
float dy = cr.y - e.y;
if (dx * dx + dy * dy < e.r * e.r) shootCraft(k);
}
} }
} }
} }
@@ -781,6 +1035,16 @@ static void updateExpls()
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Waves / game flow // Waves / game flow
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Host hook: the desktop emulator can request skipping straight to a wave
// (--wave N). Weak definition so the device build simply sees 0.
__attribute__((weak)) int emuStartWaveRequest = 0;
static void beginGame()
{
resetGame();
startWave(emuStartWaveRequest > 0 ? emuStartWaveRequest : 1);
}
static void resetGame() static void resetGame()
{ {
for (int i = 0; i < CITY_COUNT; i++) cityAlive[i] = true; for (int i = 0; i < CITY_COUNT; i++) cityAlive[i] = true;
@@ -791,8 +1055,13 @@ static void resetGame()
for (int i = 0; i < MAX_BOMBS; i++) bombs[i].alive = false; for (int i = 0; i < MAX_BOMBS; i++) bombs[i].alive = false;
for (int i = 0; i < MAX_ROCKETS; i++) rockets[i].alive = false; for (int i = 0; i < MAX_ROCKETS; i++) rockets[i].alive = false;
for (int i = 0; i < MAX_EXPL; i++) expls[i].alive = false; for (int i = 0; i < MAX_EXPL; i++) expls[i].alive = false;
for (int i = 0; i < MAX_CRAFTS; i++) crafts[i].alive = false;
score = 0; score = 0;
wave = 0; wave = 0;
citiesLostWave = 0;
reserveCities = 0;
nextCityAt = 10000;
cityOSDUntil = 0;
cross.x = cross.dx = scrW / 2; cross.x = cross.dx = scrW / 2;
cross.y = cross.dy = scrH / 2; cross.y = cross.dy = scrH / 2;
} }
@@ -800,8 +1069,12 @@ static void resetGame()
static void startWave(int n) static void startWave(int n)
{ {
wave = n; wave = n;
// arcade rule: between levels missile batteries are rebuilt and
// replenished; only destroyed cities stay destroyed until a bonus
// city is deployed
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
if (batAlive[i]) batAmmo[i] = AMMO_MAX; batAlive[i] = true;
batAmmo[i] = AMMO_MAX;
} }
// every third wave one destroyed city is rebuilt // every third wave one destroyed city is rebuilt
if (n > 1 && (n - 1) % 3 == 0) { if (n > 1 && (n - 1) % 3 == 0) {
@@ -820,11 +1093,17 @@ static void startWave(int n)
if (spawnIntervalMs < 500) spawnIntervalMs = 500; if (spawnIntervalMs < 500) spawnIntervalMs = 500;
bombsSpawned = 0; bombsSpawned = 0;
waveKills = 0; waveKills = 0;
citiesLostWave = 0; // fresh city-kill budget each wave
spawningStopped = false; spawningStopped = false;
lastSpawnMs = millis(); lastSpawnMs = millis();
drawWorld(); drawWorld();
// bombers (and later satellites) cross the sky, launching bombs
for (int i = 0; i < MAX_CRAFTS; i++) crafts[i].alive = false;
if (n >= 2) spawnCraft(false);
if (n >= 5 && random(100) < 45) spawnCraft(true);
tft.setFreeFont(&FreeSansBold18pt7b); tft.setFreeFont(&FreeSansBold18pt7b);
tft.setTextColor(TFT_YELLOW, TFT_BLACK); tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.setTextDatum(MC_DATUM); tft.setTextDatum(MC_DATUM);
@@ -851,6 +1130,16 @@ static void enterBonus()
{ {
sfxWaveClear(); sfxWaveClear();
// deploy reserve cities to rebuild destroyed ones (arcade rule)
int deployed = 0;
for (int i = 0; i < CITY_COUNT; i++) {
if (!cityAlive[i] && reserveCities > 0) {
reserveCities--;
cityAlive[i] = true;
deployed++;
}
}
int aliveCities = 0; int aliveCities = 0;
for (int i = 0; i < CITY_COUNT; i++) if (cityAlive[i]) aliveCities++; for (int i = 0; i < CITY_COUNT; i++) if (cityAlive[i]) aliveCities++;
@@ -860,8 +1149,7 @@ static void enterBonus()
int mult = scoreMult(); int mult = scoreMult();
int cityPts = aliveCities * 100 * mult; int cityPts = aliveCities * 100 * mult;
int ammoPts = ammoLeft * 5 * mult; int ammoPts = ammoLeft * 5 * mult;
score += cityPts + ammoPts; addScore(cityPts + ammoPts);
if (score > highScore) highScore = score;
int px = 60, pw = scrW - 120, py = 64, ph = 108; int px = 60, pw = scrW - 120, py = 64, ph = 108;
tft.fillRect(px, py, pw, ph, TFT_BLACK); tft.fillRect(px, py, pw, ph, TFT_BLACK);
@@ -872,11 +1160,18 @@ static void enterBonus()
tft.setTextDatum(MC_DATUM); tft.setTextDatum(MC_DATUM);
tft.drawString("WAVE " + String(wave) + " CLEARED", scrW / 2, py + 12); tft.drawString("WAVE " + String(wave) + " CLEARED", scrW / 2, py + 12);
tft.setTextColor(TFT_WHITE, TFT_BLACK); tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawString("MISSILES " + String(waveKills) + " x " + String(25 * mult), scrW / 2, py + 34); tft.drawString("MISSILES " + String(waveKills) + " x " + String(25 * mult), scrW / 2, py + 30);
tft.drawString("CITIES " + String(aliveCities) + " x " + String(100 * mult), scrW / 2, py + 52); tft.drawString("CITIES " + String(aliveCities) + " x " + String(100 * mult), scrW / 2, py + 46);
tft.drawString("AMMO " + String(ammoLeft) + " x " + String(5 * mult), scrW / 2, py + 70); tft.drawString("AMMO " + String(ammoLeft) + " x " + String(5 * mult), scrW / 2, py + 62);
if (deployed > 0) {
tft.setTextColor(TFT_CYAN, TFT_BLACK);
tft.drawString("REBUILT " + String(deployed) + " (BONUS CITY)", scrW / 2, py + 78);
} else if (reserveCities > 0) {
tft.setTextColor(TFT_GOLD, TFT_BLACK);
tft.drawString("RESERVE CITIES " + String(reserveCities), scrW / 2, py + 78);
}
tft.setTextColor(TFT_GOLD, TFT_BLACK); tft.setTextColor(TFT_GOLD, TFT_BLACK);
tft.drawString("SCORE " + String(score), scrW / 2, py + 90); tft.drawString("SCORE " + String(score), scrW / 2, py + 94);
gameState = ST_BONUS; gameState = ST_BONUS;
stateT0 = millis(); stateT0 = millis();
@@ -965,8 +1260,7 @@ static void pollTrackball()
switch (gameState) { switch (gameState) {
case ST_TITLE: case ST_TITLE:
resetGame(); beginGame();
startWave(1);
break; break;
case ST_BONUS: case ST_BONUS:
startWave(wave + 1); startWave(wave + 1);
@@ -997,8 +1291,7 @@ static void pollTouch()
wasDown = true; wasDown = true;
switch (gameState) { switch (gameState) {
case ST_TITLE: case ST_TITLE:
resetGame(); beginGame();
startWave(1);
break; break;
case ST_BONUS: case ST_BONUS:
startWave(wave + 1); startWave(wave + 1);
@@ -1055,8 +1348,7 @@ static void pollKeyboard()
switch (gameState) { switch (gameState) {
case ST_TITLE: case ST_TITLE:
resetGame(); beginGame();
startWave(1);
break; break;
case ST_BONUS: case ST_BONUS:
startWave(wave + 1); startWave(wave + 1);
@@ -1098,6 +1390,7 @@ static void loopPlaying()
updateRockets(); updateRockets();
updateBombs(); updateBombs();
updateCrafts();
updateExpls(); updateExpls();
renderCross(); renderCross();
@@ -1267,4 +1560,5 @@ void loop()
} }
drawVolumeOSD(); drawVolumeOSD();
drawBonusCityOSD();
} }
+8
View File
@@ -72,6 +72,14 @@ void SoundEngine::task()
case SND_SWEEP: renderSweep(e.freq, e.freq2, e.dur, e.amp); break; case SND_SWEEP: renderSweep(e.freq, e.freq2, e.dur, e.amp); break;
case SND_NOISE: renderNoise(e.dur, e.amp); break; case SND_NOISE: renderNoise(e.dur, e.amp); break;
} }
// After the LAST queued event the I2S TX DMA ring would keep
// replaying its remaining ~64 ms of buffer forever — heard as the
// sound's tail (rocket sweep, melody tone) repeating endlessly.
// Zero the ring once the queue is empty (not between queued events,
// which would punch silence-gaps into back-to-back melodies).
if (uxQueueMessagesWaiting(q_) == 0) {
i2s_zero_dma_buffer(SND_I2S_PORT);
}
} }
} }