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

Revision 2, 13.6 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#ifndef MANGOS_MAP_H
20#define MANGOS_MAP_H
21
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"
29#include "Cell.h"
30#include "Object.h"
31#include "Timer.h"
32#include "SharedDefines.h"
33#include "GameSystem/GridRefManager.h"
34
35#include <bitset>
36#include <list>
37
38class Unit;
39class WorldPacket;
40class InstanceData;
41class Group;
42class InstanceSave;
43
44namespace ZThread
45{
46    class Lockable;
47    class ReadWriteLock;
48}
49
50typedef ZThread::FairReadWriteLock GridRWLock;
51
52template<class MUTEX, class LOCK_TYPE>
53struct RGuard
54{
55    RGuard(MUTEX &l) : i_lock(l.getReadLock()) {}
56    MaNGOS::GeneralLock<LOCK_TYPE> i_lock;
57};
58
59template<class MUTEX, class LOCK_TYPE>
60struct WGuard
61{
62    WGuard(MUTEX &l) : i_lock(l.getWriteLock()) {}
63    MaNGOS::GeneralLock<LOCK_TYPE> i_lock;
64};
65
66typedef RGuard<GridRWLock, ZThread::Lockable> GridReadGuard;
67typedef WGuard<GridRWLock, ZThread::Lockable> GridWriteGuard;
68typedef MaNGOS::SingleThreaded<GridRWLock>::Lock NullGuard;
69
70typedef struct
71{
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];
76}GridMap;
77
78struct CreatureMover
79{
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) {}
82
83    float x, y, z, ang;
84};
85
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__ )
88#pragma pack(1)
89#else
90#pragma pack(push,1)
91#endif
92
93struct InstanceTemplate
94{
95    uint32 map;
96    uint32 parent;
97    uint32 levelMin;
98    uint32 levelMax;
99    uint32 maxPlayers;
100    uint32 reset_delay;
101    float startLocX;
102    float startLocY;
103    float startLocZ;
104    float startLocO;
105    char const* script;
106};
107
108enum LevelRequirementVsMode
109{
110    LEVELREQUIREMENT_HEROIC = 70
111};
112
113#if defined( __GNUC__ )
114#pragma pack()
115#else
116#pragma pack(pop)
117#endif
118
119typedef HM_NAMESPACE::hash_map<Creature*, CreatureMover> CreatureMoveList;
120
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
124
125class MANGOS_DLL_SPEC Map : public GridRefManager<NGridType>, public MaNGOS::ObjectLevelLockable<Map, ZThread::Mutex>
126{
127    public:
128        Map(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode);
129        virtual ~Map();
130
131        // currently unused for normal maps
132        virtual bool CanUnload(const uint32& diff);
133
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);
138
139        virtual void Update(const uint32&);
140
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);
145
146        void PlayerRelocation(Player *, float x, float y, float z, float angl);
147        void CreatureRelocation(Creature *creature, float x, float y, float, float);
148
149        template<class LOCK_TYPE, class T, class CONTAINER> void Visit(const CellLock<LOCK_TYPE> &cell, TypeContainerVisitor<T, CONTAINER> &visitor);
150
151        inline bool IsRemovalGrid(float x, float y) const
152        {
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 );
155        }
156
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);
162
163        void ResetGridExpiry(NGridType &grid, float factor = 1) const
164        {
165            grid.ResetTimeTracker((time_t)((float)i_gridExpiry*factor));
166        }
167
168        time_t GetGridExpiry(void) const { return i_gridExpiry; }
169        uint32 GetId(void) const { return i_id; }
170
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);
174
175        static void InitStateMachine();
176        static void DeleteStateMachine();
177
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
182
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;
187
188        static uint32 GetAreaId(uint16 areaflag,uint32 map_id);
189        static uint32 GetZoneId(uint16 areaflag,uint32 map_id);
190
191        uint32 GetAreaId(float x, float y) const
192        {
193            return GetAreaId(GetAreaFlag(x,y),i_id);
194        }
195
196        uint32 GetZoneId(float x, float y) const
197        {
198            return GetZoneId(GetAreaFlag(x,y),i_id);
199        }
200
201        virtual void MoveAllCreaturesInMoveList();
202        virtual void RemoveAllObjectsInRemoveList();
203
204        bool CreatureRespawnRelocation(Creature *c);        // used only in MoveAllCreaturesInMoveList and ObjectGridUnloader
205
206        // assert print helper
207        bool CheckGridIntegrity(Creature* c, bool moved) const;
208
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;
213
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
223        void AddObjectToRemoveList(WorldObject *obj);
224        void DoDelayedMovesAndRemoves();
225
226        virtual bool RemoveBones(uint64 guid, float x, float y);
227
228        void UpdateObjectVisibility(WorldObject* obj, Cell cell, CellPair cellpair);
229        void UpdatePlayerVisibility(Player* player, Cell cell, CellPair cellpair);
230        void UpdateObjectsVisibilityFor(Player* player, Cell cell, CellPair cellpair);
231
232        void resetMarkedCells() { marked_cells.reset(); }
233        bool isCellMarked(uint32 pCellId) { return marked_cells.test(pCellId); }
234        void markCell(uint32 pCellId) { marked_cells.set(pCellId); }
235    private:
236        void LoadVMap(int pX, int pY);
237        void LoadMap(uint32 mapid, uint32 instanceid, int x,int y);
238
239        void SetTimer(uint32 t) { i_gridExpiry = t < MIN_GRID_DELAY ? MIN_GRID_DELAY : t; }
240        //uint64 CalculateGridMask(const uint32 &y) const;
241
242        void SendInitSelf( Player * player );
243
244        void SendInitTransports( Player * player );
245        void SendRemoveTransports( Player * player );
246
247        void PlayerRelocationNotify(Player* player, Cell cell, CellPair cellpair);
248        void CreatureRelocationNotify(Creature *creature, Cell newcell, CellPair newval);
249
250        bool CreatureCellRelocation(Creature *creature, Cell new_cell);
251
252        void AddCreatureToMoveList(Creature *c, float x, float y, float z, float ang);
253        CreatureMoveList i_creaturesToMove;
254
255        bool loaded(const GridPair &) const;
256        void EnsureGridLoadedForPlayer(const Cell&, Player*, bool add_player);
257        void  EnsureGridCreated(const GridPair &);
258
259        void buildNGridLinkage(NGridType* pNGridType) { pNGridType->link(this); }
260
261        template<class T> void AddType(T *obj);
262        template<class T> void RemoveType(T *obj, bool);
263
264        NGridType* getNGrid(uint32 x, uint32 y) const
265        {
266            return i_grids[x][y];
267        }
268
269        bool isGridObjectDataLoaded(uint32 x, uint32 y) const { return getNGrid(x,y)->isGridObjectDataLoaded(); }
270        void setGridObjectDataLoaded(bool pLoaded, uint32 x, uint32 y) { getNGrid(x,y)->setGridObjectDataLoaded(pLoaded); }
271
272        inline void setNGrid(NGridType* grid, uint32 x, uint32 y);
273
274    protected:
275        typedef MaNGOS::ObjectLevelLockable<Map, ZThread::Mutex>::Lock Guard;
276
277        MapEntry const* i_mapEntry;
278        uint8 i_spawnMode;
279        uint32 i_id;
280        uint32 i_InstanceId;
281        uint32 m_unloadTimer;
282
283    private:
284        typedef GridReadGuard ReadGuard;
285        typedef GridWriteGuard WriteGuard;
286
287        NGridType* i_grids[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
288        GridMap *GridMaps[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
289        std::bitset<TOTAL_NUMBER_OF_CELLS_PER_MAP*TOTAL_NUMBER_OF_CELLS_PER_MAP> marked_cells;
290
291        time_t i_gridExpiry;
292
293        std::set<WorldObject *> i_objectsToRemove;
294
295        // Type specific code for add/remove to/from grid
296        template<class T>
297            void AddToGrid(T*, NGridType *, Cell const&);
298
299        template<class T>
300            void AddNotifier(T*, Cell const&, CellPair const&);
301
302        template<class T>
303            void RemoveFromGrid(T*, NGridType *, Cell const&);
304
305        template<class T>
306            void DeleteFromWorld(T*);
307};
308
309enum InstanceResetMethod
310{
311    INSTANCE_RESET_ALL,
312    INSTANCE_RESET_CHANGE_DIFFICULTY,
313    INSTANCE_RESET_GLOBAL,
314    INSTANCE_RESET_GROUP_DISBAND,
315    INSTANCE_RESET_GROUP_JOIN,
316    INSTANCE_RESET_RESPAWN_DELAY
317};
318
319class MANGOS_DLL_SPEC InstanceMap : public Map
320{
321    public:
322        typedef std::list<Player *> PlayerList;                 // online players only
323
324        InstanceMap(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode);
325        ~InstanceMap();
326        bool Add(Player *);
327        void Remove(Player *, bool);
328        void Update(const uint32&);
329        void CreateInstanceData(bool load);
330        bool Reset(uint8 method);
331        std::string GetScript() { return i_script; }
332        InstanceData* GetInstanceData() { return i_data; }
333        void PermBindAllPlayers(Player *player);
334        PlayerList const& GetPlayers() const { return i_Players;}
335        void SendToPlayers(WorldPacket const* data) const;
336        time_t GetResetTime();
337        void UnloadAll(bool pForce);
338        bool CanEnter(Player* player);
339        uint32 GetPlayersCountExceptGMs() const;
340        uint32 HavePlayers() const { return !i_Players.empty(); }
341        void SendResetWarnings(uint32 timeLeft);
342        void SetResetSchedule(bool on);
343    private:
344        bool m_resetAfterUnload;
345        bool m_unloadWhenEmpty;
346        InstanceData* i_data;
347        std::string i_script;
348        // only online players that are inside the instance currently
349        // TODO ? - use the grid instead to access the players
350        PlayerList i_Players;
351};
352
353class MANGOS_DLL_SPEC BattleGroundMap : public Map
354{
355    public:
356        typedef std::list<Player *> PlayerList;                 // online players only
357
358        BattleGroundMap(uint32 id, time_t, uint32 InstanceId);
359        ~BattleGroundMap();
360
361        bool Add(Player *);
362        void Remove(Player *, bool);
363        bool CanEnter(Player* player);
364        void SetUnload();
365        void UnloadAll(bool pForce);
366    private:
367        PlayerList i_Players;
368};
369
370/*inline
371uint64
372Map::CalculateGridMask(const uint32 &y) const
373{
374    uint64 mask = 1;
375    mask <<= y;
376    return mask;
377}
378*/
379
380template<class LOCK_TYPE, class T, class CONTAINER>
381inline void
382Map::Visit(const CellLock<LOCK_TYPE> &cell, TypeContainerVisitor<T, CONTAINER> &visitor)
383{
384    const uint32 x = cell->GridX();
385    const uint32 y = cell->GridY();
386    const uint32 cell_x = cell->CellX();
387    const uint32 cell_y = cell->CellY();
388
389    if( !cell->NoCreate() || loaded(GridPair(x,y)) )
390    {
391        EnsureGridLoadedForPlayer(cell, NULL, false);
392        //LOCK_TYPE guard(i_info[x][y]->i_lock);
393        getNGrid(x, y)->Visit(cell_x, cell_y, visitor);
394    }
395}
396#endif
Note: See TracBrowser for help on using the browser.