[7924] Not teleport player to non-player caster at ressurection.
[getmangos.git] / src / game / Map.h
bloba07553181aad3bbb398aea4b3891d96548293540
1 /*
2 * Copyright (C) 2005-2009 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 "ace/RW_Thread_Mutex.h"
25 #include "ace/Thread_Mutex.h"
27 #include "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;
46 typedef ACE_RW_Thread_Mutex GridRWLock;
48 template<class MUTEX, class LOCK_TYPE>
49 struct RGuard
51 RGuard(MUTEX &l) : i_lock(l.getReadLock()) {}
52 MaNGOS::GeneralLock<LOCK_TYPE> i_lock;
55 template<class MUTEX, class LOCK_TYPE>
56 struct WGuard
58 WGuard(MUTEX &l) : i_lock(l.getWriteLock()) {}
59 MaNGOS::GeneralLock<LOCK_TYPE> i_lock;
62 typedef RGuard<GridRWLock, ACE_Thread_Mutex> GridReadGuard;
63 typedef WGuard<GridRWLock, ACE_Thread_Mutex> GridWriteGuard;
64 typedef MaNGOS::SingleThreaded<GridRWLock>::Lock NullGuard;
66 //******************************************
67 // Map file format defines
68 //******************************************
69 #define MAP_MAGIC 'SPAM'
70 #define MAP_VERSION_MAGIC '0.1w'
71 #define MAP_AREA_MAGIC 'AERA'
72 #define MAP_HEIGTH_MAGIC 'TGHM'
73 #define MAP_LIQUID_MAGIC 'QILM'
75 struct map_fileheader{
76 uint32 mapMagic;
77 uint32 versionMagic;
78 uint32 areaMapOffset;
79 uint32 areaMapSize;
80 uint32 heightMapOffset;
81 uint32 heightMapSize;
82 uint32 liquidMapOffset;
83 uint32 liquidMapSize;
86 #define MAP_AREA_NO_AREA 0x0001
87 struct map_areaHeader{
88 uint32 fourcc;
89 uint16 flags;
90 uint16 gridArea;
93 #define MAP_HEIGHT_NO_HIGHT 0x0001
94 #define MAP_HEIGHT_AS_INT16 0x0002
95 #define MAP_HEIGHT_AS_INT8 0x0004
97 struct map_heightHeader{
98 uint32 fourcc;
99 uint32 flags;
100 float gridHeight;
101 float gridMaxHeight;
104 #define MAP_LIQUID_NO_TYPE 0x0001
105 #define MAP_LIQUID_NO_HIGHT 0x0002
106 struct map_liquidHeader{
107 uint32 fourcc;
108 uint16 flags;
109 uint16 liquidType;
110 uint8 offsetX;
111 uint8 offsetY;
112 uint8 width;
113 uint8 height;
114 float liquidLevel;
117 enum ZLiquidStatus{
118 LIQUID_MAP_NO_WATER = 0x00000000,
119 LIQUID_MAP_ABOVE_WATER = 0x00000001,
120 LIQUID_MAP_WATER_WALK = 0x00000002,
121 LIQUID_MAP_IN_WATER = 0x00000004,
122 LIQUID_MAP_UNDER_WATER = 0x00000008
125 #define MAP_LIQUID_TYPE_NO_WATER 0x00
126 #define MAP_LIQUID_TYPE_WATER 0x01
127 #define MAP_LIQUID_TYPE_OCEAN 0x02
128 #define MAP_LIQUID_TYPE_MAGMA 0x04
129 #define MAP_LIQUID_TYPE_SLIME 0x08
131 #define MAP_ALL_LIQUIDS (MAP_LIQUID_TYPE_WATER | MAP_LIQUID_TYPE_OCEAN | MAP_LIQUID_TYPE_MAGMA | MAP_LIQUID_TYPE_SLIME)
133 #define MAP_LIQUID_TYPE_DARK_WATER 0x10
134 #define MAP_LIQUID_TYPE_WMO_WATER 0x20
136 struct LiquidData{
137 uint32 type;
138 float level;
139 float depth_level;
142 class GridMap
144 uint32 m_flags;
145 // Area data
146 uint16 m_gridArea;
147 uint16 *m_area_map;
148 // Height level data
149 float m_gridHeight;
150 float m_gridIntHeightMultiplier;
151 union{
152 float *m_V9;
153 uint16 *m_uint16_V9;
154 uint8 *m_uint8_V9;
156 union{
157 float *m_V8;
158 uint16 *m_uint16_V8;
159 uint8 *m_uint8_V8;
161 // Liquid data
162 uint16 m_liquidType;
163 uint8 m_liquid_offX;
164 uint8 m_liquid_offY;
165 uint8 m_liquid_width;
166 uint8 m_liquid_height;
167 float m_liquidLevel;
168 uint8 *m_liquid_type;
169 float *m_liquid_map;
171 bool loadAreaData(FILE *in, uint32 offset, uint32 size);
172 bool loadHeihgtData(FILE *in, uint32 offset, uint32 size);
173 bool loadLiquidData(FILE *in, uint32 offset, uint32 size);
175 // Get height functions and pointers
176 typedef float (GridMap::*pGetHeightPtr) (float x, float y) const;
177 pGetHeightPtr m_gridGetHeight;
178 float getHeightFromFloat(float x, float y) const;
179 float getHeightFromUint16(float x, float y) const;
180 float getHeightFromUint8(float x, float y) const;
181 float getHeightFromFlat(float x, float y) const;
183 public:
184 GridMap();
185 ~GridMap();
186 bool loadData(char *filaname);
187 void unloadData();
189 uint16 getArea(float x, float y);
190 inline float getHeight(float x, float y) {return (this->*m_gridGetHeight)(x, y);}
191 float getLiquidLevel(float x, float y);
192 uint8 getTerrainType(float x, float y);
193 ZLiquidStatus getLiquidStatus(float x, float y, float z, uint8 ReqLiquidType, LiquidData *data = 0);
196 struct CreatureMover
198 CreatureMover() : x(0), y(0), z(0), ang(0) {}
199 CreatureMover(float _x, float _y, float _z, float _ang) : x(_x), y(_y), z(_z), ang(_ang) {}
201 float x, y, z, ang;
204 // 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
205 #if defined( __GNUC__ )
206 #pragma pack(1)
207 #else
208 #pragma pack(push,1)
209 #endif
211 struct InstanceTemplate
213 uint32 map;
214 uint32 parent;
215 uint32 levelMin;
216 uint32 levelMax;
217 uint32 maxPlayers;
218 uint32 maxPlayersHeroic;
219 uint32 reset_delay; // FIX ME: now exist normal/heroic raids with possible different time of reset.
220 float startLocX;
221 float startLocY;
222 float startLocZ;
223 float startLocO;
224 uint32 script_id;
227 enum LevelRequirementVsMode
229 LEVELREQUIREMENT_HEROIC = 70
232 #if defined( __GNUC__ )
233 #pragma pack()
234 #else
235 #pragma pack(pop)
236 #endif
238 typedef UNORDERED_MAP<Creature*, CreatureMover> CreatureMoveList;
240 #define MAX_HEIGHT 100000.0f // can be use for find ground height at surface
241 #define INVALID_HEIGHT -100000.0f // for check, must be equal to VMAP_INVALID_HEIGHT, real value for unknown height is VMAP_INVALID_HEIGHT_VALUE
242 #define MIN_UNLOAD_DELAY 1 // immediate unload
244 class MANGOS_DLL_SPEC Map : public GridRefManager<NGridType>, public MaNGOS::ObjectLevelLockable<Map, ACE_Thread_Mutex>
246 friend class MapReference;
247 public:
248 Map(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode);
249 virtual ~Map();
251 // currently unused for normal maps
252 bool CanUnload(uint32 diff)
254 if(!m_unloadTimer) return false;
255 if(m_unloadTimer <= diff) return true;
256 m_unloadTimer -= diff;
257 return false;
260 virtual bool Add(Player *);
261 virtual void Remove(Player *, bool);
262 template<class T> void Add(T *);
263 template<class T> void Remove(T *, bool);
265 virtual void Update(const uint32&);
267 void MessageBroadcast(Player *, WorldPacket *, bool to_self);
268 void MessageBroadcast(WorldObject *, WorldPacket *);
269 void MessageDistBroadcast(Player *, WorldPacket *, float dist, bool to_self, bool own_team_only = false);
270 void MessageDistBroadcast(WorldObject *, WorldPacket *, float dist);
272 void PlayerRelocation(Player *, float x, float y, float z, float angl);
273 void CreatureRelocation(Creature *creature, float x, float y, float, float);
275 template<class LOCK_TYPE, class T, class CONTAINER> void Visit(const CellLock<LOCK_TYPE> &cell, TypeContainerVisitor<T, CONTAINER> &visitor);
277 bool IsRemovalGrid(float x, float y) const
279 GridPair p = MaNGOS::ComputeGridPair(x, y);
280 return( !getNGrid(p.x_coord, p.y_coord) || getNGrid(p.x_coord, p.y_coord)->GetGridState() == GRID_STATE_REMOVAL );
283 bool GetUnloadLock(const GridPair &p) const { return getNGrid(p.x_coord, p.y_coord)->getUnloadLock(); }
284 void SetUnloadLock(const GridPair &p, bool on) { getNGrid(p.x_coord, p.y_coord)->setUnloadExplicitLock(on); }
285 void LoadGrid(const Cell& cell, bool no_unload = false);
286 bool UnloadGrid(const uint32 &x, const uint32 &y, bool pForce);
287 virtual void UnloadAll(bool pForce);
289 void ResetGridExpiry(NGridType &grid, float factor = 1) const
291 grid.ResetTimeTracker((time_t)((float)i_gridExpiry*factor));
294 time_t GetGridExpiry(void) const { return i_gridExpiry; }
295 uint32 GetId(void) const { return i_id; }
297 static bool ExistMap(uint32 mapid, int gx, int gy);
298 static bool ExistVMap(uint32 mapid, int gx, int gy);
300 static void InitStateMachine();
301 static void DeleteStateMachine();
303 // some calls like isInWater should not use vmaps due to processor power
304 // can return INVALID_HEIGHT if under z+2 z coord not found height
305 float GetHeight(float x, float y, float z, bool pCheckVMap=true) const;
306 bool IsInWater(float x, float y, float z) const; // does not use z pos. This is for future use
308 ZLiquidStatus getLiquidStatus(float x, float y, float z, uint8 ReqLiquidType, LiquidData *data = 0) const;
310 uint16 GetAreaFlag(float x, float y, float z) const;
311 uint8 GetTerrainType(float x, float y ) const;
312 float GetWaterLevel(float x, float y ) const;
313 bool IsUnderWater(float x, float y, float z) const;
315 static uint32 GetAreaIdByAreaFlag(uint16 areaflag,uint32 map_id);
316 static uint32 GetZoneIdByAreaFlag(uint16 areaflag,uint32 map_id);
317 static void GetZoneAndAreaIdByAreaFlag(uint32& zoneid, uint32& areaid, uint16 areaflag,uint32 map_id);
319 uint32 GetAreaId(float x, float y, float z) const
321 return GetAreaIdByAreaFlag(GetAreaFlag(x,y,z),i_id);
324 uint32 GetZoneId(float x, float y, float z) const
326 return GetZoneIdByAreaFlag(GetAreaFlag(x,y,z),i_id);
329 void GetZoneAndAreaId(uint32& zoneid, uint32& areaid, float x, float y, float z) const
331 GetZoneAndAreaIdByAreaFlag(zoneid,areaid,GetAreaFlag(x,y,z),i_id);
334 virtual void MoveAllCreaturesInMoveList();
335 virtual void RemoveAllObjectsInRemoveList();
337 bool CreatureRespawnRelocation(Creature *c); // used only in MoveAllCreaturesInMoveList and ObjectGridUnloader
339 // assert print helper
340 bool CheckGridIntegrity(Creature* c, bool moved) const;
342 uint32 GetInstanceId() const { return i_InstanceId; }
343 uint8 GetSpawnMode() const { return (i_spawnMode); }
344 virtual bool CanEnter(Player* /*player*/) { return true; }
345 const char* GetMapName() const;
347 bool Instanceable() const { return i_mapEntry && i_mapEntry->Instanceable(); }
348 // NOTE: this duplicate of Instanceable(), but Instanceable() can be changed when BG also will be instanceable
349 bool IsDungeon() const { return i_mapEntry && i_mapEntry->IsDungeon(); }
350 bool IsRaid() const { return i_mapEntry && i_mapEntry->IsRaid(); }
351 bool IsHeroic() const { return i_spawnMode == DIFFICULTY_HEROIC; }
352 bool IsBattleGround() const { return i_mapEntry && i_mapEntry->IsBattleGround(); }
353 bool IsBattleArena() const { return i_mapEntry && i_mapEntry->IsBattleArena(); }
354 bool IsBattleGroundOrArena() const { return i_mapEntry && i_mapEntry->IsBattleGroundOrArena(); }
355 bool GetEntrancePos(int32 &mapid, float &x, float &y)
357 if(!i_mapEntry)
358 return false;
359 if(i_mapEntry->entrance_map < 0)
360 return false;
361 mapid = i_mapEntry->entrance_map;
362 x = i_mapEntry->entrance_x;
363 y = i_mapEntry->entrance_y;
364 return true;
367 void AddObjectToRemoveList(WorldObject *obj);
368 void DoDelayedMovesAndRemoves();
370 virtual bool RemoveBones(uint64 guid, float x, float y);
372 void UpdateObjectVisibility(WorldObject* obj, Cell cell, CellPair cellpair);
373 void UpdatePlayerVisibility(Player* player, Cell cell, CellPair cellpair);
374 void UpdateObjectsVisibilityFor(Player* player, Cell cell, CellPair cellpair);
376 void resetMarkedCells() { marked_cells.reset(); }
377 bool isCellMarked(uint32 pCellId) { return marked_cells.test(pCellId); }
378 void markCell(uint32 pCellId) { marked_cells.set(pCellId); }
380 bool HavePlayers() const { return !m_mapRefManager.isEmpty(); }
381 uint32 GetPlayersCountExceptGMs() const;
382 bool ActiveObjectsNearGrid(uint32 x,uint32 y) const;
384 void SendToPlayers(WorldPacket const* data) const;
386 typedef MapRefManager PlayerList;
387 PlayerList const& GetPlayers() const { return m_mapRefManager; }
389 // must called with AddToWorld
390 template<class T>
391 void AddToActive(T* obj) { AddToActiveHelper(obj); }
393 void AddToActive(Creature* obj);
395 // must called with RemoveFromWorld
396 template<class T>
397 void RemoveFromActive(T* obj) { RemoveFromActiveHelper(obj); }
399 void RemoveFromActive(Creature* obj);
401 Creature* GetCreature(uint64 guid);
402 GameObject* GetGameObject(uint64 guid);
403 DynamicObject* GetDynamicObject(uint64 guid);
404 private:
405 void LoadMapAndVMap(int gx, int gy);
406 void LoadVMap(int gx, int gy);
407 void LoadMap(int gx,int gy, bool reload = false);
408 GridMap *GetGrid(float x, float y);
410 void SetTimer(uint32 t) { i_gridExpiry = t < MIN_GRID_DELAY ? MIN_GRID_DELAY : t; }
411 //uint64 CalculateGridMask(const uint32 &y) const;
413 void SendInitSelf( Player * player );
415 void SendInitTransports( Player * player );
416 void SendRemoveTransports( Player * player );
418 void PlayerRelocationNotify(Player* player, Cell cell, CellPair cellpair);
419 void CreatureRelocationNotify(Creature *creature, Cell newcell, CellPair newval);
421 bool CreatureCellRelocation(Creature *creature, Cell new_cell);
423 void AddCreatureToMoveList(Creature *c, float x, float y, float z, float ang);
424 CreatureMoveList i_creaturesToMove;
426 bool loaded(const GridPair &) const;
427 void EnsureGridCreated(const GridPair &);
428 bool EnsureGridLoaded(Cell const&);
429 void EnsureGridLoadedAtEnter(Cell const&, Player* player = NULL);
431 void buildNGridLinkage(NGridType* pNGridType) { pNGridType->link(this); }
433 template<class T> void AddType(T *obj);
434 template<class T> void RemoveType(T *obj, bool);
436 NGridType* getNGrid(uint32 x, uint32 y) const
438 return i_grids[x][y];
441 bool isGridObjectDataLoaded(uint32 x, uint32 y) const { return getNGrid(x,y)->isGridObjectDataLoaded(); }
442 void setGridObjectDataLoaded(bool pLoaded, uint32 x, uint32 y) { getNGrid(x,y)->setGridObjectDataLoaded(pLoaded); }
444 void setNGrid(NGridType* grid, uint32 x, uint32 y);
446 protected:
447 void SetUnloadReferenceLock(const GridPair &p, bool on) { getNGrid(p.x_coord, p.y_coord)->setUnloadReferenceLock(on); }
449 typedef MaNGOS::ObjectLevelLockable<Map, ACE_Thread_Mutex>::Lock Guard;
451 MapEntry const* i_mapEntry;
452 uint8 i_spawnMode;
453 uint32 i_id;
454 uint32 i_InstanceId;
455 uint32 m_unloadTimer;
457 MapRefManager m_mapRefManager;
458 MapRefManager::iterator m_mapRefIter;
460 typedef std::set<WorldObject*> ActiveNonPlayers;
461 ActiveNonPlayers m_activeNonPlayers;
462 ActiveNonPlayers::iterator m_activeNonPlayersIter;
463 private:
464 typedef GridReadGuard ReadGuard;
465 typedef GridWriteGuard WriteGuard;
467 NGridType* i_grids[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
468 GridMap *GridMaps[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
469 std::bitset<TOTAL_NUMBER_OF_CELLS_PER_MAP*TOTAL_NUMBER_OF_CELLS_PER_MAP> marked_cells;
471 time_t i_gridExpiry;
473 std::set<WorldObject *> i_objectsToRemove;
475 // Type specific code for add/remove to/from grid
476 template<class T>
477 void AddToGrid(T*, NGridType *, Cell const&);
479 template<class T>
480 void AddNotifier(T*, Cell const&, CellPair const&);
482 template<class T>
483 void RemoveFromGrid(T*, NGridType *, Cell const&);
485 template<class T>
486 void DeleteFromWorld(T*);
488 template<class T>
489 void AddToActiveHelper(T* obj)
491 m_activeNonPlayers.insert(obj);
494 template<class T>
495 void RemoveFromActiveHelper(T* obj)
497 // Map::Update for active object in proccess
498 if(m_activeNonPlayersIter != m_activeNonPlayers.end())
500 ActiveNonPlayers::iterator itr = m_activeNonPlayers.find(obj);
501 if(itr==m_activeNonPlayersIter)
502 ++m_activeNonPlayersIter;
503 m_activeNonPlayers.erase(itr);
505 else
506 m_activeNonPlayers.erase(obj);
510 enum InstanceResetMethod
512 INSTANCE_RESET_ALL,
513 INSTANCE_RESET_CHANGE_DIFFICULTY,
514 INSTANCE_RESET_GLOBAL,
515 INSTANCE_RESET_GROUP_DISBAND,
516 INSTANCE_RESET_GROUP_JOIN,
517 INSTANCE_RESET_RESPAWN_DELAY
520 class MANGOS_DLL_SPEC InstanceMap : public Map
522 public:
523 InstanceMap(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode);
524 ~InstanceMap();
525 bool Add(Player *);
526 void Remove(Player *, bool);
527 void Update(const uint32&);
528 void CreateInstanceData(bool load);
529 bool Reset(uint8 method);
530 uint32 GetScriptId() { return i_script_id; }
531 InstanceData* GetInstanceData() { return i_data; }
532 void PermBindAllPlayers(Player *player);
533 void UnloadAll(bool pForce);
534 bool CanEnter(Player* player);
535 void SendResetWarnings(uint32 timeLeft) const;
536 void SetResetSchedule(bool on);
537 uint32 GetMaxPlayers() const;
538 private:
539 bool m_resetAfterUnload;
540 bool m_unloadWhenEmpty;
541 InstanceData* i_data;
542 uint32 i_script_id;
545 class MANGOS_DLL_SPEC BattleGroundMap : public Map
547 public:
548 BattleGroundMap(uint32 id, time_t, uint32 InstanceId);
549 ~BattleGroundMap();
551 bool Add(Player *);
552 void Remove(Player *, bool);
553 bool CanEnter(Player* player);
554 void SetUnload();
555 void UnloadAll(bool pForce);
558 /*inline
559 uint64
560 Map::CalculateGridMask(const uint32 &y) const
562 uint64 mask = 1;
563 mask <<= y;
564 return mask;
568 template<class LOCK_TYPE, class T, class CONTAINER>
569 inline void
570 Map::Visit(const CellLock<LOCK_TYPE> &cell, TypeContainerVisitor<T, CONTAINER> &visitor)
572 const uint32 x = cell->GridX();
573 const uint32 y = cell->GridY();
574 const uint32 cell_x = cell->CellX();
575 const uint32 cell_y = cell->CellY();
577 if( !cell->NoCreate() || loaded(GridPair(x,y)) )
579 EnsureGridLoaded(cell);
580 //LOCK_TYPE guard(i_info[x][y]->i_lock);
581 getNGrid(x, y)->Visit(cell_x, cell_y, visitor);
584 #endif