diff --git a/emulator/tests/unit_tests.h b/emulator/tests/unit_tests.h index b44931a..a70cca5 100644 --- a/emulator/tests/unit_tests.h +++ b/emulator/tests/unit_tests.h @@ -233,3 +233,117 @@ TEST(unit/frenzy_hive_sphere_influence) // Note: may or may not acquire target depending on LOS, but the key test // is that with frenzy + outside sphere, target is rejected. } + +// --- fog of war (DESIGN.md §5.D) ------------------------------------------- + +// tile helpers for fog assertions +static bool fogNowAt(int tx, int ty) { return fogNow[tileIdx(tx, ty)] != 0; } +static bool fogSeenAt(int tx, int ty) { return fogSeen[tileIdx(tx, ty)] != 0; } + +TEST(unit/fog_initial_reveal_from_los_and_airlock_node) +{ + freshScenario(); + // marines in the airlock: their LOS window + the always-online AIRLK + // node uncover the airlock room; the rest of the ship is unknown + EXPECT(fogNowAt(2, 21)); // under marine 0 + EXPECT(fogNowAt(3, 21)); // under marine 1 + EXPECT(fogNowAt(4, 20)); // AIRLK node tile + EXPECT(fogNowAt(4, 18)); // room interior, marine LOS + EXPECT(fogNowAt(4, 20) == fogSeenAt(4, 20)); // current implies historical + // far side of the ship stays unknown until the squad gets there + EXPECT(!fogSeenAt(35, 7)); // server room near terminal + EXPECT(!fogSeenAt(36, 2)); // HIVE corner + // every currently-visible tile is also historical + for (int i = 0; i < nd::TILE_COLS * nd::TILE_ROWS; i++) + if (fogNow[i]) EXPECT(fogSeen[i]); +} + +TEST(unit/fog_walls_block_sight_but_faces_render) +{ + freshScenario(); + // a closed blast door seen from the corridor: the door tile itself + // renders (ray target), the server room behind it stays unknown + for (int i = 0; i < nd::TILE_COLS * nd::TILE_ROWS; i++) { + fogNow[i] = 0; + fogSeen[i] = 0; + } + revealLos(26, 12, FOG_SIGHT_R); // corridor, 2 tiles west of door + EXPECT(fogNowAt(27, 12)); // floor in front of the door + EXPECT(fogNowAt(28, 12)); // door tile itself (ray target) + EXPECT(fogSeenAt(28, 11)); // wall face beside it renders + EXPECT(!fogSeenAt(29, 12)); // behind the closed door: blocked + // and a plain wall segment blocks mid-ray + EXPECT(fogSeenAt(24, 12)); + EXPECT(!fogSeenAt(31, 12)); // server room interior +} + +TEST(unit/fog_historical_persists_after_losing_sight) +{ + freshScenario(); + // teleport marine 0 deep into the corridor and re-derive the fog: + // tiles the airlock used to see stay historical, but stop being current + EXPECT(fogNowAt(2, 14)); // visible from marine 0 + units[0].tx = 20; units[0].ty = 12; // corridor droid's old spot + units[1].alive = false; // isolate marine 0's window + updateFog(); + EXPECT(fogNowAt(20, 12)); // new position visible + // the airlock room: the online AIRLK node (4,20) keeps ITS connected + // tiles current, but tiles only the marine used to see go historical + EXPECT(fogNowAt(4, 20)); // node-connected: still lit + EXPECT(!fogNowAt(2, 14)); // out of every sight window... + EXPECT(fogSeenAt(2, 14)); // ...but remembered (grayscale) + // fresh LOS from the corridor reveals corridor tiles, not the server room + EXPECT(fogNowAt(18, 12)); + EXPECT(!fogSeenAt(34, 7)); // terminal still unknown +} + +TEST(unit/fog_online_nodes_uncover_connected_tiles) +{ + freshScenario(); + // JUNC at (18,6) is unknown until its link goes up + EXPECT(!fogSeenAt(18, 6)); + tryLink(N_JUNC); // AIRLK root is online + stepTicks(LINK_TICKS); // link build completes + EXPECT(nodeOnline[N_JUNC]); + EXPECT(fogNowAt(18, 6)); // node tile uncovered + EXPECT(fogNowAt(17, 6)); // connected room surroundings + // HIVE side stays unknown + EXPECT(!fogSeenAt(35, 4)); + // severing the link: JUNC tiles drop from current but stay historical + int li = linkIdx(0); + severLink(li); + updateFog(); + EXPECT(!nodeOnline[N_JUNC]); + EXPECT(!fogNowAt(18, 6)); + EXPECT(fogSeenAt(18, 6)); // historical (grayscale) memory +} + +TEST(unit/fog_gray565_is_neutral_and_clamped) +{ + uint16_t w = gray565(0xFFFF); // white -> white-ish gray + EXPECT_EQ((w >> 11) & 0x1F, w & 0x1F); // red == blue channel + EXPECT_EQ((w >> 5) & 0x3F, 2 * ((w >> 11) & 0x1F)); // green is double-wide + uint16_t g = gray565(nd::C_HIVE); // magenta -> neutral gray + EXPECT_EQ((g >> 11) & 0x1F, g & 0x1F); // red == blue channel + EXPECT(g != nd::C_HIVE); // hue removed + EXPECT_EQ(gray565(0x0000), 0x0000); // black stays black +} + +// fog sight must cover the marines' fire range: acquireTarget() uses the +// same losClear ray, so anything a marine can shoot at must be revealed — +// regression for marines shooting into darkness (fog radius < fire range) +TEST(unit/fog_sight_covers_marine_fire_range) +{ + freshScenario(); + units[0].tx = 20; units[0].ty = 12; // open corridor + units[1].alive = false; + updateFog(); + // (12,12) is exactly 8.0 tiles west along open row 12 = STATS marine + // range boundary: dx^2+dy^2 = 64 = rng2/4 — must be revealed + EXPECT(fogNowAt(12, 12)); + // (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 +} diff --git a/src/netdeck/NetDeck.ino b/src/netdeck/NetDeck.ino index 73b1e4b..d152a19 100644 --- a/src/netdeck/NetDeck.ino +++ b/src/netdeck/NetDeck.ino @@ -333,6 +333,8 @@ static void playTraceProgress() sound.play(e); } +static void updateFog(); // fwd decl: fog recompute lives below the unit layer + // --------------------------------------------------------------------------- // Sub-net logic // --------------------------------------------------------------------------- @@ -388,6 +390,7 @@ static void tryLink(int node) doorOpen = !doorOpen; if (doorOpen) playDoorOpen(); else playDoorClose(); + updateFog(); // the door tile changes the squad's sightlines Serial.printf("[net] DOOR_CTRL: blast door %s\n", doorOpen ? "OPEN" : "CLOSED"); flashMsg(doorOpen ? "DOOR OPEN" : "DOOR CLOSED"); return; @@ -613,6 +616,51 @@ static bool losClear(int x0, int y0, int x1, int y1) return true; } +// --------------------------------------------------------------------------- +// Fog of War (DESIGN.md §5.D): two bitmaps over the tile grid +// fogSeen[i] — historical: the tile has been seen at least once +// fogNow[i] — current: visible to the squad by LOS, or uncovered by an +// online network node +// Rendering: neither -> dark gray unknown, seen only -> grayscale memory, +// visible now -> full palette color. +// --------------------------------------------------------------------------- +static uint8_t fogNow[nd::TILE_COLS * nd::TILE_ROWS]; +static uint8_t fogSeen[nd::TILE_COLS * nd::TILE_ROWS]; + +// squad sight radius (tiles) — LOS rays from each marine reveal this window. +// MUST be >= the marines' fire range (STATS[U_MARINE].rng2 = 16*16 half-tiles² +// = 8.0 tiles): acquireTarget() uses the same losClear ray, so any tile a +// 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; + +// unknown-space color (RGB565): flat dark gray, no floorplan detail +constexpr uint16_t C_FOG_UNKNOWN = 0x2108; + +// reveal every tile visible from (cx,cy) within radius r: circular window, +// one Bresenham ray per tile. Walls and closed blast doors block the ray +// mid-flight, but a blocking tile that IS the ray target still renders +// (losClear ignores its endpoints), so room outlines are remembered. +static void revealLos(int cx, int cy, int r) +{ + int r2 = r * r; + for (int dy = -r; dy <= r; dy++) { + int dyy = dy * dy; + for (int dx = -r; dx <= r; dx++) { + if (dx * dx + dyy > r2) continue; + int tx = cx + dx, ty = cy + dy; + if (tx < 0 || ty < 0 || tx >= nd::TILE_COLS || ty >= nd::TILE_ROWS) + continue; + if ((dx || dy) && !losClear(cx, cy, tx, ty)) continue; + int i = tileIdx(tx, ty); + fogNow[i] = 1; + fogSeen[i] = 1; + } + } +} + // adjacent to a cover crate? (MVP: halves incoming hit chance) // (unit index, not a reference — see the planPath() note) static bool inCover(int ui) @@ -790,6 +838,8 @@ static void unitMove(int i) u.hasPath = false; // door shut / another unit in the way return; } + // fog of war: tile visibility re-derives from the marine's new position + // in updateFog() at the end of the tick u.tx = nx; u.ty = ny; u.mvCd = STATS[u.kind].speed; @@ -866,6 +916,32 @@ static void stepUnits() } } +// --------------------------------------------------------------------------- +// Fog of War recompute (DESIGN.md §5.D) +// --------------------------------------------------------------------------- +// Current visibility drains into the historical bitmap (seen-once stays +// remembered), then re-derives from the world state: +// - each living marine casts LOS rays over its sight window +// - each ONLINE network node uncovers its connected tiles (the room around +// the node — cyberspace access lights up physical space) +// Runs once per sim tick, after units/network stepped, plus after door +// toggles and scenario resets so the fog never goes stale. +static void updateFog() +{ + const int N = nd::TILE_COLS * nd::TILE_ROWS; + for (int i = 0; i < N; i++) + if (fogNow[i]) fogSeen[i] = 1; + for (int i = 0; i < N; i++) fogNow[i] = 0; + + for (int i = 0; i < 7; i++) { + 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++) + if (nodeOnline[i]) + revealLos(NODES[i].tx, NODES[i].ty, FOG_NODE_R); +} + // --------------------------------------------------------------------------- // Scenario reset (MVP: R replays the 60-second scenario instantly) // --------------------------------------------------------------------------- @@ -895,6 +971,14 @@ static void resetScenario() spawnUnit(5, U_TURRET, 33, 5); units[6].alive = false; // reinforcement slot + // fog of war starts blank: only what the marines see in the airlock and + // the always-online [AIRLK] node's connected tiles is uncovered + for (int i = 0; i < nd::TILE_COLS * nd::TILE_ROWS; i++) { + fogNow[i] = 0; + fogSeen[i] = 0; + } + updateFog(); + scenario = SC_LIVE; paused = true; } @@ -1128,6 +1212,7 @@ static void stepSim() if (blindT > 0) blindT--; stepNetwork(); stepUnits(); + updateFog(); // fog re-derives after this tick's movement + links } // --------------------------------------------------------------------------- @@ -1154,31 +1239,57 @@ static uint16_t dim565(uint16_t c) return (uint16_t)((r << 11) | (g << 5) | b); } +// grayscale conversion for the historical fog layer: luma-weighted RGB565 +// components re-packed as a neutral gray (r = b = luma, g = 2*luma) +static uint16_t gray565(uint16_t c) +{ + uint32_t r = (c >> 11) & 0x1F; + uint32_t g = (c >> 5) & 0x3F; + uint32_t b = c & 0x1F; + uint32_t lum = (r * 30 + (g * 59) / 2 + b * 11) / 100; // 0..31 + return (uint16_t)((lum << 11) | ((lum * 2) << 5) | lum); +} + 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]; + int fi = tileIdx(tx, ty); + bool now = fogNow[fi]; // in squad LOS / node reveal right now + bool seen = fogSeen[fi]; // historical: seen at least once + + // --- Fog of War layers (DESIGN.md §5.D) --- + // never seen -> flat dark gray (unknown space) + // seen, not now -> grayscale memory of the tile + // visible now -> full palette color 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; + if (!seen) c = C_FOG_UNKNOWN; + else if (!now) c = gray565(c); + + // the blast door: open frame when open (fog layers still apply) + if (t == '+' && doorOpen && seen) + c = now ? dim565(nd::C_FLOOR) : gray565(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; + if (t == '+' && doorOpen && seen) { + uint16_t dc = now ? (dim ? dim565(nd::C_DOOR) : nd::C_DOOR) + : gray565(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') { + if (t == 'A' && seen) { GFXP->drawRect(tx * nd::TILE, ty * nd::TILE, nd::TILE, nd::TILE, - dim ? dim565(nd::C_FLOOR) : nd::C_FLOOR); + now ? (dim ? dim565(nd::C_FLOOR) : nd::C_FLOOR) + : gray565(nd::C_FLOOR)); } - if (t == 'T') { // objective terminal: dark rack + cyan screen - uint16_t dc = dim ? dim565(nd::C_HUDBG) : nd::C_HUDBG; + if (t == 'T' && seen) { // objective terminal: dark rack + cyan screen + uint16_t dc = now ? (dim ? dim565(nd::C_HUDBG) : nd::C_HUDBG) + : gray565(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); + uint16_t tc = now ? (dim ? dim565(nd::C_PACKET) : nd::C_PACKET) + : gray565(nd::C_PACKET); + GFXP->fillRect(tx * nd::TILE + 1, ty * nd::TILE + 1, 6, 3, tc); } } } @@ -1442,6 +1553,9 @@ static void renderUnits(bool full) for (int i = 0; i < 7; i++) { const Unit &u = units[i]; if (!u.alive) continue; + // fog of war: hostiles on tiles the squad can't currently see are + // hidden (marines are the sight source, so they always render) + if (u.team == TEAM_DROID && !fogNow[tileIdx(u.tx, u.ty)]) continue; int px = unitPx(i), py = unitPy(i); uint16_t body, line; if (u.kind == U_MARINE) {