aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Maps/MapManager.cpp
blob: 788d27301a4069952c7ffb78fc5cb904eddf15e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
 * 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 "MapManager.h"
#include "Battleground.h"
#include "Containers.h"
#include "DatabaseEnv.h"
#include "DB2Stores.h"
#include "GarrisonMap.h"
#include "Group.h"
#include "InstanceLockMgr.h"
#include "Log.h"
#include "Map.h"
#include "Player.h"
#include "ScenarioMgr.h"
#include "ScriptMgr.h"
#include "World.h"
#include "WorldStateMgr.h"
#include <boost/dynamic_bitset.hpp>
#include <numeric>

MapManager::MapManager()
    : _freeInstanceIds(std::make_unique<InstanceIds>()), _nextInstanceId(0), _scheduledScripts(0)
{
    i_gridCleanUpDelay = sWorld->getIntConfig(CONFIG_INTERVAL_GRIDCLEAN);
    i_timer.SetInterval(sWorld->getIntConfig(CONFIG_INTERVAL_MAPUPDATE));
}

MapManager::~MapManager() = default;

void MapManager::Initialize()
{
    Map::InitStateMachine();

    int num_threads(sWorld->getIntConfig(CONFIG_NUMTHREADS));
    // Start mtmaps if needed.
    if (num_threads > 0)
        m_updater.activate(num_threads);
}

void MapManager::InitializeVisibilityDistanceInfo()
{
    for (auto iter = i_maps.begin(); iter != i_maps.end(); ++iter)
        iter->second->InitVisibilityDistance();
}

MapManager* MapManager::instance()
{
    static MapManager instance;
    return &instance;
}

Map* MapManager::FindMap_i(uint32 mapId, uint32 instanceId) const
{
    return Trinity::Containers::MapGetValuePtr(i_maps, { mapId, instanceId });
}

Map* MapManager::CreateWorldMap(uint32 mapId, uint32 instanceId)
{
    Map* map = new Map(mapId, i_gridCleanUpDelay, instanceId, DIFFICULTY_NONE);
    map->LoadRespawnTimes();
    map->LoadCorpseData();

    if (sWorld->getBoolConfig(CONFIG_BASEMAP_LOAD_GRIDS))
        map->LoadAllCells();

    return map;
}

InstanceMap* MapManager::CreateInstance(uint32 mapId, uint32 instanceId, InstanceLock* instanceLock, Difficulty difficulty, TeamId team, Group* group)
{
    // make sure we have a valid map id
    MapEntry const* entry = sMapStore.LookupEntry(mapId);
    if (!entry)
    {
        TC_LOG_ERROR("maps", "CreateInstance: no entry for map %d", mapId);
        ABORT();
    }

    // some instances only have one difficulty
    sDB2Manager.GetDownscaledMapDifficultyData(mapId, difficulty);

    TC_LOG_DEBUG("maps", "MapInstanced::CreateInstance: %smap instance %d for %d created with difficulty %s",
        instanceLock && instanceLock->GetInstanceId() ? "" : "new ", instanceId, mapId, sDifficultyStore.AssertEntry(difficulty)->Name[sWorld->GetDefaultDbcLocale()]);

    InstanceMap* map = new InstanceMap(mapId, i_gridCleanUpDelay, instanceId, difficulty, team, instanceLock);
    ASSERT(map->IsDungeon());

    map->LoadRespawnTimes();
    map->LoadCorpseData();
    if (group)
        map->TrySetOwningGroup(group);

    map->CreateInstanceData();
    map->SetInstanceScenario(sScenarioMgr->CreateInstanceScenario(map, team));

    if (sWorld->getBoolConfig(CONFIG_INSTANCEMAP_LOAD_GRIDS))
        map->LoadAllCells();

    return map;
}

BattlegroundMap* MapManager::CreateBattleground(uint32 mapId, uint32 instanceId, Battleground* bg)
{
    TC_LOG_DEBUG("maps", "MapInstanced::CreateBattleground: map bg %d for %d created.", instanceId, mapId);

    BattlegroundMap* map = new BattlegroundMap(mapId, i_gridCleanUpDelay, instanceId, DIFFICULTY_NONE);
    ASSERT(map->IsBattlegroundOrArena());
    map->SetBG(bg);
    bg->SetBgMap(map);
    return map;
}

