root/trunk/src/game/Map.h @ 248

Revision 206, 13.9 kB (checked in by yumileroy, 17 years ago)

[svn] * Switch from hashmap to unordered map. - cleanup source - mangos. Help - Aokromes

Original author: KingPin?
Date: 2008-11-10 06:53:00-06:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef TRINITY_MAP_H
22#define TRINITY_MAP_H
23
24#include "Platform/Define.h"
25#include "Policies/ThreadingModel.h"
26#include "zthread/Lockable.h"
27#include "zthread/Mutex.h"
28#include "zthread/FairReadWriteLock.h"
29#include "Database/DBCStructure.h"
30#include "GridDefines.h"
31#include "Cell.h"
32#include "Object.h"
33#include "Timer.h"
34#include "SharedDefines.h"
35#include "GameSystem/GridRefManager.h"
36
37#include <bitset>
38#include <list>
39
40class Unit;
41class WorldPacket;
42class InstanceData;
43class Group;
44class InstanceSave;
45
46namespace ZThread
47{
48    class Lockable;
49    class ReadWriteLock;
50}
51
52typedef ZThread::FairReadWriteLock GridRWLock;
53
54template<class MUTEX, class LOCK_TYPE>
55struct RGuard
56{
57    RGuard(MUTEX &l) : i_lock(l.getReadLock()) {}
58    Trinity::GeneralLock<LOCK_TYPE> i_lock;
59};
60
61template<class MUTEX, class LOCK_TYPE>
62struct WGuard
63{
64    WGuard(MUTEX &l) : i_lock(l.getWriteLock()) {}
65    Trinity::GeneralLock<LOCK_TYPE> i_lock;
66};
67
68typedef RGuard<GridRWLock, ZThread::Lockable> GridReadGuard;
69typedef WGuard<GridRWLock, ZThread::Lockable> GridWriteGuard;
70typedef Trinity::SingleThreaded<GridRWLock>::Lock NullGuard;
71
72typedef struct
73{
74    uint16 area_flag[16][16];
75    uint8 terrain_type[16][16];
76    float liquid_level[128][128];
77    float Z[MAP_RESOLUTION][MAP_RESOLUTION];
78}GridMap;
79
80struct CreatureMover
81{
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) {}
84
85    float x, y, z, ang;
86};
87
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__ )
90#pragma pack(1)
91#else
92#pragma pack(push,1)
93#endif
94
95struct InstanceTemplate
96{
97    uint32 map;
98    uint32 parent;
99    uint32 levelMin;
100    uint32 levelMax;
101    uint32 maxPlayers;
102    uint32 reset_delay;
103    float startLocX;
104    float startLocY;
105    float startLocZ;
106    float startLocO;
107    char const* script;
108};
109
110enum LevelRequirementVsMode
111{
112    LEVELREQUIREMENT_HEROIC = 70
113};
114
115#if defined( __GNUC__ )
116#pragma pack()
117#else
118#pragma pack(pop)
119#endif
120
121typedef UNORDERED_MAP<Creature*, CreatureMover> CreatureMoveList;
122
123#define MAX_HEIGHT            100000.0f                     // can be use for find ground height at surface
124#define INVALID_HEIGHT       -100000.0f                     // for check, must be equal to VMAP_INVALID_HEIGHT, real value for unknown height is VMAP_INVALID_HEIGHT_VALUE
125#define MIN_UNLOAD_DELAY      1                             // immediate unload
126
127class TRINITY_DLL_SPEC Map : public GridRefManager<NGridType>, public Trinity::ObjectLevelLockable<Map, ZThread::Mutex>
128{
129    public:
130        Map(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode);
131        virtual ~Map();
132
133        // currently unused for normal maps
134        virtual bool CanUnload(const uint32& diff);
135
136        virtual bool Add(Player *);
137        virtual void Remove(Player *, bool);
138        template<class T> void Add(T *);
139        template<class T> void Remove(T *, bool);
140
141        virtual void Update(const uint32&);
142
143        void MessageBroadcast(Player *, WorldPacket *, bool to_self, bool to_possessor);
144        void MessageBroadcast(WorldObject *, WorldPacket *, bool to_possessor);
145        void MessageDistBroadcast(Player *, WorldPacket *, float dist, bool to_self, bool to_possessor, bool own_team_only = false);
146        void MessageDistBroadcast(WorldObject *, WorldPacket *, float dist, bool to_possessor);
147
148        void PlayerRelocation(Player *, float x, float y, float z, float angl);
149        void CreatureRelocation(Creature *creature, float x, float y, float, float);
150
151        template<class LOCK_TYPE, class T, class CONTAINER> void Visit(const CellLock<LOCK_TYPE> &cell, TypeContainerVisitor<T, CONTAINER> &visitor);
152
153        inline bool IsRemovalGrid(float x, float y) const
154        {
155            GridPair p = Trinity::ComputeGridPair(x, y);
156            return( !getNGrid(p.x_coord, p.y_coord) || getNGrid(p.x_coord, p.y_coord)->GetGridState() == GRID_STATE_REMOVAL );
157        }
158
159        bool GetUnloadFlag(const GridPair &p) const { return getNGrid(p.x_coord, p.y_coord)->getUnloadFlag(); }
160        void SetUnloadFlag(const GridPair &p, bool unload) { getNGrid(p.x_coord, p.y_coord)->setUnloadFlag(unload); }
161        void LoadGrid(const Cell& cell, bool no_unload = false);
162        bool UnloadGrid(const uint32 &x, const uint32 &y, bool pForce);
163        virtual void UnloadAll(bool pForce);
164
165        void ResetGridExpiry(NGridType &grid, float factor = 1) const
166        {
167            grid.ResetTimeTracker((time_t)((float)i_gridExpiry*factor));
168        }
169
170        time_t GetGridExpiry(void) const { return i_gridExpiry; }
171        uint32 GetId(void) const { return i_id; }
172
173        static bool ExistMap(uint32 mapid, int x, int y);
174        static bool ExistVMap(uint32 mapid, int x, int y);
175        void LoadMapAndVMap(uint32 mapid, uint32 instanceid, int x, int y);
176
177        static void InitStateMachine();
178        static void DeleteStateMachine();
179
180        // some calls like isInWater should not use vmaps due to processor power
181        // can return INVALID_HEIGHT if under z+2 z coord not found height
182        float GetHeight(float x, float y, float z, bool pCheckVMap=true) const;
183        bool IsInWater(float x, float y, float z) const;    // does not use z pos. This is for future use
184
185        uint16 GetAreaFlag(float x, float y ) const;
186        uint8 GetTerrainType(float x, float y ) const;
187        float GetWaterLevel(float x, float y ) const;
188        bool IsUnderWater(float x, float y, float z) const;
189
190        static uint32 GetAreaId(uint16 areaflag,uint32 map_id);
191        static uint32 GetZoneId(uint16 areaflag,uint32 map_id);
192
193        uint32 GetAreaId(float x, float y) const
194        {
195            return GetAreaId(GetAreaFlag(x,y),i_id);
196        }
197
198        uint32 GetZoneId(float x, float y) const
199        {
200            return GetZoneId(GetAreaFlag(x,y),i_id);
201        }
202
203        virtual void MoveAllCreaturesInMoveList();
204        virtual void RemoveAllObjectsInRemoveList();
205
206        bool CreatureRespawnRelocation(Creature *c);        // used only in MoveAllCreaturesInMoveList and ObjectGridUnloader
207
208        // assert print helper
209        bool CheckGridIntegrity(Creature* c, bool moved) const;
210
211        uint32 GetInstanceId() { return i_InstanceId; }
212        uint8 GetSpawnMode() { return (i_spawnMode); }
213        virtual bool CanEnter(Player* /*player*/) { return true; }
214        const char* GetMapName() const;
215
216        bool Instanceable() const { return i_mapEntry && i_mapEntry->Instanceable(); }
217        // NOTE: this duplicate of Instanceable(), but Instanceable() can be changed when BG also will be instanceable
218        bool IsDungeon() const { return i_mapEntry && i_mapEntry->IsDungeon(); }
219        bool IsRaid() const { return i_mapEntry && i_mapEntry->IsRaid(); }
220        bool IsHeroic() const { return i_spawnMode == DIFFICULTY_HEROIC; }
221        bool IsBattleGround() const { return i_mapEntry && i_mapEntry->IsBattleGround(); }
222        bool IsBattleArena() const { return i_mapEntry && i_mapEntry->IsBattleArena(); }
223        bool IsBattleGroundOrArena() const { return i_mapEntry && i_mapEntry->IsBattleGroundOrArena(); }
224
225        void AddObjectToRemoveList(WorldObject *obj);
226        void DoDelayedMovesAndRemoves();
227
228        virtual bool RemoveBones(uint64 guid, float x, float y);
229
230        void UpdateObjectVisibility(WorldObject* obj, Cell cell, CellPair cellpair);
231        void UpdatePlayerVisibility(Player* player, Cell cell, CellPair cellpair);
232        void UpdateObjectsVisibilityFor(Player* player, Cell cell, CellPair cellpair);
233
234        void resetMarkedCells() { marked_cells.reset(); }
235        bool isCellMarked(uint32 pCellId) { return marked_cells.test(pCellId); }
236        void markCell(uint32 pCellId) { marked_cells.set(pCellId); }
237        Creature* GetCreatureInMap(uint64 guid);
238        GameObject* GetGameObjectInMap(uint64 guid);
239
240        template<class T> void SwitchGridContainers(T* obj, bool active);
241    private:
242        void LoadVMap(int pX, int pY);
243        void LoadMap(uint32 mapid, uint32 instanceid, int x,int y);
244
245        void SetTimer(uint32 t) { i_gridExpiry = t < MIN_GRID_DELAY ? MIN_GRID_DELAY : t; }
246        //uint64 CalculateGridMask(const uint32 &y) const;
247
248        void SendInitSelf( Player * player );
249
250        void SendInitTransports( Player * player );
251        void SendRemoveTransports( Player * player );
252
253        void PlayerRelocationNotify(Player* player, Cell cell, CellPair cellpair);
254        void CreatureRelocationNotify(Creature *creature, Cell newcell, CellPair newval);
255
256        bool CreatureCellRelocation(Creature *creature, Cell new_cell);
257
258        void AddCreatureToMoveList(Creature *c, float x, float y, float z, float ang);
259        CreatureMoveList i_creaturesToMove;
260
261        bool loaded(const GridPair &) const;
262        void EnsureGridLoadedForPlayer(const Cell&, Player*, bool add_player);
263        void  EnsureGridCreated(const GridPair &);
264
265        void buildNGridLinkage(NGridType* pNGridType) { pNGridType->link(this); }
266
267        template<class T> void AddType(T *obj);
268        template<class T> void RemoveType(T *obj, bool);
269
270        NGridType* getNGrid(uint32 x, uint32 y) const
271        {
272            return i_grids[x][y];
273        }
274
275        bool isGridObjectDataLoaded(uint32 x, uint32 y) const { return getNGrid(x,y)->isGridObjectDataLoaded(); }
276        void setGridObjectDataLoaded(bool pLoaded, uint32 x, uint32 y) { getNGrid(x,y)->setGridObjectDataLoaded(pLoaded); }
277
278        inline void setNGrid(NGridType* grid, uint32 x, uint32 y);
279
280    protected:
281        typedef Trinity::ObjectLevelLockable<Map, ZThread::Mutex>::Lock Guard;
282
283        MapEntry const* i_mapEntry;
284        uint8 i_spawnMode;
285        uint32 i_id;
286        uint32 i_InstanceId;
287        uint32 m_unloadTimer;
288
289    private:
290        typedef GridReadGuard ReadGuard;
291        typedef GridWriteGuard WriteGuard;
292
293        NGridType* i_grids[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
294        GridMap *GridMaps[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
295        std::bitset<TOTAL_NUMBER_OF_CELLS_PER_MAP*TOTAL_NUMBER_OF_CELLS_PER_MAP> marked_cells;
296
297        time_t i_gridExpiry;
298
299        std::set<WorldObject *> i_objectsToRemove;
300
301        // Type specific code for add/remove to/from grid
302        template<class T>
303            void AddToGrid(T*, NGridType *, Cell const&);
304
305        template<class T>
306            void AddNotifier(T*, Cell const&, CellPair const&);
307
308        template<class T>
309            void RemoveFromGrid(T*, NGridType *, Cell const&);
310
311        template<class T>
312            void DeleteFromWorld(T*);
313};
314
315enum InstanceResetMethod
316{
317    INSTANCE_RESET_ALL,
318    INSTANCE_RESET_CHANGE_DIFFICULTY,
319    INSTANCE_RESET_GLOBAL,
320    INSTANCE_RESET_GROUP_DISBAND,
321    INSTANCE_RESET_GROUP_JOIN,
322    INSTANCE_RESET_RESPAWN_DELAY
323};
324
325class TRINITY_DLL_SPEC InstanceMap : public Map
326{
327    public:
328        typedef std::list<Player *> PlayerList;                 // online players only
329
330        InstanceMap(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode);
331        ~InstanceMap();
332        bool Add(Player *);
333        void Remove(Player *, bool);
334        void Update(const uint32&);
335        void CreateInstanceData(bool load);
336        bool Reset(uint8 method);
337        std::string GetScript() { return i_script; }
338        InstanceData* GetInstanceData() { return i_data; }
339        void PermBindAllPlayers(Player *player);
340        PlayerList const& GetPlayers() const { return i_Players;}
341        void SendToPlayers(WorldPacket const* data) const;
342        time_t GetResetTime();
343        void UnloadAll(bool pForce);
344        bool CanEnter(Player* player);
345        uint32 GetPlayersCountExceptGMs() const;
346        uint32 HavePlayers() const { return !i_Players.empty(); }
347        void SendResetWarnings(uint32 timeLeft);
348        void SetResetSchedule(bool on);
349    private:
350        bool m_resetAfterUnload;
351        bool m_unloadWhenEmpty;
352        InstanceData* i_data;
353        std::string i_script;
354        // only online players that are inside the instance currently
355        // TODO ? - use the grid instead to access the players
356        PlayerList i_Players;
357};
358
359class TRINITY_DLL_SPEC BattleGroundMap : public Map
360{
361    public:
362        typedef std::list<Player *> PlayerList;                 // online players only
363
364        BattleGroundMap(uint32 id, time_t, uint32 InstanceId);
365        ~BattleGroundMap();
366
367        bool Add(Player *);
368        void Remove(Player *, bool);
369        bool CanEnter(Player* player);
370        void SetUnload();
371        void UnloadAll(bool pForce);
372    private:
373        PlayerList i_Players;
374};
375
376/*inline
377uint64
378Map::CalculateGridMask(const uint32 &y) const
379{
380    uint64 mask = 1;
381    mask <<= y;
382    return mask;
383}
384*/
385
386template<class LOCK_TYPE, class T, class CONTAINER>
387inline void
388Map::Visit(const CellLock<LOCK_TYPE> &cell, TypeContainerVisitor<T, CONTAINER> &visitor)
389{
390    const uint32 x = cell->GridX();
391    const uint32 y = cell->GridY();
392    const uint32 cell_x = cell->CellX();
393    const uint32 cell_y = cell->CellY();
394
395    if( !cell->NoCreate() || loaded(GridPair(x,y)) )
396    {
397        EnsureGridLoadedForPlayer(cell, NULL, false);
398        //LOCK_TYPE guard(i_info[x][y]->i_lock);
399        getNGrid(x, y)->Visit(cell_x, cell_y, visitor);
400    }
401}
402#endif
Note: See TracBrowser for help on using the browser.