// Validates the pollution field by correlation against the saved XPLT minimap. // // Pollution diffuses iteratively (the previous field feeds the next), so a single step from a saved // city won't be bit-equal to the saved field, but at the city's steady state it must correlate // strongly with it. The original game's own validation reported r≈0.99; we require a high Pearson r. #include "sc2k/sim/pollution.hpp" #include "sc2k/io/sc2_file.hpp" #include #include #include #include "test_paths.hpp" #include "test_harness.hpp" static double pearson(const std::array& a, const sc2k::Layer& b) { double sx = 0, sy = 0, sxx = 0, syy = 0, sxy = 0; const int n = 64 * 64; for (int i = 0; i < n; ++i) { double x = a[i], y = b.cells[i]; sx += x; sy += y; sxx += x * x; syy += y * y; sxy += x * y; } double num = n * sxy - sx * sy; double den = std::sqrt((n * sxx - sx * sx) * (n * syy - sy * sy)); return den > 0 ? num / den : 0.0; } static void check_city(const char* name) { using namespace sc2k; std::string path = (cities_dir()) + name; City c; auto r = io::load_sc2(path, c); if (!r.ok) { std::printf(" %-13s skipped (%s)\n", name, r.error.c_str()); return; } // Skip cities with negligible pollution (correlation is meaningless on a near-zero field). long savedTotal = 0; for (auto v : c.xplt.cells) savedTotal += v; if (savedTotal < 64 * 64) { std::printf(" %-13s (little pollution; skipped)\n", name); return; } std::array out{}; pollution_step(c, out); double rr = pearson(out, c.xplt); std::printf(" %-13s Pearson r(recomputed, saved XPLT) = %.4f\n", name, rr); CHECK(rr > 0.90); } int main() { for (const char* n : {"TOKYO.SC2", "LONDON.SC2", "NYC.SC2", "PARIS.SC2", "BIGGESTC.SC2"}) check_city(n); if (failures == 0) { std::printf("pollution: all checks passed\n"); return 0; } std::printf("pollution: %d failure(s)\n", failures); return 1; }