2 * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
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.
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.
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
19 #ifndef MANGOS_NGRID_H
20 #define MANGOS_NGRID_H
22 /** NGrid is nothing more than a wrapper of the Grid with an NxN cells
25 #include "GameSystem/Grid.h"
26 #include "GameSystem/GridReference.h"
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
); }
48 GRID_STATE_INVALID
= 0,
49 GRID_STATE_ACTIVE
= 1,
51 GRID_STATE_REMOVAL
= 3,
59 class WORLD_OBJECT_TYPES
,
60 class GRID_OBJECT_TYPES
,
61 class ThreadModel
= MaNGOS::SingleThreaded
<ACTIVE_OBJECT
>
63 class MANGOS_DLL_DECL NGrid
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)
71 i_GridInfo
= GridInfo(expiry
, unload
);
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
]; }
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
; }
84 void link(GridRefManager
<NGrid
<N
, ACTIVE_OBJECT
, WORLD_OBJECT_TYPES
, GRID_OBJECT_TYPES
, ThreadModel
> >* pTo
)
86 i_Reference
.link(pTo
, this);
88 bool isGridObjectDataLoaded() const { return i_GridObjectDataLoaded
; }
89 void setGridObjectDataLoaded(bool pLoaded
) { i_GridObjectDataLoaded
= pLoaded
; }
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
); }
98 template<class SPECIFIC_OBJECT
> void AddWorldObject(const uint32 x
, const uint32 y
, SPECIFIC_OBJECT
*obj
, OBJECT_HANDLE hdl
)
100 i_cells
[x
][y
].AddWorldObject(obj
, hdl
);
103 template<class SPECIFIC_OBJECT
> void RemoveWorldObject(const uint32 x
, const uint32 y
, SPECIFIC_OBJECT
*obj
, OBJECT_HANDLE hdl
)
105 i_cells
[x
][y
].RemoveWorldObject(obj
, hdl
);
108 template<class T
, class TT
> void Visit(TypeContainerVisitor
<T
, TypeMapContainer
<TT
> > &visitor
)
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
);
115 template<class T
, class TT
> void Visit(const uint32
&x
, const uint32
&y
, TypeContainerVisitor
<T
, TypeMapContainer
<TT
> > &visitor
)
117 i_cells
[x
][y
].Visit(visitor
);
120 unsigned int ActiveObjectsInGrid(void) const
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();
129 template<class SPECIFIC_OBJECT
> const SPECIFIC_OBJECT
* GetGridObject(const uint32 x
, const uint32 y
, OBJECT_HANDLE hdl
) const
131 return i_cells
[x
][y
].template GetGridObject
<SPECIFIC_OBJECT
>(hdl
);
134 template<class SPECIFIC_OBJECT
> SPECIFIC_OBJECT
* GetGridObject(const uint32 x
, const uint32 y
, OBJECT_HANDLE hdl
)
136 return i_cells
[x
][y
].template GetGridObject
<SPECIFIC_OBJECT
>(hdl
);
139 template<class SPECIFIC_OBJECT
> bool AddGridObject(const uint32 x
, const uint32 y
, SPECIFIC_OBJECT
*obj
, OBJECT_HANDLE hdl
)
141 return i_cells
[x
][y
].AddGridObject(hdl
, obj
);
144 template<class SPECIFIC_OBJECT
> bool RemoveGridObject(const uint32 x
, const uint32 y
, SPECIFIC_OBJECT
*obj
, OBJECT_HANDLE hdl
)
146 return i_cells
[x
][y
].RemoveGridObject(obj
, hdl
);
153 GridReference
<NGrid
<N
, ACTIVE_OBJECT
, WORLD_OBJECT_TYPES
, GRID_OBJECT_TYPES
, ThreadModel
> > i_Reference
;
156 grid_state_t i_cellstate
;
157 GridType i_cells
[N
][N
];
158 bool i_GridObjectDataLoaded
;