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 "Database/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
;
73 uint16 area_flag
[16][16];
74 uint8 terrain_type
[16][16];
75 float liquid_level
[128][128];
76 float v9
[MAP_RESOLUTION
+ 1][MAP_RESOLUTION
+ 1];
77 float v8
[MAP_RESOLUTION
][MAP_RESOLUTION
];
82 CreatureMover() : x(0), y(0), z(0), ang(0) {}
83 CreatureMover(float _x
, float _y
, float _z
, float _ang
) : x(_x
), y(_y
), z(_z
), ang(_ang
) {}
88 // 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
89 #if defined( __GNUC__ )
95 struct InstanceTemplate
102 uint32 maxPlayersHeroic
;
103 uint32 reset_delay
; // FIX ME: now exist normal/heroic raids with possible different time of reset.
111 enum LevelRequirementVsMode
113 LEVELREQUIREMENT_HEROIC
= 70
116 #if defined( __GNUC__ )
122 typedef UNORDERED_MAP
<Creature
*, CreatureMover
> CreatureMoveList
;
124 #define MAX_HEIGHT 100000.0f // can be use for find ground height at surface
125 #define INVALID_HEIGHT -100000.0f // for check, must be equal to VMAP_INVALID_HEIGHT, real value for unknown height is VMAP_INVALID_HEIGHT_VALUE
126 #define MIN_UNLOAD_DELAY 1 // immediate unload
128 class MANGOS_DLL_SPEC Map
: public GridRefManager
<NGridType
>, public MaNGOS::ObjectLevelLockable
<Map
, ZThread::Mutex
>
130 friend class MapReference
;
132 Map(uint32 id
, time_t, uint32 InstanceId
, uint8 SpawnMode
);
135 // currently unused for normal maps
136 bool CanUnload(uint32 diff
)
138 if(!m_unloadTimer
) return false;
139 if(m_unloadTimer
<= diff
) return true;
140 m_unloadTimer
-= diff
;
144 virtual bool Add(Player
*);
145 virtual void Remove(Player
*, bool);
146 template<class T
> void Add(T
*);
147 template<class T
> void Remove(T
*, bool);
149 virtual void Update(const uint32
&);
151 void MessageBroadcast(Player
*, WorldPacket
*, bool to_self
);
152 void MessageBroadcast(WorldObject
*, WorldPacket
*);
153 void MessageDistBroadcast(Player
*, WorldPacket
*, float dist
, bool to_self
, bool own_team_only
= false);
154 void MessageDistBroadcast(WorldObject
*, WorldPacket
*, float dist
);
156 void PlayerRelocation(Player
*, float x
, float y
, float z
, float angl
);
157 void CreatureRelocation(Creature
*creature
, float x
, float y
, float, float);
159 template<class LOCK_TYPE
, class T
, class CONTAINER
> void Visit(const CellLock
<LOCK_TYPE
> &cell
, TypeContainerVisitor
<T
, CONTAINER
> &visitor
);
161 bool IsRemovalGrid(float x
, float y
) const
163 GridPair p
= MaNGOS::ComputeGridPair(x
, y
);
164 return( !getNGrid(p
.x_coord
, p
.y_coord
) || getNGrid(p
.x_coord
, p
.y_coord
)->GetGridState() == GRID_STATE_REMOVAL
);
167 bool GetUnloadFlag(const GridPair
&p
) const { return getNGrid(p
.x_coord
, p
.y_coord
)->getUnloadFlag(); }
168 void SetUnloadFlag(const GridPair
&p
, bool unload
) { getNGrid(p
.x_coord
, p
.y_coord
)->setUnloadFlag(unload
); }
169 void LoadGrid(const Cell
& cell
, bool no_unload
= false);
170 bool UnloadGrid(const uint32
&x
, const uint32
&y
, bool pForce
);
171 virtual void UnloadAll(bool pForce
);
173 void ResetGridExpiry(NGridType
&grid
, float factor
= 1) const
175 grid
.ResetTimeTracker((time_t)((float)i_gridExpiry
*factor
));
178 time_t GetGridExpiry(void) const { return i_gridExpiry
; }
179 uint32
GetId(void) const { return i_id
; }
181 static bool ExistMap(uint32 mapid
, int x
, int y
);
182 static bool ExistVMap(uint32 mapid
, int x
, int y
);
183 void LoadMapAndVMap(uint32 mapid
, uint32 instanceid
, int x
, int y
);
185 static void InitStateMachine();
186 static void DeleteStateMachine();
188 // some calls like isInWater should not use vmaps due to processor power
189 // can return INVALID_HEIGHT if under z+2 z coord not found height
190 float GetHeight(float x
, float y
, float z
, bool pCheckVMap
=true) const;
191 bool IsInWater(float x
, float y
, float z
) const; // does not use z pos. This is for future use
193 uint16
GetAreaFlag(float x
, float y
, float z
) const;
194 uint8
GetTerrainType(float x
, float y
) const;
195 float GetWaterLevel(float x
, float y
) const;
196 bool IsUnderWater(float x
, float y
, float z
) const;
198 static uint32
GetAreaId(uint16 areaflag
,uint32 map_id
);
199 static uint32
GetZoneId(uint16 areaflag
,uint32 map_id
);
201 uint32
GetAreaId(float x
, float y
, float z
) const
203 return GetAreaId(GetAreaFlag(x
,y
,z
),i_id
);
206 uint32
GetZoneId(float x
, float y
, float z
) const
208 return GetZoneId(GetAreaFlag(x
,y
,z
),i_id
);
211 virtual void MoveAllCreaturesInMoveList();
212 virtual void RemoveAllObjectsInRemoveList();
214 bool CreatureRespawnRelocation(Creature
*c
); // used only in MoveAllCreaturesInMoveList and ObjectGridUnloader
216 // assert print helper
217 bool CheckGridIntegrity(Creature
* c
, bool moved
) const;
219 uint32
GetInstanceId() { return i_InstanceId
; }
220 uint8
GetSpawnMode() { return (i_spawnMode
); }
221 virtual bool CanEnter(Player
* /*player*/) { return true; }
222 const char* GetMapName() const;
224 bool Instanceable() const { return i_mapEntry
&& i_mapEntry
->Instanceable(); }
225 // NOTE: this duplicate of Instanceable(), but Instanceable() can be changed when BG also will be instanceable
226 bool IsDungeon() const { return i_mapEntry
&& i_mapEntry
->IsDungeon(); }
227 bool IsRaid() const { return i_mapEntry
&& i_mapEntry
->IsRaid(); }
228 bool IsHeroic() const { return i_spawnMode
== DIFFICULTY_HEROIC
; }
229 bool IsBattleGround() const { return i_mapEntry
&& i_mapEntry
->IsBattleGround(); }
230 bool IsBattleArena() const { return i_mapEntry
&& i_mapEntry
->IsBattleArena(); }
231 bool IsBattleGroundOrArena() const { return i_mapEntry
&& i_mapEntry
->IsBattleGroundOrArena(); }
232 bool GetEntrancePos(int32
&mapid
, float &x
, float &y
)
236 if(i_mapEntry
->entrance_map
< 0)
238 mapid
= i_mapEntry
->entrance_map
;
239 x
= i_mapEntry
->entrance_x
;
240 y
= i_mapEntry
->entrance_y
;
244 void AddObjectToRemoveList(WorldObject
*obj
);
245 void DoDelayedMovesAndRemoves();
247 virtual bool RemoveBones(uint64 guid
, float x
, float y
);
249 void UpdateObjectVisibility(WorldObject
* obj
, Cell cell
, CellPair cellpair
);
250 void UpdatePlayerVisibility(Player
* player
, Cell cell
, CellPair cellpair
);
251 void UpdateObjectsVisibilityFor(Player
* player
, Cell cell
, CellPair cellpair
);
253 void resetMarkedCells() { marked_cells
.reset(); }
254 bool isCellMarked(uint32 pCellId
) { return marked_cells
.test(pCellId
); }
255 void markCell(uint32 pCellId
) { marked_cells
.set(pCellId
); }
257 bool HavePlayers() const { return !m_mapRefManager
.isEmpty(); }
258 uint32
GetPlayersCountExceptGMs() const;
259 bool PlayersNearGrid(uint32 x
,uint32 y
) const;
261 void SendToPlayers(WorldPacket
const* data
) const;
263 typedef MapRefManager PlayerList
;
264 PlayerList
const& GetPlayers() const { return m_mapRefManager
; }
266 void LoadVMap(int pX
, int pY
);
267 void LoadMap(uint32 mapid
, uint32 instanceid
, int x
,int y
);
269 void SetTimer(uint32 t
) { i_gridExpiry
= t
< MIN_GRID_DELAY
? MIN_GRID_DELAY
: t
; }
270 //uint64 CalculateGridMask(const uint32 &y) const;
272 void SendInitSelf( Player
* player
);
274 void SendInitTransports( Player
* player
);
275 void SendRemoveTransports( Player
* player
);
277 void PlayerRelocationNotify(Player
* player
, Cell cell
, CellPair cellpair
);
278 void CreatureRelocationNotify(Creature
*creature
, Cell newcell
, CellPair newval
);
280 bool CreatureCellRelocation(Creature
*creature
, Cell new_cell
);
282 void AddCreatureToMoveList(Creature
*c
, float x
, float y
, float z
, float ang
);
283 CreatureMoveList i_creaturesToMove
;
285 bool loaded(const GridPair
&) const;
286 void EnsureGridLoadedForPlayer(const Cell
&, Player
*, bool add_player
);
287 void EnsureGridCreated(const GridPair
&);
289 void buildNGridLinkage(NGridType
* pNGridType
) { pNGridType
->link(this); }
291 template<class T
> void AddType(T
*obj
);
292 template<class T
> void RemoveType(T
*obj
, bool);
294 NGridType
* getNGrid(uint32 x
, uint32 y
) const
296 return i_grids
[x
][y
];
299 bool isGridObjectDataLoaded(uint32 x
, uint32 y
) const { return getNGrid(x
,y
)->isGridObjectDataLoaded(); }
300 void setGridObjectDataLoaded(bool pLoaded
, uint32 x
, uint32 y
) { getNGrid(x
,y
)->setGridObjectDataLoaded(pLoaded
); }
302 void setNGrid(NGridType
* grid
, uint32 x
, uint32 y
);
305 typedef MaNGOS::ObjectLevelLockable
<Map
, ZThread::Mutex
>::Lock Guard
;
307 MapEntry
const* i_mapEntry
;
311 uint32 m_unloadTimer
;
313 MapRefManager m_mapRefManager
;
314 MapRefManager::iterator m_mapRefIter
;
316 typedef GridReadGuard ReadGuard
;
317 typedef GridWriteGuard WriteGuard
;
319 NGridType
* i_grids
[MAX_NUMBER_OF_GRIDS
][MAX_NUMBER_OF_GRIDS
];
320 GridMap
*GridMaps
[MAX_NUMBER_OF_GRIDS
][MAX_NUMBER_OF_GRIDS
];
321 std::bitset
<TOTAL_NUMBER_OF_CELLS_PER_MAP
*TOTAL_NUMBER_OF_CELLS_PER_MAP
> marked_cells
;
325 std::set
<WorldObject
*> i_objectsToRemove
;
327 // Type specific code for add/remove to/from grid
329 void AddToGrid(T
*, NGridType
*, Cell
const&);
332 void AddNotifier(T
*, Cell
const&, CellPair
const&);
335 void RemoveFromGrid(T
*, NGridType
*, Cell
const&);
338 void DeleteFromWorld(T
*);
341 enum InstanceResetMethod
344 INSTANCE_RESET_CHANGE_DIFFICULTY
,
345 INSTANCE_RESET_GLOBAL
,
346 INSTANCE_RESET_GROUP_DISBAND
,
347 INSTANCE_RESET_GROUP_JOIN
,
348 INSTANCE_RESET_RESPAWN_DELAY
351 class MANGOS_DLL_SPEC InstanceMap
: public Map
354 InstanceMap(uint32 id
, time_t, uint32 InstanceId
, uint8 SpawnMode
);
357 void Remove(Player
*, bool);
358 void Update(const uint32
&);
359 void CreateInstanceData(bool load
);
360 bool Reset(uint8 method
);
361 uint32
GetScriptId() { return i_script_id
; }
362 InstanceData
* GetInstanceData() { return i_data
; }
363 void PermBindAllPlayers(Player
*player
);
364 time_t GetResetTime();
365 void UnloadAll(bool pForce
);
366 bool CanEnter(Player
* player
);
367 void SendResetWarnings(uint32 timeLeft
) const;
368 void SetResetSchedule(bool on
);
369 uint32
GetMaxPlayers() const;
371 bool m_resetAfterUnload
;
372 bool m_unloadWhenEmpty
;
373 InstanceData
* i_data
;
377 class MANGOS_DLL_SPEC BattleGroundMap
: public Map
380 BattleGroundMap(uint32 id
, time_t, uint32 InstanceId
);
384 void Remove(Player
*, bool);
385 bool CanEnter(Player
* player
);
387 void UnloadAll(bool pForce
);
392 Map::CalculateGridMask(const uint32 &y) const
400 template<class LOCK_TYPE
, class T
, class CONTAINER
>
402 Map::Visit(const CellLock
<LOCK_TYPE
> &cell
, TypeContainerVisitor
<T
, CONTAINER
> &visitor
)
404 const uint32 x
= cell
->GridX();
405 const uint32 y
= cell
->GridY();
406 const uint32 cell_x
= cell
->CellX();
407 const uint32 cell_y
= cell
->CellY();
409 if( !cell
->NoCreate() || loaded(GridPair(x
,y
)) )
411 EnsureGridLoadedForPlayer(cell
, NULL
, false);
412 //LOCK_TYPE guard(i_info[x][y]->i_lock);
413 getNGrid(x
, y
)->Visit(cell_x
, cell_y
, visitor
);