Files
AVA2/tests/attack_combo_test.cpp

306 lines
16 KiB
C++

// Attack wiring (mobile step 3b): CRaceMotionData .msa parsing, the PythonPlayerSettingsModule combo
// tables, the ActorInstanceBattle combo state machine and __NormalAttackProcess hit detection.
// The checks call the code under test inside assert(); keep them in release builds.
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "src/eter_render/ActorInstance.h"
#include "src/eter_render/RaceData.h"
#include "test_rundir.h"
#include <cassert>
#include <cmath>
#include <iostream>
namespace {
using ava2::RaceData;
using ava2::RaceMotionData;
bool Near(float a, float b, float eps = 1e-4f) { return std::fabs(a - b) < eps; }
void TestParse() {
// Old single-hit layout (AttackingType key, no HitDataCount).
const char* oldMsa =
"ScriptType MotionData\n"
"MotionFileName \"d:/ymir work/pc/warrior/general/attack.gr2\"\n"
"MotionDuration 1.000000\n"
"Group ComboInputData\n{\n"
" PreInputTime 1.0\n DirectInputTime 1.0\n InputLimitTime 0.602564\n LinkTime 0.0\n}\n"
"Group AttackingData\n{\n"
" AttackingType 1\n HittingType 2\n StiffenTime 0.0\n InvisibleTime 0.5\n ExternalForce 0.0\n"
" AttackingStartTime 0.456410\n AttackingEndTime 0.597436\n"
" AttackingBone \"equip_right_hand\"\n WeaponLength 0.0\n"
" List HitPosition\n {\n"
" 0.456410 1.0 45.0 104.0 1.5 45.5 104.5\n"
" 0.464744 -3.0 41.0 102.0 -3.5 41.5 102.5\n"
" }\n}\n";
RaceMotionData a;
assert(ava2::ParseRaceMotionData(oldMsa, &a));
assert(Near(a.motionDuration, 1.0f));
assert(a.isComboMotion && Near(a.inputStartTime, 1.0f) && Near(a.nextComboTime, 1.0f) && Near(a.inputEndTime, 0.602564f));
assert(a.isAttackingMotion && a.motionType == RaceMotionData::MOTION_TYPE_NORMAL);
assert(a.attackType == 0 && a.hitLimitCount == 0); // AttackType missing: ATTACK_TYPE_SPLASH (0)
assert(Near(a.invisibleTime, 0.5f));
assert(a.hitData.size() == 1 && a.hitData[0].boneName == "equip_right_hand");
assert(Near(a.hitData[0].attackStartTime, 0.456410f) && a.hitData[0].hitPositions.size() == 2);
const auto& first = a.hitData[0].hitPositions.begin()->second;
assert(Near(first.last[0], 1.0f) && Near(first.last[1], 45.0f) && Near(first.pos[2], 104.5f));
// Grouped layout (MotionType + HitDataCount + HitDataNN), accumulation and hit limit.
const char* groupMsa =
"ScriptType MotionData\n"
"MotionFileName \"d:/ymir work/pc2/warrior/onehand_sword/combo_03.gr2\"\n"
"MotionDuration 1.066667\n"
"Accumulation 0.00 -143.01 0.00\n"
"Group ComboInputData\n{\n"
" PreInputTime 0.084786\n DirectInputTime 0.418462\n InputLimitTime 0.664615\n LinkTime 0.06\n}\n"
"Group AttackingData\n{\n"
" AttackType 0\n HittingType 2\n StiffenTime 0.0\n InvisibleTime 0.2\n ExternalForce 5.0\n"
" HitLimitCount 3\n MotionType 2\n HitDataCount 2\n"
" Group HitData00\n {\n"
" AttackingStartTime 0.18\n AttackingEndTime 0.52\n"
" AttackingBone \"equip_right_hand\"\n WeaponLength 130.0\n"
" List HitPosition\n {\n 0.180513 -31.0 -37.0 78.0 -88.0 77.0 55.0\n }\n }\n"
" Group HitData01\n {\n"
" AttackingStartTime 0.6\n AttackingEndTime 0.7\n"
" AttackingBone \"equip_left_hand\"\n WeaponLength 0.0\n"
" List HitPosition\n {\n 0.6 1 2 3 4 5 6\n 0.7 7 8 9 10 11 12\n }\n }\n"
"}\n";
RaceMotionData b;
assert(ava2::ParseRaceMotionData(groupMsa, &b));
assert(Near(b.accumulation[1], -143.01f));
assert(Near(b.inputStartTime, 0.084786f) && Near(b.nextComboTime, 0.418462f) && Near(b.inputEndTime, 0.664615f));
assert(b.motionType == RaceMotionData::MOTION_TYPE_COMBO && b.attackType == 0 && b.hitLimitCount == 3);
assert(b.hitData.size() == 2);
assert(b.hitData[1].boneName == "equip_left_hand" && b.hitData[1].hitPositions.size() == 2);
assert(Near(b.hitData[1].hitPositions.rbegin()->second.pos[0], 10.0f));
// CRaceMotionData::LoadMotionData fails without MotionDuration.
RaceMotionData c;
assert(!ava2::ParseRaceMotionData("ScriptType MotionData\nMotionFileName \"x.gr2\"\n", &c));
}
void TestComboTables() {
const RaceData* warrior = ava2::RaceManager::Instance().GetRaceDataPointer(0);
assert(warrior);
const auto* type1 = warrior->GetComboData(2, 0); // MODE_ONEHAND_SWORD, COMBO_TYPE_1
assert(type1 && type1->size() == 4);
for (uint32_t k = 0; k < 4; ++k) assert((*type1)[k] == RaceData::NAME_COMBO_ATTACK_1 + k);
const auto* type2 = warrior->GetComboData(2, 1);
assert(type2 && type2->size() == 5 && type2->back() == RaceData::NAME_COMBO_ATTACK_7);
const auto* type3 = warrior->GetComboData(2, 2);
assert(type3 && type3->size() == 6 && type3->back() == RaceData::NAME_COMBO_ATTACK_4);
// Horse weapon combos are three swings.
const auto* horse = warrior->GetComboData(10, 0);
assert(horse && horse->size() == 3);
const auto* motions = warrior->GetMotions(2, RaceData::NAME_COMBO_ATTACK_2);
assert(motions && !motions->empty());
const RaceMotionData* combo2 = ava2::GetRaceMotionData(motions->front().fileName);
assert(combo2 && combo2->isComboMotion && combo2->isAttackingMotion);
assert(combo2->motionType == RaceMotionData::MOTION_TYPE_COMBO);
assert(!combo2->hitData.empty() && !combo2->hitData[0].hitPositions.empty());
// Cached by path.
assert(ava2::GetRaceMotionData(motions->front().fileName) == combo2);
}
// Advances the current motion to motion time t (attack speed 1).
void AdvanceTo(ava2::ActorInstance& actor, float t) {
const float dt = t - actor.GetAttackingElapsedTime();
assert(dt > 0.0f);
actor.Update(dt);
assert(Near(actor.GetAttackingElapsedTime(), t, 1e-3f));
}
void TestComboStateMachine(const char* model) {
ava2::ActorInstance actor;
assert(actor.SetRace(0));
assert(actor.SetModel(model));
actor.SetMotionMode(2);
assert(actor.PlayMotion(ava2::MotionType::Wait, true));
actor.SetReportAttacks(true);
actor.SetAttackSpeed(1.0f);
uint16_t motion = 0;
assert(actor.GetComboIndex() == 0 && !actor.PopAttackMotion(&motion));
// First input starts combo 1 facing the input direction and reports it.
assert(actor.InputComboAttackCommand(90.0f));
assert(actor.GetComboIndex() == 1);
assert(actor.PopAttackMotion(&motion) && motion == RaceData::NAME_COMBO_ATTACK_1);
assert(!actor.PopAttackMotion(&motion));
assert(actor.IsAttacking() && actor.CanCheckAttacking());
const RaceMotionData* m1 = actor.GetCurrentRaceMotionData();
assert(m1 && m1->isComboMotion);
assert(m1->inputStartTime < m1->nextComboTime && m1->nextComboTime < m1->inputEndTime);
// Before PreInputTime the input is ignored.
AdvanceTo(actor, m1->inputStartTime * 0.5f);
assert(!actor.InputComboAttackCommand(90.0f) && actor.GetComboIndex() == 1);
// Between PreInputTime and DirectInputTime it is buffered; ComboProcess fires it at DirectInputTime.
AdvanceTo(actor, (m1->inputStartTime + m1->nextComboTime) * 0.5f);
assert(!actor.InputComboAttackCommand(90.0f));
actor.ComboProcess();
assert(actor.GetComboIndex() == 1 && !actor.PopAttackMotion(&motion));
AdvanceTo(actor, (m1->nextComboTime + m1->inputEndTime) * 0.5f);
actor.ComboProcess();
assert(actor.GetComboIndex() == 2);
assert(actor.PopAttackMotion(&motion) && motion == RaceData::NAME_COMBO_ATTACK_2);
assert(Near(actor.GetAttackingElapsedTime(), 0.0f));
// After DirectInputTime a direct input chains immediately.
const RaceMotionData* m2 = actor.GetCurrentRaceMotionData();
assert(m2 && m2 != m1);
AdvanceTo(actor, (m2->nextComboTime + m2->inputEndTime) * 0.5f);
assert(actor.InputComboAttackCommand(90.0f));
assert(actor.GetComboIndex() == 3);
assert(actor.PopAttackMotion(&motion) && motion == RaceData::NAME_COMBO_ATTACK_3);
// Past InputLimitTime the combo can no longer continue.
const RaceMotionData* m3 = actor.GetCurrentRaceMotionData();
AdvanceTo(actor, std::min(m3->motionDuration * 0.99f, m3->inputEndTime + 0.02f));
assert(!actor.InputComboAttackCommand(90.0f) && actor.GetComboIndex() == 3);
assert(!actor.PopAttackMotion(&motion));
// Attack speed scales the motion clock of attack motions.
ava2::ActorInstance fast;
assert(fast.SetRace(0) && fast.SetModel(model));
fast.SetMotionMode(2);
assert(fast.PlayMotion(ava2::MotionType::Wait, true));
fast.SetAttackSpeed(1.5f);
assert(fast.InputComboAttackCommand(0.0f));
fast.Update(0.1f);
assert(Near(fast.GetAttackingElapsedTime(), 0.15f, 1e-3f));
// SetAttackSpeed stores zero but does not retime the already bound attack.
fast.SetAttackSpeed(0);
fast.Update(0.1f);
assert(Near(fast.GetAttackingElapsedTime(), 0.30f, 1e-3f));
assert(fast.RunComboAttack(0, RaceData::NAME_COMBO_ATTACK_1));
fast.Update(0.1f);
assert(Near(fast.GetAttackingElapsedTime(), 0));
assert(!fast.IsMotionFinished());
fast.SetAttackSpeed(2);
fast.Update(0.1f);
assert(Near(fast.GetAttackingElapsedTime(), 0));
assert(fast.RunComboAttack(0, RaceData::NAME_COMBO_ATTACK_1));
fast.Update(0.1f);
assert(Near(fast.GetAttackingElapsedTime(), 0.2f, 1e-3f));
fast.SetAttackSpeed(-1);
assert(Near(fast.GetAttackSpeed(), 2));
// Without SetReportAttacks nothing is queued (remote actors).
ava2::ActorInstance remote;
assert(remote.SetRace(0) && remote.SetModel(model));
remote.SetMotionMode(2);
assert(remote.PlayMotion(ava2::MotionType::Wait, true));
assert(remote.RunComboAttack(45.0f, RaceData::NAME_COMBO_ATTACK_2));
assert(remote.GetCurrentMotion() == ava2::MotionType(RaceData::NAME_COMBO_ATTACK_2));
assert(!remote.PopAttackMotion(&motion));
}
void TestHitDetection(const char* model) {
for (float yaw : {0.0f, 90.0f}) {
ava2::ActorInstance actor;
assert(actor.SetRace(0) && actor.SetModel(model));
actor.SetMotionMode(2);
assert(actor.PlayMotion(ava2::MotionType::Wait, true));
const float px = 1000.0f, pz = 2000.0f;
actor.SetPosition(px, 0.0f, pz);
assert(!actor.CanCheckAttacking());
assert(!actor.TestAttackHit(7, px, pz, 50.0f, 0.05f)); // waiting: no attacking motion
assert(actor.InputComboAttackCommand(yaw));
const RaceMotionData* m = actor.GetCurrentRaceMotionData();
assert(m && !m->hitData.empty());
const auto key = *m->hitData[0].hitPositions.begin();
AdvanceTo(actor, key.first + 0.01f);
// Graphic local (x, y) rotated by the yaw, port Z = -graphic y.
const float* p = key.second.pos;
float vx, vz;
if (yaw == 0.0f) { vx = px + p[0]; vz = pz - p[1]; }
else { vx = px - p[1]; vz = pz - p[0]; }
// A victim at the blade position is hit once per combo motion.
assert(actor.TestAttackHit(7, vx, vz, 50.0f, 0.05f));
assert(!actor.TestAttackHit(7, vx, vz, 50.0f, 0.05f));
// Another victim at the same spot is still hit.
assert(actor.TestAttackHit(8, vx, vz, 50.0f, 0.05f));
// On the far side of the actor the swing does not reach.
const float ox = px - (vx - px) / std::hypot(vx - px, vz - pz) * 250.0f;
const float oz = pz - (vz - pz) / std::hypot(vx - px, vz - pz) * 250.0f;
assert(!actor.TestAttackHit(9, ox, oz, 50.0f, 0.05f));
// Out of the 300 range (500 for huge victims) nothing is tested.
assert(!actor.TestAttackHit(10, px + 400.0f, pz, 500.0f, 0.05f));
// The next combo motion clears the hit map: victim 7 can be hit again by its swing.
actor.SetReportAttacks(true);
AdvanceTo(actor, (m->nextComboTime + m->inputEndTime) * 0.5f);
assert(actor.InputComboAttackCommand(yaw) && actor.GetComboIndex() == 2);
}
}
void TestMotionEvents() {
// LoopData + MotionEventData (sound, special attacking with collision spheres, show/hide).
const char* skillMsa =
"ScriptType MotionData\n"
"MotionFileName \"d:/ymir work/pc/warrior/skill/palbang.gr2\"\n"
"MotionDuration 2.0\n"
"Group LoopData\n{\n MotionLoopCount 3\n LoopStartTime 0.5\n LoopEndTime 1.25\n}\n"
"Group MotionEventData\n{\n MotionEventDataCount 4\n"
" Group Event00\n {\n MotionEventType 5\n StartingTime 0.1\n"
" SoundFileName \"sound/pc/warrior/palbang.wav\"\n }\n"
" Group Event01\n {\n MotionEventType 4\n StartingTime 0.6\n"
" DuringTime 0.3\n EnableHitProcess 1\n AttackType 1\n HittingType 3\n"
" StiffenTime 0.1\n InvisibleTime 0.2\n ExternalForce 4.0\n HitLimitCount 5\n"
" CollisionType 0\n SphereDataCount 1\n"
" Group SphereData00\n {\n Radius 150.0\n Position 0.0 -120.0 80.0\n }\n }\n"
" Group Event02\n {\n MotionEventType 8\n StartingTime 0.7\n }\n"
" Group Event03\n {\n MotionEventType 7\n StartingTime 1.0\n }\n"
"}\n";
RaceMotionData m;
assert(ava2::ParseRaceMotionData(skillMsa, &m));
assert(m.isLoopMotion && m.loopCount == 3 && Near(m.loopStartTime, 0.5f) && Near(m.loopEndTime, 1.25f));
assert(m.events.size() == 4 && m.hasSplashEvent);
assert(m.events[0].type == RaceMotionData::MOTION_EVENT_TYPE_SOUND && m.events[0].frame == 6);
assert(m.events[0].fileName == "sound/pc/warrior/palbang.wav");
const auto& splash = m.events[1];
assert(splash.type == RaceMotionData::MOTION_EVENT_TYPE_SPECIAL_ATTACKING && splash.frame == 36);
assert(Near(splash.durationTime, 0.3f) && splash.attackType == 1 && splash.hittingType == 3 && splash.hitLimitCount == 5);
assert(splash.spheres.size() == 1 && Near(splash.spheres[0].radius, 150.0f) && Near(splash.spheres[0].position[1], -120.0f));
assert(m.events[2].type == RaceMotionData::MOTION_EVENT_TYPE_CHARACTER_HIDE && m.events[3].type == RaceMotionData::MOTION_EVENT_TYPE_CHARACTER_SHOW);
// An unknown event type fails the whole .msa, like "Strange Event Type".
const char* badMsa =
"MotionFileName \"x.gr2\"\nMotionDuration 1.0\n"
"Group MotionEventData\n{\n MotionEventDataCount 1\n"
" Group Event00\n {\n MotionEventType 3\n StartingTime 0.1\n }\n}\n";
RaceMotionData bad;
assert(!ava2::ParseRaceMotionData(badMsa, &bad));
// Per-motion .mss sound script.
std::vector<RaceMotionData::SoundInstance> sounds;
assert(ava2::ParseSoundScriptData(
"ScriptType CharacterSoundData\nSoundDataCount 2\n"
"SoundData00 0.726000 \"sound/common/walk_dirt_n.wav\"\n"
"SoundData01 0.100000 \"sound/common/walk_dirt_1.wav\"\n", &sounds));
assert(sounds.size() == 2 && sounds[0].fileName == "sound/common/walk_dirt_n.wav" && sounds[0].frame == 43);
assert(!ava2::ParseSoundScriptData("SoundDataCount 1\nSoundData00 0.5\n", &sounds) && sounds.size() == 2);
}
} // namespace
int main(int argc, char** argv) {
TestParse();
TestMotionEvents();
std::cout << "msa parsing ok\n";
if (argc < 2) return 0;
if (!ava2_test::EnterTestRunDir()) return ava2_test::kSkip;
ava2::RaceManager::Instance().LoadPlayerRaces();
if (!ava2::RaceManager::Instance().GetRaceDataPointer(0)) {
std::cerr << "SKIP: player races missing from the run directory\n";
return ava2_test::kSkip;
}
TestComboTables();
TestComboStateMachine(argv[1]);
TestHitDetection(argv[1]);
std::cout << "Attack combos, timings and hit detection follow the original\n";
return 0;
}