// Validation of the Query-tool analyzer (ui/query.hpp build_query): names, the resource (power/water/ // road/job) rows, and the abandonment-reason chain (no power → no road → can't reach jobs → no demand). // Deterministic (the job-access probe uses a fixed-seed RNG internally). Panel strings are catalog // keys — load the English catalog and assert the English text. #include "sc2k/render/asset_loader.hpp" #include "sc2k/ui/query.hpp" #include #include "test_harness.hpp" namespace { using namespace sc2k; std::string row_value(const ui::QueryInfo& q, const std::string& label) { for (const auto& r : q.rows) if (r.label == label) return r.value; return ""; } bool has_row(const ui::QueryInfo& q, const std::string& label) { for (const auto& r : q.rows) if (r.label == label) return true; return false; } const int kNoDemand[3] = {-1500, -1500, -1500}; const int kHiDemand[3] = {1800, 1800, 1800}; // A building of zone-nibble `znib` (and optional XBLD `bcode`) at (30,30), powered + watered, with a road // run leading to BOTH an R and a C job zone — so a commute from R, C, or I all reach a job (high score). City connected(int znib, int bcode) { City c; c.xzon.at(30, 30) = std::uint8_t(znib); if (bcode) c.xbld.at(30, 30) = std::uint8_t(bcode); c.xbit.at(30, 30) = 0x40 | 0x10; // powered + watered for (int x = 31; x <= 35; ++x) c.xbld.at(30, x) = 0x1D; // a road run east c.xzon.at(30, 36) = 0x01; // an R job past the road end c.xzon.at(29, 35) = 0x03; // a C job beside the road return c; } } // namespace int main() { using namespace sc2k; i18n::load_locale(render::assets_dir() + "/lang", "en"); // panel strings are catalog keys // (1) Bare terrain / water / trees / loose rubble are NOT queryable. { City c; CHECK(!ui::build_query(c, 40, 40, kHiDemand).queryable); c.xbld.at(40, 40) = 8; CHECK(!ui::build_query(c, 40, 40, kHiDemand).queryable); // tree c.xbld.at(40, 40) = 3; CHECK(!ui::build_query(c, 40, 40, kHiDemand).queryable); // rubble, unzoned } // (2) A road network tile IS queryable and names correctly. { City c; c.xbld.at(20, 20) = 0x1D; ui::QueryInfo q = ui::build_query(c, 20, 20, kHiDemand); CHECK(q.queryable); CHECK(q.title == "Road"); } // (3) A developed, powered, watered R building WITH reachable jobs → all-good + "Stable". Labels match // the game (Powered/Watered). { ui::QueryInfo q = ui::build_query(connected(0x01, 130), 30, 30, kHiDemand); CHECK(q.queryable); CHECK(q.subtitle.find("Residential") != std::string::npos); CHECK(q.subtitle.find("low-density") != std::string::npos); CHECK(row_value(q, "Powered") == "Yes"); CHECK(row_value(q, "Watered") == "Yes"); CHECK(row_value(q, "Job access") == "Yes"); CHECK(row_value(q, "Outlook") == "Stable"); CHECK(has_row(q, "Land Value") && has_row(q, "Crime") && has_row(q, "Pollution") && has_row(q, "Altitude")); } // (4) NO POWER → at risk, named via the red Powered row. { City c = connected(0x01, 130); c.xbit.at(30, 30) = 0; // strip power (keep road + jobs) ui::QueryInfo q = ui::build_query(c, 30, 30, kHiDemand); CHECK(row_value(q, "Powered") == "No"); CHECK(row_value(q, "Outlook") == "At risk of abandonment"); } // (5) Powered but NO ROAD adjacent → can't reach jobs. { City c; c.xzon.at(30, 30) = 0x01; c.xbld.at(30, 30) = 130; c.xbit.at(30, 30) = 0x40; ui::QueryInfo q = ui::build_query(c, 30, 30, kHiDemand); CHECK(row_value(q, "Job access") == "No"); CHECK(row_value(q, "Outlook") == "At risk of abandonment"); } // (6) A road is adjacent but there's NO reachable job → Job access No, at risk. { City c; c.xzon.at(30, 30) = 0x01; c.xbld.at(30, 30) = 130; c.xbit.at(30, 30) = 0x40 | 0x10; for (int x = 31; x <= 33; ++x) c.xbld.at(30, x) = 0x1D; // a short dead-end road, no jobs anywhere ui::QueryInfo q = ui::build_query(c, 30, 30, kHiDemand); CHECK(row_value(q, "Job access") == "No"); // jobs are unreachable CHECK(row_value(q, "Outlook") == "At risk of abandonment"); } // (7) Eligible (powered + reachable) but low demand → NOT falsely warned (the bug the user caught): a // reachable, powered building reads "Stable", because that's what the sim does. { ui::QueryInfo q = ui::build_query(connected(0x01, 130), 30, 30, kNoDemand); CHECK(row_value(q, "Job access") == "Yes"); CHECK(row_value(q, "Outlook") == "Stable"); } // (8) A zoned-but-VACANT lot: won't develop without power. { City c; c.xzon.at(30, 30) = 0x03; // empty C zone ui::QueryInfo q = ui::build_query(c, 30, 30, kHiDemand); CHECK(q.queryable); CHECK(q.title.find("Commercial") != std::string::npos); CHECK(row_value(q, "Outlook") == "Will not develop"); } // (9) Names: density wording + civic + power-plant. { ui::QueryInfo q = ui::build_query(connected(0x02, 130), 30, 30, kHiDemand); CHECK(q.subtitle.find("high-density") != std::string::npos); City c2; c2.xbld.at(10, 10) = 210; CHECK(ui::build_query(c2, 10, 10, kHiDemand).title == "Police Station"); c2.xbld.at(11, 11) = 207; CHECK(ui::build_query(c2, 11, 11, kHiDemand).title == "Coal Power Plant"); } // (10) An ABANDONED ruin (code 172) is named "Abandoned building", never "Stable" — eligible ⇒ // "Redeveloping"; no power ⇒ "Abandoned". { City c = connected(0x06, 172); // dense-I ruin, powered + road + jobs ui::QueryInfo q = ui::build_query(c, 30, 30, kHiDemand); CHECK(q.title == "Abandoned building"); CHECK(row_value(q, "Job access") == "Yes"); CHECK(row_value(q, "Outlook") == "Redeveloping"); City c2 = connected(0x06, 172); c2.xbit.at(30, 30) = 0; // strip power CHECK(row_value(ui::build_query(c2, 30, 30, kHiDemand), "Outlook") == "Abandoned"); c2.xbld.at(30, 30) = 164; // a legit (non-ruin) I building CHECK(ui::build_query(c2, 30, 30, kHiDemand).title != "Abandoned building"); } // ---- microsim label subsystem: provision a slot, rename, and see it in the Query title ---- { City c; c.set_funds(100000); for (int r = 60; r < 64; ++r) // a 4×4 stadium (215) footprint for (int cc = 60; cc < 64; ++cc) { c.xbld.at(r, cc) = 215; c.xbit.at(r, cc) = 0xE0; } const int demand[3] = {0, 0, 0}; CHECK(ui::build_query(c, 61, 61, demand).title == "Stadium"); // built-in name, no label yet CHECK(sim::building_label_slot(c, 61, 61) == 0); // Provision + name it: a dynamic building takes the first free XMIC slot (10) → label slot 61. CHECK(sim::provision_label(c, 62, 62, "Gotham Rockets") == 61); for (int r = 60; r < 64; ++r) // the WHOLE footprint carries the slot for (int cc = 60; cc < 64; ++cc) CHECK(sim::building_label_slot(c, r, cc) == 61); CHECK(c.xmic[10 * 8] == 215); // microsim record marks the slot allocated CHECK(ui::build_query(c, 60, 63, demand).title == "Gotham Rockets"); CHECK(sim::label_text(c, 61) == "Gotham Rockets"); // Rename in place reuses the same slot (no second allocation). CHECK(sim::provision_label(c, 61, 61, "City Sluggers") == 61); CHECK(ui::build_query(c, 62, 61, demand).title == "City Sluggers"); CHECK(c.xmic[11 * 8] == 0); // A singleton reward (Mayor's House 236 → type 17 → fixed slot 1 → label 52). c.xbld.at(70, 70) = 236; c.xbit.at(70, 70) = 0xE0; CHECK(sim::provision_label(c, 70, 70, "Manor") == 52); CHECK(sim::label_text(c, 52) == "Manor"); // A plain RCI building has no microsim → no slot; keeps its built-in title, rename is a no-op. c.xbld.at(80, 80) = 0x74; CHECK(sim::microsim_type(0x74) == 0); CHECK(sim::provision_label(c, 80, 80, "Nope") == 0); CHECK(sim::building_label_slot(c, 80, 80) == 0); std::printf("test_query: microsim labels — provision/rename/footprint/singleton/query OK\n"); } // ---- stadium local team: provision generates a team, the record round-trips, annual tick advances it ---- { City c; c.set_funds(100000); for (int r = 40; r < 44; ++r) // a 4×4 stadium (0xD7) for (int cc = 40; cc < 44; ++cc) { c.xbld.at(r, cc) = 0xD7; c.xbit.at(r, cc) = 0xE0; } CHECK(sim::stadium_slot(c, 41, 41) == -1); // no team until provisioned sim::provision_stadium(c, 41, 41, 137); const int i = sim::stadium_slot(c, 41, 41); CHECK(i >= 10); // a dynamic microsim slot CHECK(c.xmic[std::size_t(i) * 8] == 0xD7); // record tagged as a stadium const sim::StadiumTeam t = sim::stadium_team(c, 41, 41); CHECK(t.valid); CHECK(t.capacity == 25000); CHECK(t.wins + t.losses == 40); // a 40-game season CHECK(t.attendance > 0 && t.attendance <= 25000); CHECK(!t.sport.empty() && !t.team.empty()); // sport + generated team name CHECK(t.team.find(' ') != std::string::npos); // " " // The team name lives in its own XLAB slot (201-250), distinct from the building-name slot. const int teamSlot = sim::xmic_u16(c, i, 6); CHECK(teamSlot >= 201 && teamSlot < 251); CHECK(sim::label_text(c, teamSlot) == t.team); CHECK(sim::building_label_slot(c, 41, 41) != teamSlot); // Re-provision is idempotent (keeps the existing team). sim::provision_stadium(c, 42, 42, 999); CHECK(sim::stadium_team(c, 41, 41).team == t.team); // Annual tick advances the record within bounds and keeps a full season. for (int y = 0; y < 20; ++y) sim::stadium_annual_tick(c, 250000, unsigned(y * 7 + 1)); const sim::StadiumTeam t2 = sim::stadium_team(c, 41, 41); CHECK(t2.wins + t2.losses == 40); CHECK(t2.wins >= 4 && t2.wins <= 38); CHECK(t2.attendance > 0 && t2.attendance <= 25000); // The Query window surfaces the team: Capacity / Attendance / Local Team / Wins-Losses rows, and // the title is the (renamable) stadium building name, not the team. const int dmd0[3] = {0, 0, 0}; const ui::QueryInfo sq = ui::build_query(c, 41, 41, dmd0); CHECK(sq.title == "Stadium"); // building name (renamable), not the team CHECK(row_value(sq, "Capacity") == "25,000"); CHECK(row_value(sq, "Local Team") == t2.team); // team name and sport are separate rows CHECK(row_value(sq, "Sport") == i18n::tr(t2.sport)); // sim carries the sport KEY; the row shows the text CHECK(row_value(sq, "Wins-Losses") == std::to_string(t2.wins) + "-" + std::to_string(t2.losses)); CHECK(!row_value(sq, "Attendance").empty()); CHECK(sq.renamable); // still renamable (the stadium name) std::printf("test_query: stadium team — sport/name/record/attendance generate + advance + query OK\n"); } if (failures == 0) std::printf("test_query: ALL CHECKS PASSED\n"); else std::printf("test_query: %d CHECK(S) FAILED\n", failures); return failures ? 1 : 0; }