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

Revision 102, 13.7 kB (checked in by yumileroy, 17 years ago)

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05: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 HM_NAMESPACE::hash_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);
144        void MessageBroadcast(WorldObject *, WorldPacket *);
145        void MessageDistBroadcast(Player *, WorldPacket *, float dist, bool to_self, bool own_team_only = false);
146        void MessageDistBroadcast(WorldObject *, WorldPacket *, float dist);
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    private:
240        void LoadVMap(int pX, int pY);
241        void LoadMap(uint32 mapid, uint32 instanceid, int x,int y);
242
243        void SetTimer(uint32 t) { i_gridExpiry = t < MIN_GRID_DELAY ? MIN_GRID_DELAY : t; }
244        //uint64 CalculateGridMask(const uint32 &y) const;
245
246        void SendInitSelf( Player * player );
247
248        void SendInitTransports( Player * player );
249        void SendRemoveTransports( Player * player );
250
251        void PlayerRelocationNotify(Player* player, Cell cell, CellPair cellpair);
252        void CreatureRelocationNotify(Creature *creature, Cell newcell, CellPair newval);
253
254        bool CreatureCellRelocation(Creature *creature, Cell new_cell);
255
256        void AddCreatureToMoveList(Creature *c, float x, float y, float z, float ang);
257        CreatureMoveList i_creaturesToMove;
258
259        bool loaded(const GridPair &) const;
260        void EnsureGridLoadedForPlayer(const Cell&, Player*, bool add_player);
261        void  EnsureGridCreated(const GridPair &);
262
263        void buildNGridLinkage(NGridType* pNGridType) { pNGridType->link(this); }
264
265        template<class T> void AddType(T *obj);
266        template<class T> void RemoveType(T *obj, bool);
267
268        NGridType* getNGrid(uint32 x, uint32 y) const
269        {
270            return i_grids[x][y];
271        }
272
273        bool isGridObjectDataLoaded(uint32 x, uint32 y) const { return getNGrid(x,y)->isGridObjectDataLoaded(); }
274        void setGridObjectDataLoaded(bool pLoaded, uint32 x, uint32 y) { getNGrid(x,y)->setGridObjectDataLoaded(pLoaded); }
275
276        inline void setNGrid(NGridType* grid, uint32 x, uint32 y);
277
278    protected:
279        typedef Trinity::ObjectLevelLockable<Map, ZThread::Mutex>::Lock Guard;
280
281        MapEntry const* i_mapEntry;
282        uint8 i_spawnMode;
283        uint32 i_id;
284        uint32 i_InstanceId;
285        uint32 m_unloadTimer;
286
287    private:
288        typedef GridReadGuard ReadGuard;
289        typedef GridWriteGuard WriteGuard;
290
291        NGridType* i_grids[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
292        GridMap *GridMaps[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
293        std::bitset<TOTAL_NUMBER_OF_CELLS_PER_MAP*TOTAL_NUMBER_OF_CELLS_PER_MAP> marked_cells;
294
295        time_t i_gridExpiry;
296
297        std::set<WorldObject *> i_objectsToRemove;
298
299        // Type specific code for add/remove to/from grid
300        template<class T>
301            void AddToGrid(T*, NGridType *, Cell const&);
302
303        template<class T>
304            void AddNotifier(T*, Cell const&, CellPair const&);
305
306        template<class T>
307            void RemoveFromGrid(T*, NGridType *, Cell const&);
308
309        template<class T>
310            void DeleteFromWorld(T*);
311};
312
313enum InstanceResetMethod
314{
315    INSTANCE_RESET_ALL,
316    INSTANCE_RESET_CHANGE_DIFFICULTY,
317    INSTANCE_RESET_GLOBAL,
318    INSTANCE_RESET_GROUP_DISBAND,
319    INSTANCE_RESET_GROUP_JOIN,
320    INSTANCE_RESET_RESPAWN_DELAY
321};
322
323class TRINITY_DLL_SPEC InstanceMap : public Map
324{
325    public:
326        typedef std::list<Player *> PlayerList;                 // online players only
327
328        InstanceMap(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode);
329        ~InstanceMap();
330        bool Add(Player *);
331        void Remove(Player *, bool);
332        void Update(const uint32&);
333        void CreateInstanceData(bool load);
334        bool Reset(uint8 method);
335        std::string GetScript() { return i_script; }
336        InstanceData* GetInstanceData() { return i_data; }
337        void PermBindAllPlayers(Player *player);
338        PlayerList const& GetPlayers() const { return i_Players;}
339        void SendToPlayers(WorldPacket const* data) const;
340        time_t GetResetTime();
341        void UnloadAll(bool pForce);
342        bool CanEnter(Player* player);
343        uint32 GetPlayersCountExceptGMs() const;
344        uint32 HavePlayers() const { return !i_Players.empty(); }
345        void SendResetWarnings(uint32 timeLeft);
346        void SetResetSchedule(bool on);
347    private:
348        bool m_resetAfterUnload;
349        bool m_unloadWhenEmpty;
350        InstanceData* i_data;
351        std::string i_script;
352        // only online players that are inside the instance currently
353        // TODO ? - use the grid instead to access the players
354        PlayerList i_Players;
355};
356
357class TRINITY_DLL_SPEC BattleGroundMap : public Map
358{
359    public:
360        typedef std::list<Player *> PlayerList;                 // online players only
361
362        BattleGroundMap(uint32 id, time_t, uint32 InstanceId);
363        ~BattleGroundMap();
364
365        bool Add(Player *);
366        void Remove(Player *, bool);
367        bool CanEnter(Player* player);
368        void SetUnload();
369        void UnloadAll(bool pForce);
370    private:
371        PlayerList i_Players;
372};
373
374/*inline
375uint64
376Map::CalculateGridMask(const uint32 &y) const
377{
378    uint64 mask = 1;
379    mask <<= y;
380    return mask;
381}
382*/
383
384template<class LOCK_TYPE, class T, class CONTAINER>
385inline void
386Map::Visit(const CellLock<LOCK_TYPE> &cell, TypeContainerVisitor<T, CONTAINER> &visitor)
387{
388    const uint32 x = cell->GridX();
389    const uint32 y = cell->GridY();
390    const uint32 cell_x = cell->CellX();
391    const uint32 cell_y = cell->CellY();
392
393    if( !cell->NoCreate() || loaded(GridPair(x,y)) )
394    {
395        EnsureGridLoadedForPlayer(cell, NULL, false);
396        //LOCK_TYPE guard(i_info[x][y]->i_lock);
397        getNGrid(x, y)->Visit(cell_x, cell_y, visitor);
398    }
399}
400#endif
Note: See TracBrowser for help on using the browser.