feat(animation): decompose AnimationController into FSM-based architecture

Replace the 2,200-line monolithic AnimationController (goto-driven,
single class, untestable) with a composed FSM architecture per
refactor.md.

New subsystem (src/rendering/animation/ — 16 headers, 10 sources):
- CharacterAnimator: FSM composer implementing ICharacterAnimator
- LocomotionFSM: idle/walk/run/sprint/jump/swim/strafe
- CombatFSM: melee/ranged/spell cast/stun/hit reaction/charge
- ActivityFSM: emote/loot/sit-down/sitting/sit-up
- MountFSM: idle/run/flight/taxi/fidget/rear-up (per-instance RNG)
- AnimCapabilitySet + AnimCapabilityProbe: probe once at model load,
  eliminate per-frame hasAnimation() linear search
- AnimationManager: registry of CharacterAnimator by GUID
- EmoteRegistry: DBC-backed emote command → animId singleton
- FootstepDriver, SfxStateDriver: extracted from AnimationController

animation_ids.hpp/.cpp moved to animation/ subdirectory (452 named
constants); all include paths updated.

AnimationController retained as thin adapter (~400 LOC): collects
FrameInput, delegates to CharacterAnimator, applies AnimOutput.

Priority order: Mount > Stun > HitReaction > Spell > Charge >
Melee/Ranged > CombatIdle > Emote > Loot > Sit > Locomotion.
STAY_IN_STATE policy when all FSMs return valid=false.

Bugs fixed:
- Remove static mt19937 in mount fidget (shared state across all
  mounted units) — replaced with per-instance seeded RNG
- Remove goto from mounted animation branch (skipped init)
- Remove per-frame hasAnimation() calls (now one probe at load)
- Fix VK_INDEX_TYPE_UINT16 → UINT32 in shadow pass

Tests (4 new suites, all ASAN+UBSan clean):
- test_locomotion_fsm: 167 assertions
- test_combat_fsm: 125 cases
- test_activity_fsm: 112 cases
- test_anim_capability: 56 cases

docs/ANIMATION_SYSTEM.md added (architecture reference).
This commit is contained in:
Paul 2026-04-05 12:27:35 +03:00
parent e58f9b4b40
commit b4989dc11f
53 changed files with 5110 additions and 2099 deletions

View file

