refactor: extract spline math, consolidate packet parsing, decompose TransportManager

Extract CatmullRomSpline (include/math/spline.hpp, src/math/spline.cpp) as a
standalone, immutable, thread-safe spline module with O(log n) binary segment
search and fused position+tangent evaluation — replacing the duplicated O(n)
evalTimedCatmullRom/orientationFromTangent pair in TransportManager.

Consolidate 7 copies of spline packet parsing into shared functions in
game/spline_packet.{hpp,cpp}: parseMonsterMoveSplineBody (WotLK/TBC),
parseMonsterMoveSplineBodyVanilla, parseClassicMoveUpdateSpline,
parseWotlkMoveUpdateSpline, and decodePackedDelta. Named SplineFlag constants
replace magic hex literals throughout.

Extract TransportPathRepository (game/transport_path_repository.{hpp,cpp}) from
TransportManager — owns path data, DBC loading, and path inference. Paths stored
as PathEntry wrapping CatmullRomSpline + metadata (zOnly, fromDBC, worldCoords).
TransportManager reduced from ~1200 to ~500 lines, focused on transport lifecycle
and server sync.

Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
This commit is contained in:
Pavel Okhlopkov 2026-04-11 08:30:28 +03:00
parent 535cc20afe
commit de0383aa6b
32 changed files with 2198 additions and 1293 deletions

View file

@ -0,0 +1,108 @@
// include/game/spline_packet.hpp
// Consolidated spline packet parsing — replaces 7 duplicated parsing locations.
#pragma once
#include "network/packet.hpp"
#include <glm/glm.hpp>
#include <vector>
#include <cstdint>
namespace wowee::game {
/// Decoded spline data from a movement or MonsterMove packet.
struct SplineBlockData {
uint32_t splineFlags = 0;
uint32_t duration = 0;
// Animation (splineFlag 0x00400000)
bool hasAnimation = false;
uint8_t animationType = 0;
uint32_t animationStartTime = 0;
// Parabolic (splineFlag 0x00000800 for MonsterMove, 0x00000008 for MoveUpdate)
bool hasParabolic = false;
float verticalAcceleration = 0.0f;
uint32_t parabolicStartTime = 0;
// FINAL_POINT / FINAL_TARGET / FINAL_ANGLE (movement update only)
bool hasFinalPoint = false;
glm::vec3 finalPoint{0};
bool hasFinalTarget = false;
uint64_t finalTarget = 0;
bool hasFinalAngle = false;
float finalAngle = 0.0f;
// Timing (movement update only)
uint32_t timePassed = 0;
uint32_t splineId = 0;
// Waypoints (server coordinates, decoded from packed-delta if compressed)
std::vector<glm::vec3> waypoints;
glm::vec3 destination{0};
bool hasDest = false;
// SplineMode (movement update WotLK only)
uint8_t splineMode = 0;
glm::vec3 endPoint{0};
bool hasEndPoint = false;
};
// ── Spline flag constants ───────────────────────────────────────
namespace SplineFlag {
constexpr uint32_t FINAL_POINT = 0x00010000;
constexpr uint32_t FINAL_TARGET = 0x00020000;
constexpr uint32_t FINAL_ANGLE = 0x00040000;
constexpr uint32_t CATMULLROM = 0x00080000; // Uncompressed Catmull-Rom
constexpr uint32_t CYCLIC = 0x00100000; // Cyclic path
constexpr uint32_t ENTER_CYCLE = 0x00200000; // Entering cyclic path
constexpr uint32_t ANIMATION = 0x00400000; // Animation spline
constexpr uint32_t PARABOLIC_MM = 0x00000800; // Parabolic in MonsterMove
constexpr uint32_t PARABOLIC_MU = 0x00000008; // Parabolic in MoveUpdate
// Mask: if any of these are set, waypoints are uncompressed
constexpr uint32_t UNCOMPRESSED_MASK = CATMULLROM | CYCLIC | ENTER_CYCLE;
// TBC-era alternative for uncompressed check
constexpr uint32_t UNCOMPRESSED_MASK_TBC = CATMULLROM | 0x00002000;
} // namespace SplineFlag
/// Decode a single packed-delta waypoint.
/// Format: bits [0:10] = X (11-bit signed), [11:21] = Y (11-bit signed), [22:31] = Z (10-bit signed).
/// Each component is multiplied by 0.25 and subtracted from `midpoint`.
[[nodiscard]] glm::vec3 decodePackedDelta(uint32_t packed, const glm::vec3& midpoint);
/// Parse a MonsterMove spline body (after splineFlags has already been read).
/// Handles: Animation, duration, Parabolic, pointCount, compressed/uncompressed waypoints.
/// `startPos` is the creature's current position (needed for packed-delta midpoint calculation).
/// `splineFlags` is the already-read spline flags value.
/// `useTbcUncompressedMask`: if true, use 0x00080000|0x00002000 for uncompressed check (TBC format).
[[nodiscard]] bool parseMonsterMoveSplineBody(
network::Packet& packet,
SplineBlockData& out,
uint32_t splineFlags,
const glm::vec3& startPos,
bool useTbcUncompressedMask = false);
/// Parse a MonsterMove spline body where waypoints are always compressed (Vanilla format).
/// `startPos` is the creature's current position.
[[nodiscard]] bool parseMonsterMoveSplineBodyVanilla(
network::Packet& packet,
SplineBlockData& out,
uint32_t splineFlags,
const glm::vec3& startPos);
/// Parse a Classic/Turtle movement update spline block.
/// Format: splineFlags, FINAL_POINT/TARGET/ANGLE, timePassed, duration, splineId,
/// pointCount, uncompressed waypoints (12 bytes each), endPoint (no splineMode).
[[nodiscard]] bool parseClassicMoveUpdateSpline(
network::Packet& packet,
SplineBlockData& out);
/// Parse a WotLK movement update spline block.
/// Format: splineFlags, FINAL_POINT/TARGET/ANGLE, timePassed, duration, splineId,
/// then WotLK header (durationMod, durationModNext, [Animation], [Parabolic],
/// pointCount, splineMode, endPoint) with multi-strategy fallback.
[[nodiscard]] bool parseWotlkMoveUpdateSpline(
network::Packet& packet,
SplineBlockData& out,
const glm::vec3& entityPos = glm::vec3(0));
} // namespace wowee::game

