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
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"
32 #include "SharedDefines.h"
33 #include "GameSystem/GridRefManager.h"
34 #include "MapRefManager.h"
35 #include "Utilities/TypeList.h"
51 typedef ACE_RW_Thread_Mutex GridRWLock
;
53 template<class MUTEX
, class LOCK_TYPE
>
56 RGuard(MUTEX
&l
) : i_lock(l
.getReadLock()) {}
57 MaNGOS::GeneralLock
<LOCK_TYPE
> i_lock
;
60 template<class MUTEX
, class LOCK_TYPE
>
63 WGuard(MUTEX
&l
) : i_lock(l
.getWriteLock()) {}
64 MaNGOS::GeneralLock
<LOCK_TYPE
> i_lock
;
67 typedef RGuard
<GridRWLock
, ACE_Thread_Mutex
> GridReadGuard
;
68 typedef WGuard
<GridRWLock
, ACE_Thread_Mutex
> GridWriteGuard
;
69 typedef MaNGOS::SingleThreaded
<GridRWLock
>::Lock NullGuard
;
71 //******************************************
72 // Map file format defines
73 //******************************************
80 uint32 heightMapOffset
;
82 uint32 liquidMapOffset
;
86 #define MAP_AREA_NO_AREA 0x0001
95 #define MAP_HEIGHT_NO_HEIGHT 0x0001
96 #define MAP_HEIGHT_AS_INT16 0x0002
97 #define MAP_HEIGHT_AS_INT8 0x0004
99 struct map_heightHeader
107 #define MAP_LIQUID_NO_TYPE 0x0001
108 #define MAP_LIQUID_NO_HEIGHT 0x0002
110 struct map_liquidHeader
124 LIQUID_MAP_NO_WATER
= 0x00000000,
125 LIQUID_MAP_ABOVE_WATER
= 0x00000001,
126 LIQUID_MAP_WATER_WALK
= 0x00000002,
127 LIQUID_MAP_IN_WATER
= 0x00000004,
128 LIQUID_MAP_UNDER_WATER
= 0x00000008
131 #define MAP_LIQUID_TYPE_NO_WATER 0x00
132 #define MAP_LIQUID_TYPE_WATER 0x01
133 #define MAP_LIQUID_TYPE_OCEAN 0x02
134 #define MAP_LIQUID_TYPE_MAGMA 0x04
135 #define MAP_LIQUID_TYPE_SLIME 0x08
137 #define MAP_ALL_LIQUIDS (MAP_LIQUID_TYPE_WATER | MAP_LIQUID_TYPE_OCEAN | MAP_LIQUID_TYPE_MAGMA | MAP_LIQUID_TYPE_SLIME)
139 #define MAP_LIQUID_TYPE_DARK_WATER 0x10
140 #define MAP_LIQUID_TYPE_WMO_WATER 0x20
157 float m_gridIntHeightMultiplier
;
172 uint8 m_liquid_width
;
173 uint8 m_liquid_height
;
175 uint8
*m_liquid_type
;
178 bool loadAreaData(FILE *in
, uint32 offset
, uint32 size
);
179 bool loadHeightData(FILE *in
, uint32 offset
, uint32 size
);
180 bool loadLiquidData(FILE *in
, uint32 offset
, uint32 size
);
182 // Get height functions and pointers
183 typedef float (GridMap::*pGetHeightPtr
) (float x
, float y
) const;
184 pGetHeightPtr m_gridGetHeight
;
185 float getHeightFromFloat(float x
, float y
) const;
186 float getHeightFromUint16(float x
, float y
) const;
187 float getHeightFromUint8(float x
, float y
) const;
188 float getHeightFromFlat(float x
, float y
) const;
193 bool loadData(char *filaname
);
196 uint16
getArea(float x
, float y
);
197 inline float getHeight(float x
, float y
) {return (this->*m_gridGetHeight
)(x
, y
);}
198 float getLiquidLevel(float x
, float y
);
199 uint8
getTerrainType(float x
, float y
);
200 ZLiquidStatus
getLiquidStatus(float x
, float y
, float z
, uint8 ReqLiquidType
, LiquidData
*data
= 0);
205 CreatureMover() : x(0), y(0), z(0), ang(0) {}
206 CreatureMover(float _x
, float _y
, float _z
, float _ang
) : x(_x
), y(_y
), z(_z
), ang(_ang
) {}
211 // 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
212 #if defined( __GNUC__ )
218 struct InstanceTemplate
231 enum LevelRequirementVsMode
233 LEVELREQUIREMENT_HEROIC
= 70
236 #if defined( __GNUC__ )
242 typedef UNORDERED_MAP
<Creature
*, CreatureMover
> CreatureMoveList
;
244 #define MAX_HEIGHT 100000.0f // can be use for find ground height at surface
245 #define INVALID_HEIGHT -100000.0f // for check, must be equal to VMAP_INVALID_HEIGHT, real value for unknown height is VMAP_INVALID_HEIGHT_VALUE
246 #define MIN_UNLOAD_DELAY 1 // immediate unload
248 class MANGOS_DLL_SPEC Map
: public GridRefManager
<NGridType
>, public MaNGOS::ObjectLevelLockable
<Map
, ACE_Thread_Mutex
>
250 friend class MapReference
;
252 Map(uint32 id
, time_t, uint32 InstanceId
, uint8 SpawnMode
, Map
* _parent
= NULL
);
255 // currently unused for normal maps
256 bool CanUnload(uint32 diff
)
258 if(!m_unloadTimer
) return false;
259 if(m_unloadTimer
<= diff
) return true;
260 m_unloadTimer
-= diff
;
264 virtual bool Add(Player
*);
265 virtual void Remove(Player
*, bool);
266 template<class T
> void Add(T
*);
267 template<class T
> void Remove(T
*, bool);
269 virtual void Update(const uint32
&);
271 void MessageBroadcast(Player
*, WorldPacket
*, bool to_self
);
272 void MessageBroadcast(WorldObject
*, WorldPacket
*);
273 void MessageDistBroadcast(Player
*, WorldPacket
*, float dist
, bool to_self
, bool own_team_only
= false);
274 void MessageDistBroadcast(WorldObject
*, WorldPacket
*, float dist
);
276 float GetVisibilityDistance() const { return m_VisibleDistance
; }
277 //function for setting up visibility distance for maps on per-type/per-Id basis
278 virtual void InitVisibilityDistance();
280 void PlayerRelocation(Player
*, float x
, float y
, float z
, float angl
);
281 void CreatureRelocation(Creature
*creature
, float x
, float y
, float z
, float orientation
);
283 template<class LOCK_TYPE
, class T
, class CONTAINER
> void Visit(const CellLock
<LOCK_TYPE
> &cell
, TypeContainerVisitor
<T
, CONTAINER
> &visitor
);
285 bool IsRemovalGrid(float x
, float y
) const
287 GridPair p
= MaNGOS::ComputeGridPair(x
, y
);
288 return( !getNGrid(p
.x_coord
, p
.y_coord
) || getNGrid(p
.x_coord
, p
.y_coord
)->GetGridState() == GRID_STATE_REMOVAL
);
291 bool IsLoaded(float x
, float y
) const
293 GridPair p
= MaNGOS::ComputeGridPair(x
, y
);
297 bool GetUnloadLock(const GridPair
&p
) const { return getNGrid(p
.x_coord
, p
.y_coord
)->getUnloadLock(); }
298 void SetUnloadLock(const GridPair
&p
, bool on
) { getNGrid(p
.x_coord
, p
.y_coord
)->setUnloadExplicitLock(on
); }
299 void LoadGrid(const Cell
& cell
, bool no_unload
= false);
300 bool UnloadGrid(const uint32
&x
, const uint32
&y
, bool pForce
);
301 virtual void UnloadAll(bool pForce
);
303 void ResetGridExpiry(NGridType
&grid
, float factor
= 1) const
305 grid
.ResetTimeTracker((time_t)((float)i_gridExpiry
*factor
));
308 time_t GetGridExpiry(void) const { return i_gridExpiry
; }
309 uint32
GetId(void) const { return i_id
; }
311 static bool ExistMap(uint32 mapid
, int gx
, int gy
);
312 static bool ExistVMap(uint32 mapid
, int gx
, int gy
);
314 static void InitStateMachine();
315 static void DeleteStateMachine();
317 Map
const * GetParent() const { return m_parentMap
; }
319 // some calls like isInWater should not use vmaps due to processor power
320 // can return INVALID_HEIGHT if under z+2 z coord not found height
321 float GetHeight(float x
, float y
, float z
, bool pCheckVMap
=true) const;
322 bool IsInWater(float x
, float y
, float z
) const; // does not use z pos. This is for future use
324 ZLiquidStatus
getLiquidStatus(float x
, float y
, float z
, uint8 ReqLiquidType
, LiquidData
*data
= 0) const;
326 uint16
GetAreaFlag(float x
, float y
, float z
) const;
327 uint8
GetTerrainType(float x
, float y
) const;
328 float GetWaterLevel(float x
, float y
) const;
329 bool IsUnderWater(float x
, float y
, float z
) const;
331 static uint32
GetAreaIdByAreaFlag(uint16 areaflag
,uint32 map_id
);
332 static uint32
GetZoneIdByAreaFlag(uint16 areaflag
,uint32 map_id
);
333 static void GetZoneAndAreaIdByAreaFlag(uint32
& zoneid
, uint32
& areaid
, uint16 areaflag
,uint32 map_id
);
335 uint32
GetAreaId(float x
, float y
, float z
) const
337 return GetAreaIdByAreaFlag(GetAreaFlag(x
,y
,z
),i_id
);
340 uint32
GetZoneId(float x
, float y
, float z
) const
342 return GetZoneIdByAreaFlag(GetAreaFlag(x
,y
,z
),i_id
);
345 void GetZoneAndAreaId(uint32
& zoneid
, uint32
& areaid
, float x
, float y
, float z
) const
347 GetZoneAndAreaIdByAreaFlag(zoneid
,areaid
,GetAreaFlag(x
,y
,z
),i_id
);
350 virtual void MoveAllCreaturesInMoveList();
351 virtual void RemoveAllObjectsInRemoveList();
353 bool CreatureRespawnRelocation(Creature
*c
); // used only in MoveAllCreaturesInMoveList and ObjectGridUnloader
355 // assert print helper
356 bool CheckGridIntegrity(Creature
* c
, bool moved
) const;
358 uint32
GetInstanceId() const { return i_InstanceId
; }
359 virtual bool CanEnter(Player
* /*player*/) { return true; }
360 const char* GetMapName() const;
362 // have meaning only for instanced map (that have set real difficulty), NOT USE its for BaseMap
363 // _currently_ spawnmode == difficulty, but this can be changes later, so use appropriate spawmmode/difficult functions
364 // for simplify later code support
365 // regular difficulty = continent/dungeon normal/first raid normal difficulty
366 uint8
GetSpawnMode() const { return (i_spawnMode
); }
367 Difficulty
GetDifficulty() const { return Difficulty(GetSpawnMode()); }
368 bool IsRegularDifficulty() const { return GetDifficulty() == REGULAR_DIFFICULTY
; }
369 uint32
GetMaxPlayers() const; // dependent from map difficulty
370 uint32
GetMaxResetDelay() const; // dependent from map difficulty
371 MapDifficulty
const* GetMapDifficulty() const; // dependent from map difficulty
373 bool Instanceable() const { return i_mapEntry
&& i_mapEntry
->Instanceable(); }
374 // NOTE: this duplicate of Instanceable(), but Instanceable() can be changed when BG also will be instanceable
375 bool IsDungeon() const { return i_mapEntry
&& i_mapEntry
->IsDungeon(); }
376 bool IsRaid() const { return i_mapEntry
&& i_mapEntry
->IsRaid(); }
377 bool IsRaidOrHeroicDungeon() const { return IsRaid() || GetDifficulty() > DUNGEON_DIFFICULTY_NORMAL
; }
378 bool IsBattleGround() const { return i_mapEntry
&& i_mapEntry
->IsBattleGround(); }
379 bool IsBattleArena() const { return i_mapEntry
&& i_mapEntry
->IsBattleArena(); }
380 bool IsBattleGroundOrArena() const { return i_mapEntry
&& i_mapEntry
->IsBattleGroundOrArena(); }
381 bool GetEntrancePos(int32
&mapid
, float &x
, float &y
)
385 if(i_mapEntry
->entrance_map
< 0)
387 mapid
= i_mapEntry
->entrance_map
;
388 x
= i_mapEntry
->entrance_x
;
389 y
= i_mapEntry
->entrance_y
;
393 void AddObjectToRemoveList(WorldObject
*obj
);
394 void DoDelayedMovesAndRemoves();
396 virtual bool RemoveBones(uint64 guid
, float x
, float y
);
398 void UpdateObjectVisibility(WorldObject
* obj
, Cell cell
, CellPair cellpair
);
399 void UpdatePlayerVisibility(Player
* player
, Cell cell
, CellPair cellpair
);
400 void UpdateObjectsVisibilityFor(Player
* player
, Cell cell
, CellPair cellpair
);
402 void resetMarkedCells() { marked_cells
.reset(); }
403 bool isCellMarked(uint32 pCellId
) { return marked_cells
.test(pCellId
); }
404 void markCell(uint32 pCellId
) { marked_cells
.set(pCellId
); }
406 bool HavePlayers() const { return !m_mapRefManager
.isEmpty(); }
407 uint32
GetPlayersCountExceptGMs() const;
408 bool ActiveObjectsNearGrid(uint32 x
,uint32 y
) const;
410 void SendToPlayers(WorldPacket
const* data
) const;
412 typedef MapRefManager PlayerList
;
413 PlayerList
const& GetPlayers() const { return m_mapRefManager
; }
415 //per-map script storage
416 void ScriptsStart(std::map
<uint32
, std::multimap
<uint32
, ScriptInfo
> > const& scripts
, uint32 id
, Object
* source
, Object
* target
);
417 void ScriptCommandStart(ScriptInfo
const& script
, uint32 delay
, Object
* source
, Object
* target
);
419 // must called with AddToWorld
421 void AddToActive(T
* obj
) { AddToActiveHelper(obj
); }
423 void AddToActive(Creature
* obj
);
425 // must called with RemoveFromWorld
427 void RemoveFromActive(T
* obj
) { RemoveFromActiveHelper(obj
); }
429 void RemoveFromActive(Creature
* obj
);
431 Creature
* GetCreature(uint64 guid
);
432 Vehicle
* GetVehicle(uint64 guid
);
433 Pet
* GetPet(uint64 guid
);
434 Creature
* GetCreatureOrPetOrVehicle(uint64 guid
);
435 GameObject
* GetGameObject(uint64 guid
);
436 DynamicObject
* GetDynamicObject(uint64 guid
);
437 Corpse
* GetCorpse(uint64 guid
);
438 WorldObject
* GetWorldObject(uint64 guid
);
440 TypeUnorderedMapContainer
<AllMapStoredObjectTypes
>& GetObjectsStore() { return m_objectsStore
; }
442 void AddUpdateObject(Object
*obj
)
444 i_objectsToClientUpdate
.insert(obj
);
447 void RemoveUpdateObject(Object
*obj
)
449 i_objectsToClientUpdate
.erase( obj
);
452 // DynObjects currently
453 uint32
GenerateLocalLowGuid(HighGuid guidhigh
);
455 void LoadMapAndVMap(int gx
, int gy
);
456 void LoadVMap(int gx
, int gy
);
457 void LoadMap(int gx
,int gy
, bool reload
= false);
458 GridMap
*GetGrid(float x
, float y
);
460 void SetTimer(uint32 t
) { i_gridExpiry
= t
< MIN_GRID_DELAY
? MIN_GRID_DELAY
: t
; }
462 void SendInitSelf( Player
* player
);
464 void SendInitTransports( Player
* player
);
465 void SendRemoveTransports( Player
* player
);
467 void PlayerRelocationNotify(Player
* player
, Cell cell
, CellPair cellpair
);
468 void CreatureRelocationNotify(Creature
*creature
, Cell newcell
, CellPair newval
);
470 bool CreatureCellRelocation(Creature
*creature
, Cell new_cell
);
472 void AddCreatureToMoveList(Creature
*c
, float x
, float y
, float z
, float ang
);
473 CreatureMoveList i_creaturesToMove
;
475 bool loaded(const GridPair
&) const;
476 void EnsureGridCreated(const GridPair
&);
477 bool EnsureGridLoaded(Cell
const&);
478 void EnsureGridLoadedAtEnter(Cell
const&, Player
* player
= NULL
);
480 void buildNGridLinkage(NGridType
* pNGridType
) { pNGridType
->link(this); }
482 template<class T
> void AddType(T
*obj
);
483 template<class T
> void RemoveType(T
*obj
, bool);
485 NGridType
* getNGrid(uint32 x
, uint32 y
) const
487 ASSERT(x
< MAX_NUMBER_OF_GRIDS
);
488 ASSERT(y
< MAX_NUMBER_OF_GRIDS
);
489 return i_grids
[x
][y
];
492 bool isGridObjectDataLoaded(uint32 x
, uint32 y
) const { return getNGrid(x
,y
)->isGridObjectDataLoaded(); }
493 void setGridObjectDataLoaded(bool pLoaded
, uint32 x
, uint32 y
) { getNGrid(x
,y
)->setGridObjectDataLoaded(pLoaded
); }
495 void setNGrid(NGridType
* grid
, uint32 x
, uint32 y
);
496 void ScriptsProcess();
498 void SendObjectUpdates();
499 std::set
<Object
*> i_objectsToClientUpdate
;
501 void SetUnloadReferenceLock(const GridPair
&p
, bool on
) { getNGrid(p
.x_coord
, p
.y_coord
)->setUnloadReferenceLock(on
); }
503 typedef MaNGOS::ObjectLevelLockable
<Map
, ACE_Thread_Mutex
>::Lock Guard
;
505 MapEntry
const* i_mapEntry
;
509 uint32 m_unloadTimer
;
510 float m_VisibleDistance
;
512 MapRefManager m_mapRefManager
;
513 MapRefManager::iterator m_mapRefIter
;
515 typedef std::set
<WorldObject
*> ActiveNonPlayers
;
516 ActiveNonPlayers m_activeNonPlayers
;
517 ActiveNonPlayers::iterator m_activeNonPlayersIter
;
518 TypeUnorderedMapContainer
<AllMapStoredObjectTypes
> m_objectsStore
;
522 //used for fast base_map (e.g. MapInstanced class object) search for
523 //InstanceMaps and BattleGroundMaps...
526 typedef GridReadGuard ReadGuard
;
527 typedef GridWriteGuard WriteGuard
;
529 NGridType
* i_grids
[MAX_NUMBER_OF_GRIDS
][MAX_NUMBER_OF_GRIDS
];
530 GridMap
*GridMaps
[MAX_NUMBER_OF_GRIDS
][MAX_NUMBER_OF_GRIDS
];
531 std::bitset
<TOTAL_NUMBER_OF_CELLS_PER_MAP
*TOTAL_NUMBER_OF_CELLS_PER_MAP
> marked_cells
;
533 std::set
<WorldObject
*> i_objectsToRemove
;
534 std::multimap
<time_t, ScriptAction
> m_scriptSchedule
;
536 // Map local low guid counters
537 uint32 m_hiDynObjectGuid
;
539 uint32 m_hiVehicleGuid
;
541 // Type specific code for add/remove to/from grid
543 void AddToGrid(T
*, NGridType
*, Cell
const&);
546 void AddNotifier(T
*, Cell
const&, CellPair
const&);
549 void RemoveFromGrid(T
*, NGridType
*, Cell
const&);
552 void DeleteFromWorld(T
*);
555 void AddToActiveHelper(T
* obj
)
557 m_activeNonPlayers
.insert(obj
);
561 void RemoveFromActiveHelper(T
* obj
)
563 // Map::Update for active object in proccess
564 if(m_activeNonPlayersIter
!= m_activeNonPlayers
.end())
566 ActiveNonPlayers::iterator itr
= m_activeNonPlayers
.find(obj
);
567 if(itr
==m_activeNonPlayersIter
)
568 ++m_activeNonPlayersIter
;
569 m_activeNonPlayers
.erase(itr
);
572 m_activeNonPlayers
.erase(obj
);
576 enum InstanceResetMethod
579 INSTANCE_RESET_CHANGE_DIFFICULTY
,
580 INSTANCE_RESET_GLOBAL
,
581 INSTANCE_RESET_GROUP_DISBAND
,
582 INSTANCE_RESET_GROUP_JOIN
,
583 INSTANCE_RESET_RESPAWN_DELAY
586 class MANGOS_DLL_SPEC InstanceMap
: public Map
589 InstanceMap(uint32 id
, time_t, uint32 InstanceId
, uint8 SpawnMode
, Map
* _parent
);
592 void Remove(Player
*, bool);
593 void Update(const uint32
&);
594 void CreateInstanceData(bool load
);
595 bool Reset(uint8 method
);
596 uint32
GetScriptId() { return i_script_id
; }
597 InstanceData
* GetInstanceData() { return i_data
; }
598 void PermBindAllPlayers(Player
*player
);
599 void UnloadAll(bool pForce
);
600 bool CanEnter(Player
* player
);
601 void SendResetWarnings(uint32 timeLeft
) const;
602 void SetResetSchedule(bool on
);
604 virtual void InitVisibilityDistance();
606 bool m_resetAfterUnload
;
607 bool m_unloadWhenEmpty
;
608 InstanceData
* i_data
;
612 class MANGOS_DLL_SPEC BattleGroundMap
: public Map
615 BattleGroundMap(uint32 id
, time_t, uint32 InstanceId
, Map
* _parent
, uint8 spawnMode
);
619 void Remove(Player
*, bool);
620 bool CanEnter(Player
* player
);
622 void UnloadAll(bool pForce
);
624 virtual void InitVisibilityDistance();
625 BattleGround
* GetBG() { return m_bg
; }
626 void SetBG(BattleGround
* bg
) { m_bg
= bg
; }
633 Map::CalculateGridMask(const uint32 &y) const
641 template<class LOCK_TYPE
, class T
, class CONTAINER
>
643 Map::Visit(const CellLock
<LOCK_TYPE
> &cell
, TypeContainerVisitor
<T
, CONTAINER
> &visitor
)
645 const uint32 x
= cell
->GridX();
646 const uint32 y
= cell
->GridY();
647 const uint32 cell_x
= cell
->CellX();
648 const uint32 cell_y
= cell
->CellY();
650 if( !cell
->NoCreate() || loaded(GridPair(x
,y
)) )
652 EnsureGridLoaded(cell
);
653 //LOCK_TYPE guard(i_info[x][y]->i_lock);
654 getNGrid(x
, y
)->Visit(cell_x
, cell_y
, visitor
);