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

Revision 120, 18.2 kB (checked in by yumileroy, 17 years ago)

[svn] * Allow WorldObjects? to keep the grid active, and prevent it from being unloaded. This can be done through calling WorldObject::setActive(bool) from the scripting library. Note that entire instances are still unloaded if no player is present on that map to save resources. This behavior can be changed if the need arises.

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