- RomAi: transliteration of re/tools/ai_ref.py (planner 81:D9BA, pad logic 81:8A52) over a WRAM model; tables read from the user's ROM at runtime - tools/ai_validate: replays the Mesen captures — 411/411 planner calls and the same pad-frame matches as the Python reference - FaithfulCpu: builds the ROM memory image from Match state, spawn setup (12-frame entry, difficulty 0/1 drop delays), 82:D1FB flags with all power predicates, pad -> Input respecting rotate/slide states - Player: entry-window held shifts every frame, entry/soft-drop overrides - app: cpu[0-3] uses the original AI when rom/tbg_jp.sfc (or --rom) is valid Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
88 lines
3.3 KiB
C++
88 lines
3.3 KiB
C++
// 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 <algorithm>
|
|
#include <cstdio>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iterator>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "core/rom_ai.hpp"
|
|
|
|
namespace {
|
|
|
|
std::vector<std::uint8_t> slurp(const std::filesystem::path& p) {
|
|
std::ifstream f(p, std::ios::binary);
|
|
return {std::istreambuf_iterator<char>(f), {}};
|
|
}
|
|
|
|
void validate_planner(const std::vector<std::uint8_t>& rom, const std::filesystem::path& dir, int& ok, int& total) {
|
|
const auto data = slurp(dir / "calls.bin");
|
|
std::size_t i = 0;
|
|
std::vector<std::uint8_t> entry;
|
|
while (i + 4 <= data.size()) {
|
|
const std::string tag(data.begin() + static_cast<long>(i), data.begin() + static_cast<long>(i + 4));
|
|
i += 4;
|
|
if (tag == "ENTR") {
|
|
entry.assign(data.begin() + static_cast<long>(i), data.begin() + static_cast<long>(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<std::size_t>(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<std::uint8_t>& 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<unsigned>(std::stoul(vs, nullptr, 16));
|
|
if (f < 1 || static_cast<std::size_t>(f) > n) continue;
|
|
tbg::RomAi ai(rom);
|
|
std::copy_n(rec.begin() + static_cast<long>((static_cast<std::size_t>(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;
|
|
}
|