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

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

[svn] * Merge Temp dev SVN with Assembla.
* Changes include:

  • Implementation of w12x's Outdoor PvP and Game Event Systems.
  • Temporary removal of IRC Chat Bot (until infinite loop when disabled is fixed).
  • All mangos -> trinity (to convert your mangos_string table, please run mangos_string_to_trinity_string.sql).
  • Improved Config cleanup.
  • And many more changes.

Original author: Seline
Date: 2008-10-14 11:57:03-05:00

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