// Replays Mesen captures of the real game through the C++ RomAi port (same method as re/tools/ai_ref.py). // usage: ai_validate ROM DIR... DIR with calls.bin -> planner check; DIR with rec.bin + pad.txt -> pad check #include #include #include #include #include #include #include #include "core/rom_ai.hpp" namespace { std::vector slurp(const std::filesystem::path& p) { std::ifstream f(p, std::ios::binary); return {std::istreambuf_iterator(f), {}}; } void validate_planner(const std::vector& rom, const std::filesystem::path& dir, int& ok, int& total) { const auto data = slurp(dir / "calls.bin"); std::size_t i = 0; std::vector entry; while (i + 4 <= data.size()) { const std::string tag(data.begin() + static_cast(i), data.begin() + static_cast(i + 4)); i += 4; if (tag == "ENTR") { entry.assign(data.begin() + static_cast(i), data.begin() + static_cast(i + 0x800)); i += 0x800; } else if (tag == "EXIT") { i += 0x100; } else if (tag == "E1B6") { const std::uint8_t* want = data.data() + i; i += 0x100; tbg::RomAi ai(rom); std::copy(entry.begin(), entry.end(), ai.wram().begin()); const bool ran = ai.plan(entry[0x12A] ? 0x0462 : 0x0202); bool good = ran; for (int k = 0x9D; k < 0xFC; ++k) good &= ai.wram()[static_cast(0x100 + k)] == want[k]; ++total; ok += good; if (!good) std::printf("%s call %d: want col,rot=%d,%d got=%d,%d%s\n", dir.c_str(), total, want[0xDD], want[0xDC], ai.wram()[0x1DD], ai.wram()[0x1DC], ran ? "" : " (trap)"); } else { std::printf("%s: bad tag\n", dir.c_str()); return; } } } void validate_pad(const std::vector& rom, const std::filesystem::path& dir) { constexpr std::size_t kFrame = 0x800; const auto rec = slurp(dir / "rec.bin"); const std::size_t n = rec.size() / kFrame; std::ifstream pads(dir / "pad.txt"); std::string fs, vs; int ok = 0, bad = 0; while (pads >> fs >> vs) { if (vs == "PLAN") continue; const long f = std::stol(fs); const unsigned v = static_cast(std::stoul(vs, nullptr, 16)); if (f < 1 || static_cast(f) > n) continue; tbg::RomAi ai(rom); std::copy_n(rec.begin() + static_cast((static_cast(f) - 1) * kFrame), kFrame, ai.wram().begin()); (ai.pad(0x0462) == v ? ok : bad)++; } std::printf("%s: pad %d/%d frames match\n", dir.c_str(), ok, ok + bad); } } // namespace int main(int argc, char** argv) { if (argc < 3) { std::fprintf(stderr, "usage: %s ROM DIR...\n", argv[0]); return 2; } const auto rom = slurp(argv[1]); int ok = 0, total = 0; for (int a = 2; a < argc; ++a) { const std::filesystem::path d = argv[a]; if (std::filesystem::exists(d / "calls.bin")) validate_planner(rom, d, ok, total); if (std::filesystem::exists(d / "rec.bin")) validate_pad(rom, d); } if (total) std::printf("planner: %d/%d calls reproduced exactly\n", ok, total); return ok == total ? 0 : 1; }