GarrisonMap* MapManager::CreateGarrison(uint32 mapId, uint32 instanceId, Player* owner)
{
    GarrisonMap* map = new GarrisonMap(mapId, i_gridCleanUpDelay, instanceId, owner->GetGUID());
    ASSERT(map->IsGarrison());
    return map;
}

/*
- return the right instance for the object, based on its InstanceId
- create the instance if it's not created already
- the player is not actually added to the instance (only in InstanceMap::Add)
*/
Map* MapManager::CreateMap(uint32 mapId, Player* player)
{
    if (!player)
        return nullptr;

    MapEntry const* entry = sMapStore.LookupEntry(mapId);
    if (!entry)
        return nullptr;

    std::unique_lock<std::shared_mutex> lock(_mapsLock);

    Map* map = nullptr;
    uint32 newInstanceId = 0;                       // instanceId of the resulting map

    if (entry->IsBattlegroundOrArena())
    {
        // instantiate or find existing bg map for player
        // the instance id is set in battlegroundid
        newInstanceId = player->GetBattlegroundId();
        if (!newInstanceId)
            return nullptr;

        map = FindMap_i(mapId, newInstanceId);
        if (!map)
        {
            if (Battleground* bg = player->GetBattleground())
                map = CreateBattleground(mapId, newInstanceId, bg);
            else
            {
                player->TeleportToBGEntryPoint();
                return nullptr;
            }
        }
    }
    else if (entry->IsDungeon())
    {
        Group* group = player->GetGroup();
        Difficulty difficulty = group ? group->GetDifficultyID(entry) : player->GetDifficultyID(entry);
        MapDb2Entries entries{ entry, sDB2Manager.GetDownscaledMapDifficultyData(mapId, difficulty) };
        ObjectGuid instanceOwnerGuid = group ? group->GetRecentInstanceOwner(mapId) : player->GetGUID();
        InstanceLock* instanceLock = sInstanceLockMgr.FindActiveInstanceLock(instanceOwnerGuid, entries);
        if (instanceLock)
        {
            newInstanceId = instanceLock->GetInstanceId();

            // Reset difficulty to the one used in instance lock
            if (!entries.Map->IsFlexLocking())
                difficulty = instanceLock->GetDifficultyId();
        }
        else
        {
            // Try finding instance id for normal dungeon
            if (!entries.MapDifficulty->HasResetSchedule())
                newInstanceId = group ? group->GetRecentInstanceId(mapId) : player->GetRecentInstanceId(mapId);

            // If not found or instance is not a normal dungeon, generate new one
            if (!newInstanceId)
                newInstanceId = GenerateInstanceId();

            instanceLock = sInstanceLockMgr.CreateInstanceLockForNewInstance(instanceOwnerGuid, entries, newInstanceId);
        }

        // it is possible that the save exists but the map doesn't
        map = FindMap_i(mapId, newInstanceId);

        // is is also possible that instance id is already in use by another group for boss-based locks
        if (!entries.IsInstanceIdBound() && instanceLock && map && map->ToInstanceMap()->GetInstanceLock() != instanceLock)
        {
            newInstanceId = GenerateInstanceId();
            instanceLock->SetInstanceId(newInstanceId);
            map = nullptr;
        }

        if (!map)
        {
            map = CreateInstance(mapId, newInstanceId, instanceLock, difficulty, player->GetTeamId(), group);
            if (group)
                group->SetRecentInstance(mapId, instanceOwnerGuid, newInstanceId);
            else
                player->SetRecentInstance(mapId, newInstanceId);
        }
    }
    else if (entry->IsGarrison())
    {
        newInstanceId = player->GetGUID().GetCounter();
        map = FindMap_i(mapId, newInstanceId);
        if (!map)
            map = CreateGarrison(mapId, newInstanceId, player);
    }
    else
    {
        newInstanceId = 0;
        if (entry->IsSplitByFaction())
            newInstanceId = player->GetTeamId();

        map = FindMap_i(mapId, newInstanceId);
        if (!map)
            map = CreateWorldMap(mapId, newInstanceId);
    }

    if (map)
        i_maps[{ map->GetId(), map->GetInstanceId() }] = map;

    return map;
}

