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"
50 //******************************************
51 // Map file format defines
52 //******************************************
60 uint32 heightMapOffset
;
62 uint32 liquidMapOffset
;
66 #define MAP_AREA_NO_AREA 0x0001
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
87 #define MAP_LIQUID_NO_TYPE 0x0001
88 #define MAP_LIQUID_NO_HEIGHT 0x0002
90 struct map_liquidHeader
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
137 float m_gridIntHeightMultiplier
;
152 uint8 m_liquid_width
;
153 uint8 m_liquid_height
;
155 uint8
*m_liquid_type
;
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;
173 bool loadData(char *filaname
);
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__ )
190 struct InstanceTemplate
203 enum LevelRequirementVsMode
205 LEVELREQUIREMENT_HEROIC
= 70
208 #if defined( __GNUC__ )
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
;
222 Map(uint32 id
, time_t, uint32 InstanceId
, uint8 SpawnMode
, Map
* _parent
= NULL
);
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
;
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
);
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
)
354 if(i_mapEntry
->entrance_map
< 0)
356 mapid
= i_mapEntry
->entrance_map
;
357 x
= i_mapEntry
->entrance_x
;
358 y
= i_mapEntry
->entrance_y
;
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
389 void AddToActive(T
* obj
) { AddToActiveHelper(obj
); }
391 void AddToActive(Creature
* obj
);
393 // must called with RemoveFromWorld
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
);
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
;
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
;
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
;
486 //used for fast base_map (e.g. MapInstanced class object) search for
487 //InstanceMaps and BattleGroundMaps...
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
504 void AddToGrid(T
*, NGridType
*, Cell
const&);
507 void AddNotifier(T
*, Cell
const&, CellPair
const&);
510 void RemoveFromGrid(T
*, NGridType
*, Cell
const&);
513 void DeleteFromWorld(T
*);
516 void AddToActiveHelper(T
* obj
)
518 m_activeNonPlayers
.insert(obj
);
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
);
533 m_activeNonPlayers
.erase(obj
);
537 enum InstanceResetMethod
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
550 InstanceMap(uint32 id
, time_t, uint32 InstanceId
, uint8 SpawnMode
, Map
* _parent
);
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();
567 bool m_resetAfterUnload
;
568 bool m_unloadWhenEmpty
;
569 InstanceData
* i_data
;
573 class MANGOS_DLL_SPEC BattleGroundMap
: public Map
576 BattleGroundMap(uint32 id
, time_t, uint32 InstanceId
, Map
* _parent
, uint8 spawnMode
);
580 void Remove(Player
*, bool);
581 bool CanEnter(Player
* player
);
583 void UnloadAll(bool pForce
);
585 virtual void InitVisibilityDistance();
586 BattleGround
* GetBG() { return m_bg
; }
587 void SetBG(BattleGround
* bg
) { m_bg
= bg
; }
594 Map::CalculateGridMask(const uint32 &y) const
602 template<class T
, class CONTAINER
>
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
);