323 lines
12 KiB
C++
323 lines
12 KiB
C++
// Modern ruleset tests: SRS + kicks, lock delay resets, hold, 7-bag, guideline attack, T-spins, cancelling.
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <functional>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
#include "check.hpp"
|
|
#include "core/match.hpp"
|
|
#include "core/player.hpp"
|
|
#include "core/queue.hpp"
|
|
|
|
namespace {
|
|
|
|
using namespace tbg;
|
|
|
|
bool has_cell(const Shape& s, int x, int y) {
|
|
return std::any_of(s.begin(), s.end(), [&](const Offset& o) { return o.x == x && o.y == y; });
|
|
}
|
|
|
|
Timing modern_timing() {
|
|
Timing t = Timing::modern(Timing::instant());
|
|
t.das_delay = 0;
|
|
t.das_repeat = 0; // instant auto-shift after the first press
|
|
return t;
|
|
}
|
|
|
|
PieceGenerator cycle(std::vector<PieceType> seq) {
|
|
return [seq, i = std::size_t{0}]() mutable { return QueuedPiece{seq[i++ % seq.size()], false}; };
|
|
}
|
|
|
|
// Step until a piece is in play (instant timing: the first step).
|
|
void spawn(Player& p) {
|
|
for (int k = 0; k < 4 && !p.active(); ++k) p.step({});
|
|
}
|
|
|
|
// Press then release a button (edge-triggered actions).
|
|
FrameResult tap(Player& p, bool Input::* button) {
|
|
Input in;
|
|
in.*button = true;
|
|
FrameResult r = p.step(in);
|
|
if (!r.locked) {
|
|
const FrameResult r2 = p.step({});
|
|
if (r2.locked) r = r2;
|
|
}
|
|
return r;
|
|
}
|
|
|
|
void test_srs_shapes_and_kicks() {
|
|
// T rotates about its centre: R points right, 2 down.
|
|
const PieceDef& t = piece_def(PieceType::T, true);
|
|
CHECK(has_cell(t.rotations[1], 2, 1) && has_cell(t.rotations[1], 1, 0) && has_cell(t.rotations[1], 1, 2));
|
|
CHECK(has_cell(t.rotations[2], 1, 2) && !has_cell(t.rotations[2], 1, 0));
|
|
// I: spawn row 1, R = column 2, 2 = row 2, L = column 1 (true SRS).
|
|
const PieceDef& i = piece_def(PieceType::I, true);
|
|
for (int k = 0; k < 4; ++k) {
|
|
CHECK(has_cell(i.rotations[0], k, 1));
|
|
CHECK(has_cell(i.rotations[1], 2, k));
|
|
CHECK(has_cell(i.rotations[2], k, 2));
|
|
CHECK(has_cell(i.rotations[3], 1, k));
|
|
}
|
|
// Kick tables (+y down here): JLSTZ 0->R = (0,0) (-1,0) (-1,-1) (0,2) (-1,2).
|
|
const auto k0r = srs_kicks(PieceType::T, 0, 1);
|
|
CHECK(k0r[1].x == -1 && k0r[1].y == 0);
|
|
CHECK(k0r[2].x == -1 && k0r[2].y == -1);
|
|
CHECK(k0r[4].x == -1 && k0r[4].y == 2);
|
|
// I R->2 = (0,0) (-1,0) (2,0) (-1,-2) (2,1).
|
|
const auto ir2 = srs_kicks(PieceType::I, 1, 2);
|
|
CHECK(ir2[2].x == 2 && ir2[2].y == 0);
|
|
CHECK(ir2[3].x == -1 && ir2[3].y == -2);
|
|
// O never kicks.
|
|
const auto ok = srs_kicks(PieceType::O, 0, 1);
|
|
CHECK(std::all_of(ok.begin(), ok.end(), [](const Offset& o) { return o.x == 0 && o.y == 0; }));
|
|
// O: the cells stay, the crystal walks clockwise TR -> BR -> BL -> TL.
|
|
const PieceDef& o = piece_def(PieceType::O, true);
|
|
const Offset want[4] = {{2, 0}, {2, 1}, {1, 1}, {1, 0}};
|
|
for (std::size_t r = 0; r < 4; ++r) {
|
|
const Offset c = o.rotations[r][o.crystal_cell[r]];
|
|
CHECK(c.x == want[r].x && c.y == want[r].y);
|
|
}
|
|
}
|
|
|
|
void test_srs_wall_kick() {
|
|
PieceQueue q(cycle({PieceType::I}), 1);
|
|
Player p(0, q, modern_timing());
|
|
spawn(p);
|
|
CHECK(p.active() && p.active()->srs && p.active()->x == 3);
|
|
tap(p, &Input::rotate_cw); // vertical: column box+2
|
|
CHECK(p.active()->rot == 1);
|
|
Input left;
|
|
left.left = true;
|
|
for (int k = 0; k < 3; ++k) p.step(left); // instant ARR: against the wall
|
|
p.step({});
|
|
CHECK(p.active()->x == -2);
|
|
// Flat would poke out of the wall: R->2 test 3 (2,0) puts it back in.
|
|
tap(p, &Input::rotate_cw);
|
|
CHECK(p.active()->rot == 2);
|
|
CHECK(p.active()->x == 0);
|
|
}
|
|
|
|
void test_classic_has_no_kicks() {
|
|
PieceQueue q(cycle({PieceType::I}), 1);
|
|
Timing t = Timing::instant();
|
|
Player p(0, q, t);
|
|
spawn(p);
|
|
CHECK(!p.active()->srs);
|
|
tap(p, &Input::rotate_cw); // ROM I: rot 1 = column box+2
|
|
const int rot = p.active()->rot;
|
|
Input left;
|
|
left.left = true;
|
|
for (int k = 0; k < 40 && p.active() && p.active()->x > -2; ++k) {
|
|
p.step(left);
|
|
p.step({});
|
|
}
|
|
if (p.active() && p.active()->x == -2) {
|
|
tap(p, &Input::rotate_cw);
|
|
CHECK(p.active()->rot == rot); // blocked, reverted
|
|
}
|
|
}
|
|
|
|
void test_lock_delay_and_resets() {
|
|
PieceQueue q(cycle({PieceType::O}), 1);
|
|
Player p(0, q, modern_timing());
|
|
spawn(p);
|
|
Input hd;
|
|
hd.soft_drop = true;
|
|
int frames = 0;
|
|
while (p.active() && p.active()->y < p.board().height() - 2 && frames < 200) p.step(hd), ++frames;
|
|
CHECK(p.active() && p.active()->y == p.board().height() - 2);
|
|
// Grounded: locks after modern_lock_delay frames without input.
|
|
int grounded = 0;
|
|
while (p.active() && grounded < 100) p.step({}), ++grounded;
|
|
CHECK(grounded >= 29 && grounded <= 31);
|
|
|
|
// Moves restart the delay, at most 15 times; then it locks as soon as it's grounded.
|
|
spawn(p);
|
|
while (p.active() && p.active()->y < p.board().height() - 4 && frames < 400) p.step(hd), ++frames;
|
|
int n = 0;
|
|
Input l, r;
|
|
l.left = true;
|
|
r.right = true;
|
|
while (p.active() && n < 1000) {
|
|
p.step(n % 4 == 0 ? l : n % 4 == 2 ? r : Input{});
|
|
++n;
|
|
}
|
|
CHECK(!p.active() || n < 1000);
|
|
CHECK(n >= 28 && n < 15 * 32);
|
|
}
|
|
|
|
void test_hard_drop_and_ghost() {
|
|
PieceQueue q(cycle({PieceType::T, PieceType::O}), 1);
|
|
Player p(0, q, modern_timing());
|
|
spawn(p);
|
|
const auto g = p.ghost();
|
|
CHECK(g && g->y == p.board().height() - 2); // T spawn state: 2 rows tall
|
|
const FrameResult r = tap(p, &Input::hard_drop);
|
|
CHECK(r.locked);
|
|
CHECK(p.board().at(4, p.board().height() - 2) != cell::kEmpty);
|
|
}
|
|
|
|
void test_hold() {
|
|
PieceQueue q(cycle({PieceType::T, PieceType::S, PieceType::Z, PieceType::O}), 1);
|
|
Player p(0, q, modern_timing());
|
|
spawn(p);
|
|
CHECK(p.active()->type == PieceType::T && !p.held());
|
|
FrameResult r = tap(p, &Input::hold);
|
|
CHECK(r.held);
|
|
CHECK(p.active()->type == PieceType::S && p.held() && p.held()->type == PieceType::T);
|
|
r = tap(p, &Input::hold); // once per piece
|
|
CHECK(!r.held && p.active()->type == PieceType::S);
|
|
tap(p, &Input::hard_drop);
|
|
spawn(p);
|
|
CHECK(p.active()->type == PieceType::Z);
|
|
tap(p, &Input::hold); // swap: T comes back, Z is held
|
|
CHECK(p.active()->type == PieceType::T && p.held()->type == PieceType::Z);
|
|
|
|
// Off in Classic by default.
|
|
PieceQueue q2(cycle({PieceType::T, PieceType::S}), 1);
|
|
Player c(0, q2, Timing::instant());
|
|
spawn(c);
|
|
CHECK(!tap(c, &Input::hold).held && c.active()->type == PieceType::T);
|
|
}
|
|
|
|
void test_bag7() {
|
|
PieceGenerator g = make_generator(GeneratorKind::Bag7, 123, 0, {});
|
|
for (int bag = 0; bag < 20; ++bag) {
|
|
std::array<int, kPieceTypeCount> seen{};
|
|
for (int k = 0; k < 7; ++k) ++seen[static_cast<std::size_t>(g().type)];
|
|
CHECK(std::all_of(seen.begin(), seen.end(), [](int n) { return n == 1; }));
|
|
}
|
|
}
|
|
|
|
void test_attack_table() {
|
|
CHECK(guideline_attack(1, 0, 0, false, false) == 0);
|
|
CHECK(guideline_attack(2, 0, 0, false, false) == 1);
|
|
CHECK(guideline_attack(3, 0, 0, false, false) == 2);
|
|
CHECK(guideline_attack(4, 0, 0, false, false) == 4);
|
|
CHECK(guideline_attack(1, 2, 0, false, false) == 2);
|
|
CHECK(guideline_attack(2, 2, 0, false, false) == 4);
|
|
CHECK(guideline_attack(3, 2, 0, false, false) == 6);
|
|
CHECK(guideline_attack(1, 1, 0, false, false) == 0);
|
|
CHECK(guideline_attack(2, 1, 0, false, false) == 1);
|
|
CHECK(guideline_attack(4, 0, 0, true, false) == 5);
|
|
CHECK(guideline_attack(1, 0, 2, false, false) == 1);
|
|
CHECK(guideline_attack(1, 0, 10, false, false) == 5);
|
|
CHECK(guideline_attack(1, 0, 0, false, true) == 10);
|
|
CHECK(guideline_attack(0, 2, 3, true, false) == 0);
|
|
}
|
|
|
|
// TSD slot at the bottom: overhang at (3,21), row 22 open at 3-5, row 23 open at 4.
|
|
void build_tsd(Board& b) {
|
|
const int h = b.height();
|
|
for (int x = 0; x < b.width(); ++x) {
|
|
if (x < 3 || x > 5) b.set(x, h - 2, cell::kBlock);
|
|
if (x != 4) b.set(x, h - 1, cell::kBlock);
|
|
}
|
|
b.set(3, h - 3, cell::kBlock);
|
|
}
|
|
|
|
void test_tspin_double() {
|
|
PieceQueue q(cycle({PieceType::T}), 1);
|
|
Timing t = modern_timing();
|
|
t.guideline_attack = true;
|
|
Player p(0, q, t);
|
|
build_tsd(p.board());
|
|
spawn(p);
|
|
tap(p, &Input::rotate_cw); // pointing right, over columns 4-5
|
|
Input sd;
|
|
sd.soft_drop = true;
|
|
for (int k = 0; k < 100 && p.active()->y < p.board().height() - 3; ++k) p.step(sd);
|
|
p.step({});
|
|
CHECK(p.active()->y == p.board().height() - 3 && p.active()->x == 3);
|
|
tap(p, &Input::rotate_cw); // into the slot
|
|
CHECK(p.active()->rot == 2);
|
|
const FrameResult r = tap(p, &Input::hard_drop);
|
|
CHECK(r.locked && r.cleared.rows == 2);
|
|
CHECK(r.tspin == 2);
|
|
CHECK(r.attack == 4);
|
|
CHECK(p.attack_stock() == 4);
|
|
|
|
// The same slot filled without a rotation last is no T-spin.
|
|
PieceQueue q2(cycle({PieceType::T}), 1);
|
|
Player p2(0, q2, t);
|
|
build_tsd(p2.board());
|
|
p2.board().set(3, p2.board().height() - 3, cell::kEmpty);
|
|
p2.board().set(0, p2.board().height() - 3, cell::kBlock); // not a perfect clear
|
|
spawn(p2);
|
|
tap(p2, &Input::rotate_cw);
|
|
tap(p2, &Input::rotate_cw); // pointing down at the top
|
|
const FrameResult r2 = tap(p2, &Input::hard_drop);
|
|
CHECK(r2.locked && r2.cleared.rows == 2 && r2.tspin == 0 && r2.attack == 1);
|
|
}
|
|
|
|
const CharacterDef* chr(std::string_view name) {
|
|
const Roster& r = Roster::original();
|
|
return &r.characters[static_cast<std::size_t>(r.find(name))];
|
|
}
|
|
|
|
// Both players clear a tetris on the same frame: with guideline garbage the second cancels the first.
|
|
// Each tetris empties the board: `bonuses` counts the perfect-clear Bonus events.
|
|
int tetris_exchange(bool guideline, int* bonuses = nullptr, bool netting = true) {
|
|
MatchConfig cfg = MatchConfig::instant();
|
|
cfg.stock_netting = netting;
|
|
cfg.seed = 3;
|
|
cfg.timing = modern_timing();
|
|
cfg.timing.guideline_attack = guideline;
|
|
cfg.generator = GeneratorKind::Weighted;
|
|
cfg.generator_weights = {0, 0, 0, 0, 0, 1, 0};
|
|
cfg.crystal_interval = 0;
|
|
Match m(cfg, {chr("Halloween"), chr("Mirurun")});
|
|
for (int i = 0; i < 2; ++i) {
|
|
Board& b = m.player(i).board();
|
|
for (int y = b.height() - 4; y < b.height(); ++y)
|
|
for (int x = 0; x < 9; ++x) b.set(x, y, cell::kBlock);
|
|
}
|
|
std::array<Input, 2> in{};
|
|
m.step(in);
|
|
in[0].rotate_cw = in[1].rotate_cw = true;
|
|
m.step(in);
|
|
in = {};
|
|
in[0].right = in[1].right = true;
|
|
for (int k = 0; k < 3; ++k) m.step(in);
|
|
in = {};
|
|
m.step(in);
|
|
in[0].hard_drop = in[1].hard_drop = true;
|
|
m.step(in);
|
|
if (bonuses)
|
|
for (const MatchEvent& e : m.events()) *bonuses += e.type == MatchEvent::Type::Bonus && e.value == (4 | 1 << 6);
|
|
in = {};
|
|
for (int k = 0; k < 5; ++k) m.step(in);
|
|
int garbage = 0;
|
|
for (int i = 0; i < 2; ++i) {
|
|
const Board& b = m.player(i).board();
|
|
for (int y = b.hidden_rows(); y < b.height(); ++y) garbage += !b.row_empty(y);
|
|
}
|
|
return garbage;
|
|
}
|
|
|
|
void test_cancelling() {
|
|
int bonuses = 0;
|
|
CHECK(tetris_exchange(true, &bonuses) == 0);
|
|
CHECK(bonuses == 2);
|
|
bonuses = 0;
|
|
CHECK(tetris_exchange(false, &bonuses, false) == 8); // TBG garbage without netting: both send 4 rows
|
|
CHECK(bonuses == 0); // and no Bonus events
|
|
CHECK(tetris_exchange(false) == 0); // with the original's stock netting (81:D6AE) the two Tetrises cancel
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void run_modern_tests() {
|
|
test_srs_shapes_and_kicks();
|
|
test_srs_wall_kick();
|
|
test_classic_has_no_kicks();
|
|
test_lock_delay_and_resets();
|
|
test_hard_drop_and_ghost();
|
|
test_hold();
|
|
test_bag7();
|
|
test_attack_table();
|
|
test_tspin_double();
|
|
test_cancelling();
|
|
}
|