2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "ByteBuffer.h"
24 #include "UpdateFields.h"
25 #include "UpdateData.h"
26 #include "GameSystem/GridReference.h"
27 #include "ObjectDefines.h"
32 #define CONTACT_DISTANCE 0.5f
33 #define INTERACTION_DISTANCE 5.0f
34 #define ATTACK_DISTANCE 5.0f
35 #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)
36 #define DEFAULT_VISIBILITY_DISTANCE (SIZE_OF_GRID_CELL) // default visible distance
38 #define DEFAULT_WORLD_OBJECT_SIZE 0.388999998569489f // player size, also currently used (correctly?) for any non Unit world objects
42 TYPEMASK_OBJECT
= 0x0001,
43 TYPEMASK_ITEM
= 0x0002,
44 TYPEMASK_CONTAINER
= 0x0006, // TYPEMASK_ITEM | 0x0004
45 TYPEMASK_UNIT
= 0x0008,
46 TYPEMASK_PLAYER
= 0x0010,
47 TYPEMASK_GAMEOBJECT
= 0x0020,
48 TYPEMASK_DYNAMICOBJECT
= 0x0040,
49 TYPEMASK_CORPSE
= 0x0080
59 TYPEID_GAMEOBJECT
= 5,
60 TYPEID_DYNAMICOBJECT
= 6,
64 #define NUM_CLIENT_OBJECT_TYPES 8
66 uint32
GuidHigh2TypeId(uint32 guid_hi
);
70 TEMPSUMMON_TIMED_OR_DEAD_DESPAWN
= 1, // despawns after a specified time OR when the creature disappears
71 TEMPSUMMON_TIMED_OR_CORPSE_DESPAWN
= 2, // despawns after a specified time OR when the creature dies
72 TEMPSUMMON_TIMED_DESPAWN
= 3, // despawns after a specified time
73 TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT
= 4, // despawns after a specified time after the creature is out of combat
74 TEMPSUMMON_CORPSE_DESPAWN
= 5, // despawns instantly after death
75 TEMPSUMMON_CORPSE_TIMED_DESPAWN
= 6, // despawns after a specified time after death
76 TEMPSUMMON_DEAD_DESPAWN
= 7, // despawns when the creature disappears
77 TEMPSUMMON_MANUAL_DESPAWN
= 8 // despawns when UnSummon() is called
82 PHASEMASK_NORMAL
= 0x00000001,
83 PHASEMASK_ANYWHERE
= 0xFFFFFFFF
96 typedef UNORDERED_MAP
<Player
*, UpdateData
> UpdateDataMapType
;
105 explicit WorldLocation(uint32 _mapid
= 0, float _x
= 0, float _y
= 0, float _z
= 0, float _o
= 0)
106 : mapid(_mapid
), coord_x(_x
), coord_y(_y
), coord_z(_z
), orientation(_o
) {}
107 WorldLocation(WorldLocation
const &loc
)
108 : mapid(loc
.mapid
), coord_x(loc
.coord_x
), coord_y(loc
.coord_y
), coord_z(loc
.coord_z
), orientation(loc
.orientation
) {}
111 class MANGOS_DLL_SPEC Object
116 const bool& IsInWorld() const { return m_inWorld
; }
117 virtual void AddToWorld()
124 // synchronize values mirror with values array (changes will send in updatecreate opcode any way
125 ClearUpdateMask(true);
127 virtual void RemoveFromWorld()
129 // if we remove from world then sending changes not required
131 ClearUpdateMask(true);
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 void SetEntry(uint32 entry
) { SetUInt32Value(OBJECT_FIELD_ENTRY
, entry
); }
143 uint8
GetTypeId() const { return m_objectTypeId
; }
144 bool isType(uint16 mask
) const { return (mask
& m_objectType
); }
146 virtual void BuildCreateUpdateBlockForPlayer( UpdateData
*data
, Player
*target
) const;
147 void SendUpdateToPlayer(Player
* player
);
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
&);
154 virtual void DestroyForPlayer( Player
*target
, bool anim
= false ) const;
156 const int32
& GetInt32Value( uint16 index
) const
158 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false) );
159 return m_int32Values
[ index
];
162 const uint32
& GetUInt32Value( uint16 index
) const
164 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false) );
165 return m_uint32Values
[ index
];
168 const uint64
& GetUInt64Value( uint16 index
) const
170 ASSERT( index
+ 1 < m_valuesCount
|| PrintIndexError( index
, false) );
171 return *((uint64
*)&(m_uint32Values
[ index
]));
174 const float& GetFloatValue( uint16 index
) const
176 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false ) );
177 return m_floatValues
[ index
];
180 uint8
GetByteValue( uint16 index
, uint8 offset
) const
182 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false) );
183 ASSERT( offset
< 4 );
184 return *(((uint8
*)&m_uint32Values
[ index
])+offset
);
187 uint16
GetUInt16Value( uint16 index
, uint8 offset
) const
189 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false) );
190 ASSERT( offset
< 2 );
191 return *(((uint16
*)&m_uint32Values
[ index
])+offset
);
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
);
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
);
210 void ApplyPercentModFloatValue(uint16 index
, float val
, bool apply
)
212 val
= val
!= -100.0f
? val
: -99.9f
;
213 SetFloatValue(index
, GetFloatValue(index
) * (apply
?(100.0f
+val
)/100.0f
: 100.0f
/ (100.0f
+val
)) );
216 void SetFlag( uint16 index
, uint32 newFlag
);
217 void RemoveFlag( uint16 index
, uint32 oldFlag
);
219 void ToggleFlag( uint16 index
, uint32 flag
)
221 if(HasFlag(index
, flag
))
222 RemoveFlag(index
, flag
);
224 SetFlag(index
, flag
);
227 bool HasFlag( uint16 index
, uint32 flag
) const
229 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false ) );
230 return (m_uint32Values
[ index
] & flag
) != 0;
233 void SetByteFlag( uint16 index
, uint8 offset
, uint8 newFlag
);
234 void RemoveByteFlag( uint16 index
, uint8 offset
, uint8 newFlag
);
236 void ToggleFlag( uint16 index
, uint8 offset
, uint8 flag
)
238 if(HasByteFlag(index
, offset
, flag
))
239 RemoveByteFlag(index
, offset
, flag
);
241 SetByteFlag(index
, offset
, flag
);
244 bool HasByteFlag( uint16 index
, uint8 offset
, uint8 flag
) const
246 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false ) );
247 ASSERT( offset
< 4 );
248 return (((uint8
*)&m_uint32Values
[index
])[offset
] & flag
) != 0;
251 void ApplyModFlag( uint16 index
, uint32 flag
, bool apply
)
253 if(apply
) SetFlag(index
,flag
); else RemoveFlag(index
,flag
);
256 void SetFlag64( uint16 index
, uint64 newFlag
)
258 uint64 oldval
= GetUInt64Value(index
);
259 uint64 newval
= oldval
| newFlag
;
260 SetUInt64Value(index
,newval
);
263 void RemoveFlag64( uint16 index
, uint64 oldFlag
)
265 uint64 oldval
= GetUInt64Value(index
);
266 uint64 newval
= oldval
& ~oldFlag
;
267 SetUInt64Value(index
,newval
);
270 void ToggleFlag64( uint16 index
, uint64 flag
)
272 if(HasFlag64(index
, flag
))
273 RemoveFlag64(index
, flag
);
275 SetFlag64(index
, flag
);
278 bool HasFlag64( uint16 index
, uint64 flag
) const
280 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false ) );
281 return (GetUInt64Value( index
) & flag
) != 0;
284 void ApplyModFlag64( uint16 index
, uint64 flag
, bool apply
)
286 if(apply
) SetFlag64(index
,flag
); else RemoveFlag64(index
,flag
);
289 void ClearUpdateMask(bool remove
);
290 void SendUpdateObjectToAllExcept(Player
* exceptPlayer
);
292 bool LoadValues(const char* data
);
294 uint16
GetValuesCount() const { return m_valuesCount
; }
296 void InitValues() { _InitValues(); }
298 virtual bool hasQuest(uint32
/* quest_id */) const { return false; }
299 virtual bool hasInvolvedQuest(uint32
/* quest_id */) const { return false; }
305 void _Create (uint32 guidlow
, uint32 entry
, HighGuid guidhigh
);
307 virtual void _SetUpdateBits(UpdateMask
*updateMask
, Player
*target
) const;
309 virtual void _SetCreateBits(UpdateMask
*updateMask
, Player
*target
) const;
310 void _BuildMovementUpdate(ByteBuffer
* data
, uint16 flags
, uint32 flags2
) const;
311 void _BuildValuesUpdate(uint8 updatetype
, ByteBuffer
*data
, UpdateMask
*updateMask
, Player
*target
) const;
315 uint8 m_objectTypeId
;
320 int32
*m_int32Values
;
321 uint32
*m_uint32Values
;
322 float *m_floatValues
;
325 uint32
*m_uint32Values_mirror
;
327 uint16 m_valuesCount
;
329 bool m_objectUpdated
;
334 ByteBuffer m_PackGUID
;
336 // for output helpfull error messages from asserts
337 bool PrintIndexError(uint32 index
, bool set
) const;
338 Object(const Object
&); // prevent generation copy constructor
339 Object
& operator=(Object
const&); // prevent generation assigment operator
342 class MANGOS_DLL_SPEC WorldObject
: public Object
345 virtual ~WorldObject ( ) {}
347 virtual void Update ( uint32
/*time_diff*/ ) { }
349 void _Create( uint32 guidlow
, HighGuid guidhigh
, uint32 phaseMask
);
351 void Relocate(float x
, float y
, float z
, float orientation
)
356 m_orientation
= orientation
;
359 void Relocate(float x
, float y
, float z
)
366 void SetOrientation(float orientation
) { m_orientation
= orientation
; }
368 float GetPositionX( ) const { return m_positionX
; }
369 float GetPositionY( ) const { return m_positionY
; }
370 float GetPositionZ( ) const { return m_positionZ
; }
371 void GetPosition( float &x
, float &y
, float &z
) const
372 { x
= m_positionX
; y
= m_positionY
; z
= m_positionZ
; }
373 void GetPosition( WorldLocation
&loc
) const
374 { loc
.mapid
= m_mapId
; GetPosition(loc
.coord_x
, loc
.coord_y
, loc
.coord_z
); loc
.orientation
= GetOrientation(); }
375 float GetOrientation( ) const { return m_orientation
; }
376 void GetNearPoint2D( float &x
, float &y
, float distance
, float absAngle
) const;
377 void GetNearPoint( WorldObject
const* searcher
, float &x
, float &y
, float &z
, float searcher_size
, float distance2d
,float absAngle
) const;
378 void GetClosePoint(float &x
, float &y
, float &z
, float size
, float distance2d
= 0, float angle
= 0) const
380 // angle calculated from current orientation
381 GetNearPoint(NULL
,x
,y
,z
,size
,distance2d
,GetOrientation() + angle
);
383 void GetContactPoint( const WorldObject
* obj
, float &x
, float &y
, float &z
, float distance2d
= CONTACT_DISTANCE
) const
385 // angle to face `obj` to `this` using distance includes size of `obj`
386 GetNearPoint(obj
,x
,y
,z
,obj
->GetObjectSize(),distance2d
,GetAngle( obj
));
389 float GetObjectSize() const
391 return ( m_valuesCount
> UNIT_FIELD_BOUNDINGRADIUS
) ? m_floatValues
[UNIT_FIELD_BOUNDINGRADIUS
] : DEFAULT_WORLD_OBJECT_SIZE
;
393 bool IsPositionValid() const;
394 void UpdateGroundPositionZ(float x
, float y
, float &z
) const;
396 void GetRandomPoint( float x
, float y
, float z
, float distance
, float &rand_x
, float &rand_y
, float &rand_z
) const;
398 uint32
GetMapId() const { return m_mapId
; }
399 uint32
GetInstanceId() const { return m_InstanceId
; }
401 virtual void SetPhaseMask(uint32 newPhaseMask
, bool update
);
402 uint32
GetPhaseMask() const { return m_phaseMask
; }
403 bool InSamePhase(WorldObject
const* obj
) const { return InSamePhase(obj
->GetPhaseMask()); }
404 bool InSamePhase(uint32 phasemask
) const { return (GetPhaseMask() & phasemask
); }
406 uint32
GetZoneId() const;
407 uint32
GetAreaId() const;
408 void GetZoneAndAreaId(uint32
& zoneid
, uint32
& areaid
) const;
410 InstanceData
* GetInstanceData();
412 const char* GetName() const { return m_name
.c_str(); }
413 void SetName(const std::string
& newname
) { m_name
=newname
; }
415 virtual const char* GetNameForLocaleIdx(int32
/*locale_idx*/) const { return GetName(); }
417 float GetDistance( const WorldObject
* obj
) const;
418 float GetDistance(float x
, float y
, float z
) const;
419 float GetDistance2d(const WorldObject
* obj
) const;
420 float GetDistance2d(float x
, float y
) const;
421 float GetDistanceZ(const WorldObject
* obj
) const;
422 bool IsInMap(const WorldObject
* obj
) const
424 return IsInWorld() && obj
->IsInWorld() && (GetMap() == obj
->GetMap()) && InSamePhase(obj
);
426 bool IsWithinDist3d(float x
, float y
, float z
, float dist2compare
) const;
427 bool IsWithinDist2d(float x
, float y
, float dist2compare
) const;
428 bool _IsWithinDist(WorldObject
const* obj
, float dist2compare
, bool is3D
) const;
429 bool IsWithinDist(WorldObject
const* obj
, float dist2compare
, bool is3D
= true) const
430 // use only if you will sure about placing both object at same map
432 return obj
&& _IsWithinDist(obj
,dist2compare
,is3D
);
434 bool IsWithinDistInMap(WorldObject
const* obj
, float dist2compare
, bool is3D
= true) const
436 return obj
&& IsInMap(obj
) && _IsWithinDist(obj
,dist2compare
,is3D
);
438 bool IsWithinLOS(float x
, float y
, float z
) const;
439 bool IsWithinLOSInMap(const WorldObject
* obj
) const;
440 bool GetDistanceOrder(WorldObject
const* obj1
, WorldObject
const* obj2
, bool is3D
= true) const;
441 bool IsInRange(WorldObject
const* obj
, float minRange
, float maxRange
, bool is3D
= true) const;
442 bool IsInRange2d(float x
, float y
, float minRange
, float maxRange
) const;
443 bool IsInRange3d(float x
, float y
, float z
, float minRange
, float maxRange
) const;
445 float GetAngle( const WorldObject
* obj
) const;
446 float GetAngle( const float x
, const float y
) const;
447 bool HasInArc( const float arcangle
, const WorldObject
* obj
) const;
448 bool isInFrontInMap(WorldObject
const* target
,float distance
, float arc
= M_PI
) const;
449 bool isInBackInMap(WorldObject
const* target
, float distance
, float arc
= M_PI
) const;
451 virtual void CleanupsBeforeDelete(); // used in destructor or explicitly before mass creature delete to remove cross-references to already deleted units
453 virtual void SendMessageToSet(WorldPacket
*data
, bool self
);
454 virtual void SendMessageToSetInRange(WorldPacket
*data
, float dist
, bool self
);
456 void MonsterSay(const char* text
, uint32 language
, uint64 TargetGuid
);
457 void MonsterYell(const char* text
, uint32 language
, uint64 TargetGuid
);
458 void MonsterTextEmote(const char* text
, uint64 TargetGuid
, bool IsBossEmote
= false);
459 void MonsterWhisper(const char* text
, uint64 receiver
, bool IsBossWhisper
= false);
460 void MonsterSay(int32 textId
, uint32 language
, uint64 TargetGuid
);
461 void MonsterYell(int32 textId
, uint32 language
, uint64 TargetGuid
);
462 void MonsterTextEmote(int32 textId
, uint64 TargetGuid
, bool IsBossEmote
= false);
463 void MonsterWhisper(int32 textId
, uint64 receiver
, bool IsBossWhisper
= false);
464 void MonsterYellToZone(int32 textId
, uint32 language
, uint64 TargetGuid
);
465 void BuildMonsterChat(WorldPacket
*data
, uint8 msgtype
, char const* text
, uint32 language
, char const* name
, uint64 TargetGuid
) const;
467 void PlayDistanceSound(uint32 sound_id
, Player
* target
= NULL
);
468 void PlayDirectSound(uint32 sound_id
, Player
* target
= NULL
);
470 void SendObjectDeSpawnAnim(uint64 guid
);
472 virtual void SaveRespawnTime() {}
473 void AddObjectToRemoveList();
475 // main visibility check function in normal case (ignore grey zone distance check)
476 bool isVisibleFor(Player
const* u
, WorldObject
const* viewPoint
) const { return isVisibleForInState(u
,viewPoint
,false); }
478 // low level function for visibility change code, must be define in all main world object subclasses
479 virtual bool isVisibleForInState(Player
const* u
, WorldObject
const* viewPoint
, bool inVisibleList
) const = 0;
481 void SetMap(Map
* map
);
482 Map
* GetMap() const { ASSERT(m_currMap
); return m_currMap
; }
483 //used to check all object's GetMap() calls when object is not in world!
484 void ResetMap() { m_currMap
= NULL
; }
486 //this function should be removed in nearest time...
487 Map
const* GetBaseMap() const;
489 Creature
* SummonCreature(uint32 id
, float x
, float y
, float z
, float ang
,TempSummonType spwtype
,uint32 despwtime
);
492 explicit WorldObject();
495 //these functions are used mostly for Relocate() and Corpse/Player specific stuff...
496 //use them ONLY in LoadFromDB()/Create() funcs and nowhere else!
497 //mapId/instanceId should be set in SetMap() function!
498 void SetLocationMapId(uint32 _mapId
) { m_mapId
= _mapId
; }
499 void SetLocationInstanceId(uint32 _instanceId
) { m_InstanceId
= _instanceId
; }
502 Map
* m_currMap
; //current object's Map location
504 uint32 m_mapId
; // object at map with map_id
505 uint32 m_InstanceId
; // in map copy with instance id
506 uint32 m_phaseMask
; // in area phase state