Files
lilygo-t-deck/src/netdeck/MapTest.cpp
T
Dejvino b38d5d4b56 Extract map-specific data into a generic MapSource interface
One interface (nd_mapsource.h) now owns everything map-specific: tiles,
rooms/doors/cameras, the network graph (nodes + edges with roles replacing
the hardcoded N_AIRLK/N_DOOR/N_HIVE indices) and the entity spawn list.
The scenario populates its structures once via selectMap() and never checks
which map is active.

- DemoMapSource: self-contained demo grid (moved out of the sketch), demo
  node/edge/spawn tables
- GenMapSource: adapts the random MapGenerator — now with L-corridors for
  guaranteed connectivity, doors that lead somewhere, airlock pad, terminal
  and crates (never on corridor tiles); network graph derived from the room
  chain, marine-first spawn list with droid patrols
- NetDeck.ino: all source branching removed; red trace follows the real
  active-link path (BFS HIVE->ROOT); reinforcement spawns near the blast door
- Tests: MapTest validates 50 generated runs + demo via the interface with
  an airlock flood-fill; new generated-map integration journey; test node
  handles resolve through the source (no hardcoded indices)
2026-11-15 18:21:35 +01:00

147 lines
5.3 KiB
C++

// Minimal test for the map sources (nd_mapsource.h / nd_mapgen.h)
// Validates the generated map and the generic MapSource interface.
#include "nd_mapgen.h"
#include "nd_demomap.h"
static bool allPass = true;
static void check(const char *name, bool ok) {
printf("%-42s %s\n", name, ok ? "PASS" : "FAIL");
if (!ok) allPass = false;
}
// every node reachable from the root over the edge list?
static bool graphConnected(const nd::MapSource &m) {
int n = m.nodeCount();
int root = m.rootNode();
if (n <= 0 || root < 0) return false;
bool seen[nd::MAX_NODES] = {};
int queue[nd::MAX_NODES], qh = 0, qt = 0;
queue[qt++] = root;
seen[root] = true;
while (qh < qt) {
int cur = queue[qh++];
for (int e = 0; e < m.edgeCount(); e++) {
int a = m.edge(e).a, b = m.edge(e).b;
int nbr = (a == cur) ? b : (b == cur) ? a : -1;
if (nbr < 0 || seen[nbr]) continue;
seen[nbr] = true;
queue[qt++] = nbr;
}
}
for (int i = 0; i < n; i++)
if (!seen[i]) return false;
return true;
}
// walkable flood fill from the airlock ('+' doors treated as open):
// the terminal and every entity spawn must be reachable on foot
static bool tilesConnected(const nd::MapSource &m) {
static bool seen[nd::TILE_COLS * nd::TILE_ROWS];
for (int i = 0; i < nd::TILE_COLS * nd::TILE_ROWS; i++) seen[i] = false;
int ax, ay;
if (!m.findTile('A', ax, ay)) return false;
int queue[nd::TILE_COLS * nd::TILE_ROWS], qh = 0, qt = 0;
queue[qt++] = ay * nd::TILE_COLS + ax;
seen[queue[0]] = true;
while (qh < qt) {
int cur = queue[qh++];
int cx = cur % nd::TILE_COLS, cy = cur / nd::TILE_COLS;
static const int DX[4] = { 1, -1, 0, 0 };
static const int DY[4] = { 0, 0, 1, -1 };
for (int d = 0; d < 4; d++) {
int nx = cx + DX[d], ny = cy + DY[d];
if (nx < 0 || ny < 0 || nx >= nd::TILE_COLS || ny >= nd::TILE_ROWS) continue;
char t = m.tileAt(nx, ny);
bool pass = (t == '.' || t == 'A' || t == 'T' || t == '+');
if (!pass || seen[ny * nd::TILE_COLS + nx]) continue;
seen[ny * nd::TILE_COLS + nx] = true;
queue[qt++] = ny * nd::TILE_COLS + nx;
}
}
int tx, ty;
if (!m.findTile('T', tx, ty) || !seen[ty * nd::TILE_COLS + tx]) return false;
for (int s = 0; s < m.spawnCount(); s++) {
const nd::UnitSpawn &sp = m.spawn(s);
if (!seen[sp.ty * nd::TILE_COLS + sp.tx]) return false;
}
return true;
}
static void testGeneratedSource(int seed) {
nd::MapGenerator gen;
nd::GenMapSource src(gen);
src.generate();
char title[64];
printf("--- generated map (run %d) ---\n", seed);
// structural tiles exist
int tx, ty;
snprintf(title, sizeof(title), "airlock + terminal tiles present");
check(title, src.findTile('A', tx, ty) && src.findTile('T', tx, ty));
// room count (4-6)
snprintf(title, sizeof(title), "room count 4-6 (got %zu)",
src.getRooms().size());
check(title, src.getRooms().size() >= 4 && src.getRooms().size() <= 6);
// marines: spawns 0/1 must be marines (the squad)
check("spawns 0/1 are marines",
src.spawnCount() >= 2 &&
src.spawn(0).kind == 0 && src.spawn(1).kind == 0);
// spawn budget: leaves room for the reinforcement droid
snprintf(title, sizeof(title), "spawn count %d <= MAX_UNITS-1", src.spawnCount());
check(title, src.spawnCount() > 0 && src.spawnCount() < nd::MAX_UNITS);
// roles present
check("root/hive/door nodes present",
src.rootNode() >= 0 && src.hiveNode() >= 0 && src.doorNode() >= 0);
// node node(0) is the online root
check("root node online", src.node(src.rootNode()).online);
// network graph connected
check("network graph connected from root", graphConnected(src));
// physical walkability: terminal + all spawns reachable from the airlock
check("tiles connected (airlock flood)", tilesConnected(src));
// node/edge pools within bounds
snprintf(title, sizeof(title), "nodes %d<=%d, edges %d<=%d",
src.nodeCount(), nd::MAX_NODES, src.edgeCount(), nd::MAX_EDGES);
check(title, src.nodeCount() <= nd::MAX_NODES && src.edgeCount() <= nd::MAX_EDGES);
}
static void testDemoSource() {
printf("--- demo map ---\n");
nd::DemoMapSource demo;
check("demo terminal tile at (34,7)", demo.tileAt(34, 7) == 'T');
check("demo airlock tile at (2,21)", demo.tileAt(2, 21) == 'A');
check("demo blast door at (28,12)", demo.tileAt(28, 12) == '+');
check("demo node count 7", demo.nodeCount() == 7);
check("demo roles root/door/hive",
demo.rootNode() == 0 && demo.doorNode() == 2 && demo.hiveNode() == 3);
check("demo spawns 11, marines first",
demo.spawnCount() == 11 && demo.spawn(0).kind == 0 && demo.spawn(1).kind == 0);
check("demo patrol endpoints set", demo.spawn(2).patA >= 0 && demo.spawn(2).patB >= 0);
check("demo graph connected", graphConnected(demo));
check("demo tiles connected (airlock flood)", tilesConnected(demo));
}
int main() {
for (int i = 0; i < 50; i++) testGeneratedSource(i);
testDemoSource();
printf("\n");
if (allPass) {
printf("ALL TESTS PASSED\n");
return 0;
}
printf("SOME TESTS FAILED\n");
return 1;
}