summaryrefslogtreecommitdiff
path: root/src/server/game/Grids/MapGridManager.cpp
blob: 21ca5bfed31199e7fba32fe02e02d9c3c2f016bd (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
#include "MapGridManager.h"
#include "GridObjectLoader.h"
#include "GridTerrainLoader.h"

void MapGridManager::CreateGrid(uint16 const x, uint16 const y)
{
    std::lock_guard<std::mutex> guard(_gridLock);
    if (IsGridCreated(x, y))
        return;

    std::unique_ptr<MapGridType> grid = std::make_unique<MapGridType>(x, y);
    grid->link(_map);

    GridTerrainLoader loader(*grid, _map);
    loader.LoadTerrain();

    _mapGrid[x][y] = std::move(grid);

    ++_createdGridsCount;
}

bool MapGridManager::LoadGrid(uint16 const x, uint16 const y)
{
    MapGridType* grid = GetGrid(x, y);
    if (!grid || grid->IsObjectDataLoaded())
        return false;

    // Must mark as loaded first, as GridObjectLoader spawning objects can attempt to recursively load the grid
    grid->SetObjectDataLoaded();

    GridObjectLoader loader(*grid, _map);
    loader.LoadAllCellsInGrid();

    ++_loadedGridsCount;
    return true;
}

void MapGridManager::UnloadGrid(uint16 const x, uint16 const y)
{
    MapGridType* grid = GetGrid(x, y);
    if (!grid)
        return;

    {
        GridObjectCleaner worker;
        TypeContainerVisitor<GridObjectCleaner, GridTypeMapContainer> visitor(worker);
        grid->VisitAllCells(visitor);
    }

    _map->RemoveAllObjectsInRemoveList();

    {
        GridObjectUnloader worker;
        TypeContainerVisitor<GridObjectUnloader, GridTypeMapContainer> visitor(worker);
        grid->VisitAllCells(visitor);
    }

    GridTerrainUnloader terrainUnloader(*grid, _map);
    terrainUnloader.UnloadTerrain();

    _mapGrid[x][y] = nullptr;
}

bool MapGridManager::IsGridCreated(uint16 const x, uint16 const y) const
{
    if (!MapGridManager::IsValidGridCoordinates(x, y))
        return false;

    return _mapGrid[x][y].get();
}

bool MapGridManager::IsGridLoaded(uint16 const x, uint16 const y) const
{
    if (!MapGridManager::IsValidGridCoordinates(x, y))
        return false;

    return _mapGrid[x][y].get() && _mapGrid[x][y]->IsObjectDataLoaded();
}

MapGridType* MapGridManager::GetGrid(uint16 const x, uint16 const y)
{
    if (!MapGridManager::IsValidGridCoordinates(x, y))
        return nullptr;

    return _mapGrid[x][y].get();
}

uint32 MapGridManager::GetCreatedGridsCount()
{
    return _createdGridsCount;
}

uint32 MapGridManager::GetLoadedGridsCount()
{
    return _loadedGridsCount;
}

uint32 MapGridManager::GetCreatedCellsInGridCount(uint16 const x, uint16 const y)
{
    MapGridType* grid = GetGrid(x, y);
    if (grid)
        return grid->GetCreatedCellsCount();

    return 0;
}

uint32 MapGridManager::GetCreatedCellsInMapCount()
{
    uint32 count = 0;
    for (uint32 gridX = 0; gridX < MAX_NUMBER_OF_GRIDS; ++gridX)
    {
        for (uint32 gridY = 0; gridY < MAX_NUMBER_OF_GRIDS; ++gridY)
        {
            if (MapGridType* grid = GetGrid(gridX, gridY))
                count += grid->GetCreatedCellsCount();
        }
    }
    return count;
}

bool MapGridManager::IsGridsFullyCreated() const
{
    return _createdGridsCount == (MAX_NUMBER_OF_GRIDS * MAX_NUMBER_OF_GRIDS);
}

bool MapGridManager::IsGridsFullyLoaded() const
{
    return _loadedGridsCount == (MAX_NUMBER_OF_GRIDS * MAX_NUMBER_OF_GRIDS);
}