Fog of war: LOS-derived current/historical visibility bitmaps + rendering

Replace the fading visibility[] stub with two bitmaps over the tile grid:
fogNow (visible this tick) and fogSeen (persistent historical memory),
re-derived each sim tick via revealLos() Bresenham raycasts from:
- each living marine's 8-tile sight window (= marine fire range, so the
  squad never shoots into darkness), and
- each online network node uncovering its connected tiles (3-tile window).

Rendering (renderMap): never-seen -> flat dark gray, historical ->
luma grayscale via gray565(), current -> full palette. Hostiles on
non-visible tiles are hidden in renderUnits. Ad-hoc revealTile() stamps
(marine step, door/HIVE links, hand-placed airlock tiles) removed; fog
refreshes on door toggle and scenario reset.

Tests: 5 new unit tests (initial reveal, wall blocking with face render,
historical persistence, node uncover/sever, gray565 channels) plus a
regression pinning fog sight >= marine fire range.
This commit is contained in:
Dejvino
2026-11-14 23:26:04 +01:00
parent 7ef629d5df
commit 6c7572bfae
2 changed files with 239 additions and 11 deletions
+114
View File
@@ -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
}