[2771] Applied MaNGOS coding style (see trunk/bcpp.cfg).
[mangos-git.git] / src / game / Map.h
blob16326da5572b5cdc834fd15cf9a2f951f67b05c8
1 /*
2 * Copyright (C) 2005,2006 MaNGOS <http://www.mangosproject.org/>
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_MAP_H
20 #define MANGOS_MAP_H
22 #include "Platform/Define.h"
23 #include "Policies/ThreadingModel.h"
24 #include "zthread/Lockable.h"
25 #include "zthread/Mutex.h"
26 #include "zthread/FairReadWriteLock.h"
27 #include "GridDefines.h"
28 #include "Cell.h"
29 #include "Object.h"
30 #include "Timer.h"
32 class Unit;
33 class WorldPacket;
35 namespace ZThread
37 class Lockable;
38 class ReadWriteLock;
41 typedef ZThread::FairReadWriteLock GridRWLock;
43 template<class MUTEX, class LOCK_TYPE>
44 struct RGuard
46 RGuard(MUTEX &l) : i_lock(l.getReadLock()) {}
47 MaNGOS::GeneralLock<LOCK_TYPE> i_lock;
50 template<class MUTEX, class LOCK_TYPE>
51 struct WGuard
53 WGuard(MUTEX &l) : i_lock(l.getWriteLock()) {}
54 MaNGOS::GeneralLock<LOCK_TYPE> i_lock;
57 typedef RGuard<GridRWLock, ZThread::Lockable> GridReadGuard;
58 typedef WGuard<GridRWLock, ZThread::Lockable> GridWriteGuard;
59 typedef MaNGOS::SingleThreaded<GridRWLock>::Lock NullGuard;
61 struct GridInfo
63 GridInfo(time_t expiry, bool unload = true ) : i_timer(expiry), i_unloadflag(unload) {}
64 GridRWLock i_lock;
65 TimeTracker i_timer;
66 bool i_unloadflag;
69 typedef struct
71 uint16 area_flag[16][16];
72 uint8 terrain_type[16][16];
73 float liquid_level[128][128];
74 float Z[MAP_RESOLUTION][MAP_RESOLUTION];
75 }GridMap;
77 struct CreatureMover
79 CreatureMover() : x(0), y(0), z(0), ang(0) {}
80 CreatureMover(float _x, float _y, float _z, float _ang) : x(_x), y(_y), z(_z), ang(_ang) {}
82 float x, y, z, ang;
85 typedef HM_NAMESPACE::hash_map<Creature*, CreatureMover> CreatureMoveList;
87 class MANGOS_DLL_DECL Map : public MaNGOS::ObjectLevelLockable<Map, ZThread::Mutex>
89 public:
91 Map(uint32 id, time_t);
93 void Add(Player *);
94 void Remove(Player *, bool);
95 template<class T> void Add(T *);
96 template<class T> void Remove(T *, bool);
97 template<class T> bool Find(T *) const;
99 template<class T> T* GetObjectNear(Object const &obj, OBJECT_HANDLE hdl);
100 template<class T> T* GetObjectNear(float x, float y, OBJECT_HANDLE hdl);
102 void Update(const uint32&);
103 uint64 CalculateGridMask(const uint32 &y) const;
105 void MessageBoardcast(Player *, WorldPacket *, bool to_self, bool own_team_only = false);
107 void MessageBoardcast(Object *, WorldPacket *);
109 void PlayerRelocation(Player *, float x, float y, float z, float angl, bool visibilityChanges = false);
111 void CreatureRelocation(Creature *creature, float x, float y, float, float);
113 template<class LOCK_TYPE, class T, class CONTAINER> void Visit(const CellLock<LOCK_TYPE> &cell, TypeContainerVisitor<T, CONTAINER> &visitor);
115 void SetTimer(uint32 t)
117 i_gridExpiry = t < MIN_GRID_DELAY ? MIN_GRID_DELAY : t;
120 inline bool IsActiveGrid(Object *obj) const
122 return IsActiveGrid(obj->GetPositionX(),obj->GetPositionY());
125 inline bool IsActiveGrid(float x, float y) const
127 GridPair p = MaNGOS::ComputeGridPair(x, y);
128 return( i_grids[p.x_coord][p.y_coord]->GetGridState() == GRID_STATE_ACTIVE );
131 inline bool IsRemovalGrid(float x, float y) const
133 GridPair p = MaNGOS::ComputeGridPair(x, y);
134 return( !i_grids[p.x_coord][p.y_coord] || i_grids[p.x_coord][p.y_coord]->GetGridState() == GRID_STATE_REMOVAL );
137 bool GetUnloadFlag(const GridPair &p) const { return i_info[p.x_coord][p.y_coord]->i_unloadflag; }
138 void SetUnloadFlag(const GridPair &p, bool unload) { i_info[p.x_coord][p.y_coord]->i_unloadflag = unload; }
139 void LoadGrid(const Cell& cell, bool no_unload = false);
140 bool UnloadGrid(const uint32 &x, const uint32 &y);
142 void GetUnitList(float x, float y, std::list<Unit*> &unlist);
144 void ResetGridExpiry(GridInfo &info) const
146 info.i_timer.Reset(i_gridExpiry);
149 time_t GetGridExpiry(void) const { return i_gridExpiry; }
150 uint32 GetId(void) const { return i_id; }
152 static bool ExistMAP(int mapid, int x, int y);
153 GridMap * LoadMAP(int mapid, int x, int y);
155 static void InitStateMachine();
156 static void DeleteStateMachine();
158 float GetHeight(float x, float y);
159 uint16 GetAreaFlag(float x, float y );
160 uint8 GetTerrainType(float x, float y );
161 float GetWaterLevel(float x, float y );
162 float IsInWater(float x, float y);
163 float IsUnderWater(float x, float y, float z);
165 uint32 GetAreaId(uint16 areaflag);
166 uint32 GetZoneId(uint16 areaflag);
168 uint32 GetAreaId(float x, float y)
170 return GetAreaId(GetAreaFlag(x,y));
173 uint32 GetZoneId(float x, float y)
175 return GetZoneId(GetAreaFlag(x,y));
178 void MoveAllCreaturesInMoveList();
180 bool CreatureRespawnRelocation(Creature *c); // used only in MoveAllCreaturesInMoveList and ObjectGridUnloader
182 // assert print helper
183 bool CheckGridIntegrity(Creature* c, bool moved) const;
184 private:
185 bool CreatureCellRelocation(Creature *creature, Cell new_cell);
186 void CreatureRelocationNotifying(Creature *creature, Cell newcell, CellPair newval);
188 void AddCreatureToMoveList(Creature *c, float x, float y, float z, float ang);
189 CreatureMoveList i_creaturesToMove;
191 bool loaded(const GridPair &) const;
192 void EnsureGridLoadedForPlayer(const Cell&, Player*, bool add_player);
193 void NotifyPlayerVisibility(const Cell &, const CellPair &, Player *);
194 uint64 EnsureGridCreated(const GridPair &);
196 template<class T> void AddType(T *obj);
197 template<class T> void RemoveType(T *obj, bool);
199 uint32 i_id;
201 GridMap *GridMaps[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
202 volatile uint64 i_gridMask[MAX_NUMBER_OF_GRIDS];
203 volatile uint64 i_gridStatus[MAX_NUMBER_OF_GRIDS];
205 typedef MaNGOS::ObjectLevelLockable<Map, ZThread::Mutex>::Lock Guard;
206 typedef GridReadGuard ReadGuard;
207 typedef GridWriteGuard WriteGuard;
209 NGridType* i_grids[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
210 GridInfo *i_info[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
212 time_t i_gridExpiry;
215 inline
216 uint64
217 Map::CalculateGridMask(const uint32 &y) const
219 uint64 mask = 1;
220 mask <<= y;
221 return mask;
224 template<class LOCK_TYPE, class T, class CONTAINER>
225 inline void
226 Map::Visit(const CellLock<LOCK_TYPE> &cell, TypeContainerVisitor<T, CONTAINER> &visitor)
228 const uint32 x = cell->GridX();
229 const uint32 y = cell->GridY();
230 const uint32 cell_x = cell->CellX();
231 const uint32 cell_y = cell->CellY();
233 if( !cell->NoCreate() || loaded(GridPair(x,y)) )
235 EnsureGridLoadedForPlayer(cell, NULL, false);
236 LOCK_TYPE guard(i_info[x][y]->i_lock);
237 i_grids[x][y]->Visit(cell_x, cell_y, visitor);
240 #endif