[7829] Rename 3d (x,y,.. versions of IsWithinDist/IsWithinDist functions with adding...
[getmangos.git] / src / game / Object.h
blobb42acbd04e805c481dc15a15a8e2873f2e93f8eb
1 /*
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
19 #ifndef _OBJECT_H
20 #define _OBJECT_H
22 #include "Common.h"
23 #include "ByteBuffer.h"
24 #include "UpdateFields.h"
25 #include "UpdateData.h"
26 #include "GameSystem/GridReference.h"
27 #include "ObjectDefines.h"
29 #include <set>
30 #include <string>
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
40 enum TypeMask
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,
50 TYPEMASK_AIGROUP = 0x0100,
51 TYPEMASK_AREATRIGGER = 0x0200
54 enum TypeID
56 TYPEID_OBJECT = 0,
57 TYPEID_ITEM = 1,
58 TYPEID_CONTAINER = 2,
59 TYPEID_UNIT = 3,
60 TYPEID_PLAYER = 4,
61 TYPEID_GAMEOBJECT = 5,
62 TYPEID_DYNAMICOBJECT = 6,
63 TYPEID_CORPSE = 7,
64 TYPEID_AIGROUP = 8,
65 TYPEID_AREATRIGGER = 9
67 #define MAX_TYPEID 10
69 uint32 GuidHigh2TypeId(uint32 guid_hi);
71 enum TempSummonType
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
83 enum PhaseMasks
85 PHASEMASK_NORMAL = 0x00000001,
86 PHASEMASK_ANYWHERE = 0xFFFFFFFF
89 class WorldPacket;
90 class UpdateData;
91 class ByteBuffer;
92 class WorldSession;
93 class Creature;
94 class Player;
95 class Map;
96 class UpdateMask;
97 class InstanceData;
99 typedef UNORDERED_MAP<Player*, UpdateData> UpdateDataMapType;
101 struct WorldLocation
103 uint32 mapid;
104 float x;
105 float y;
106 float z;
107 float o;
108 explicit WorldLocation(uint32 _mapid = 0, float _x = 0, float _y = 0, float _z = 0, float _o = 0)
109 : mapid(_mapid), x(_x), y(_y), z(_z), o(_o) {}
110 WorldLocation(WorldLocation const &loc)
111 : mapid(loc.mapid), x(loc.x), y(loc.y), z(loc.z), o(loc.o) {}
114 class MANGOS_DLL_SPEC Object
116 public:
117 virtual ~Object ( );
119 const bool& IsInWorld() const { return m_inWorld; }
120 virtual void AddToWorld()
122 if(m_inWorld)
123 return;
125 m_inWorld = true;
127 // synchronize values mirror with values array (changes will send in updatecreate opcode any way
128 ClearUpdateMask(true);
130 virtual void RemoveFromWorld()
132 // if we remove from world then sending changes not required
133 if(m_uint32Values)
134 ClearUpdateMask(true);
135 m_inWorld = false;
138 const uint64& GetGUID() const { return GetUInt64Value(0); }
139 uint32 GetGUIDLow() const { return GUID_LOPART(GetUInt64Value(0)); }
140 uint32 GetGUIDMid() const { return GUID_ENPART(GetUInt64Value(0)); }
141 uint32 GetGUIDHigh() const { return GUID_HIPART(GetUInt64Value(0)); }
142 const ByteBuffer& GetPackGUID() const { return m_PackGUID; }
143 uint32 GetEntry() const { return GetUInt32Value(OBJECT_FIELD_ENTRY); }
144 void SetEntry(uint32 entry) { SetUInt32Value(OBJECT_FIELD_ENTRY, entry); }
146 uint8 GetTypeId() const { return m_objectTypeId; }
147 bool isType(uint16 mask) const { return (mask & m_objectType); }
149 virtual void BuildCreateUpdateBlockForPlayer( UpdateData *data, Player *target ) const;
150 void SendUpdateToPlayer(Player* player);
152 void BuildValuesUpdateBlockForPlayer( UpdateData *data, Player *target ) const;
153 void BuildOutOfRangeUpdateBlock( UpdateData *data ) const;
154 void BuildMovementUpdateBlock( UpdateData * data, uint32 flags = 0 ) const;
155 void BuildUpdate(UpdateDataMapType &);
157 virtual void DestroyForPlayer( Player *target ) const;
159 const int32& GetInt32Value( uint16 index ) const
161 ASSERT( index < m_valuesCount || PrintIndexError( index , false) );
162 return m_int32Values[ index ];
165 const uint32& GetUInt32Value( uint16 index ) const
167 ASSERT( index < m_valuesCount || PrintIndexError( index , false) );
168 return m_uint32Values[ index ];
171 const uint64& GetUInt64Value( uint16 index ) const
173 ASSERT( index + 1 < m_valuesCount || PrintIndexError( index , false) );
174 return *((uint64*)&(m_uint32Values[ index ]));
177 const float& GetFloatValue( uint16 index ) const
179 ASSERT( index < m_valuesCount || PrintIndexError( index , false ) );
180 return m_floatValues[ index ];
183 uint8 GetByteValue( uint16 index, uint8 offset) const
185 ASSERT( index < m_valuesCount || PrintIndexError( index , false) );
186 ASSERT( offset < 4 );
187 return *(((uint8*)&m_uint32Values[ index ])+offset);
190 uint16 GetUInt16Value( uint16 index, uint8 offset) const
192 ASSERT( index < m_valuesCount || PrintIndexError( index , false) );
193 ASSERT( offset < 2 );
194 return *(((uint16*)&m_uint32Values[ index ])+offset);
197 void SetInt32Value( uint16 index, int32 value );
198 void SetUInt32Value( uint16 index, uint32 value );
199 void SetUInt64Value( uint16 index, const uint64 &value );
200 void SetFloatValue( uint16 index, float value );
201 void SetByteValue( uint16 index, uint8 offset, uint8 value );
202 void SetUInt16Value( uint16 index, uint8 offset, uint16 value );
203 void SetInt16Value( uint16 index, uint8 offset, int16 value ) { SetUInt16Value(index,offset,(uint16)value); }
204 void SetStatFloatValue( uint16 index, float value);
205 void SetStatInt32Value( uint16 index, int32 value);
207 void ApplyModUInt32Value(uint16 index, int32 val, bool apply);
208 void ApplyModInt32Value(uint16 index, int32 val, bool apply);
209 void ApplyModUInt64Value(uint16 index, int32 val, bool apply);
210 void ApplyModPositiveFloatValue( uint16 index, float val, bool apply);
211 void ApplyModSignedFloatValue( uint16 index, float val, bool apply);
213 void ApplyPercentModFloatValue(uint16 index, float val, bool apply)
215 val = val != -100.0f ? val : -99.9f ;
216 SetFloatValue(index, GetFloatValue(index) * (apply?(100.0f+val)/100.0f : 100.0f / (100.0f+val)) );
219 void SetFlag( uint16 index, uint32 newFlag );
220 void RemoveFlag( uint16 index, uint32 oldFlag );
222 void ToggleFlag( uint16 index, uint32 flag)
224 if(HasFlag(index, flag))
225 RemoveFlag(index, flag);
226 else
227 SetFlag(index, flag);
230 bool HasFlag( uint16 index, uint32 flag ) const
232 ASSERT( index < m_valuesCount || PrintIndexError( index , false ) );
233 return (m_uint32Values[ index ] & flag) != 0;
236 void SetByteFlag( uint16 index, uint8 offset, uint8 newFlag );
237 void RemoveByteFlag( uint16 index, uint8 offset, uint8 newFlag );
239 void ToggleFlag( uint16 index, uint8 offset, uint8 flag )
241 if(HasByteFlag(index, offset, flag))
242 RemoveByteFlag(index, offset, flag);
243 else
244 SetByteFlag(index, offset, flag);
247 bool HasByteFlag( uint16 index, uint8 offset, uint8 flag ) const
249 ASSERT( index < m_valuesCount || PrintIndexError( index , false ) );
250 ASSERT( offset < 4 );
251 return (((uint8*)&m_uint32Values[index])[offset] & flag) != 0;
254 void ApplyModFlag( uint16 index, uint32 flag, bool apply)
256 if(apply) SetFlag(index,flag); else RemoveFlag(index,flag);
259 void SetFlag64( uint16 index, uint64 newFlag )
261 uint64 oldval = GetUInt64Value(index);
262 uint64 newval = oldval | newFlag;
263 SetUInt64Value(index,newval);
266 void RemoveFlag64( uint16 index, uint64 oldFlag )
268 uint64 oldval = GetUInt64Value(index);
269 uint64 newval = oldval & ~oldFlag;
270 SetUInt64Value(index,newval);
273 void ToggleFlag64( uint16 index, uint64 flag)
275 if(HasFlag64(index, flag))
276 RemoveFlag64(index, flag);
277 else
278 SetFlag64(index, flag);
281 bool HasFlag64( uint16 index, uint64 flag ) const
283 ASSERT( index < m_valuesCount || PrintIndexError( index , false ) );
284 return (GetUInt64Value( index ) & flag) != 0;
287 void ApplyModFlag64( uint16 index, uint64 flag, bool apply)
289 if(apply) SetFlag64(index,flag); else RemoveFlag64(index,flag);
292 void ClearUpdateMask(bool remove);
293 void SendUpdateObjectToAllExcept(Player* exceptPlayer);
295 bool LoadValues(const char* data);
297 uint16 GetValuesCount() const { return m_valuesCount; }
299 void InitValues() { _InitValues(); }
301 virtual bool hasQuest(uint32 /* quest_id */) const { return false; }
302 virtual bool hasInvolvedQuest(uint32 /* quest_id */) const { return false; }
303 protected:
305 Object ( );
307 void _InitValues();
308 void _Create (uint32 guidlow, uint32 entry, HighGuid guidhigh);
310 virtual void _SetUpdateBits(UpdateMask *updateMask, Player *target) const;
312 virtual void _SetCreateBits(UpdateMask *updateMask, Player *target) const;
313 void _BuildMovementUpdate(ByteBuffer * data, uint8 flags, uint32 flags2 ) const;
314 void _BuildValuesUpdate(uint8 updatetype, ByteBuffer *data, UpdateMask *updateMask, Player *target ) const;
316 uint16 m_objectType;
318 uint8 m_objectTypeId;
319 uint8 m_updateFlag;
321 union
323 int32 *m_int32Values;
324 uint32 *m_uint32Values;
325 float *m_floatValues;
328 uint32 *m_uint32Values_mirror;
330 uint16 m_valuesCount;
332 bool m_objectUpdated;
334 private:
335 bool m_inWorld;
337 ByteBuffer m_PackGUID;
339 // for output helpfull error messages from asserts
340 bool PrintIndexError(uint32 index, bool set) const;
341 Object(const Object&); // prevent generation copy constructor
342 Object& operator=(Object const&); // prevent generation assigment operator
345 class MANGOS_DLL_SPEC WorldObject : public Object
347 public:
348 virtual ~WorldObject ( ) {}
350 virtual void Update ( uint32 /*time_diff*/ ) { }
352 void _Create( uint32 guidlow, HighGuid guidhigh, uint32 mapid, uint32 phaseMask);
354 void Relocate(float x, float y, float z, float orientation)
356 m_positionX = x;
357 m_positionY = y;
358 m_positionZ = z;
359 m_orientation = orientation;
362 void Relocate(float x, float y, float z)
364 m_positionX = x;
365 m_positionY = y;
366 m_positionZ = z;
369 void Relocate(WorldLocation const & loc)
371 SetMapId(loc.mapid);
372 Relocate(loc.x, loc.y, loc.z, loc.o);
375 void SetOrientation(float orientation) { m_orientation = orientation; }
377 float GetPositionX( ) const { return m_positionX; }
378 float GetPositionY( ) const { return m_positionY; }
379 float GetPositionZ( ) const { return m_positionZ; }
380 void GetPosition( float &x, float &y, float &z ) const
381 { x = m_positionX; y = m_positionY; z = m_positionZ; }
382 void GetPosition( WorldLocation &loc ) const
383 { loc.mapid = GetMapId(); GetPosition(loc.x, loc.y, loc.z); loc.o = GetOrientation(); }
384 float GetOrientation( ) const { return m_orientation; }
385 void GetNearPoint2D( float &x, float &y, float distance, float absAngle) const;
386 void GetNearPoint( WorldObject const* searcher, float &x, float &y, float &z, float searcher_size, float distance2d,float absAngle) const;
387 void GetClosePoint(float &x, float &y, float &z, float size, float distance2d = 0, float angle = 0) const
389 // angle calculated from current orientation
390 GetNearPoint(NULL,x,y,z,size,distance2d,GetOrientation() + angle);
392 void GetContactPoint( const WorldObject* obj, float &x, float &y, float &z, float distance2d = CONTACT_DISTANCE) const
394 // angle to face `obj` to `this` using distance includes size of `obj`
395 GetNearPoint(obj,x,y,z,obj->GetObjectSize(),distance2d,GetAngle( obj ));
398 float GetObjectSize() const
400 return ( m_valuesCount > UNIT_FIELD_BOUNDINGRADIUS ) ? m_floatValues[UNIT_FIELD_BOUNDINGRADIUS] : DEFAULT_WORLD_OBJECT_SIZE;
402 bool IsPositionValid() const;
403 void UpdateGroundPositionZ(float x, float y, float &z) const;
405 void GetRandomPoint( float x, float y, float z, float distance, float &rand_x, float &rand_y, float &rand_z ) const;
407 void SetMapId(uint32 newMap) { m_mapId = newMap; }
408 uint32 GetMapId() const { return m_mapId; }
410 virtual void SetPhaseMask(uint32 newPhaseMask, bool update);
411 uint32 GetPhaseMask() const { return m_phaseMask; }
412 bool InSamePhase(WorldObject const* obj) const { return InSamePhase(obj->GetPhaseMask()); }
413 bool InSamePhase(uint32 phasemask) const { return (GetPhaseMask() & phasemask); }
415 uint32 GetZoneId() const;
416 uint32 GetAreaId() const;
417 void GetZoneAndAreaId(uint32& zoneid, uint32& areaid) const;
419 InstanceData* GetInstanceData();
421 const char* GetName() const { return m_name.c_str(); }
422 void SetName(const std::string& newname) { m_name=newname; }
424 virtual const char* GetNameForLocaleIdx(int32 /*locale_idx*/) const { return GetName(); }
426 float GetDistance( const WorldObject* obj ) const;
427 float GetDistance(float x, float y, float z) const;
428 float GetDistance2d(const WorldObject* obj) const;
429 float GetDistance2d(float x, float y) const;
430 float GetDistanceZ(const WorldObject* obj) const;
431 bool IsInMap(const WorldObject* obj) const
433 return IsInWorld() && obj->IsInWorld() && GetMapId()==obj->GetMapId() &&
434 GetInstanceId()==obj->GetInstanceId() && InSamePhase(obj);
436 bool IsWithinDist3d(float x, float y, float z, float dist2compare) const;
437 bool IsWithinDist2d(float x, float y, float dist2compare) const;
438 bool _IsWithinDist(WorldObject const* obj, float dist2compare, bool is3D) const;
439 bool IsWithinDist(WorldObject const* obj, float dist2compare, bool is3D = true) const
440 // use only if you will sure about placing both object at same map
442 return obj && _IsWithinDist(obj,dist2compare,is3D);
444 bool IsWithinDistInMap(WorldObject const* obj, float dist2compare, bool is3D = true) const
446 return obj && IsInMap(obj) && _IsWithinDist(obj,dist2compare,is3D);
448 bool IsWithinLOS(float x, float y, float z) const;
449 bool IsWithinLOSInMap(const WorldObject* obj) const;
450 bool GetDistanceOrder(WorldObject const* obj1, WorldObject const* obj2, bool is3D = true) const;
451 bool IsInRange(WorldObject const* obj, float minRange, float maxRange, bool is3D = true) const;
452 bool IsInRange2d(float x, float y, float minRange, float maxRange) const;
453 bool IsInRange3d(float x, float y, float z, float minRange, float maxRange) const;
455 float GetAngle( const WorldObject* obj ) const;
456 float GetAngle( const float x, const float y ) const;
457 bool HasInArc( const float arcangle, const WorldObject* obj ) const;
459 virtual void SendMessageToSet(WorldPacket *data, bool self);
460 virtual void SendMessageToSetInRange(WorldPacket *data, float dist, bool self);
461 void BuildHeartBeatMsg( WorldPacket *data ) const;
462 void BuildTeleportAckMsg( WorldPacket *data, float x, float y, float z, float ang) const;
464 void MonsterSay(const char* text, uint32 language, uint64 TargetGuid);
465 void MonsterYell(const char* text, uint32 language, uint64 TargetGuid);
466 void MonsterTextEmote(const char* text, uint64 TargetGuid, bool IsBossEmote = false);
467 void MonsterWhisper(const char* text, uint64 receiver, bool IsBossWhisper = false);
468 void MonsterSay(int32 textId, uint32 language, uint64 TargetGuid);
469 void MonsterYell(int32 textId, uint32 language, uint64 TargetGuid);
470 void MonsterTextEmote(int32 textId, uint64 TargetGuid, bool IsBossEmote = false);
471 void MonsterWhisper(int32 textId, uint64 receiver, bool IsBossWhisper = false);
472 void MonsterYellToZone(int32 textId, uint32 language, uint64 TargetGuid);
473 void BuildMonsterChat(WorldPacket *data, uint8 msgtype, char const* text, uint32 language, char const* name, uint64 TargetGuid) const;
475 void PlayDistanceSound(uint32 sound_id, Player* target = NULL);
476 void PlayDirectSound(uint32 sound_id, Player* target = NULL);
478 void SendObjectDeSpawnAnim(uint64 guid);
480 virtual void SaveRespawnTime() {}
482 uint32 GetInstanceId() const { return m_InstanceId; }
483 void SetInstanceId(uint32 val) { m_InstanceId = val; }
485 void AddObjectToRemoveList();
487 // main visibility check function in normal case (ignore grey zone distance check)
488 bool isVisibleFor(Player const* u) const { return isVisibleForInState(u,false); }
490 // low level function for visibility change code, must be define in all main world object subclasses
491 virtual bool isVisibleForInState(Player const* u, bool inVisibleList) const = 0;
493 Map * GetMap() const;
494 Map const* GetBaseMap() const;
495 Creature* SummonCreature(uint32 id, float x, float y, float z, float ang,TempSummonType spwtype,uint32 despwtime);
497 protected:
498 explicit WorldObject();
499 std::string m_name;
501 private:
502 uint32 m_mapId; // object at map with map_id
503 uint32 m_InstanceId; // in map copy with instance id
504 uint32 m_phaseMask; // in area phase state
506 float m_positionX;
507 float m_positionY;
508 float m_positionZ;
509 float m_orientation;
511 #endif