Fixed possible crash
[getmangos.git] / src / game / Map.h
blob70137a614f9d53fe57a2e411e9c22c35637f4d09
1 /*
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_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 "Database/DBCStructure.h"
28 #include "GridDefines.h"
29 #include "Cell.h"
30 #include "Object.h"
31 #include "Timer.h"
32 #include "SharedDefines.h"
33 #include "GameSystem/GridRefManager.h"
34 #include "MapRefManager.h"
36 #include <bitset>
37 #include <list>
39 class Unit;
40 class WorldPacket;
41 class InstanceData;
42 class Group;
43 class InstanceSave;
45 namespace ZThread
47 class Lockable;
48 class ReadWriteLock;
51 typedef ZThread::FairReadWriteLock GridRWLock;
53 template<class MUTEX, class LOCK_TYPE>
54 struct RGuard
56 RGuard(MUTEX &l) : i_lock(l.getReadLock()) {}
57 MaNGOS::GeneralLock<LOCK_TYPE> i_lock;
60 template<class MUTEX, class LOCK_TYPE>
61 struct WGuard
63 WGuard(MUTEX &l) : i_lock(l.getWriteLock()) {}
64 MaNGOS::GeneralLock<LOCK_TYPE> i_lock;
67 typedef RGuard<GridRWLock, ZThread::Lockable> GridReadGuard;
68 typedef WGuard<GridRWLock, ZThread::Lockable> GridWriteGuard;
69 typedef MaNGOS::SingleThreaded<GridRWLock>::Lock NullGuard;
71 typedef struct
73 uint16 area_flag[16][16];
74 uint8 terrain_type[16][16];
75 float liquid_level[128][128];
76 float Z[MAP_RESOLUTION][MAP_RESOLUTION];
77 }GridMap;
79 struct CreatureMover
81 CreatureMover() : x(0), y(0), z(0), ang(0) {}
82 CreatureMover(float _x, float _y, float _z, float _ang) : x(_x), y(_y), z(_z), ang(_ang) {}
84 float x, y, z, ang;
87 // GCC have alternative #pragma pack(N) syntax and old gcc version not support pack(push,N), also any gcc version not support it at some platform
88 #if defined( __GNUC__ )
89 #pragma pack(1)
90 #else
91 #pragma pack(push,1)
92 #endif
94 struct InstanceTemplate
96 uint32 map;
97 uint32 parent;
98 uint32 levelMin;
99 uint32 levelMax;
100 uint32 maxPlayers;
101 uint32 reset_delay;
102 float startLocX;
103 float startLocY;
104 float startLocZ;
105 float startLocO;
106 uint32 script_id;
109 enum LevelRequirementVsMode
111 LEVELREQUIREMENT_HEROIC = 70
114 #if defined( __GNUC__ )
115 #pragma pack()
116 #else
117 #pragma pack(pop)
118 #endif
120 typedef UNORDERED_MAP<Creature*, CreatureMover> CreatureMoveList;
122 #define MAX_HEIGHT 100000.0f // can be use for find ground height at surface
123 #define INVALID_HEIGHT -100000.0f // for check, must be equal to VMAP_INVALID_HEIGHT, real value for unknown height is VMAP_INVALID_HEIGHT_VALUE
124 #define MIN_UNLOAD_DELAY 1 // immediate unload
126 class MANGOS_DLL_SPEC Map : public GridRefManager<NGridType>, public MaNGOS::ObjectLevelLockable<Map, ZThread::Mutex>
128 friend class MapReference;
129 public:
130 Map(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode);
131 virtual ~Map();
133 // currently unused for normal maps
134 virtual bool CanUnload(const uint32& diff);
136 virtual bool Add(Player *);
137 virtual void Remove(Player *, bool);
138 template<class T> void Add(T *);
139 template<class T> void Remove(T *, bool);
141 virtual void Update(const uint32&);
143 void MessageBroadcast(Player *, WorldPacket *, bool to_self);
144 void MessageBroadcast(WorldObject *, WorldPacket *);
145 void MessageDistBroadcast(Player *, WorldPacket *, float dist, bool to_self, bool own_team_only = false);
146 void MessageDistBroadcast(WorldObject *, WorldPacket *, float dist);
148 void PlayerRelocation(Player *, float x, float y, float z, float angl);
149 void CreatureRelocation(Creature *creature, float x, float y, float, float);
151 template<class LOCK_TYPE, class T, class CONTAINER> void Visit(const CellLock<LOCK_TYPE> &cell, TypeContainerVisitor<T, CONTAINER> &visitor);
153 inline bool IsRemovalGrid(float x, float y) const
155 GridPair p = MaNGOS::ComputeGridPair(x, y);
156 return( !getNGrid(p.x_coord, p.y_coord) || getNGrid(p.x_coord, p.y_coord)->GetGridState() == GRID_STATE_REMOVAL );
159 bool GetUnloadFlag(const GridPair &p) const { return getNGrid(p.x_coord, p.y_coord)->getUnloadFlag(); }
160 void SetUnloadFlag(const GridPair &p, bool unload) { getNGrid(p.x_coord, p.y_coord)->setUnloadFlag(unload); }
161 void LoadGrid(const Cell& cell, bool no_unload = false);
162 bool UnloadGrid(const uint32 &x, const uint32 &y, bool pForce);
163 virtual void UnloadAll(bool pForce);
165 void ResetGridExpiry(NGridType &grid, float factor = 1) const
167 grid.ResetTimeTracker((time_t)((float)i_gridExpiry*factor));
170 time_t GetGridExpiry(void) const { return i_gridExpiry; }
171 uint32 GetId(void) const { return i_id; }
173 static bool ExistMap(uint32 mapid, int x, int y);
174 static bool ExistVMap(uint32 mapid, int x, int y);
175 void LoadMapAndVMap(uint32 mapid, uint32 instanceid, int x, int y);
177 static void InitStateMachine();
178 static void DeleteStateMachine();
180 // some calls like isInWater should not use vmaps due to processor power
181 // can return INVALID_HEIGHT if under z+2 z coord not found height
182 float GetHeight(float x, float y, float z, bool pCheckVMap=true) const;
183 bool IsInWater(float x, float y, float z) const; // does not use z pos. This is for future use
185 uint16 GetAreaFlag(float x, float y ) const;
186 uint8 GetTerrainType(float x, float y ) const;
187 float GetWaterLevel(float x, float y ) const;
188 bool IsUnderWater(float x, float y, float z) const;
190 static uint32 GetAreaId(uint16 areaflag,uint32 map_id);
191 static uint32 GetZoneId(uint16 areaflag,uint32 map_id);
193 uint32 GetAreaId(float x, float y) const
195 return GetAreaId(GetAreaFlag(x,y),i_id);
198 uint32 GetZoneId(float x, float y) const
200 return GetZoneId(GetAreaFlag(x,y),i_id);
203 virtual void MoveAllCreaturesInMoveList();
204 virtual void RemoveAllObjectsInRemoveList();
206 bool CreatureRespawnRelocation(Creature *c); // used only in MoveAllCreaturesInMoveList and ObjectGridUnloader
208 // assert print helper
209 bool CheckGridIntegrity(Creature* c, bool moved) const;
211 uint32 GetInstanceId() { return i_InstanceId; }
212 uint8 GetSpawnMode() { return (i_spawnMode); }
213 virtual bool CanEnter(Player* /*player*/) { return true; }
214 const char* GetMapName() const;
216 bool Instanceable() const { return i_mapEntry && i_mapEntry->Instanceable(); }
217 // NOTE: this duplicate of Instanceable(), but Instanceable() can be changed when BG also will be instanceable
218 bool IsDungeon() const { return i_mapEntry && i_mapEntry->IsDungeon(); }
219 bool IsRaid() const { return i_mapEntry && i_mapEntry->IsRaid(); }
220 bool IsHeroic() const { return i_spawnMode == DIFFICULTY_HEROIC; }
221 bool IsBattleGround() const { return i_mapEntry && i_mapEntry->IsBattleGround(); }
222 bool IsBattleArena() const { return i_mapEntry && i_mapEntry->IsBattleArena(); }
223 bool IsBattleGroundOrArena() const { return i_mapEntry && i_mapEntry->IsBattleGroundOrArena(); }
224 bool GetEntrancePos(int32 &mapid, float &x, float &y)
226 if(!i_mapEntry)
227 return false;
228 if(i_mapEntry->entrance_map < 0)
229 return false;
230 mapid = i_mapEntry->entrance_map;
231 x = i_mapEntry->entrance_x;
232 y = i_mapEntry->entrance_y;
233 return true;
236 void AddObjectToRemoveList(WorldObject *obj);
237 void DoDelayedMovesAndRemoves();
239 virtual bool RemoveBones(uint64 guid, float x, float y);
241 void UpdateObjectVisibility(WorldObject* obj, Cell cell, CellPair cellpair);
242 void UpdatePlayerVisibility(Player* player, Cell cell, CellPair cellpair);
243 void UpdateObjectsVisibilityFor(Player* player, Cell cell, CellPair cellpair);
245 void resetMarkedCells() { marked_cells.reset(); }
246 bool isCellMarked(uint32 pCellId) { return marked_cells.test(pCellId); }
247 void markCell(uint32 pCellId) { marked_cells.set(pCellId); }
249 bool HavePlayers() const { return !m_mapRefManager.isEmpty(); }
250 uint32 GetPlayersCountExceptGMs() const;
251 bool PlayersNearGrid(uint32 x,uint32 y) const;
253 void SendToPlayers(WorldPacket const* data) const;
255 typedef MapRefManager PlayerList;
256 PlayerList const& GetPlayers() const { return m_mapRefManager; }
257 private:
258 void LoadVMap(int pX, int pY);
259 void LoadMap(uint32 mapid, uint32 instanceid, int x,int y);
261 void SetTimer(uint32 t) { i_gridExpiry = t < MIN_GRID_DELAY ? MIN_GRID_DELAY : t; }
262 //uint64 CalculateGridMask(const uint32 &y) const;
264 void SendInitSelf( Player * player );
266 void SendInitTransports( Player * player );
267 void SendRemoveTransports( Player * player );
269 void PlayerRelocationNotify(Player* player, Cell cell, CellPair cellpair);
270 void CreatureRelocationNotify(Creature *creature, Cell newcell, CellPair newval);
272 bool CreatureCellRelocation(Creature *creature, Cell new_cell);
274 void AddCreatureToMoveList(Creature *c, float x, float y, float z, float ang);
275 CreatureMoveList i_creaturesToMove;
277 bool loaded(const GridPair &) const;
278 void EnsureGridLoadedForPlayer(const Cell&, Player*, bool add_player);
279 void EnsureGridCreated(const GridPair &);
281 void buildNGridLinkage(NGridType* pNGridType) { pNGridType->link(this); }
283 template<class T> void AddType(T *obj);
284 template<class T> void RemoveType(T *obj, bool);
286 NGridType* getNGrid(uint32 x, uint32 y) const
288 return i_grids[x][y];
291 bool isGridObjectDataLoaded(uint32 x, uint32 y) const { return getNGrid(x,y)->isGridObjectDataLoaded(); }
292 void setGridObjectDataLoaded(bool pLoaded, uint32 x, uint32 y) { getNGrid(x,y)->setGridObjectDataLoaded(pLoaded); }
294 inline void setNGrid(NGridType* grid, uint32 x, uint32 y);
296 protected:
297 typedef MaNGOS::ObjectLevelLockable<Map, ZThread::Mutex>::Lock Guard;
299 MapEntry const* i_mapEntry;
300 uint8 i_spawnMode;
301 uint32 i_id;
302 uint32 i_InstanceId;
303 uint32 m_unloadTimer;
305 MapRefManager m_mapRefManager;
306 MapRefManager::iterator m_mapRefIter;
307 private:
308 typedef GridReadGuard ReadGuard;
309 typedef GridWriteGuard WriteGuard;
311 NGridType* i_grids[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
312 GridMap *GridMaps[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
313 std::bitset<TOTAL_NUMBER_OF_CELLS_PER_MAP*TOTAL_NUMBER_OF_CELLS_PER_MAP> marked_cells;
315 time_t i_gridExpiry;
317 std::set<WorldObject *> i_objectsToRemove;
319 // Type specific code for add/remove to/from grid
320 template<class T>
321 void AddToGrid(T*, NGridType *, Cell const&);
323 template<class T>
324 void AddNotifier(T*, Cell const&, CellPair const&);
326 template<class T>
327 void RemoveFromGrid(T*, NGridType *, Cell const&);
329 template<class T>
330 void DeleteFromWorld(T*);
333 enum InstanceResetMethod
335 INSTANCE_RESET_ALL,
336 INSTANCE_RESET_CHANGE_DIFFICULTY,
337 INSTANCE_RESET_GLOBAL,
338 INSTANCE_RESET_GROUP_DISBAND,
339 INSTANCE_RESET_GROUP_JOIN,
340 INSTANCE_RESET_RESPAWN_DELAY
343 class MANGOS_DLL_SPEC InstanceMap : public Map
345 public:
346 InstanceMap(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode);
347 ~InstanceMap();
348 bool Add(Player *);
349 void Remove(Player *, bool);
350 void Update(const uint32&);
351 void CreateInstanceData(bool load);
352 bool Reset(uint8 method);
353 uint32 GetScriptId() { return i_script_id; }
354 InstanceData* GetInstanceData() { return i_data; }
355 void PermBindAllPlayers(Player *player);
356 time_t GetResetTime();
357 void UnloadAll(bool pForce);
358 bool CanEnter(Player* player);
359 void SendResetWarnings(uint32 timeLeft) const;
360 void SetResetSchedule(bool on);
361 private:
362 bool m_resetAfterUnload;
363 bool m_unloadWhenEmpty;
364 InstanceData* i_data;
365 uint32 i_script_id;
368 class MANGOS_DLL_SPEC BattleGroundMap : public Map
370 public:
371 BattleGroundMap(uint32 id, time_t, uint32 InstanceId);
372 ~BattleGroundMap();
374 bool Add(Player *);
375 void Remove(Player *, bool);
376 bool CanEnter(Player* player);
377 void SetUnload();
378 void UnloadAll(bool pForce);
381 /*inline
382 uint64
383 Map::CalculateGridMask(const uint32 &y) const
385 uint64 mask = 1;
386 mask <<= y;
387 return mask;
391 template<class LOCK_TYPE, class T, class CONTAINER>
392 inline void
393 Map::Visit(const CellLock<LOCK_TYPE> &cell, TypeContainerVisitor<T, CONTAINER> &visitor)
395 const uint32 x = cell->GridX();
396 const uint32 y = cell->GridY();
397 const uint32 cell_x = cell->CellX();
398 const uint32 cell_y = cell->CellY();
400 if( !cell->NoCreate() || loaded(GridPair(x,y)) )
402 EnsureGridLoadedForPlayer(cell, NULL, false);
403 //LOCK_TYPE guard(i_info[x][y]->i_lock);
404 getNGrid(x, y)->Visit(cell_x, cell_y, visitor);
407 #endif