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

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

[svn] Fix some mistakes of the last two commits.

Original author: megamage
Date: 2008-10-27 08:28:57-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 _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
31#include <set>
32#include <string>
33
34#define CONTACT_DISTANCE            0.5f
35#define INTERACTION_DISTANCE        5
36#define ATTACK_DISTANCE             5
37#define DETECT_DISTANCE             20                      // max distance to successful detect stealthed unit
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 HM_NAMESPACE::hash_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 Update ( uint32 /*time_diff*/ ) { }
329
330        void _Create( uint32 guidlow, HighGuid guidhigh, uint32 mapid );
331
332        void Relocate(float x, float y, float z, float orientation)
333        {
334            m_positionX = x;
335            m_positionY = y;
336            m_positionZ = z;
337            m_orientation = orientation;
338        }
339
340        void Relocate(float x, float y, float z)
341        {
342            m_positionX = x;
343            m_positionY = y;
344            m_positionZ = z;
345        }
346
347        void Relocate(WorldLocation const & loc)
348        {
349            SetMapId(loc.mapid);
350            Relocate(loc.x, loc.y, loc.z, loc.o);
351        }
352
353        void SetOrientation(float orientation) { m_orientation = orientation; }
354
355        float GetPositionX( ) const { return m_positionX; }
356        float GetPositionY( ) const { return m_positionY; }
357        float GetPositionZ( ) const { return m_positionZ; }
358        void GetPosition( float &x, float &y, float &z ) const
359            { x = m_positionX; y = m_positionY; z = m_positionZ; }
360        void GetPosition( WorldLocation &loc ) const
361            { loc.mapid = GetMapId(); GetPosition(loc.x, loc.y, loc.z); loc.o = GetOrientation(); }
362        float GetOrientation( ) const { return m_orientation; }
363        void GetNearPoint2D( float &x, float &y, float distance, float absAngle) const;
364        void GetNearPoint( WorldObject const* searcher, float &x, float &y, float &z, float searcher_size, float distance2d,float absAngle) const;
365        void GetClosePoint(float &x, float &y, float &z, float size, float distance2d = 0, float angle = 0) const
366        {
367            // angle calculated from current orientation
368            GetNearPoint(NULL,x,y,z,size,distance2d,GetOrientation() + angle);
369        }
370        void GetContactPoint( const WorldObject* obj, float &x, float &y, float &z, float distance2d = CONTACT_DISTANCE) const
371        {
372            // angle to face `obj` to `this` using distance includes size of `obj`
373            GetNearPoint(obj,x,y,z,obj->GetObjectSize(),distance2d,GetAngle( obj ));
374        }
375        void GetRandomContactPoint( const WorldObject* obj, float &x, float &y, float &z, float distance2dMin, float distance2dMax ) const;
376
377        float GetObjectSize() const
378        {
379            return ( m_valuesCount > UNIT_FIELD_BOUNDINGRADIUS ) ? m_floatValues[UNIT_FIELD_BOUNDINGRADIUS] : DEFAULT_WORLD_OBJECT_SIZE;
380        }
381        bool IsPositionValid() const;
382        void UpdateGroundPositionZ(float x, float y, float &z) const;
383
384        void GetRandomPoint( float x, float y, float z, float distance, float &rand_x, float &rand_y, float &rand_z ) const;
385
386        void SetMapId(uint32 newMap) { m_mapId = newMap; }
387
388        uint32 GetMapId() const { return m_mapId; }
389
390        uint32 GetZoneId() const;
391        uint32 GetAreaId() const;
392
393        InstanceData* GetInstanceData();
394
395        const char* GetName() const { return m_name.c_str(); }
396        void SetName(std::string newname) { m_name=newname; }
397
398        float GetDistance( const WorldObject* obj ) const;
399        float GetDistance(const float x, const float y, const float z) const;
400        float GetDistance2d(const WorldObject* obj) const;
401        float GetDistance2d(const float x, const float y) const;
402        float GetDistanceZ(const WorldObject* obj) const;
403        bool IsInMap(const WorldObject* obj) const { return GetMapId()==obj->GetMapId() && GetInstanceId()==obj->GetInstanceId(); }
404        bool IsWithinDistInMap(const WorldObject* obj, const float dist2compare) const;
405        bool IsWithinLOS(const float x, const float y, const float z ) const;
406        bool IsWithinLOSInMap(const WorldObject* obj) const;
407
408        float GetAngle( const WorldObject* obj ) const;
409        float GetAngle( const float x, const float y ) const;
410        bool HasInArc( const float arcangle, const WorldObject* obj ) const;
411
412        virtual void SendMessageToSet(WorldPacket *data, bool self);
413        virtual void SendMessageToSetInRange(WorldPacket *data, float dist, bool self);
414        void BuildHeartBeatMsg( WorldPacket *data ) const;
415        void BuildTeleportAckMsg( WorldPacket *data, float x, float y, float z, float ang) const;
416        bool IsBeingTeleported() { return mSemaphoreTeleport; }
417        void SetSemaphoreTeleport(bool semphsetting) { mSemaphoreTeleport = semphsetting; }
418
419        void MonsterSay(const char* text, uint32 language, uint64 TargetGuid);
420        void MonsterYell(const char* text, uint32 language, uint64 TargetGuid);
421        void MonsterTextEmote(const char* text, uint64 TargetGuid, bool IsBossEmote = false);
422        void MonsterWhisper(const char* text, uint64 receiver, bool IsBossWhisper = false);
423        void MonsterSay(int32 textId, uint32 language, uint64 TargetGuid);
424        void MonsterYell(int32 textId, uint32 language, uint64 TargetGuid);
425        void MonsterTextEmote(int32 textId, uint64 TargetGuid, bool IsBossEmote = false);
426        void MonsterWhisper(int32 textId, uint64 receiver, bool IsBossWhisper = false);
427        void BuildMonsterChat(WorldPacket *data, uint8 msgtype, char const* text, uint32 language, char const* name, uint64 TargetGuid) const;
428
429        void SendObjectDeSpawnAnim(uint64 guid);
430
431        virtual void SaveRespawnTime() {}
432
433        uint32 GetInstanceId() const { return m_InstanceId; }
434        void SetInstanceId(uint32 val) { m_InstanceId = val; }
435
436        void AddObjectToRemoveList();
437
438        // main visibility check function in normal case (ignore grey zone distance check)
439        bool isVisibleFor(Player const* u) const { return isVisibleForInState(u,false); }
440
441        // low level function for visibility change code, must be define in all main world object subclasses
442        virtual bool isVisibleForInState(Player const* u, bool inVisibleList) const = 0;
443
444        // Low Level Packets
445        void SendPlaySound(uint32 Sound, bool OnlySelf);
446
447        Map      * GetMap() const;
448        Map const* GetBaseMap() const;
449        Creature* SummonCreature(uint32 id, float x, float y, float z, float ang,TempSummonType spwtype,uint32 despwtime);
450        GameObject* SummonGameObject(uint32 entry, float x, float y, float z, float ang, float rotation0, float rotation1, float rotation2, float rotation3, uint32 respawnTime);
451    protected:
452        explicit WorldObject();
453        std::string m_name;
454
455    private:
456        uint32 m_mapId;
457
458        float m_positionX;
459        float m_positionY;
460        float m_positionZ;
461        float m_orientation;
462
463        bool mSemaphoreTeleport;
464
465        uint32 m_InstanceId;
466};
467#endif
Note: See TracBrowser for help on using the browser.