// Battle-layer tests: garbage routing, casting, powers, rounds. #include #include #include #include #include #include #include #include "check.hpp" #include "core/ai.hpp" #include "core/board_ops.hpp" #include "core/campaign.hpp" #include "core/replay.hpp" #include "core/match.hpp" namespace { using namespace tbg; const CharacterDef* chr(std::string_view name) { const Roster& r = Roster::original(); return &r.characters[static_cast(r.find(name))]; } MatchConfig test_config() { MatchConfig cfg = MatchConfig::instant(); cfg.seed = 7; return cfg; } const std::array kIdle{}; // Casting discards the falling pieces (state 13); step until `player` has a piece in play again. template void wait_spawn(M& m, int player) { const std::vector idle(static_cast(m.player_count())); for (int f = 0; f < 400 && !m.player(player).active(); ++f) m.step(idle); } int filled_rows(const Board& b) { int n = 0; for (int y = b.hidden_rows(); y < b.height(); ++y) n += !b.row_empty(y); return n; } int cells_with(const Board& b, Cell v) { int n = 0; for (int y = 0; y < b.height(); ++y) for (int x = 0; x < b.width(); ++x) n += b.at(x, y) == v; return n; } void test_garbage_arrives_at_victim_cycle_start() { Match m(test_config(), {chr("Halloween"), chr("Mirurun")}); m.player(0).set_attack_stock(3); m.step(kIdle); // both players start a cycle: P2 receives P1's 3 lines CHECK(m.player(0).attack_stock() == 0); const Board& b = m.player(1).board(); CHECK(filled_rows(b) == 3); int hole = -1; for (int y = b.height() - 3; y < b.height(); ++y) { int empties = 0, col = -1; for (int x = 0; x < b.width(); ++x) if (b.at(x, y) == cell::kEmpty) ++empties, col = x; CHECK(empties == 1); CHECK(col >= 0 && col <= 8); // never column 9 if (hole == -1) hole = col; CHECK(col == hole); // same hole for consecutive rows } CHECK(filled_rows(m.player(0).board()) == 0); } void test_stocks_net_at_cycle_start() { // 81:D6AE, observed in Mesen: stocks 3 (P1) vs 2 (P2) -> 1 vs 0 at the next cycle start; P2 gets one row. Match m(test_config(), {chr("Halloween"), chr("Mirurun")}); m.player(0).set_attack_stock(3); m.player(1).set_attack_stock(2); m.step(kIdle); CHECK(m.player(0).attack_stock() == 0 && m.player(1).attack_stock() == 0); CHECK(filled_rows(m.player(1).board()) == 1); CHECK(filled_rows(m.player(0).board()) == 0); // Old replays (no netting): P2 receives P1's full stock. MatchConfig old = test_config(); old.stock_netting = false; Match o(old, {chr("Halloween"), chr("Mirurun")}); o.player(0).set_attack_stock(3); o.player(1).set_attack_stock(2); o.step(kIdle); CHECK(filled_rows(o.player(1).board()) == 3); } void test_big_clear_freezes_the_match() { // 81:92D9: a 4-line clear blocks both players for 16 frames. MatchConfig cfg = test_config(); cfg.big_clear_freeze = 16; Match m(cfg, {chr("Halloween"), chr("Mirurun")}); m.queue().set_override(PieceType::I); m.step(kIdle); Board& b = m.player(0).board(); for (int y = b.height() - 4; y < b.height(); ++y) for (int x = 0; x < b.width(); ++x) if (x != 9) b.set(x, y, cell::kBlock); // Stand the I up at the right wall and let it fall. std::array in{}; in[0].rotate_cw = true; m.step(in); in = {}; in[0].right = true; for (int f = 0; f < 3000 && m.player(0).total_lines() == 0; ++f) m.step(in); CHECK(m.player(0).total_lines() == 4); CHECK(m.big_clear() == 16); const int other = m.frame(); for (int k = 0; k < 16; ++k) m.step(kIdle); CHECK(m.big_clear() == 0 && m.frame() == other); // no player stepped m.step(kIdle); CHECK(m.frame() == other + 1); } void test_cast_picks_highest_affordable_level_and_resets_meter() { Match m(test_config(), {chr("Aladdin"), chr("Mirurun")}); m.step(kIdle); m.player(0).set_meter(3); // affords level 3 (cost 3): Aladdin's Fax CHECK(m.affordable_level(0) == 2); ops::fill_bottom_holes(m.player(0).board(), 2); m.cast(0); CHECK(m.player(0).meter() == 0); CHECK(filled_rows(m.player(1).board()) == 2); // opponent's field became a copy of ours } void test_cast_freeze_cap_shortens_the_pause_only() { // Compact casts: the freeze is capped; the power applies when it ends, with the same result as the original. auto run = [](int cap) { MatchConfig cfg = test_config(); cfg.power_suspension = true; cfg.cast_freeze_cap = cap; auto m = std::make_unique(cfg, std::vector{chr("Aladdin"), chr("Mirurun")}); m->step(kIdle); ops::fill_bottom_holes(m->player(0).board(), 2); m->player(0).set_meter(3); // Fax m->cast(0); return m; }; auto full = run(0), capped = run(30); CHECK(full->frozen() > 30 && capped->frozen() == 30); CHECK(capped->cast_suspend() == full->cast_suspend()); CHECK(filled_rows(capped->player(1).board()) == 0); // not applied during the freeze while (full->frozen() > 0) full->step(kIdle); while (capped->frozen() > 0) capped->step(kIdle); CHECK(filled_rows(capped->player(1).board()) == 2); const Board &a = capped->player(1).board(), &b = full->player(1).board(); bool same = true; for (int y = 0; y < a.height(); ++y) for (int x = 0; x < a.width(); ++x) same = same && a.at(x, y) == b.at(x, y); CHECK(same); } void test_cast_with_empty_meter_does_nothing() { Match m(test_config(), {chr("Queen"), chr("Mirurun")}); m.step(kIdle); ops::fill_bottom_holes(m.player(0).board(), 3); m.cast(0); // meter 0: nothing CHECK(filled_rows(m.player(0).board()) == 3); } void test_change_swaps_fields() { Match m(test_config(), {chr("Dragon"), chr("Mirurun")}); m.step(kIdle); ops::fill_bottom_holes(m.player(0).board(), 1); m.player(0).set_meter(4); // Dragon level 4 = Change m.cast(0); CHECK(filled_rows(m.player(0).board()) == 0); CHECK(filled_rows(m.player(1).board()) == 1); } void test_shield_blocks_garbage() { Match m(test_config(), {chr("Halloween"), chr("Aladdin")}); m.step(kIdle); m.player(1).set_meter(2); // Aladdin level 2 = Shield (status) m.cast(1); CHECK(m.active_power(1) && m.active_power(1)->id == PowerId::Shield); m.player(0).set_attack_stock(4); // Drive until P2 starts its next cycle. for (int f = 0; f < 3000 && m.player(0).attack_stock() > 0; ++f) m.step(kIdle); CHECK(m.player(0).attack_stock() == 0); CHECK(cells_with(m.player(1).board(), cell::kBlock) <= 4 * 2); // at most its own locked pieces, no garbage rows } void test_doubles_doubles_attack() { Match m(test_config(), {chr("Queen"), chr("Mirurun")}); m.queue().set_override(PieceType::I); // nothing generated yet: every piece is a flat I at cols 3-6 m.step(kIdle); m.player(0).set_meter(3); // Queen level 3 = Doubles m.cast(0); CHECK(m.active_power(0) && m.active_power(0)->id == PowerId::Doubles); Board& b = m.player(0).board(); for (int x = 0; x < b.width(); ++x) if (x < 3 || x > 6) b.set(x, b.height() - 1, cell::kBlock); for (int f = 0; f < 3000 && m.player(0).total_lines() == 0; ++f) m.step(kIdle); CHECK(m.player(0).total_lines() == 1); CHECK(m.player(0).attack_stock() == 2); } void test_reverse_swaps_victim_controls() { Match m(test_config(), {chr("Queen"), chr("Mirurun")}); m.step(kIdle); m.step(kIdle); m.player(0).set_meter(2); // Queen level 2 = Reverse m.cast(0); wait_spawn(m, 1); const int x0 = m.player(1).active()->x; std::array in{}; in[1].left = true; m.step(in); CHECK(m.player(1).active()->x == x0 + 1); // pressing left moved right } void test_fever_floods_queue_with_casters_piece() { Match m(test_config(), {chr("Bit"), chr("Mirurun")}); m.step(kIdle); // both players spawn const PieceType mine = m.player(0).active()->type; m.player(0).set_meter(2); // Bit level 2 = Fever m.cast(0); CHECK(m.active_power(0) && m.active_power(0)->id == PowerId::Fever); for (int k = 0; k < 3 + 7; ++k) CHECK(m.queue().pop(0).type == mine); // 3 previews + 7 generated m.step(kIdle); CHECK(!m.active_power(0)); // ended at the 8th generation } void test_stone_rows_crack_instead_of_clearing() { Board b; const int bottom = b.height() - 1; for (int x = 0; x < b.width(); ++x) b.set(x, bottom, cell::kStone); // all stone: 2 lines, removed for (int x = 0; x < b.width(); ++x) b.set(x, bottom - 1, x == 3 ? cell::kStone : cell::kBlock); b.set(5, bottom - 2, cell::kBlock); const ClearResult r = b.clear_full_rows(); CHECK(r.rows == 3); // The mixed row stayed (now bottom) with only its cracked stone; the loose block sits above it. CHECK(b.at(3, bottom) == cell::kCracked); CHECK(b.at(0, bottom) == cell::kEmpty); CHECK(b.at(5, bottom - 1) == cell::kBlock); } void test_meter_caps_at_level4_cost() { Match m(test_config(), {chr("Halloween"), chr("Mirurun")}); m.player(0).set_meter(15); CHECK(m.player(0).meter() == 4); } void drop_pieces(Match& m, int player, int pieces) { std::array in{}; for (int f = 0; f < 20000 && pieces > 0; ++f) { in[static_cast(player)].hard_drop = f % 2 == 0; const bool had = m.player(player).active().has_value(); m.step(in); if (had && !m.player(player).active()) --pieces; } } MatchConfig hard_drop_config() { MatchConfig cfg = test_config(); cfg.timing.allow_hard_drop = true; return cfg; } void test_victim_counted_duration() { Match m(hard_drop_config(), {chr("Queen"), chr("Mirurun")}); m.step(kIdle); m.player(0).set_meter(2); // Queen level 2 = Reverse: counter 5 = the 4 new victim pieces after the cast m.cast(0); CHECK(m.targeted_by(1, PowerId::Reverse)); drop_pieces(m, 1, 4); // the piece in play was discarded by the cast: 4 new ones CHECK(m.targeted_by(1, PowerId::Reverse)); drop_pieces(m, 1, 1); // the 5th new piece enters play m.step(kIdle); CHECK(!m.targeted_by(1, PowerId::Reverse)); } // State 13 (81:AC26) and the cut-in's end (rel ~100): casting throws away the caster's falling piece at once and // everyone else's when the cut-in ends; Mirror (no cut-in) leaves the opponent's piece alone. (re/runs/pw2_disc_*) void test_cast_discards_pieces() { MatchConfig cfg; // real timings: a discarded piece leaves the field empty for the queue animation cfg.seed = 7; { Match m(cfg, {chr("Queen"), chr("Mirurun")}); wait_spawn(m, 0); wait_spawn(m, 1); const int lines0 = filled_rows(m.player(1).board()); m.player(0).set_meter(2); // Reverse (suspension 38 < 100: discards when it ends) m.cast(0); CHECK(!m.player(0).active()); CHECK(m.player(1).active()); for (int f = 0; f < 38; ++f) m.step(kIdle); CHECK(!m.player(1).active()); CHECK(filled_rows(m.player(1).board()) == lines0); // discarded, not locked } { Match m(cfg, {chr("Mirurun"), chr("Princess")}); wait_spawn(m, 0); wait_spawn(m, 1); m.player(1).set_meter(2); // Mirror: no cut-in m.cast(1); CHECK(!m.player(1).active()); m.step(kIdle); CHECK(m.player(0).active()); } } void prepare_clear_under_piece(Player& p); // keep_pieces_on_cast (a queue each): the caster's piece still goes, the opponents' survive the cast and its cut-in. void test_cast_keeps_pieces_with_own_queues() { MatchConfig cfg; cfg.seed = 7; cfg.keep_pieces_on_cast = true; Match m(cfg, {chr("Queen"), chr("Mirurun")}); wait_spawn(m, 0); wait_spawn(m, 1); const PieceType t1 = m.player(1).active()->type; m.player(0).set_meter(2); // Reverse m.cast(0); CHECK(!m.player(0).active()); for (int f = 0; f < 38; ++f) m.step(kIdle); CHECK(m.player(1).active() && m.player(1).active()->type == t1); CHECK(m.targeted_by(1, PowerId::Reverse)); } // buffer_casts: a cast pressed while someone's rows flash goes off once the flash is over (the original drops it). void test_cast_buffered_through_line_flash() { for (bool buffer : {false, true}) { MatchConfig cfg; cfg.seed = 7; cfg.buffer_casts = buffer; Match m(cfg, {chr("Queen"), chr("Mirurun")}); wait_spawn(m, 0); wait_spawn(m, 1); prepare_clear_under_piece(m.player(1)); for (int f = 0; f < 3000 && !m.player(1).clearing(); ++f) m.step(kIdle); CHECK(m.player(1).clearing()); m.player(0).set_meter(2); m.cast(0); // refused: P2's rows are flashing CHECK(!m.targeted_by(1, PowerId::Reverse)); for (int f = 0; f < 200; ++f) m.step(kIdle); CHECK(m.targeted_by(1, PowerId::Reverse) == buffer); } } // Remocon (82:B02E): the victim's pad is replaced by the caster's (the victim's own buttons do nothing) while the // caster's pad still drives the caster's own piece; it lasts the victim's next 2 pieces (counter 3, the piece in // play being discarded by the cast). Emulator: re/runs/pw2_rem_* (2P mode, port-2 input via setInput(t, 1, 1)). void test_remocon_takes_over_victim() { Match m(hard_drop_config(), {chr("Aladdin"), chr("Mirurun")}); m.step(kIdle); m.player(0).set_meter(4); // Aladdin level 4 = Remocon m.cast(0); CHECK(m.targeted_by(1, PowerId::Remocon)); wait_spawn(m, 0); wait_spawn(m, 1); const int x0 = m.player(0).active()->x, x1 = m.player(1).active()->x; std::array in{}; in[1].right = true; // the victim's own pad: ignored m.step(in); CHECK(m.player(1).active()->x == x1); in = {}; in[0].left = true; // the caster's pad moves both pieces m.step(in); CHECK(m.player(0).active()->x == x0 - 1); CHECK(m.player(1).active()->x == x1 - 1); in = {}; for (int piece = 0; piece < 2; ++piece) { // two victim pieces, dropped by the caster's hard drop CHECK(m.targeted_by(1, PowerId::Remocon)); for (int f = 0; f < 4000 && m.player(1).active(); ++f) { in[0].hard_drop = f % 2 == 0; m.step(in); } wait_spawn(m, 1); } CHECK(!m.targeted_by(1, PowerId::Remocon)); // the 3rd victim piece entering play ends it } // Make the falling piece an O and fill every cell of the rows it will land in except its own, so locking it clears // them (an O drops straight into its gap; other shapes could catch on the overhang). void prepare_clear_under_piece(Player& p) { p.replace_active(PieceType::O, false); const auto g = p.ghost(); Board& b = p.board(); for (const Offset& o : g->shape()) { const int y = g->y + o.y; for (int x = 0; x < b.width(); ++x) b.set(x, y, cell::kBlock); } for (const Offset& o : g->shape()) b.set(g->x + o.x, g->y + o.y, cell::kEmpty); } int piece_rows(const Piece& g) { int lo = 99, hi = -1; for (const Offset& o : g.shape()) lo = std::min(lo, g.y + o.y), hi = std::max(hi, g.y + o.y); return hi - lo + 1; } // Gang (81:97CD): the victim's crystals vanish in place and fill the caster's meter (re/runs/pw2_all_16: 2 -> +2). void test_gang_steals_crystals_into_meter() { Match m(test_config(), {chr("Bit"), chr("Mirurun")}); m.step(kIdle); Board& v = m.player(1).board(); v.set(0, v.height() - 1, cell::kCrystal); v.set(3, v.height() - 1, cell::kCrystal); v.set(5, v.height() - 2, cell::kBlock); m.player(0).set_meter(3); // Bit level 3 = Gang m.cast(0); CHECK(v.at(0, v.height() - 1) == cell::kEmpty && v.at(3, v.height() - 1) == cell::kEmpty); CHECK(v.at(5, v.height() - 2) == cell::kBlock); // no collapse CHECK(m.player(0).meter() == 2); } // Vampire: the victim's whole meter goes to the caster, capped at the caster's level-4 cost (re/runs/pw2_vamp3, // pw2_vamp5: victim 3 -> 0, caster +3; victim 5 -> 0, caster 4). void test_vampire_takes_every_crystal() { for (const auto [have, gets] : {std::pair{3, 3}, std::pair{4, 4}, std::pair{0, 0}}) { Match m(test_config(), {chr("Halloween"), chr("Mirurun")}); m.step(kIdle); m.player(1).set_meter(have); m.player(0).set_meter(3); // Halloween level 3 = Vampire m.cast(0); CHECK(m.player(1).meter() == 0); CHECK(m.player(0).meter() == gets); } } // Fly fills the bottom 3 rows but keeps stone (crystals become blocks); HyperFly fills the bottom 4 and turns // stone into blocks but keeps crystals (re/runs/pw2_flys, pw2_hfly). void test_fly_and_hyperfly_cells() { for (const char* who : {"Halloween", "Queen"}) { Match m(test_config(), {chr(who), chr("Mirurun")}); m.step(kIdle); Board& b = m.player(0).board(); const int y = b.height() - 1; b.set(1, y, cell::kStone); b.set(3, y, cell::kCrystal); b.set(5, y, cell::kBlock); m.player(0).set_meter(1); // level 1: Fly / HyperFly m.cast(0); const bool hyper = std::string_view(who) == "Queen"; CHECK(b.at(0, y) == cell::kBlock && b.at(5, y) == cell::kBlock); CHECK(b.at(1, y) == (hyper ? cell::kBlock : cell::kStone)); CHECK(b.at(3, y) == (hyper ? cell::kCrystal : cell::kBlock)); } } // Stardust: strikes land one by one during the cut-in, each deleting the row above its column's top block; the // timed run ends on the same board as all strikes at once. A flat stack is untouched, a ragged one loses rows. void test_stardust_strikes_over_time() { for (const bool ragged : {false, true}) { std::vector finals; for (const bool timed : {false, true}) { MatchConfig cfg = test_config(); cfg.power_suspension = timed; Match m(cfg, {chr("Bit"), chr("Mirurun")}); m.step(kIdle); Board& b = m.player(0).board(); for (int y = b.height() - 4; y < b.height(); ++y) for (int x = 0; x < b.width(); ++x) b.set(x, y, cell::kBlock); if (ragged) for (int x = 0; x < b.width(); x += 2) for (int y = b.height() - 5 - x / 2; y < b.height() - 4; ++y) b.set(x, y, cell::kBlock); const Board before = b; m.player(0).set_meter(1); // Bit level 1 = Stardust m.cast(0); if (timed) { CHECK(m.player(0).board() == before); // nothing yet: the stars are still to come bool seen = false; for (int f = 0; f < 600; ++f) { seen |= !m.falling_stars().empty(); m.step(kIdle); } CHECK(seen && m.falling_stars().empty()); } finals.push_back(m.player(0).board()); CHECK((m.player(0).board() == before) == !ragged); } CHECK(finals[0] == finals[1]); } } // Seesaw (re/runs/anim/s0B): the victim's field rises 2 at rel 175, the caster's bottom 2 rows lift off at 236, // its field drops at 277 and the rows fill the victim's gap at 293; the end board equals the all-at-once version, // also in compact casts (the steps start as play resumes). void test_seesaw_moves_over_time() { std::vector> finals; for (const int mode : {0, 1, 2}) { // instant, original suspension, compact MatchConfig cfg = test_config(); cfg.power_suspension = mode > 0; if (mode == 2) cfg.cast_freeze_cap = 30; Match m(cfg, {chr("Aladdin"), chr("Mirurun")}); m.step(kIdle); Board& b = m.player(0).board(); Board& o = m.player(1).board(); const int h = b.height(); for (int x = 0; x < b.width(); ++x) b.set(x, h - 3, cell::kBlock); b.set(0, h - 1, cell::kBlock); b.set(9, h - 2, cell::kCrystal); for (int x = 1; x < o.width(); ++x) o.set(x, h - 1, cell::kBlock); const Board own0 = b, opp0 = o; m.player(0).set_meter(1); // Aladdin level 1 = Seesaw m.cast(0); if (mode > 0) { CHECK(b == own0 && o == opp0); const int shift = mode == 2 ? 105 - 30 : 0; // compact: the kids appear as play resumes (rel 30) for (int rel = 1; rel <= 342 - shift; ++rel) { m.step(kIdle); const int t = rel + shift; if (t == 200) CHECK(o.row_empty(h - 1) && o.row_empty(h - 2) && o.at(1, h - 3) == cell::kBlock && b == own0); if (t == 250) CHECK(b.row_empty(h - 1) && b.row_empty(h - 2) && !b.row_empty(h - 3) && m.seesaw()->rows.size() == 2); if (t == 285) CHECK(!b.row_empty(h - 1) && b.row_empty(h - 2) && o.row_empty(h - 1)); } CHECK(!m.seesaw()); } CHECK(b.at(0, h - 1) == cell::kBlock && b.row_empty(h - 2)); CHECK(o.at(0, h - 1) == cell::kBlock && o.at(9, h - 2) == cell::kCrystal && o.at(1, h - 3) == cell::kBlock); finals.emplace_back(b, o); } CHECK(finals[0] == finals[1] && finals[0] == finals[2]); } // Sol (82:C33A): Left/Right aim the 3-column beam during the 3-second countdown (from rel 103), A fires early. void test_sol_is_aimed() { MatchConfig cfg = test_config(); cfg.power_suspension = true; Match m(cfg, {chr("Princess"), chr("Mirurun")}); m.step(kIdle); Board& b = m.player(0).board(); for (int y = b.height() - 6; y < b.height(); ++y) for (int x = 0; x < b.width() - 1; ++x) b.set(x, y, cell::kBlock); m.player(0).set_meter(1); // Princess level 1 = Sol m.cast(0); std::array in{}; int f = 0; auto press = [&](bool Input::*button) { in[0].*button = true; m.step(in), ++f; in = {}; m.step(in), ++f; }; while (f < 110) m.step(kIdle), ++f; press(&Input::left); press(&Input::left); // column 4 -> 2 const int before = m.frozen(); press(&Input::rotate_cw); // A: fire now CHECK(m.frozen() < before - 100); while (m.frozen() > 0) m.step(kIdle); for (int x = 0; x < 9; ++x) CHECK((b.at(x, b.height() - 1) == cell::kEmpty) == (x >= 2 && x <= 4)); { // compact casts (30-frame freeze): the countdown follows the short freeze MatchConfig cc = cfg; cc.cast_freeze_cap = 30; Match k(cc, {chr("Princess"), chr("Mirurun")}); k.step(kIdle); Board& kb = k.player(0).board(); for (int y = kb.height() - 6; y < kb.height(); ++y) for (int x = 0; x < kb.width() - 1; ++x) kb.set(x, y, cell::kBlock); k.player(0).set_meter(1); k.cast(0); for (int g = 0; g < 32; ++g) k.step(kIdle); CHECK(k.frozen() > 100); // still aiming std::array ki{}; for (bool Input::*button : {&Input::right, &Input::right, &Input::rotate_cw}) { ki[0].*button = true; k.step(ki); ki = {}; k.step(ki); } while (k.frozen() > 0) k.step(kIdle); for (int x = 0; x < 9; ++x) CHECK((kb.at(x, kb.height() - 1) == cell::kEmpty) == (x >= 6 && x <= 8)); } } // Dainashi (Ruin): the victim's cleared rows empty in place and nothing above drops (re/runs/pw2_dai_y). void test_dainashi_rows_do_not_collapse() { Match m(hard_drop_config(), {chr("Ninja"), chr("Mirurun")}); m.step(kIdle); m.player(0).set_meter(3); // Ninja level 3 = Dainashi m.cast(0); wait_spawn(m, 1); Player& v = m.player(1); prepare_clear_under_piece(v); const Piece g = *v.ghost(); // an O resting in its gap: its rows are the ones that clear int top = 99; // the highest row that will clear; a block rests just above it for (const Offset& o : g.shape()) top = std::min(top, g.y + o.y); v.board().set(0, top - 1, cell::kBlock); std::array in{}; in[1].hard_drop = true; m.step(in); // instant timings: the clear is applied at once (more idle steps would lock more pieces) const Board& s = v.settled_board(); CHECK(s.at(0, top - 1) == cell::kBlock); // stayed: no collapse for (int y = top; y < top + piece_rows(g); ++y) CHECK(s.row_empty(y)); } // Doton (Thwack): the caster's cleared rows empty in place, then loose blocks fall (crystals and stone stay put) // and rows that completes clear too (re/runs/pw2_dot_y: a chained second clear; pw2_dot2: crystal/stone fixed). void test_doton_drops_loose_blocks() { Match m(hard_drop_config(), {chr("Wolfman"), chr("Mirurun")}); m.step(kIdle); m.player(0).set_meter(4); // Wolfman level 4 = Doton m.cast(0); wait_spawn(m, 0); Player& p = m.player(0); Board& b = p.board(); const int h = b.height(); // Raise the field: bottom row full except column 0 (completed by the drop), then the piece's clear rows above. for (int x = 1; x < b.width(); ++x) b.set(x, h - 1, cell::kBlock); for (int y = h - 5; y < h - 1; ++y) for (int x = 0; x < b.width(); ++x) b.set(x, y, cell::kBlock); // a floor the piece lands on for (int y = h - 5; y < h - 1; ++y) b.set(0, y, cell::kEmpty); // column 0 open down to the bottom hole b.set(0, 5, cell::kBlock); // loose: falls all the way, completing the bottom row b.set(9, 3, cell::kCrystal); // stays where it is prepare_clear_under_piece(p); const int lines0 = p.total_lines(); std::array in{}; in[0].hard_drop = true; m.step(in); // instant timings: the clear is applied at once const Board& s = p.settled_board(); CHECK(s.at(9, 3) == cell::kCrystal); CHECK(s.at(0, 5) == cell::kEmpty); CHECK(s.at(0, h - 1) == cell::kEmpty); // the chained bottom row cleared and the floor above dropped into it CHECK(p.total_lines() == lines0 + 2 + 1); // the O's 2 rows + the chained bottom row } // Moses (86:DD6F): after the parting, loose blocks fall; crystals and stone stay where they are (re/runs/pw2_mos). void test_moses_keeps_crystals_and_stone() { Match m(test_config(), {chr("Shaman"), chr("Mirurun")}); m.step(kIdle); Board& b = m.player(0).board(); const int y = b.height() - 7; b.set(2, y, cell::kCrystal); // -> column 0 b.set(4, y, cell::kBlock); // -> column 2, falls b.set(6, y, cell::kStone); // -> column 8 m.player(0).set_meter(1); // Shaman level 1 = Moses m.cast(0); CHECK(b.at(0, y) == cell::kCrystal); CHECK(b.at(8, y) == cell::kStone); CHECK(b.at(2, y) == cell::kEmpty && b.at(2, b.height() - 1) != cell::kEmpty); } // 81:D442: a push-up moves only the visible rows; blocks pushed out of the top visible row vanish (the hidden rows // keep whatever they had), so a tall stack under garbage doesn't spill into the vanishing zone. void test_push_up_vanishes_into_hidden_rows() { Match m(test_config(), {chr("Mirurun"), chr("Halloween")}); m.step(kIdle); Board& v = m.player(1).board(); const int top = v.hidden_rows(); for (int y = top; y < top + 5; ++y) v.set(3, y, cell::kBlock); // a column reaching the top visible row m.player(0).set_meter(3); // Mirurun level 3 = Pentris: 5 garbage rows m.cast(0); for (int y = 0; y < top; ++y) CHECK(v.row_empty(y)); } // Kaiten (re/runs/pw2_kai_*): Down is no soft drop; every 3 frames it rotates the piece and each such rotation // drops it one row (a normal soft drop would be 1 row per frame). void test_kaiten_down_rotates_and_steps() { Match m(test_config(), {chr("Shaman"), chr("Mirurun")}); m.step(kIdle); m.player(0).set_meter(2); // Shaman level 2 = Kaiten m.cast(0); wait_spawn(m, 1); m.player(1).replace_active(PieceType::T, false); const int y0 = m.player(1).active()->y; std::array in{}; in[1].soft_drop = true; for (int f = 0; f < 30 && m.player(1).active(); ++f) m.step(in); const int dy = m.player(1).active()->y - y0; CHECK(dy >= 8 && dy <= 11); } void test_mirror_reflects_and_cancels() { { Match m(test_config(), {chr("Mirurun"), chr("Princess")}); m.step(kIdle); m.player(1).set_meter(2); // Princess level 2 = Mirror (silent, caster-counted) m.cast(1); CHECK(m.active_power(1) && m.active_power(1)->id == PowerId::Mirror); m.player(0).set_meter(3); // Mirurun level 3 = Pentris, bounced back m.cast(0); CHECK(filled_rows(m.player(0).board()) == 5); CHECK(filled_rows(m.player(1).board()) == 0); CHECK(!m.active_power(1)); } { Match m(test_config(), {chr("Princess"), chr("Princess")}); m.step(kIdle); m.player(1).set_meter(2); m.cast(1); m.player(0).set_meter(2); m.cast(0); // Mirror vs Mirror: both gone CHECK(!m.active_power(0) && !m.active_power(1)); } } void test_select_changes_own_piece() { Match m(test_config(), {chr("Dragon"), chr("Mirurun")}); m.step(kIdle); m.player(0).set_meter(2); // Dragon level 2 = Select m.cast(0); wait_spawn(m, 0); wait_spawn(m, 1); const PieceType before = m.player(0).active()->type; const PieceType opp_before = m.player(1).active()->type; std::array in{}; in[0].l = true; m.step(in); CHECK(static_cast(m.player(0).active()->type) == (static_cast(before) + 1) % kPieceTypeCount); CHECK(m.player(1).active()->type == opp_before); } void test_board_rules() { auto fresh = [](std::string_view who) { auto m = std::make_unique(test_config(), std::vector{chr(who), chr("Mirurun")}); return m; }; { // Cut empties 4 rows from the top of the stack, in place auto m = fresh("Wolfman"); Board& b = m->player(0).board(); ops::fill_bottom_holes(b, 6); m->player(0).set_meter(1); m->cast(0); CHECK(filled_rows(b) == 2); CHECK(!b.row_empty(b.height() - 1) && b.row_empty(b.height() - 3)); } { // Sol empties columns 4-6 over the full height auto m = fresh("Princess"); Board& b = m->player(0).board(); ops::fill_bottom_holes(b, 5); m->player(0).set_meter(1); m->cast(0); CHECK(b.at(4, b.height() - 1) == cell::kEmpty && b.at(6, b.height() - 5) == cell::kEmpty); CHECK(b.at(3, b.height() - 1) == cell::kBlock && b.at(7, b.height() - 1) == cell::kBlock); } { // Seesaw: own bottom 2 rows become the opponent's bottom 2, order kept auto m = fresh("Aladdin"); Board& b = m->player(0).board(); b.set(0, b.height() - 1, cell::kBlock); // marker in the bottom row b.set(9, b.height() - 2, cell::kBlock); // marker in the row above m->player(0).set_meter(1); m->cast(0); const Board& o = m->player(1).board(); CHECK(o.at(0, o.height() - 1) == cell::kBlock && o.at(9, o.height() - 2) == cell::kBlock); CHECK(filled_rows(b) == 0); } { // Press: bottom 3 rows go, the rest squeezes toward the centre auto m = fresh("Dragon"); Board& b = m->player(0).board(); ops::fill_bottom_holes(b, 3); b.set(0, b.height() - 4, cell::kBlock); b.set(9, b.height() - 4, cell::kCrystal); m->player(0).set_meter(1); m->cast(0); CHECK(b.at(4, b.height() - 1) == cell::kBlock && b.at(5, b.height() - 1) == cell::kCrystal); CHECK(filled_rows(b) == 1); } { // Suritote inverts the victim from its top row down, coins kept auto m = fresh("Mirurun"); Board& o = m->player(1).board(); o.set(2, o.height() - 2, cell::kBlock); o.set(3, o.height() - 1, cell::kCrystal); m->player(0).set_meter(4); m->cast(0); CHECK(o.at(2, o.height() - 2) == cell::kEmpty && o.at(0, o.height() - 2) == cell::kBlock); CHECK(o.at(3, o.height() - 1) == cell::kCrystal && o.at(0, o.height() - 1) == cell::kBlock); CHECK(o.row_empty(o.height() - 3)); } } void test_best_of_three() { Match m(test_config(), {chr("Halloween"), chr("Mirurun")}); for (int round = 0; round < 2; ++round) { // Fill P2's field to the top except one column so its next lock tops out. Board& b = m.player(1).board(); for (int y = 2; y < b.height(); ++y) for (int x = 1; x < b.width(); ++x) b.set(x, y, cell::kBlock); const int before = m.wins(0); for (int f = 0; f < 5000 && m.wins(0) == before; ++f) m.step(kIdle); CHECK(m.wins(0) == before + 1); } CHECK(m.match_winner() && *m.match_winner() == 0); } void test_simple_cpu_beats_idle_player() { Match m(test_config(), {chr("Halloween"), chr("Mirurun")}); SimpleCpu cpu(3); std::array in{}; for (int f = 0; f < 200000 && !m.match_winner(); ++f) { in[0] = cpu.next(m, 0); m.step(in); } CHECK(m.match_winner() && *m.match_winner() == 0); CHECK(m.player(0).total_lines() > 0); } // Needs the user's ROM: TBG_ROM=rom/tbg_jp.sfc ./build/tbg_tests void test_faithful_cpu_beats_idle_player() { const char* path = std::getenv("TBG_ROM"); if (!path) return; std::ifstream f(path, std::ios::binary); const std::vector rom{std::istreambuf_iterator(f), {}}; CHECK(FaithfulCpu::rom_supported(rom)); if (!FaithfulCpu::rom_supported(rom)) return; for (int difficulty = 0; difficulty < 4; ++difficulty) { MatchConfig cfg; // real timings: the COM relies on its entry window and the cycle delays cfg.seed = 11; Match m(cfg, {chr("Halloween"), chr("Mirurun")}); FaithfulCpu cpu(rom, difficulty, 5); cpu.attach(m, 1); std::array in{}; for (int fr = 0; fr < 300000 && !m.match_winner(); ++fr) { in[1] = cpu.next(m, 1); m.step(in); } CHECK(m.match_winner() && *m.match_winner() == 1); CHECK(m.player(1).total_lines() > 0); } } void test_campaign_order() { Campaign c = Campaign::for_roster(Roster::original(), 2); // Shaman: 0,1,3..7, then Dragon (8), Queen (9) const int expected[] = {0, 1, 3, 4, 5, 6, 7, 8, 9}; for (int opp : expected) { CHECK(c.opponent() == opp); CHECK(!c.finished()); if (opp == 4) CHECK(c.record_continue() == 1); // a loss + continue keeps the same opponent CHECK(c.opponent() == opp); c.record_win(); } CHECK(c.finished()); CHECK(c.continues(4) == 1); } void test_level_up_by_time_and_cost_base() { MatchConfig cfg = test_config(); cfg.start_levels = {2, 0}; cfg.cost_bases = {2, 1}; Match m(cfg, {chr("Halloween"), chr("Mirurun")}); CHECK(m.player(0).speed_level() == 2 && m.player(1).speed_level() == 0); CHECK(m.level_costs(0)[3] == 8); m.player(0).set_meter(7); CHECK(m.affordable_level(0) == 2); // costs 2/4/6/8 m.player(0).set_meter(9); CHECK(m.player(0).meter() == 8); // capped at the level-4 cost for (int f = 0; f < 3 * 3600; ++f) { // keep both alive so the round (and its clock) continues ops::clear_all(m.player(0).board()); ops::clear_all(m.player(1).board()); m.step(kIdle); } CHECK(m.round() == 1); CHECK(m.player(0).speed_level() == 3 && m.player(1).speed_level() == 1); } void test_four_player_match() { for (Targeting t : {Targeting::Next, Targeting::Random, Targeting::HighestStack, Targeting::LowestStack}) { MatchConfig cfg = test_config(); cfg.targeting = t; Match m(cfg, {chr("Halloween"), chr("Mirurun"), chr("Shaman"), chr("Aladdin")}); std::array cpus{SimpleCpu(3), SimpleCpu(3), SimpleCpu(3), SimpleCpu(3)}; std::array in{}; std::array garbage_seen{}; for (int f = 0; f < 400000 && !m.match_winner(); ++f) { for (int i = 0; i < 4; ++i) { in[static_cast(i)] = cpus[static_cast(i)].next(m, i); const int tgt = m.target_of(i); CHECK(tgt != i); if (m.player(i).attack_stock() > 0) ++garbage_seen[static_cast(tgt)]; } m.step(in); } CHECK(m.match_winner().has_value()); int targeted = 0; for (int g : garbage_seen) targeted += g > 0; CHECK(targeted >= 2); // attacks reached more than one opponent } } void test_roster_parsing() { std::string err; const auto r = parse_roster(R"( [rules] crystal_interval = 4 level_costs = [2, 4, 6, 8] [[power]] name = "Pentris" amount = 6 [[power]] name = "Mega Mirurun" effect = "Mirurun" amount = 6 [[character]] name = "Witch" powers = ["Mega Mirurun", "Reverse", "Pentris", "Bomb"] # comment color = "#40c0a0" [[character]] name = "Demon" powers = ["Clear", "Clear", "Clear", "Clear"] boss = true )", Roster::original(), &err); CHECK(r.has_value()); if (!r) { std::printf("parse error: %s\n", err.c_str()); return; } CHECK(r->rules.crystal_interval == 4 && r->rules.level_costs[3] == 8); CHECK(r->powers[r->powers.find("Pentris")].amount == 6); const int w = r->find("witch"); CHECK(w == 10); CHECK(r->power(w, 0).effect == PowerId::Mirurun && r->power(w, 0).amount == 6); CHECK(r->characters[static_cast(w)].color == 0x40c0a0); // The new boss comes last in 1P: Halloween fights 1..7, Dragon, Queen, Demon (Witch is a regular). const Campaign c = Campaign::for_roster(*r, 0); CHECK(c.order().back() == r->find("Demon")); CHECK(c.order()[7] == w); // The retuned power plays with its new number. MatchConfig cfg = test_config(); cfg.powers = &r->powers; Match m(cfg, {&r->characters[static_cast(w)], chr("Halloween")}); m.step(kIdle); m.player(0).set_meter(3); // level 3 = retuned Pentris m.cast(0); CHECK(filled_rows(m.player(1).board()) == 6); CHECK(!parse_roster("[[character]]\nname = \"X\"\npowers = [\"Fly\", \"Nope\", \"Fly\", \"Fly\"]\n", Roster::original(), &err)); CHECK(err.find("line 3") != std::string::npos); CHECK(!parse_roster("key = 1\n", Roster::original(), &err)); CHECK(!parse_roster("[roster]\nreplace = true\n", Roster::original(), &err)); // nothing left CHECK(!parse_roster("[rules\n", Roster::original(), &err)); // TOML syntax error } void test_events() { Match m(hard_drop_config(), {chr("Halloween"), chr("Mirurun")}); bool spawn = false, lock = false, clear = false, garbage = false, ko = false, round = false; Board& b = m.player(0).board(); for (int x = 0; x < b.width(); ++x) if (x < 3 || x > 6) b.set(x, b.height() - 1, cell::kBlock); m.queue().set_override(PieceType::I); std::array in{}; for (int f = 0; f < 20000 && !round; ++f) { in[0].hard_drop = in[1].hard_drop = f % 2 == 0; m.step(in); for (const MatchEvent& e : m.events()) { spawn |= e.type == MatchEvent::Type::Spawn; lock |= e.type == MatchEvent::Type::Lock; clear |= e.type == MatchEvent::Type::Clear && e.player == 0 && e.value == 1; garbage |= e.type == MatchEvent::Type::Garbage && e.player == 1; ko |= e.type == MatchEvent::Type::KO; round |= e.type == MatchEvent::Type::RoundEnd; } } CHECK(spawn && lock && clear && garbage && ko && round); } bool same_boards(const Match& a, const Match& b) { for (int i = 0; i < a.player_count(); ++i) for (int y = 0; y < a.player(i).board().height(); ++y) for (int x = 0; x < a.player(i).board().width(); ++x) if (a.player(i).board().at(x, y) != b.player(i).board().at(x, y)) return false; return true; } template void check_replay_roundtrip(MatchConfig cfg, std::vector names, MakeCpu make_cpu) { const Roster& roster = Roster::original(); std::vector defs; for (const auto& n : names) defs.push_back(chr(n)); Match m(cfg, defs); std::vector> cpus; for (int i = 0; i < m.player_count(); ++i) { cpus.push_back(make_cpu(i)); cpus.back()->attach(m, i); } ReplayRecorder rec; rec.begin(m, cfg, roster); std::vector in(static_cast(m.player_count())); for (int f = 0; f < 400000 && !m.match_winner(); ++f) { for (int i = 0; i < m.player_count(); ++i) in[static_cast(i)] = cpus[static_cast(i)]->next(m, i); m.step(in); rec.record(m, in); } CHECK(m.match_winner().has_value()); std::string err; const auto parsed = Replay::parse(rec.replay().serialize(), &err); CHECK(parsed.has_value()); if (!parsed) { std::printf("replay parse: %s\n", err.c_str()); return; } CHECK(parsed->config.cast_freeze_cap == cfg.cast_freeze_cap); auto again = parsed->make_match(roster, &err); CHECK(again != nullptr); CHECK(err.empty()); // same roster: no divergence warning if (!again) return; ReplayPlayer player(*parsed); while (!player.done()) { const auto inputs = player.next(*again); again->step(inputs); } CHECK(again->match_winner() == m.match_winner()); CHECK(same_boards(m, *again)); } void test_replay_roundtrip() { MatchConfig cfg = MatchConfig::instant(); cfg.seed = 99; cfg.targeting = Targeting::Random; check_replay_roundtrip(cfg, {"Halloween", "Bit", "Ninja", "Queen"}, [](int) { return std::make_unique(3); }); // Compact casts (capped suspension) are recorded and replay identically. MatchConfig compact = MatchConfig::instant(); compact.seed = 12; compact.power_suspension = true; compact.cast_freeze_cap = 30; check_replay_roundtrip(compact, {"Aladdin", "Bit"}, [](int) { return std::make_unique(3); }); if (const char* path = std::getenv("TBG_ROM")) { std::ifstream f(path, std::ios::binary); static const std::vector rom{std::istreambuf_iterator(f), {}}; MatchConfig real; // full timings, difficulty 1 exercises the per-piece soft-drop records real.seed = 4; check_replay_roundtrip(real, {"Shaman", "Dragon"}, [](int i) { return std::make_unique(rom, 1, 7u + static_cast(i)); }); } } void test_targeting_all() { MatchConfig cfg = test_config(); cfg.targeting = Targeting::All; cfg.timing.allow_hard_drop = true; Match m(cfg, {chr("Mirurun"), chr("Halloween"), chr("Shaman"), chr("Aladdin")}); std::array idle{}; m.player(0).set_attack_stock(2); m.step(idle); // every opponent's first cycle start delivers P1's 2 lines for (int v = 1; v < 4; ++v) CHECK(filled_rows(m.player(v).board()) == 2); CHECK(m.player(0).attack_stock() == 0); // Pentris (Mirurun level 3) pushes 5 rows under every opponent. m.player(0).set_meter(3); m.cast(0); for (int v = 1; v < 4; ++v) CHECK(filled_rows(m.player(v).board()) == 7); // A status power hits all three, each victim counting its own pieces. Match s(cfg, {chr("Queen"), chr("Halloween"), chr("Shaman"), chr("Aladdin")}); s.step(idle); s.player(0).set_meter(2); // Reverse: 5 victim pieces s.cast(0); for (int v = 1; v < 4; ++v) CHECK(s.targeted_by(v, PowerId::Reverse)); std::array in{}; int dropped = 0; for (int f = 0; f < 20000 && dropped < 6; ++f) { in[1].hard_drop = f % 2 == 0; // only seat 2 plays: its count runs out first const bool had = s.player(1).active().has_value(); s.step(in); if (had && !s.player(1).active()) ++dropped; } CHECK(!s.targeted_by(1, PowerId::Reverse)); CHECK(s.targeted_by(2, PowerId::Reverse) && s.targeted_by(3, PowerId::Reverse)); CHECK(s.active_power(0).has_value()); // Copy takes the lowest opponent field. Match c(cfg, {chr("Princess"), chr("Halloween"), chr("Shaman"), chr("Aladdin")}); c.step(idle); ops::fill_bottom_holes(c.player(1).board(), 6); ops::fill_bottom_holes(c.player(2).board(), 1); ops::fill_bottom_holes(c.player(3).board(), 3); c.player(0).set_meter(4); // Princess level 4 = Copy c.cast(0); CHECK(filled_rows(c.player(0).board()) == 1); } } // namespace void run_match_tests() { test_stocks_net_at_cycle_start(); test_big_clear_freezes_the_match(); test_garbage_arrives_at_victim_cycle_start(); test_cast_picks_highest_affordable_level_and_resets_meter(); test_cast_with_empty_meter_does_nothing(); test_change_swaps_fields(); test_cast_freeze_cap_shortens_the_pause_only(); test_shield_blocks_garbage(); test_doubles_doubles_attack(); test_reverse_swaps_victim_controls(); test_fever_floods_queue_with_casters_piece(); test_stone_rows_crack_instead_of_clearing(); test_meter_caps_at_level4_cost(); test_victim_counted_duration(); test_cast_discards_pieces(); test_cast_keeps_pieces_with_own_queues(); test_cast_buffered_through_line_flash(); test_kaiten_down_rotates_and_steps(); test_push_up_vanishes_into_hidden_rows(); test_gang_steals_crystals_into_meter(); test_vampire_takes_every_crystal(); test_fly_and_hyperfly_cells(); test_sol_is_aimed(); test_stardust_strikes_over_time(); test_seesaw_moves_over_time(); test_dainashi_rows_do_not_collapse(); test_doton_drops_loose_blocks(); test_moses_keeps_crystals_and_stone(); test_remocon_takes_over_victim(); test_mirror_reflects_and_cancels(); test_select_changes_own_piece(); test_board_rules(); test_campaign_order(); test_level_up_by_time_and_cost_base(); test_four_player_match(); test_roster_parsing(); test_events(); test_replay_roundtrip(); test_targeting_all(); test_best_of_three(); test_simple_cpu_beats_idle_player(); test_faithful_cpu_beats_idle_player(); }