mirror of
https://github.com/araxiaonline/TrinityCore.git
synced 2026-06-13 03:32:28 -04:00
- Fix waypoint markers showing as human models in 11.2.5 client - Use Elven Wisp (displayId 1824) which renders correctly - Add runtime display config via Lua shared data (no recompile needed) - Add client refresh (DestroyForNearbyPlayers + UpdateObjectVisibilityOnCreate) - Graceful MySQL error handling (no crash on bad MCP queries) - Fix displayId arg parsing in VisualizeWaypointPath - Add documentation for display IDs and configuration
416 lines
13 KiB
C++
416 lines
13 KiB
C++
/*
|
|
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License as published by the
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
* option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
* more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "WaypointManager.h"
|
|
#include "DatabaseEnv.h"
|
|
#include "GridDefines.h"
|
|
#include "Log.h"
|
|
#include "MapUtils.h"
|
|
#include "ObjectAccessor.h"
|
|
#include "Optional.h"
|
|
#include "QueryResultStructured.h"
|
|
#include "TemporarySummon.h"
|
|
#include "Unit.h"
|
|
|
|
DEFINE_FIELD_ACCESSOR_CACHE(WaypointMgr::, PathQueryResult, PreparedResultSet, (PathId)(MoveType)(Flags)(Velocity));
|
|
DEFINE_FIELD_ACCESSOR_CACHE(WaypointMgr::, PathNodeQueryResult, PreparedResultSet, (PathId)(NodeId)(PositionX)(PositionY)(PositionZ)(Orientation)(Delay));
|
|
|
|
WaypointMgr::WaypointMgr() = default;
|
|
WaypointMgr::~WaypointMgr() = default;
|
|
|
|
void WaypointMgr::LoadPaths()
|
|
{
|
|
_LoadPaths();
|
|
_LoadPathNodes();
|
|
DoPostLoadingChecks();
|
|
}
|
|
|
|
void WaypointMgr::_LoadPaths()
|
|
{
|
|
uint32 oldMSTime = getMSTime();
|
|
|
|
_pathStore.clear();
|
|
|
|
WorldDatabasePreparedStatement* stmt = WorldDatabase.GetPreparedStatement(WORLD_SEL_WAYPOINT_PATH);
|
|
stmt->setUInt32(0, 0);
|
|
stmt->setUInt32(1, 1);
|
|
|
|
PreparedQueryResult result = WorldDatabase.Query(stmt);
|
|
|
|
if (!result)
|
|
{
|
|
TC_LOG_INFO("server.loading", ">> Loaded 0 waypoint paths. DB table `waypoint_path` is empty!");
|
|
return;
|
|
}
|
|
|
|
uint32 count = 0;
|
|
|
|
do
|
|
{
|
|
LoadPathFromDB(*result);
|
|
++count;
|
|
} while (result->NextRow());
|
|
|
|
TC_LOG_INFO("server.loading", ">> Loaded {} waypoint paths in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
|
|
}
|
|
|
|
void WaypointMgr::_LoadPathNodes()
|
|
{
|
|
uint32 oldMSTime = getMSTime();
|
|
|
|
WorldDatabasePreparedStatement* stmt = WorldDatabase.GetPreparedStatement(WORLD_SEL_WAYPOINT_PATH_NODE);
|
|
stmt->setUInt32(0, 0);
|
|
stmt->setUInt32(1, 1);
|
|
|
|
PreparedQueryResult result = WorldDatabase.Query(stmt);
|
|
|
|
if (!result)
|
|
{
|
|
TC_LOG_INFO("server.loading", ">> Loaded 0 waypoint path nodes. DB table `waypoint_path_node` is empty!");
|
|
return;
|
|
}
|
|
|
|
uint32 count = 0;
|
|
|
|
do
|
|
{
|
|
LoadPathNodesFromDB(*result);
|
|
++count;
|
|
}
|
|
while (result->NextRow());
|
|
|
|
TC_LOG_INFO("server.loading", ">> Loaded {} waypoint path nodes in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
|
|
}
|
|
|
|
void WaypointMgr::LoadPathFromDB(PathQueryResult const& fields)
|
|
{
|
|
uint32 pathId = fields.PathId().GetUInt32();
|
|
|
|
WaypointPath& path = _pathStore[pathId];
|
|
|
|
path.MoveType = WaypointMoveType(fields.MoveType().GetUInt8());
|
|
if (path.MoveType >= WaypointMoveType::Max)
|
|
{
|
|
TC_LOG_ERROR("sql.sql", "PathId {} in `waypoint_path` has invalid MoveType {}, ignoring", pathId, AsUnderlyingType(path.MoveType));
|
|
return;
|
|
}
|
|
|
|
path.Id = pathId;
|
|
path.Flags = WaypointPathFlags(fields.Flags().GetUInt8());
|
|
path.Velocity = fields.Velocity().GetFloatOrNull();
|
|
|
|
if (path.Velocity && *path.Velocity <= 0.0f)
|
|
{
|
|
TC_LOG_ERROR("sql.sql", "PathId {} in `waypoint_path` has invalid velocity {}, using default velocity instead", pathId, *path.Velocity);
|
|
path.Velocity.reset();
|
|
}
|
|
|
|
path.Nodes.clear();
|
|
}
|
|
|
|
void WaypointMgr::LoadPathNodesFromDB(PathNodeQueryResult const& fields)
|
|
{
|
|
uint32 pathId = fields.PathId().GetUInt32();
|
|
|
|
WaypointPath* path = Trinity::Containers::MapGetValuePtr(_pathStore, pathId);
|
|
if (!path)
|
|
{
|
|
TC_LOG_ERROR("sql.sql", "PathId {} in `waypoint_path_node` does not exist in `waypoint_path`, ignoring", pathId);
|
|
return;
|
|
}
|
|
|
|
float x = fields.PositionX().GetFloat();
|
|
float y = fields.PositionY().GetFloat();
|
|
float z = fields.PositionZ().GetFloat();
|
|
Optional<float> o = fields.Orientation().GetFloatOrNull();
|
|
|
|
Optional<Milliseconds> delay;
|
|
if (uint32 delayMs = fields.Delay().GetUInt32())
|
|
delay.emplace(delayMs);
|
|
|
|
Trinity::NormalizeMapCoord(x);
|
|
Trinity::NormalizeMapCoord(y);
|
|
|
|
path->Nodes.emplace_back(fields.NodeId().GetUInt32(), x, y, z, o, delay);
|
|
}
|
|
|
|
void WaypointMgr::DoPostLoadingChecks()
|
|
{
|
|
for (auto& [pathId, pathInfo] : _pathStore)
|
|
{
|
|
pathInfo.BuildSegments();
|
|
|
|
if (pathInfo.Nodes.empty())
|
|
TC_LOG_ERROR("sql.sql", "PathId {} in `waypoint_path` has no assigned nodes in `waypoint_path_node`", pathInfo.Id);
|
|
|
|
if (pathInfo.Flags.HasFlag(WaypointPathFlags::FollowPathBackwardsFromEndToStart) && pathInfo.Nodes.size() < WAYPOINT_PATH_FLAG_FOLLOW_PATH_BACKWARDS_MINIMUM_NODES)
|
|
TC_LOG_ERROR("sql.sql", "PathId {} in `waypoint_path` has FollowPathBackwardsFromEndToStart set, but only {} nodes, requires {}", pathInfo.Id, pathInfo.Nodes.size(), WAYPOINT_PATH_FLAG_FOLLOW_PATH_BACKWARDS_MINIMUM_NODES);
|
|
}
|
|
}
|
|
|
|
WaypointMgr* WaypointMgr::instance()
|
|
{
|
|
static WaypointMgr instance;
|
|
return &instance;
|
|
}
|
|
|
|
void WaypointMgr::ReloadPath(uint32 pathId)
|
|
{
|
|
// waypoint_path
|
|
{
|
|
WorldDatabasePreparedStatement* stmt = WorldDatabase.GetPreparedStatement(WORLD_SEL_WAYPOINT_PATH);
|
|
stmt->setUInt32(0, pathId);
|
|
stmt->setUInt32(1, 0);
|
|
|
|
PreparedQueryResult result = WorldDatabase.Query(stmt);
|
|
|
|
if (!result)
|
|
{
|
|
TC_LOG_ERROR("sql.sql", "PathId {} in `waypoint_path` not found, ignoring", pathId);
|
|
return;
|
|
}
|
|
|
|
do
|
|
{
|
|
LoadPathFromDB(*result);
|
|
} while (result->NextRow());
|
|
}
|
|
|
|
// waypoint_path_data
|
|
{
|
|
WorldDatabasePreparedStatement* stmt = WorldDatabase.GetPreparedStatement(WORLD_SEL_WAYPOINT_PATH_NODE);
|
|
stmt->setUInt32(0, pathId);
|
|
stmt->setUInt32(1, 0);
|
|
|
|
PreparedQueryResult result = WorldDatabase.Query(stmt);
|
|
|
|
if (!result)
|
|
{
|
|
TC_LOG_ERROR("sql.sql", "PathId {} in `waypoint_path_node` not found, ignoring", pathId);
|
|
return;
|
|
}
|
|
|
|
do
|
|
{
|
|
LoadPathNodesFromDB(*result);
|
|
} while (result->NextRow());
|
|
|
|
if (WaypointPath* path = Trinity::Containers::MapGetValuePtr(_pathStore, pathId))
|
|
path->BuildSegments();
|
|
}
|
|
}
|
|
|
|
void WaypointMgr::VisualizePath(Unit* owner, WaypointPath const* path, Optional<uint32> displayId)
|
|
{
|
|
for (WaypointNode const& node : path->Nodes)
|
|
{
|
|
std::pair<uint32, uint32> pathNodePair(path->Id, node.Id);
|
|
|
|
auto itr = _nodeToVisualWaypointGUIDsMap.find(pathNodePair);
|
|
if (itr != _nodeToVisualWaypointGUIDsMap.end())
|
|
{
|
|
// Validate that the tracked creature still exists
|
|
Creature* existingMarker = ObjectAccessor::GetCreature(*owner, itr->second);
|
|
if (existingMarker)
|
|
continue; // Marker exists, skip
|
|
|
|
// Marker is gone (stale entry), clean up and respawn
|
|
_visualWaypointGUIDToNodeMap.erase(itr->second);
|
|
_nodeToVisualWaypointGUIDsMap.erase(pathNodePair);
|
|
}
|
|
|
|
TempSummon* summon = owner->SummonCreature(VISUAL_WAYPOINT, node.X, node.Y, node.Z, node.Orientation ? *node.Orientation : 0.0f);
|
|
if (!summon)
|
|
continue;
|
|
|
|
// Always set display - use passed displayId or default to Elven Wisp (1824)
|
|
constexpr uint32 DEFAULT_WAYPOINT_DISPLAY = 1824; // Elven Wisp - works in 11.2.5
|
|
summon->SetDisplayId(displayId.value_or(DEFAULT_WAYPOINT_DISPLAY), true);
|
|
summon->SetObjectScale(0.5f);
|
|
|
|
// Force client to refresh the creature's appearance
|
|
// (SetDisplayId alone doesn't update already-spawned creatures on client)
|
|
summon->DestroyForNearbyPlayers();
|
|
summon->UpdateObjectVisibilityOnCreate();
|
|
|
|
_nodeToVisualWaypointGUIDsMap[pathNodePair] = summon->GetGUID();
|
|
_visualWaypointGUIDToNodeMap[summon->GetGUID()] = std::pair<WaypointPath const*, WaypointNode const*>(path, &node);
|
|
}
|
|
}
|
|
|
|
void WaypointMgr::DevisualizePath(Unit* owner, WaypointPath const* path)
|
|
{
|
|
for (WaypointNode const& node : path->Nodes)
|
|
{
|
|
std::pair<uint32, uint32> pathNodePair(path->Id, node.Id);
|
|
auto itr = _nodeToVisualWaypointGUIDsMap.find(pathNodePair);
|
|
if (itr == _nodeToVisualWaypointGUIDsMap.end())
|
|
continue;
|
|
|
|
// Always clean up the maps, even if creature is already gone
|
|
ObjectGuid markerGuid = itr->second;
|
|
_visualWaypointGUIDToNodeMap.erase(markerGuid);
|
|
_nodeToVisualWaypointGUIDsMap.erase(pathNodePair);
|
|
|
|
// Try to despawn the creature if it still exists
|
|
if (owner)
|
|
{
|
|
if (Creature* creature = ObjectAccessor::GetCreature(*owner, markerGuid))
|
|
creature->DespawnOrUnsummon();
|
|
}
|
|
}
|
|
}
|
|
|
|
void WaypointMgr::ClearAllVisualizations(Unit* owner)
|
|
{
|
|
// Despawn all tracked visual waypoint creatures and clear tracking maps
|
|
for (auto const& pair : _nodeToVisualWaypointGUIDsMap)
|
|
{
|
|
if (owner)
|
|
{
|
|
if (Creature* creature = ObjectAccessor::GetCreature(*owner, pair.second))
|
|
creature->DespawnOrUnsummon();
|
|
}
|
|
}
|
|
|
|
_nodeToVisualWaypointGUIDsMap.clear();
|
|
_visualWaypointGUIDToNodeMap.clear();
|
|
}
|
|
|
|
bool WaypointMgr::IsPathVisualized(uint32 pathId) const
|
|
{
|
|
for (auto const& pair : _nodeToVisualWaypointGUIDsMap)
|
|
{
|
|
if (pair.first.first == pathId)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void WaypointMgr::MoveNode(WaypointPath const* path, WaypointNode const* node, Position const& pos)
|
|
{
|
|
WorldDatabasePreparedStatement* stmt = WorldDatabase.GetPreparedStatement(WORLD_UPD_WAYPOINT_PATH_NODE_POSITION);
|
|
stmt->setFloat(0, pos.GetPositionX());
|
|
stmt->setFloat(1, pos.GetPositionY());
|
|
stmt->setFloat(2, pos.GetPositionZ());
|
|
stmt->setFloat(3, pos.GetOrientation());
|
|
stmt->setUInt32(4, path->Id);
|
|
stmt->setUInt32(5, node->Id);
|
|
WorldDatabase.Execute(stmt);
|
|
}
|
|
|
|
void WaypointMgr::DeleteNode(WaypointPath const* path, WaypointNode const* node)
|
|
{
|
|
WorldDatabasePreparedStatement* stmt = WorldDatabase.GetPreparedStatement(WORLD_DEL_WAYPOINT_PATH_NODE);
|
|
stmt->setUInt32(0, path->Id);
|
|
stmt->setUInt32(1, node->Id);
|
|
WorldDatabase.Execute(stmt);
|
|
|
|
stmt = WorldDatabase.GetPreparedStatement(WORLD_UPD_WAYPOINT_PATH_NODE);
|
|
stmt->setUInt32(0, path->Id);
|
|
stmt->setUInt32(1, node->Id);
|
|
WorldDatabase.Execute(stmt);
|
|
}
|
|
|
|
void WaypointMgr::DeleteNode(uint32 pathId, uint32 nodeId)
|
|
{
|
|
WaypointPath const* path = GetPath(pathId);
|
|
if (!path)
|
|
return;
|
|
|
|
WaypointNode const* node = GetNode(path, nodeId);
|
|
if (!node)
|
|
return;
|
|
|
|
DeleteNode(path, node);
|
|
}
|
|
|
|
WaypointPath const* WaypointMgr::GetPath(uint32 pathId) const
|
|
{
|
|
return Trinity::Containers::MapGetValuePtr(_pathStore, pathId);
|
|
}
|
|
|
|
WaypointNode const* WaypointMgr::GetNode(WaypointPath const* path, uint32 nodeId) const
|
|
{
|
|
for (WaypointNode const& node : path->Nodes)
|
|
{
|
|
if (node.Id == nodeId)
|
|
return &node;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
WaypointNode const* WaypointMgr::GetNode(uint32 pathId, uint32 nodeId) const
|
|
{
|
|
WaypointPath const* path = GetPath(pathId);
|
|
if (!path)
|
|
return nullptr;
|
|
|
|
return GetNode(path, nodeId);
|
|
}
|
|
|
|
WaypointPath const* WaypointMgr::GetPathByVisualGUID(ObjectGuid guid) const
|
|
{
|
|
auto itr = _visualWaypointGUIDToNodeMap.find(guid);
|
|
if (itr == _visualWaypointGUIDToNodeMap.end())
|
|
return nullptr;
|
|
|
|
return itr->second.first;
|
|
}
|
|
|
|
WaypointNode const* WaypointMgr::GetNodeByVisualGUID(ObjectGuid guid) const
|
|
{
|
|
auto itr = _visualWaypointGUIDToNodeMap.find(guid);
|
|
if (itr == _visualWaypointGUIDToNodeMap.end())
|
|
return nullptr;
|
|
|
|
return itr->second.second;
|
|
}
|
|
|
|
ObjectGuid const& WaypointMgr::GetVisualGUIDByNode(uint32 pathId, uint32 nodeId) const
|
|
{
|
|
auto itr = _nodeToVisualWaypointGUIDsMap.find(std::make_pair(pathId, nodeId));
|
|
if (itr == _nodeToVisualWaypointGUIDsMap.end())
|
|
return ObjectGuid::Empty;
|
|
|
|
return itr->second;
|
|
}
|
|
|
|
void WaypointPath::BuildSegments()
|
|
{
|
|
ContinuousSegments.assign(1, { 0, 0 });
|
|
for (std::size_t i = 0; i < Nodes.size(); ++i)
|
|
{
|
|
++ContinuousSegments.back().second;
|
|
|
|
// split on delay
|
|
if (i + 1 != Nodes.size() && Nodes[i].Delay)
|
|
ContinuousSegments.emplace_back(i, 1);
|
|
}
|
|
}
|
|
|
|
bool WaypointMgr::GetPathAndNodeByVisualGUID(ObjectGuid guid, uint32& outPathId, uint32& outNodeId) const
|
|
{
|
|
auto itr = _visualWaypointGUIDToNodeMap.find(guid);
|
|
if (itr == _visualWaypointGUIDToNodeMap.end())
|
|
return false;
|
|
|
|
outPathId = itr->second.first->Id;
|
|
outNodeId = itr->second.second->Id;
|
|
return true;
|
|
}
|