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
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 "DBCStructure.h"
28 #include "GridDefines.h"
32 #include "SharedDefines.h"
33 #include "GameSystem/GridRefManager.h"
34 #include "MapRefManager.h"
51 typedef ZThread::FairReadWriteLock 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
, ZThread::Lockable
> GridReadGuard
;
68 typedef WGuard
<GridRWLock
, ZThread::Lockable
> GridWriteGuard
;
69 typedef MaNGOS::SingleThreaded
<GridRWLock
>::Lock NullGuard
;
71 //******************************************
72 // Map file format defines
73 //******************************************
74 #define MAP_MAGIC 'SPAM'
75 #define MAP_VERSION_MAGIC '0.1w'
76 #define MAP_AREA_MAGIC 'AERA'
77 #define MAP_HEIGTH_MAGIC 'TGHM'
78 #define MAP_LIQUID_MAGIC 'QILM'
80 struct map_fileheader
{
85 uint32 heightMapOffset
;
87 uint32 liquidMapOffset
;
91 #define MAP_AREA_NO_AREA 0x0001
92 struct map_areaHeader
{
98 #define MAP_HEIGHT_NO_HIGHT 0x0001
99 #define MAP_HEIGHT_AS_INT16 0x0002
100 #define MAP_HEIGHT_AS_INT8 0x0004
102 struct map_heightHeader
{
109 #define MAP_LIQUID_NO_TYPE 0x0001
110 #define MAP_LIQUID_NO_HIGHT 0x0002
111 struct map_liquidHeader
{
123 LIQUID_MAP_NO_WATER
= 0x00000000,
124 LIQUID_MAP_ABOVE_WATER
= 0x00000001,
125 LIQUID_MAP_WATER_WALK
= 0x00000002,
126 LIQUID_MAP_IN_WATER
= 0x00000004,
127 LIQUID_MAP_UNDER_WATER
= 0x00000008
130 #define MAP_LIQUID_TYPE_NO_WATER 0x00
131 #define MAP_LIQUID_TYPE_WATER 0x01
132 #define MAP_LIQUID_TYPE_OCEAN 0x02
133 #define MAP_LIQUID_TYPE_MAGMA 0x04
134 #define MAP_LIQUID_TYPE_SLIME 0x08
136 #define MAP_ALL_LIQUIDS (MAP_LIQUID_TYPE_WATER | MAP_LIQUID_TYPE_OCEAN | MAP_LIQUID_TYPE_MAGMA | MAP_LIQUID_TYPE_SLIME)
138 #define MAP_LIQUID_TYPE_DARK_WATER 0x10
139 #define MAP_LIQUID_TYPE_WMO_WATER 0x20
155 float m_gridIntHeightMultiplier
;
170 uint8 m_liquid_width
;
171 uint8 m_liquid_height
;
173 uint8
*m_liquid_type
;
176 bool loadAreaData(FILE *in
, uint32 offset
, uint32 size
);
177 bool loadHeihgtData(FILE *in
, uint32 offset
, uint32 size
);
178 bool loadLiquidData(FILE *in
, uint32 offset
, uint32 size
);
180 // Get height functions and pointers
181 typedef float (GridMap::*pGetHeightPtr
) (float x
, float y
) const;
182 pGetHeightPtr m_gridGetHeight
;
183 float getHeightFromFloat(float x
, float y
) const;
184 float getHeightFromUint16(float x
, float y
) const;
185 float getHeightFromUint8(float x
, float y
) const;
186 float getHeightFromFlat(float x
, float y
) const;
191 bool loadData(char *filaname
);
194 uint16
getArea(float x
, float y
);
195 inline float getHeight(float x
, float y
) {return (this->*m_gridGetHeight
)(x
, y
);}
196 float getLiquidLevel(float x
, float y
);
197 uint8
getTerrainType(float x
, float y
);
198 ZLiquidStatus
getLiquidStatus(float x
, float y
, float z
, uint8 ReqLiquidType
, LiquidData
*data
= 0);
203 CreatureMover() : x(0), y(0), z(0), ang(0) {}
204 CreatureMover(float _x
, float _y
, float _z
, float _ang
) : x(_x
), y(_y
), z(_z
), ang(_ang
) {}
209 // 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
210 #if defined( __GNUC__ )
216 struct InstanceTemplate
223 uint32 maxPlayersHeroic
;
224 uint32 reset_delay
; // FIX ME: now exist normal/heroic raids with possible different time of reset.
232 enum LevelRequirementVsMode
234 LEVELREQUIREMENT_HEROIC
= 70
237 #if defined( __GNUC__ )
243 typedef UNORDERED_MAP
<Creature
*, CreatureMover
> CreatureMoveList
;
245 #define MAX_HEIGHT 100000.0f // can be use for find ground height at surface
246 #define INVALID_HEIGHT -100000.0f // for check, must be equal to VMAP_INVALID_HEIGHT, real value for unknown height is VMAP_INVALID_HEIGHT_VALUE
247 #define MIN_UNLOAD_DELAY 1 // immediate unload
249 class MANGOS_DLL_SPEC Map
: public GridRefManager
<NGridType
>, public MaNGOS::ObjectLevelLockable
<Map
, ZThread::Mutex
>
251 friend class MapReference
;
253 Map(uint32 id
, time_t, uint32 InstanceId
, uint8 SpawnMode
);
256 // currently unused for normal maps
257 bool CanUnload(uint32 diff
)
259 if(!m_unloadTimer
) return false;
260 if(m_unloadTimer
<= diff
) return true;
261 m_unloadTimer
-= diff
;
265 virtual bool Add(Player
*);
266 virtual void Remove(Player
*, bool);
267 template<class T
> void Add(T
*);
268 template<class T
> void Remove(T
*, bool);
270 virtual void Update(const uint32
&);
272 void MessageBroadcast(Player
*, WorldPacket
*, bool to_self
);
273 void MessageBroadcast(WorldObject
*, WorldPacket
*);
274 void MessageDistBroadcast(Player
*, WorldPacket
*, float dist
, bool to_self
, bool own_team_only
= false);
275 void MessageDistBroadcast(WorldObject
*, WorldPacket
*, float dist
);
277 void PlayerRelocation(Player
*, float x
, float y
, float z
, float angl
);
278 void CreatureRelocation(Creature
*creature
, float x
, float y
, float, float);
280 template<class LOCK_TYPE
, class T
, class CONTAINER
> void Visit(const CellLock
<LOCK_TYPE
> &cell
, TypeContainerVisitor
<T
, CONTAINER
> &visitor
);
282 bool IsRemovalGrid(float x
, float y
) const
284 GridPair p
= MaNGOS::ComputeGridPair(x
, y
);
285 return( !getNGrid(p
.x_coord
, p
.y_coord
) || getNGrid(p
.x_coord
, p
.y_coord
)->GetGridState() == GRID_STATE_REMOVAL
);
288 bool GetUnloadLock(const GridPair
&p
) const { return getNGrid(p
.x_coord
, p
.y_coord
)->getUnloadLock(); }
289 void SetUnloadLock(const GridPair
&p
, bool on
) { getNGrid(p
.x_coord
, p
.y_coord
)->setUnloadExplicitLock(on
); }
290 void LoadGrid(const Cell
& cell
, bool no_unload
= false);
291 bool UnloadGrid(const uint32
&x
, const uint32
&y
, bool pForce
);
292 virtual void UnloadAll(bool pForce
);
294 void ResetGridExpiry(NGridType
&grid
, float factor
= 1) const
296 grid
.ResetTimeTracker((time_t)((float)i_gridExpiry
*factor
));
299 time_t GetGridExpiry(void) const { return i_gridExpiry
; }
300 uint32
GetId(void) const { return i_id
; }
302 static bool ExistMap(uint32 mapid
, int gx
, int gy
);
303 static bool ExistVMap(uint32 mapid
, int gx
, int gy
);
305 static void InitStateMachine();
306 static void DeleteStateMachine();
308 // some calls like isInWater should not use vmaps due to processor power
309 // can return INVALID_HEIGHT if under z+2 z coord not found height
310 float GetHeight(float x
, float y
, float z
, bool pCheckVMap
=true) const;
311 bool IsInWater(float x
, float y
, float z
) const; // does not use z pos. This is for future use
313 ZLiquidStatus
getLiquidStatus(float x
, float y
, float z
, uint8 ReqLiquidType
, LiquidData
*data
= 0) const;
315 uint16
GetAreaFlag(float x
, float y
, float z
) const;
316 uint8
GetTerrainType(float x
, float y
) const;
317 float GetWaterLevel(float x
, float y
) const;
318 bool IsUnderWater(float x
, float y
, float z
) const;
320 static uint32
GetAreaIdByAreaFlag(uint16 areaflag
,uint32 map_id
);
321 static uint32
GetZoneIdByAreaFlag(uint16 areaflag
,uint32 map_id
);
322 static void GetZoneAndAreaIdByAreaFlag(uint32
& zoneid
, uint32
& areaid
, uint16 areaflag
,uint32 map_id
);
324 uint32
GetAreaId(float x
, float y
, float z
) const
326 return GetAreaIdByAreaFlag(GetAreaFlag(x
,y
,z
),i_id
);
329 uint32
GetZoneId(float x
, float y
, float z
) const
331 return GetZoneIdByAreaFlag(GetAreaFlag(x
,y
,z
),i_id
);
334 void GetZoneAndAreaId(uint32
& zoneid
, uint32
& areaid
, float x
, float y
, float z
) const
336 GetZoneAndAreaIdByAreaFlag(zoneid
,areaid
,GetAreaFlag(x
,y
,z
),i_id
);
339 virtual void MoveAllCreaturesInMoveList();
340 virtual void RemoveAllObjectsInRemoveList();
342 bool CreatureRespawnRelocation(Creature
*c
); // used only in MoveAllCreaturesInMoveList and ObjectGridUnloader
344 // assert print helper
345 bool CheckGridIntegrity(Creature
* c
, bool moved
) const;
347 uint32
GetInstanceId() const { return i_InstanceId
; }
348 uint8
GetSpawnMode() const { return (i_spawnMode
); }
349 virtual bool CanEnter(Player
* /*player*/) { return true; }
350 const char* GetMapName() const;
352 bool Instanceable() const { return i_mapEntry
&& i_mapEntry
->Instanceable(); }
353 // NOTE: this duplicate of Instanceable(), but Instanceable() can be changed when BG also will be instanceable
354 bool IsDungeon() const { return i_mapEntry
&& i_mapEntry
->IsDungeon(); }
355 bool IsRaid() const { return i_mapEntry
&& i_mapEntry
->IsRaid(); }
356 bool IsHeroic() const { return i_spawnMode
== DIFFICULTY_HEROIC
; }
357 bool IsBattleGround() const { return i_mapEntry
&& i_mapEntry
->IsBattleGround(); }
358 bool IsBattleArena() const { return i_mapEntry
&& i_mapEntry
->IsBattleArena(); }
359 bool IsBattleGroundOrArena() const { return i_mapEntry
&& i_mapEntry
->IsBattleGroundOrArena(); }
360 bool GetEntrancePos(int32
&mapid
, float &x
, float &y
)
364 if(i_mapEntry
->entrance_map
< 0)
366 mapid
= i_mapEntry
->entrance_map
;
367 x
= i_mapEntry
->entrance_x
;
368 y
= i_mapEntry
->entrance_y
;
372 void AddObjectToRemoveList(WorldObject
*obj
);
373 void DoDelayedMovesAndRemoves();
375 virtual bool RemoveBones(uint64 guid
, float x
, float y
);
377 void UpdateObjectVisibility(WorldObject
* obj
, Cell cell
, CellPair cellpair
);
378 void UpdatePlayerVisibility(Player
* player
, Cell cell
, CellPair cellpair
);
379 void UpdateObjectsVisibilityFor(Player
* player
, Cell cell
, CellPair cellpair
);
381 void resetMarkedCells() { marked_cells
.reset(); }
382 bool isCellMarked(uint32 pCellId
) { return marked_cells
.test(pCellId
); }
383 void markCell(uint32 pCellId
) { marked_cells
.set(pCellId
); }
385 bool HavePlayers() const { return !m_mapRefManager
.isEmpty(); }
386 uint32
GetPlayersCountExceptGMs() const;
387 bool ActiveObjectsNearGrid(uint32 x
,uint32 y
) const;
389 void SendToPlayers(WorldPacket
const* data
) const;
391 typedef MapRefManager PlayerList
;
392 PlayerList
const& GetPlayers() const { return m_mapRefManager
; }
394 // must called with AddToWorld
396 void AddToActive(T
* obj
) { AddToActiveHelper(obj
); }
398 void AddToActive(Creature
* obj
);
400 // must called with RemoveFromWorld
402 void RemoveFromActive(T
* obj
) { RemoveFromActiveHelper(obj
); }
404 void RemoveFromActive(Creature
* obj
);
406 Creature
* GetCreature(uint64 guid
);
407 GameObject
* GetGameObject(uint64 guid
);
408 DynamicObject
* GetDynamicObject(uint64 guid
);
410 void LoadMapAndVMap(int gx
, int gy
);
411 void LoadVMap(int gx
, int gy
);
412 void LoadMap(int gx
,int gy
);
413 GridMap
*GetGrid(float x
, float y
);
415 void SetTimer(uint32 t
) { i_gridExpiry
= t
< MIN_GRID_DELAY
? MIN_GRID_DELAY
: t
; }
416 //uint64 CalculateGridMask(const uint32 &y) const;
418 void SendInitSelf( Player
* player
);
420 void SendInitTransports( Player
* player
);
421 void SendRemoveTransports( Player
* player
);
423 void PlayerRelocationNotify(Player
* player
, Cell cell
, CellPair cellpair
);
424 void CreatureRelocationNotify(Creature
*creature
, Cell newcell
, CellPair newval
);
426 bool CreatureCellRelocation(Creature
*creature
, Cell new_cell
);
428 void AddCreatureToMoveList(Creature
*c
, float x
, float y
, float z
, float ang
);
429 CreatureMoveList i_creaturesToMove
;
431 bool loaded(const GridPair
&) const;
432 void EnsureGridCreated(const GridPair
&);
433 bool EnsureGridLoaded(Cell
const&);
434 void EnsureGridLoadedAtEnter(Cell
const&, Player
* player
= NULL
);
436 void buildNGridLinkage(NGridType
* pNGridType
) { pNGridType
->link(this); }
438 template<class T
> void AddType(T
*obj
);
439 template<class T
> void RemoveType(T
*obj
, bool);
441 NGridType
* getNGrid(uint32 x
, uint32 y
) const
443 return i_grids
[x
][y
];
446 bool isGridObjectDataLoaded(uint32 x
, uint32 y
) const { return getNGrid(x
,y
)->isGridObjectDataLoaded(); }
447 void setGridObjectDataLoaded(bool pLoaded
, uint32 x
, uint32 y
) { getNGrid(x
,y
)->setGridObjectDataLoaded(pLoaded
); }
449 void setNGrid(NGridType
* grid
, uint32 x
, uint32 y
);
452 void SetUnloadReferenceLock(const GridPair
&p
, bool on
) { getNGrid(p
.x_coord
, p
.y_coord
)->setUnloadReferenceLock(on
); }
454 typedef MaNGOS::ObjectLevelLockable
<Map
, ZThread::Mutex
>::Lock Guard
;
456 MapEntry
const* i_mapEntry
;
460 uint32 m_unloadTimer
;
462 MapRefManager m_mapRefManager
;
463 MapRefManager::iterator m_mapRefIter
;
465 typedef std::set
<WorldObject
*> ActiveNonPlayers
;
466 ActiveNonPlayers m_activeNonPlayers
;
467 ActiveNonPlayers::iterator m_activeNonPlayersIter
;
469 typedef GridReadGuard ReadGuard
;
470 typedef GridWriteGuard WriteGuard
;
472 NGridType
* i_grids
[MAX_NUMBER_OF_GRIDS
][MAX_NUMBER_OF_GRIDS
];
473 GridMap
*GridMaps
[MAX_NUMBER_OF_GRIDS
][MAX_NUMBER_OF_GRIDS
];
474 std::bitset
<TOTAL_NUMBER_OF_CELLS_PER_MAP
*TOTAL_NUMBER_OF_CELLS_PER_MAP
> marked_cells
;
478 std::set
<WorldObject
*> i_objectsToRemove
;
480 // Type specific code for add/remove to/from grid
482 void AddToGrid(T
*, NGridType
*, Cell
const&);
485 void AddNotifier(T
*, Cell
const&, CellPair
const&);
488 void RemoveFromGrid(T
*, NGridType
*, Cell
const&);
491 void DeleteFromWorld(T
*);
494 void AddToActiveHelper(T
* obj
)
496 m_activeNonPlayers
.insert(obj
);
500 void RemoveFromActiveHelper(T
* obj
)
502 // Map::Update for active object in proccess
503 if(m_activeNonPlayersIter
!= m_activeNonPlayers
.end())
505 ActiveNonPlayers::iterator itr
= m_activeNonPlayers
.find(obj
);
506 if(itr
==m_activeNonPlayersIter
)
507 ++m_activeNonPlayersIter
;
508 m_activeNonPlayers
.erase(itr
);
511 m_activeNonPlayers
.erase(obj
);
515 enum InstanceResetMethod
518 INSTANCE_RESET_CHANGE_DIFFICULTY
,
519 INSTANCE_RESET_GLOBAL
,
520 INSTANCE_RESET_GROUP_DISBAND
,
521 INSTANCE_RESET_GROUP_JOIN
,
522 INSTANCE_RESET_RESPAWN_DELAY
525 class MANGOS_DLL_SPEC InstanceMap
: public Map
528 InstanceMap(uint32 id
, time_t, uint32 InstanceId
, uint8 SpawnMode
);
531 void Remove(Player
*, bool);
532 void Update(const uint32
&);
533 void CreateInstanceData(bool load
);
534 bool Reset(uint8 method
);
535 uint32
GetScriptId() { return i_script_id
; }
536 InstanceData
* GetInstanceData() { return i_data
; }
537 void PermBindAllPlayers(Player
*player
);
538 void UnloadAll(bool pForce
);
539 bool CanEnter(Player
* player
);
540 void SendResetWarnings(uint32 timeLeft
) const;
541 void SetResetSchedule(bool on
);
542 uint32
GetMaxPlayers() const;
544 bool m_resetAfterUnload
;
545 bool m_unloadWhenEmpty
;
546 InstanceData
* i_data
;
550 class MANGOS_DLL_SPEC BattleGroundMap
: public Map
553 BattleGroundMap(uint32 id
, time_t, uint32 InstanceId
);
557 void Remove(Player
*, bool);
558 bool CanEnter(Player
* player
);
560 void UnloadAll(bool pForce
);
565 Map::CalculateGridMask(const uint32 &y) const
573 template<class LOCK_TYPE
, class T
, class CONTAINER
>
575 Map::Visit(const CellLock
<LOCK_TYPE
> &cell
, TypeContainerVisitor
<T
, CONTAINER
> &visitor
)
577 const uint32 x
= cell
->GridX();
578 const uint32 y
= cell
->GridY();
579 const uint32 cell_x
= cell
->CellX();
580 const uint32 cell_y
= cell
->CellY();
582 if( !cell
->NoCreate() || loaded(GridPair(x
,y
)) )
584 EnsureGridLoaded(cell
);
585 //LOCK_TYPE guard(i_info[x][y]->i_lock);
586 getNGrid(x
, y
)->Visit(cell_x
, cell_y
, visitor
);