Wire attack and combo execution (mobile step 3b)

Port the ActorInstanceBattle combo state machine, CRaceMotionData attack
data, player motion/combo tables, NEW_Attack + OnAttack/OnHit packets and
remote FUNC_ATTACK/FUNC_COMBO playback so keyboard and touch attacks swing
and hit on the real-server path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 20:30:57 +09:00
co-authored by Claude Opus 5.5
parent 5597842d90
commit 260d421bde
14 changed files with 1130 additions and 47 deletions
+208 -16
View File
@@ -413,19 +413,210 @@ bool ActorInstance::PlayMotion(MotionType type, bool loop) {
m_model->SetAnimationLoop(loop, loop);
SyncLodMotion();
if (type == MotionType::Attack || type == MotionType::ComboAttack1 ||
type == MotionType::ComboAttack2 || type == MotionType::ComboAttack3 ||
type == MotionType::Skill1 || type == MotionType::Skill2 ||
type == MotionType::Skill3 || type == MotionType::Skill4 ||
type == MotionType::Skill5) {
m_isAttacking = true;
} else {
m_isAttacking = false;
}
const uint32_t index = uint32_t(type);
m_isAttacking = (index >= RaceData::NAME_NORMAL_ATTACK && index <= RaceData::NAME_COMBO_ATTACK_8) ||
(index >= RaceData::NAME_SKILL && index < RaceData::NAME_SKILL_END);
OnMotionBound(index);
return true;
}
void ActorInstance::OnMotionBound(uint32_t type) {
// ActorInstanceMotion.cpp __BindMotionData: the race motion data of the new motion.
m_curRaceMotion = nullptr;
uint16_t keyMode;
if (m_raceData && m_raceData->GetMotionKey(uint16_t(m_motionMode), uint16_t(type), &keyMode)) {
const auto* entries = m_raceData->GetMotions(keyMode, type);
if (entries && !entries->empty()) m_curRaceMotion = GetRaceMotionData(entries->front().fileName);
}
if (!m_curRaceMotion) {
m_comboIndex = 0;
m_motionSpeed = 1.0f;
return;
}
if (m_curRaceMotion->isAttackingMotion) m_hitDataMap.clear();
if (m_comboIndex != 0 && !m_curRaceMotion->isComboMotion) m_comboIndex = 0;
// InterceptOnceMotion(..., __GetAttackSpeed()) for normal and combo attacks.
const bool attackMotion = m_curRaceMotion->isAttackingMotion &&
(m_curRaceMotion->motionType == RaceMotionData::MOTION_TYPE_NORMAL ||
m_curRaceMotion->motionType == RaceMotionData::MOTION_TYPE_COMBO);
m_motionSpeed = attackMotion ? m_atkSpd : 1.0f;
}
bool ActorInstance::IsWaiting() const {
const uint32_t index = uint32_t(m_currentMotion);
return index == RaceData::NAME_WAIT || index == RaceData::NAME_INTRO_WAIT || index == RaceData::NAME_STOP;
}
bool ActorInstance::IsUsingSkill() const {
const uint32_t index = uint32_t(m_currentMotion);
return (index >= RaceData::NAME_SKILL && index < RaceData::NAME_SKILL_END) ||
(index >= RaceData::NAME_SPECIAL_1 && index <= RaceData::NAME_SPECIAL_6);
}
float ActorInstance::GetDefendingRadius() const {
return m_raceData ? m_raceData->GetDefendingRadius() * m_scale : 0.0f;
}
bool ActorInstance::CanCheckAttacking() const {
if (!m_curRaceMotion || !m_curRaceMotion->isAttackingMotion || IsMotionFinished()) return false;
return m_curRaceMotion->motionType == RaceMotionData::MOTION_TYPE_NORMAL ||
m_curRaceMotion->motionType == RaceMotionData::MOTION_TYPE_COMBO;
}
uint16_t ActorInstance::GetCurrentComboType() const {
// __GetCurrentComboType
if (IsBowMode() || IsHandMode() || m_isMountingHorse) return 0;
return m_wcurComboType;
}
void ActorInstance::RunNextCombo() {
++m_comboIndex;
if (!m_raceData) return;
const auto* combo = m_raceData->GetComboData(uint16_t(m_motionMode), GetCurrentComboType());
// The original TraceErrors these two cases on every input; the port stays quiet.
if (!combo) return;
const uint32_t arrayIndex = m_comboIndex - 1;
if (arrayIndex >= combo->size()) return;
const uint32_t comboSize = uint32_t(combo->size());
const uint16_t motion = uint16_t((*combo)[arrayIndex]);
RunComboAttack(m_atkDirYaw, motion);
if (m_comboIndex == comboSize) {
// __OnEndCombo: mounted combos wrap; on foot the index resets when Wait binds.
if (m_isMountingHorse) m_comboIndex = 1;
}
}
bool ActorInstance::InputComboAttackCommand(float dirYaw) {
m_atkDirYaw = dirYaw;
if (m_isPreInput) return false;
if (m_comboIndex == 0) {
RunNextCombo();
return true;
}
if (!m_curRaceMotion) return false;
const float elapsed = GetAttackingElapsedTime();
if (m_curRaceMotion->isComboMotion) {
if (elapsed > m_curRaceMotion->inputEndTime) {
if (IsBowMode()) m_isNextPreInput = true;
return false;
}
if (elapsed > m_curRaceMotion->nextComboTime) {
RunNextCombo();
return true;
}
if (elapsed > m_curRaceMotion->inputStartTime) m_isPreInput = true;
} else if (elapsed > m_curRaceMotion->motionDuration * 0.9f) {
RunNextCombo();
return true;
}
return false;
}
bool ActorInstance::InputNormalAttackCommand(float dirYaw) {
// __CanInputNormalAttackCommand + NormalAttack
bool canInput = IsWaiting();
if (!canInput && m_curRaceMotion && m_curRaceMotion->isAttackingMotion &&
m_curRaceMotion->motionType == RaceMotionData::MOTION_TYPE_NORMAL)
canInput = GetAttackingElapsedTime() > m_curRaceMotion->motionDuration * 0.9f;
if (!canInput) return false;
return RunNormalAttack(dirYaw);
}
bool ActorInstance::RunNormalAttack(float dirYaw) {
uint16_t motion;
if (!m_raceData || !m_raceData->GetNormalAttackIndex(uint16_t(m_motionMode), &motion)) return false;
m_yaw = dirYaw;
if (!InterceptMotionIndex(motion, false)) return false;
if (m_reportAttacks) m_attackMotions.push_back(motion); // __OnAttack
return true;
}
bool ActorInstance::RunComboAttack(float dirYaw, uint16_t motion) {
// ComboAttack: rotate, InterceptOnceMotion at the attack speed, __OnAttack. The combo index is
// kept across the bind (the original binds before it compares the new motion's combo data).
m_yaw = dirYaw;
if (!InterceptMotionIndex(motion, false)) return false;
if (m_reportAttacks) m_attackMotions.push_back(motion);
return true;
}
void ActorInstance::ComboProcess() {
if (m_comboIndex != 0) {
if (!m_curRaceMotion) {
m_comboIndex = 0; // __ClearCombo
m_isPreInput = false;
return;
}
if (m_isPreInput && GetAttackingElapsedTime() > m_curRaceMotion->nextComboTime) {
RunNextCombo();
m_isPreInput = false;
}
} else {
m_isPreInput = false;
if (!IsUsingSkill() && m_isNextPreInput) {
RunNextCombo();
m_isNextPreInput = false;
}
}
}
bool ActorInstance::PopAttackMotion(uint16_t* motion) {
if (m_attackMotions.empty()) return false;
*motion = m_attackMotions.front();
m_attackMotions.pop_front();
return true;
}
bool ActorInstance::TestAttackHit(uint32_t victimVID, float x, float z, float radius, float frameDt, bool huge) {
// __NormalAttackProcess. The original's distance vector is (dx, dz, dz); horizontal distance here.
const float distX = x - m_posX, distZ = z - m_posZ;
const float range = huge ? 500.0f : 300.0f;
if (distX * distX + distZ * distZ >= range * range) return false;
if (!CanCheckAttacking()) return false;
const float attackRadius = 20.0f;
const float victimRadius = radius > 0.0f ? radius : 50.0f;
const float limit = attackRadius + victimRadius;
const auto& pad = *m_curRaceMotion;
const float motionTime = GetAttackingElapsedTime();
const float c = std::cos(m_yaw * DEG_TO_RAD), s = std::sin(m_yaw * DEG_TO_RAD);
// Graphic space (x, y) -> port (X, Z = -y); the port yaw is the graphic rotation.
auto toWorld = [&](const float* local, float& outX, float& outZ) {
outX = m_posX + local[0] * c - local[1] * s;
outZ = m_posZ - (local[0] * s + local[1] * c);
};
for (size_t hitIndex = 0; hitIndex < pad.hitData.size(); ++hitIndex) {
const auto& hit = pad.hitData[hitIndex];
auto& hitted = m_hitDataMap[hitIndex];
const auto already = hitted.find(victimVID);
if (already != hitted.end() &&
(pad.motionType == RaceMotionData::MOTION_TYPE_COMBO || already->second > m_localTime))
continue;
auto it = hit.hitPositions.lower_bound(motionTime - frameDt);
const auto end = hit.hitPositions.upper_bound(motionTime);
for (; it != end; ++it) {
// __GetReachScale() is 1: v3Position itself.
float ax, az, bx, bz;
toWorld(it->second.last, ax, az);
toWorld(it->second.pos, bx, bz);
// Segment (a, b) against the static victim point.
const float sx = bx - ax, sz = bz - az;
const float lenSq = sx * sx + sz * sz;
float t = lenSq > 1e-6f ? ((x - ax) * sx + (z - az) * sz) / lenSq : 0.0f;
t = std::clamp(t, 0.0f, 1.0f);
const float px = ax + sx * t - x, pz = az + sz * t - z;
if (px * px + pz * pz > limit * limit) continue;
// insert keeps an existing entry, like std::map::insert in the original.
hitted.insert({victimVID, m_localTime + pad.invisibleTime});
const size_t count = hitted.size();
const bool normal = pad.motionType == RaceMotionData::MOTION_TYPE_COMBO ||
pad.motionType == RaceMotionData::MOTION_TYPE_NORMAL;
if (normal ? count > 16 : int(count) > pad.hitLimitCount) return false;
return true;
}
}
return false;
}
bool ActorInstance::PlayMotionIndex(uint32_t index, bool loop) {
uint16_t keyMode;
if (!m_raceData || !m_raceData->GetMotionKey(uint16_t(m_motionMode), uint16_t(index), &keyMode)) return false;
@@ -586,8 +777,9 @@ void ActorInstance::Update(float dt, const TerrainHeightSource* terrain, const M
}
}
// Advance animation time
m_animTime += dt;
// Advance animation time (attack motions run at the attack speed ratio).
m_animTime += dt * m_motionSpeed;
m_localTime += dt;
m_lodLocalTime += dt; // CGrannyModelInstance::UpdateLocalTime
if (!m_motionQueue.empty() && m_animTime >= m_motionDuration) {
// ReservingMotionProcess: the next pushed motion takes over when the current one ends.
@@ -616,17 +808,17 @@ bool ActorInstance::Attack() {
if (m_curHP <= 0) return false;
if (!m_isAttacking) {
m_comboIndex = 1;
m_previewComboIndex = 1;
if (!PlayMotion(MotionType::ComboAttack1, false)) {
PlayMotion(MotionType::Attack, false);
}
return true;
} else if (m_animTime > m_motionDuration * 0.35f) {
// Advance combo chain
m_comboIndex = (m_comboIndex % 3) + 1;
m_previewComboIndex = (m_previewComboIndex % 3) + 1;
MotionType next = MotionType::ComboAttack1;
if (m_comboIndex == 2) next = MotionType::ComboAttack2;
else if (m_comboIndex == 3) next = MotionType::ComboAttack3;
if (m_previewComboIndex == 2) next = MotionType::ComboAttack2;
else if (m_previewComboIndex == 3) next = MotionType::ComboAttack3;
if (!PlayMotion(next, false)) {
PlayMotion(MotionType::Attack, false);