TrinityCore
Loading...
Searching...
No Matches
cs_mmaps.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
26#include "ScriptMgr.h"
27#include "CellImpl.h"
28#include "Chat.h"
29#include "DisableMgr.h"
30#include "GridNotifiersImpl.h"
31#include "Map.h"
32#include "MMapFactory.h"
33#include "PathGenerator.h"
34#include "Player.h"
36#include "RBAC.h"
37
38#if TRINITY_COMPILER == TRINITY_COMPILER_GNU
39#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
40#endif
41
43{
44public:
45 mmaps_commandscript() : CommandScript("mmaps_commandscript") { }
46
47 std::vector<ChatCommand> GetCommands() const override
48 {
49 static std::vector<ChatCommand> mmapCommandTable =
50 {
56 };
57
58 static std::vector<ChatCommand> commandTable =
59 {
60 { "mmap", rbac::RBAC_PERM_COMMAND_MMAP, true, nullptr, "", mmapCommandTable },
61 };
62 return commandTable;
63 }
64
65 static bool HandleMmapPathCommand(ChatHandler* handler, char const* args)
66 {
68 {
69 handler->PSendSysMessage("NavMesh not loaded for current map.");
70 return true;
71 }
72
73 handler->PSendSysMessage("mmap path:");
74
75 // units
76 Player* player = handler->GetSession()->GetPlayer();
77 Unit* target = handler->getSelectedUnit();
78 if (!player || !target)
79 {
80 handler->PSendSysMessage("Invalid target/source selection.");
81 return true;
82 }
83
84 char* para = strtok((char*)args, " ");
85
86 bool useStraightPath = false;
87 if (para && strcmp(para, "true") == 0)
88 useStraightPath = true;
89
90 bool useRaycast = false;
91 if (para && (strcmp(para, "line") == 0 || strcmp(para, "ray") == 0 || strcmp(para, "raycast") == 0))
92 useRaycast = true;
93
94 // unit locations
95 float x, y, z;
96 player->GetPosition(x, y, z);
97
98 // path
99 PathGenerator path(target);
100 path.SetUseStraightPath(useStraightPath);
101 path.SetUseRaycast(useRaycast);
102 bool result = path.CalculatePath(x, y, z, false);
103
104 Movement::PointsArray const& pointPath = path.GetPath();
105 handler->PSendSysMessage("%s's path to %s:", target->GetName().c_str(), player->GetName().c_str());
106 handler->PSendSysMessage("Building: %s", useStraightPath ? "StraightPath" : useRaycast ? "Raycast" : "SmoothPath");
107 handler->PSendSysMessage("Result: %s - Length: %zu - Type: %u", (result ? "true" : "false"), pointPath.size(), path.GetPathType());
108
109 G3D::Vector3 const& start = path.GetStartPosition();
110 G3D::Vector3 const& end = path.GetEndPosition();
111 G3D::Vector3 const& actualEnd = path.GetActualEndPosition();
112
113 handler->PSendSysMessage("StartPosition (%.3f, %.3f, %.3f)", start.x, start.y, start.z);
114 handler->PSendSysMessage("EndPosition (%.3f, %.3f, %.3f)", end.x, end.y, end.z);
115 handler->PSendSysMessage("ActualEndPosition (%.3f, %.3f, %.3f)", actualEnd.x, actualEnd.y, actualEnd.z);
116
117 if (!player->IsGameMaster())
118 handler->PSendSysMessage("Enable GM mode to see the path points.");
119
120 for (uint32 i = 0; i < pointPath.size(); ++i)
121 player->SummonCreature(VISUAL_WAYPOINT, pointPath[i].x, pointPath[i].y, pointPath[i].z, 0, TEMPSUMMON_TIMED_DESPAWN, 9s);
122
123 return true;
124 }
125
126 static bool HandleMmapLocCommand(ChatHandler* handler, char const* /*args*/)
127 {
128 handler->PSendSysMessage("mmap tileloc:");
129
130 // grid tile location
131 Player* player = handler->GetSession()->GetPlayer();
132
133 int32 gx = 32 - player->GetPositionX() / SIZE_OF_GRIDS;
134 int32 gy = 32 - player->GetPositionY() / SIZE_OF_GRIDS;
135
136 handler->PSendSysMessage("%03u%02i%02i.mmtile", player->GetMapId(), gx, gy);
137 handler->PSendSysMessage("tileloc [%i, %i]", gy, gx);
138
139 // calculate navmesh tile location
140 dtNavMesh const* navmesh = MMAP::MMapFactory::createOrGetMMapManager()->GetNavMesh(handler->GetSession()->GetPlayer()->GetMapId());
141 dtNavMeshQuery const* navmeshquery = MMAP::MMapFactory::createOrGetMMapManager()->GetNavMeshQuery(handler->GetSession()->GetPlayer()->GetMapId(), player->GetInstanceId());
142 if (!navmesh || !navmeshquery)
143 {
144 handler->PSendSysMessage("NavMesh not loaded for current map.");
145 return true;
146 }
147
148 float const* min = navmesh->getParams()->orig;
149 float x, y, z;
150 player->GetPosition(x, y, z);
151 float location[VERTEX_SIZE] = {y, z, x};
152 float extents[VERTEX_SIZE] = {3.0f, 5.0f, 3.0f};
153
154 int32 tilex = int32((y - min[0]) / SIZE_OF_GRIDS);
155 int32 tiley = int32((x - min[2]) / SIZE_OF_GRIDS);
156
157 handler->PSendSysMessage("Calc [%02i, %02i]", tilex, tiley);
158
159 // navmesh poly -> navmesh tile location
160 dtQueryFilter filter = dtQueryFilter();
161 dtPolyRef polyRef = INVALID_POLYREF;
162 if (dtStatusFailed(navmeshquery->findNearestPoly(location, extents, &filter, &polyRef, nullptr)))
163 {
164 handler->PSendSysMessage("Dt [??,??] (invalid poly, probably no tile loaded)");
165 return true;
166 }
167
168 if (polyRef == INVALID_POLYREF)
169 handler->PSendSysMessage("Dt [??, ??] (invalid poly, probably no tile loaded)");
170 else
171 {
172 dtMeshTile const* tile;
173 dtPoly const* poly;
174 if (dtStatusSucceed(navmesh->getTileAndPolyByRef(polyRef, &tile, &poly)))
175 {
176 if (tile)
177 {
178 handler->PSendSysMessage("Dt [%02i,%02i]", tile->header->x, tile->header->y);
179 return true;
180 }
181 }
182
183 handler->PSendSysMessage("Dt [??,??] (no tile loaded)");
184 }
185
186 return true;
187 }
188
189 static bool HandleMmapLoadedTilesCommand(ChatHandler* handler, char const* /*args*/)
190 {
191 uint32 mapid = handler->GetSession()->GetPlayer()->GetMapId();
192 dtNavMesh const* navmesh = MMAP::MMapFactory::createOrGetMMapManager()->GetNavMesh(mapid);
193 dtNavMeshQuery const* navmeshquery = MMAP::MMapFactory::createOrGetMMapManager()->GetNavMeshQuery(mapid, handler->GetSession()->GetPlayer()->GetInstanceId());
194 if (!navmesh || !navmeshquery)
195 {
196 handler->PSendSysMessage("NavMesh not loaded for current map.");
197 return true;
198 }
199
200 handler->PSendSysMessage("mmap loadedtiles:");
201
202 for (int32 i = 0; i < navmesh->getMaxTiles(); ++i)
203 {
204 dtMeshTile const* tile = navmesh->getTile(i);
205 if (!tile || !tile->header)
206 continue;
207
208 handler->PSendSysMessage("[%02i, %02i]", tile->header->x, tile->header->y);
209 }
210
211 return true;
212 }
213
214 static bool HandleMmapStatsCommand(ChatHandler* handler, char const* /*args*/)
215 {
216 uint32 mapId = handler->GetSession()->GetPlayer()->GetMapId();
217 handler->PSendSysMessage("mmap stats:");
218 handler->PSendSysMessage(" global mmap pathfinding is %sabled", DisableMgr::IsPathfindingEnabled(mapId) ? "en" : "dis");
219
221 handler->PSendSysMessage(" %u maps loaded with %u tiles overall", manager->getLoadedMapsCount(), manager->getLoadedTilesCount());
222
223 dtNavMesh const* navmesh = manager->GetNavMesh(handler->GetSession()->GetPlayer()->GetMapId());
224 if (!navmesh)
225 {
226 handler->PSendSysMessage("NavMesh not loaded for current map.");
227 return true;
228 }
229
230 uint32 tileCount = 0;
231 uint32 nodeCount = 0;
232 uint32 polyCount = 0;
233 uint32 vertCount = 0;
234 uint32 triCount = 0;
235 uint32 triVertCount = 0;
236 uint32 dataSize = 0;
237 for (int32 i = 0; i < navmesh->getMaxTiles(); ++i)
238 {
239 dtMeshTile const* tile = navmesh->getTile(i);
240 if (!tile || !tile->header)
241 continue;
242
243 tileCount++;
244 nodeCount += tile->header->bvNodeCount;
245 polyCount += tile->header->polyCount;
246 vertCount += tile->header->vertCount;
247 triCount += tile->header->detailTriCount;
248 triVertCount += tile->header->detailVertCount;
249 dataSize += tile->dataSize;
250 }
251
252 handler->PSendSysMessage("Navmesh stats:");
253 handler->PSendSysMessage(" %u tiles loaded", tileCount);
254 handler->PSendSysMessage(" %u BVTree nodes", nodeCount);
255 handler->PSendSysMessage(" %u polygons (%u vertices)", polyCount, vertCount);
256 handler->PSendSysMessage(" %u triangles (%u vertices)", triCount, triVertCount);
257 handler->PSendSysMessage(" %.2f MB of data (not including pointers)", ((float)dataSize / sizeof(unsigned char)) / 1048576);
258
259 return true;
260 }
261
262 static bool HandleMmapTestArea(ChatHandler* handler, char const* /*args*/)
263 {
264 float radius = 40.0f;
265 WorldObject* object = handler->GetSession()->GetPlayer();
266
267 // Get Creatures
268 std::list<Creature*> creatureList;
269 Trinity::AnyUnitInObjectRangeCheck go_check(object, radius);
270 Trinity::CreatureListSearcher<Trinity::AnyUnitInObjectRangeCheck> go_search(object, creatureList, go_check);
271 Cell::VisitGridObjects(object, go_search, radius);
272
273 if (!creatureList.empty())
274 {
275 handler->PSendSysMessage("Found %zu Creatures.", creatureList.size());
276
277 uint32 paths = 0;
278 uint32 uStartTime = getMSTime();
279
280 float gx, gy, gz;
281 object->GetPosition(gx, gy, gz);
282 for (std::list<Creature*>::iterator itr = creatureList.begin(); itr != creatureList.end(); ++itr)
283 {
284 PathGenerator path(*itr);
285 path.CalculatePath(gx, gy, gz);
286 ++paths;
287 }
288
289 uint32 uPathLoadTime = getMSTimeDiff(uStartTime, getMSTime());
290 handler->PSendSysMessage("Generated %i paths in %i ms", paths, uPathLoadTime);
291 }
292 else
293 handler->PSendSysMessage("No creatures in %f yard range.", radius);
294
295 return true;
296 }
297};
298
int32_t int32
Definition Define.h:129
uint32_t uint32
Definition Define.h:133
#define SIZE_OF_GRIDS
Definition GridDefines.h:38
@ TEMPSUMMON_TIMED_DESPAWN
#define VERTEX_SIZE
#define INVALID_POLYREF
Role Based Access Control related classes definition.
uint32 getMSTime()
Definition Timer.h:33
uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
Definition Timer.h:40
#define VISUAL_WAYPOINT
Definition Unit.h:34
Unit * getSelectedUnit()
Definition Chat.cpp:314
WorldSession * GetSession()
Definition Chat.h:46
void PSendSysMessage(char const *fmt, Args &&... args)
Definition Chat.h:69
static MMapManager * createOrGetMMapManager()
uint32 getLoadedMapsCount() const
Definition MMapManager.h:76
dtNavMeshQuery const * GetNavMeshQuery(uint32 mapId, uint32 instanceId)
dtNavMesh const * GetNavMesh(uint32 mapId)
uint32 getLoadedTilesCount() const
Definition MMapManager.h:75
void SetUseRaycast(bool useRaycast)
Movement::PointsArray const & GetPath() const
G3D::Vector3 const & GetStartPosition() const
PathType GetPathType() const
G3D::Vector3 const & GetEndPosition() const
bool CalculatePath(float destX, float destY, float destZ, bool forceDest=false)
void SetUseStraightPath(bool useStraightPath)
G3D::Vector3 const & GetActualEndPosition() const
bool IsGameMaster() const
Definition Player.h:998
Definition Unit.h:769
uint32 GetMapId() const
Definition Position.h:193
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:1992
uint32 GetInstanceId() const
Definition Object.h:365
std::string const & GetName() const
Definition Object.h:382
Player * GetPlayer() const
static bool HandleMmapTestArea(ChatHandler *handler, char const *)
Definition cs_mmaps.cpp:262
static bool HandleMmapLocCommand(ChatHandler *handler, char const *)
Definition cs_mmaps.cpp:126
static bool HandleMmapPathCommand(ChatHandler *handler, char const *args)
Definition cs_mmaps.cpp:65
static bool HandleMmapStatsCommand(ChatHandler *handler, char const *)
Definition cs_mmaps.cpp:214
static bool HandleMmapLoadedTilesCommand(ChatHandler *handler, char const *)
Definition cs_mmaps.cpp:189
std::vector< ChatCommand > GetCommands() const override
Definition cs_mmaps.cpp:47
void AddSC_mmaps_commandscript()
Definition cs_mmaps.cpp:299
bool IsPathfindingEnabled(uint32 mapId)
std::vector< Vector3 > PointsArray
@ RBAC_PERM_COMMAND_MMAP_LOC
Definition RBAC.h:408
@ RBAC_PERM_COMMAND_MMAP
Definition RBAC.h:406
@ RBAC_PERM_COMMAND_MMAP_TESTAREA
Definition RBAC.h:411
@ RBAC_PERM_COMMAND_MMAP_STATS
Definition RBAC.h:410
@ RBAC_PERM_COMMAND_MMAP_LOADEDTILES
Definition RBAC.h:407
@ RBAC_PERM_COMMAND_MMAP_PATH
Definition RBAC.h:409
static void VisitGridObjects(WorldObject const *obj, T &visitor, float radius, bool dont_load=true)
Definition CellImpl.h:168
float GetPositionX() const
Definition Position.h:79
void GetPosition(float &x, float &y) const
Definition Position.h:84
float GetPositionY() const
Definition Position.h:80