View file

@ -1,5 +1,6 @@
#pragma once
#include "game/transport_path_repository.hpp"
#include <cstdint>
#include <vector>
#include <unordered_map>
@ -18,21 +19,6 @@ namespace wowee::pipeline {
namespace wowee::game {
struct TimedPoint {
uint32_t tMs; // Time in milliseconds from DBC
glm::vec3 pos; // Position at this time
};
struct TransportPath {
uint32_t pathId;
std::vector<TimedPoint> points; // Time-indexed waypoints (includes duplicate first point at end for wrap)
bool looping; // Set to false after adding explicit wrap point
uint32_t durationMs; // Total loop duration in ms (includes wrap segment if added)
bool zOnly; // True if path only has Z movement (elevator/bobbing), false if real XY travel
bool fromDBC; // True if loaded from TransportAnimation.dbc, false for runtime fallback/custom paths
bool worldCoords = false; // True if points are absolute world coords (TaxiPathNode), not local offsets
};
struct ActiveTransport {
uint64_t guid; // Entity GUID
uint32_t wmoInstanceId; // WMO renderer instance ID
@ -137,13 +123,12 @@ public:
private:
void updateTransportMovement(ActiveTransport& transport, float deltaTime);
glm::vec3 evalTimedCatmullRom(const TransportPath& path, uint32_t pathTimeMs);
glm::quat orientationFromTangent(const TransportPath& path, uint32_t pathTimeMs);
void updateTransformMatrices(ActiveTransport& transport);
/// Legacy transport orientation from tangent (preserves original cross-product order).
static glm::quat orientationFromSplineTangent(const glm::vec3& tangent);
TransportPathRepository pathRepo_;
std::unordered_map<uint64_t, ActiveTransport> transports_;
std::unordered_map<uint32_t, TransportPath> paths_; // Indexed by transportEntry (pathId from TransportAnimation.dbc)
std::unordered_map<uint32_t, TransportPath> taxiPaths_; // Indexed by TaxiPath.dbc ID (world-coord paths for MO_TRANSPORT)
rendering::WMORenderer* wmoRenderer_ = nullptr;
rendering::M2Renderer* m2Renderer_ = nullptr;
bool clientSideAnimation_ = false; // DISABLED - use server positions instead of client prediction

View file

@ -0,0 +1,67 @@
// include/game/transport_path_repository.hpp
// Owns and manages transport path data — DBC, taxi, and custom paths.
// Uses CatmullRomSpline for spline evaluation (replaces duplicated evalTimedCatmullRom).
// Separated from TransportManager for SOLID-S (single responsibility).
#pragma once
#include "math/spline.hpp"
#include <cstdint>
#include <vector>
#include <unordered_map>
#include <glm/glm.hpp>
namespace wowee::pipeline {
class AssetManager;
}
namespace wowee::game {
/// Metadata + CatmullRomSpline for a transport path.
struct PathEntry {
math::CatmullRomSpline spline;
uint32_t pathId = 0;
bool zOnly = false; // Elevator/bobbing — no meaningful XY travel
bool fromDBC = false; // Loaded from TransportAnimation.dbc
bool worldCoords = false; // TaxiPathNode absolute world positions (not local offsets)
PathEntry(math::CatmullRomSpline s, uint32_t id, bool zo, bool dbc, bool wc)
: spline(std::move(s)), pathId(id), zOnly(zo), fromDBC(dbc), worldCoords(wc) {}
};
/// Owns and manages transport path data.
class TransportPathRepository {
public:
TransportPathRepository() = default;
// ── DBC loading ─────────────────────────────────────────
bool loadTransportAnimationDBC(pipeline::AssetManager* assetMgr);
bool loadTaxiPathNodeDBC(pipeline::AssetManager* assetMgr);
// ── Path construction ───────────────────────────────────
void loadPathFromNodes(uint32_t pathId, const std::vector<glm::vec3>& waypoints,
bool looping = true, float speed = 18.0f);
// ── Lookup ──────────────────────────────────────────────
const PathEntry* findPath(uint32_t pathId) const;
const PathEntry* findTaxiPath(uint32_t taxiPathId) const;
bool hasPathForEntry(uint32_t entry) const;
bool hasTaxiPath(uint32_t taxiPathId) const;
// ── Query ───────────────────────────────────────────────
bool hasUsableMovingPathForEntry(uint32_t entry, float minXYRange = 1.0f) const;
uint32_t inferDbcPathForSpawn(const glm::vec3& spawnWorldPos, float maxDistance,
bool allowZOnly) const;
uint32_t inferMovingPathForSpawn(const glm::vec3& spawnWorldPos,
float maxDistance = 1200.0f) const;
uint32_t pickFallbackMovingPath(uint32_t entry, uint32_t displayId) const;
// ── Mutation ─────────────────────────────────────────────
/// Store or overwrite a path entry (used by assignTaxiPathToTransport).
void storePath(uint32_t pathId, PathEntry entry);
private:
std::unordered_map<uint32_t, PathEntry> paths_;
std::unordered_map<uint32_t, PathEntry> taxiPaths_;
};
} // namespace wowee::game