Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
MapManager.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#include "CellImpl.h"
7#include "Config.h"
8#include "Corpse.h"
9#include "DatabaseEnv.h"
10#include "GridDefines.h"
11#include "Group.h"
12#include "InstanceSaveMgr.h"
13#include "InstanceScript.h"
14#include "Language.h"
15#include "Log.h"
16#include "MapInstanced.h"
17#include "MapManager.h"
18#include "ObjectAccessor.h"
19#include "ObjectMgr.h"
20#include "Opcodes.h"
21#include "Player.h"
22#include "RuntimeMetrics.h"
23#include "Transport.h"
24#include "TransportMgr.h"
25#include "World.h"
26#include "WorldPacket.h"
27#include "WorldSession.h"
28
29extern GridState* si_GridStates[]; // debugging code, should be deleted some day
30
36
38
40{
42
43 // debugging code, should be deleted some day
44 {
45 for (uint8 i = 0; i < MAX_GRID_STATE; ++i)
47
49 }
50 int num_threads(sWorld->getIntConfig(WorldIntConfigs::CONFIG_NUMTHREADS));
51 // Start mtmaps if needed.
52 if (num_threads > 0 && m_updater.activate(num_threads) == -1)
53 abort();
54}
55
57{
58 for (MapMapType::iterator iter = i_maps.begin(); iter != i_maps.end(); ++iter)
59 (*iter).second->InitVisibilityDistance();
60}
61
62// debugging code, should be deleted some day
64{
65 bool ok = true;
66 for (int i = 0; i < MAX_GRID_STATE; i++)
67 {
68 if (i_GridStates[i] != si_GridStates[i])
69 {
70 SF_LOG_ERROR("maps", "MapManager::checkGridStates(), GridState: si_GridStates is currupt !!!");
71 ok = false;
73 }
74#ifdef SKYFIRE_DEBUG
75 // inner class checking only when compiled with debug
76 if (!si_GridStates[i]->checkMagic())
77 {
78 ok = false;
79 si_GridStates[i]->setMagic();
80 }
81#endif
82 }
83 if (!ok)
85}
86
88{
89 Map* map = FindBaseMap(id);
90
91 if (!map)
92 {
93 MapEntry const* entry = sMapStore.LookupEntry(id);
94 if (entry->entrance_map != -1)
95 {
97
98 // must have been created by parent map
99 map = FindBaseMap(id);
100 ASSERT(entry);
101 }
102
103 std::lock_guard<std::mutex> guard(Lock);
104 if (entry->Instanceable())
105 map = new MapInstanced(entry->MapID, i_gridCleanUpDelay);
106 else
107 map = new Map(entry->MapID, i_gridCleanUpDelay, 0, DIFFICULTY_NONE);
108
109 i_maps[entry->MapID] = map;
110
111 if (!entry->Instanceable())
112 {
113 map->LoadRespawnTimes();
114 sTransportMgr->SpawnLocalTransports(map);
115 }
116 }
117
118 ASSERT(map);
119 return map;
120 /*
121 if (map == NULL)
122 {
123 std::lock_guard<std::mutex> guard(Lock);
124
125 MapEntry const* entry = sMapStore.LookupEntry(id);
126 ASSERT(entry);
127
128 if (entry->Instanceable())
129 map = new MapInstanced(id, i_gridCleanUpDelay);
130 else
131 {
132 map = new Map(id, i_gridCleanUpDelay, 0, DIFFICULTY_NONE);
133 map->LoadRespawnTimes();
134 }
135
136 i_maps[id] = map;
137 }
138
139 ASSERT(map);
140 return map;
141 */
142}
143
145{
146 Map* map = FindBaseMap(mapId);
147 if (map && map->Instanceable())
148 return NULL;
149 return map;
150}
151
153{
154 Map* m = CreateBaseMap(id);
155
156 if (m && m->Instanceable())
157 m = ((MapInstanced*)m)->CreateInstanceForPlayer(id, player);
158
159 return m;
160}
161
162Map* MapManager::FindMap(uint32 mapid, uint32 instanceId) const
163{
164 Map* map = FindBaseMap(mapid);
165 if (!map)
166 return NULL;
167
168 if (!map->Instanceable())
169 return instanceId == 0 ? map : NULL;
170
171 return ((MapInstanced*)map)->FindInstanceMap(instanceId);
172}
173
174bool MapManager::CanPlayerEnter(uint32 mapid, Player* player, bool loginCheck)
175{
176 MapEntry const* entry = sMapStore.LookupEntry(mapid);
177 if (!entry)
178 return false;
179
180 if (!entry->IsInstance())
181 return true;
182
183 InstanceTemplate const* instance = sObjectMgr->GetInstanceTemplate(mapid);
184 if (!instance)
185 return false;
186
187 DifficultyID targetDifficulty = player->GetDifficulty(entry);
188 //The player has a heroic mode and tries to enter into instance which has no a heroic mode
189 MapDifficulty const* mapDiff = GetMapDifficultyData(entry->MapID, targetDifficulty);
190 if (!mapDiff)
191 {
192 // Send aborted message for dungeons
193 if (entry->IsNonRaidInstance())
194 {
196 return false;
197 }
198 else // attempt to downscale
199 mapDiff = GetDownscaledMapDifficultyData(entry->MapID, targetDifficulty);
200 }
201 // FIXME: mapDiff is never used
202
203 //Bypass checks for GMs
204 if (player->IsGameMaster())
205 return true;
206
207 char const* mapName = entry->name;
208
209 Group* group = player->GetGroup();
210 if (entry->IsRaid())
211 {
212 // can only enter in a raid group
213 if ((!group || !group->isRaidGroup()) && !sWorld->GetBoolConfig(WorldBoolConfigs::CONFIG_INSTANCE_IGNORE_RAID))
214 {
215 // probably there must be special opcode, because client has this string constant in GlobalStrings.lua
218 SF_LOG_DEBUG("maps", "MAP: Player '%s' must be in a raid group to enter instance '%s'", player->GetName().c_str(), mapName);
219 return false;
220 }
221 }
222
223 if (!player->IsAlive())
224 {
225 if (Corpse* corpse = player->GetCorpse())
226 {
227 // let enter in ghost mode in instance that connected to inner instance with corpse
228 uint32 corpseMap = corpse->GetMapId();
229 do
230 {
231 if (corpseMap == mapid)
232 break;
233
234 InstanceTemplate const* corpseInstance = sObjectMgr->GetInstanceTemplate(corpseMap);
235 corpseMap = corpseInstance ? corpseInstance->Parent : 0;
236 } while (corpseMap);
237
238 if (!corpseMap)
239 {
241 player->GetSession()->SendPacket(&data);
242 SF_LOG_DEBUG("maps", "MAP: Player '%s' does not have a corpse in instance '%s' and cannot enter.", player->GetName().c_str(), mapName);
243 return false;
244 }
245 SF_LOG_DEBUG("maps", "MAP: Player '%s' has corpse in instance '%s' and can enter.", player->GetName().c_str(), mapName);
246 player->ResurrectPlayer(0.5f, false);
247 player->SpawnCorpseBones();
248 }
249 else
250 SF_LOG_DEBUG("maps", "Map::CanPlayerEnter - player '%s' is dead but does not have a corpse!", player->GetName().c_str());
251 }
252
253 //Get instance where player's group is bound & its map
254 if (group)
255 {
256 InstanceGroupBind* boundInstance = group->GetBoundInstance(entry);
257 if (boundInstance && boundInstance->save)
258 if (Map* boundMap = sMapMgr->FindMap(mapid, boundInstance->save->GetInstanceId()))
259 if (!loginCheck && !boundMap->CanEnter(player))
260 return false;
261 /*
262 This check has to be moved to InstanceMap::CanEnter()
263 // Player permanently bounded to different instance than groups one
264 InstancePlayerBind* playerBoundedInstance = player->GetBoundInstance(mapid, player->GetDifficulty(entry->IsRaid()));
265 if (playerBoundedInstance && playerBoundedInstance->perm && playerBoundedInstance->save &&
266 boundedInstance->save->GetInstanceId() != playerBoundedInstance->save->GetInstanceId())
267 {
269 return false;
270 }*/
271 }
272
273 // players are only allowed to enter 5 instances per hour
274 if (entry->IsInstance() && (!player->GetGroup() || (player->GetGroup() && !player->GetGroup()->isLFGGroup())))
275 {
276 uint32 instaceIdToCheck = 0;
277 if (InstanceSave* save = player->GetInstanceSave(mapid))
278 instaceIdToCheck = save->GetInstanceId();
279
280 // instanceId can never be 0 - will not be found
281 if (!player->CheckInstanceCount(instaceIdToCheck) && !player->isDead())
282 {
284 return false;
285 }
286 }
287
288 //Other requirements
289 return player->Satisfy(sObjectMgr->GetAccessRequirement(mapid, targetDifficulty), mapid, true);
290}
291
293{
294 i_timer.Update(diff);
295 if (!i_timer.Passed())
296 return;
297
299
300 MapMapType::iterator iter = i_maps.begin();
301 for (; iter != i_maps.end(); ++iter)
302 {
303 if (m_updater.activated())
304 m_updater.schedule_update(*iter->second, uint32(i_timer.GetCurrent()));
305 else
306 iter->second->Update(uint32(i_timer.GetCurrent()));
307 }
308 if (m_updater.activated())
309 m_updater.wait();
310
311 for (iter = i_maps.begin(); iter != i_maps.end(); ++iter)
312 iter->second->DelayedUpdate(uint32(i_timer.GetCurrent()));
313
314 sObjectAccessor->Update(uint32(i_timer.GetCurrent()));
315
316 i_timer.SetCurrent(0);
317}
318
320
321bool MapManager::ExistMapAndVMap(uint32 mapid, float x, float y)
322{
324
325 int gx = 63 - p.x_coord;
326 int gy = 63 - p.y_coord;
327
328 return Map::ExistMap(mapid, gx, gy) && Map::ExistVMap(mapid, gx, gy);
329}
330
331bool MapManager::IsValidMAP(uint32 mapid, bool startUp)
332{
333 MapEntry const* mEntry = sMapStore.LookupEntry(mapid);
334
335 if (startUp)
336 return mEntry ? true : false;
337 else
338 return mEntry && (!mEntry->IsInstance() || sObjectMgr->GetInstanceTemplate(mapid));
339
341}
342
344{
345 for (MapMapType::iterator iter = i_maps.begin(); iter != i_maps.end();)
346 {
347 iter->second->UnloadAll();
348 delete iter->second;
349 i_maps.erase(iter++);
350 }
351
352 if (m_updater.activated())
353 m_updater.deactivate();
354
356}
357
359{
360 std::lock_guard<std::mutex> guard(Lock);
361
362 uint32 ret = 0;
363 for (MapMapType::iterator itr = i_maps.begin(); itr != i_maps.end(); ++itr)
364 {
365 Map* map = itr->second;
366 if (!map->Instanceable())
367 continue;
368 MapInstanced::InstancedMaps& maps = ((MapInstanced*)map)->GetInstancedMaps();
369 for (MapInstanced::InstancedMaps::iterator mitr = maps.begin(); mitr != maps.end(); ++mitr)
370 if (mitr->second->IsInstance()) ret++;
371 }
372 return ret;
373}
374
376{
377 std::lock_guard<std::mutex> guard(Lock);
378
379 uint32 ret = 0;
380 for (MapMapType::iterator itr = i_maps.begin(); itr != i_maps.end(); ++itr)
381 {
382 Map* map = itr->second;
383 if (!map->Instanceable())
384 continue;
385 MapInstanced::InstancedMaps& maps = ((MapInstanced*)map)->GetInstancedMaps();
386 for (MapInstanced::InstancedMaps::iterator mitr = maps.begin(); mitr != maps.end(); ++mitr)
387 if (mitr->second->IsInstance())
388 ret += ((InstanceMap*)mitr->second)->GetPlayers().getSize();
389 }
390 return ret;
391}
392
394{
395 _nextInstanceId = 1;
396
397 QueryResult result = CharacterDatabase.Query("SELECT MAX(id) FROM instance");
398 if (result)
399 {
400 uint32 maxId = (*result)[0].GetUInt32();
401
402 // Resize to multiples of 32 (vector<bool> allocates memory the same way)
403 _instanceIds.resize((maxId / 32) * 32 + (maxId % 32 > 0 ? 32 : 0));
404 }
405}
406
408{
409 // Allocation and sizing was done in InitInstanceIds()
410 _instanceIds[instanceId] = true;
411}
412
414{
415 uint32 newInstanceId = _nextInstanceId;
416
417 // Find the lowest available id starting from the current NextInstanceId (which should be the lowest according to the logic in FreeInstanceId()
418 for (uint32 i = ++_nextInstanceId; i < 0xFFFFFFFF; ++i)
419 {
420 if ((i < _instanceIds.size() && !_instanceIds[i]) || i >= _instanceIds.size())
421 {
422 _nextInstanceId = i;
423 break;
424 }
425 }
426
427 if (newInstanceId == _nextInstanceId)
428 {
429 SF_LOG_ERROR("maps", "Instance ID overflow!! Can't continue, shutting down server. ");
431 }
432
433 // Allocate space if necessary
434 if (newInstanceId >= uint32(_instanceIds.size()))
435 {
436 // Due to the odd memory allocation behavior of vector<bool> we match size to capacity before triggering a new allocation
437 if (_instanceIds.size() < _instanceIds.capacity())
438 {
439 _instanceIds.resize(_instanceIds.capacity());
440 }
441 else
442 _instanceIds.resize((newInstanceId / 32) * 32 + (newInstanceId % 32 > 0 ? 32 : 0));
443 }
444
445 _instanceIds[newInstanceId] = true;
446
447 return newInstanceId;
448}
449
451{
452 // If freed instance id is lower than the next id available for new instances, use the freed one instead
453 if (instanceId < _nextInstanceId)
454 SetNextInstanceId(instanceId);
455
456 _instanceIds[instanceId] = false;
457}
DifficultyID
Definition DBCEnums.h:330
@ DIFFICULTY_NONE
Definition DBCEnums.h:331
MapDifficulty const * GetMapDifficultyData(uint32 mapId, DifficultyID difficulty)
MapDifficulty const * GetDownscaledMapDifficultyData(uint32 mapId, DifficultyID &difficulty)
DBCStorage< MapEntry > sMapStore(MapEntryfmt)
std::uint8_t uint8
Definition Define.h:79
std::uint32_t uint32
Definition Define.h:77
#define ASSERT
Definition Errors.h:29
CoordPair< MAX_NUMBER_OF_GRIDS > GridCoord
@ LANG_INSTANCE_RAID_GROUP_ONLY
Definition Language.h:1074
#define SF_LOG_DEBUG(filterType__,...)
Definition Log.h:134
#define SF_LOG_ERROR(filterType__,...)
Definition Log.h:143
GridState * si_GridStates[MAX_GRID_STATE]
Definition Map.cpp:38
#define sMapMgr
Definition MapManager.h:145
@ MAX_GRID_STATE
Definition NGrid.h:54
#define sObjectAccessor
#define sObjectMgr
Definition ObjectMgr.h:1617
@ TRANSFER_ABORT_TOO_MANY_INSTANCES
Definition Player.h:793
Skyfire::AutoPtr< ResultSet, Skyfire::Mutex > QueryResult
Definition QueryResult.h:48
#define sTransportMgr
Definition Group.h:147
bool isLFGGroup() const
Definition Group.cpp:2543
InstanceGroupBind * GetBoundInstance(Player *player)
Definition Group.cpp:2393
bool isRaidGroup() const
Definition Group.cpp:2548
uint32 GetInstanceId() const
Definition Map.h:238
void LoadRespawnTimes()
Definition Map.cpp:3272
static void DeleteStateMachine()
Definition Map.cpp:214
bool Instanceable() const
Definition Map.h:367
static void InitStateMachine()
Definition Map.cpp:206
static bool ExistVMap(uint32 mapid, int gx, int gy)
Definition Map.cpp:104
static bool ExistMap(uint32 mapid, int gx, int gy)
Definition Map.cpp:75
UNORDERED_MAP< uint32, Map * > InstancedMaps
void RegisterInstanceId(uint32 instanceId)
Map * FindBaseNonInstanceMap(uint32 mapId) const
void SetNextInstanceId(uint32 nextInstanceId)
Definition MapManager.h:111
InstanceIds _instanceIds
Definition MapManager.h:141
uint32 GetNumPlayersInInstances()
static bool ExistMapAndVMap(uint32 mapid, float x, float y)
MapUpdater m_updater
Definition MapManager.h:143
void checkAndCorrectGridStatesArray()
GridState * i_GridStates[MAX_GRID_STATE]
Definition MapManager.h:121
Map * FindBaseMap(uint32 mapId) const
Definition MapManager.h:127
uint32 i_gridCleanUpDelay
Definition MapManager.h:137
void Initialize(void)
void Update(uint32)
void DoDelayedMovesAndRemoves()
Map * FindMap(uint32 mapId, uint32 instanceId) const
void InitializeVisibilityDistanceInfo()
void UnloadAll()
Map * CreateMap(uint32 mapId, Player *player)
uint32 GetNumInstances()
uint32 _nextInstanceId
Definition MapManager.h:142
MapMapType i_maps
Definition MapManager.h:138
void InitInstanceIds()
int i_GridStateErrorCount
Definition MapManager.h:122
std::mutex Lock
Definition MapManager.h:136
bool CanPlayerEnter(uint32 mapid, Player *player, bool loginCheck=false)
Map * CreateBaseMap(uint32 mapId)
static bool IsValidMAP(uint32 mapid, bool startUp)
uint32 GenerateInstanceId()
IntervalTimer i_timer
Definition MapManager.h:139
void FreeInstanceId(uint32 instanceId)
InstanceSave * GetInstanceSave(uint32 mapid)
Definition Player.cpp:13891
bool CheckInstanceCount(uint32 instanceId) const
Definition Player.cpp:14223
DifficultyID GetDifficulty(MapEntry const *mapEntry) const
Definition Player.cpp:20762
WorldSession * GetSession() const
Definition Player.h:2417
Corpse * GetCorpse() const
Definition Player.cpp:5385
bool Satisfy(AccessRequirement const *ar, uint32 target_map, bool report=false)
Definition Player.cpp:14123
Group * GetGroup()
Definition Player.h:2972
void SpawnCorpseBones()
Definition Player.cpp:5378
bool IsGameMaster() const
Definition Player.h:1369
void SendTransferAborted(uint32 mapid, TransferAbortReason reason, uint8 arg=0)
Definition Player.cpp:18715
DifficultyID GetDungeonDifficulty() const
Definition Player.h:2315
void ResurrectPlayer(float restore_percent, bool applySickness=false)
Definition Player.cpp:5203
bool IsAlive() const
Definition Unit.h:2032
bool isDead() const
Definition Unit.h:2040
static void StopNow(uint8 exitcode)
Definition World.h:674
std::string const & GetName() const
Definition Object.h:664
void SendNotification(const char *format,...) ATTR_PRINTF(2
void SendPacket(WorldPacket const *packet, bool forced=false)
Send a packet to the client.
CharacterDatabaseWorkerPool CharacterDatabase
Accessor to the character database.
Definition Main.cpp:41
@ SMSG_CORPSE_NOT_IN_INSTANCE
Definition Opcodes.h:625
#define sWorld
Definition World.h:910
@ CONFIG_INTERVAL_MAPUPDATE
Definition World.h:202
@ CONFIG_NUMTHREADS
Definition World.h:311
@ CONFIG_INTERVAL_GRIDCLEAN
Definition World.h:201
@ CONFIG_INSTANCE_IGNORE_RAID
Definition World.h:105
@ ERROR_EXIT_CODE
Definition World.h:65
RuntimeMetrics & GetRuntimeMetrics()
GridCoord ComputeGridCoord(float x, float y)
uint32 x_coord
uint32 y_coord
InstanceSave * save
Definition Group.h:137
uint32 Parent
Definition Map.h:219
bool IsNonRaidInstance() const
int32 entrance_map
bool IsInstance() const
uint32 MapID
char * name
bool IsRaid() const
bool Instanceable() const