Files
shadow-over-bishkek001/src/CharacterState.h
T

228 lines
8.8 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <unordered_map>
#include <array>
#include <cstdint>
#include <memory>
#include <Eigen/Core>
#include <Eigen/Geometry>
#include "external/nlohmann/json.hpp"
#include "ISaveable.h"
#include "navigation/PathFinder.h"
namespace FRG {
constexpr float VISIBLE_RANGE = 6.f;
// Distance at which a character starts attacking its target.
constexpr float kAttackRange = 1.25f;
constexpr float kJumpAttackRange = 5.0f;
// How close the player must be to a peaceful NPC to start a conversation.
constexpr float kNpcTalkDistance = 1.35f;
struct CharacterObservation;
struct CharacterUpdateContext;
struct CharacterUpdateEvents;
struct LocationNetworkState;
enum class AnimationState {
STAND = 0,
WALK = 1,
STAND_TO_ACTION = 2,
ACTION_ATTACK = 3,
ACTION_IDLE = 4,
ACTION_TO_STAND = 5,
ACTION_TO_DEATH = 6,
DEATH_IDLE = 7,
ACTION_ATTACK_JUMP = 8,
ACTION_ATTACK_POWER = 9,
};
// Paths and fixed parameters needed to recreate a Character's visual assets
// from scratch (e.g. after loading a saved game).
struct CharacterCreationInfo {
// Each entry is (AnimationState, filepath). Binary .anim format is auto-detected.
std::vector<std::pair<AnimationState, std::string>> animationPaths;
// Per-mesh texture paths (mesh name → path); takes priority over singleTexturePath.
std::unordered_map<std::string, std::string> meshTexturePaths;
// Single shared texture path (used when setTexture(shared_ptr) was called on all meshes).
std::string singleTexturePath;
};
// All serializable authoritative state for a character. GameState or
// LocationGameState owns it; rendering-only data lives in Character.
class CharacterState : public ISaveable {
public:
// Distance below which the character is considered to have reached its target.
static constexpr float kWalkThreshold = 0.05f;
static fp kWalkThreshold_SQUARED() { return static_cast<fp>(kWalkThreshold * kWalkThreshold); };
// --- Identity ---
std::string npcName;
bool isPlayer = false;
bool enabled = false;
bool canAttack = false;
// --- Position / orientation ---
Vec3fp position;
fp facingAngle{0};
fp targetFacingAngle{0};
// --- Health ---
float hp = 200.f;
float initialHp = 200.f;
fp visibleRange = static_cast<fp>(VISIBLE_RANGE);
// --- Combat flags ---
int battle_state = 0;
bool resetAnim = false;
int attack = 0;
AnimationState currentState = AnimationState::STAND;
float attack_cooldown = 0.f;
// Jump attack is selected by an ordinary hostile-target command, but all
// timing and movement are authoritative simulation state.
bool jumpAttackUnlocked = false;
bool jumpAttackPending = false;
bool jumpAttackActive = false;
bool jumpAttackImpactConsumed = false;
int jumpAttackCooldownTicks = 0;
int jumpAttackAnimationSubframe = 0; // 1000 units per animation frame.
std::string jumpAttackTargetId;
Vec3fp jumpAttackStartPosition;
Vec3fp jumpAttackLandingPosition;
bool powerAttackPending = false;
int powerAttackCooldownTicks = 0;
std::string powerAttackTargetId;
// Weapon visibility toggle (changes during animation)
bool showWeapon = true;
// --- Stable character references ---
// Empty means no target. "player1" is the campaign player; NPC IDs are
// location-local and validated while loading location content.
std::string attackTargetId;
std::string faceTargetId;
// --- Home position for drift recovery ---
Vec3fp homePosition;
// --- Authoritative logical animation clock ---
// Frame counts are immutable metadata copied from loaded clips during
// character construction. Simulation advances animationFrame itself.
float animationFrame = 0.0f;
std::array<int, 10> animationStartFrames = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
std::array<int, 10> animationFrameCounts = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
std::array<float, 10> animationPlaybackRates = {{
1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f}};
// --- Movement / visual parameters ---
fp walkSpeed = static_cast<fp>(3.0f);
fp rotationSpeed = static_cast<fp>(4.0f);
float modelScale = 0.12f;
Eigen::Quaternionf modelCorrectionRotation = Eigen::Quaternionf::Identity();
//fp collisionRadius = static_cast<fp>(0.4f); // Радиус капсулы персонажа
fp collisionRadius = static_cast<fp>(kCharacterAgentRadius); // Радиус капсулы персонажа
// Векторы скорости в плоскости XZ (сериализовать в JSON не обязательно, если считаются каждый тик,
// но для строгого lockstep-снапшота velocity лучше сохранять)
Vec2fp velocity{ fp(0), fp(0) }; // Реальная скорость с прошлого тика (нужна соседям в ORCA)
Vec2fp preferredVelocity{ fp(0), fp(0) }; // Желаемая скорость к waypoint'у на текущем тике
fp interactionRadius = static_cast<fp>(0.0f);
// --- Pathfinding data (serializable parts) ---
Vec3fp walkTarget;
Vec3fp requestedWalkTarget;
std::vector<Vec3fp> pathWaypoints;
size_t currentWaypointIndex = 0;
PathFinder::PathOutcome navigationOutcome =
PathFinder::PathOutcome::ReachedExactGoal;
// Internal drift-check timer (serializable so it survives save/load).
fp homeDriftCheckTimer = static_cast<fp>(5.0f);
int navigationRetryTicks = 0;
std::vector<int> navigationRetryCooldownTicks;
std::size_t navigationRetryCooldownIndex = 0;
// Global Lua function called when the character reaches the final waypoint.
// The name is serialized, so no runtime callback needs to be rehydrated.
std::string onArrivedCallbackName;
// --- Asset / creation info (used to reload visuals from a saved state) ---
CharacterCreationInfo creationInfo;
// --- Pure-state methods ---
void configureAsPlayer();
void configureNavigationRetryCooldowns(
const std::string& entityId,
std::vector<int> cooldownTicks = {});
bool isMoving() const;
PathFinder::PathOutcome getNavigationOutcome() const { return navigationOutcome; }
Vec3fp getCurrentNavigationTarget() const;
void stopInPlace();
void setTarget(const Vec3fp& target,
std::string callbackName,
const LocationNetworkState& location);
void forceReplan(const LocationNetworkState& location);
void stopAndRotateToTarget(std::string characterId);
void leaveBattleState();
void resetPlayerAfterDeath();
bool tryPowerAttack(const std::string& targetId);
bool reserveJumpAttack(const std::string& targetId);
void cancelPendingJumpAttack();
void beginJumpAttack(const Vec3fp& landingPosition);
void setHp(float newHp);
CharacterUpdateEvents update(std::int64_t deltaMs, const CharacterUpdateContext& context);
void setAnimationMetadata(AnimationState state, int startFrame, int frameCount);
// --- Serialisation (runtime mutable fields only; creation info is reloaded from config) ---
void save(nlohmann::json& out) const override;
void load(const nlohmann::json& in) override;
private:
void scheduleNextNavigationRetry();
// Queries the active navigation map for a path to `target` and applies it.
void applyPathTo(const Vec3fp& target, const LocationNetworkState& location);
// Points targetFacingAngle at `faceTarget`, or clears a stale faceTargetId.
void updateFaceTarget(const CharacterObservation* faceTarget);
};
using PlayerStateMap =
std::unordered_map<std::string, std::shared_ptr<CharacterState>>;
struct CharacterObservation {
Vec3fp position;
float hp = 0.f;
};
struct CharacterUpdateContext {
const CharacterObservation* attackTarget = nullptr;
const CharacterObservation* faceTarget = nullptr;
};
struct CharacterUpdateEvents {
bool attackImpact = false;
bool jumpAttackImpact = false;
bool powerAttackImpact = false;
bool deathAnimationComplete = false;
bool navigationRequested = false;
bool forceNavigationReplan = false;
Vec3fp navigationTarget;
std::string arrivedCallbackName;
std::string attackTargetId;
Vec3fp attackDirection;
float attackDamage = 10.f;
};
} // namespace FRG