root/trunk/src/framework/GameSystem/NGrid.h @ 102

Revision 102, 6.1 kB (checked in by yumileroy, 17 years ago)

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05:00

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