@ -4,22 +4,31 @@
#include <string>
#include <vector>
#include <glm/glm.hpp>
#include "rendering/animation/footstep_driver.hpp"
#include "rendering/animation/sfx_state_driver.hpp"
#include "rendering/animation/weapon_type.hpp"
#include "rendering/animation/anim_capability_set.hpp"
#include "rendering/animation/character_animator.hpp"
namespace wowee {
namespace audio { enum class FootstepSurface : uint8_t; }
namespace rendering {
class Renderer;
/// Ranged weapon type for animation selection (bow/gun/crossbow/thrown)
enum class RangedWeaponType : uint8_t { NONE = 0, BOW, GUN, CROSSBOW, THROWN };
// ============================================================================
// AnimationController — extracted from Renderer (§4.2)
// AnimationController — thin adapter wrapping CharacterAnimator
//
// Owns the character locomotion state machine, mount animation state,
// emote system, footstep triggering, surface detection, melee combat
// animation, and activity SFX transition tracking.
// Bridges the Renderer world (camera state, CharacterRenderer, audio)
// and the pure-logic CharacterAnimator + sub-FSMs. Public API unchanged.
//
// Responsibilities:
// · Collect inputs from renderer/camera → CharacterAnimator::FrameInput
// · Forward state-changing calls → CharacterAnimator
// · Read AnimOutput from CharacterAnimator → apply via CharacterRenderer
// · Mount discovery (needs renderer for sequence queries)
// · Mount positioning (needs renderer for attachment queries)
// · Melee/ranged resolution (needs renderer for sequence queries)
// · Footstep and SFX drivers (already extracted)
// ============================================================================
class AnimationController {
public:
@ -28,6 +37,11 @@ public:
void initialize(Renderer* renderer);
/// Probe (or re-probe) animation capabilities for the current character model.
/// Called once during initialize() / onCharacterFollow() and after model changes.
void probeCapabilities();
const AnimCapabilitySet& getCapabilities() const { return characterAnimator_.getCapabilities(); }
// ── Per-frame update hooks (called from Renderer::update) ──────────────
// Runs the character animation state machine (mounted + unmounted).
void updateCharacterAnimation();
@ -47,7 +61,7 @@ public:
// ── Emote support ──────────────────────────────────────────────────────
void playEmote(const std::string& emoteName);
void cancelEmote();
bool isEmoteActive() const { return emoteActive_; }
bool isEmoteActive() const { return characterAnimator_.getActivity().isEmoteActive(); }
static std::string getEmoteText(const std::string& emoteName,
const std::string* targetName = nullptr);
static uint32_t getEmoteDbcId(const std::string& emoteName);
@ -58,7 +72,7 @@ public:
// ── Targeting / combat ─────────────────────────────────────────────────
void setTargetPosition(const glm::vec3* pos);
void setInCombat(bool combat) { inCombat_ = combat; }
void setInCombat(bool combat);
bool isInCombat() const { return inCombat_; }
const glm::vec3* getTargetPosition() const { return targetPosition_; }
void resetCombatVisualState();
@ -70,30 +84,19 @@ public:
/// is2HLoose: true for polearms/staves (use ATTACK_2H_LOOSE instead of ATTACK_2H)
void setEquippedWeaponType(uint32_t inventoryType, bool is2HLoose = false,
bool isFist = false, bool isDagger = false,
bool hasOffHand = false, bool hasShield = false) {
equippedWeaponInvType_ = inventoryType;
equippedIs2HLoose_ = is2HLoose;
equippedIsFist_ = isFist;
equippedIsDagger_ = isDagger;
equippedHasOffHand_ = hasOffHand;
equippedHasShield_ = hasShield;
meleeAnimId_ = 0; // Force re-resolve on next swing
}
bool hasOffHand = false, bool hasShield = false);
/// Play a special attack animation for a melee ability (spellId → SPECIAL_1H/2H/SHIELD_BASH/WHIRLWIND)
void triggerSpecialAttack(uint32_t spellId);
// ── Sprint aura animation ────────────────────────────────────────────
void setSprintAuraActive(bool active) { sprintAuraActive_ = active; }
void setSprintAuraActive(bool active);
// ── Ranged combat ──────────────────────────────────────────────────────
void setEquippedRangedType(RangedWeaponType type) {
equippedRangedType_ = type;
rangedAnimId_ = 0; // Force re-resolve
}
void setEquippedRangedType(RangedWeaponType type);
/// Trigger a ranged shot animation (Auto Shot, Shoot, Throw)
void triggerRangedShot();
RangedWeaponType getEquippedRangedType() const { return equippedRangedType_; }
void setCharging(bool charging) { charging_ = charging; }
RangedWeaponType getEquippedRangedType() const { return weaponLoadout_.rangedType; }
void setCharging(bool charging);
bool isCharging() const { return charging_; }
// ── Spell casting ──────────────────────────────────────────────────────
@ -122,7 +125,7 @@ public:
// ── Health-based idle ──────────────────────────────────────────────────
/// When true, idle/combat-idle will prefer STAND_WOUND if the model has it.
void setLowHealth(bool low) { lowHealth_ = low; }
void setLowHealth(bool low);
// ── Stand state (sit/sleep/kneel transitions) ──────────────────────────
// WoW UnitStandStateType constants
@ -166,139 +169,55 @@ public:
private:
Renderer* renderer_ = nullptr;
// Character animation state machine
enum class CharAnimState {
IDLE, WALK, RUN, JUMP_START, JUMP_MID, JUMP_END, SIT_DOWN, SITTING,
SIT_UP, EMOTE, SWIM_IDLE, SWIM, MELEE_SWING, MOUNT, CHARGE, COMBAT_IDLE,
SPELL_PRECAST, SPELL_CASTING, SPELL_FINALIZE, HIT_REACTION, STUNNED, LOOTING,
UNSHEATHE, SHEATHE, // Weapon draw/put-away one-shot transitions
RANGED_SHOOT, RANGED_LOAD // Ranged attack sequence: shoot → reload
};
CharAnimState charAnimState_ = CharAnimState::IDLE;
float locomotionStopGraceTimer_ = 0.0f;
bool locomotionWasSprinting_ = false;
// ── CharacterAnimator: owns the complete character animation FSM ─────
CharacterAnimator characterAnimator_;
bool capabilitiesProbed_ = false;
// ── Animation change detection ───────────────────────────────────────
uint32_t lastPlayerAnimRequest_ = UINT32_MAX;
bool lastPlayerAnimLoopRequest_ = true;
float lastDeltaTime_ = 0.0f;
// Emote state
bool emoteActive_ = false;
uint32_t emoteAnimId_ = 0;
bool emoteLoop_ = false;
// Spell cast sequence state (PRECAST → CASTING → FINALIZE)
uint32_t spellPrecastAnimId_ = 0; // One-shot wind-up (phase 1)
uint32_t spellCastAnimId_ = 0; // Looping cast hold (phase 2)
uint32_t spellFinalizeAnimId_ = 0; // One-shot release (phase 3)
bool spellCastLoop_ = false;
// Hit reaction state
uint32_t hitReactionAnimId_ = 0;
// Crowd control
bool stunned_ = false;
// Health-based idle
bool lowHealth_ = false;
// Stand state (sit/sleep/kneel)
uint8_t standState_ = 0;
uint32_t sitDownAnim_ = 0; // Transition-in animation (one-shot)
uint32_t sitLoopAnim_ = 0; // Looping pose animation
uint32_t sitUpAnim_ = 0; // Transition-out animation (one-shot)
// Stealth
bool stealthed_ = false;
// Target facing
// ── Externally-queried state (mirrored to CharacterAnimator) ────────────
const glm::vec3* targetPosition_ = nullptr;
bool inCombat_ = false;
// Footstep event tracking (animation-driven)
uint32_t footstepLastAnimationId_ = 0;
float footstepLastNormTime_ = 0.0f;
bool footstepNormInitialized_ = false;
// Footstep surface cache (avoid expensive queries every step)
mutable audio::FootstepSurface cachedFootstepSurface_{};
mutable glm::vec3 cachedFootstepPosition_{0.0f, 0.0f, 0.0f};
mutable float cachedFootstepUpdateTimer_{999.0f};
// Mount footstep tracking (separate from player's)
uint32_t mountFootstepLastAnimId_ = 0;
float mountFootstepLastNormTime_ = 0.0f;
bool mountFootstepNormInitialized_ = false;
// SFX transition state
bool sfxStateInitialized_ = false;
bool sfxPrevGrounded_ = true;
bool sfxPrevJumping_ = false;
bool sfxPrevFalling_ = false;
bool sfxPrevSwimming_ = false;
// Melee combat
bool stunned_ = false;
bool charging_ = false;
// ── Footstep event tracking (delegated to FootstepDriver) ────────────
FootstepDriver footstepDriver_;
// ── SFX transition state (delegated to SfxStateDriver) ───────────────
SfxStateDriver sfxStateDriver_;
// ── Melee combat (needs renderer for sequence queries) ───────────────
float meleeSwingTimer_ = 0.0f;
float meleeSwingCooldown_ = 0.0f;
float meleeAnimDurationMs_ = 0.0f;
uint32_t meleeAnimId_ = 0;
uint32_t specialAttackAnimId_ = 0; // Non-zero during special attack (overrides resolveMeleeAnimId)
uint32_t equippedWeaponInvType_ = 0;
bool equippedIs2HLoose_ = false; // Polearm or staff
bool equippedIsFist_ = false; // Fist weapon
bool equippedIsDagger_ = false; // Dagger (uses pierce variants)
bool equippedHasOffHand_ = false; // Has off-hand weapon (dual wield)
bool equippedHasShield_ = false; // Has shield equipped (for SHIELD_BASH)
bool meleeOffHandTurn_ = false; // Alternates main/off-hand swings
uint32_t specialAttackAnimId_ = 0;
WeaponLoadout weaponLoadout_;
bool meleeOffHandTurn_ = false;
// Ranged weapon state
RangedWeaponType equippedRangedType_ = RangedWeaponType::NONE;
float rangedShootTimer_ = 0.0f; // Countdown for ranged attack animation
uint32_t rangedAnimId_ = 0; // Cached ranged attack animation
// Mount animation capabilities (discovered at mount time, varies per model)
struct MountAnimSet {
uint32_t jumpStart = 0; // Jump start animation
uint32_t jumpLoop = 0; // Jump airborne loop
uint32_t jumpEnd = 0; // Jump landing
uint32_t rearUp = 0; // Rear-up / special flourish
uint32_t run = 0; // Run animation (discovered, don't assume)
uint32_t stand = 0; // Stand animation (discovered)
// Flight animations (discovered from mount model)
uint32_t flyIdle = 0;
uint32_t flyForward = 0;
uint32_t flyBackwards = 0;
uint32_t flyLeft = 0;
uint32_t flyRight = 0;
uint32_t flyUp = 0;
uint32_t flyDown = 0;
std::vector<uint32_t> fidgets; // Idle fidget animations (head turn, tail swish, etc.)
};
enum class MountAction { None, Jump, RearUp };
// ── Ranged weapon state ──────────────────────────────────────────────
float rangedShootTimer_ = 0.0f;
uint32_t rangedAnimId_ = 0;
// ── Mount state (discovery + positioning need renderer) ──────────────
uint32_t mountInstanceId_ = 0;
float mountHeightOffset_ = 0.0f;
float mountPitch_ = 0.0f; // Up/down tilt (radians)
float mountRoll_ = 0.0f; // Left/right banking (radians)
int mountSeatAttachmentId_ = -1; // -1 unknown, -2 unavailable
float mountPitch_ = 0.0f;
float mountRoll_ = 0.0f; // External roll for taxi flights (lean roll computed by MountFSM)
int mountSeatAttachmentId_ = -1;
glm::vec3 smoothedMountSeatPos_ = glm::vec3(0.0f);
bool mountSeatSmoothingInit_ = false;
float prevMountYaw_ = 0.0f; // Previous yaw for turn rate calculation (procedural lean)
float lastDeltaTime_ = 0.0f; // Cached for use in updateCharacterAnimation()
MountAction mountAction_ = MountAction::None; // Current mount action (jump/rear-up)
uint32_t mountActionPhase_ = 0; // 0=start, 1=loop, 2=end (for jump chaining)
MountAnimSet mountAnims_; // Cached animation IDs for current mount
float mountIdleFidgetTimer_ = 0.0f; // Timer for random idle fidgets
float mountIdleSoundTimer_ = 0.0f; // Timer for ambient idle sounds
uint32_t mountActiveFidget_ = 0; // Currently playing fidget animation ID (0 = none)
bool taxiFlight_ = false;
bool taxiAnimsLogged_ = false;
bool sprintAuraActive_ = false; // Sprint/Dash aura active → use SPRINT anim
bool sprintAuraActive_ = false;
// Private animation helpers
bool shouldTriggerFootstepEvent(uint32_t animationId, float animationTimeMs, float animationDurationMs);
audio::FootstepSurface resolveFootstepSurface() const;
// ── Private helpers ──────────────────────────────────────────────────
uint32_t resolveMeleeAnimId();
void updateMountedAnimation(float deltaTime);
void applyMountPositioning(float mountBob, float mountRoll, float characterYaw);
};
} // namespace rendering