TrinityCore
WaypointManager.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "WaypointManager.h"
19#include "DatabaseEnv.h"
20#include "GridDefines.h"
21#include "Log.h"
22#include "MapUtils.h"
23#include "ObjectAccessor.h"
24#include "Optional.h"
25#include "TemporarySummon.h"
26#include "Unit.h"
27
29{
30 _LoadPaths();
33}
34
36{
37 uint32 oldMSTime = getMSTime();
38
39 _pathStore.clear();
40
41 // 0 1 2
42 QueryResult result = WorldDatabase.Query("SELECT PathId, MoveType, Flags FROM waypoint_path");
43
44 if (!result)
45 {
46 TC_LOG_INFO("server.loading", ">> Loaded 0 waypoint paths. DB table `waypoint_path` is empty!");
47 return;
48 }
49
50 uint32 count = 0;
51
52 do
53 {
54 LoadPathFromDB(result->Fetch());
55 ++count;
56 } while (result->NextRow());
57
58 TC_LOG_INFO("server.loading", ">> Loaded {} waypoint paths in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
59}
60
62{
63 uint32 oldMSTime = getMSTime();
64 // 0 1 2 3 4 5 6
65 QueryResult result = WorldDatabase.Query("SELECT PathId, NodeId, PositionX, PositionY, PositionZ, Orientation, Delay FROM waypoint_path_node ORDER BY PathId, NodeId");
66
67 if (!result)
68 {
69 TC_LOG_INFO("server.loading", ">> Loaded 0 waypoint path nodes. DB table `waypoint_path_node` is empty!");
70 return;
71 }
72
73 uint32 count = 0;
74
75 do
76 {
77 LoadPathNodesFromDB(result->Fetch());
78 ++count;
79 }
80 while (result->NextRow());
81
82 TC_LOG_INFO("server.loading", ">> Loaded {} waypoint path nodes in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
84}
85
87{
88 uint32 pathId = fields[0].GetUInt32();
89
90 WaypointPath& path = _pathStore[pathId];
91 path.Id = pathId;
92 path.MoveType = WaypointMoveType(fields[1].GetUInt8());
93
95 {
96 TC_LOG_ERROR("sql.sql", "PathId {} in `waypoint_path` has invalid MoveType {}, ignoring", pathId, AsUnderlyingType(path.MoveType));
97 return;
98 }
99 path.Flags = WaypointPathFlags(fields[2].GetUInt8());
100 path.Nodes.clear();
101}
102
104{
105 uint32 pathId = fields[0].GetUInt32();
106
108 if (!path)
109 {
110 TC_LOG_ERROR("sql.sql", "PathId {} in `waypoint_path_node` does not exist in `waypoint_path`, ignoring", pathId);
111 return;
112 }
113
114 float x = fields[2].GetFloat();
115 float y = fields[3].GetFloat();
116 float z = fields[4].GetFloat();
118 if (!fields[5].IsNull())
119 o = fields[5].GetFloat();
120
123
124 path->Nodes.emplace_back(fields[1].GetUInt32(), x, y, z, o, fields[6].GetUInt32());
125}
126
128{
129 for (auto const& path : _pathStore)
130 {
131 WaypointPath pathInfo = path.second;
132 if (pathInfo.Nodes.empty())
133 TC_LOG_ERROR("sql.sql", "PathId {} in `waypoint_path` has no assigned nodes in `waypoint_path_node`", pathInfo.Id);
134
136 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);
137 }
138}
139
141{
142 static WaypointMgr instance;
143 return &instance;
144}
145
147{
148 // waypoint_path
149 {
151 stmt->setUInt32(0, pathId);
152
153 PreparedQueryResult result = WorldDatabase.Query(stmt);
154
155 if (!result)
156 {
157 TC_LOG_ERROR("sql.sql", "PathId {} in `waypoint_path` not found, ignoring", pathId);
158 return;
159 }
160
161 do
162 {
163 LoadPathFromDB(result->Fetch());
164 } while (result->NextRow());
165 }
166
167 // waypoint_path_data
168 {
170 stmt->setUInt32(0, pathId);
171
172 PreparedQueryResult result = WorldDatabase.Query(stmt);
173
174 if (!result)
175 {
176 TC_LOG_ERROR("sql.sql", "PathId {} in `waypoint_path_node` not found, ignoring", pathId);
177 return;
178 }
179
180 do
181 {
182 LoadPathNodesFromDB(result->Fetch());
183 } while (result->NextRow());
184 }
185}
186
188{
189 for (WaypointNode const& node : path->Nodes)
190 {
191 std::pair<uint32, uint32> pathNodePair(path->Id, node.Id);
192
193 auto itr = _nodeToVisualWaypointGUIDsMap.find(pathNodePair);
194 if (itr != _nodeToVisualWaypointGUIDsMap.end())
195 continue;
196
197 TempSummon* summon = owner->SummonCreature(VISUAL_WAYPOINT, node.X, node.Y, node.Z, node.Orientation ? *node.Orientation : 0.0f);
198 if (!summon)
199 continue;
200
201 if (displayId)
202 {
203 summon->SetDisplayId(*displayId, true);
204 summon->SetObjectScale(0.5f);
205 }
206
207 _nodeToVisualWaypointGUIDsMap[pathNodePair] = summon->GetGUID();
208 _visualWaypointGUIDToNodeMap[summon->GetGUID()] = std::pair<WaypointPath const*, WaypointNode const*>(path, &node);
209 }
210}
211
213{
214 for (WaypointNode const& node : path->Nodes)
215 {
216 std::pair<uint32, uint32> pathNodePair(path->Id, node.Id);
217 auto itr = _nodeToVisualWaypointGUIDsMap.find(pathNodePair);
218 if (itr == _nodeToVisualWaypointGUIDsMap.end())
219 continue;
220
221 Creature* creature = ObjectAccessor::GetCreature(*owner, itr->second);
222 if (!creature)
223 continue;
224
225 _visualWaypointGUIDToNodeMap.erase(itr->second);
226 _nodeToVisualWaypointGUIDsMap.erase(pathNodePair);
227
228 creature->DespawnOrUnsummon();
229 }
230}
231
232void WaypointMgr::MoveNode(WaypointPath const* path, WaypointNode const* node, Position const& pos)
233{
235 stmt->setFloat(0, pos.GetPositionX());
236 stmt->setFloat(1, pos.GetPositionY());
237 stmt->setFloat(2, pos.GetPositionZ());
238 stmt->setFloat(3, pos.GetOrientation());
239 stmt->setUInt32(4, path->Id);
240 stmt->setUInt32(5, node->Id);
241 WorldDatabase.Execute(stmt);
242}
243
245{
247 stmt->setUInt32(0, path->Id);
248 stmt->setUInt32(1, node->Id);
249 WorldDatabase.Execute(stmt);
250
251 stmt = WorldDatabase.GetPreparedStatement(WORLD_UPD_WAYPOINT_PATH_NODE);
252 stmt->setUInt32(0, path->Id);
253 stmt->setUInt32(1, node->Id);
254 WorldDatabase.Execute(stmt);
255}
256
258{
259 WaypointPath const* path = GetPath(pathId);
260 if (!path)
261 return;
262
263 WaypointNode const* node = GetNode(path, nodeId);
264 if (!node)
265 return;
266
267 DeleteNode(path, node);
268}
269
271{
273}
274
275WaypointNode const* WaypointMgr::GetNode(WaypointPath const* path, uint32 nodeId) const
276{
277 for (WaypointNode const& node : path->Nodes)
278 {
279 if (node.Id == nodeId)
280 return &node;
281 }
282 return nullptr;
283}
284
285WaypointNode const* WaypointMgr::GetNode(uint32 pathId, uint32 nodeId) const
286{
287 WaypointPath const* path = GetPath(pathId);
288 if (!path)
289 return nullptr;
290
291 return GetNode(path->Id, nodeId);
292}
293
295{
296 auto itr = _visualWaypointGUIDToNodeMap.find(guid);
297 if (itr == _visualWaypointGUIDToNodeMap.end())
298 return nullptr;
299
300 return itr->second.first;
301}
302
304{
305 auto itr = _visualWaypointGUIDToNodeMap.find(guid);
306 if (itr == _visualWaypointGUIDToNodeMap.end())
307 return nullptr;
308
309 return itr->second.second;
310}
311
313{
314 auto itr = _nodeToVisualWaypointGUIDsMap.find(std::make_pair(pathId, nodeId));
315 if (itr == _nodeToVisualWaypointGUIDsMap.end())
316 return ObjectGuid::Empty;
317
318 return itr->second;
319}
std::shared_ptr< ResultSet > QueryResult
std::shared_ptr< PreparedResultSet > PreparedQueryResult
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
Definition: DatabaseEnv.cpp:20
uint32_t uint32
Definition: Define.h:142
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:165
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:159
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:57
uint32 getMSTime()
Definition: Timer.h:33
#define VISUAL_WAYPOINT
Definition: Unit.h:35
constexpr std::underlying_type< E >::type AsUnderlyingType(E enumValue)
Definition: Util.h:491
#define WAYPOINT_PATH_FLAG_FOLLOW_PATH_BACKWARDS_MINIMUM_NODES
WaypointMoveType
WaypointPathFlags
@ FollowPathBackwardsFromEndToStart
@ WORLD_SEL_WAYPOINT_PATH_NODE_BY_PATHID
Definition: WorldDatabase.h:58
@ WORLD_DEL_WAYPOINT_PATH_NODE
Definition: WorldDatabase.h:54
@ WORLD_SEL_WAYPOINT_PATH_BY_PATHID
Definition: WorldDatabase.h:52
@ WORLD_UPD_WAYPOINT_PATH_NODE
Definition: WorldDatabase.h:55
@ WORLD_UPD_WAYPOINT_PATH_NODE_POSITION
Definition: WorldDatabase.h:56
void SetObjectScale(float scale) override
Definition: Creature.cpp:3391
void DespawnOrUnsummon(Milliseconds timeToDespawn=0s, Seconds forceRespawnTime=0s)
Definition: Creature.cpp:2415
void SetDisplayId(uint32 displayId, bool setNative=false) override
Definition: Creature.cpp:3402
constexpr bool HasFlag(T flag) const
Definition: EnumFlag.h:106
Class used to access individual fields of database query result.
Definition: Field.h:90
float GetFloat() const
Definition: Field.cpp:94
uint32 GetUInt32() const
Definition: Field.cpp:62
static ObjectGuid const Empty
Definition: ObjectGuid.h:274
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:159
void setFloat(const uint8 index, const float value)
void setUInt32(const uint8 index, const uint32 value)
Definition: Unit.h:627
void DoPostLoadingChecks()
void DeleteNode(WaypointPath const *path, WaypointNode const *node)
void LoadPathFromDB(Field *fields)
std::unordered_map< std::pair< uint32, uint32 >, ObjectGuid > _nodeToVisualWaypointGUIDsMap
std::unordered_map< ObjectGuid, std::pair< WaypointPath const *, WaypointNode const * > > _visualWaypointGUIDToNodeMap
WaypointNode const * GetNodeByVisualGUID(ObjectGuid guid) const
WaypointPath const * GetPathByVisualGUID(ObjectGuid guid) const
std::unordered_map< uint32, WaypointPath > _pathStore
void MoveNode(WaypointPath const *path, WaypointNode const *node, Position const &pos)
WaypointNode const * GetNode(WaypointPath const *path, uint32 nodeId) const
static WaypointMgr * instance()
void ReloadPath(uint32 pathId)
WaypointPath const * GetPath(uint32 pathId) const
void VisualizePath(Unit *owner, WaypointPath const *path, Optional< uint32 > displayId)
ObjectGuid const & GetVisualGUIDByNode(uint32 pathId, uint32 nodeId) const
void _LoadPathNodes()
void DevisualizePath(Unit *owner, WaypointPath const *path)
void LoadPathNodesFromDB(Field *fields)
TempSummon * SummonCreature(uint32 entry, Position const &pos, TempSummonType despawnType=TEMPSUMMON_MANUAL_DESPAWN, Milliseconds despawnTime=0s, uint32 vehId=0, uint32 spellId=0, ObjectGuid privateObjectOwner=ObjectGuid::Empty)
Definition: Object.cpp:2025
TC_GAME_API Creature * GetCreature(WorldObject const &u, ObjectGuid const &guid)
auto MapGetValuePtr(M &map, typename M::key_type const &key)
Definition: MapUtils.h:29
void NormalizeMapCoord(float &c)
Definition: GridDefines.h:223
constexpr float GetPositionX() const
Definition: Position.h:76
constexpr float GetPositionY() const
Definition: Position.h:77
constexpr float GetOrientation() const
Definition: Position.h:79
constexpr float GetPositionZ() const
Definition: Position.h:78
Optional< float > Orientation
std::vector< WaypointNode > Nodes
EnumFlag< WaypointPathFlags > Flags
WaypointMoveType MoveType