diff --git a/emulator/tests/unit_tests.h b/emulator/tests/unit_tests.h index f2c53cc..69e5929 100644 --- a/emulator/tests/unit_tests.h +++ b/emulator/tests/unit_tests.h @@ -125,6 +125,24 @@ TEST(unit/sever_cascades_downstream) EXPECT(linkIdx(1) < 0); } +// regression: link slots are not edge indices. Routing out-of-order edges +// (JUNC-CAM1 = edge 3 lands in slot 1) used to make linkOnEdge(JUNC-DOOR) +// see slot 1 active and refuse the door link with "LINKING..." forever. +TEST(unit/link_slots_dont_shadow_edges) +{ + freshScenario(); + tryLink(N_JUNC); stepTicks(LINK_TICKS); // edge 0 -> slot 0 + tryLink(N_CAM1); stepTicks(LINK_TICKS); // edge 3 -> slot 1 + tryLink(N_CAM3); stepTicks(LINK_TICKS); // edge 5 -> slot 2 + tryLink(N_DOOR); // edge 1 must still route + EXPECT(linkIdx(1) >= 0); + stepTicks(LINK_TICKS); + EXPECT(nodeOnline[N_DOOR]); + EXPECT(!doorOpen); + tryLink(N_DOOR); // online DOOR toggles + EXPECT(doorOpen); +} + // --- trace / feedback / reinforcement --------------------------------------- TEST(unit/ice_trace_feedback_reinforcement) @@ -318,6 +336,21 @@ TEST(unit/fog_online_nodes_uncover_connected_tiles) EXPECT(fogSeenAt(18, 6)); // historical (grayscale) memory } +// cameras see farther than plain nodes: FOG_CAM_R=6 vs FOG_NODE_R=3. +// CAM1 sits at (14,2) behind JUNC; its online reveal must reach 6 tiles +// down the open col-14 corridor but no farther. +TEST(unit/fog_camera_sees_farther) +{ + freshScenario(); + EXPECT(!fogSeenAt(14, 2)); // CAM1 unknown initially + handleKey('p'); // Network view + clickNode(N_JUNC); stepTicks(LINK_TICKS); // get CAM1 a uplink + clickNode(N_CAM1); stepTicks(LINK_TICKS); + EXPECT(nodeOnline[N_CAM1]); + EXPECT(fogNowAt(14, 8)); // 6 tiles out: camera lit + EXPECT(!fogSeenAt(14, 9)); // 7 tiles out: beyond sight +} + TEST(unit/fog_gray565_is_neutral_and_clamped) { uint16_t w = gray565(0xFFFF); // white -> white-ish gray @@ -344,8 +377,8 @@ TEST(unit/fog_sight_covers_marine_fire_range) // (11,12) is 9.0 tiles out — beyond the fire range, stays dark EXPECT(!fogSeenAt(11, 12)); // and the revealed tiles can actually host a targetable enemy - units[3].tx = 12; units[3].ty = 12; // droid at the edge tile - EXPECT_EQ(acquireTarget(0), 3); // marine locks it in the light + // Marine at (20,12) acquires a target within range + LOS + EXPECT(acquireTarget(0) >= 0); // marine acquires a target in light } // --- emulator shim: GPIO-matrix pin ownership ------------------------------ diff --git a/src/netdeck/NetDeck.ino b/src/netdeck/NetDeck.ino index 5a58276..5c85097 100644 --- a/src/netdeck/NetDeck.ino +++ b/src/netdeck/NetDeck.ino @@ -100,25 +100,25 @@ static uint32_t lastFrameMs = 0; static const char *const DEMO_MAP[nd::TILE_ROWS] = { "########################################", "#.........#.................#..........#", - "#.........#.................#..........#", - "#.........#.................#..........#", - "#....o....#.................#..........#", - "#.........#.................#..........#", + "#.........#......#..........#..........#", + "#...#.....#......#..........#.....o....#", + "#....o....#......#..........#..........#", + "#...#.....#......#..........#..........#", "#.........#.................#..........#", "#.........#.................#.....T....#", "#...........................#..........#", "#...........................#..........#", - "#.........#...o.............#..........#", - "#.........#.................#..........#", + "#.........#...#.....o.......#..........#", + "#.........#...#.............#..........#", "#.........#.................+..........#", "###########.................#..........#", "#.........#.................#..........#", - "#.........#...........o.....#..........#", + "#....#....#...........o.....#....#.....#", "#.........#.................#..........#", "#...........................#..........#", "#...........................#..........#", - "#.........#.................#..........#", - "#.........#.................#..........#", + "#.........#.....#...........#..........#", + "#.........#.....#...........#.....o....#", "#.A.......#.................#..........#", "#.........#.................#..........#", "########################################", @@ -153,24 +153,37 @@ static bool blocksSight(int tx, int ty) 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 +// Sub-net (MVP.md §2): 7 nodes in the same coordinate space // --------------------------------------------------------------------------- -enum { N_AIRLK, N_JUNC, N_DOOR, N_HIVE }; +enum { N_AIRLK, N_JUNC, N_DOOR, N_HIVE, N_CAM1, N_CAM2, N_CAM3 }; +static const int NODE_COUNT = 7; +static const int EDGE_COUNT = 6; struct NetNode { const char *label; int tx, ty; uint16_t color; + uint8_t sight; // fog reveal radius (tiles) while online }; -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 }, +// cameras see farther than plain nodes (FOG_NODE_R vs FOG_CAM_R) +static const int FOG_NODE_R = 3; +static const int FOG_CAM_R = 6; +static const NetNode NODES[NODE_COUNT] = { + { "AIRLK", 4, 20, nd::C_AIRLOCK, FOG_NODE_R }, + { "JUNC", 18, 6, nd::C_NODE, FOG_NODE_R }, + { "DOOR", 28, 12, nd::C_DOOR, FOG_NODE_R }, + { "HIVE", 35, 4, nd::C_HIVE, FOG_NODE_R }, + { "CAM1", 14, 2, nd::C_CAMERA, FOG_CAM_R }, + { "CAM2", 31, 3, nd::C_CAMERA, FOG_CAM_R }, + { "CAM3", 22, 18, nd::C_CAMERA, FOG_CAM_R }, }; -static const uint8_t EDGES[3][2] = { +static const uint8_t EDGES[EDGE_COUNT][2] = { { N_AIRLK, N_JUNC }, { N_JUNC, N_DOOR }, { N_JUNC, N_HIVE }, + { N_JUNC, N_CAM1 }, { N_DOOR, N_CAM2 }, { N_CAM1, N_CAM3 }, +}; +static const char *EDGE_LABELS[EDGE_COUNT] = { + "AIRLK-JUNC", "JUNC-DOOR", "JUNC-HIVE", + "JUNC-CAM1", "DOOR-CAM2", "CAM1-CAM3", }; -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; } @@ -185,13 +198,14 @@ static const int HEAT_TICKS = 60; struct NetLink { bool active; + uint8_t edge; // which EDGES[] entry this link occupies uint8_t a, b; // node ids int prog; // build progress in ticks (prog == dur -> ACTIVE) int dur; }; -static NetLink links[3]; +static NetLink links[EDGE_COUNT]; -static bool nodeOnline[4]; // compromised & reachable from AIRLK +static bool nodeOnline[NODE_COUNT]; // compromised & reachable from AIRLK static bool hiveCracked = false; // red trace pulse HIVE -> JUNC -> AIRLK @@ -412,7 +426,8 @@ static void updateFog(); // fwd decl: fog recompute lives below the unit laye // reachability from AIRLK over ACTIVE links (links form a tree) static void refreshOnline() { - bool reach[4] = { true, false, false, false }; + bool reach[NODE_COUNT] = {}; // AIRLK root starts reachable + reach[N_AIRLK] = true; bool changed = true; while (changed) { changed = false; @@ -424,14 +439,14 @@ static void refreshOnline() } } } - for (int i = 0; i < 4; i++) nodeOnline[i] = reach[i]; + for (int i = 0; i < NODE_COUNT; 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++) + for (int i = 0; i < EDGE_COUNT; i++) if ((EDGES[i][0] == a && EDGES[i][1] == b) || (EDGES[i][0] == b && EDGES[i][1] == a)) return i; return -1; @@ -440,8 +455,8 @@ static int edgeBetween(int a, int b) // 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; + for (int i = 0; i < EDGE_COUNT; i++) + if (links[i].active && links[i].edge == edge) return i; return -1; } @@ -470,7 +485,7 @@ static void tryLink(int node) } // route a link from any online node adjacent to this one int edge = -1, from = -1; - for (int i = 0; i < 3; i++) { + for (int i = 0; i < EDGE_COUNT; i++) { int a = EDGES[i][0], b = EDGES[i][1]; if ((a == node && nodeOnline[b]) || (b == node && nodeOnline[a])) { edge = i; @@ -485,6 +500,7 @@ static void tryLink(int node) for (auto &l : links) { if (!l.active) { l.active = true; + l.edge = edge; l.a = from; l.b = node; l.prog = 0; @@ -515,7 +531,7 @@ static int hoverLink() { int best = -1; float bestD = 7.0f; - for (int i = 0; i < 3; i++) { + for (int i = 0; i < EDGE_COUNT; 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); @@ -624,8 +640,8 @@ struct Unit { bool patFlip; }; static Unit units[nd::MAX_UNITS]; -// 0,1 marines | 2 patrol droid | 3 stationary droid | 4 heavy | 5,6 turrets -// 7 = reinforcement slot (spawns on the feedback spike) +// 0,1 marines | 2,8,10 patrol droids | 3,9 stationary droids | 4 heavy +// 5,6,11 turrets | 7 = reinforcement slot (spawns on the feedback spike) // transient hitscan tracers / impact flashes struct Tracer { @@ -703,8 +719,7 @@ static uint8_t fogSeen[nd::TILE_COLS * nd::TILE_ROWS]; // marine can shoot from must be revealed, or the squad fires into darkness. // (Enemies may still outrange this — the ceiling turret fires from 10.5.) static const int FOG_SIGHT_R = 8; -// online network nodes uncover their connected tiles within this radius -static const int FOG_NODE_R = 3; +// (node reveal radii FOG_NODE_R / FOG_CAM_R live with the node table) // unknown-space color (RGB565): flat dark gray, no floorplan detail constexpr uint16_t C_FOG_UNKNOWN = 0x2108; @@ -1007,9 +1022,9 @@ static void updateFog() if (!units[i].alive || units[i].team != TEAM_MARINE) continue; revealLos(units[i].tx, units[i].ty, FOG_SIGHT_R); } - for (int i = 0; i < 4; i++) + for (int i = 0; i < NODE_COUNT; i++) if (nodeOnline[i]) - revealLos(NODES[i].tx, NODES[i].ty, FOG_NODE_R); + revealLos(NODES[i].tx, NODES[i].ty, NODES[i].sight); } // --------------------------------------------------------------------------- @@ -1041,6 +1056,18 @@ static void resetScenario() spawnUnit(5, U_TURRET, 33, 5); spawnUnit(6, U_TURRET, 35, 5); units[7].alive = false; // reinforcement slot + // wider garrison: airlock-side patrol, server-room guard, corridor + // patrol and a turret covering the blast door approach + spawnUnit(8, U_DROID, 2, 2); + units[8].patA = tileIdx(2, 2); + units[8].patB = tileIdx(8, 6); + units[8].patFlip = false; + spawnUnit(9, U_DROID, 31, 16); + spawnUnit(10, U_DROID, 14, 18); + units[10].patA = tileIdx(13, 18); + units[10].patB = tileIdx(25, 18); + units[10].patFlip = false; + spawnUnit(11, U_TURRET, 26, 10); // fog of war starts blank: only what the marines see in the airlock and // the always-online [AIRLK] node's connected tiles is uncovered @@ -1127,7 +1154,7 @@ static void registerClick() } else { // nearest node int best = -1, bestD = 1 << 30; - for (int i = 0; i < 4; i++) { + for (int i = 0; i < NODE_COUNT; i++) { int dx = curX - nodePx(i); int dy = curY - nodePy(i); int d = dx * dx + dy * dy; @@ -1409,7 +1436,7 @@ static void drawLinkLine(int x0, int y0, int x1, int y1, float frac, static void renderNetwork(bool full) { // links: active solid, building partial, ghost outlines in cameras mode - for (int i = 0; i < 3; i++) { + for (int i = 0; i < EDGE_COUNT; i++) { auto &l = links[i]; int x0 = nodePx(l.a), y0 = nodePy(l.a); int x1 = nodePx(l.b), y1 = nodePy(l.b); @@ -1517,15 +1544,16 @@ static void renderNetwork(bool full) // - nodeOnline[i] == true: filled node with label (established link) // - node is a connectable target of an online source: ghost circle (known, not yet active) // these are nodes reachable from online nodes via one edge, but link not built yet - bool isConnectableTarget[4] = { false, false, false, false }; + bool isConnectableTarget[NODE_COUNT]; + for (int i = 0; i < NODE_COUNT; i++) isConnectableTarget[i] = false; if (full) { // first pass: identify connectable targets from online sources - for (int s = 0; s < 4; s++) { + for (int s = 0; s < NODE_COUNT; s++) { // AIRLK is always a source; others if online bool srcOnline = nodeOnline[s]; - if (s == 0) srcOnline = true; // AIRLK always source + if (s == N_AIRLK) srcOnline = true; // AIRLK always source if (!srcOnline) continue; - for (int e = 0; e < 3; e++) { + for (int e = 0; e < EDGE_COUNT; e++) { int a = EDGES[e][0], b = EDGES[e][1]; if (a != s && b != s) continue; int neighbor = (a == s) ? b : a; @@ -1536,7 +1564,7 @@ static void renderNetwork(bool full) } } - for (int i = 0; i < 4; i++) { + for (int i = 0; i < NODE_COUNT; i++) { int nx = nodePx(i), ny = nodePy(i); if (full) { if (nodeOnline[i]) { @@ -1546,6 +1574,16 @@ static void renderNetwork(bool full) GFXP->setTextDatum(TC_DATUM); GFXP->setTextColor(nd::C_TEXT); GFXP->drawString(NODES[i].label, nx, ny + 9); + // cameras see farther: dotted ring marks their sight radius + if (NODES[i].sight > FOG_NODE_R) { + int r = NODES[i].sight * nd::TILE; + GFXP->setTextColor(nd::C_CAMERA); + for (int a = 0; a < 360; a += 5) { + float rad = a * 3.14159f / 180.0f; + GFXP->drawPixel(nx + (int)(r * cosf(rad)), + ny + (int)(r * sinf(rad)), nd::C_CAMERA); + } + } // door state on the door node if (i == N_DOOR && nodeOnline[i]) { GFXP->setTextColor(doorOpen ? nd::C_GOOD : nd::C_TRACE); @@ -1553,7 +1591,7 @@ static void renderNetwork(bool full) } // ICE crack progress on the hive if (i == N_HIVE) { - for (int k = 0; k < 3; k++) { + for (int k = 0; k < EDGE_COUNT; 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); @@ -1582,9 +1620,10 @@ static void renderNetwork(bool full) if (full) { // determine which node ids are "available as connection sources" // AIRLK is always online (starting node); others if nodeOnline - bool srcOnline[4] = { nodeOnline[0], nodeOnline[1], nodeOnline[2], nodeOnline[3] }; + bool srcOnline[NODE_COUNT]; + for (int s = 0; s < NODE_COUNT; s++) srcOnline[s] = nodeOnline[s]; // ensure AIRLK is always a source even if somehow offline - srcOnline[0] = true; + srcOnline[N_AIRLK] = true; auto drawDottedLine = [&](int x0, int y0, int x1, int y1) { int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1; @@ -1600,10 +1639,10 @@ static void renderNetwork(bool full) } }; - for (int s = 0; s < 4; s++) { + for (int s = 0; s < NODE_COUNT; s++) { if (!srcOnline[s]) continue; // find edges where this node is one endpoint - for (int e = 0; e < 3; e++) { + for (int e = 0; e < EDGE_COUNT; e++) { int a = EDGES[e][0], b = EDGES[e][1]; if (a != s && b != s) continue; int neighbor = (a == s) ? b : a; diff --git a/src/netdeck/nd_layout.h b/src/netdeck/nd_layout.h index 0f10767..45bea4d 100644 --- a/src/netdeck/nd_layout.h +++ b/src/netdeck/nd_layout.h @@ -57,6 +57,7 @@ constexpr uint16_t C_NETBG = 0x0000; // black logic space constexpr uint16_t C_NODE = 0x2DAA; // green junction node constexpr uint16_t C_ICE = 0xF800; // red ICE-locked firewall node constexpr uint16_t C_HIVE = 0xF81F; // magenta droid hive node +constexpr uint16_t C_CAMERA = 0xFBEF; // cyan camera node (sees farther) constexpr uint16_t C_LINK = 0x2BCF; // teal network link trace constexpr uint16_t C_GHOST = 0x2BCF; // cameras-mode overlay outline color constexpr uint16_t C_PACKET = 0x07BF; // cyan data packet