[9581] Fixed apply damage reduction to melee/ranged damage.
[getmangos.git] / src / game / Map.h
blobbd6c0c952831e40b838855889faa0d82a492fcd7
1 /*
2 * Copyright (C) 2005-2010 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"
35 #include "Utilities/TypeList.h"
37 #include <bitset>
38 #include <list>
40 class Creature;
41 class Unit;
42 class WorldPacket;
43 class InstanceData;
44 class Group;
45 class InstanceSave;
46 struct ScriptInfo;
47 struct ScriptAction;
48 class BattleGround;
50 //******************************************
51 // Map file format defines
52 //******************************************
53 struct map_fileheader
55 uint32 mapMagic;
56 uint32 versionMagic;
57 uint32 buildMagic;
58 uint32 areaMapOffset;
59 uint32 areaMapSize;
60 uint32 heightMapOffset;
61 uint32 heightMapSize;
62 uint32 liquidMapOffset;
63 uint32 liquidMapSize;
66 #define MAP_AREA_NO_AREA 0x0001
68 struct map_areaHeader
70 uint32 fourcc;
71 uint16 flags;
72 uint16 gridArea;
75 #define MAP_HEIGHT_NO_HEIGHT 0x0001
76 #define MAP_HEIGHT_AS_INT16 0x0002
77 #define MAP_HEIGHT_AS_INT8 0x0004
79 struct map_heightHeader
81 uint32 fourcc;
82 uint32 flags;
83 float gridHeight;
84 float gridMaxHeight;
87 #define MAP_LIQUID_NO_TYPE 0x0001
88 #define MAP_LIQUID_NO_HEIGHT 0x0002
90 struct map_liquidHeader
92 uint32 fourcc;
93 uint16 flags;
94 uint16 liquidType;
95 uint8 offsetX;
96 uint8 offsetY;
97 uint8 width;
98 uint8 height;
99 float liquidLevel;
102 enum ZLiquidStatus
104 LIQUID_MAP_NO_WATER = 0x00000000,
105 LIQUID_MAP_ABOVE_WATER = 0x00000001,
106 LIQUID_MAP_WATER_WALK = 0x00000002,
107 LIQUID_MAP_IN_WATER = 0x00000004,
108 LIQUID_MAP_UNDER_WATER = 0x00000008
111 #define MAP_LIQUID_TYPE_NO_WATER 0x00
112 #define MAP_LIQUID_TYPE_WATER 0x01
113 #define MAP_LIQUID_TYPE_OCEAN 0x02
114 #define MAP_LIQUID_TYPE_MAGMA 0x04
115 #define MAP_LIQUID_TYPE_SLIME 0x08
117 #define MAP_ALL_LIQUIDS (MAP_LIQUID_TYPE_WATER | MAP_LIQUID_TYPE_OCEAN | MAP_LIQUID_TYPE_MAGMA | MAP_LIQUID_TYPE_SLIME)
119 #define MAP_LIQUID_TYPE_DARK_WATER 0x10
120 #define MAP_LIQUID_TYPE_WMO_WATER 0x20
122 struct LiquidData
124 uint32 type;
125 float level;
126 float depth_level;
129 class GridMap
131 uint32 m_flags;
132 // Area data
133 uint16 m_gridArea;
134 uint16 *m_area_map;
135 // Height level data
136 float m_gridHeight;
137 float m_gridIntHeightMultiplier;
138 union{
139 float *m_V9;
140 uint16 *m_uint16_V9;
141 uint8 *m_uint8_V9;
143 union{
144 float *m_V8;
145 uint16 *m_uint16_V8;
146 uint8 *m_uint8_V8;
148 // Liquid data
149 uint16 m_liquidType;
150 uint8 m_liquid_offX;
151 uint8 m_liquid_offY;
152 uint8 m_liquid_width;
153 uint8 m_liquid_height;
154 float m_liquidLevel;
155 uint8 *m_liquid_type;
156 float *m_liquid_map;
158 bool loadAreaData(FILE *in, uint32 offset, uint32 size);
159 bool loadHeightData(FILE *in, uint32 offset, uint32 size);
160 bool loadLiquidData(FILE *in, uint32 offset, uint32 size);
162 // Get height functions and pointers
163 typedef float (GridMap::*pGetHeightPtr) (float x, float y) const;
164 pGetHeightPtr m_gridGetHeight;
165 float getHeightFromFloat(float x, float y) const;
166 float getHeightFromUint16(float x, float y) const;
167 float getHeightFromUint8(float x, float y) const;
168 float getHeightFromFlat(float x, float y) const;
170 public:
171 GridMap();
172 ~GridMap();
173 bool loadData(char *filaname);
174 void unloadData();
176 uint16 getArea(float x, float y);
177 inline float getHeight(float x, float y) {return (this->*m_gridGetHeight)(x, y);}
178 float getLiquidLevel(float x, float y);
179 uint8 getTerrainType(float x, float y);
180 ZLiquidStatus getLiquidStatus(float x, float y, float z, uint8 ReqLiquidType, LiquidData *data = 0);
183 // 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
184 #if defined( __GNUC__ )
185 #pragma pack(1)
186 #else
187 #pragma pack(push,1)
188 #endif
190 struct InstanceTemplate
192 uint32 map;
193 uint32 parent;
194 uint32 levelMin;
195 uint32 levelMax;
196 float startLocX;
197 float startLocY;
198 float startLocZ;
199 float startLocO;
200 uint32 script_id;
203 enum LevelRequirementVsMode
205 LEVELREQUIREMENT_HEROIC = 70
208 #if defined( __GNUC__ )
209 #pragma pack()
210 #else
211 #pragma pack(pop)
212 #endif
214 #define MAX_HEIGHT 100000.0f // can be use for find ground height at surface
215 #define INVALID_HEIGHT -100000.0f // for check, must be equal to VMAP_INVALID_HEIGHT, real value for unknown height is VMAP_INVALID_HEIGHT_VALUE
216 #define MIN_UNLOAD_DELAY 1 // immediate unload
218 class MANGOS_DLL_SPEC Map : public GridRefManager<NGridType>, public MaNGOS::ObjectLevelLockable<Map, ACE_Thread_Mutex>
220 friend class MapReference;
221 public:
222 Map(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode, Map* _parent = NULL);
223 virtual ~Map();
225 // currently unused for normal maps
226 bool CanUnload(uint32 diff)
228 if(!m_unloadTimer) return false;
229 if(m_unloadTimer <= diff) return true;
230 m_unloadTimer -= diff;
231 return false;
234 virtual bool Add(Player *);
235 virtual void Remove(Player *, bool);
236 template<class T> void Add(T *);
237 template<class T> void Remove(T *, bool);
239 virtual void Update(const uint32&);
241 void MessageBroadcast(Player *, WorldPacket *, bool to_self);
242 void MessageBroadcast(WorldObject *, WorldPacket *);
243 void MessageDistBroadcast(Player *, WorldPacket *, float dist, bool to_self, bool own_team_only = false);
244 void MessageDistBroadcast(WorldObject *, WorldPacket *, float dist);
246 float GetVisibilityDistance() const { return m_VisibleDistance; }
247 //function for setting up visibility distance for maps on per-type/per-Id basis
248 virtual void InitVisibilityDistance();
250 void PlayerRelocation(Player *, float x, float y, float z, float angl);
251 void CreatureRelocation(Creature *creature, float x, float y, float z, float orientation);
253 template<class T, class CONTAINER> void Visit(const Cell& cell, TypeContainerVisitor<T, CONTAINER> &visitor);
255 bool IsRemovalGrid(float x, float y) const
257 GridPair p = MaNGOS::ComputeGridPair(x, y);
258 return( !getNGrid(p.x_coord, p.y_coord) || getNGrid(p.x_coord, p.y_coord)->GetGridState() == GRID_STATE_REMOVAL );
261 bool IsLoaded(float x, float y) const
263 GridPair p = MaNGOS::ComputeGridPair(x, y);
264 return loaded(p);
267 bool GetUnloadLock(const GridPair &p) const { return getNGrid(p.x_coord, p.y_coord)->getUnloadLock(); }
268 void SetUnloadLock(const GridPair &p, bool on) { getNGrid(p.x_coord, p.y_coord)->setUnloadExplicitLock(on); }
269 void LoadGrid(const Cell& cell, bool no_unload = false);
270 bool UnloadGrid(const uint32 &x, const uint32 &y, bool pForce);
271 virtual void UnloadAll(bool pForce);
273 void ResetGridExpiry(NGridType &grid, float factor = 1) const
275 grid.ResetTimeTracker((time_t)((float)i_gridExpiry*factor));
278 time_t GetGridExpiry(void) const { return i_gridExpiry; }
279 uint32 GetId(void) const { return i_id; }
281 static bool ExistMap(uint32 mapid, int gx, int gy);
282 static bool ExistVMap(uint32 mapid, int gx, int gy);
284 static void InitStateMachine();
285 static void DeleteStateMachine();
287 Map const * GetParent() const { return m_parentMap; }
289 // some calls like isInWater should not use vmaps due to processor power
290 // can return INVALID_HEIGHT if under z+2 z coord not found height
291 float GetHeight(float x, float y, float z, bool pCheckVMap=true) const;
292 bool IsInWater(float x, float y, float z) const; // does not use z pos. This is for future use
294 ZLiquidStatus getLiquidStatus(float x, float y, float z, uint8 ReqLiquidType, LiquidData *data = 0) const;
296 uint16 GetAreaFlag(float x, float y, float z) const;
297 uint8 GetTerrainType(float x, float y ) const;
298 float GetWaterLevel(float x, float y ) const;
299 bool IsUnderWater(float x, float y, float z) const;
301 static uint32 GetAreaIdByAreaFlag(uint16 areaflag,uint32 map_id);
302 static uint32 GetZoneIdByAreaFlag(uint16 areaflag,uint32 map_id);
303 static void GetZoneAndAreaIdByAreaFlag(uint32& zoneid, uint32& areaid, uint16 areaflag,uint32 map_id);
305 uint32 GetAreaId(float x, float y, float z) const
307 return GetAreaIdByAreaFlag(GetAreaFlag(x,y,z),i_id);
310 uint32 GetZoneId(float x, float y, float z) const
312 return GetZoneIdByAreaFlag(GetAreaFlag(x,y,z),i_id);
315 void GetZoneAndAreaId(uint32& zoneid, uint32& areaid, float x, float y, float z) const
317 GetZoneAndAreaIdByAreaFlag(zoneid,areaid,GetAreaFlag(x,y,z),i_id);
320 virtual void RemoveAllObjectsInRemoveList();
322 bool CreatureRespawnRelocation(Creature *c); // used only in CreatureRelocation and ObjectGridUnloader
324 // assert print helper
325 bool CheckGridIntegrity(Creature* c, bool moved) const;
327 uint32 GetInstanceId() const { return i_InstanceId; }
328 virtual bool CanEnter(Player* /*player*/) { return true; }
329 const char* GetMapName() const;
331 // have meaning only for instanced map (that have set real difficulty), NOT USE its for BaseMap
332 // _currently_ spawnmode == difficulty, but this can be changes later, so use appropriate spawmmode/difficult functions
333 // for simplify later code support
334 // regular difficulty = continent/dungeon normal/first raid normal difficulty
335 uint8 GetSpawnMode() const { return (i_spawnMode); }
336 Difficulty GetDifficulty() const { return Difficulty(GetSpawnMode()); }
337 bool IsRegularDifficulty() const { return GetDifficulty() == REGULAR_DIFFICULTY; }
338 uint32 GetMaxPlayers() const; // dependent from map difficulty
339 uint32 GetMaxResetDelay() const; // dependent from map difficulty
340 MapDifficulty const* GetMapDifficulty() const; // dependent from map difficulty
342 bool Instanceable() const { return i_mapEntry && i_mapEntry->Instanceable(); }
343 // NOTE: this duplicate of Instanceable(), but Instanceable() can be changed when BG also will be instanceable
344 bool IsDungeon() const { return i_mapEntry && i_mapEntry->IsDungeon(); }
345 bool IsRaid() const { return i_mapEntry && i_mapEntry->IsRaid(); }
346 bool IsRaidOrHeroicDungeon() const { return IsRaid() || GetDifficulty() > DUNGEON_DIFFICULTY_NORMAL; }
347 bool IsBattleGround() const { return i_mapEntry && i_mapEntry->IsBattleGround(); }
348 bool IsBattleArena() const { return i_mapEntry && i_mapEntry->IsBattleArena(); }
349 bool IsBattleGroundOrArena() const { return i_mapEntry && i_mapEntry->IsBattleGroundOrArena(); }
350 bool GetEntrancePos(int32 &mapid, float &x, float &y)
352 if(!i_mapEntry)
353 return false;
354 if(i_mapEntry->entrance_map < 0)
355 return false;
356 mapid = i_mapEntry->entrance_map;
357 x = i_mapEntry->entrance_x;
358 y = i_mapEntry->entrance_y;
359 return true;
362 void AddObjectToRemoveList(WorldObject *obj);
364 virtual bool RemoveBones(uint64 guid, float x, float y);
366 void UpdateObjectVisibility(WorldObject* obj, Cell cell, CellPair cellpair);
367 void UpdatePlayerVisibility(Player* player, Cell cell, CellPair cellpair);
368 void UpdateObjectsVisibilityFor(Player* player, Cell cell, CellPair cellpair);
370 void resetMarkedCells() { marked_cells.reset(); }
371 bool isCellMarked(uint32 pCellId) { return marked_cells.test(pCellId); }
372 void markCell(uint32 pCellId) { marked_cells.set(pCellId); }
374 bool HavePlayers() const { return !m_mapRefManager.isEmpty(); }
375 uint32 GetPlayersCountExceptGMs() const;
376 bool ActiveObjectsNearGrid(uint32 x,uint32 y) const;
378 void SendToPlayers(WorldPacket const* data) const;
380 typedef MapRefManager PlayerList;
381 PlayerList const& GetPlayers() const { return m_mapRefManager; }
383 //per-map script storage
384 void ScriptsStart(std::map<uint32, std::multimap<uint32, ScriptInfo> > const& scripts, uint32 id, Object* source, Object* target);
385 void ScriptCommandStart(ScriptInfo const& script, uint32 delay, Object* source, Object* target);
387 // must called with AddToWorld
388 template<class T>
389 void AddToActive(T* obj) { AddToActiveHelper(obj); }
391 void AddToActive(Creature* obj);
393 // must called with RemoveFromWorld
394 template<class T>
395 void RemoveFromActive(T* obj) { RemoveFromActiveHelper(obj); }
397 void RemoveFromActive(Creature* obj);
399 Creature* GetCreature(ObjectGuid guid);
400 Vehicle* GetVehicle(ObjectGuid guid);
401 Pet* GetPet(ObjectGuid guid);
402 Creature* GetCreatureOrPetOrVehicle(ObjectGuid guid);
403 GameObject* GetGameObject(ObjectGuid guid);
404 DynamicObject* GetDynamicObject(ObjectGuid guid);
405 Corpse* GetCorpse(ObjectGuid guid);
406 WorldObject* GetWorldObject(ObjectGuid guid);
408 TypeUnorderedMapContainer<AllMapStoredObjectTypes>& GetObjectsStore() { return m_objectsStore; }
410 void AddUpdateObject(Object *obj)
412 i_objectsToClientUpdate.insert(obj);
415 void RemoveUpdateObject(Object *obj)
417 i_objectsToClientUpdate.erase( obj );
420 // DynObjects currently
421 uint32 GenerateLocalLowGuid(HighGuid guidhigh);
422 private:
423 void LoadMapAndVMap(int gx, int gy);
424 void LoadVMap(int gx, int gy);
425 void LoadMap(int gx,int gy, bool reload = false);
426 GridMap *GetGrid(float x, float y);
428 void SetTimer(uint32 t) { i_gridExpiry = t < MIN_GRID_DELAY ? MIN_GRID_DELAY : t; }
430 void SendInitSelf( Player * player );
432 void SendInitTransports( Player * player );
433 void SendRemoveTransports( Player * player );
435 void PlayerRelocationNotify(Player* player, Cell cell, CellPair cellpair);
437 bool CreatureCellRelocation(Creature *creature, Cell new_cell);
439 bool loaded(const GridPair &) const;
440 void EnsureGridCreated(const GridPair &);
441 bool EnsureGridLoaded(Cell const&);
442 void EnsureGridLoadedAtEnter(Cell const&, Player* player = NULL);
444 void buildNGridLinkage(NGridType* pNGridType) { pNGridType->link(this); }
446 template<class T> void AddType(T *obj);
447 template<class T> void RemoveType(T *obj, bool);
449 NGridType* getNGrid(uint32 x, uint32 y) const
451 ASSERT(x < MAX_NUMBER_OF_GRIDS);
452 ASSERT(y < MAX_NUMBER_OF_GRIDS);
453 return i_grids[x][y];
456 bool isGridObjectDataLoaded(uint32 x, uint32 y) const { return getNGrid(x,y)->isGridObjectDataLoaded(); }
457 void setGridObjectDataLoaded(bool pLoaded, uint32 x, uint32 y) { getNGrid(x,y)->setGridObjectDataLoaded(pLoaded); }
459 void setNGrid(NGridType* grid, uint32 x, uint32 y);
460 void ScriptsProcess();
462 void SendObjectUpdates();
463 std::set<Object *> i_objectsToClientUpdate;
464 protected:
465 void SetUnloadReferenceLock(const GridPair &p, bool on) { getNGrid(p.x_coord, p.y_coord)->setUnloadReferenceLock(on); }
467 typedef MaNGOS::ObjectLevelLockable<Map, ACE_Thread_Mutex>::Lock Guard;
469 MapEntry const* i_mapEntry;
470 uint8 i_spawnMode;
471 uint32 i_id;
472 uint32 i_InstanceId;
473 uint32 m_unloadTimer;
474 float m_VisibleDistance;
476 MapRefManager m_mapRefManager;
477 MapRefManager::iterator m_mapRefIter;
479 typedef std::set<WorldObject*> ActiveNonPlayers;
480 ActiveNonPlayers m_activeNonPlayers;
481 ActiveNonPlayers::iterator m_activeNonPlayersIter;
482 TypeUnorderedMapContainer<AllMapStoredObjectTypes> m_objectsStore;
483 private:
484 time_t i_gridExpiry;
486 //used for fast base_map (e.g. MapInstanced class object) search for
487 //InstanceMaps and BattleGroundMaps...
488 Map* m_parentMap;
490 NGridType* i_grids[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
491 GridMap *GridMaps[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
492 std::bitset<TOTAL_NUMBER_OF_CELLS_PER_MAP*TOTAL_NUMBER_OF_CELLS_PER_MAP> marked_cells;
494 std::set<WorldObject *> i_objectsToRemove;
495 std::multimap<time_t, ScriptAction> m_scriptSchedule;
497 // Map local low guid counters
498 ObjectGuidGenerator<HIGHGUID_DYNAMICOBJECT> m_DynObjectGuids;
499 ObjectGuidGenerator<HIGHGUID_PET> m_PetGuids;
500 ObjectGuidGenerator<HIGHGUID_VEHICLE> m_VehicleGuids;
502 // Type specific code for add/remove to/from grid
503 template<class T>
504 void AddToGrid(T*, NGridType *, Cell const&);
506 template<class T>
507 void AddNotifier(T*, Cell const&, CellPair const&);
509 template<class T>
510 void RemoveFromGrid(T*, NGridType *, Cell const&);
512 template<class T>
513 void DeleteFromWorld(T*);
515 template<class T>
516 void AddToActiveHelper(T* obj)
518 m_activeNonPlayers.insert(obj);
521 template<class T>
522 void RemoveFromActiveHelper(T* obj)
524 // Map::Update for active object in proccess
525 if(m_activeNonPlayersIter != m_activeNonPlayers.end())
527 ActiveNonPlayers::iterator itr = m_activeNonPlayers.find(obj);
528 if(itr==m_activeNonPlayersIter)
529 ++m_activeNonPlayersIter;
530 m_activeNonPlayers.erase(itr);
532 else
533 m_activeNonPlayers.erase(obj);
537 enum InstanceResetMethod
539 INSTANCE_RESET_ALL,
540 INSTANCE_RESET_CHANGE_DIFFICULTY,
541 INSTANCE_RESET_GLOBAL,
542 INSTANCE_RESET_GROUP_DISBAND,
543 INSTANCE_RESET_GROUP_JOIN,
544 INSTANCE_RESET_RESPAWN_DELAY
547 class MANGOS_DLL_SPEC InstanceMap : public Map
549 public:
550 InstanceMap(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode, Map* _parent);
551 ~InstanceMap();
552 bool Add(Player *);
553 void Remove(Player *, bool);
554 void Update(const uint32&);
555 void CreateInstanceData(bool load);
556 bool Reset(uint8 method);
557 uint32 GetScriptId() { return i_script_id; }
558 InstanceData* GetInstanceData() { return i_data; }
559 void PermBindAllPlayers(Player *player);
560 void UnloadAll(bool pForce);
561 bool CanEnter(Player* player);
562 void SendResetWarnings(uint32 timeLeft) const;
563 void SetResetSchedule(bool on);
565 virtual void InitVisibilityDistance();
566 private:
567 bool m_resetAfterUnload;
568 bool m_unloadWhenEmpty;
569 InstanceData* i_data;
570 uint32 i_script_id;
573 class MANGOS_DLL_SPEC BattleGroundMap : public Map
575 public:
576 BattleGroundMap(uint32 id, time_t, uint32 InstanceId, Map* _parent, uint8 spawnMode);
577 ~BattleGroundMap();
579 bool Add(Player *);
580 void Remove(Player *, bool);
581 bool CanEnter(Player* player);
582 void SetUnload();
583 void UnloadAll(bool pForce);
585 virtual void InitVisibilityDistance();
586 BattleGround* GetBG() { return m_bg; }
587 void SetBG(BattleGround* bg) { m_bg = bg; }
588 private:
589 BattleGround* m_bg;
592 /*inline
593 uint64
594 Map::CalculateGridMask(const uint32 &y) const
596 uint64 mask = 1;
597 mask <<= y;
598 return mask;
602 template<class T, class CONTAINER>
603 inline void
604 Map::Visit(const Cell& cell, TypeContainerVisitor<T, CONTAINER> &visitor)
606 const uint32 x = cell.GridX();
607 const uint32 y = cell.GridY();
608 const uint32 cell_x = cell.CellX();
609 const uint32 cell_y = cell.CellY();
611 if( !cell.NoCreate() || loaded(GridPair(x,y)) )
613 EnsureGridLoaded(cell);
614 getNGrid(x, y)->Visit(cell_x, cell_y, visitor);
617 #endif