TrinityCore
Loading...
Searching...
No Matches
NGrid.h
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#ifndef TRINITY_NGRID_H
19#define TRINITY_NGRID_H
20
24#include "Grid.h"
25#include "GridRefManager.h"
26#include "GridReference.h"
27#include "Timer.h"
28
29#define DEFAULT_VISIBILITY_NOTIFY_PERIOD 1000
30
32{
33public:
34 GridInfo();
35 GridInfo(time_t expiry, bool unload = true);
36 TimeTracker const& getTimeTracker() const { return i_timer; }
37 bool getUnloadLock() const { return i_unloadActiveLockCount || i_unloadExplicitLock; }
38 void setUnloadExplicitLock(bool on) { i_unloadExplicitLock = on; }
39 void incUnloadActiveLock() { ++i_unloadActiveLockCount; }
40 void decUnloadActiveLock() { if (i_unloadActiveLockCount) --i_unloadActiveLockCount; }
41
42 void setTimer(TimeTracker const& pTimer) { i_timer = pTimer; }
43 void ResetTimeTracker(time_t interval) { i_timer.Reset(interval); }
44 void UpdateTimeTracker(time_t diff) { i_timer.Update(diff); }
45 PeriodicTimer& getRelocationTimer() { return vis_Update; }
46private:
49
50 uint16 i_unloadActiveLockCount : 16; // lock from active object spawn points (prevent clone loading)
51 bool i_unloadExplicitLock : 1; // explicit manual lock or config setting
52};
53
62
63template
64<
65uint32 N,
66class WORLD_OBJECT_CONTAINER,
67class GRID_OBJECT_CONTAINER
68>
69class NGrid
70{
71 public:
73 NGrid(uint32 id, int32 x, int32 y, time_t expiry, bool unload = true) :
74 i_gridId(id), i_GridInfo(GridInfo(expiry, unload)), i_x(x), i_y(y),
76 { }
77
78 GridType& GetGridType(const uint32 x, const uint32 y)
79 {
80 ASSERT(x < N && y < N);
81 return i_cells[x][y];
82 }
83
84 GridType const& GetGridType(const uint32 x, const uint32 y) const
85 {
86 ASSERT(x < N && y < N);
87 return i_cells[x][y];
88 }
89
90 uint32 GetGridId(void) const { return i_gridId; }
91 grid_state_t GetGridState(void) const { return i_cellstate; }
93 int32 getX() const { return i_x; }
94 int32 getY() const { return i_y; }
95
97 {
98 i_Reference.link(pTo, this);
99 }
101 void setGridObjectDataLoaded(bool pLoaded) { i_GridObjectDataLoaded = pLoaded; }
102
105 bool getUnloadLock() const { return i_GridInfo.getUnloadLock(); }
109 void ResetTimeTracker(time_t interval) { i_GridInfo.ResetTimeTracker(interval); }
110 void UpdateTimeTracker(time_t diff) { i_GridInfo.UpdateTimeTracker(diff); }
111
112 /*
113 template<class SPECIFIC_OBJECT> void AddWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
114 {
115 GetGridType(x, y).AddWorldObject(obj);
116 }
117
118 template<class SPECIFIC_OBJECT> void RemoveWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
119 {
120 GetGridType(x, y).RemoveWorldObject(obj);
121 }
122
123 template<class SPECIFIC_OBJECT> void AddGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
124 {
125 GetGridType(x, y).AddGridObject(obj);
126 }
127
128 template<class SPECIFIC_OBJECT> void RemoveGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
129 {
130 GetGridType(x, y).RemoveGridObject(obj);
131 }
132 */
133
134 // Visit all Grids (cells) in NGrid (grid)
135 template<class VISITOR>
137 {
138 for (uint32 x = 0; x < N; ++x)
139 for (uint32 y = 0; y < N; ++y)
140 GetGridType(x, y).Visit(visitor);
141 }
142
143 template<class VISITOR>
145 {
146 for (uint32 x = 0; x < N; ++x)
147 for (uint32 y = 0; y < N; ++y)
148 GetGridType(x, y).Visit(visitor);
149 }
150
151 // Visit a single Grid (cell) in NGrid (grid)
152 template<class VISITOR>
157
158 template<class VISITOR>
163
164 template<class T>
165 std::size_t GetWorldObjectCountInNGrid() const
166 {
167 uint32 count = 0;
168 for (uint32 x = 0; x < N; ++x)
169 for (uint32 y = 0; y < N; ++y)
170 count += i_cells[x][y].template GetWorldObjectCountInGrid<T>();
171 return count;
172 }
173
174 template<class T>
175 std::size_t GetGridObjectCountInNGrid() const
176 {
177 uint32 count = 0;
178 for (uint32 x = 0; x < N; ++x)
179 for (uint32 y = 0; y < N; ++y)
180 count += i_cells[x][y].template GetGridObjectCountInGrid<T>();
181 return count;
182 }
183
184 template<class T>
186 {
187 for (uint32 x = 0; x < N; ++x)
188 for (uint32 y = 0; y < N; ++y)
189 if (i_cells[x][y].template GetWorldObjectCountInGrid<T>() != 0)
190 return true;
191 return false;
192 }
193
194 template<class T>
196 {
197 for (uint32 x = 0; x < N; ++x)
198 for (uint32 y = 0; y < N; ++y)
199 if (i_cells[x][y].template GetGridObjectCountInGrid<T>() != 0)
200 return true;
201 return false;
202 }
203
204 private:
213};
214#endif
#define TC_GAME_API
Definition Define.h:129
int32_t int32
Definition Define.h:150
uint16_t uint16
Definition Define.h:155
uint32_t uint32
Definition Define.h:154
#define ASSERT
Definition Errors.h:80
grid_state_t
Definition NGrid.h:55
@ GRID_STATE_REMOVAL
Definition NGrid.h:59
@ GRID_STATE_INVALID
Definition NGrid.h:56
@ GRID_STATE_IDLE
Definition NGrid.h:58
@ GRID_STATE_ACTIVE
Definition NGrid.h:57
@ MAX_GRID_STATE
Definition NGrid.h:60
bool i_unloadExplicitLock
Definition NGrid.h:51
void decUnloadActiveLock()
Definition NGrid.h:40
PeriodicTimer vis_Update
Definition NGrid.h:48
TimeTracker const & getTimeTracker() const
Definition NGrid.h:36
TimeTracker i_timer
Definition NGrid.h:47
PeriodicTimer & getRelocationTimer()
Definition NGrid.h:45
uint16 i_unloadActiveLockCount
Definition NGrid.h:50
void setTimer(TimeTracker const &pTimer)
Definition NGrid.h:42
void setUnloadExplicitLock(bool on)
Definition NGrid.h:38
bool getUnloadLock() const
Definition NGrid.h:37
void ResetTimeTracker(time_t interval)
Definition NGrid.h:43
void UpdateTimeTracker(time_t diff)
Definition NGrid.h:44
void incUnloadActiveLock()
Definition NGrid.h:39
Definition Grid.h:42
void Visit(TypeContainerVisitor< T, GRID_OBJECT_CONTAINER > &visitor)
Definition Grid.h:72
Definition NGrid.h:70
void VisitGrid(uint32 x, uint32 y, TypeContainerVisitor< VISITOR, WORLD_OBJECT_CONTAINER > &visitor)
Definition NGrid.h:153
void ResetTimeTracker(time_t interval)
Definition NGrid.h:109
void decUnloadActiveLock()
Definition NGrid.h:108
void setUnloadExplicitLock(bool on)
Definition NGrid.h:106
void SetGridState(grid_state_t s)
Definition NGrid.h:92
GridType const & GetGridType(const uint32 x, const uint32 y) const
Definition NGrid.h:84
void UpdateTimeTracker(time_t diff)
Definition NGrid.h:110
TimeTracker const & getTimeTracker() const
Definition NGrid.h:104
grid_state_t GetGridState(void) const
Definition NGrid.h:91
void VisitAllGrids(TypeContainerVisitor< VISITOR, WORLD_OBJECT_CONTAINER > &visitor)
Definition NGrid.h:136
bool i_GridObjectDataLoaded
Definition NGrid.h:212
GridType & GetGridType(const uint32 x, const uint32 y)
Definition NGrid.h:78
int32 i_y
Definition NGrid.h:209
Grid< WORLD_OBJECT_CONTAINER, GRID_OBJECT_CONTAINER > GridType
Definition NGrid.h:72
void VisitAllGrids(TypeContainerVisitor< VISITOR, GRID_OBJECT_CONTAINER > &visitor)
Definition NGrid.h:144
GridInfo i_GridInfo
Definition NGrid.h:206
bool HasWorldObjectsInNGrid() const
Definition NGrid.h:185
GridType i_cells[N][N]
Definition NGrid.h:211
bool HasGridObjectsInNGrid() const
Definition NGrid.h:195
uint32 i_gridId
Definition NGrid.h:205
void setGridObjectDataLoaded(bool pLoaded)
Definition NGrid.h:101
bool isGridObjectDataLoaded() const
Definition NGrid.h:100
uint32 GetGridId(void) const
Definition NGrid.h:90
std::size_t GetWorldObjectCountInNGrid() const
Definition NGrid.h:165
std::size_t GetGridObjectCountInNGrid() const
Definition NGrid.h:175
bool getUnloadLock() const
Definition NGrid.h:105
void link(GridRefManager< NGrid > *pTo)
Definition NGrid.h:96
int32 getX() const
Definition NGrid.h:93
GridInfo * getGridInfoRef()
Definition NGrid.h:103
NGrid(uint32 id, int32 x, int32 y, time_t expiry, bool unload=true)
Definition NGrid.h:73
GridReference< NGrid > i_Reference
Definition NGrid.h:207
int32 getY() const
Definition NGrid.h:94
int32 i_x
Definition NGrid.h:208
grid_state_t i_cellstate
Definition NGrid.h:210
void VisitGrid(uint32 x, uint32 y, TypeContainerVisitor< VISITOR, GRID_OBJECT_CONTAINER > &visitor)
Definition NGrid.h:159
void incUnloadActiveLock()
Definition NGrid.h:107
void link(TO *toObj, FROM *fromObj)
Definition Reference.h:47