TrinityCore
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
54typedef enum
55{
62
63template
64<
65uint32 N,
66class ACTIVE_OBJECT,
67class WORLD_OBJECT_CONTAINER,
68class GRID_OBJECT_CONTAINER
69>
70class NGrid
71{
72 public:
74 NGrid(uint32 id, int32 x, int32 y, time_t expiry, bool unload = true) :
75 i_gridId(id), i_GridInfo(GridInfo(expiry, unload)), i_x(x), i_y(y),
77 { }
78
79 GridType& GetGridType(const uint32 x, const uint32 y)
80 {
81 ASSERT(x < N && y < N);
82 return i_cells[x][y];
83 }
84
85 GridType const& GetGridType(const uint32 x, const uint32 y) const
86 {
87 ASSERT(x < N && y < N);
88 return i_cells[x][y];
89 }
90
91 uint32 GetGridId(void) const { return i_gridId; }
92 grid_state_t GetGridState(void) const { return i_cellstate; }
94 int32 getX() const { return i_x; }
95 int32 getY() const { return i_y; }
96
98 {
99 i_Reference.link(pTo, this);
100 }
102 void setGridObjectDataLoaded(bool pLoaded) { i_GridObjectDataLoaded = pLoaded; }
103
106 bool getUnloadLock() const { return i_GridInfo.getUnloadLock(); }
110 void ResetTimeTracker(time_t interval) { i_GridInfo.ResetTimeTracker(interval); }
111 void UpdateTimeTracker(time_t diff) { i_GridInfo.UpdateTimeTracker(diff); }
112
113 /*
114 template<class SPECIFIC_OBJECT> void AddWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
115 {
116 GetGridType(x, y).AddWorldObject(obj);
117 }
118
119 template<class SPECIFIC_OBJECT> void RemoveWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
120 {
121 GetGridType(x, y).RemoveWorldObject(obj);
122 }
123
124 template<class SPECIFIC_OBJECT> void AddGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
125 {
126 GetGridType(x, y).AddGridObject(obj);
127 }
128
129 template<class SPECIFIC_OBJECT> void RemoveGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
130 {
131 GetGridType(x, y).RemoveGridObject(obj);
132 }
133 */
134
135 // Visit all Grids (cells) in NGrid (grid)
136 template<class VISITOR>
138 {
139 for (uint32 x = 0; x < N; ++x)
140 for (uint32 y = 0; y < N; ++y)
141 GetGridType(x, y).Visit(visitor);
142 }
143
144 template<class VISITOR>
146 {
147 for (uint32 x = 0; x < N; ++x)
148 for (uint32 y = 0; y < N; ++y)
149 GetGridType(x, y).Visit(visitor);
150 }
151
152 // Visit a single Grid (cell) in NGrid (grid)
153 template<class VISITOR>
155 {
156 GetGridType(x, y).Visit(visitor);
157 }
158
159 template<class VISITOR>
161 {
162 GetGridType(x, y).Visit(visitor);
163 }
164
165 //This gets the player count in grid
166 //I disable this to avoid confusion (active object usually means something else)
167 /*
168 uint32 GetActiveObjectCountInGrid() const
169 {
170 uint32 count = 0;
171 for (uint32 x = 0; x < N; ++x)
172 for (uint32 y = 0; y < N; ++y)
173 count += i_cells[x][y].ActiveObjectsInGrid();
174 return count;
175 }
176 */
177
178 template<class T>
180 {
181 uint32 count = 0;
182 for (uint32 x = 0; x < N; ++x)
183 for (uint32 y = 0; y < N; ++y)
184 count += i_cells[x][y].template GetWorldObjectCountInGrid<T>();
185 return count;
186 }
187
188 private:
197};
198#endif
#define TC_GAME_API
Definition: Define.h:123
int32_t int32
Definition: Define.h:138
uint16_t uint16
Definition: Define.h:143
uint32_t uint32
Definition: Define.h:142
#define ASSERT
Definition: Errors.h:68
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
Definition: NGrid.h:32
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:46
void Visit(TypeContainerVisitor< T, GRID_OBJECT_CONTAINER > &visitor)
Definition: Grid.h:88
Definition: NGrid.h:71
GridReference< NGrid > i_Reference
Definition: NGrid.h:191
grid_state_t i_cellstate
Definition: NGrid.h:194
GridType i_cells[N][N]
Definition: NGrid.h:195
void decUnloadActiveLock()
Definition: NGrid.h:109
int32 i_y
Definition: NGrid.h:193
GridType & GetGridType(const uint32 x, const uint32 y)
Definition: NGrid.h:79
grid_state_t GetGridState(void) const
Definition: NGrid.h:92
GridType const & GetGridType(const uint32 x, const uint32 y) const
Definition: NGrid.h:85
uint32 i_gridId
Definition: NGrid.h:189
void VisitGrid(uint32 x, uint32 y, TypeContainerVisitor< VISITOR, WORLD_OBJECT_CONTAINER > &visitor)
Definition: NGrid.h:154
void ResetTimeTracker(time_t interval)
Definition: NGrid.h:110
int32 i_x
Definition: NGrid.h:192
void VisitAllGrids(TypeContainerVisitor< VISITOR, GRID_OBJECT_CONTAINER > &visitor)
Definition: NGrid.h:145
Grid< ACTIVE_OBJECT, WORLD_OBJECT_CONTAINER, GRID_OBJECT_CONTAINER > GridType
Definition: NGrid.h:73
void SetGridState(grid_state_t s)
Definition: NGrid.h:93
bool isGridObjectDataLoaded() const
Definition: NGrid.h:101
NGrid(uint32 id, int32 x, int32 y, time_t expiry, bool unload=true)
Definition: NGrid.h:74
void VisitAllGrids(TypeContainerVisitor< VISITOR, WORLD_OBJECT_CONTAINER > &visitor)
Definition: NGrid.h:137
TimeTracker const & getTimeTracker() const
Definition: NGrid.h:105
void setGridObjectDataLoaded(bool pLoaded)
Definition: NGrid.h:102
void link(GridRefManager< NGrid > *pTo)
Definition: NGrid.h:97
int32 getX() const
Definition: NGrid.h:94
int32 getY() const
Definition: NGrid.h:95
GridInfo i_GridInfo
Definition: NGrid.h:190
void setUnloadExplicitLock(bool on)
Definition: NGrid.h:107
void VisitGrid(uint32 x, uint32 y, TypeContainerVisitor< VISITOR, GRID_OBJECT_CONTAINER > &visitor)
Definition: NGrid.h:160
GridInfo * getGridInfoRef()
Definition: NGrid.h:104
uint32 GetWorldObjectCountInNGrid() const
Definition: NGrid.h:179
void incUnloadActiveLock()
Definition: NGrid.h:108
bool i_GridObjectDataLoaded
Definition: NGrid.h:196
uint32 GetGridId(void) const
Definition: NGrid.h:91
void UpdateTimeTracker(time_t diff)
Definition: NGrid.h:111
bool getUnloadLock() const
Definition: NGrid.h:106
void link(TO *toObj, FROM *fromObj)
Definition: Reference.h:47