Kelsidavis-WoWee/include/rendering/animation/activity_fsm.hpp
Paul b4989dc11f 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).
2026-04-05 12:27:35 +03:00

110 lines
3.9 KiB
C++

#pragma once
#include "rendering/animation/anim_capability_set.hpp"
#include "rendering/animation/anim_event.hpp"
#include <cstdint>
#include <string>
namespace wowee {
namespace rendering {
// ============================================================================
// ActivityFSM
//
// Pure logic state machine for non-combat activities: emote, loot, sit/sleep/kneel.
// Sit chain (down → loop → up) auto-advances on one-shot completion.
// All activities cancel on movement.
// ============================================================================
class ActivityFSM {
public:
enum class State : uint8_t {
NONE,
EMOTE,
LOOTING, // One-shot LOOT anim
LOOT_KNEELING, // KNEEL_LOOP until loot window closes
LOOT_END, // One-shot KNEEL_END exit anim
SIT_DOWN,
SITTING,
SIT_UP,
};
struct Input {
bool moving = false;
bool sprinting = false;
bool jumping = false;
bool grounded = true;
bool swimming = false;
bool sitting = false; // Camera controller sitting state
bool stunned = false;
// Animation state query for one-shot completion detection
uint32_t currentAnimId = 0;
float currentAnimTime = 0.0f;
float currentAnimDuration = 0.0f;
bool haveAnimState = false;
};
void onEvent(AnimEvent event);
/// Evaluate current state against input and capabilities.
AnimOutput resolve(const Input& in, const AnimCapabilitySet& caps);
State getState() const { return state_; }
void setState(State s) { state_ = s; }
bool isActive() const { return state_ != State::NONE; }
void reset();
// ── Emote management ────────────────────────────────────────────────
void startEmote(uint32_t animId, bool loop);
void cancelEmote();
bool isEmoteActive() const { return emoteActive_; }
uint32_t getEmoteAnimId() const { return emoteAnimId_; }
// ── Sit/sleep/kneel management ──────────────────────────────────────
// WoW UnitStandStateType constants
static constexpr uint8_t STAND_STATE_STAND = 0;
static constexpr uint8_t STAND_STATE_SIT = 1;
static constexpr uint8_t STAND_STATE_SIT_CHAIR = 2;
static constexpr uint8_t STAND_STATE_SLEEP = 3;
static constexpr uint8_t STAND_STATE_SIT_LOW = 4;
static constexpr uint8_t STAND_STATE_SIT_MED = 5;
static constexpr uint8_t STAND_STATE_SIT_HIGH = 6;
static constexpr uint8_t STAND_STATE_DEAD = 7;
static constexpr uint8_t STAND_STATE_KNEEL = 8;
void setStandState(uint8_t standState);
uint8_t getStandState() const { return standState_; }
// ── Loot management ─────────────────────────────────────────────────
void startLooting();
void stopLooting();
static constexpr uint8_t PRIORITY = 30;
private:
State state_ = State::NONE;
// Emote state
bool emoteActive_ = false;
uint32_t emoteAnimId_ = 0;
bool emoteLoop_ = false;
// Sit/sleep/kneel transition animations
uint8_t standState_ = 0;
uint32_t sitDownAnim_ = 0;
uint32_t sitLoopAnim_ = 0;
uint32_t sitUpAnim_ = 0;
bool sitDownAnimSeen_ = false; // Track whether one-shot has started playing
bool sitUpAnimSeen_ = false;
uint8_t sitDownFrames_ = 0; // Frames spent in SIT_DOWN (for safety timeout)
uint8_t sitUpFrames_ = 0; // Frames spent in SIT_UP
bool lootAnimSeen_ = false;
uint8_t lootFrames_ = 0;
bool lootEndAnimSeen_ = false;
uint8_t lootEndFrames_ = 0;
void updateTransitions(const Input& in);
bool oneShotComplete(const Input& in, uint32_t expectedAnimId) const;
};
} // namespace rendering
} // namespace wowee