Combat half of the MVP vertical slice on the shared coordinate space: - 2 marines (BFS to the player beacon, stop-and-fire hitscan), cover crates halve incoming hit chance, overhead + HUD HP meters - patrol/stationary security droids, heavy droid + ceiling turret on the objective terminal; feedback spike now spawns a reinforcement droid - win: marine reaches the terminal (BREACH SUCCESS); loss: SQUAD KIA; R resets either way; tracers dim in Network view - firmware build fix: Arduino auto-prototypes break Unit& params (planPath/unitMove/inCover/acquireTarget now take unit indices) - HUD fix: R:RESET no longer overlaps RESUME/PAUSE; daemon slots and SQUAD row spaced out - STATUS.md: M3 section incl. scope note — BREACH SUCCESS is gated on the M4 FRENZY/BLIND daemons; loss path + reset verified headlessly
1417 lines
47 KiB
Arduino
1417 lines
47 KiB
Arduino
/**
|
|
* @file NetDeck.ino
|
|
* @brief NetDeck — Breach & Board: Milestone 2 — network graph & traces.
|
|
*
|
|
* Milestone 3 (DESIGN.md §7): adds the squad-vs-droids combat half of the
|
|
* MVP vertical slice (MVP.md §1) on top of the M2 sub-net:
|
|
*
|
|
* - 3-room floorplan (airlock / main corridor / server room) with one
|
|
* interactive blast door and the objective terminal
|
|
* - 4 hardcoded nodes in the same coordinate space:
|
|
* [AIRLK] root (always online) - [JUNC] - [DOOR] / [HIVE] (3 s ICE)
|
|
* the [DOOR] node sits exactly on its blast-door tile
|
|
* - click an adjacent node to route a link: animated dotted line that
|
|
* turns solid over 1.5 s (3.0 s when ICE-locked); X severs the hovered
|
|
* link (everything beyond it drops, ICE progress resets)
|
|
* - [DOOR] node (once online) toggles the blast door open/closed
|
|
* - cracking [HIVE] spawns a red trace pulse: 4 s back to [AIRLK];
|
|
* arrival = feedback spike (links drop + 3 s OVERHEAT lockout)
|
|
* - HUD: [1:FRENZY] [2:BLIND] slots (effects land in M4), TEMP bar,
|
|
* RESET (R); boot goes straight into the paused encounter
|
|
* - 2 marines (BFS to the player-placed beacon, stop-and-fire hitscan),
|
|
* 2 corridor droids (1 patrol + 1 guard), heavy droid + ceiling turret
|
|
* guarding the terminal; cover crates halve incoming hit chance;
|
|
* a feedback spike spawns a reinforcement droid
|
|
* - win: a marine reaches the objective terminal (BREACH SUCCESS);
|
|
* loss: both marines down (SQUAD KIA) — R resets either way
|
|
*
|
|
* Run on the desktop: make APP=NetDeck run (from emulator/)
|
|
* Headless smoke test: make smoke APP=NetDeck
|
|
*
|
|
* Controls (emulator / device):
|
|
* Trackball move cursor (tiles in Cameras, nodes/links in Network)
|
|
* Enter / click trackball press / screen tap = primary action
|
|
* Space toggle tactical freeze (starts PAUSED)
|
|
* P (or Tab) switch view: Cameras <-> Network
|
|
* X sever the hovered network link
|
|
* R reset the scenario
|
|
* 1 / 2 FRENZY / BLIND daemons (need an online [HIVE] link; M4)
|
|
*/
|
|
|
|
#include <Arduino.h>
|
|
#include <SPI.h>
|
|
#include <TFT_eSPI.h>
|
|
#include <Wire.h>
|
|
#include "utilities.h"
|
|
#include "TouchDrvGT911.hpp"
|
|
#include "../../src/netdeck/nd_layout.h"
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Hardware + full-frame rendering (M1 anti-flicker pattern)
|
|
// ---------------------------------------------------------------------------
|
|
TFT_eSPI tft;
|
|
TouchDrvGT911 touch;
|
|
static bool touchAvail = false;
|
|
|
|
#ifndef EMULATOR
|
|
static TFT_eSprite fb(&tft); // full-frame 320x240x16 buffer (PSRAM)
|
|
static TFT_eSprite *const GFXP = &fb;
|
|
#else
|
|
static TFT_eSPI *const GFXP = &tft;
|
|
#endif
|
|
|
|
#ifndef EMULATOR
|
|
static void present()
|
|
{
|
|
if (!fb.created()) return;
|
|
tft.startWrite();
|
|
fb.pushSprite(0, 0);
|
|
tft.endWrite();
|
|
}
|
|
#else
|
|
static void present() {} // shim blits the SDL framebuffer
|
|
#endif
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Game state
|
|
// ---------------------------------------------------------------------------
|
|
enum GameState { ST_BOOT, ST_RUN };
|
|
static GameState gameState = ST_BOOT;
|
|
enum ViewMode { VM_CAMERAS, VM_NETWORK };
|
|
static ViewMode viewMode = VM_CAMERAS;
|
|
enum Scenario { SC_LIVE, SC_WIN, SC_LOSS };
|
|
static Scenario scenario = SC_LIVE;
|
|
|
|
static uint32_t stateT0 = 0;
|
|
static bool paused = true; // MVP: boot straight into the paused encounter
|
|
static uint32_t simTick = 0;
|
|
static uint32_t simAcc = 0;
|
|
static uint32_t lastFrameMs = 0;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Floorplan (MVP.md §1): 40x24 tiles, 3 rooms
|
|
// '#' wall '.' floor 'o' cover crate '+' blast door 'A' airlock 'T' terminal
|
|
// Room 1 airlock (bottom-left), Room 2 main corridor (L-shaped middle),
|
|
// Room 3 server room (right). The '+' door is the Room2/Room3 blast door.
|
|
// ---------------------------------------------------------------------------
|
|
static const char *const DEMO_MAP[nd::TILE_ROWS] = {
|
|
"########################################",
|
|
"#.........#.................#..........#",
|
|
"#.........#.................#..........#",
|
|
"#.........#.................#..........#",
|
|
"#....o....#.................#..........#",
|
|
"#.........#.................#..........#",
|
|
"#.........#.................#..........#",
|
|
"#.........#.................#.....T....#",
|
|
"#...........................#..........#",
|
|
"#...........................#..........#",
|
|
"#.........#...o.............#..........#",
|
|
"#.........#.................#..........#",
|
|
"#.........#.................+..........#",
|
|
"###########.................#..........#",
|
|
"#.........#.................#..........#",
|
|
"#.........#...........o.....#..........#",
|
|
"#.........#.................#..........#",
|
|
"#...........................#..........#",
|
|
"#...........................#..........#",
|
|
"#.........#.................#..........#",
|
|
"#.........#.................#..........#",
|
|
"#.A.......#.................#..........#",
|
|
"#.........#.................#..........#",
|
|
"########################################",
|
|
};
|
|
|
|
static const int DOOR_TX = 28, DOOR_TY = 12; // the Room2/Room3 blast door
|
|
static const int AIRLOCK_TX = 2, AIRLOCK_TY = 21;
|
|
|
|
static bool doorOpen = false;
|
|
|
|
static char tileAt(int tx, int ty)
|
|
{
|
|
if (tx < 0 || ty < 0 || tx >= nd::TILE_COLS || ty >= nd::TILE_ROWS) return '#';
|
|
return DEMO_MAP[ty][tx];
|
|
}
|
|
|
|
// blocks unit movement (closed blast door included)
|
|
static bool walkable(int tx, int ty)
|
|
{
|
|
char t = tileAt(tx, ty);
|
|
if (t == '+' && !doorOpen) return false;
|
|
return t == '.' || t == 'A' || t == 'T' || t == '+';
|
|
}
|
|
|
|
// blocks line-of-sight raycasts
|
|
static bool blocksSight(int tx, int ty)
|
|
{
|
|
char t = tileAt(tx, ty);
|
|
return t == '#' || (t == '+' && !doorOpen);
|
|
}
|
|
|
|
static int tileIdx(int tx, int ty) { return ty * nd::TILE_COLS + tx; }
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Sub-net (MVP.md §2): 4 nodes in the same coordinate space
|
|
// ---------------------------------------------------------------------------
|
|
enum { N_AIRLK, N_JUNC, N_DOOR, N_HIVE };
|
|
struct NetNode {
|
|
const char *label;
|
|
int tx, ty;
|
|
uint16_t color;
|
|
};
|
|
static const NetNode NODES[4] = {
|
|
{ "AIRLK", 4, 20, nd::C_AIRLOCK },
|
|
{ "JUNC", 18, 6, nd::C_NODE },
|
|
{ "DOOR", 28, 12, nd::C_DOOR },
|
|
{ "HIVE", 35, 4, nd::C_HIVE },
|
|
};
|
|
static const uint8_t EDGES[3][2] = {
|
|
{ N_AIRLK, N_JUNC }, { N_JUNC, N_DOOR }, { N_JUNC, N_HIVE },
|
|
};
|
|
static const char *EDGE_LABELS[3] = { "AIRLK-JUNC", "JUNC-DOOR", "JUNC-HIVE" };
|
|
|
|
static int nodePx(int i) { return NODES[i].tx * nd::TILE + nd::TILE / 2; }
|
|
static int nodePy(int i) { return NODES[i].ty * nd::TILE + nd::TILE / 2; }
|
|
|
|
// link build durations (sim ticks @ 50 ms): 1.5 s plain, 3.0 s through ICE
|
|
static const int LINK_TICKS = 30;
|
|
static const int ICE_TICKS = 60;
|
|
// red trace: 4 s from HIVE back to AIRLK
|
|
static const int TRACE_TICKS = 80;
|
|
// overheat lockout after a feedback spike
|
|
static const int HEAT_TICKS = 60;
|
|
|
|
struct NetLink {
|
|
bool active;
|
|
uint8_t a, b; // node ids
|
|
int prog; // build progress in ticks (prog == dur -> ACTIVE)
|
|
int dur;
|
|
};
|
|
static NetLink links[3];
|
|
|
|
static bool nodeOnline[4]; // compromised & reachable from AIRLK
|
|
static bool hiveCracked = false;
|
|
|
|
// red trace pulse HIVE -> JUNC -> AIRLK
|
|
static bool traceActive = false;
|
|
static int traceProg = 0; // 0..TRACE_TICKS
|
|
|
|
// deck thermal: >0 = OVERHEAT lockout (ticks remaining, shown as %)
|
|
static int heatT = 0;
|
|
|
|
// feedback spike queues a reinforcement droid (spawned by the combat layer)
|
|
static bool reinforcePending = false;
|
|
|
|
// cursor
|
|
static int curX = 60, curY = 120;
|
|
static int beaconTX = AIRLOCK_TX, beaconTY = AIRLOCK_TY;
|
|
|
|
// transient UI feedback
|
|
static uint32_t viewFlashUntil = 0;
|
|
static char msgText[24] = "";
|
|
static uint32_t msgUntil = 0;
|
|
|
|
static void flashMsg(const char *t)
|
|
{
|
|
snprintf(msgText, sizeof(msgText), "%s", t);
|
|
msgUntil = millis() + 1200;
|
|
Serial.printf("[msg] %s\n", t);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Sub-net logic
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// reachability from AIRLK over ACTIVE links (links form a tree)
|
|
static void refreshOnline()
|
|
{
|
|
bool reach[4] = { true, false, false, false };
|
|
bool changed = true;
|
|
while (changed) {
|
|
changed = false;
|
|
for (auto &l : links) {
|
|
if (!l.active || l.prog != l.dur) continue;
|
|
if (reach[l.a] != reach[l.b]) {
|
|
reach[l.a] = reach[l.b] = true;
|
|
changed = true;
|
|
}
|
|
}
|
|
}
|
|
for (int i = 0; i < 4; i++) nodeOnline[i] = reach[i];
|
|
if (!nodeOnline[N_HIVE]) hiveCracked = false;
|
|
}
|
|
|
|
// is there an edge between two nodes?
|
|
static int edgeBetween(int a, int b)
|
|
{
|
|
for (int i = 0; i < 3; i++)
|
|
if ((EDGES[i][0] == a && EDGES[i][1] == b) ||
|
|
(EDGES[i][0] == b && EDGES[i][1] == a)) return i;
|
|
return -1;
|
|
}
|
|
|
|
// is a link already routed on this edge (building or active)?
|
|
static int linkOnEdge(int edge)
|
|
{
|
|
for (int i = 0; i < 3; i++)
|
|
if (links[i].active && edge == i) return i;
|
|
return -1;
|
|
}
|
|
|
|
static void dropLinks()
|
|
{
|
|
for (auto &l : links) l.active = false;
|
|
traceActive = false;
|
|
refreshOnline();
|
|
}
|
|
|
|
static void tryLink(int node)
|
|
{
|
|
if (heatT > 0) { flashMsg("OVERHEAT"); return; }
|
|
if (nodeOnline[node]) {
|
|
if (node == N_DOOR) {
|
|
doorOpen = !doorOpen;
|
|
Serial.printf("[net] DOOR_CTRL: blast door %s\n", doorOpen ? "OPEN" : "CLOSED");
|
|
flashMsg(doorOpen ? "DOOR OPEN" : "DOOR CLOSED");
|
|
return;
|
|
}
|
|
flashMsg("ONLINE");
|
|
return;
|
|
}
|
|
// route a link from any online node adjacent to this one
|
|
int edge = -1, from = -1;
|
|
for (int i = 0; i < 3; i++) {
|
|
int a = EDGES[i][0], b = EDGES[i][1];
|
|
if ((a == node && nodeOnline[b]) || (b == node && nodeOnline[a])) {
|
|
edge = i;
|
|
from = (a == node) ? b : a;
|
|
break;
|
|
}
|
|
}
|
|
if (edge < 0) { flashMsg("NO UPLINK"); return; }
|
|
if (linkOnEdge(edge) >= 0) { flashMsg("LINKING..."); return; }
|
|
|
|
bool ice = (node == N_HIVE);
|
|
for (auto &l : links) {
|
|
if (!l.active) {
|
|
l.active = true;
|
|
l.a = from;
|
|
l.b = node;
|
|
l.prog = 0;
|
|
l.dur = ice ? ICE_TICKS : LINK_TICKS;
|
|
refreshOnline(); // offline until the build completes
|
|
Serial.printf("[net] routing %s%s (%.1fs)\n", EDGE_LABELS[edge],
|
|
ice ? " [ICE]" : "", l.dur * nd::SIM_TICK_MS / 1000.0f);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// sever the link (and everything that loses its path to AIRLK)
|
|
static void severLink(int li)
|
|
{
|
|
Serial.printf("[net] severed %s\n", EDGE_LABELS[edgeBetween(links[li].a, links[li].b)]);
|
|
links[li].active = false;
|
|
refreshOnline();
|
|
if (!nodeOnline[N_HIVE]) traceActive = false; // trace cut short: reset safely
|
|
flashMsg("LINK SEVERED");
|
|
}
|
|
|
|
// distance cursor->link segment; returns the hovered link or -1
|
|
static int hoverLink()
|
|
{
|
|
int best = -1;
|
|
float bestD = 7.0f;
|
|
for (int i = 0; i < 3; i++) {
|
|
if (!links[i].active) continue;
|
|
float x0 = nodePx(links[i].a), y0 = nodePy(links[i].a);
|
|
float x1 = nodePx(links[i].b), y1 = nodePy(links[i].b);
|
|
float dx = x1 - x0, dy = y1 - y0;
|
|
float len2 = dx * dx + dy * dy;
|
|
float t = ((curX - x0) * dx + (curY - y0) * dy) / len2;
|
|
if (t < 0) t = 0;
|
|
if (t > 1) t = 1;
|
|
float px = x0 + t * dx, py = y0 + t * dy;
|
|
float d = hypotf(curX - px, curY - py);
|
|
if (d < bestD) { bestD = d; best = i; }
|
|
}
|
|
return best;
|
|
}
|
|
|
|
static void spawnTrace()
|
|
{
|
|
traceActive = true;
|
|
traceProg = 0;
|
|
Serial.println("[net] TRACE detected: red pulse inbound!");
|
|
}
|
|
|
|
static void feedbackSpike()
|
|
{
|
|
traceActive = false;
|
|
dropLinks();
|
|
heatT = HEAT_TICKS;
|
|
reinforcePending = true;
|
|
Serial.println("[net] FEEDBACK SPIKE: links dropped, OVERHEAT 3s, reinforcement inbound");
|
|
flashMsg("FEEDBACK!");
|
|
}
|
|
|
|
static void stepNetwork()
|
|
{
|
|
// link builds
|
|
for (auto &l : links) {
|
|
if (!l.active || l.prog == l.dur) continue;
|
|
l.prog++;
|
|
if (l.prog == l.dur) {
|
|
refreshOnline();
|
|
if (l.b == N_HIVE && nodeOnline[N_HIVE]) {
|
|
hiveCracked = true;
|
|
spawnTrace();
|
|
} else {
|
|
Serial.printf("[net] link up: %s\n", EDGE_LABELS[edgeBetween(l.a, l.b)]);
|
|
}
|
|
}
|
|
}
|
|
// red trace
|
|
if (traceActive) {
|
|
traceProg++;
|
|
if (traceProg >= TRACE_TICKS) feedbackSpike();
|
|
}
|
|
// thermal cooldown
|
|
if (heatT > 0) {
|
|
heatT--;
|
|
if (heatT == 0) Serial.println("[net] deck cooled: systems online");
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Squad & droids (MVP.md §1) — autonomous entities in the shared space
|
|
// ---------------------------------------------------------------------------
|
|
enum { U_MARINE, U_DROID, U_HEAVY, U_TURRET };
|
|
|
|
struct UnitStat {
|
|
uint16_t rng2; // fire range squared, in half-tiles
|
|
uint8_t cd; // shot cooldown (ticks)
|
|
uint8_t acc; // base hit chance %
|
|
uint8_t dmg; // damage per hit
|
|
uint8_t speed; // ticks per tile step (0 = stationary)
|
|
uint8_t maxHp;
|
|
};
|
|
static const UnitStat STATS[4] = {
|
|
{ 16 * 16, 5, 65, 1, 4, 6 }, // marine: rng 8.0, 4 shots / 2 s
|
|
{ 14 * 14, 6, 55, 1, 5, 4 }, // droid: rng 7.0
|
|
{ 14 * 14, 7, 70, 2, 7, 10 }, // heavy: rng 7.0, 2 dmg
|
|
{ 21 * 21, 4, 80, 1, 0, 60 }, // turret: rng 10.5, high ROF
|
|
};
|
|
|
|
enum { TEAM_MARINE = 0, TEAM_DROID = 1 };
|
|
|
|
struct Unit {
|
|
bool alive;
|
|
uint8_t kind, team;
|
|
int tx, ty; // tile position
|
|
int hp;
|
|
int path[64]; // BFS waypoint tile indices
|
|
int pathLen, pathIdx;
|
|
int goalTX, goalTY; // what the current path leads to
|
|
bool hasPath;
|
|
int mvCd, fireCd;
|
|
int tgt; // unit index or -1
|
|
int patA, patB; // patrol endpoints as tileIdx (-1 = stationary)
|
|
bool patFlip;
|
|
};
|
|
static Unit units[7];
|
|
// 0,1 marines | 2 patrol droid | 3 stationary droid | 4 heavy | 5 turret
|
|
// 6 = reinforcement slot (spawns on the feedback spike)
|
|
|
|
// transient hitscan tracers / impact flashes
|
|
struct Tracer {
|
|
int x0, y0, x1, y1, ttl;
|
|
uint16_t color;
|
|
bool impact; // draw a burst at the endpoint instead of a line
|
|
};
|
|
static Tracer tracers[8];
|
|
|
|
static int unitPx(int i) { return units[i].tx * nd::TILE + nd::TILE / 2; }
|
|
static int unitPy(int i) { return units[i].ty * nd::TILE + nd::TILE / 2; }
|
|
|
|
static void spawnUnit(int i, uint8_t kind, int tx, int ty)
|
|
{
|
|
Unit &u = units[i];
|
|
u.alive = true;
|
|
u.kind = kind;
|
|
u.team = (kind == U_MARINE) ? TEAM_MARINE : TEAM_DROID;
|
|
u.tx = tx;
|
|
u.ty = ty;
|
|
u.hp = STATS[kind].maxHp;
|
|
u.pathLen = u.pathIdx = 0;
|
|
u.goalTX = u.goalTY = -1;
|
|
u.hasPath = false;
|
|
u.mvCd = u.fireCd = 0;
|
|
u.tgt = -1;
|
|
u.patA = u.patB = -1;
|
|
u.patFlip = false;
|
|
}
|
|
|
|
static void addTracer(int x0, int y0, int x1, int y1, uint16_t color, bool impact)
|
|
{
|
|
for (auto &t : tracers) {
|
|
if (t.ttl > 0) continue;
|
|
t.x0 = x0; t.y0 = y0; t.x1 = x1; t.y1 = y1;
|
|
t.ttl = 2;
|
|
t.color = color;
|
|
t.impact = impact;
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Bresenham tile raycast: walls and closed blast doors block sight
|
|
static bool losClear(int x0, int y0, int x1, int y1)
|
|
{
|
|
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
|
|
int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
|
|
int err = dx + dy;
|
|
int x = x0, y = y0;
|
|
for (;;) {
|
|
if (!(x == x0 && y == y0) && !(x == x1 && y == y1) && blocksSight(x, y))
|
|
return false;
|
|
if (x == x1 && y == y1) break;
|
|
int e2 = 2 * err;
|
|
if (e2 >= dy) { err += dy; x += sx; }
|
|
if (e2 <= dx) { err += dx; y += sy; }
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// adjacent to a cover crate? (MVP: halves incoming hit chance)
|
|
// (unit index, not a reference — see the planPath() note)
|
|
static bool inCover(int ui)
|
|
{
|
|
const Unit &u = units[ui];
|
|
static const int DX[4] = { 1, -1, 0, 0 }, DY[4] = { 0, 0, 1, -1 };
|
|
for (int d = 0; d < 4; d++)
|
|
if (tileAt(u.tx + DX[d], u.ty + DY[d]) == 'o') return true;
|
|
return false;
|
|
}
|
|
|
|
// nearest living enemy within range + LOS (unit index — see planPath() note)
|
|
static int acquireTarget(int si)
|
|
{
|
|
const Unit &u = units[si];
|
|
int best = -1;
|
|
long bestD = 1L << 30;
|
|
for (int i = 0; i < 7; i++) {
|
|
if (i == si || !units[i].alive) continue;
|
|
if (units[i].team == u.team) continue;
|
|
int ddx = (u.tx - units[i].tx) * 2, ddy = (u.ty - units[i].ty) * 2;
|
|
long d2 = (long)ddx * ddx + (long)ddy * ddy;
|
|
if (d2 > STATS[u.kind].rng2) continue;
|
|
if (!losClear(u.tx, u.ty, units[i].tx, units[i].ty)) continue;
|
|
if (d2 < bestD) { bestD = d2; best = i; }
|
|
}
|
|
return best;
|
|
}
|
|
|
|
// BFS over walkable tiles; returns step count written to out (or -1)
|
|
static int bfsPath(int sx, int sy, int gx, int gy, int *out, int cap)
|
|
{
|
|
if (!walkable(gx, gy) || !walkable(sx, sy)) return -1;
|
|
if (sx == gx && sy == gy) return 0;
|
|
static int16_t par[nd::TILE_COLS * nd::TILE_ROWS];
|
|
static int16_t q[nd::TILE_COLS * nd::TILE_ROWS];
|
|
for (int i = 0; i < nd::TILE_COLS * nd::TILE_ROWS; i++) par[i] = -1;
|
|
int start = tileIdx(sx, sy), goal = tileIdx(gx, gy);
|
|
int qh = 0, qt = 0;
|
|
q[qt++] = (int16_t)start;
|
|
par[start] = (int16_t)start;
|
|
bool found = false;
|
|
while (qh < qt) {
|
|
int cur = q[qh++];
|
|
if (cur == goal) { found = true; break; }
|
|
int cx = cur % nd::TILE_COLS, cy = cur / nd::TILE_COLS;
|
|
for (int d = 0; d < 4; d++) {
|
|
static const int DX[4] = { 1, -1, 0, 0 }, DY[4] = { 0, 0, 1, -1 };
|
|
int nx = cx + DX[d], ny = cy + DY[d];
|
|
if (!walkable(nx, ny)) continue;
|
|
int ni = tileIdx(nx, ny);
|
|
if (par[ni] != -1) continue;
|
|
par[ni] = (int16_t)cur;
|
|
q[qt++] = (int16_t)ni;
|
|
}
|
|
}
|
|
if (!found) return -1;
|
|
int rev[64], n = 0;
|
|
int cur = goal;
|
|
while (cur != start) {
|
|
if (n >= cap) return -1;
|
|
rev[n++] = cur;
|
|
cur = par[cur];
|
|
}
|
|
for (int i = 0; i < n; i++) out[i] = rev[n - 1 - i];
|
|
return n;
|
|
}
|
|
|
|
// (re)plan a unit's path toward a goal tile
|
|
// NOTE: takes a unit *index*, not a reference — the Arduino preprocessor
|
|
// injects prototypes above the Unit struct, and reference params of a
|
|
// user type break the generated firmware build (the emulator never sees it).
|
|
static void planPath(int i, int gx, int gy)
|
|
{
|
|
Unit &u = units[i];
|
|
u.pathLen = bfsPath(u.tx, u.ty, gx, gy, u.path, 64);
|
|
u.pathIdx = 0;
|
|
u.goalTX = gx;
|
|
u.goalTY = gy;
|
|
u.hasPath = u.pathLen > 0;
|
|
}
|
|
|
|
static void unitKill(int i)
|
|
{
|
|
Unit &u = units[i];
|
|
u.alive = false;
|
|
u.tgt = -1;
|
|
for (auto &o : units) if (o.tgt == i) o.tgt = -1;
|
|
Serial.printf("[combat] %s destroyed at %d,%d\n",
|
|
u.kind == U_MARINE ? "MARINE" :
|
|
u.kind == U_HEAVY ? "HEAVY DROID" :
|
|
u.kind == U_TURRET ? "TURRET" : "SECURITY DROID", u.tx, u.ty);
|
|
}
|
|
|
|
static void fireAt(int si, int ti)
|
|
{
|
|
Unit &s = units[si];
|
|
Unit &t = units[ti];
|
|
s.fireCd = STATS[s.kind].cd;
|
|
int acc = STATS[s.kind].acc;
|
|
if (inCover(ti)) acc /= 2; // MVP cover rule
|
|
bool hit = (int)random(100) < acc;
|
|
uint16_t col = s.team == TEAM_MARINE ? nd::C_PACKET : nd::C_TRACE;
|
|
if (hit) {
|
|
addTracer(unitPx(si), unitPy(si), unitPx(ti), unitPy(ti), col, true);
|
|
t.hp -= STATS[s.kind].dmg;
|
|
Serial.printf("[combat] %c%d hits %c%d (%d hp left)\n",
|
|
s.team == TEAM_MARINE ? 'M' : 'D', si,
|
|
t.team == TEAM_MARINE ? 'M' : 'D', ti, t.hp > 0 ? t.hp : 0);
|
|
if (t.hp <= 0) unitKill(ti);
|
|
} else {
|
|
addTracer(unitPx(si), unitPy(si), unitPx(ti), unitPy(ti), col, false);
|
|
Serial.printf("[combat] %c%d misses %c%d\n",
|
|
s.team == TEAM_MARINE ? 'M' : 'D', si,
|
|
t.team == TEAM_MARINE ? 'M' : 'D', ti);
|
|
}
|
|
}
|
|
|
|
// any living unit standing on this tile? (units may not share a tile)
|
|
static bool tileOccupied(int tx, int ty, int self)
|
|
{
|
|
for (int i = 0; i < 7; i++) {
|
|
if (i == self || !units[i].alive) continue;
|
|
if (units[i].tx == tx && units[i].ty == ty) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// move one tile along the current path when the step timer expires
|
|
// (unit index, not a reference — see the planPath() note)
|
|
static void unitMove(int i)
|
|
{
|
|
Unit &u = units[i];
|
|
const int self = i;
|
|
if (!u.hasPath) return;
|
|
if (u.mvCd > 0) { u.mvCd--; return; }
|
|
if (u.pathIdx >= u.pathLen) { u.hasPath = false; return; }
|
|
int next = u.path[u.pathIdx++];
|
|
int nx = next % nd::TILE_COLS, ny = next / nd::TILE_COLS;
|
|
if (!walkable(nx, ny) || tileOccupied(nx, ny, self)) {
|
|
u.hasPath = false; // door shut / another unit in the way
|
|
return;
|
|
}
|
|
u.tx = nx;
|
|
u.ty = ny;
|
|
u.mvCd = STATS[u.kind].speed;
|
|
if (u.pathIdx >= u.pathLen) u.hasPath = false;
|
|
}
|
|
|
|
static void stepUnits()
|
|
{
|
|
// tracer decay runs even while a banner is up
|
|
for (auto &t : tracers) if (t.ttl > 0) t.ttl--;
|
|
|
|
if (scenario != SC_LIVE) return;
|
|
|
|
// reinforcement droid queued by the feedback spike
|
|
if (reinforcePending) {
|
|
reinforcePending = false;
|
|
if (!units[6].alive) {
|
|
// spawn inside the server room by the blast door, first free tile
|
|
static const int SP[5][2] = { {31,12}, {30,12}, {32,12}, {31,11}, {31,13} };
|
|
for (int s = 0; s < 5; s++) {
|
|
int sx = SP[s][0], sy = SP[s][1];
|
|
if (walkable(sx, sy) && !tileOccupied(sx, sy, 6)) {
|
|
spawnUnit(6, U_DROID, sx, sy);
|
|
Serial.printf("[spawn] reinforcement droid at %d,%d\n", sx, sy);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < 7; i++) {
|
|
Unit &u = units[i];
|
|
if (!u.alive) continue;
|
|
|
|
// combat: stop and fire at the nearest enemy in range + LOS
|
|
if (u.fireCd > 0) u.fireCd--;
|
|
u.tgt = acquireTarget(i);
|
|
if (u.tgt >= 0) {
|
|
if (u.fireCd == 0) fireAt(i, u.tgt);
|
|
continue; // hold position while firing
|
|
}
|
|
|
|
// movement goals
|
|
if (u.kind == U_MARINE) {
|
|
if (!u.hasPath || u.goalTX != beaconTX || u.goalTY != beaconTY ||
|
|
(simTick & 7) == 0)
|
|
planPath(i, beaconTX, beaconTY);
|
|
unitMove(i);
|
|
} else if (u.patA >= 0) { // patrolling droid
|
|
if (!u.hasPath) {
|
|
int goal = u.patFlip ? u.patA : u.patB;
|
|
planPath(i, goal % nd::TILE_COLS, goal / nd::TILE_COLS);
|
|
u.patFlip = !u.patFlip;
|
|
}
|
|
unitMove(i);
|
|
}
|
|
// stationary droids / heavy / turret hold position
|
|
|
|
// objective + wipe checks
|
|
if (u.kind == U_MARINE && u.tx == 34 && u.ty == 7) {
|
|
scenario = SC_WIN;
|
|
Serial.println("[objective] terminal reached — BREACH SUCCESS");
|
|
flashMsg("BREACH SUCCESS");
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (!units[0].alive && !units[1].alive && scenario == SC_LIVE) {
|
|
scenario = SC_LOSS;
|
|
Serial.println("[objective] squad wiped — SQUAD KIA");
|
|
flashMsg("SQUAD KIA");
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Scenario reset (MVP: R replays the 60-second scenario instantly)
|
|
// ---------------------------------------------------------------------------
|
|
static void resetScenario()
|
|
{
|
|
for (auto &l : links) l.active = false;
|
|
traceActive = false;
|
|
traceProg = 0;
|
|
hiveCracked = false;
|
|
heatT = 0;
|
|
doorOpen = false;
|
|
reinforcePending = false;
|
|
for (auto &t : tracers) t.ttl = 0;
|
|
refreshOnline(); // AIRLK root comes up online
|
|
|
|
// squad: 2 marines in the airlock (out of enemy LOS)
|
|
spawnUnit(0, U_MARINE, 2, 21);
|
|
spawnUnit(1, U_MARINE, 3, 21);
|
|
// corridor: 1 patrolling + 1 stationary security droid
|
|
spawnUnit(2, U_DROID, 12, 2);
|
|
units[2].patA = tileIdx(12, 2);
|
|
units[2].patB = tileIdx(26, 18);
|
|
units[2].patFlip = false;
|
|
spawnUnit(3, U_DROID, 20, 12);
|
|
// server room: heavy droid + ceiling turret guarding the terminal
|
|
spawnUnit(4, U_HEAVY, 32, 10);
|
|
spawnUnit(5, U_TURRET, 33, 5);
|
|
units[6].alive = false; // reinforcement slot
|
|
|
|
scenario = SC_LIVE;
|
|
paused = true;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Trackball input — edge-counting ISRs + momentum (unchanged from M1)
|
|
// ---------------------------------------------------------------------------
|
|
static volatile uint32_t tbEdges[4];
|
|
static volatile uint32_t tbClicks;
|
|
|
|
static void IRAM_ATTR tbIsrRight() { tbEdges[0]++; }
|
|
static void IRAM_ATTR tbIsrUp() { tbEdges[1]++; }
|
|
static void IRAM_ATTR tbIsrLeft() { tbEdges[2]++; }
|
|
static void IRAM_ATTR tbIsrDown() { tbEdges[3]++; }
|
|
static void IRAM_ATTR tbIsrClick() { tbClicks++; }
|
|
|
|
static const int TB_FP = 8;
|
|
#ifdef EMULATOR
|
|
static const int TB_IMPULSE = 6;
|
|
static const int TB_FRICTION = 12;
|
|
#else
|
|
static const int TB_IMPULSE = 24;
|
|
static const int TB_FRICTION = 15;
|
|
#endif
|
|
static const int TB_VMAX = 24 << TB_FP;
|
|
|
|
static int32_t tbVelX, tbVelY;
|
|
static int32_t tbRemX, tbRemY;
|
|
|
|
static void attachTrackball()
|
|
{
|
|
static const uint8_t tbPins[5] = {
|
|
BOARD_TBOX_G02, BOARD_TBOX_G01, BOARD_TBOX_G04, BOARD_TBOX_G03, BOARD_BOOT_PIN
|
|
};
|
|
for (int i = 0; i < 5; i++) pinMode(tbPins[i], INPUT_PULLUP);
|
|
attachInterrupt(digitalPinToInterrupt(tbPins[0]), tbIsrRight, FALLING);
|
|
attachInterrupt(digitalPinToInterrupt(tbPins[1]), tbIsrUp, FALLING);
|
|
attachInterrupt(digitalPinToInterrupt(tbPins[2]), tbIsrLeft, FALLING);
|
|
attachInterrupt(digitalPinToInterrupt(tbPins[3]), tbIsrDown, FALLING);
|
|
attachInterrupt(digitalPinToInterrupt(tbPins[4]), tbIsrClick, FALLING);
|
|
}
|
|
|
|
static void resetTrackball()
|
|
{
|
|
for (int i = 0; i < 4; i++) tbEdges[i] = 0;
|
|
tbClicks = 0;
|
|
tbVelX = tbVelY = 0;
|
|
tbRemX = tbRemY = 0;
|
|
}
|
|
|
|
static void clampCursor()
|
|
{
|
|
if (curX < 0) curX = 0;
|
|
if (curX > nd::VIEW_W - 1) curX = nd::VIEW_W - 1;
|
|
if (curY < 0) curY = 0;
|
|
if (curY > nd::VIEW_H - 1) curY = nd::VIEW_H - 1;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Primary action
|
|
// ---------------------------------------------------------------------------
|
|
static void registerClick()
|
|
{
|
|
if (viewMode == VM_CAMERAS) {
|
|
int tx = curX / nd::TILE, ty = curY / nd::TILE;
|
|
if (walkable(tx, ty)) {
|
|
beaconTX = tx;
|
|
beaconTY = ty;
|
|
Serial.printf("[click] beacon -> tile %d,%d\n", tx, ty);
|
|
} else {
|
|
Serial.println("[click] blocked tile: beacon ignored");
|
|
}
|
|
} else {
|
|
// nearest node
|
|
int best = -1, bestD = 1 << 30;
|
|
for (int i = 0; i < 4; i++) {
|
|
int dx = curX - nodePx(i);
|
|
int dy = curY - nodePy(i);
|
|
int d = dx * dx + dy * dy;
|
|
if (d < bestD) { bestD = d; best = i; }
|
|
}
|
|
if (bestD > 24 * 24) { flashMsg("NO NODE"); return; }
|
|
tryLink(best);
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Keyboard device @ I2C 0x55
|
|
// ---------------------------------------------------------------------------
|
|
static void handleKey(char c)
|
|
{
|
|
switch (c) {
|
|
case ' ':
|
|
paused = !paused;
|
|
Serial.printf("[key] tactical freeze %s\n", paused ? "ON" : "OFF");
|
|
break;
|
|
case 'p': case '\t':
|
|
viewMode = (viewMode == VM_CAMERAS) ? VM_NETWORK : VM_CAMERAS;
|
|
viewFlashUntil = millis() + 350;
|
|
Serial.printf("[key] view -> %s\n",
|
|
viewMode == VM_CAMERAS ? "CAMERAS" : "NETWORK");
|
|
break;
|
|
case '1': case '2':
|
|
if (heatT > 0) { flashMsg("OVERHEAT"); break; }
|
|
if (!nodeOnline[N_HIVE]) { flashMsg("NO HIVE LINK"); break; }
|
|
// FRENZY / BLIND payloads land in Milestone 4
|
|
Serial.printf("[key] daemon %c armed (effect lands in M4)\n", c);
|
|
flashMsg("M4 PENDING");
|
|
break;
|
|
case 'x': {
|
|
if (heatT > 0) { flashMsg("OVERHEAT"); break; }
|
|
int li = hoverLink();
|
|
if (li >= 0) severLink(li);
|
|
else flashMsg("NO LINK HERE");
|
|
break;
|
|
}
|
|
case 'r':
|
|
Serial.println("[key] RESET");
|
|
resetScenario();
|
|
Serial.println("[reset] scenario re-armed (paused)");
|
|
break;
|
|
case 'q': case 'e':
|
|
Serial.printf("[key] cycle %s (groundwork)\n", c == 'q' ? "back" : "fwd");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void pollKeyboard()
|
|
{
|
|
static char lastKey = 0;
|
|
Wire.requestFrom((uint8_t)0x55, (uint8_t)1);
|
|
if (Wire.available() == 0) {
|
|
lastKey = 0;
|
|
return;
|
|
}
|
|
char k = (char)Wire.read();
|
|
if (k == 0) { lastKey = 0; return; }
|
|
if (k == lastKey) return;
|
|
lastKey = k;
|
|
handleKey((char)tolower(k));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Touch (screen tap = move cursor + primary action)
|
|
// ---------------------------------------------------------------------------
|
|
static void pollTouch()
|
|
{
|
|
if (!touchAvail) return;
|
|
static bool wasDown = false;
|
|
bool down = touch.isPressed();
|
|
if (!down) { wasDown = false; return; }
|
|
if (wasDown) return; // only act on new taps
|
|
int16_t x[1], y[1];
|
|
int gp = touch.getPoint(x, y, 1);
|
|
if (gp > 0) {
|
|
wasDown = true;
|
|
curX = x[0];
|
|
curY = y[0];
|
|
clampCursor();
|
|
registerClick();
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Trackball -> cursor
|
|
// ---------------------------------------------------------------------------
|
|
static void applyTrackball()
|
|
{
|
|
uint32_t e[4], clicks;
|
|
for (int i = 0; i < 4; i++) { e[i] = tbEdges[i]; tbEdges[i] = 0; }
|
|
clicks = tbClicks;
|
|
tbClicks = 0;
|
|
|
|
tbVelX += ((int32_t)e[0] - (int32_t)e[2]) * TB_IMPULSE;
|
|
tbVelY += ((int32_t)e[3] - (int32_t)e[1]) * TB_IMPULSE;
|
|
if (tbVelX > TB_VMAX) tbVelX = TB_VMAX;
|
|
if (tbVelX < -TB_VMAX) tbVelX = -TB_VMAX;
|
|
if (tbVelY > TB_VMAX) tbVelY = TB_VMAX;
|
|
if (tbVelY < -TB_VMAX) tbVelY = -TB_VMAX;
|
|
|
|
tbVelX = tbVelX * TB_FRICTION / 16;
|
|
tbVelY = tbVelY * TB_FRICTION / 16;
|
|
tbRemX += tbVelX;
|
|
tbRemY += tbVelY;
|
|
int pxX = tbRemX / TB_FP;
|
|
int pxY = tbRemY / TB_FP;
|
|
tbRemX -= pxX * TB_FP;
|
|
tbRemY -= pxY * TB_FP;
|
|
if (pxX || pxY) {
|
|
curX += pxX;
|
|
curY += pxY;
|
|
clampCursor();
|
|
}
|
|
|
|
if (clicks) registerClick();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Simulation — deterministic 20 Hz ticks
|
|
// ---------------------------------------------------------------------------
|
|
static void stepSim()
|
|
{
|
|
simTick++;
|
|
stepNetwork();
|
|
stepUnits();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Rendering
|
|
// ---------------------------------------------------------------------------
|
|
|
|
static uint16_t tileColor(char t)
|
|
{
|
|
switch (t) {
|
|
case '#': return nd::C_WALL;
|
|
case 'o': return nd::C_COVER;
|
|
case '+': return nd::C_DOOR;
|
|
case 'A': return nd::C_AIRLOCK;
|
|
case 'T': return nd::C_NODE;
|
|
default: return nd::C_FLOOR;
|
|
}
|
|
}
|
|
|
|
static uint16_t dim565(uint16_t c)
|
|
{
|
|
uint32_t r = (((c >> 11) & 0x1F) * 22) >> 6;
|
|
uint32_t g = (((c >> 5) & 0x3F) * 22) >> 6;
|
|
uint32_t b = ((c & 0x1F) * 22) >> 6;
|
|
return (uint16_t)((r << 11) | (g << 5) | b);
|
|
}
|
|
|
|
static void renderMap(bool dim)
|
|
{
|
|
for (int ty = 0; ty < nd::TILE_ROWS; ty++) {
|
|
const char *row = DEMO_MAP[ty];
|
|
for (int tx = 0; tx < nd::TILE_COLS; tx++) {
|
|
char t = row[tx];
|
|
uint16_t c = tileColor(t);
|
|
if (dim) c = dim565(c);
|
|
// the blast door: amber block when closed, open frame when open
|
|
if (t == '+' && doorOpen) c = dim ? dim565(nd::C_FLOOR) : nd::C_FLOOR;
|
|
GFXP->fillRect(tx * nd::TILE, ty * nd::TILE, nd::TILE, nd::TILE, c);
|
|
if (t == '+' && doorOpen) {
|
|
uint16_t dc = dim ? dim565(nd::C_DOOR) : nd::C_DOOR;
|
|
GFXP->fillRect(tx * nd::TILE, ty * nd::TILE, 2, nd::TILE, dc);
|
|
GFXP->fillRect(tx * nd::TILE + 6, ty * nd::TILE, 2, nd::TILE, dc);
|
|
}
|
|
if (t == 'A') {
|
|
GFXP->drawRect(tx * nd::TILE, ty * nd::TILE, nd::TILE, nd::TILE,
|
|
dim ? dim565(nd::C_FLOOR) : nd::C_FLOOR);
|
|
}
|
|
if (t == 'T') { // objective terminal: dark rack + cyan screen
|
|
uint16_t dc = dim ? dim565(nd::C_HUDBG) : nd::C_HUDBG;
|
|
GFXP->fillRect(tx * nd::TILE, ty * nd::TILE, nd::TILE, nd::TILE, dc);
|
|
GFXP->fillRect(tx * nd::TILE + 1, ty * nd::TILE + 1, 6, 3,
|
|
dim ? dim565(nd::C_PACKET) : nd::C_PACKET);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// partial line: solid fraction, dotted remainder (link build animation)
|
|
static void drawLinkLine(int x0, int y0, int x1, int y1, float frac,
|
|
uint16_t solid, uint16_t dotted, bool dottedFirst)
|
|
{
|
|
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
|
|
int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
|
|
int err = dx + dy;
|
|
int steps = dx - dy; // bresenham step count approx
|
|
int solidSteps = (int)(steps * frac);
|
|
int i = 0;
|
|
for (;;) {
|
|
bool solidPix = dottedFirst ? (i >= steps - solidSteps)
|
|
: (i < solidSteps);
|
|
if (solidPix) GFXP->drawPixel(x0, y0, solid);
|
|
else if ((i & 1) == 0) GFXP->drawPixel(x0, y0, dotted);
|
|
if (x0 == x1 && y0 == y1) break;
|
|
int e2 = 2 * err;
|
|
if (e2 >= dy) { err += dy; x0 += sx; }
|
|
if (e2 <= dx) { err += dx; y0 += sy; }
|
|
i++;
|
|
}
|
|
}
|
|
|
|
static void renderNetwork(bool full)
|
|
{
|
|
// links: active solid, building partial, ghost outlines in cameras mode
|
|
for (int i = 0; i < 3; i++) {
|
|
auto &l = links[i];
|
|
int x0 = nodePx(l.a), y0 = nodePy(l.a);
|
|
int x1 = nodePx(l.b), y1 = nodePy(l.b);
|
|
bool hovered = (hoverLink() == i && full);
|
|
uint16_t solidC = hovered ? nd::C_CURSOR : nd::C_LINK;
|
|
if (l.active && full) {
|
|
if (l.prog < l.dur) {
|
|
drawLinkLine(x0, y0, x1, y1, (float)l.prog / l.dur,
|
|
solidC, nd::C_PACKET, false);
|
|
} else {
|
|
GFXP->drawLine(x0, y0, x1, y1, solidC);
|
|
}
|
|
} else if (l.active && !full) {
|
|
if (l.prog < l.dur) {
|
|
drawLinkLine(x0, y0, x1, y1, (float)l.prog / l.dur,
|
|
nd::C_GHOST, nd::C_GHOST, false);
|
|
} else {
|
|
for (int k = 0; k <= 1; k++) {} // dotted ghost:
|
|
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
|
|
int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
|
|
int err = dx + dy, step = 0;
|
|
for (;;) {
|
|
if ((step++ & 1) == 0) GFXP->drawPixel(x0, y0, nd::C_GHOST);
|
|
if (x0 == x1 && y0 == y1) break;
|
|
int e2 = 2 * err;
|
|
if (e2 >= dy) { err += dy; x0 += sx; }
|
|
if (e2 <= dx) { err += dx; y0 += sy; }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// red trace pulse along HIVE -> JUNC -> AIRLK
|
|
if (traceActive) {
|
|
float f = (float)traceProg / TRACE_TICKS * 2.0f; // 2 segments
|
|
int seg = (f >= 1.0f) ? 1 : 0;
|
|
float t = f - seg;
|
|
int x0 = nodePx(N_HIVE), y0 = nodePy(N_HIVE);
|
|
int x1 = nodePx(N_JUNC), y1 = nodePy(N_JUNC);
|
|
if (seg == 1) { x0 = nodePx(N_JUNC); y0 = nodePy(N_JUNC); x1 = nodePx(N_AIRLK); y1 = nodePy(N_AIRLK); }
|
|
int px = x0 + (int)((x1 - x0) * t);
|
|
int py = y0 + (int)((y1 - y0) * t);
|
|
GFXP->fillCircle(px, py, 3, nd::C_TRACE);
|
|
GFXP->drawCircle(px, py, 5, nd::C_TRACE);
|
|
}
|
|
|
|
// nodes
|
|
for (int i = 0; i < 4; i++) {
|
|
int nx = nodePx(i), ny = nodePy(i);
|
|
int hoverDx = curX - nx, hoverDy = curY - ny;
|
|
bool hovered = viewMode == VM_NETWORK && hoverDx * hoverDx + hoverDy * hoverDy < 24 * 24;
|
|
if (full) {
|
|
if (nodeOnline[i]) {
|
|
GFXP->fillCircle(nx, ny, 6, NODES[i].color);
|
|
GFXP->drawCircle(nx, ny, 6, TFT_BLACK);
|
|
} else {
|
|
GFXP->drawCircle(nx, ny, 6, hovered ? nd::C_CURSOR : nd::C_GHOST);
|
|
}
|
|
GFXP->setTextDatum(TC_DATUM);
|
|
GFXP->setTextColor(nodeOnline[i] ? nd::C_TEXT : nd::C_TEXTDIM);
|
|
GFXP->drawString(NODES[i].label, nx, ny + 9);
|
|
// door state on the door node
|
|
if (i == N_DOOR && nodeOnline[i]) {
|
|
GFXP->setTextColor(doorOpen ? nd::C_GOOD : nd::C_TRACE);
|
|
GFXP->drawString(doorOpen ? "OPEN" : "SHUT", nx, ny + 19);
|
|
}
|
|
// ICE crack progress on the hive
|
|
if (i == N_HIVE) {
|
|
for (int k = 0; k < 3; k++) {
|
|
if (links[k].active && links[k].b == N_HIVE && links[k].prog < links[k].dur) {
|
|
GFXP->setTextColor(nd::C_TRACE);
|
|
GFXP->drawString("CRACKING", nx, ny + 19);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
GFXP->drawCircle(nx, ny, 6,
|
|
nodeOnline[i] ? nd::C_GHOST
|
|
: (hovered ? nd::C_CURSOR : nd::C_GHOST));
|
|
}
|
|
}
|
|
|
|
if (full) {
|
|
GFXP->setTextDatum(TL_DATUM);
|
|
GFXP->setTextColor(nd::C_TEXTDIM);
|
|
GFXP->drawString("NETWORK", 4, 4);
|
|
}
|
|
}
|
|
|
|
static void renderBeacon()
|
|
{
|
|
int bx = beaconTX * nd::TILE + nd::TILE / 2;
|
|
int by = beaconTY * nd::TILE + nd::TILE / 2;
|
|
uint16_t c = (viewMode == VM_NETWORK) ? dim565(nd::C_BEACON) : nd::C_BEACON;
|
|
GFXP->fillTriangle(bx, by - 3, bx + 3, by, bx, by + 3, c);
|
|
GFXP->fillTriangle(bx, by - 3, bx - 3, by, bx, by + 3, c);
|
|
}
|
|
|
|
// entity sprites + overhead HP bars (dimmed ghosting in Network view)
|
|
static void renderUnits(bool full)
|
|
{
|
|
for (int i = 0; i < 7; i++) {
|
|
const Unit &u = units[i];
|
|
if (!u.alive) continue;
|
|
int px = unitPx(i), py = unitPy(i);
|
|
uint16_t body, line;
|
|
if (u.kind == U_MARINE) {
|
|
body = nd::C_GOOD;
|
|
line = nd::C_HUDBG;
|
|
} else if (u.kind == U_TURRET) {
|
|
body = nd::C_HOT;
|
|
line = nd::C_HUDBG;
|
|
} else {
|
|
body = nd::C_TRACE;
|
|
line = nd::C_HUDBG;
|
|
}
|
|
if (!full) { body = dim565(body); line = dim565(line); }
|
|
|
|
if (u.kind == U_TURRET) { // ceiling mount: circle + barrel
|
|
GFXP->fillCircle(px, py - 1, 3, body);
|
|
GFXP->drawCircle(px, py - 1, 3, line);
|
|
GFXP->drawFastVLine(px, py + 2, 3, line);
|
|
} else {
|
|
int s = (u.kind == U_HEAVY) ? 7 : 6; // heavy is one size up
|
|
GFXP->fillRect(px - s / 2, py - s / 2, s, s, body);
|
|
GFXP->drawRect(px - s / 2, py - s / 2, s, s, line);
|
|
GFXP->drawFastHLine(px - 1, py - s / 2 - 2, 2, line); // antenna
|
|
}
|
|
|
|
// overhead HP bar
|
|
int pct = u.hp * 100 / STATS[u.kind].maxHp;
|
|
uint16_t hc = pct > 50 ? nd::C_GOOD : (pct > 25 ? nd::C_WARN : nd::C_HOT);
|
|
if (!full) hc = dim565(hc);
|
|
GFXP->fillRect(px - 4, py - 7, 8, 2, nd::C_HUDBG);
|
|
GFXP->fillRect(px - 4, py - 7, 8 * pct / 100, 2, hc);
|
|
}
|
|
}
|
|
|
|
static void renderTracers(bool full)
|
|
{
|
|
for (const auto &t : tracers) {
|
|
if (t.ttl <= 0) continue;
|
|
uint16_t c = full ? t.color : dim565(t.color);
|
|
if (t.impact) GFXP->fillCircle(t.x1, t.y1, 2, c);
|
|
else GFXP->drawLine(t.x0, t.y0, t.x1, t.y1, c);
|
|
}
|
|
}
|
|
|
|
// 1-line banner on win/loss (MVP: BREACH SUCCESS / SQUAD KIA, R to replay)
|
|
static void renderBanner()
|
|
{
|
|
if (scenario == SC_LIVE || (millis() / 400) & 1) return;
|
|
uint16_t c = scenario == SC_WIN ? nd::C_GOOD : nd::C_HOT;
|
|
GFXP->fillRect(40, 78, 240, 34, nd::C_HUDBG);
|
|
GFXP->drawRect(40, 78, 240, 34, c);
|
|
GFXP->setTextDatum(MC_DATUM);
|
|
GFXP->setTextColor(c);
|
|
GFXP->drawString(scenario == SC_WIN ? "BREACH SUCCESS" : "SQUAD KIA",
|
|
160, 95);
|
|
GFXP->setTextFont(1);
|
|
GFXP->setTextColor(nd::C_TEXTDIM);
|
|
GFXP->drawString("R: RESET", 160, 108);
|
|
GFXP->setTextDatum(TL_DATUM);
|
|
}
|
|
|
|
static void meterBar(int x, int y, int w, int h, int pct, uint16_t c)
|
|
{
|
|
GFXP->drawRect(x, y, w, h, nd::C_BORDER);
|
|
int fw = (w - 2) * pct / 100;
|
|
if (fw > 0) GFXP->fillRect(x + 1, y + 1, fw, h - 2, c);
|
|
}
|
|
|
|
static void renderHud()
|
|
{
|
|
GFXP->fillRect(nd::HUD_X, nd::HUD_Y, nd::HUD_W, nd::HUD_H, nd::C_HUDBG);
|
|
GFXP->drawFastHLine(nd::HUD_X, nd::HUD_Y, nd::HUD_W, nd::C_BORDER);
|
|
|
|
// 2 exploit daemon slots (MVP: FRENZY + BLIND)
|
|
static const char *const NAMES[2] = { "FRENZY", "BLIND" };
|
|
for (int i = 0; i < 2; i++) {
|
|
int x = 6 + i * 64, y = nd::HUD_Y + 6;
|
|
GFXP->setTextDatum(TL_DATUM);
|
|
GFXP->setTextColor(nd::C_TEXT);
|
|
GFXP->drawString(String("[") + char('1' + i) + "]", x, y);
|
|
GFXP->setTextColor(nd::C_TEXTDIM);
|
|
GFXP->drawString(NAMES[i], x + 20, y);
|
|
meterBar(x, y + 14, 48, 6, 100, nodeOnline[N_HIVE] ? nd::C_GOOD : nd::C_BORDER);
|
|
}
|
|
|
|
// TEMP bar (overheat lockout)
|
|
int heatPct = heatT * 100 / HEAT_TICKS;
|
|
GFXP->setTextColor(heatT > 0 ? nd::C_HOT : nd::C_TEXT);
|
|
GFXP->drawString(heatT > 0 ? "OVERHEAT" : "TEMP", 130, nd::HUD_Y + 6);
|
|
meterBar(130, nd::HUD_Y + 16, 56, 7, heatT > 0 ? heatPct : 15,
|
|
heatT > 0 ? nd::C_HOT : nd::C_WARN);
|
|
|
|
// pause hint / state (right column: SPACE / RESUME / R: RESET / VIEW)
|
|
GFXP->setTextDatum(TR_DATUM);
|
|
GFXP->setTextColor(paused ? nd::C_FREEZE : nd::C_TEXT);
|
|
GFXP->drawString("SPACE", nd::HUD_X + nd::HUD_W - 6, nd::HUD_Y + 4);
|
|
GFXP->setTextColor(paused ? nd::C_FREEZE : nd::C_TEXTDIM);
|
|
GFXP->drawString(paused ? "RESUME" : "PAUSE", nd::HUD_X + nd::HUD_W - 6, nd::HUD_Y + 14);
|
|
GFXP->setTextColor(nd::C_TEXTDIM);
|
|
GFXP->drawString("R: RESET", nd::HUD_X + nd::HUD_W - 6, nd::HUD_Y + 24);
|
|
GFXP->setTextDatum(TL_DATUM);
|
|
|
|
// view mode indicator (bottom-right)
|
|
GFXP->setTextDatum(TR_DATUM);
|
|
GFXP->setTextColor(nd::C_FREEZE);
|
|
GFXP->drawString(viewMode == VM_CAMERAS ? "VIEW: CAMERAS [P]" : "VIEW: NETWORK [P]",
|
|
nd::HUD_X + nd::HUD_W - 6, nd::HUD_Y + 34);
|
|
GFXP->setTextDatum(TL_DATUM);
|
|
|
|
// squad readout: live marine HP
|
|
GFXP->setTextColor(nd::C_TEXTDIM);
|
|
GFXP->drawString("SQUAD", 6, nd::HUD_Y + 30);
|
|
for (int i = 0; i < 2; i++) {
|
|
const Unit &u = units[i];
|
|
int x = 62 + i * 46;
|
|
GFXP->setTextColor(u.alive ? nd::C_TEXTDIM : nd::C_HOT);
|
|
GFXP->drawString(String("M") + String(i + 1), x - 14, nd::HUD_Y + 30);
|
|
meterBar(x, nd::HUD_Y + 34, 30, 7,
|
|
u.alive ? u.hp * 100 / STATS[u.kind].maxHp : 0,
|
|
u.alive ? nd::C_GOOD : nd::C_BORDER);
|
|
}
|
|
|
|
// transient message
|
|
if ((int32_t)(millis() - msgUntil) < 0) {
|
|
GFXP->setTextColor(nd::C_CURSOR);
|
|
GFXP->drawString(msgText, 200, nd::HUD_Y + 6);
|
|
}
|
|
}
|
|
|
|
static void renderCursor()
|
|
{
|
|
uint16_t c = (viewMode == VM_NETWORK) ? nd::C_PACKET : nd::C_CURSOR;
|
|
if (viewMode == VM_CAMERAS) {
|
|
int tx = curX / nd::TILE, ty = curY / nd::TILE;
|
|
GFXP->drawRect(tx * nd::TILE, ty * nd::TILE, nd::TILE, nd::TILE, c);
|
|
GFXP->drawCircle(curX, curY, 3, c);
|
|
GFXP->drawPixel(curX, curY, c);
|
|
} else {
|
|
GFXP->drawCircle(curX, curY, 4, c);
|
|
GFXP->drawCircle(curX, curY, 1, c);
|
|
}
|
|
}
|
|
|
|
static void renderFreezeTint()
|
|
{
|
|
for (int y = 0; y < nd::VIEW_H; y += 3) {
|
|
GFXP->drawFastHLine(0, y, nd::VIEW_W, 0x0000);
|
|
}
|
|
GFXP->setTextDatum(TR_DATUM);
|
|
GFXP->setTextColor(nd::C_FREEZE, 0x0000);
|
|
if ((millis() / 500) & 1) {
|
|
GFXP->drawString("* TACTICAL FREEZE *", nd::VIEW_W - 4, 4);
|
|
}
|
|
GFXP->setTextDatum(TL_DATUM);
|
|
}
|
|
|
|
static void render()
|
|
{
|
|
renderMap(viewMode == VM_NETWORK);
|
|
renderNetwork(viewMode == VM_NETWORK);
|
|
renderUnits(viewMode == VM_CAMERAS);
|
|
renderTracers(viewMode == VM_CAMERAS);
|
|
renderBeacon();
|
|
renderHud();
|
|
|
|
uint16_t bc = (int32_t)(millis() - viewFlashUntil) < 0 ? nd::C_CURSOR : nd::C_BORDER;
|
|
GFXP->drawFastHLine(nd::HUD_X, nd::HUD_Y - 1, nd::HUD_W, bc);
|
|
if ((int32_t)(millis() - viewFlashUntil) < 0) {
|
|
GFXP->drawRect(0, 0, nd::VIEW_W, nd::VIEW_H, nd::C_CURSOR);
|
|
}
|
|
|
|
renderCursor();
|
|
if (paused) renderFreezeTint();
|
|
renderBanner();
|
|
}
|
|
|
|
static void drawBoot()
|
|
{
|
|
GFXP->fillScreen(TFT_BLACK);
|
|
GFXP->setFreeFont(&FreeSansBold18pt7b);
|
|
GFXP->setTextDatum(MC_DATUM);
|
|
GFXP->setTextColor(nd::C_FREEZE);
|
|
GFXP->drawString("NETDECK", 160, 74);
|
|
GFXP->setTextColor(nd::C_CURSOR);
|
|
GFXP->drawString("BREACH & BOARD", 160, 108);
|
|
GFXP->setTextFont(2);
|
|
GFXP->setTextColor(nd::C_TEXTDIM);
|
|
GFXP->drawString("M3 // VERTICAL SLICE", 160, 136);
|
|
GFXP->drawString("p: cameras/network space: freeze", 160, 158);
|
|
GFXP->drawString("ball: cursor enter: select x: sever", 160, 174);
|
|
GFXP->setTextDatum(TL_DATUM);
|
|
GFXP->setTextFont(1);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Setup / loop
|
|
// ---------------------------------------------------------------------------
|
|
static void hardwareInit()
|
|
{
|
|
Serial.begin(115200);
|
|
Serial.println("NetDeck M3 — vertical slice (squad vs. droids)");
|
|
|
|
pinMode(BOARD_POWERON, OUTPUT);
|
|
digitalWrite(BOARD_POWERON, HIGH);
|
|
|
|
pinMode(BOARD_SDCARD_CS, OUTPUT);
|
|
pinMode(RADIO_CS_PIN, OUTPUT);
|
|
pinMode(BOARD_TFT_CS, OUTPUT);
|
|
digitalWrite(BOARD_SDCARD_CS, HIGH);
|
|
digitalWrite(RADIO_CS_PIN, HIGH);
|
|
digitalWrite(BOARD_TFT_CS, HIGH);
|
|
|
|
pinMode(BOARD_SPI_MISO, INPUT_PULLUP);
|
|
SPI.begin(BOARD_SPI_SCK, BOARD_SPI_MISO, BOARD_SPI_MOSI);
|
|
|
|
tft.begin();
|
|
tft.setRotation(1);
|
|
tft.fillScreen(TFT_BLACK);
|
|
|
|
pinMode(BOARD_BL_PIN, OUTPUT);
|
|
digitalWrite(BOARD_BL_PIN, HIGH);
|
|
|
|
Wire.begin(BOARD_I2C_SDA, BOARD_I2C_SCL);
|
|
|
|
touch.setPins(-1, BOARD_TOUCH_INT);
|
|
touchAvail = touch.begin(Wire);
|
|
if (touchAvail) {
|
|
touch.setMaxCoordinates(tft.width(), tft.height());
|
|
touch.setSwapXY(true);
|
|
touch.setMirrorXY(false, true);
|
|
}
|
|
|
|
attachTrackball();
|
|
randomSeed(esp_random());
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
hardwareInit();
|
|
|
|
#ifndef EMULATOR
|
|
fb.setColorDepth(16);
|
|
if (!fb.createSprite(tft.width(), tft.height()))
|
|
Serial.println("NetDeck: framebuffer alloc failed (blank screen)");
|
|
#endif
|
|
|
|
drawBoot();
|
|
resetScenario();
|
|
stateT0 = millis();
|
|
lastFrameMs = stateT0;
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
uint32_t now = millis();
|
|
|
|
if (gameState == ST_BOOT) {
|
|
if (now - stateT0 >= nd::BOOT_MS) {
|
|
gameState = ST_RUN;
|
|
stateT0 = now;
|
|
for (int i = 0; i < 8; i++) {
|
|
if (Wire.requestFrom((uint8_t)0x55, (uint8_t)16) == 0) break;
|
|
}
|
|
resetTrackball();
|
|
Serial.println("[boot] encounter loaded (paused, cameras view)");
|
|
}
|
|
delay(1);
|
|
return;
|
|
}
|
|
|
|
static uint32_t nextFrame = 0;
|
|
if ((int32_t)(now - nextFrame) < 0) { delay(1); return; }
|
|
nextFrame = now + nd::FRAME_MS;
|
|
|
|
uint32_t dt = now - lastFrameMs;
|
|
lastFrameMs = now;
|
|
if (dt > 200) dt = 200;
|
|
|
|
applyTrackball();
|
|
pollTouch();
|
|
pollKeyboard();
|
|
|
|
if (!paused) {
|
|
simAcc += dt;
|
|
while (simAcc >= nd::SIM_TICK_MS) {
|
|
stepSim();
|
|
simAcc -= nd::SIM_TICK_MS;
|
|
}
|
|
}
|
|
|
|
render();
|
|
present();
|
|
}
|