Map* MapManager::FindMap(uint32 mapId, uint32 instanceId) const
{
    std::shared_lock<std::shared_mutex> lock(_mapsLock);
    return FindMap_i(mapId, instanceId);
}

uint32 MapManager::FindInstanceIdForPlayer(uint32 mapId, Player const* player) const
{
    MapEntry const* entry = sMapStore.LookupEntry(mapId);
    if (!entry)
        return 0;

    if (entry->IsBattlegroundOrArena())
        return player->GetBattlegroundId();
    else if (entry->IsDungeon())
    {
        Group const* group = player->GetGroup();
        Difficulty difficulty = group ? group->GetDifficultyID(entry) : player->GetDifficultyID(entry);
        MapDb2Entries entries{ entry, sDB2Manager.GetDownscaledMapDifficultyData(mapId, difficulty) };
        ObjectGuid instanceOwnerGuid = group ? group->GetRecentInstanceOwner(mapId) : player->GetGUID();
        InstanceLock* instanceLock = sInstanceLockMgr.FindActiveInstanceLock(instanceOwnerGuid, entries);
        uint32 newInstanceId = 0;
        if (instanceLock)
            newInstanceId = instanceLock->GetInstanceId();
        else if (!entries.MapDifficulty->HasResetSchedule()) // Try finding instance id for normal dungeon
            newInstanceId = group ? group->GetRecentInstanceId(mapId) : player->GetRecentInstanceId(mapId);

        if (!newInstanceId)
            return 0;

        Map* map = FindMap(mapId, newInstanceId);

        // is is possible that instance id is already in use by another group for boss-based locks
        if (!entries.IsInstanceIdBound() && instanceLock && map && map->ToInstanceMap()->GetInstanceLock() != instanceLock)
            return 0;

        return newInstanceId;
    }
    else if (entry->IsGarrison())
        return uint32(player->GetGUID().GetCounter());
    else
    {
        if (entry->IsSplitByFaction())
            return player->GetTeamId();

        return 0;
    }
}

void MapManager::Update(uint32 diff)
{
    i_timer.Update(diff);
    if (!i_timer.Passed())
        return;

    MapMapType::iterator iter = i_maps.begin();
    while (iter != i_maps.end())
    {
        if (iter->second->CanUnload(diff))
        {
            if (DestroyMap(iter->second))
                iter = i_maps.erase(iter);
            else
                ++iter;

            continue;
        }

        if (m_updater.activated())
            m_updater.schedule_update(*iter->second, uint32(i_timer.GetCurrent()));
        else
            iter->second->Update(uint32(i_timer.GetCurrent()));

        ++iter;
    }
    if (m_updater.activated())
        m_updater.wait();

    for (iter = i_maps.begin(); iter != i_maps.end(); ++iter)
        iter->second->DelayedUpdate(uint32(i_timer.GetCurrent()));

    i_timer.SetCurrent(0);
}

bool MapManager::DestroyMap(Map* map)
{
    map->RemoveAllPlayers();
    if (map->HavePlayers())
        return false;

    map->UnloadAll();

    // Free up the instance id and allow it to be reused for bgs and arenas (other instances are handled in the InstanceSaveMgr)
    if (map->IsBattlegroundOrArena())
        sMapMgr->FreeInstanceId(map->GetInstanceId());

    // erase map
    delete map;
    return true;
}

bool MapManager::IsValidMAP(uint32 mapId)
{
    return sMapStore.LookupEntry(mapId) != nullptr;
}

void MapManager::UnloadAll()
{
    // first unload maps
    for (auto iter = i_maps.begin(); iter != i_maps.end(); ++iter)
        iter->second->UnloadAll();

    // then delete them
    for (auto iter = i_maps.begin(); iter != i_maps.end(); ++iter)
        delete iter->second;

    i_maps.clear();

    if (m_updater.activated())
        m_updater.deactivate();

    Map::DeleteStateMachine();
}

