TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
NGrid.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TRINITY_NGRID_H
20 #define TRINITY_NGRID_H
21 
25 #include "Grid.h"
26 #include "GridReference.h"
27 #include "Timer.h"
28 #include "Util.h"
29 
30 #define DEFAULT_VISIBILITY_NOTIFY_PERIOD 1000
31 
32 class GridInfo
33 {
34 public:
38  GridInfo(time_t expiry, bool unload = true )
41  const TimeTracker& getTimeTracker() const { return i_timer; }
47 
48  void setTimer(const TimeTracker& pTimer) { i_timer = pTimer; }
49  void ResetTimeTracker(time_t interval) { i_timer.Reset(interval); }
50  void UpdateTimeTracker(time_t diff) { i_timer.Update(diff); }
52 private:
55 
56  uint16 i_unloadActiveLockCount : 16; // lock from active object spawn points (prevent clone loading)
57  bool i_unloadExplicitLock : 1; // explicit manual lock or config setting
58  bool i_unloadReferenceLock : 1; // lock from instance map copy
59 };
60 
61 typedef enum
62 {
68 } grid_state_t;
69 
70 template
71 <
72 uint32 N,
73 class ACTIVE_OBJECT,
74 class WORLD_OBJECT_TYPES,
75 class GRID_OBJECT_TYPES
76 >
77 class NGrid
78 {
79  public:
81  NGrid(uint32 id, int32 x, int32 y, time_t expiry, bool unload = true) :
82  i_gridId(id), i_GridInfo(GridInfo(expiry, unload)), i_x(x), i_y(y),
84  { }
85 
86  GridType& GetGridType(const uint32 x, const uint32 y)
87  {
88  ASSERT(x < N && y < N);
89  return i_cells[x][y];
90  }
91 
92  GridType const& GetGridType(const uint32 x, const uint32 y) const
93  {
94  ASSERT(x < N && y < N);
95  return i_cells[x][y];
96  }
97 
98  uint32 GetGridId(void) const { return i_gridId; }
99  void SetGridId(const uint32 id) const { i_gridId = id; }
100  grid_state_t GetGridState(void) const { return i_cellstate; }
102  int32 getX() const { return i_x; }
103  int32 getY() const { return i_y; }
104 
106  {
107  i_Reference.link(pTo, this);
108  }
110  void setGridObjectDataLoaded(bool pLoaded) { i_GridObjectDataLoaded = pLoaded; }
111 
114  bool getUnloadLock() const { return i_GridInfo.getUnloadLock(); }
119  void ResetTimeTracker(time_t interval) { i_GridInfo.ResetTimeTracker(interval); }
120  void UpdateTimeTracker(time_t diff) { i_GridInfo.UpdateTimeTracker(diff); }
121 
122  /*
123  template<class SPECIFIC_OBJECT> void AddWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
124  {
125  GetGridType(x, y).AddWorldObject(obj);
126  }
127 
128  template<class SPECIFIC_OBJECT> void RemoveWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
129  {
130  GetGridType(x, y).RemoveWorldObject(obj);
131  }
132 
133  template<class SPECIFIC_OBJECT> void AddGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
134  {
135  GetGridType(x, y).AddGridObject(obj);
136  }
137 
138  template<class SPECIFIC_OBJECT> void RemoveGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
139  {
140  GetGridType(x, y).RemoveGridObject(obj);
141  }
142  */
143 
144  // Visit all Grids (cells) in NGrid (grid)
145  template<class T, class TT>
147  {
148  for (uint32 x = 0; x < N; ++x)
149  for (uint32 y = 0; y < N; ++y)
150  GetGridType(x, y).Visit(visitor);
151  }
152 
153  // Visit a single Grid (cell) in NGrid (grid)
154  template<class T, class TT>
156  {
157  GetGridType(x, y).Visit(visitor);
158  }
159 
160  //This gets the player count in grid
161  //I disable this to avoid confusion (active object usually means something else)
162  /*
163  uint32 GetActiveObjectCountInGrid() const
164  {
165  uint32 count = 0;
166  for (uint32 x = 0; x < N; ++x)
167  for (uint32 y = 0; y < N; ++y)
168  count += i_cells[x][y].ActiveObjectsInGrid();
169  return count;
170  }
171  */
172 
173  template<class T>
175  {
176  uint32 count = 0;
177  for (uint32 x = 0; x < N; ++x)
178  for (uint32 y = 0; y < N; ++y)
179  count += i_cells[x][y].template GetWorldObjectCountInGrid<T>();
180  return count;
181  }
182 
183  private:
190  GridType i_cells[N][N];
192 };
193 #endif
grid_state_t i_cellstate
Definition: NGrid.h:189
Definition: NGrid.h:77
void UpdateTimeTracker(time_t diff)
Definition: NGrid.h:50
void setUnloadExplicitLock(bool on)
Definition: NGrid.h:115
bool getUnloadLock() const
Definition: NGrid.h:114
bool i_unloadExplicitLock
Definition: NGrid.h:57
Definition: TypeContainerVisitor.h:32
Definition: NGrid.h:32
int32 i_y
Definition: NGrid.h:188
void decUnloadActiveLock()
Definition: NGrid.h:118
void SetGridId(const uint32 id) const
Definition: NGrid.h:99
grid_state_t GetGridState(void) const
Definition: NGrid.h:100
void setGridObjectDataLoaded(bool pLoaded)
Definition: NGrid.h:110
uint16 i_unloadActiveLockCount
Definition: NGrid.h:56
void setUnloadExplicitLock(bool on)
Definition: NGrid.h:43
void UpdateTimeTracker(time_t diff)
Definition: NGrid.h:120
GridReference< NGrid< N, ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES > > i_Reference
Definition: NGrid.h:186
void ResetTimeTracker(time_t interval)
Definition: NGrid.h:119
TimeTracker i_timer
Definition: NGrid.h:53
#define DEFAULT_VISIBILITY_NOTIFY_PERIOD
Definition: NGrid.h:30
#define false
Definition: CascPort.h:18
const TimeTracker & getTimeTracker() const
Definition: NGrid.h:113
Definition: Grid.h:46
grid_state_t
Definition: NGrid.h:61
void incUnloadActiveLock()
Definition: NGrid.h:45
void decUnloadActiveLock()
Definition: NGrid.h:46
NGrid(uint32 id, int32 x, int32 y, time_t expiry, bool unload=true)
Definition: NGrid.h:81
void Reset(time_t interval)
Definition: Timer.h:119
bool i_GridObjectDataLoaded
Definition: NGrid.h:191
GridType & GetGridType(const uint32 x, const uint32 y)
Definition: NGrid.h:86
Definition: TypeContainer.h:86
Definition: NGrid.h:64
GridType i_cells[N][N]
Definition: NGrid.h:190
int32 i_x
Definition: NGrid.h:187
uint32 GetWorldObjectCountInNGrid() const
Definition: NGrid.h:174
int32 irand(int32 min, int32 max)
Definition: Random.cpp:39
void setUnloadReferenceLock(bool on)
Definition: NGrid.h:116
Definition: NGrid.h:65
const TimeTracker & getTimeTracker() const
Definition: NGrid.h:41
Definition: NGrid.h:63
GridInfo()
Definition: NGrid.h:35
GridInfo(time_t expiry, bool unload=true)
Definition: NGrid.h:38
Definition: GridReference.h:25
Definition: NGrid.h:66
Definition: Timer.h:168
void VisitGrid(const uint32 x, const uint32 y, TypeContainerVisitor< T, TypeMapContainer< TT > > &visitor)
Definition: NGrid.h:155
void VisitAllGrids(TypeContainerVisitor< T, TypeMapContainer< TT > > &visitor)
Definition: NGrid.h:146
bool i_unloadReferenceLock
Definition: NGrid.h:58
int32_t int32
Definition: Define.h:146
void Update(time_t diff)
Definition: Timer.h:109
void ResetTimeTracker(time_t interval)
Definition: NGrid.h:49
uint32_t uint32
Definition: Define.h:150
G3D::int16 y
Definition: Vector2int16.h:38
uint16_t uint16
Definition: Define.h:151
void SetGridState(grid_state_t s)
Definition: NGrid.h:101
GridInfo * getGridInfoRef()
Definition: NGrid.h:112
PeriodicTimer vis_Update
Definition: NGrid.h:54
uint32 i_gridId
Definition: NGrid.h:184
void incUnloadActiveLock()
Definition: NGrid.h:117
void link(TO *toObj, FROM *fromObj)
Definition: Reference.h:46
Definition: GridReference.h:28
int32 getY() const
Definition: NGrid.h:103
void Visit(TypeContainerVisitor< T, TypeMapContainer< GRID_OBJECT_TYPES > > &visitor)
Definition: Grid.h:89
bool getUnloadLock() const
Definition: NGrid.h:42
int32 getX() const
Definition: NGrid.h:102
Definition: Timer.h:100
PeriodicTimer & getRelocationTimer()
Definition: NGrid.h:51
Definition: NGrid.h:67
void setUnloadReferenceLock(bool on)
Definition: NGrid.h:44
void link(GridRefManager< NGrid< N, ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES > > *pTo)
Definition: NGrid.h:105
#define ASSERT
Definition: Errors.h:55
GridType const & GetGridType(const uint32 x, const uint32 y) const
Definition: NGrid.h:92
G3D::int16 x
Definition: Vector2int16.h:37
GridInfo i_GridInfo
Definition: NGrid.h:185
bool isGridObjectDataLoaded() const
Definition: NGrid.h:109
uint32 GetGridId(void) const
Definition: NGrid.h:98
void setTimer(const TimeTracker &pTimer)
Definition: NGrid.h:48
Grid< ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES > GridType
Definition: NGrid.h:80