| 1 | /* |
|---|
| 2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
|---|
| 3 | * |
|---|
| 4 | * This program is free software; you can redistribute it and/or modify |
|---|
| 5 | * it under the terms of the GNU General Public License as published by |
|---|
| 6 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 | * (at your option) any later version. |
|---|
| 8 | * |
|---|
| 9 | * This program is distributed in the hope that it will be useful, |
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | * GNU General Public License for more details. |
|---|
| 13 | * |
|---|
| 14 | * You should have received a copy of the GNU General Public License |
|---|
| 15 | * along with this program; if not, write to the Free Software |
|---|
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #ifndef MANGOS_NGRID_H |
|---|
| 20 | #define MANGOS_NGRID_H |
|---|
| 21 | |
|---|
| 22 | /** NGrid is nothing more than a wrapper of the Grid with an NxN cells |
|---|
| 23 | */ |
|---|
| 24 | |
|---|
| 25 | #include "GameSystem/Grid.h" |
|---|
| 26 | #include "GameSystem/GridReference.h" |
|---|
| 27 | #include "Timer.h" |
|---|
| 28 | |
|---|
| 29 | class GridInfo |
|---|
| 30 | { |
|---|
| 31 | public: |
|---|
| 32 | GridInfo() : i_timer(0) {} |
|---|
| 33 | GridInfo(time_t expiry, bool unload = true ) : i_timer(expiry), i_unloadflag(unload) {} |
|---|
| 34 | const TimeTracker& getTimeTracker() const { return i_timer; } |
|---|
| 35 | bool getUnloadFlag() const { return i_unloadflag; } |
|---|
| 36 | void setUnloadFlag( bool pFlag) { i_unloadflag = pFlag; } |
|---|
| 37 | void setTimer(const TimeTracker& pTimer) { i_timer = pTimer; } |
|---|
| 38 | void ResetTimeTracker(time_t interval) { i_timer.Reset(interval); } |
|---|
| 39 | void UpdateTimeTracker(time_t diff) { i_timer.Update(diff); } |
|---|
| 40 | |
|---|
| 41 | private: |
|---|
| 42 | TimeTracker i_timer; |
|---|
| 43 | bool i_unloadflag; |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | typedef enum |
|---|
| 47 | { |
|---|
| 48 | GRID_STATE_INVALID = 0, |
|---|
| 49 | GRID_STATE_ACTIVE = 1, |
|---|
| 50 | GRID_STATE_IDLE = 2, |
|---|
| 51 | GRID_STATE_REMOVAL= 3, |
|---|
| 52 | MAX_GRID_STATE = 4 |
|---|
| 53 | } grid_state_t; |
|---|
| 54 | |
|---|
| 55 | template |
|---|
| 56 | < |
|---|
| 57 | unsigned int N, |
|---|
| 58 | class ACTIVE_OBJECT, |
|---|
| 59 | class WORLD_OBJECT_TYPES, |
|---|
| 60 | class GRID_OBJECT_TYPES, |
|---|
| 61 | class ThreadModel = MaNGOS::SingleThreaded<ACTIVE_OBJECT> |
|---|
| 62 | > |
|---|
| 63 | class MANGOS_DLL_DECL NGrid |
|---|
| 64 | { |
|---|
| 65 | public: |
|---|
| 66 | |
|---|
| 67 | typedef Grid<ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES, ThreadModel> GridType; |
|---|
| 68 | NGrid(uint32 id, int32 x, int32 y, time_t expiry, bool unload = true) : |
|---|
| 69 | i_gridId(id), i_cellstate(GRID_STATE_INVALID), i_x(x), i_y(y), i_GridObjectDataLoaded(false) |
|---|
| 70 | { |
|---|
| 71 | i_GridInfo = GridInfo(expiry, unload); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | const GridType& operator()(unsigned short x, unsigned short y) const { return i_cells[x][y]; } |
|---|
| 75 | GridType& operator()(unsigned short x, unsigned short y) { return i_cells[x][y]; } |
|---|
| 76 | |
|---|
| 77 | inline const uint32& GetGridId(void) const { return i_gridId; } |
|---|
| 78 | inline void SetGridId(const uint32 id) const { i_gridId = id; } |
|---|
| 79 | inline grid_state_t GetGridState(void) const { return i_cellstate; } |
|---|
| 80 | inline void SetGridState(grid_state_t s) { i_cellstate = s; } |
|---|
| 81 | inline int32 getX() const { return i_x; } |
|---|
| 82 | inline int32 getY() const { return i_y; } |
|---|
| 83 | |
|---|
| 84 | void link(GridRefManager<NGrid<N, ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES, ThreadModel> >* pTo) |
|---|
| 85 | { |
|---|
| 86 | i_Reference.link(pTo, this); |
|---|
| 87 | } |
|---|
| 88 | bool isGridObjectDataLoaded() const { return i_GridObjectDataLoaded; } |
|---|
| 89 | void setGridObjectDataLoaded(bool pLoaded) { i_GridObjectDataLoaded = pLoaded; } |
|---|
| 90 | |
|---|
| 91 | GridInfo* getGridInfoRef() { return &i_GridInfo; } |
|---|
| 92 | const TimeTracker& getTimeTracker() const { return i_GridInfo.getTimeTracker(); } |
|---|
| 93 | bool getUnloadFlag() const { return i_GridInfo.getUnloadFlag(); } |
|---|
| 94 | void setUnloadFlag( bool pFlag) { i_GridInfo.setUnloadFlag(pFlag); } |
|---|
| 95 | void ResetTimeTracker(time_t interval) { i_GridInfo.ResetTimeTracker(interval); } |
|---|
| 96 | void UpdateTimeTracker(time_t diff) { i_GridInfo.UpdateTimeTracker(diff); } |
|---|
| 97 | |
|---|
| 98 | template<class SPECIFIC_OBJECT> void AddWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj, OBJECT_HANDLE hdl) |
|---|
| 99 | { |
|---|
| 100 | i_cells[x][y].AddWorldObject(obj, hdl); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | template<class SPECIFIC_OBJECT> void RemoveWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj, OBJECT_HANDLE hdl) |
|---|
| 104 | { |
|---|
| 105 | i_cells[x][y].RemoveWorldObject(obj, hdl); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | template<class T, class TT> void Visit(TypeContainerVisitor<T, TypeMapContainer<TT> > &visitor) |
|---|
| 109 | { |
|---|
| 110 | for(unsigned int x=0; x < N; ++x) |
|---|
| 111 | for(unsigned int y=0; y < N; ++y) |
|---|
| 112 | i_cells[x][y].Visit(visitor); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | template<class T, class TT> void Visit(const uint32 &x, const uint32 &y, TypeContainerVisitor<T, TypeMapContainer<TT> > &visitor) |
|---|
| 116 | { |
|---|
| 117 | i_cells[x][y].Visit(visitor); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | unsigned int ActiveObjectsInGrid(void) const |
|---|
| 121 | { |
|---|
| 122 | unsigned int count=0; |
|---|
| 123 | for(unsigned int x=0; x < N; ++x) |
|---|
| 124 | for(unsigned int y=0; y < N; ++y) |
|---|
| 125 | count += i_cells[x][y].ActiveObjectsInGrid(); |
|---|
| 126 | return count; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | template<class SPECIFIC_OBJECT> const SPECIFIC_OBJECT* GetGridObject(const uint32 x, const uint32 y, OBJECT_HANDLE hdl) const |
|---|
| 130 | { |
|---|
| 131 | return i_cells[x][y].template GetGridObject<SPECIFIC_OBJECT>(hdl); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | template<class SPECIFIC_OBJECT> SPECIFIC_OBJECT* GetGridObject(const uint32 x, const uint32 y, OBJECT_HANDLE hdl) |
|---|
| 135 | { |
|---|
| 136 | return i_cells[x][y].template GetGridObject<SPECIFIC_OBJECT>(hdl); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | template<class SPECIFIC_OBJECT> bool AddGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj, OBJECT_HANDLE hdl) |
|---|
| 140 | { |
|---|
| 141 | return i_cells[x][y].AddGridObject(hdl, obj); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | template<class SPECIFIC_OBJECT> bool RemoveGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj, OBJECT_HANDLE hdl) |
|---|
| 145 | { |
|---|
| 146 | return i_cells[x][y].RemoveGridObject(obj, hdl); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | private: |
|---|
| 150 | |
|---|
| 151 | uint32 i_gridId; |
|---|
| 152 | GridInfo i_GridInfo; |
|---|
| 153 | GridReference<NGrid<N, ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES, ThreadModel> > i_Reference; |
|---|
| 154 | int32 i_x; |
|---|
| 155 | int32 i_y; |
|---|
| 156 | grid_state_t i_cellstate; |
|---|
| 157 | GridType i_cells[N][N]; |
|---|
| 158 | bool i_GridObjectDataLoaded; |
|---|
| 159 | }; |
|---|
| 160 | #endif |
|---|