diff --git a/.gitignore b/.gitignore index 874413c..ecd3fbe 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,6 @@ script test SX126x_Transmit config +emulator/missilecommand-emu +emulator/seq*.bmp diff --git a/emulator/src/main.cpp b/emulator/src/main.cpp index 67c94ca..44967b3 100644 --- a/emulator/src/main.cpp +++ b/emulator/src/main.cpp @@ -63,9 +63,12 @@ int main(int argc, char **argv) long maxFrames = -1; const char *dumpPath = nullptr; bool autoplay = false; + int dumpEvery = 0; for (int i = 1; i < argc; 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-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; } @@ -206,6 +209,10 @@ int main(int argc, char **argv) if (maxFrames > 0 && --maxFrames == 0) { running = false; 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); } } diff --git a/emulator/src/shim/Arduino.h b/emulator/src/shim/Arduino.h index 9b7bcec..d2e7079 100644 --- a/emulator/src/shim/Arduino.h +++ b/emulator/src/shim/Arduino.h @@ -139,6 +139,13 @@ inline int xQueueSend(QueueHandle_t q, const void *item, unsigned /*timeout*/) return pdTRUE; } +inline unsigned uxQueueMessagesWaiting(QueueHandle_t q) +{ + if (!q) return 0; + std::lock_guard lk(q->m); + return (unsigned)q->items.size(); +} + inline int xQueueReceive(QueueHandle_t q, void *out, unsigned timeout) { if (!q) return pdFALSE; diff --git a/examples/MissileCommand/MissileCommand.ino b/examples/MissileCommand/MissileCommand.ino index d2ca83f..e24805a 100644 --- a/examples/MissileCommand/MissileCommand.ino +++ b/examples/MissileCommand/MissileCommand.ino @@ -52,6 +52,11 @@ static const float ROCKET_SPEED = 6.0f; // px / frame static const float ROCKET_RADIUS = 24.0f; static const uint32_t BANNER_MS = 1500; 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 }; @@ -61,11 +66,14 @@ static const uint16_t EXPL_COLORS[3] = { TFT_WHITE, TFT_ORANGE, TFT_YELLOW }; struct Bomb { bool alive; 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 tx, ty; // target - float dirx, diry; // unit direction + float dirx, diry; // unit direction (basis of the path) float speed; // px per frame - float wob; // wiggle phase (smart bombs) + float wob; // wiggle phase offset (smart bombs) bool smart; }; @@ -84,9 +92,21 @@ struct Explosion { 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 Rocket rockets[MAX_ROCKETS]; static Explosion expls[MAX_EXPL]; +static Craft crafts[MAX_CRAFTS]; // --------------------------------------------------------------------------- // Game state @@ -113,6 +133,13 @@ static uint32_t spawnIntervalMs = 1400; static uint32_t lastSpawnMs = 0; static int waveKills = 0; 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 { int x, y; // current @@ -131,6 +158,9 @@ static void drawWorld(); static void drawHUD(); static void drawAmmoPyramid(int bat); 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 @@ -309,6 +339,23 @@ static int scoreMult() 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() { tft.fillRect(0, 0, scrW, 16, TFT_BLACK); @@ -319,7 +366,9 @@ static void drawHUD() tft.setTextDatum(TC_DATUM); tft.drawString(String(score), scrW / 2, 2); 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); } @@ -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 // --------------------------------------------------------------------------- @@ -452,15 +520,15 @@ static int pickTargetX() return tx; } -static void spawnBomb() +static void spawnBombAt(float sx, float sy, bool smart, bool fromCraft) { for (int i = 0; i < MAX_BOMBS; i++) { Bomb &b = bombs[i]; if (b.alive) continue; - b.smart = (random(100) < (wave >= 3 ? (2 + wave > 30 ? 30 : 2 + wave) : 0)); - b.sx = random(4, scrW - 4); - b.sy = 2; + b.smart = smart; + b.sx = sx; + b.sy = sy; b.tx = pickTargetX(); b.ty = groundY; float dx = b.tx - b.sx; @@ -468,16 +536,27 @@ static void spawnBomb() float len = sqrtf(dx * dx + dy * dy); b.dirx = dx / len; b.diry = dy / len; + b.len = len; + b.d = 0; b.speed = bombSpeed * (b.smart ? 1.25f : 1.0f); b.x = b.sx; 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; - bombsSpawned++; + if (!fromCraft) bombsSpawned++; // craft bombs don't use the quota 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() { 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; r.dirx = dx / 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; sfxFire(); 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. -// 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) { 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)? 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; } -// Enemy trails persist while the bomb falls (classic look, drawn TRAIL_W px -// thick with a 2px head dot), but vanish the moment the bomb is destroyed or -// 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 -// must sweep a wider corridor. +// after an erase pass: repair everything the erase band may have swept over +static void trailRepair(const Bomb &b, float dEnd) +{ + if (cross.drawn) { + 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) { - drawThickLine((int)b.sx, (int)b.sy, (int)b.x, (int)b.y, - b.smart ? 7 : 5, TFT_BLACK); - 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)) { - drawCrossAt(cross.dx, cross.dy, TFT_BLACK); // remove it now, - cross.drawn = false; // redraw next frame + trailWalk(b, b.d, TRAIL_ERASE, TFT_BLACK); + trailRepair(b, b.d); +} + +// --------------------------------------------------------------------------- +// Bomber planes & satellites +// --------------------------------------------------------------------------- +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) { + // classic rule: enemy weapons are only able to destroy three cities + // during one wave + if (!cityAlive[i] || citiesLostWave >= 3) return; + citiesLostWave++; cityAlive[i] = false; drawCity(i); sfxCityBoom(); @@ -661,38 +895,49 @@ static void killBattery(int i) 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++) { Bomb &b = bombs[i]; 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; - // Restore the previous head dot to trail color (so the path stays a - // continuous 3px line), extend the trail, then draw the new head: a - // bright dot that flashes, like the 2600 version. - tft.fillCircle((int)b.x, (int)b.y, 2, col); - drawThickLine((int)b.x, (int)b.y, (int)nxf, (int)nyf, TRAIL_W, col); - uint16_t headCol = (frameCount % 4) < 2 ? TFT_WHITE : col; - tft.fillCircle((int)nxf, (int)nyf, 2, headCol); - b.x = nxf; - b.y = nyf; + // advance along the analytic path: position is exact for any d, + // independent of the blink phase (no history, no drift) + float dOld = b.d; + float dNew = dOld + b.speed; + + if (cameOn) { + // repaint the whole streak from the start (covers the head dot + // last drawn before the dark frames) + 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? for (int e = 0; e < MAX_EXPL; e++) { @@ -703,7 +948,7 @@ static void updateBombs() if (ddx * ddx + ddy * ddy < ex.r * ex.r) { b.alive = false; waveKills++; - score += 25 * scoreMult(); + addScore(25 * scoreMult()); eraseBombTrail(b); // also refreshes the HUD with the new score spawnExpl(b.x, b.y, 14.0f, 1.4f); sfxKill(); @@ -712,11 +957,11 @@ static void updateBombs() } if (!b.alive) continue; - // reached the ground? - if (b.y >= b.ty) { + // reached the target? + if (b.d >= b.len) { b.alive = false; eraseBombTrail(b); - spawnExpl(b.tx, groundY - 4, 11.0f, 1.3f); + spawnExpl(b.x, groundY - 4, 11.0f, 1.3f); sfxBoom(); } } @@ -774,6 +1019,15 @@ static void updateExpls() float dy = (groundY - 4) - e.y; 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 // --------------------------------------------------------------------------- +// 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() { 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_ROCKETS; i++) rockets[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; wave = 0; + citiesLostWave = 0; + reserveCities = 0; + nextCityAt = 10000; + cityOSDUntil = 0; cross.x = cross.dx = scrW / 2; cross.y = cross.dy = scrH / 2; } @@ -800,8 +1069,12 @@ static void resetGame() static void startWave(int 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++) { - if (batAlive[i]) batAmmo[i] = AMMO_MAX; + batAlive[i] = true; + batAmmo[i] = AMMO_MAX; } // every third wave one destroyed city is rebuilt if (n > 1 && (n - 1) % 3 == 0) { @@ -820,11 +1093,17 @@ static void startWave(int n) if (spawnIntervalMs < 500) spawnIntervalMs = 500; bombsSpawned = 0; waveKills = 0; + citiesLostWave = 0; // fresh city-kill budget each wave spawningStopped = false; lastSpawnMs = millis(); 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.setTextColor(TFT_YELLOW, TFT_BLACK); tft.setTextDatum(MC_DATUM); @@ -851,6 +1130,16 @@ static void enterBonus() { 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; for (int i = 0; i < CITY_COUNT; i++) if (cityAlive[i]) aliveCities++; @@ -860,8 +1149,7 @@ static void enterBonus() int mult = scoreMult(); int cityPts = aliveCities * 100 * mult; int ammoPts = ammoLeft * 5 * mult; - score += cityPts + ammoPts; - if (score > highScore) highScore = score; + addScore(cityPts + ammoPts); int px = 60, pw = scrW - 120, py = 64, ph = 108; tft.fillRect(px, py, pw, ph, TFT_BLACK); @@ -872,11 +1160,18 @@ static void enterBonus() tft.setTextDatum(MC_DATUM); tft.drawString("WAVE " + String(wave) + " CLEARED", scrW / 2, py + 12); tft.setTextColor(TFT_WHITE, TFT_BLACK); - tft.drawString("MISSILES " + String(waveKills) + " x " + String(25 * mult), scrW / 2, py + 34); - tft.drawString("CITIES " + String(aliveCities) + " x " + String(100 * mult), scrW / 2, py + 52); - tft.drawString("AMMO " + String(ammoLeft) + " x " + String(5 * mult), scrW / 2, py + 70); + 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 + 46); + 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.drawString("SCORE " + String(score), scrW / 2, py + 90); + tft.drawString("SCORE " + String(score), scrW / 2, py + 94); gameState = ST_BONUS; stateT0 = millis(); @@ -965,8 +1260,7 @@ static void pollTrackball() switch (gameState) { case ST_TITLE: - resetGame(); - startWave(1); + beginGame(); break; case ST_BONUS: startWave(wave + 1); @@ -997,8 +1291,7 @@ static void pollTouch() wasDown = true; switch (gameState) { case ST_TITLE: - resetGame(); - startWave(1); + beginGame(); break; case ST_BONUS: startWave(wave + 1); @@ -1055,8 +1348,7 @@ static void pollKeyboard() switch (gameState) { case ST_TITLE: - resetGame(); - startWave(1); + beginGame(); break; case ST_BONUS: startWave(wave + 1); @@ -1098,6 +1390,7 @@ static void loopPlaying() updateRockets(); updateBombs(); + updateCrafts(); updateExpls(); renderCross(); @@ -1267,4 +1560,5 @@ void loop() } drawVolumeOSD(); + drawBonusCityOSD(); } diff --git a/examples/MissileCommand/sound_engine.cpp b/examples/MissileCommand/sound_engine.cpp index fdc75e8..86b896f 100644 --- a/examples/MissileCommand/sound_engine.cpp +++ b/examples/MissileCommand/sound_engine.cpp @@ -72,6 +72,14 @@ void SoundEngine::task() case SND_SWEEP: renderSweep(e.freq, e.freq2, 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); + } } }