// 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/sim/growth.hpp" #include "sc2k/sim/region.hpp" #include "sc2k/sim/stadium_seasons.hpp" #include "sc2k/sim/things.hpp" #include "sc2k/ui/query.hpp" #include #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"); } // (11) The job-access row uses typed regional authority: a generic marker alone is not enough, and // a compatible road connection changes the same route assessment the growth scan consumes. { City c; c.xzon.at(64, 6) = 1; c.xbld.at(64, 6) = 130; c.xbit.at(64, 6) = 0x40 | 0x10; for (int column = 0; column <= 5; ++column) c.xbld.at(64, column) = 0x1D; c.xtxt.at(64, 0) = 0xFA; CHECK(row_value(ui::build_query(c, 64, 6, kHiDemand), "Job access") == "No"); const std::array connections = {sim::BorderConnection{64, 0, 3, 64, sim::BorderConnectionKind::Road, sim::BorderConnectionLayer::Surface, 1}}; const ui::QueryInfo connected = ui::build_query(c, 64, 6, kHiDemand, nullptr, connections); CHECK(row_value(connected, "Job access") == "Yes"); CHECK(row_value(connected, "Outlook") == "Stable"); } // (12) Query job access must use the live growth route's uniform effective limit. A level-one building // now retains the base limit 100, so this cost-77 path succeeds in both growth and the read-only Query. { City c; c.xzon.at(64, 64) = 0xF1; c.xbld.at(64, 64) = 112; c.xbit.at(64, 64) = 0x40; for (int column = 65; column <= 84; ++column) c.xbld.at(64, column) = 0x1D; c.xzon.at(64, 85) = 3; MsRand rng(91); CommuteRouteProbe liveProbe; const bool live = commute_route(c, 64, 64, 1, 1, 100, rng, {}, &liveProbe); CHECK(live && liveProbe.stop == CommuteRouteStop::Destination); CHECK(liveProbe.effectiveMaxCost == 100); CHECK(liveProbe.generalizedCost == 77); CHECK(ui::query_reaches_jobs(c, 64, 64, 1) == live); CHECK(row_value(ui::build_query(c, 64, 64, kHiDemand), "Job access") == "Yes"); } // (13) Every tile of a complete multi-tile building reports the growth anchor's result. Growth scans // the rotation-specific corner; a click on the opposite corner is within road-access distance only if // Query incorrectly routes from the clicked footprint tile. Exercise all four saved rotations. { const int anchors[4][2] = {{30, 31}, {30, 30}, {31, 30}, {31, 31}}; const int opposites[4][2] = {{31, 30}, {31, 31}, {30, 31}, {30, 30}}; for (int rotation = 0; rotation < 4; ++rotation) { City c; c.set_misc_be32(misc::kViewRotation, rotation); for (int row = 30; row <= 31; ++row) for (int column = 30; column <= 31; ++column) { c.xbld.at(row, column) = 140; c.xzon.at(row, column) = 1; c.xbit.at(row, column) = 0x40; } c.xzon.at(30, 30) |= 0x10; c.xzon.at(30, 31) |= 0x80; c.xzon.at(31, 30) |= 0x20; c.xzon.at(31, 31) |= 0x40; const int roadRow = opposites[rotation][0] + (opposites[rotation][0] == 31 ? 5 : -5); const int roadColumn = opposites[rotation][1]; c.xbld.at(roadRow, roadColumn) = 0x1D; c.xzon.at(roadRow, roadColumn + 1) = 3; MsRand rng(93 + rotation); CommuteRouteProbe liveProbe; const bool live = commute_route(c, anchors[rotation][0], anchors[rotation][1], 1, 2, 100, rng, {}, &liveProbe); CHECK(!live && liveProbe.stop == CommuteRouteStop::NoAccess); for (int row = 30; row <= 31; ++row) for (int column = 30; column <= 31; ++column) CHECK(row_value(ui::build_query(c, row, column, kHiDemand), "Job access") == "No"); } } // Funded transit is another shared Query/growth input. A physically intact rail-only commute reports // unavailable at 0% rail service and reachable at 100%; Query does not silently use its default. { City c; c.xzon.at(64, 64) = 0xF1; c.xbld.at(64, 64) = 112; c.xbit.at(64, 64) = 0x40; c.xbld.at(64, 65) = xbld::kRailDepot; for (int column = 66; column <= 72; ++column) c.xbld.at(64, column) = 0x2C; c.xbld.at(64, 73) = xbld::kRailDepot; c.xzon.at(64, 74) = 3; CommuteRouteConfig stopped; stopped.serviceEfficiency.modeBasisPoints[static_cast( CommuteTravelMode::Rail)] = 0; MsRand rng(97); CommuteRouteProbe liveProbe; CHECK(!commute_route(c, 64, 64, 1, 1, 100, rng, {}, &liveProbe, stopped)); CHECK(liveProbe.stop == CommuteRouteStop::ServiceUnavailable); CHECK(!ui::query_reaches_jobs(c, 64, 64, 1, {}, stopped)); CHECK(row_value(ui::build_query(c, 64, 64, kHiDemand, nullptr, {}, stopped), "Job access") == "No"); CommuteRouteConfig funded; CHECK(ui::query_reaches_jobs(c, 64, 64, 1, {}, funded)); CHECK(row_value(ui::build_query(c, 64, 64, kHiDemand, nullptr, {}, funded), "Job access") == "Yes"); } // ---- 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); constexpr int occupant = 1; sim::thing_field(c, occupant, sim::thg::Type) = 1; sim::thing_field(c, occupant, sim::thg::X) = 61; sim::thing_field(c, occupant, sim::thg::Y) = 61; c.xtxt.at(61, 61) = static_cast(xtxt::kThingFirst + occupant); // 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.xtxt.at(61, 61) == static_cast(xtxt::kThingFirst + occupant)); CHECK(sim::thing_field(c, occupant, sim::thg::SavedTile) == 61); CHECK(sim::valid_thing_ownership_for_city(c)); CHECK(sim::capture_thing_continuation(c, MsRand(0x51A7)).has_value()); 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); CHECK(construct_building(c, 40, 40, xbld::kStadium, sim::kStadiumLifecycleFootprintSize, 0)); 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); // Live annual STAD authority advances the record within bounds and keeps a full season. const auto migratedStadiums = sim::make_migrated_stadium_lifecycle_state(c, 0); CHECK(migratedStadiums.has_value()); if (!migratedStadiums) return 1; sim::StadiumLifecycleState stadiums = *migratedStadiums; sim::EventBuffer stadiumEvents; for (int y = 0; y < 20; ++y) { stadiumEvents.begin_batch(300 * (y + 1)); const auto settlement = sim::settle_annual_stadium_seasons( c, stadiums, 300 * (y + 1), 250000, &stadiumEvents); CHECK(settlement.status == sim::StadiumSeasonSettlementStatus::Settled); CHECK(stadiumEvents.view().size() == 1); } const sim::StadiumTeam t2 = sim::stadium_team(c, 41, 41); const auto* season = std::get_if(&stadiumEvents.view()[0].payload); CHECK(season && season->microsimSlot == i); CHECK(season && season->wins == t2.wins && season->losses == t2.losses); CHECK(season && season->attendance == t2.attendance); CHECK(t2.wins + t2.losses == 40); CHECK(t2.wins >= 9 && t2.wins <= 40); 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; }