/** * @file NetDeck.ino * @brief NetDeck — Breach & Board: Milestone 2 — network graph & traces. * * Milestone 2 (DESIGN.md §7) implementing the sub-net half of the MVP * vertical slice (MVP.md §2/§3) over the shared coordinate space: * * - 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 * * 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 #include #include #include #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; 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) — used from M3 on [[maybe_unused]] 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 — used from M3 on [[maybe_unused]] static bool blocksSight(int tx, int ty) { char t = tileAt(tx, ty); return t == '#' || (t == '+' && !doorOpen); } [[maybe_unused]] 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; // 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; Serial.println("[net] FEEDBACK SPIKE: links dropped, OVERHEAT 3s, reinforcement inbound"); flashMsg("FEEDBACK!"); // reinforcement droid spawns in M3 } 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"); } } // --------------------------------------------------------------------------- // 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; refreshOnline(); // AIRLK root comes up online paused = true; // entities (marines/droids) arrive in M3 } // --------------------------------------------------------------------------- // 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; char t = tileAt(tx, ty); if (t != '#') { beaconTX = tx; beaconTY = ty; Serial.printf("[click] beacon -> tile %d,%d\n", tx, ty); } else { Serial.println("[click] wall 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(); } // --------------------------------------------------------------------------- // 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); } 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 * 58, 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 + 18, 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 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 + 6); GFXP->setTextColor(paused ? nd::C_FREEZE : nd::C_TEXTDIM); GFXP->drawString(paused ? "RESUME" : "PAUSE", nd::HUD_X + nd::HUD_W - 6, nd::HUD_Y + 16); GFXP->setTextColor(nd::C_TEXTDIM); GFXP->drawString("R: RESET", nd::HUD_X + nd::HUD_W - 6, nd::HUD_Y + 32); GFXP->setTextDatum(TL_DATUM); // view mode indicator GFXP->setTextDatum(TR_DATUM); GFXP->setTextColor(nd::C_FREEZE); GFXP->drawString(viewMode == VM_CAMERAS ? "VIEW: CAMERAS [P]" : "VIEW: NETWORK [P]", nd::HUD_X + 190, nd::HUD_Y + 32); GFXP->setTextDatum(TL_DATUM); // squad readout (live marine HP in M3; placeholder now) GFXP->setTextColor(nd::C_TEXTDIM); GFXP->drawString("SQUAD", 6, nd::HUD_Y + 30); for (int i = 0; i < 2; i++) { int x = 48 + i * 58; for (int s = 0; s < 5; s++) { GFXP->fillRect(x + s * 8, nd::HUD_Y + 34, 6, 8, nd::C_GOOD); } GFXP->setTextColor(nd::C_TEXTDIM); GFXP->drawString(String("M") + String(i + 1), x - 10, nd::HUD_Y + 30); } // 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); 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(); } 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("M2 // 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 M2 — vertical slice (network & traces)"); 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(); }