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 "GridReference.h"
26#include "Timer.h"
27
28#define DEFAULT_VISIBILITY_NOTIFY_PERIOD 1000
29
31{
32public:
33 GridInfo();
34 GridInfo(time_t expiry, bool unload = true);
35 TimeTracker const& getTimeTracker() const { return i_timer; }
36 bool getUnloadLock() const { return i_unloadActiveLockCount || i_unloadExplicitLock; }
37 void setUnloadExplicitLock(bool on) { i_unloadExplicitLock = on; }
38 void incUnloadActiveLock() { ++i_unloadActiveLockCount; }
39 void decUnloadActiveLock() { if (i_unloadActiveLockCount) --i_unloadActiveLockCount; }
40
41 void setTimer(TimeTracker const& pTimer) { i_timer = pTimer; }
42 void ResetTimeTracker(time_t interval) { i_timer.Reset(interval); }
43 void UpdateTimeTracker(time_t diff) { i_timer.Update(diff); }
44 PeriodicTimer& getRelocationTimer() { return vis_Update; }
45private:
48
49 uint16 i_unloadActiveLockCount : 16; // lock from active object spawn points (prevent clone loading)
50 bool i_unloadExplicitLock : 1; // explicit manual lock or config setting
51};
52
53typedef enum
54{
61
62template
63<
64uint32 N,
65class ACTIVE_OBJECT,
66class WORLD_OBJECT_TYPES,
67class GRID_OBJECT_TYPES
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 T, class TT>
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 // Visit a single Grid (cell) in NGrid (grid)
144 template<class T, class TT>
146 {
147 GetGridType(x, y).Visit(visitor);
148 }
149
150 //This gets the player count in grid
151 //I disable this to avoid confusion (active object usually means something else)
152 /*
153 uint32 GetActiveObjectCountInGrid() const
154 {
155 uint32 count = 0;
156 for (uint32 x = 0; x < N; ++x)
157 for (uint32 y = 0; y < N; ++y)
158 count += i_cells[x][y].ActiveObjectsInGrid();
159 return count;
160 }
161 */
162
163 template<class T>
165 {
166 uint32 count = 0;
167 for (uint32 x = 0; x < N; ++x)
168 for (uint32 y = 0; y < N; ++y)
169 count += i_cells[x][y].template GetWorldObjectCountInGrid<T>();
170 return count;
171 }
172
173 private:
182};
183#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:54
@ GRID_STATE_REMOVAL
Definition: NGrid.h:58
@ GRID_STATE_INVALID
Definition: NGrid.h:55
@ GRID_STATE_IDLE
Definition: NGrid.h:57
@ GRID_STATE_ACTIVE
Definition: NGrid.h:56
@ MAX_GRID_STATE
Definition: NGrid.h:59
Definition: NGrid.h:31
bool i_unloadExplicitLock
Definition: NGrid.h:50
void decUnloadActiveLock()
Definition: NGrid.h:39
PeriodicTimer vis_Update
Definition: NGrid.h:47
TimeTracker const & getTimeTracker() const
Definition: NGrid.h:35
TimeTracker i_timer
Definition: NGrid.h:46
PeriodicTimer & getRelocationTimer()
Definition: NGrid.h:44
uint16 i_unloadActiveLockCount
Definition: NGrid.h:49
void setTimer(TimeTracker const &pTimer)
Definition: NGrid.h:41
void setUnloadExplicitLock(bool on)
Definition: NGrid.h:37
bool getUnloadLock() const
Definition: NGrid.h:36
void ResetTimeTracker(time_t interval)
Definition: NGrid.h:42
void UpdateTimeTracker(time_t diff)
Definition: NGrid.h:43
void incUnloadActiveLock()
Definition: NGrid.h:38
Definition: Grid.h:46
void Visit(TypeContainerVisitor< T, TypeMapContainer< GRID_OBJECT_TYPES > > &visitor)
Definition: Grid.h:88
Definition: NGrid.h:70
bool isGridObjectDataLoaded() const
Definition: NGrid.h:100
TimeTracker const & getTimeTracker() const
Definition: NGrid.h:104
GridReference< NGrid< N, ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES > > i_Reference
Definition: NGrid.h:176
int32 getX() const
Definition: NGrid.h:93
grid_state_t GetGridState(void) const
Definition: NGrid.h:91
void ResetTimeTracker(time_t interval)
Definition: NGrid.h:109
void decUnloadActiveLock()
Definition: NGrid.h:108
int32 i_y
Definition: NGrid.h:178
void setGridObjectDataLoaded(bool pLoaded)
Definition: NGrid.h:101
uint32 GetGridId(void) const
Definition: NGrid.h:90
grid_state_t i_cellstate
Definition: NGrid.h:179
uint32 GetWorldObjectCountInNGrid() const
Definition: NGrid.h:164
void SetGridState(grid_state_t s)
Definition: NGrid.h:92
bool i_GridObjectDataLoaded
Definition: NGrid.h:181
void setUnloadExplicitLock(bool on)
Definition: NGrid.h:106
GridType & GetGridType(const uint32 x, const uint32 y)
Definition: NGrid.h:78
void incUnloadActiveLock()
Definition: NGrid.h:107
GridInfo * getGridInfoRef()
Definition: NGrid.h:103
GridType i_cells[N][N]
Definition: NGrid.h:180
uint32 i_gridId
Definition: NGrid.h:174
void VisitGrid(const uint32 x, const uint32 y, TypeContainerVisitor< T, TypeMapContainer< TT > > &visitor)
Definition: NGrid.h:145
Grid< ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES > GridType
Definition: NGrid.h:72
int32 getY() const
Definition: NGrid.h:94
NGrid(uint32 id, int32 x, int32 y, time_t expiry, bool unload=true)
Definition: NGrid.h:73
void UpdateTimeTracker(time_t diff)
Definition: NGrid.h:110
GridType const & GetGridType(const uint32 x, const uint32 y) const
Definition: NGrid.h:84
int32 i_x
Definition: NGrid.h:177
bool getUnloadLock() const
Definition: NGrid.h:105
GridInfo i_GridInfo
Definition: NGrid.h:175
void link(GridRefManager< NGrid< N, ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES > > *pTo)
Definition: NGrid.h:96
void VisitAllGrids(TypeContainerVisitor< T, TypeMapContainer< TT > > &visitor)
Definition: NGrid.h:136
void link(TO *toObj, FROM *fromObj)
Definition: Reference.h:46