root/trunk/src/game/Object.h @ 207

Revision 207, 18.0 kB (checked in by yumileroy, 17 years ago)

[svn] * Improve some arena team related DB access
* Cache GM tickets on server startup.
* Remove unused src/game/HateMatrix.h and references.
* Better check client inventory pos data received in some client packets to
skip invalid cases

Original author: KingPin?
Date: 2008-11-10 09:04:23-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 _OBJECT_H
22#define _OBJECT_H
23
24#include "Common.h"
25#include "ByteBuffer.h"
26#include "UpdateFields.h"
27#include "UpdateData.h"
28#include "GameSystem/GridReference.h"
29#include "ObjectDefines.h"
30#include "GridDefines.h"
31
32#include <set>
33#include <string>
34
35#define CONTACT_DISTANCE            0.5f
36#define INTERACTION_DISTANCE        5.0f
37#define ATTACK_DISTANCE             5.0f
38#define MAX_VISIBILITY_DISTANCE  (5*SIZE_OF_GRID_CELL/2.0f) // max distance for visible object show, limited by active zone for player based at cell size (active zone = 5x5 cells)
39#define DEFAULT_VISIBILITY_DISTANCE (SIZE_OF_GRID_CELL)     // default visible distance
40
41#define DEFAULT_WORLD_OBJECT_SIZE   0.388999998569489f      // player size, also currently used (correctly?) for any non Unit world objects
42
43enum TypeMask
44{
45    TYPEMASK_OBJECT         = 0x0001,
46    TYPEMASK_ITEM           = 0x0002,
47    TYPEMASK_CONTAINER      = 0x0006,                       // TYPEMASK_ITEM | 0x0004
48    TYPEMASK_UNIT           = 0x0008,
49    TYPEMASK_PLAYER         = 0x0010,
50    TYPEMASK_GAMEOBJECT     = 0x0020,
51    TYPEMASK_DYNAMICOBJECT  = 0x0040,
52    TYPEMASK_CORPSE         = 0x0080,
53    TYPEMASK_AIGROUP        = 0x0100,
54    TYPEMASK_AREATRIGGER    = 0x0200
55};
56
57enum TypeID
58{
59    TYPEID_OBJECT        = 0,
60    TYPEID_ITEM          = 1,
61    TYPEID_CONTAINER     = 2,
62    TYPEID_UNIT          = 3,
63    TYPEID_PLAYER        = 4,
64    TYPEID_GAMEOBJECT    = 5,
65    TYPEID_DYNAMICOBJECT = 6,
66    TYPEID_CORPSE        = 7,
67    TYPEID_AIGROUP       = 8,
68    TYPEID_AREATRIGGER   = 9
69};
70
71uint32 GuidHigh2TypeId(uint32 guid_hi);
72
73enum TempSummonType
74{
75    TEMPSUMMON_TIMED_OR_DEAD_DESPAWN       = 1,             // despawns after a specified time OR when the creature disappears
76    TEMPSUMMON_TIMED_OR_CORPSE_DESPAWN     = 2,             // despawns after a specified time OR when the creature dies
77    TEMPSUMMON_TIMED_DESPAWN               = 3,             // despawns after a specified time
78    TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT = 4,             // despawns after a specified time after the creature is out of combat
79    TEMPSUMMON_CORPSE_DESPAWN              = 5,             // despawns instantly after death
80    TEMPSUMMON_CORPSE_TIMED_DESPAWN        = 6,             // despawns after a specified time after death
81    TEMPSUMMON_DEAD_DESPAWN                = 7,             // despawns when the creature disappears
82    TEMPSUMMON_MANUAL_DESPAWN              = 8              // despawns when UnSummon() is called
83};
84
85class WorldPacket;
86class UpdateData;
87class ByteBuffer;
88class WorldSession;
89class Creature;
90class Player;
91class Map;
92class UpdateMask;
93class InstanceData;
94class GameObject;
95
96typedef UNORDERED_MAP<Player*, UpdateData> UpdateDataMapType;
97
98struct WorldLocation
99{
100    uint32 mapid;
101    float x;
102    float y;
103    float z;
104    float o;
105    explicit WorldLocation(uint32 mapid = 0, float x = 0, float y = 0, float z = 0, float o = 0)
106        : mapid(mapid), x(x), y(y), z(z), o(o) {}
107    WorldLocation(WorldLocation const &loc)
108        : mapid(loc.mapid), x(loc.x), y(loc.y), z(loc.z), o(loc.o) {}
109};
110
111class TRINITY_DLL_SPEC Object
112{
113    public:
114        virtual ~Object ( );
115
116        const bool& IsInWorld() const { return m_inWorld; }
117        virtual void AddToWorld()
118        {
119            if(m_inWorld)
120                return;
121
122            m_inWorld = true;
123
124            // synchronize values mirror with values array (changes will send in updatecreate opcode any way
125            ClearUpdateMask(true);
126        }
127        virtual void RemoveFromWorld()
128        {
129            // if we remove from world then sending changes not required
130            if(m_uint32Values)
131                ClearUpdateMask(true);
132            m_inWorld = false;
133        }
134
135        const uint64& GetGUID() const { return GetUInt64Value(0); }
136        uint32 GetGUIDLow() const { return GUID_LOPART(GetUInt64Value(0)); }
137        uint32 GetGUIDMid() const { return GUID_ENPART(GetUInt64Value(0)); }
138        uint32 GetGUIDHigh() const { return GUID_HIPART(GetUInt64Value(0)); }
139        const ByteBuffer& GetPackGUID() const { return m_PackGUID; }
140        uint32 GetEntry() const { return GetUInt32Value(OBJECT_FIELD_ENTRY); }
141
142        uint8 GetTypeId() const { return m_objectTypeId; }
143        bool isType(uint16 mask) const { return (mask & m_objectType); }
144
145        virtual void BuildCreateUpdateBlockForPlayer( UpdateData *data, Player *target ) const;
146        void SendUpdateToPlayer(Player* player);
147
148        void BuildValuesUpdateBlockForPlayer( UpdateData *data, Player *target ) const;
149        void BuildOutOfRangeUpdateBlock( UpdateData *data ) const;
150        void BuildMovementUpdateBlock( UpdateData * data, uint32 flags = 0 ) const;
151        void BuildUpdate(UpdateDataMapType &);
152
153        virtual void DestroyForPlayer( Player *target ) const;
154
155        const int32& GetInt32Value( uint16 index ) const
156        {
157            ASSERT( index < m_valuesCount || PrintIndexError( index , false) );
158            return m_int32Values[ index ];
159        }
160
161        const uint32& GetUInt32Value( uint16 index ) const
162        {
163            ASSERT( index < m_valuesCount || PrintIndexError( index , false) );
164            return m_uint32Values[ index ];
165        }
166
167        const uint64& GetUInt64Value( uint16 index ) const
168        {
169            ASSERT( index + 1 < m_valuesCount || PrintIndexError( index , false) );
170            return *((uint64*)&(m_uint32Values[ index ]));
171        }
172
173        const float& GetFloatValue( uint16 index ) const
174        {
175            ASSERT( index < m_valuesCount || PrintIndexError( index , false ) );
176            return m_floatValues[ index ];
177        }
178
179        uint8 GetByteValue( uint16 index, uint8 offset) const
180        {
181            ASSERT( index < m_valuesCount || PrintIndexError( index , false) );
182            ASSERT( offset < 4 );
183            return *(((uint8*)&m_uint32Values[ index ])+offset);
184        }
185
186        uint8 GetUInt16Value( uint16 index, uint8 offset) const
187        {
188            ASSERT( index < m_valuesCount || PrintIndexError( index , false) );
189            ASSERT( offset < 2 );
190            return *(((uint16*)&m_uint32Values[ index ])+offset);
191        }
192
193        void SetInt32Value(  uint16 index,        int32  value );
194        void SetUInt32Value( uint16 index,       uint32  value );
195        void SetUInt64Value( uint16 index, const uint64 &value );
196        void SetFloatValue(  uint16 index,       float   value );
197        void SetByteValue(   uint16 index, uint8 offset, uint8 value );
198        void SetUInt16Value( uint16 index, uint8 offset, uint16 value );
199        void SetInt16Value(  uint16 index, uint8 offset, int16 value ) { SetUInt16Value(index,offset,(uint16)value); }
200        void SetStatFloatValue( uint16 index, float value);
201        void SetStatInt32Value( uint16 index, int32 value);
202
203        void ApplyModUInt32Value(uint16 index, int32 val, bool apply);
204        void ApplyModInt32Value(uint16 index, int32 val, bool apply);
205        void ApplyModUInt64Value(uint16 index, int32 val, bool apply);
206        void ApplyModPositiveFloatValue( uint16 index, float val, bool apply);
207        void ApplyModSignedFloatValue( uint16 index, float val, bool apply);
208
209        void ApplyPercentModFloatValue(uint16 index, float val, bool apply)
210        {
211            val = val != -100.0f ? val : -99.9f ;
212            SetFloatValue(index, GetFloatValue(index) * (apply?(100.0f+val)/100.0f : 100.0f / (100.0f+val)) );
213        }
214
215        void SetFlag( uint16 index, uint32 newFlag );
216        void RemoveFlag( uint16 index, uint32 oldFlag );
217
218        void ToggleFlag( uint16 index, uint32 flag)
219        {
220            if(HasFlag(index, flag))
221                RemoveFlag(index, flag);
222            else
223                SetFlag(index, flag);
224        }
225
226        bool HasFlag( uint16 index, uint32 flag ) const
227        {
228            ASSERT( index < m_valuesCount || PrintIndexError( index , false ) );
229            return (m_uint32Values[ index ] & flag) != 0;
230        }
231
232        void ApplyModFlag( uint16 index, uint32 flag, bool apply)
233        {
234            if(apply) SetFlag(index,flag); else RemoveFlag(index,flag);
235        }
236
237        void SetFlag64( uint16 index, uint64 newFlag )
238        {
239            uint64 oldval = GetUInt64Value(index);
240            uint64 newval = oldval | newFlag;
241            SetUInt64Value(index,newval);
242        }
243
244        void RemoveFlag64( uint16 index, uint64 oldFlag )
245        {
246            uint64 oldval = GetUInt64Value(index);
247            uint64 newval = oldval & ~oldFlag;
248            SetUInt64Value(index,newval);
249        }
250
251        void ToggleFlag64( uint16 index, uint64 flag)
252        {
253            if(HasFlag64(index, flag))
254                RemoveFlag64(index, flag);
255            else
256                SetFlag64(index, flag);
257        }
258
259        bool HasFlag64( uint16 index, uint64 flag ) const
260        {
261            ASSERT( index < m_valuesCount || PrintIndexError( index , false ) );
262            return (GetUInt64Value( index ) & flag) != 0;
263        }
264
265        void ApplyModFlag64( uint16 index, uint64 flag, bool apply)
266        {
267            if(apply) SetFlag64(index,flag); else RemoveFlag64(index,flag);
268        }
269
270        void ClearUpdateMask(bool remove);
271        void SendUpdateObjectToAllExcept(Player* exceptPlayer);
272
273        bool LoadValues(const char* data);
274
275        uint16 GetValuesCount() const { return m_valuesCount; }
276
277        void InitValues() { _InitValues(); }
278
279        virtual bool hasQuest(uint32 /* quest_id */) const { return false; }
280        virtual bool hasInvolvedQuest(uint32 /* quest_id */) const { return false; }
281    protected:
282
283        Object ( );
284
285        void _InitValues();
286        void _Create (uint32 guidlow, uint32 entry, HighGuid guidhigh);
287
288        virtual void _SetUpdateBits(UpdateMask *updateMask, Player *target) const;
289
290        virtual void _SetCreateBits(UpdateMask *updateMask, Player *target) const;
291        void _BuildMovementUpdate(ByteBuffer * data, uint8 flags, uint32 flags2 ) const;
292        void _BuildValuesUpdate(uint8 updatetype, ByteBuffer *data, UpdateMask *updateMask, Player *target ) const;
293
294        uint16 m_objectType;
295
296        uint8 m_objectTypeId;
297        uint8 m_updateFlag;
298
299        union
300        {
301            int32  *m_int32Values;
302            uint32 *m_uint32Values;
303            float *m_floatValues;
304        };
305
306        uint32 *m_uint32Values_mirror;
307
308        uint16 m_valuesCount;
309
310        bool m_objectUpdated;
311
312    private:
313        bool m_inWorld;
314
315        ByteBuffer m_PackGUID;
316
317        // for output helpfull error messages from asserts
318        bool PrintIndexError(uint32 index, bool set) const;
319        Object(const Object&);                              // prevent generation copy constructor
320        Object& operator=(Object const&);                   // prevent generation assigment operator
321};
322
323class TRINITY_DLL_SPEC WorldObject : public Object
324{
325    public:
326        virtual ~WorldObject ( );
327
328        virtual void AddToWorld();
329
330        virtual void RemoveFromWorld();
331
332        virtual void Update ( uint32 /*time_diff*/ ) { }
333
334        void _Create( uint32 guidlow, HighGuid guidhigh, uint32 mapid );
335
336        void Relocate(float x, float y, float z, float orientation)
337        {
338            m_positionX = x;
339            m_positionY = y;
340            m_positionZ = z;
341            m_orientation = orientation;
342        }
343
344        void Relocate(float x, float y, float z)
345        {
346            m_positionX = x;
347            m_positionY = y;
348            m_positionZ = z;
349        }
350
351        void Relocate(WorldLocation const & loc)
352        {
353            SetMapId(loc.mapid);
354            Relocate(loc.x, loc.y, loc.z, loc.o);
355        }
356
357        void SetOrientation(float orientation) { m_orientation = orientation; }
358
359        float GetPositionX( ) const { return m_positionX; }
360        float GetPositionY( ) const { return m_positionY; }
361        float GetPositionZ( ) const { return m_positionZ; }
362        void GetPosition( float &x, float &y, float &z ) const
363            { x = m_positionX; y = m_positionY; z = m_positionZ; }
364        void GetPosition( WorldLocation &loc ) const
365            { loc.mapid = GetMapId(); GetPosition(loc.x, loc.y, loc.z); loc.o = GetOrientation(); }
366        float GetOrientation( ) const { return m_orientation; }
367        void GetNearPoint2D( float &x, float &y, float distance, float absAngle) const;
368        void GetNearPoint( WorldObject const* searcher, float &x, float &y, float &z, float searcher_size, float distance2d,float absAngle) const;
369        void GetClosePoint(float &x, float &y, float &z, float size, float distance2d = 0, float angle = 0) const
370        {
371            // angle calculated from current orientation
372            GetNearPoint(NULL,x,y,z,size,distance2d,GetOrientation() + angle);
373        }
374        void GetContactPoint( const WorldObject* obj, float &x, float &y, float &z, float distance2d = CONTACT_DISTANCE) const
375        {
376            // angle to face `obj` to `this` using distance includes size of `obj`
377            GetNearPoint(obj,x,y,z,obj->GetObjectSize(),distance2d,GetAngle( obj ));
378        }
379
380        float GetObjectSize() const
381        {
382            return ( m_valuesCount > UNIT_FIELD_BOUNDINGRADIUS ) ? m_floatValues[UNIT_FIELD_BOUNDINGRADIUS] : DEFAULT_WORLD_OBJECT_SIZE;
383        }
384        bool IsPositionValid() const;
385        void UpdateGroundPositionZ(float x, float y, float &z) const;
386
387        void GetRandomPoint( float x, float y, float z, float distance, float &rand_x, float &rand_y, float &rand_z ) const;
388
389        void SetMapId(uint32 newMap) { m_mapId = newMap; }
390
391        uint32 GetMapId() const { return m_mapId; }
392
393        uint32 GetZoneId() const;
394        uint32 GetAreaId() const;
395
396        InstanceData* GetInstanceData();
397
398        const char* GetName() const { return m_name.c_str(); }
399        void SetName(std::string newname) { m_name=newname; }
400
401        float GetDistance( const WorldObject* obj ) const;
402        float GetDistance(const float x, const float y, const float z) const;
403        float GetDistance2d(const WorldObject* obj) const;
404        float GetDistance2d(const float x, const float y) const;
405        float GetDistanceZ(const WorldObject* obj) const;
406        bool IsInMap(const WorldObject* obj) const { return GetMapId()==obj->GetMapId() && GetInstanceId()==obj->GetInstanceId(); }
407        bool IsWithinDistInMap(const WorldObject* obj, const float dist2compare) const;
408        bool IsWithinLOS(const float x, const float y, const float z ) const;
409        bool IsWithinLOSInMap(const WorldObject* obj) const;
410
411        float GetAngle( const WorldObject* obj ) const;
412        float GetAngle( const float x, const float y ) const;
413        bool HasInArc( const float arcangle, const WorldObject* obj ) const;
414
415        virtual void SendMessageToSet(WorldPacket *data, bool self, bool to_possessor = true);
416        virtual void SendMessageToSetInRange(WorldPacket *data, float dist, bool self, bool to_possessor = true);
417        void BuildHeartBeatMsg( WorldPacket *data ) const;
418        void BuildTeleportAckMsg( WorldPacket *data, float x, float y, float z, float ang) const;
419        bool IsBeingTeleported() { return mSemaphoreTeleport; }
420        void SetSemaphoreTeleport(bool semphsetting) { mSemaphoreTeleport = semphsetting; }
421
422        void MonsterSay(const char* text, uint32 language, uint64 TargetGuid);
423        void MonsterYell(const char* text, uint32 language, uint64 TargetGuid);
424        void MonsterTextEmote(const char* text, uint64 TargetGuid, bool IsBossEmote = false);
425        void MonsterWhisper(const char* text, uint64 receiver, bool IsBossWhisper = false);
426        void MonsterSay(int32 textId, uint32 language, uint64 TargetGuid);
427        void MonsterYell(int32 textId, uint32 language, uint64 TargetGuid);
428        void MonsterTextEmote(int32 textId, uint64 TargetGuid, bool IsBossEmote = false);
429        void MonsterWhisper(int32 textId, uint64 receiver, bool IsBossWhisper = false);
430        void BuildMonsterChat(WorldPacket *data, uint8 msgtype, char const* text, uint32 language, char const* name, uint64 TargetGuid) const;
431
432        void SendObjectDeSpawnAnim(uint64 guid);
433
434        virtual void SaveRespawnTime() {}
435
436        uint32 GetInstanceId() const { return m_InstanceId; }
437        void SetInstanceId(uint32 val) { m_InstanceId = val; }
438
439        void AddObjectToRemoveList();
440
441        // main visibility check function in normal case (ignore grey zone distance check)
442        bool isVisibleFor(Player const* u) const { return isVisibleForInState(u,false); }
443
444        // low level function for visibility change code, must be define in all main world object subclasses
445        virtual bool isVisibleForInState(Player const* u, bool inVisibleList) const = 0;
446
447        // Low Level Packets
448        void SendPlaySound(uint32 Sound, bool OnlySelf);
449
450        Map      * GetMap() const;
451        Map const* GetBaseMap() const;
452        Creature* SummonCreature(uint32 id, float x, float y, float z, float ang,TempSummonType spwtype,uint32 despwtime);
453        GameObject* SummonGameObject(uint32 entry, float x, float y, float z, float ang, float rotation0, float rotation1, float rotation2, float rotation3, uint32 respawnTime);
454        bool isActive() const { return m_isActive; }
455        virtual void setActive(bool isActive);
456    protected:
457        explicit WorldObject();
458        std::string m_name;
459        bool m_isActive;
460
461    private:
462        uint32 m_mapId;
463
464        float m_positionX;
465        float m_positionY;
466        float m_positionZ;
467        float m_orientation;
468
469        bool mSemaphoreTeleport;
470
471        uint32 m_InstanceId;
472};
473#endif
Note: See TracBrowser for help on using the browser.