uint32 MapManager::GetNumInstances() const
{
    std::shared_lock<std::shared_mutex> lock(_mapsLock);
    return std::count_if(i_maps.begin(), i_maps.end(), [](MapMapType::value_type const& value) { return value.second->IsDungeon(); });
}

uint32 MapManager::GetNumPlayersInInstances() const
{
    std::shared_lock<std::shared_mutex> lock(_mapsLock);
    return std::accumulate(i_maps.begin(), i_maps.end(), 0u, [](uint32 total, MapMapType::value_type const& value) { return total + (value.second->IsDungeon() ? value.second->GetPlayers().getSize() : 0); });
}

void MapManager::InitInstanceIds()
{
    _nextInstanceId = 1;

    uint64 maxExistingInstanceId = 0;
    if (QueryResult result = CharacterDatabase.Query("SELECT IFNULL(MAX(instanceId), 0) FROM instance"))
        maxExistingInstanceId = std::max(maxExistingInstanceId, (*result)[0].GetUInt64());

    if (QueryResult result = CharacterDatabase.Query("SELECT IFNULL(MAX(instanceId), 0) FROM character_instance_lock"))
        maxExistingInstanceId = std::max(maxExistingInstanceId, (*result)[0].GetUInt64());

    _freeInstanceIds->resize(maxExistingInstanceId + 2, true); // make space for one extra to be able to access [_nextInstanceId] index in case all slots are taken

    // never allow 0 id
    _freeInstanceIds->set(0, false);
}

void MapManager::RegisterInstanceId(uint32 instanceId)
{
    // Allocation and sizing was done in InitInstanceIds()
    _freeInstanceIds->set(instanceId, false);

    // Instances are pulled in ascending order from db and nextInstanceId is initialized with 1,
    // so if the instance id is used, increment until we find the first unused one for a potential new instance
    if (_nextInstanceId == instanceId)
        ++_nextInstanceId;
}

uint32 MapManager::GenerateInstanceId()
{
    if (_nextInstanceId == 0xFFFFFFFF)
    {
        TC_LOG_ERROR("maps", "Instance ID overflow!! Can't continue, shutting down server. ");
        World::StopNow(ERROR_EXIT_CODE);
        return _nextInstanceId;
    }

    uint32 newInstanceId = _nextInstanceId;
    ASSERT(newInstanceId < _freeInstanceIds->size());
    _freeInstanceIds->set(newInstanceId, false);

    // Find the lowest available id starting from the current NextInstanceId (which should be the lowest according to the logic in FreeInstanceId())
    size_t nextFreedId = _freeInstanceIds->find_next(_nextInstanceId++);
    if (nextFreedId == InstanceIds::npos)
    {
        _nextInstanceId = uint32(_freeInstanceIds->size());
        _freeInstanceIds->push_back(true);
    }
    else
        _nextInstanceId = uint32(nextFreedId);

    return newInstanceId;
}

void MapManager::FreeInstanceId(uint32 instanceId)
{
    // If freed instance id is lower than the next id available for new instances, use the freed one instead
    _nextInstanceId = std::min(instanceId, _nextInstanceId);
    _freeInstanceIds->set(instanceId, true);
}

// hack to allow conditions to access what faction owns the map (these worldstates should not be set on these maps)
class SplitByFactionMapScript : public WorldMapScript
{
public:
    SplitByFactionMapScript(char const* name, uint32 mapId) : WorldMapScript(name, mapId)
    {
    }

    void OnCreate(Map* map) override
    {
        sWorldStateMgr->SetValue(WS_TEAM_IN_INSTANCE_ALLIANCE, map->GetInstanceId() == TEAM_ALLIANCE, false, map);
        sWorldStateMgr->SetValue(WS_TEAM_IN_INSTANCE_HORDE, map->GetInstanceId() == TEAM_HORDE, false, map);
    }
};

void MapManager::AddSC_BuiltInScripts()
{
    for (MapEntry const* mapEntry : sMapStore)
        if (mapEntry->IsWorldMap() && mapEntry->IsSplitByFaction())
            new SplitByFactionMapScript(Trinity::StringFormat("world_map_set_faction_worldstates_%u", mapEntry->ID).c_str(), mapEntry->ID);
}