[8449] Deprecate healing/damage item mods and merge internal data in to spell power.
[getmangos.git] / src / game / Map.h
blobc4abec12e9ec74a19e132212c354673d866ae6ca
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;
44 struct ScriptInfo;
45 struct ScriptAction;
48 typedef ACE_RW_Thread_Mutex GridRWLock;
50 template<class MUTEX, class LOCK_TYPE>
51 struct RGuard
53 RGuard(MUTEX &l) : i_lock(l.getReadLock()) {}
54 MaNGOS::GeneralLock<LOCK_TYPE> i_lock;
57 template<class MUTEX, class LOCK_TYPE>
58 struct WGuard
60 WGuard(MUTEX &l) : i_lock(l.getWriteLock()) {}
61 MaNGOS::GeneralLock<LOCK_TYPE> i_lock;
64 typedef RGuard<GridRWLock, ACE_Thread_Mutex> GridReadGuard;
65 typedef WGuard<GridRWLock, ACE_Thread_Mutex> GridWriteGuard;
66 typedef MaNGOS::SingleThreaded<GridRWLock>::Lock NullGuard;
68 //******************************************
69 // Map file format defines
70 //******************************************
71 #define MAP_MAGIC 'SPAM'
72 #define MAP_VERSION_MAGIC '0.1w'
73 #define MAP_AREA_MAGIC 'AERA'
74 #define MAP_HEIGHT_MAGIC 'TGHM'
75 #define MAP_LIQUID_MAGIC 'QILM'
77 struct map_fileheader
79 uint32 mapMagic;
80 uint32 versionMagic;
81 uint32 areaMapOffset;
82 uint32 areaMapSize;
83 uint32 heightMapOffset;
84 uint32 heightMapSize;
85 uint32 liquidMapOffset;
86 uint32 liquidMapSize;
89 #define MAP_AREA_NO_AREA 0x0001
91 struct map_areaHeader
93 uint32 fourcc;
94 uint16 flags;
95 uint16 gridArea;
98 #define MAP_HEIGHT_NO_HEIGHT 0x0001
99 #define MAP_HEIGHT_AS_INT16 0x0002
100 #define MAP_HEIGHT_AS_INT8 0x0004
102 struct map_heightHeader
104 uint32 fourcc;
105 uint32 flags;
106 float gridHeight;
107 float gridMaxHeight;
110 #define MAP_LIQUID_NO_TYPE 0x0001
111 #define MAP_LIQUID_NO_HEIGHT 0x0002
113 struct map_liquidHeader
115 uint32 fourcc;
116 uint16 flags;
117 uint16 liquidType;
118 uint8 offsetX;
119 uint8 offsetY;
120 uint8 width;
121 uint8 height;
122 float liquidLevel;
125 enum ZLiquidStatus
127 LIQUID_MAP_NO_WATER = 0x00000000,
128 LIQUID_MAP_ABOVE_WATER = 0x00000001,
129 LIQUID_MAP_WATER_WALK = 0x00000002,
130 LIQUID_MAP_IN_WATER = 0x00000004,
131 LIQUID_MAP_UNDER_WATER = 0x00000008
134 #define MAP_LIQUID_TYPE_NO_WATER 0x00
135 #define MAP_LIQUID_TYPE_WATER 0x01
136 #define MAP_LIQUID_TYPE_OCEAN 0x02
137 #define MAP_LIQUID_TYPE_MAGMA 0x04
138 #define MAP_LIQUID_TYPE_SLIME 0x08
140 #define MAP_ALL_LIQUIDS (MAP_LIQUID_TYPE_WATER | MAP_LIQUID_TYPE_OCEAN | MAP_LIQUID_TYPE_MAGMA | MAP_LIQUID_TYPE_SLIME)
142 #define MAP_LIQUID_TYPE_DARK_WATER 0x10
143 #define MAP_LIQUID_TYPE_WMO_WATER 0x20
145 struct LiquidData
147 uint32 type;
148 float level;
149 float depth_level;
152 class GridMap
154 uint32 m_flags;
155 // Area data
156 uint16 m_gridArea;
157 uint16 *m_area_map;
158 // Height level data
159 float m_gridHeight;
160 float m_gridIntHeightMultiplier;
161 union{
162 float *m_V9;
163 uint16 *m_uint16_V9;
164 uint8 *m_uint8_V9;
166 union{
167 float *m_V8;
168 uint16 *m_uint16_V8;
169 uint8 *m_uint8_V8;
171 // Liquid data
172 uint16 m_liquidType;
173 uint8 m_liquid_offX;
174 uint8 m_liquid_offY;
175 uint8 m_liquid_width;
176 uint8 m_liquid_height;
177 float m_liquidLevel;
178 uint8 *m_liquid_type;
179 float *m_liquid_map;
181 bool loadAreaData(FILE *in, uint32 offset, uint32 size);
182 bool loadHeihgtData(FILE *in, uint32 offset, uint32 size);
183 bool loadLiquidData(FILE *in, uint32 offset, uint32 size);
185 // Get height functions and pointers
186 typedef float (GridMap::*pGetHeightPtr) (float x, float y) const;
187 pGetHeightPtr m_gridGetHeight;
188 float getHeightFromFloat(float x, float y) const;
189 float getHeightFromUint16(float x, float y) const;
190 float getHeightFromUint8(float x, float y) const;
191 float getHeightFromFlat(float x, float y) const;
193 public:
194 GridMap();
195 ~GridMap();
196 bool loadData(char *filaname);
197 void unloadData();
199 uint16 getArea(float x, float y);
200 inline float getHeight(float x, float y) {return (this->*m_gridGetHeight)(x, y);}
201 float getLiquidLevel(float x, float y);
202 uint8 getTerrainType(float x, float y);
203 ZLiquidStatus getLiquidStatus(float x, float y, float z, uint8 ReqLiquidType, LiquidData *data = 0);
206 struct CreatureMover
208 CreatureMover() : x(0), y(0), z(0), ang(0) {}
209 CreatureMover(float _x, float _y, float _z, float _ang) : x(_x), y(_y), z(_z), ang(_ang) {}
211 float x, y, z, ang;
214 // 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
215 #if defined( __GNUC__ )
216 #pragma pack(1)
217 #else
218 #pragma pack(push,1)
219 #endif
221 struct InstanceTemplate
223 uint32 map;
224 uint32 parent;
225 uint32 levelMin;
226 uint32 levelMax;
227 uint32 maxPlayers;
228 uint32 maxPlayersHeroic;
229 uint32 reset_delay; // FIX ME: now exist normal/heroic raids with possible different time of reset.
230 float startLocX;
231 float startLocY;
232 float startLocZ;
233 float startLocO;
234 uint32 script_id;
237 enum LevelRequirementVsMode
239 LEVELREQUIREMENT_HEROIC = 70
242 #if defined( __GNUC__ )
243 #pragma pack()
244 #else
245 #pragma pack(pop)
246 #endif
248 typedef UNORDERED_MAP<Creature*, CreatureMover> CreatureMoveList;
250 #define MAX_HEIGHT 100000.0f // can be use for find ground height at surface
251 #define INVALID_HEIGHT -100000.0f // for check, must be equal to VMAP_INVALID_HEIGHT, real value for unknown height is VMAP_INVALID_HEIGHT_VALUE
252 #define MIN_UNLOAD_DELAY 1 // immediate unload
254 class MANGOS_DLL_SPEC Map : public GridRefManager<NGridType>, public MaNGOS::ObjectLevelLockable<Map, ACE_Thread_Mutex>
256 friend class MapReference;
257 public:
258 Map(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode, Map* _parent = NULL);
259 virtual ~Map();
261 // currently unused for normal maps
262 bool CanUnload(uint32 diff)
264 if(!m_unloadTimer) return false;
265 if(m_unloadTimer <= diff) return true;
266 m_unloadTimer -= diff;
267 return false;
270 virtual bool Add(Player *);
271 virtual void Remove(Player *, bool);
272 template<class T> void Add(T *);
273 template<class T> void Remove(T *, bool);
275 virtual void Update(const uint32&);
277 void MessageBroadcast(Player *, WorldPacket *, bool to_self);
278 void MessageBroadcast(WorldObject *, WorldPacket *);
279 void MessageDistBroadcast(Player *, WorldPacket *, float dist, bool to_self, bool own_team_only = false);
280 void MessageDistBroadcast(WorldObject *, WorldPacket *, float dist);
282 void PlayerRelocation(Player *, float x, float y, float z, float angl);
283 void CreatureRelocation(Creature *creature, float x, float y, float, float);
285 template<class LOCK_TYPE, class T, class CONTAINER> void Visit(const CellLock<LOCK_TYPE> &cell, TypeContainerVisitor<T, CONTAINER> &visitor);
287 bool IsRemovalGrid(float x, float y) const
289 GridPair p = MaNGOS::ComputeGridPair(x, y);
290 return( !getNGrid(p.x_coord, p.y_coord) || getNGrid(p.x_coord, p.y_coord)->GetGridState() == GRID_STATE_REMOVAL );
293 bool GetUnloadLock(const GridPair &p) const { return getNGrid(p.x_coord, p.y_coord)->getUnloadLock(); }
294 void SetUnloadLock(const GridPair &p, bool on) { getNGrid(p.x_coord, p.y_coord)->setUnloadExplicitLock(on); }
295 void LoadGrid(const Cell& cell, bool no_unload = false);
296 bool UnloadGrid(const uint32 &x, const uint32 &y, bool pForce);
297 virtual void UnloadAll(bool pForce);
299 void ResetGridExpiry(NGridType &grid, float factor = 1) const
301 grid.ResetTimeTracker((time_t)((float)i_gridExpiry*factor));
304 time_t GetGridExpiry(void) const { return i_gridExpiry; }
305 uint32 GetId(void) const { return i_id; }
307 static bool ExistMap(uint32 mapid, int gx, int gy);
308 static bool ExistVMap(uint32 mapid, int gx, int gy);
310 static void InitStateMachine();
311 static void DeleteStateMachine();
313 Map const * GetParent() const { return m_parentMap; }
315 // some calls like isInWater should not use vmaps due to processor power
316 // can return INVALID_HEIGHT if under z+2 z coord not found height
317 float GetHeight(float x, float y, float z, bool pCheckVMap=true) const;
318 bool IsInWater(float x, float y, float z) const; // does not use z pos. This is for future use
320 ZLiquidStatus getLiquidStatus(float x, float y, float z, uint8 ReqLiquidType, LiquidData *data = 0) const;
322 uint16 GetAreaFlag(float x, float y, float z) const;
323 uint8 GetTerrainType(float x, float y ) const;
324 float GetWaterLevel(float x, float y ) const;
325 bool IsUnderWater(float x, float y, float z) const;
327 static uint32 GetAreaIdByAreaFlag(uint16 areaflag,uint32 map_id);
328 static uint32 GetZoneIdByAreaFlag(uint16 areaflag,uint32 map_id);
329 static void GetZoneAndAreaIdByAreaFlag(uint32& zoneid, uint32& areaid, uint16 areaflag,uint32 map_id);
331 uint32 GetAreaId(float x, float y, float z) const
333 return GetAreaIdByAreaFlag(GetAreaFlag(x,y,z),i_id);
336 uint32 GetZoneId(float x, float y, float z) const
338 return GetZoneIdByAreaFlag(GetAreaFlag(x,y,z),i_id);
341 void GetZoneAndAreaId(uint32& zoneid, uint32& areaid, float x, float y, float z) const
343 GetZoneAndAreaIdByAreaFlag(zoneid,areaid,GetAreaFlag(x,y,z),i_id);
346 virtual void MoveAllCreaturesInMoveList();
347 virtual void RemoveAllObjectsInRemoveList();
349 bool CreatureRespawnRelocation(Creature *c); // used only in MoveAllCreaturesInMoveList and ObjectGridUnloader
351 // assert print helper
352 bool CheckGridIntegrity(Creature* c, bool moved) const;
354 uint32 GetInstanceId() const { return i_InstanceId; }
355 uint8 GetSpawnMode() const { return (i_spawnMode); }
356 virtual bool CanEnter(Player* /*player*/) { return true; }
357 const char* GetMapName() const;
359 bool Instanceable() const { return i_mapEntry && i_mapEntry->Instanceable(); }
360 // NOTE: this duplicate of Instanceable(), but Instanceable() can be changed when BG also will be instanceable
361 bool IsDungeon() const { return i_mapEntry && i_mapEntry->IsDungeon(); }
362 bool IsRaid() const { return i_mapEntry && i_mapEntry->IsRaid(); }
363 bool IsHeroic() const { return i_spawnMode == DIFFICULTY_HEROIC; }
364 bool IsBattleGround() const { return i_mapEntry && i_mapEntry->IsBattleGround(); }
365 bool IsBattleArena() const { return i_mapEntry && i_mapEntry->IsBattleArena(); }
366 bool IsBattleGroundOrArena() const { return i_mapEntry && i_mapEntry->IsBattleGroundOrArena(); }
367 bool GetEntrancePos(int32 &mapid, float &x, float &y)
369 if(!i_mapEntry)
370 return false;
371 if(i_mapEntry->entrance_map < 0)
372 return false;
373 mapid = i_mapEntry->entrance_map;
374 x = i_mapEntry->entrance_x;
375 y = i_mapEntry->entrance_y;
376 return true;
379 void AddObjectToRemoveList(WorldObject *obj);
380 void DoDelayedMovesAndRemoves();
382 virtual bool RemoveBones(uint64 guid, float x, float y);
384 void UpdateObjectVisibility(WorldObject* obj, Cell cell, CellPair cellpair);
385 void UpdatePlayerVisibility(Player* player, Cell cell, CellPair cellpair);
386 void UpdateObjectsVisibilityFor(Player* player, Cell cell, CellPair cellpair);
388 void resetMarkedCells() { marked_cells.reset(); }
389 bool isCellMarked(uint32 pCellId) { return marked_cells.test(pCellId); }
390 void markCell(uint32 pCellId) { marked_cells.set(pCellId); }
392 bool HavePlayers() const { return !m_mapRefManager.isEmpty(); }
393 uint32 GetPlayersCountExceptGMs() const;
394 bool ActiveObjectsNearGrid(uint32 x,uint32 y) const;
396 void SendToPlayers(WorldPacket const* data) const;
398 typedef MapRefManager PlayerList;
399 PlayerList const& GetPlayers() const { return m_mapRefManager; }
401 //per-map script storage
402 void ScriptsStart(std::map<uint32, std::multimap<uint32, ScriptInfo> > const& scripts, uint32 id, Object* source, Object* target);
403 void ScriptCommandStart(ScriptInfo const& script, uint32 delay, Object* source, Object* target);
405 // must called with AddToWorld
406 template<class T>
407 void AddToActive(T* obj) { AddToActiveHelper(obj); }
409 void AddToActive(Creature* obj);
411 // must called with RemoveFromWorld
412 template<class T>
413 void RemoveFromActive(T* obj) { RemoveFromActiveHelper(obj); }
415 void RemoveFromActive(Creature* obj);
417 Creature* GetCreature(uint64 guid);
418 GameObject* GetGameObject(uint64 guid);
419 DynamicObject* GetDynamicObject(uint64 guid);
420 private:
421 void LoadMapAndVMap(int gx, int gy);
422 void LoadVMap(int gx, int gy);
423 void LoadMap(int gx,int gy, bool reload = false);
424 GridMap *GetGrid(float x, float y);
426 void SetTimer(uint32 t) { i_gridExpiry = t < MIN_GRID_DELAY ? MIN_GRID_DELAY : t; }
427 //uint64 CalculateGridMask(const uint32 &y) const;
429 void SendInitSelf( Player * player );
431 void SendInitTransports( Player * player );
432 void SendRemoveTransports( Player * player );
434 void PlayerRelocationNotify(Player* player, Cell cell, CellPair cellpair);
435 void CreatureRelocationNotify(Creature *creature, Cell newcell, CellPair newval);
437 bool CreatureCellRelocation(Creature *creature, Cell new_cell);
439 void AddCreatureToMoveList(Creature *c, float x, float y, float z, float ang);
440 CreatureMoveList i_creaturesToMove;
442 bool loaded(const GridPair &) const;
443 void EnsureGridCreated(const GridPair &);
444 bool EnsureGridLoaded(Cell const&);
445 void EnsureGridLoadedAtEnter(Cell const&, Player* player = NULL);
447 void buildNGridLinkage(NGridType* pNGridType) { pNGridType->link(this); }
449 template<class T> void AddType(T *obj);
450 template<class T> void RemoveType(T *obj, bool);
452 NGridType* getNGrid(uint32 x, uint32 y) const
454 return i_grids[x][y];
457 bool isGridObjectDataLoaded(uint32 x, uint32 y) const { return getNGrid(x,y)->isGridObjectDataLoaded(); }
458 void setGridObjectDataLoaded(bool pLoaded, uint32 x, uint32 y) { getNGrid(x,y)->setGridObjectDataLoaded(pLoaded); }
460 void setNGrid(NGridType* grid, uint32 x, uint32 y);
461 void ScriptsProcess();
463 protected:
464 void SetUnloadReferenceLock(const GridPair &p, bool on) { getNGrid(p.x_coord, p.y_coord)->setUnloadReferenceLock(on); }
466 typedef MaNGOS::ObjectLevelLockable<Map, ACE_Thread_Mutex>::Lock Guard;
468 MapEntry const* i_mapEntry;
469 uint8 i_spawnMode;
470 uint32 i_id;
471 uint32 i_InstanceId;
472 uint32 m_unloadTimer;
474 MapRefManager m_mapRefManager;
475 MapRefManager::iterator m_mapRefIter;
477 typedef std::set<WorldObject*> ActiveNonPlayers;
478 ActiveNonPlayers m_activeNonPlayers;
479 ActiveNonPlayers::iterator m_activeNonPlayersIter;
480 private:
481 //used for fast base_map (e.g. MapInstanced class object) search for
482 //InstanceMaps and BattleGroundMaps...
483 Map* m_parentMap;
485 typedef GridReadGuard ReadGuard;
486 typedef GridWriteGuard WriteGuard;
488 NGridType* i_grids[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
489 GridMap *GridMaps[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
490 std::bitset<TOTAL_NUMBER_OF_CELLS_PER_MAP*TOTAL_NUMBER_OF_CELLS_PER_MAP> marked_cells;
492 time_t i_gridExpiry;
494 std::set<WorldObject *> i_objectsToRemove;
495 std::multimap<time_t, ScriptAction> m_scriptSchedule;
497 // Type specific code for add/remove to/from grid
498 template<class T>
499 void AddToGrid(T*, NGridType *, Cell const&);
501 template<class T>
502 void AddNotifier(T*, Cell const&, CellPair const&);
504 template<class T>
505 void RemoveFromGrid(T*, NGridType *, Cell const&);
507 template<class T>
508 void DeleteFromWorld(T*);
510 template<class T>
511 void AddToActiveHelper(T* obj)
513 m_activeNonPlayers.insert(obj);
516 template<class T>
517 void RemoveFromActiveHelper(T* obj)
519 // Map::Update for active object in proccess
520 if(m_activeNonPlayersIter != m_activeNonPlayers.end())
522 ActiveNonPlayers::iterator itr = m_activeNonPlayers.find(obj);
523 if(itr==m_activeNonPlayersIter)
524 ++m_activeNonPlayersIter;
525 m_activeNonPlayers.erase(itr);
527 else
528 m_activeNonPlayers.erase(obj);
532 enum InstanceResetMethod
534 INSTANCE_RESET_ALL,
535 INSTANCE_RESET_CHANGE_DIFFICULTY,
536 INSTANCE_RESET_GLOBAL,
537 INSTANCE_RESET_GROUP_DISBAND,
538 INSTANCE_RESET_GROUP_JOIN,
539 INSTANCE_RESET_RESPAWN_DELAY
542 class MANGOS_DLL_SPEC InstanceMap : public Map
544 public:
545 InstanceMap(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode, Map* _parent);
546 ~InstanceMap();
547 bool Add(Player *);
548 void Remove(Player *, bool);
549 void Update(const uint32&);
550 void CreateInstanceData(bool load);
551 bool Reset(uint8 method);
552 uint32 GetScriptId() { return i_script_id; }
553 InstanceData* GetInstanceData() { return i_data; }
554 void PermBindAllPlayers(Player *player);
555 void UnloadAll(bool pForce);
556 bool CanEnter(Player* player);
557 void SendResetWarnings(uint32 timeLeft) const;
558 void SetResetSchedule(bool on);
559 uint32 GetMaxPlayers() const;
560 private:
561 bool m_resetAfterUnload;
562 bool m_unloadWhenEmpty;
563 InstanceData* i_data;
564 uint32 i_script_id;
567 class MANGOS_DLL_SPEC BattleGroundMap : public Map
569 public:
570 BattleGroundMap(uint32 id, time_t, uint32 InstanceId, Map* _parent);
571 ~BattleGroundMap();
573 bool Add(Player *);
574 void Remove(Player *, bool);
575 bool CanEnter(Player* player);
576 void SetUnload();
577 void UnloadAll(bool pForce);
580 /*inline
581 uint64
582 Map::CalculateGridMask(const uint32 &y) const
584 uint64 mask = 1;
585 mask <<= y;
586 return mask;
590 template<class LOCK_TYPE, class T, class CONTAINER>
591 inline void
592 Map::Visit(const CellLock<LOCK_TYPE> &cell, TypeContainerVisitor<T, CONTAINER> &visitor)
594 const uint32 x = cell->GridX();
595 const uint32 y = cell->GridY();
596 const uint32 cell_x = cell->CellX();
597 const uint32 cell_y = cell->CellY();
599 if( !cell->NoCreate() || loaded(GridPair(x,y)) )
601 EnsureGridLoaded(cell);
602 //LOCK_TYPE guard(i_info[x][y]->i_lock);
603 getNGrid(x, y)->Visit(cell_x, cell_y, visitor);
606 #endif