2 * Copyright (C) 2005-2010 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 333.0f // max distance for visible object show, limited in 333 yards
36 #define DEFAULT_VISIBILITY_DISTANCE 90.0f // default visible distance, 90 yards on continents
37 #define DEFAULT_VISIBILITY_INSTANCE 120.0f // default visible distance in instances, 120 yards
38 #define DEFAULT_VISIBILITY_BGARENAS 180.0f // default visible distance in BG/Arenas, 180 yards
40 #define DEFAULT_WORLD_OBJECT_SIZE 0.388999998569489f // player size, also currently used (correctly?) for any non Unit world objects
41 #define MAX_STEALTH_DETECT_RANGE 45.0f
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
62 TYPEID_GAMEOBJECT
= 5,
63 TYPEID_DYNAMICOBJECT
= 6,
67 #define NUM_CLIENT_OBJECT_TYPES 8
69 uint32
GuidHigh2TypeId(uint32 guid_hi
);
73 TEMPSUMMON_TIMED_OR_DEAD_DESPAWN
= 1, // despawns after a specified time OR when the creature disappears
74 TEMPSUMMON_TIMED_OR_CORPSE_DESPAWN
= 2, // despawns after a specified time OR when the creature dies
75 TEMPSUMMON_TIMED_DESPAWN
= 3, // despawns after a specified time
76 TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT
= 4, // despawns after a specified time after the creature is out of combat
77 TEMPSUMMON_CORPSE_DESPAWN
= 5, // despawns instantly after death
78 TEMPSUMMON_CORPSE_TIMED_DESPAWN
= 6, // despawns after a specified time after death
79 TEMPSUMMON_DEAD_DESPAWN
= 7, // despawns when the creature disappears
80 TEMPSUMMON_MANUAL_DESPAWN
= 8 // despawns when UnSummon() is called
85 PHASEMASK_NORMAL
= 0x00000001,
86 PHASEMASK_ANYWHERE
= 0xFFFFFFFF
99 typedef UNORDERED_MAP
<Player
*, UpdateData
> UpdateDataMapType
;
108 explicit WorldLocation(uint32 _mapid
= 0, float _x
= 0, float _y
= 0, float _z
= 0, float _o
= 0)
109 : mapid(_mapid
), coord_x(_x
), coord_y(_y
), coord_z(_z
), orientation(_o
) {}
110 WorldLocation(WorldLocation
const &loc
)
111 : mapid(loc
.mapid
), coord_x(loc
.coord_x
), coord_y(loc
.coord_y
), coord_z(loc
.coord_z
), orientation(loc
.orientation
) {}
114 class MANGOS_DLL_SPEC Object
119 const bool& IsInWorld() const { return m_inWorld
; }
120 virtual void AddToWorld()
127 // synchronize values mirror with values array (changes will send in updatecreate opcode any way
128 ClearUpdateMask(false); // false - we can't have update dat in update queue before adding to world
130 virtual void RemoveFromWorld()
132 // if we remove from world then sending changes not required
133 ClearUpdateMask(true);
137 const uint64
& GetGUID() const { return GetUInt64Value(0); }
138 uint32
GetGUIDLow() const { return GUID_LOPART(GetUInt64Value(0)); }
139 uint32
GetGUIDMid() const { return GUID_ENPART(GetUInt64Value(0)); }
140 uint32
GetGUIDHigh() const { return GUID_HIPART(GetUInt64Value(0)); }
141 const ByteBuffer
& GetPackGUID() const { return m_PackGUID
; }
142 uint32
GetEntry() const { return GetUInt32Value(OBJECT_FIELD_ENTRY
); }
143 void SetEntry(uint32 entry
) { SetUInt32Value(OBJECT_FIELD_ENTRY
, entry
); }
145 uint8
GetTypeId() const { return m_objectTypeId
; }
146 bool isType(uint16 mask
) const { return (mask
& m_objectType
); }
148 virtual void BuildCreateUpdateBlockForPlayer( UpdateData
*data
, Player
*target
) const;
149 void SendCreateUpdateToPlayer(Player
* player
);
151 // must be overwrite in appropriate subclasses (WorldObject, Item currently), or will crash
152 virtual void AddToClientUpdateList();
153 virtual void RemoveFromClientUpdateList();
154 virtual void BuildUpdateData(UpdateDataMapType
& update_players
);
156 void BuildValuesUpdateBlockForPlayer( UpdateData
*data
, Player
*target
) const;
157 void BuildOutOfRangeUpdateBlock( UpdateData
*data
) const;
158 void BuildMovementUpdateBlock( UpdateData
* data
, uint16 flags
= 0 ) const;
160 virtual void DestroyForPlayer( Player
*target
, bool anim
= false ) const;
162 const int32
& GetInt32Value( uint16 index
) const
164 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false) );
165 return m_int32Values
[ index
];
168 const uint32
& GetUInt32Value( uint16 index
) const
170 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false) );
171 return m_uint32Values
[ index
];
174 const uint64
& GetUInt64Value( uint16 index
) const
176 ASSERT( index
+ 1 < m_valuesCount
|| PrintIndexError( index
, false) );
177 return *((uint64
*)&(m_uint32Values
[ index
]));
180 const float& GetFloatValue( uint16 index
) const
182 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false ) );
183 return m_floatValues
[ index
];
186 uint8
GetByteValue( uint16 index
, uint8 offset
) const
188 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false) );
189 ASSERT( offset
< 4 );
190 return *(((uint8
*)&m_uint32Values
[ index
])+offset
);
193 uint16
GetUInt16Value( uint16 index
, uint8 offset
) const
195 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false) );
196 ASSERT( offset
< 2 );
197 return *(((uint16
*)&m_uint32Values
[ index
])+offset
);
200 void SetInt32Value( uint16 index
, int32 value
);
201 void SetUInt32Value( uint16 index
, uint32 value
);
202 void SetUInt64Value( uint16 index
, const uint64
&value
);
203 void SetFloatValue( uint16 index
, float value
);
204 void SetByteValue( uint16 index
, uint8 offset
, uint8 value
);
205 void SetUInt16Value( uint16 index
, uint8 offset
, uint16 value
);
206 void SetInt16Value( uint16 index
, uint8 offset
, int16 value
) { SetUInt16Value(index
,offset
,(uint16
)value
); }
207 void SetStatFloatValue( uint16 index
, float value
);
208 void SetStatInt32Value( uint16 index
, int32 value
);
210 void ApplyModUInt32Value(uint16 index
, int32 val
, bool apply
);
211 void ApplyModInt32Value(uint16 index
, int32 val
, bool apply
);
212 void ApplyModUInt64Value(uint16 index
, int32 val
, bool apply
);
213 void ApplyModPositiveFloatValue( uint16 index
, float val
, bool apply
);
214 void ApplyModSignedFloatValue( uint16 index
, float val
, bool apply
);
216 void ApplyPercentModFloatValue(uint16 index
, float val
, bool apply
)
218 val
= val
!= -100.0f
? val
: -99.9f
;
219 SetFloatValue(index
, GetFloatValue(index
) * (apply
?(100.0f
+val
)/100.0f
: 100.0f
/ (100.0f
+val
)) );
222 void SetFlag( uint16 index
, uint32 newFlag
);
223 void RemoveFlag( uint16 index
, uint32 oldFlag
);
225 void ToggleFlag( uint16 index
, uint32 flag
)
227 if(HasFlag(index
, flag
))
228 RemoveFlag(index
, flag
);
230 SetFlag(index
, flag
);
233 bool HasFlag( uint16 index
, uint32 flag
) const
235 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false ) );
236 return (m_uint32Values
[ index
] & flag
) != 0;
239 void SetByteFlag( uint16 index
, uint8 offset
, uint8 newFlag
);
240 void RemoveByteFlag( uint16 index
, uint8 offset
, uint8 newFlag
);
242 void ToggleFlag( uint16 index
, uint8 offset
, uint8 flag
)
244 if(HasByteFlag(index
, offset
, flag
))
245 RemoveByteFlag(index
, offset
, flag
);
247 SetByteFlag(index
, offset
, flag
);
250 bool HasByteFlag( uint16 index
, uint8 offset
, uint8 flag
) const
252 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false ) );
253 ASSERT( offset
< 4 );
254 return (((uint8
*)&m_uint32Values
[index
])[offset
] & flag
) != 0;
257 void ApplyModFlag( uint16 index
, uint32 flag
, bool apply
)
259 if(apply
) SetFlag(index
,flag
); else RemoveFlag(index
,flag
);
262 void SetFlag64( uint16 index
, uint64 newFlag
)
264 uint64 oldval
= GetUInt64Value(index
);
265 uint64 newval
= oldval
| newFlag
;
266 SetUInt64Value(index
,newval
);
269 void RemoveFlag64( uint16 index
, uint64 oldFlag
)
271 uint64 oldval
= GetUInt64Value(index
);
272 uint64 newval
= oldval
& ~oldFlag
;
273 SetUInt64Value(index
,newval
);
276 void ToggleFlag64( uint16 index
, uint64 flag
)
278 if(HasFlag64(index
, flag
))
279 RemoveFlag64(index
, flag
);
281 SetFlag64(index
, flag
);
284 bool HasFlag64( uint16 index
, uint64 flag
) const
286 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, false ) );
287 return (GetUInt64Value( index
) & flag
) != 0;
290 void ApplyModFlag64( uint16 index
, uint64 flag
, bool apply
)
292 if(apply
) SetFlag64(index
,flag
); else RemoveFlag64(index
, flag
);
295 void ClearUpdateMask(bool remove
);
297 bool LoadValues(const char* data
);
299 uint16
GetValuesCount() const { return m_valuesCount
; }
301 void InitValues() { _InitValues(); }
303 virtual bool hasQuest(uint32
/* quest_id */) const { return false; }
304 virtual bool hasInvolvedQuest(uint32
/* quest_id */) const { return false; }
310 void _Create (uint32 guidlow
, uint32 entry
, HighGuid guidhigh
);
312 virtual void _SetUpdateBits(UpdateMask
*updateMask
, Player
*target
) const;
314 virtual void _SetCreateBits(UpdateMask
*updateMask
, Player
*target
) const;
316 void BuildMovementUpdate(ByteBuffer
* data
, uint16 updateFlags
) const;
317 void BuildValuesUpdate(uint8 updatetype
, ByteBuffer
*data
, UpdateMask
*updateMask
, Player
*target
) const;
318 void BuildUpdateDataForPlayer(Player
* pl
, UpdateDataMapType
& update_players
);
322 uint8 m_objectTypeId
;
327 int32
*m_int32Values
;
328 uint32
*m_uint32Values
;
329 float *m_floatValues
;
332 uint32
*m_uint32Values_mirror
;
334 uint16 m_valuesCount
;
336 bool m_objectUpdated
;
341 ByteBuffer m_PackGUID
;
343 // for output helpfull error messages from asserts
344 bool PrintIndexError(uint32 index
, bool set
) const;
345 Object(const Object
&); // prevent generation copy constructor
346 Object
& operator=(Object
const&); // prevent generation assigment operator
349 struct WorldObjectChangeAccumulator
;
351 class MANGOS_DLL_SPEC WorldObject
: public Object
353 friend struct WorldObjectChangeAccumulator
;
356 virtual ~WorldObject ( ) {}
358 virtual void Update ( uint32
/*time_diff*/ ) { }
360 void _Create( uint32 guidlow
, HighGuid guidhigh
, uint32 phaseMask
);
362 void Relocate(float x
, float y
, float z
, float orientation
);
363 void Relocate(float x
, float y
, float z
);
365 void SetOrientation(float orientation
) { m_orientation
= orientation
; }
367 float GetPositionX( ) const { return m_positionX
; }
368 float GetPositionY( ) const { return m_positionY
; }
369 float GetPositionZ( ) const { return m_positionZ
; }
370 void GetPosition( float &x
, float &y
, float &z
) const
371 { x
= m_positionX
; y
= m_positionY
; z
= m_positionZ
; }
372 void GetPosition( WorldLocation
&loc
) const
373 { loc
.mapid
= m_mapId
; GetPosition(loc
.coord_x
, loc
.coord_y
, loc
.coord_z
); loc
.orientation
= GetOrientation(); }
374 float GetOrientation( ) const { return m_orientation
; }
375 void GetNearPoint2D( float &x
, float &y
, float distance
, float absAngle
) const;
376 void GetNearPoint( WorldObject
const* searcher
, float &x
, float &y
, float &z
, float searcher_size
, float distance2d
,float absAngle
) const;
377 void GetClosePoint(float &x
, float &y
, float &z
, float size
, float distance2d
= 0, float angle
= 0) const
379 // angle calculated from current orientation
380 GetNearPoint(NULL
,x
,y
,z
,size
,distance2d
,GetOrientation() + angle
);
382 void GetContactPoint( const WorldObject
* obj
, float &x
, float &y
, float &z
, float distance2d
= CONTACT_DISTANCE
) const
384 // angle to face `obj` to `this` using distance includes size of `obj`
385 GetNearPoint(obj
,x
,y
,z
,obj
->GetObjectSize(),distance2d
,GetAngle( obj
));
388 float GetObjectSize() const
390 return ( m_valuesCount
> UNIT_FIELD_BOUNDINGRADIUS
) ? m_floatValues
[UNIT_FIELD_BOUNDINGRADIUS
] : DEFAULT_WORLD_OBJECT_SIZE
;
392 bool IsPositionValid() const;
393 void UpdateGroundPositionZ(float x
, float y
, float &z
) const;
395 void GetRandomPoint( float x
, float y
, float z
, float distance
, float &rand_x
, float &rand_y
, float &rand_z
) const;
397 uint32
GetMapId() const { return m_mapId
; }
398 uint32
GetInstanceId() const { return m_InstanceId
; }
400 virtual void SetPhaseMask(uint32 newPhaseMask
, bool update
);
401 uint32
GetPhaseMask() const { return m_phaseMask
; }
402 bool InSamePhase(WorldObject
const* obj
) const { return InSamePhase(obj
->GetPhaseMask()); }
403 bool InSamePhase(uint32 phasemask
) const { return (GetPhaseMask() & phasemask
); }
405 uint32
GetZoneId() const;
406 uint32
GetAreaId() const;
407 void GetZoneAndAreaId(uint32
& zoneid
, uint32
& areaid
) const;
409 InstanceData
* GetInstanceData();
411 const char* GetName() const { return m_name
.c_str(); }
412 void SetName(const std::string
& newname
) { m_name
=newname
; }
414 virtual const char* GetNameForLocaleIdx(int32
/*locale_idx*/) const { return GetName(); }
416 float GetDistance( const WorldObject
* obj
) const;
417 float GetDistance(float x
, float y
, float z
) const;
418 float GetDistance2d(const WorldObject
* obj
) const;
419 float GetDistance2d(float x
, float y
) const;
420 float GetDistanceZ(const WorldObject
* obj
) const;
421 bool IsInMap(const WorldObject
* obj
) const
423 return IsInWorld() && obj
->IsInWorld() && (GetMap() == obj
->GetMap()) && InSamePhase(obj
);
425 bool IsWithinDist3d(float x
, float y
, float z
, float dist2compare
) const;
426 bool IsWithinDist2d(float x
, float y
, float dist2compare
) const;
427 bool _IsWithinDist(WorldObject
const* obj
, float dist2compare
, bool is3D
) const;
429 // use only if you will sure about placing both object at same map
430 bool IsWithinDist(WorldObject
const* obj
, float dist2compare
, bool is3D
= true) const
432 return obj
&& _IsWithinDist(obj
,dist2compare
,is3D
);
435 bool IsWithinDistInMap(WorldObject
const* obj
, float dist2compare
, bool is3D
= true) const
437 return obj
&& IsInMap(obj
) && _IsWithinDist(obj
,dist2compare
,is3D
);
439 bool IsWithinLOS(float x
, float y
, float z
) const;
440 bool IsWithinLOSInMap(const WorldObject
* obj
) const;
441 bool GetDistanceOrder(WorldObject
const* obj1
, WorldObject
const* obj2
, bool is3D
= true) const;
442 bool IsInRange(WorldObject
const* obj
, float minRange
, float maxRange
, bool is3D
= true) const;
443 bool IsInRange2d(float x
, float y
, float minRange
, float maxRange
) const;
444 bool IsInRange3d(float x
, float y
, float z
, float minRange
, float maxRange
) const;
446 float GetAngle( const WorldObject
* obj
) const;
447 float GetAngle( const float x
, const float y
) const;
448 bool HasInArc( const float arcangle
, const WorldObject
* obj
) const;
449 bool isInFrontInMap(WorldObject
const* target
,float distance
, float arc
= M_PI
) const;
450 bool isInBackInMap(WorldObject
const* target
, float distance
, float arc
= M_PI
) const;
451 bool isInFront(WorldObject
const* target
,float distance
, float arc
= M_PI
) const;
452 bool isInBack(WorldObject
const* target
, float distance
, float arc
= M_PI
) const;
454 virtual void CleanupsBeforeDelete(); // used in destructor or explicitly before mass creature delete to remove cross-references to already deleted units
456 virtual void SendMessageToSet(WorldPacket
*data
, bool self
);
457 virtual void SendMessageToSetInRange(WorldPacket
*data
, float dist
, bool self
);
459 void MonsterSay(const char* text
, uint32 language
, uint64 TargetGuid
);
460 void MonsterYell(const char* text
, uint32 language
, uint64 TargetGuid
);
461 void MonsterTextEmote(const char* text
, uint64 TargetGuid
, bool IsBossEmote
= false);
462 void MonsterWhisper(const char* text
, uint64 receiver
, bool IsBossWhisper
= false);
463 void MonsterSay(int32 textId
, uint32 language
, uint64 TargetGuid
);
464 void MonsterYell(int32 textId
, uint32 language
, uint64 TargetGuid
);
465 void MonsterTextEmote(int32 textId
, uint64 TargetGuid
, bool IsBossEmote
= false);
466 void MonsterWhisper(int32 textId
, uint64 receiver
, bool IsBossWhisper
= false);
467 void MonsterYellToZone(int32 textId
, uint32 language
, uint64 TargetGuid
);
468 void BuildMonsterChat(WorldPacket
*data
, uint8 msgtype
, char const* text
, uint32 language
, char const* name
, uint64 TargetGuid
) const;
470 void PlayDistanceSound(uint32 sound_id
, Player
* target
= NULL
);
471 void PlayDirectSound(uint32 sound_id
, Player
* target
= NULL
);
473 void SendObjectDeSpawnAnim(uint64 guid
);
474 void SendGameObjectCustomAnim(uint64 guid
);
476 virtual bool IsHostileTo(Unit
const* unit
) const =0;
477 virtual bool IsFriendlyTo(Unit
const* unit
) const =0;
478 bool IsControlledByPlayer() const;
480 virtual void SaveRespawnTime() {}
481 void AddObjectToRemoveList();
483 void UpdateObjectVisibility();
485 // main visibility check function in normal case (ignore grey zone distance check)
486 bool isVisibleFor(Player
const* u
, WorldObject
const* viewPoint
) const { return isVisibleForInState(u
,viewPoint
,false); }
488 // low level function for visibility change code, must be define in all main world object subclasses
489 virtual bool isVisibleForInState(Player
const* u
, WorldObject
const* viewPoint
, bool inVisibleList
) const = 0;
491 void SetMap(Map
* map
);
492 Map
* GetMap() const { ASSERT(m_currMap
); return m_currMap
; }
493 //used to check all object's GetMap() calls when object is not in world!
494 void ResetMap() { m_currMap
= NULL
; }
496 //this function should be removed in nearest time...
497 Map
const* GetBaseMap() const;
499 void AddToClientUpdateList();
500 void RemoveFromClientUpdateList();
501 void BuildUpdateData(UpdateDataMapType
&);
503 Creature
* SummonCreature(uint32 id
, float x
, float y
, float z
, float ang
,TempSummonType spwtype
,uint32 despwtime
);
505 explicit WorldObject();
507 //these functions are used mostly for Relocate() and Corpse/Player specific stuff...
508 //use them ONLY in LoadFromDB()/Create() funcs and nowhere else!
509 //mapId/instanceId should be set in SetMap() function!
510 void SetLocationMapId(uint32 _mapId
) { m_mapId
= _mapId
; }
511 void SetLocationInstanceId(uint32 _instanceId
) { m_InstanceId
= _instanceId
; }
516 Map
* m_currMap
; //current object's Map location
518 uint32 m_mapId
; // object at map with map_id
519 uint32 m_InstanceId
; // in map copy with instance id
520 uint32 m_phaseMask
; // in area phase state