2 * Copyright (C) 2005-2008 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"
50 typedef ZThread::FairReadWriteLock GridRWLock
;
52 template<class MUTEX
, class LOCK_TYPE
>
55 RGuard(MUTEX
&l
) : i_lock(l
.getReadLock()) {}
56 MaNGOS::GeneralLock
<LOCK_TYPE
> i_lock
;
59 template<class MUTEX
, class LOCK_TYPE
>
62 WGuard(MUTEX
&l
) : i_lock(l
.getWriteLock()) {}
63 MaNGOS::GeneralLock
<LOCK_TYPE
> i_lock
;
66 typedef RGuard
<GridRWLock
, ZThread::Lockable
> GridReadGuard
;
67 typedef WGuard
<GridRWLock
, ZThread::Lockable
> GridWriteGuard
;
68 typedef MaNGOS::SingleThreaded
<GridRWLock
>::Lock NullGuard
;
72 uint16 area_flag
[16][16];
73 uint8 terrain_type
[16][16];
74 float liquid_level
[128][128];
75 float Z
[MAP_RESOLUTION
][MAP_RESOLUTION
];
80 CreatureMover() : x(0), y(0), z(0), ang(0) {}
81 CreatureMover(float _x
, float _y
, float _z
, float _ang
) : x(_x
), y(_y
), z(_z
), ang(_ang
) {}
86 // 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
87 #if defined( __GNUC__ )
93 struct InstanceTemplate
108 enum LevelRequirementVsMode
110 LEVELREQUIREMENT_HEROIC
= 70
113 #if defined( __GNUC__ )
119 typedef UNORDERED_MAP
<Creature
*, CreatureMover
> CreatureMoveList
;
121 #define MAX_HEIGHT 100000.0f // can be use for find ground height at surface
122 #define INVALID_HEIGHT -100000.0f // for check, must be equal to VMAP_INVALID_HEIGHT, real value for unknown height is VMAP_INVALID_HEIGHT_VALUE
123 #define MIN_UNLOAD_DELAY 1 // immediate unload
125 class MANGOS_DLL_SPEC Map
: public GridRefManager
<NGridType
>, public MaNGOS::ObjectLevelLockable
<Map
, ZThread::Mutex
>
128 Map(uint32 id
, time_t, uint32 InstanceId
, uint8 SpawnMode
);
131 // currently unused for normal maps
132 virtual bool CanUnload(const uint32
& diff
);
134 virtual bool Add(Player
*);
135 virtual void Remove(Player
*, bool);
136 template<class T
> void Add(T
*);
137 template<class T
> void Remove(T
*, bool);
139 virtual void Update(const uint32
&);
141 void MessageBroadcast(Player
*, WorldPacket
*, bool to_self
);
142 void MessageBroadcast(WorldObject
*, WorldPacket
*);
143 void MessageDistBroadcast(Player
*, WorldPacket
*, float dist
, bool to_self
, bool own_team_only
= false);
144 void MessageDistBroadcast(WorldObject
*, WorldPacket
*, float dist
);
146 void PlayerRelocation(Player
*, float x
, float y
, float z
, float angl
);
147 void CreatureRelocation(Creature
*creature
, float x
, float y
, float, float);
149 template<class LOCK_TYPE
, class T
, class CONTAINER
> void Visit(const CellLock
<LOCK_TYPE
> &cell
, TypeContainerVisitor
<T
, CONTAINER
> &visitor
);
151 inline bool IsRemovalGrid(float x
, float y
) const
153 GridPair p
= MaNGOS::ComputeGridPair(x
, y
);
154 return( !getNGrid(p
.x_coord
, p
.y_coord
) || getNGrid(p
.x_coord
, p
.y_coord
)->GetGridState() == GRID_STATE_REMOVAL
);
157 bool GetUnloadFlag(const GridPair
&p
) const { return getNGrid(p
.x_coord
, p
.y_coord
)->getUnloadFlag(); }
158 void SetUnloadFlag(const GridPair
&p
, bool unload
) { getNGrid(p
.x_coord
, p
.y_coord
)->setUnloadFlag(unload
); }
159 void LoadGrid(const Cell
& cell
, bool no_unload
= false);
160 bool UnloadGrid(const uint32
&x
, const uint32
&y
, bool pForce
);
161 virtual void UnloadAll(bool pForce
);
163 void ResetGridExpiry(NGridType
&grid
, float factor
= 1) const
165 grid
.ResetTimeTracker((time_t)((float)i_gridExpiry
*factor
));
168 time_t GetGridExpiry(void) const { return i_gridExpiry
; }
169 uint32
GetId(void) const { return i_id
; }
171 static bool ExistMap(uint32 mapid
, int x
, int y
);
172 static bool ExistVMap(uint32 mapid
, int x
, int y
);
173 void LoadMapAndVMap(uint32 mapid
, uint32 instanceid
, int x
, int y
);
175 static void InitStateMachine();
176 static void DeleteStateMachine();
178 // some calls like isInWater should not use vmaps due to processor power
179 // can return INVALID_HEIGHT if under z+2 z coord not found height
180 float GetHeight(float x
, float y
, float z
, bool pCheckVMap
=true) const;
181 bool IsInWater(float x
, float y
, float z
) const; // does not use z pos. This is for future use
183 uint16
GetAreaFlag(float x
, float y
) const;
184 uint8
GetTerrainType(float x
, float y
) const;
185 float GetWaterLevel(float x
, float y
) const;
186 bool IsUnderWater(float x
, float y
, float z
) const;
188 static uint32
GetAreaId(uint16 areaflag
,uint32 map_id
);
189 static uint32
GetZoneId(uint16 areaflag
,uint32 map_id
);
191 uint32
GetAreaId(float x
, float y
) const
193 return GetAreaId(GetAreaFlag(x
,y
),i_id
);
196 uint32
GetZoneId(float x
, float y
) const
198 return GetZoneId(GetAreaFlag(x
,y
),i_id
);
201 virtual void MoveAllCreaturesInMoveList();
202 virtual void RemoveAllObjectsInRemoveList();
204 bool CreatureRespawnRelocation(Creature
*c
); // used only in MoveAllCreaturesInMoveList and ObjectGridUnloader
206 // assert print helper
207 bool CheckGridIntegrity(Creature
* c
, bool moved
) const;
209 uint32
GetInstanceId() { return i_InstanceId
; }
210 uint8
GetSpawnMode() { return (i_spawnMode
); }
211 virtual bool CanEnter(Player
* /*player*/) { return true; }
212 const char* GetMapName() const;
214 bool Instanceable() const { return i_mapEntry
&& i_mapEntry
->Instanceable(); }
215 // NOTE: this duplicate of Instanceable(), but Instanceable() can be changed when BG also will be instanceable
216 bool IsDungeon() const { return i_mapEntry
&& i_mapEntry
->IsDungeon(); }
217 bool IsRaid() const { return i_mapEntry
&& i_mapEntry
->IsRaid(); }
218 bool IsHeroic() const { return i_spawnMode
== DIFFICULTY_HEROIC
; }
219 bool IsBattleGround() const { return i_mapEntry
&& i_mapEntry
->IsBattleGround(); }
220 bool IsBattleArena() const { return i_mapEntry
&& i_mapEntry
->IsBattleArena(); }
221 bool IsBattleGroundOrArena() const { return i_mapEntry
&& i_mapEntry
->IsBattleGroundOrArena(); }
222 bool GetEntrancePos(int32
&mapid
, float &x
, float &y
)
226 if(i_mapEntry
->entrance_map
< 0)
228 mapid
= i_mapEntry
->entrance_map
;
229 x
= i_mapEntry
->entrance_x
;
230 y
= i_mapEntry
->entrance_y
;
234 void AddObjectToRemoveList(WorldObject
*obj
);
235 void DoDelayedMovesAndRemoves();
237 virtual bool RemoveBones(uint64 guid
, float x
, float y
);
239 void UpdateObjectVisibility(WorldObject
* obj
, Cell cell
, CellPair cellpair
);
240 void UpdatePlayerVisibility(Player
* player
, Cell cell
, CellPair cellpair
);
241 void UpdateObjectsVisibilityFor(Player
* player
, Cell cell
, CellPair cellpair
);
243 void resetMarkedCells() { marked_cells
.reset(); }
244 bool isCellMarked(uint32 x
, uint32 y
) { return marked_cells
.test(y
* TOTAL_NUMBER_OF_CELLS_PER_MAP
+ x
); }
245 void markCell(uint32 x
, uint32 y
) { marked_cells
.set(y
* TOTAL_NUMBER_OF_CELLS_PER_MAP
+ x
); }
247 void LoadVMap(int pX
, int pY
);
248 void LoadMap(uint32 mapid
, uint32 instanceid
, int x
,int y
);
250 void SetTimer(uint32 t
) { i_gridExpiry
= t
< MIN_GRID_DELAY
? MIN_GRID_DELAY
: t
; }
251 //uint64 CalculateGridMask(const uint32 &y) const;
253 void SendInitSelf( Player
* player
);
255 void SendInitTransports( Player
* player
);
256 void SendRemoveTransports( Player
* player
);
258 void PlayerRelocationNotify(Player
* player
, Cell cell
, CellPair cellpair
);
259 void CreatureRelocationNotify(Creature
*creature
, Cell newcell
, CellPair newval
);
261 bool CreatureCellRelocation(Creature
*creature
, Cell new_cell
);
263 void AddCreatureToMoveList(Creature
*c
, float x
, float y
, float z
, float ang
);
264 CreatureMoveList i_creaturesToMove
;
266 bool loaded(const GridPair
&) const;
267 void EnsureGridLoadedForPlayer(const Cell
&, Player
*, bool add_player
);
268 void EnsureGridCreated(const GridPair
&);
270 void buildNGridLinkage(NGridType
* pNGridType
) { pNGridType
->link(this); }
272 template<class T
> void AddType(T
*obj
);
273 template<class T
> void RemoveType(T
*obj
, bool);
275 NGridType
* getNGrid(uint32 x
, uint32 y
) const
277 return i_grids
[x
][y
];
280 bool isGridObjectDataLoaded(uint32 x
, uint32 y
) const { return getNGrid(x
,y
)->isGridObjectDataLoaded(); }
281 void setGridObjectDataLoaded(bool pLoaded
, uint32 x
, uint32 y
) { getNGrid(x
,y
)->setGridObjectDataLoaded(pLoaded
); }
283 inline void setNGrid(NGridType
* grid
, uint32 x
, uint32 y
);
286 typedef MaNGOS::ObjectLevelLockable
<Map
, ZThread::Mutex
>::Lock Guard
;
288 MapEntry
const* i_mapEntry
;
292 uint32 m_unloadTimer
;
295 typedef GridReadGuard ReadGuard
;
296 typedef GridWriteGuard WriteGuard
;
298 NGridType
* i_grids
[MAX_NUMBER_OF_GRIDS
][MAX_NUMBER_OF_GRIDS
];
299 GridMap
*GridMaps
[MAX_NUMBER_OF_GRIDS
][MAX_NUMBER_OF_GRIDS
];
300 std::bitset
<TOTAL_NUMBER_OF_CELLS_PER_MAP
*TOTAL_NUMBER_OF_CELLS_PER_MAP
> marked_cells
;
304 std::set
<WorldObject
*> i_objectsToRemove
;
306 // Type specific code for add/remove to/from grid
308 void AddToGrid(T
*, NGridType
*, Cell
const&);
311 void AddNotifier(T
*, Cell
const&, CellPair
const&);
314 void RemoveFromGrid(T
*, NGridType
*, Cell
const&);
317 void DeleteFromWorld(T
*);
320 enum InstanceResetMethod
323 INSTANCE_RESET_CHANGE_DIFFICULTY
,
324 INSTANCE_RESET_GLOBAL
,
325 INSTANCE_RESET_GROUP_DISBAND
,
326 INSTANCE_RESET_GROUP_JOIN
,
327 INSTANCE_RESET_RESPAWN_DELAY
330 class MANGOS_DLL_SPEC InstanceMap
: public Map
333 typedef std::list
<Player
*> PlayerList
; // online players only
335 InstanceMap(uint32 id
, time_t, uint32 InstanceId
, uint8 SpawnMode
);
338 void Remove(Player
*, bool);
339 void Update(const uint32
&);
340 void CreateInstanceData(bool load
);
341 bool Reset(uint8 method
);
342 std::string
GetScript() { return i_script
; }
343 InstanceData
* GetInstanceData() { return i_data
; }
344 void PermBindAllPlayers(Player
*player
);
345 PlayerList
const& GetPlayers() const { return i_Players
;}
346 void SendToPlayers(WorldPacket
const* data
) const;
347 time_t GetResetTime();
348 void UnloadAll(bool pForce
);
349 bool CanEnter(Player
* player
);
350 uint32
GetPlayersCountExceptGMs() const;
351 uint32
HavePlayers() const { return !i_Players
.empty(); }
352 void SendResetWarnings(uint32 timeLeft
);
353 void SetResetSchedule(bool on
);
355 bool m_resetAfterUnload
;
356 bool m_unloadWhenEmpty
;
357 InstanceData
* i_data
;
358 std::string i_script
;
359 // only online players that are inside the instance currently
360 // TODO ? - use the grid instead to access the players
361 PlayerList i_Players
;
364 class MANGOS_DLL_SPEC BattleGroundMap
: public Map
367 typedef std::list
<Player
*> PlayerList
; // online players only
369 BattleGroundMap(uint32 id
, time_t, uint32 InstanceId
);
373 void Remove(Player
*, bool);
374 bool CanEnter(Player
* player
);
376 void UnloadAll(bool pForce
);
378 PlayerList i_Players
;
383 Map::CalculateGridMask(const uint32 &y) const
391 template<class LOCK_TYPE
, class T
, class CONTAINER
>
393 Map::Visit(const CellLock
<LOCK_TYPE
> &cell
, TypeContainerVisitor
<T
, CONTAINER
> &visitor
)
395 const uint32 x
= cell
->GridX();
396 const uint32 y
= cell
->GridY();
397 const uint32 cell_x
= cell
->CellX();
398 const uint32 cell_y
= cell
->CellY();
400 if( !cell
->NoCreate() || loaded(GridPair(x
,y
)) )
402 EnsureGridLoadedForPlayer(cell
, NULL
, false);
403 //LOCK_TYPE guard(i_info[x][y]->i_lock);
404 getNGrid(x
, y
)->Visit(cell_x
, cell_y
, visitor
);