OregonCore  revision fb2a440-git
Your Favourite TBC server
NGrid.h
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore 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 OREGON_NGRID_H
19 #define OREGON_NGRID_H
20 
21 // NGrid is nothing more than a wrapper of the Grid with an NxN cells
22 
23 #include "GameSystem/Grid.h"
25 #include "Timer.h"
26 #include "Utilities/Util.h"
27 
28 #define DEFAULT_VISIBILITY_NOTIFY_PERIOD 1000
29 
30 class GridInfo
31 {
32 public:
36  GridInfo(time_t expiry, bool unload = true )
39  const TimeTracker& getTimeTracker() const { return i_timer; }
45 
46  void setTimer(const TimeTracker& pTimer) { i_timer = pTimer; }
47  void ResetTimeTracker(time_t interval) { i_timer.Reset(interval); }
48  void UpdateTimeTracker(time_t diff) { i_timer.Update(diff); }
50 private:
53 
54  uint16 i_unloadActiveLockCount : 16; // lock from active object spawn points (prevent clone loading)
55  bool i_unloadExplicitLock : 1; // explicit manual lock or config setting
56  bool i_unloadReferenceLock : 1; // lock from instance map copy
57 };
58 
59 typedef enum
60 {
66 } grid_state_t;
67 
68 template
69 <
70 uint32 N,
71 class ACTIVE_OBJECT,
72 class WORLD_OBJECT_TYPES,
73 class GRID_OBJECT_TYPES
74 >
75 class NGrid
76 {
77  public:
79  NGrid(uint32 id, int32 x, int32 y, time_t expiry, bool unload = true)
80  : i_gridId(id), i_x(x), i_y(y), i_cellstate(GRID_STATE_INVALID), i_GridObjectDataLoaded(false)
81  {
82  i_GridInfo = GridInfo(expiry, unload);
83  }
84 
85  GridType& GetGridType(const uint32 x, const uint32 y)
86  {
87  ASSERT(x < N && y < N);
88  return i_cells[x][y];
89  }
90 
91  GridType const& GetGridType(const uint32 x, const uint32 y) const
92  {
93  ASSERT(x < N && y < N);
94  return i_cells[x][y];
95  }
96 
97  uint32 GetGridId(void) const { return i_gridId; }
98  void SetGridId(const uint32 id) const { i_gridId = id; }
99  grid_state_t GetGridState(void) const { return i_cellstate; }
100  void SetGridState(grid_state_t s) { i_cellstate = s; }
101  int32 getX() const { return i_x; }
102  int32 getY() const { return i_y; }
103 
105  {
106  i_Reference.link(pTo, this);
107  }
108  bool isGridObjectDataLoaded() const { return i_GridObjectDataLoaded; }
109  void setGridObjectDataLoaded(bool pLoaded) { i_GridObjectDataLoaded = pLoaded; }
110 
111  GridInfo* getGridInfoRef() { return &i_GridInfo; }
112  const TimeTracker& getTimeTracker() const { return i_GridInfo.getTimeTracker(); }
113  bool getUnloadLock() const { return i_GridInfo.getUnloadLock(); }
114  void setUnloadExplicitLock(bool on) { i_GridInfo.setUnloadExplicitLock(on); }
115  void setUnloadReferenceLock(bool on) { i_GridInfo.setUnloadReferenceLock(on); }
116  void incUnloadActiveLock() { i_GridInfo.incUnloadActiveLock(); }
117  void decUnloadActiveLock() { i_GridInfo.decUnloadActiveLock(); }
118  void ResetTimeTracker(time_t interval) { i_GridInfo.ResetTimeTracker(interval); }
119  void UpdateTimeTracker(time_t diff) { i_GridInfo.UpdateTimeTracker(diff); }
120 
121  // Visit all Grids (cells) in NGrid (grid)
122  template<class T, class TT>
124  {
125  for (uint32 x = 0; x < N; ++x)
126  for (uint32 y = 0; y < N; ++y)
127  GetGridType(x, y).Visit(visitor);
128  }
129 
130  // Visit a single Grid (cell) in NGrid (grid)
131  template<class T, class TT>
132  void VisitGrid(const uint32 x, const uint32 y, TypeContainerVisitor<T, TypeMapContainer<TT> > &visitor)
133  {
134  GetGridType(x, y).Visit(visitor);
135  }
136 
137  template<class T>
139  {
140  uint32 count = 0;
141  for (uint32 x = 0; x < N; ++x)
142  for (uint32 y = 0; y < N; ++y)
143  count += i_cells[x][y].template GetWorldObjectCountInGrid<T>();
144  return count;
145  }
146 
147  private:
154  GridType i_cells[N][N];
156 };
157 #endif
158 
grid_state_t i_cellstate
Definition: NGrid.h:153
Definition: NGrid.h:75
void UpdateTimeTracker(time_t diff)
Definition: NGrid.h:48
void setUnloadExplicitLock(bool on)
Definition: NGrid.h:114
bool getUnloadLock() const
Definition: NGrid.h:113
bool i_unloadExplicitLock
Definition: NGrid.h:55
Definition: NGrid.h:30
int32 i_y
Definition: NGrid.h:152
void decUnloadActiveLock()
Definition: NGrid.h:117
void SetGridId(const uint32 id) const
Definition: NGrid.h:98
grid_state_t GetGridState(void) const
Definition: NGrid.h:99
void setGridObjectDataLoaded(bool pLoaded)
Definition: NGrid.h:109
uint16 i_unloadActiveLockCount
Definition: NGrid.h:54
void setUnloadExplicitLock(bool on)
Definition: NGrid.h:41
void UpdateTimeTracker(time_t diff)
Definition: NGrid.h:119
GridReference< NGrid< N, ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES > > i_Reference
Definition: NGrid.h:150
void ResetTimeTracker(time_t interval)
Definition: NGrid.h:118
TimeTracker i_timer
Definition: NGrid.h:51
#define DEFAULT_VISIBILITY_NOTIFY_PERIOD
Definition: NGrid.h:28
int32 irand(int32 min, int32 max)
Definition: Util.cpp:28
ACE_INT32 int32
Definition: Define.h:67
const TimeTracker & getTimeTracker() const
Definition: NGrid.h:112
NULL Dbg ErrDB Arena Chat Char Map MMap false
Definition: Log.cpp:556
Definition: Grid.h:45
grid_state_t
Definition: NGrid.h:59
void incUnloadActiveLock()
Definition: NGrid.h:43
void decUnloadActiveLock()
Definition: NGrid.h:44
NGrid(uint32 id, int32 x, int32 y, time_t expiry, bool unload=true)
Definition: NGrid.h:79
void Reset(time_t interval)
Definition: Timer.h:152
bool i_GridObjectDataLoaded
Definition: NGrid.h:155
GridType & GetGridType(const uint32 x, const uint32 y)
Definition: NGrid.h:85
int32 i_x
Definition: NGrid.h:151
uint32 GetWorldObjectCountInNGrid() const
Definition: NGrid.h:138
void setUnloadReferenceLock(bool on)
Definition: NGrid.h:115
const TimeTracker & getTimeTracker() const
Definition: NGrid.h:39
GridInfo()
Definition: NGrid.h:33
GridInfo(time_t expiry, bool unload=true)
Definition: NGrid.h:36
void VisitGrid(const uint32 x, const uint32 y, TypeContainerVisitor< T, TypeMapContainer< TT > > &visitor)
Definition: NGrid.h:132
void VisitAllGrids(TypeContainerVisitor< T, TypeMapContainer< TT > > &visitor)
Definition: NGrid.h:123
bool i_unloadReferenceLock
Definition: NGrid.h:56
void Update(time_t diff)
Definition: Timer.h:144
void ResetTimeTracker(time_t interval)
Definition: NGrid.h:47
void SetGridState(grid_state_t s)
Definition: NGrid.h:100
GridInfo * getGridInfoRef()
Definition: NGrid.h:111
PeriodicTimer vis_Update
Definition: NGrid.h:52
uint32 i_gridId
Definition: NGrid.h:148
void incUnloadActiveLock()
Definition: NGrid.h:116
int32 getY() const
Definition: NGrid.h:102
bool getUnloadLock() const
Definition: NGrid.h:40
int32 getX() const
Definition: NGrid.h:101
PeriodicTimer & getRelocationTimer()
Definition: NGrid.h:49
void setUnloadReferenceLock(bool on)
Definition: NGrid.h:42
void link(GridRefManager< NGrid< N, ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES > > *pTo)
Definition: NGrid.h:104
#define ASSERT
Definition: Errors.h:29
GridType const & GetGridType(const uint32 x, const uint32 y) const
Definition: NGrid.h:91
ACE_UINT16 uint16
Definition: Define.h:72
ACE_UINT32 uint32
Definition: Define.h:71
GridInfo i_GridInfo
Definition: NGrid.h:149
bool isGridObjectDataLoaded() const
Definition: NGrid.h:108
uint32 GetGridId(void) const
Definition: NGrid.h:97
void setTimer(const TimeTracker &pTimer)
Definition: NGrid.h:46
Grid< ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES > GridType
Definition: NGrid.h:78