[6982] Implemented gmlevel-based command security
[getmangos.git] / src / game / Object.h
blob9ecbf7b1d7984fa7af43484ae8eeff6d667f4652
1 /*
2 * Copyright (C) 2005-2008 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
68 uint32 GuidHigh2TypeId(uint32 guid_hi);
70 enum TempSummonType
72 TEMPSUMMON_TIMED_OR_DEAD_DESPAWN = 1, // despawns after a specified time OR when the creature disappears
73 TEMPSUMMON_TIMED_OR_CORPSE_DESPAWN = 2, // despawns after a specified time OR when the creature dies
74 TEMPSUMMON_TIMED_DESPAWN = 3, // despawns after a specified time
75 TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT = 4, // despawns after a specified time after the creature is out of combat
76 TEMPSUMMON_CORPSE_DESPAWN = 5, // despawns instantly after death
77 TEMPSUMMON_CORPSE_TIMED_DESPAWN = 6, // despawns after a specified time after death
78 TEMPSUMMON_DEAD_DESPAWN = 7, // despawns when the creature disappears
79 TEMPSUMMON_MANUAL_DESPAWN = 8 // despawns when UnSummon() is called
82 class WorldPacket;
83 class UpdateData;
84 class ByteBuffer;
85 class WorldSession;
86 class Creature;
87 class Player;
88 class Map;
89 class UpdateMask;
90 class InstanceData;
92 typedef UNORDERED_MAP<Player*, UpdateData> UpdateDataMapType;
94 struct WorldLocation
96 uint32 mapid;
97 float x;
98 float y;
99 float z;
100 float o;
101 explicit WorldLocation(uint32 _mapid = 0, float _x = 0, float _y = 0, float _z = 0, float _o = 0)
102 : mapid(_mapid), x(_x), y(_y), z(_z), o(_o) {}
103 WorldLocation(WorldLocation const &loc)
104 : mapid(loc.mapid), x(loc.x), y(loc.y), z(loc.z), o(loc.o) {}
107 class MANGOS_DLL_SPEC Object
109 public:
110 virtual ~Object ( );
112 const bool& IsInWorld() const { return m_inWorld; }
113 virtual void AddToWorld()
115 if(m_inWorld)
116 return;
118 m_inWorld = true;
120 // synchronize values mirror with values array (changes will send in updatecreate opcode any way
121 ClearUpdateMask(true);
123 virtual void RemoveFromWorld()
125 // if we remove from world then sending changes not required
126 if(m_uint32Values)
127 ClearUpdateMask(true);
128 m_inWorld = false;
131 const uint64& GetGUID() const { return GetUInt64Value(0); }
132 uint32 GetGUIDLow() const { return GUID_LOPART(GetUInt64Value(0)); }
133 uint32 GetGUIDMid() const { return GUID_ENPART(GetUInt64Value(0)); }
134 uint32 GetGUIDHigh() const { return GUID_HIPART(GetUInt64Value(0)); }
135 const ByteBuffer& GetPackGUID() const { return m_PackGUID; }
136 uint32 GetEntry() const { return GetUInt32Value(OBJECT_FIELD_ENTRY); }
137 void SetEntry(uint32 entry) { SetUInt32Value(OBJECT_FIELD_ENTRY, entry); }
139 uint8 GetTypeId() const { return m_objectTypeId; }
140 bool isType(uint16 mask) const { return (mask & m_objectType); }
142 virtual void BuildCreateUpdateBlockForPlayer( UpdateData *data, Player *target ) const;
143 void SendUpdateToPlayer(Player* player);
145 void BuildValuesUpdateBlockForPlayer( UpdateData *data, Player *target ) const;
146 void BuildOutOfRangeUpdateBlock( UpdateData *data ) const;
147 void BuildMovementUpdateBlock( UpdateData * data, uint32 flags = 0 ) const;
148 void BuildUpdate(UpdateDataMapType &);
150 virtual void DestroyForPlayer( Player *target ) const;
152 const int32& GetInt32Value( uint16 index ) const
154 ASSERT( index < m_valuesCount || PrintIndexError( index , false) );
155 return m_int32Values[ index ];
158 const uint32& GetUInt32Value( uint16 index ) const
160 ASSERT( index < m_valuesCount || PrintIndexError( index , false) );
161 return m_uint32Values[ index ];
164 const uint64& GetUInt64Value( uint16 index ) const
166 ASSERT( index + 1 < m_valuesCount || PrintIndexError( index , false) );
167 return *((uint64*)&(m_uint32Values[ index ]));
170 const float& GetFloatValue( uint16 index ) const
172 ASSERT( index < m_valuesCount || PrintIndexError( index , false ) );
173 return m_floatValues[ index ];
176 uint8 GetByteValue( uint16 index, uint8 offset) const
178 ASSERT( index < m_valuesCount || PrintIndexError( index , false) );
179 ASSERT( offset < 4 );
180 return *(((uint8*)&m_uint32Values[ index ])+offset);
183 uint8 GetUInt16Value( uint16 index, uint8 offset) const
185 ASSERT( index < m_valuesCount || PrintIndexError( index , false) );
186 ASSERT( offset < 2 );
187 return *(((uint16*)&m_uint32Values[ index ])+offset);
190 void SetInt32Value( uint16 index, int32 value );
191 void SetUInt32Value( uint16 index, uint32 value );
192 void SetUInt64Value( uint16 index, const uint64 &value );
193 void SetFloatValue( uint16 index, float value );
194 void SetByteValue( uint16 index, uint8 offset, uint8 value );
195 void SetUInt16Value( uint16 index, uint8 offset, uint16 value );
196 void SetInt16Value( uint16 index, uint8 offset, int16 value ) { SetUInt16Value(index,offset,(uint16)value); }
197 void SetStatFloatValue( uint16 index, float value);
198 void SetStatInt32Value( uint16 index, int32 value);
200 void ApplyModUInt32Value(uint16 index, int32 val, bool apply);
201 void ApplyModInt32Value(uint16 index, int32 val, bool apply);
202 void ApplyModUInt64Value(uint16 index, int32 val, bool apply);
203 void ApplyModPositiveFloatValue( uint16 index, float val, bool apply);
204 void ApplyModSignedFloatValue( uint16 index, float val, bool apply);
206 void ApplyPercentModFloatValue(uint16 index, float val, bool apply)
208 val = val != -100.0f ? val : -99.9f ;
209 SetFloatValue(index, GetFloatValue(index) * (apply?(100.0f+val)/100.0f : 100.0f / (100.0f+val)) );
212 void SetFlag( uint16 index, uint32 newFlag );
213 void RemoveFlag( uint16 index, uint32 oldFlag );
215 void ToggleFlag( uint16 index, uint32 flag)
217 if(HasFlag(index, flag))
218 RemoveFlag(index, flag);
219 else
220 SetFlag(index, flag);
223 bool HasFlag( uint16 index, uint32 flag ) const
225 ASSERT( index < m_valuesCount || PrintIndexError( index , false ) );
226 return (m_uint32Values[ index ] & flag) != 0;
229 void SetByteFlag( uint16 index, uint8 offset, uint8 newFlag );
230 void RemoveByteFlag( uint16 index, uint8 offset, uint8 newFlag );
232 void ToggleFlag( uint16 index, uint8 offset, uint8 flag )
234 if(HasByteFlag(index, offset, flag))
235 RemoveByteFlag(index, offset, flag);
236 else
237 SetByteFlag(index, offset, flag);
240 bool HasByteFlag( uint16 index, uint8 offset, uint8 flag ) const
242 ASSERT( index < m_valuesCount || PrintIndexError( index , false ) );
243 ASSERT( offset < 4 );
244 return (((uint8*)&m_uint32Values[index])[offset] & flag) != 0;
247 void ApplyModFlag( uint16 index, uint32 flag, bool apply)
249 if(apply) SetFlag(index,flag); else RemoveFlag(index,flag);
252 void SetFlag64( uint16 index, uint64 newFlag )
254 uint64 oldval = GetUInt64Value(index);
255 uint64 newval = oldval | newFlag;
256 SetUInt64Value(index,newval);
259 void RemoveFlag64( uint16 index, uint64 oldFlag )
261 uint64 oldval = GetUInt64Value(index);
262 uint64 newval = oldval & ~oldFlag;
263 SetUInt64Value(index,newval);
266 void ToggleFlag64( uint16 index, uint64 flag)
268 if(HasFlag64(index, flag))
269 RemoveFlag64(index, flag);
270 else
271 SetFlag64(index, flag);
274 bool HasFlag64( uint16 index, uint64 flag ) const
276 ASSERT( index < m_valuesCount || PrintIndexError( index , false ) );
277 return (GetUInt64Value( index ) & flag) != 0;
280 void ApplyModFlag64( uint16 index, uint64 flag, bool apply)
282 if(apply) SetFlag64(index,flag); else RemoveFlag64(index,flag);
285 void ClearUpdateMask(bool remove);
286 void SendUpdateObjectToAllExcept(Player* exceptPlayer);
288 bool LoadValues(const char* data);
290 uint16 GetValuesCount() const { return m_valuesCount; }
292 void InitValues() { _InitValues(); }
294 virtual bool hasQuest(uint32 /* quest_id */) const { return false; }
295 virtual bool hasInvolvedQuest(uint32 /* quest_id */) const { return false; }
296 protected:
298 Object ( );
300 void _InitValues();
301 void _Create (uint32 guidlow, uint32 entry, HighGuid guidhigh);
303 virtual void _SetUpdateBits(UpdateMask *updateMask, Player *target) const;
305 virtual void _SetCreateBits(UpdateMask *updateMask, Player *target) const;
306 void _BuildMovementUpdate(ByteBuffer * data, uint8 flags, uint32 flags2 ) const;
307 void _BuildValuesUpdate(uint8 updatetype, ByteBuffer *data, UpdateMask *updateMask, Player *target ) const;
309 uint16 m_objectType;
311 uint8 m_objectTypeId;
312 uint8 m_updateFlag;
314 union
316 int32 *m_int32Values;
317 uint32 *m_uint32Values;
318 float *m_floatValues;
321 uint32 *m_uint32Values_mirror;
323 uint16 m_valuesCount;
325 bool m_objectUpdated;
327 private:
328 bool m_inWorld;
330 ByteBuffer m_PackGUID;
332 // for output helpfull error messages from asserts
333 bool PrintIndexError(uint32 index, bool set) const;
334 Object(const Object&); // prevent generation copy constructor
335 Object& operator=(Object const&); // prevent generation assigment operator
338 class MANGOS_DLL_SPEC WorldObject : public Object
340 public:
341 virtual ~WorldObject ( ) {}
343 virtual void Update ( uint32 /*time_diff*/ ) { }
345 void _Create( uint32 guidlow, HighGuid guidhigh, uint32 mapid );
347 void Relocate(float x, float y, float z, float orientation)
349 m_positionX = x;
350 m_positionY = y;
351 m_positionZ = z;
352 m_orientation = orientation;
355 void Relocate(float x, float y, float z)
357 m_positionX = x;
358 m_positionY = y;
359 m_positionZ = z;
362 void Relocate(WorldLocation const & loc)
364 SetMapId(loc.mapid);
365 Relocate(loc.x, loc.y, loc.z, loc.o);
368 void SetOrientation(float orientation) { m_orientation = orientation; }
370 float GetPositionX( ) const { return m_positionX; }
371 float GetPositionY( ) const { return m_positionY; }
372 float GetPositionZ( ) const { return m_positionZ; }
373 void GetPosition( float &x, float &y, float &z ) const
374 { x = m_positionX; y = m_positionY; z = m_positionZ; }
375 void GetPosition( WorldLocation &loc ) const
376 { loc.mapid = GetMapId(); GetPosition(loc.x, loc.y, loc.z); loc.o = GetOrientation(); }
377 float GetOrientation( ) const { return m_orientation; }
378 void GetNearPoint2D( float &x, float &y, float distance, float absAngle) const;
379 void GetNearPoint( WorldObject const* searcher, float &x, float &y, float &z, float searcher_size, float distance2d,float absAngle) const;
380 void GetClosePoint(float &x, float &y, float &z, float size, float distance2d = 0, float angle = 0) const
382 // angle calculated from current orientation
383 GetNearPoint(NULL,x,y,z,size,distance2d,GetOrientation() + angle);
385 void GetContactPoint( const WorldObject* obj, float &x, float &y, float &z, float distance2d = CONTACT_DISTANCE) const
387 // angle to face `obj` to `this` using distance includes size of `obj`
388 GetNearPoint(obj,x,y,z,obj->GetObjectSize(),distance2d,GetAngle( obj ));
391 float GetObjectSize() const
393 return ( m_valuesCount > UNIT_FIELD_BOUNDINGRADIUS ) ? m_floatValues[UNIT_FIELD_BOUNDINGRADIUS] : DEFAULT_WORLD_OBJECT_SIZE;
395 bool IsPositionValid() const;
396 void UpdateGroundPositionZ(float x, float y, float &z) const;
398 void GetRandomPoint( float x, float y, float z, float distance, float &rand_x, float &rand_y, float &rand_z ) const;
400 void SetMapId(uint32 newMap) { m_mapId = newMap; }
402 uint32 GetMapId() const { return m_mapId; }
404 uint32 GetZoneId() const;
405 uint32 GetAreaId() const;
407 InstanceData* GetInstanceData();
409 const char* GetName() const { return m_name.c_str(); }
410 void SetName(const std::string& newname) { m_name=newname; }
412 virtual const char* GetNameForLocaleIdx(int32 /*locale_idx*/) const { return GetName(); }
414 float GetDistance( const WorldObject* obj ) const;
415 float GetDistance(const float x, const float y, const float z) const;
416 float GetDistance2d(const WorldObject* obj) const;
417 float GetDistance2d(const float x, const float y) const;
418 float GetDistanceZ(const WorldObject* obj) const;
419 bool IsInMap(const WorldObject* obj) const { return IsInWorld() && obj->IsInWorld() && GetMapId()==obj->GetMapId() && GetInstanceId()==obj->GetInstanceId(); }
420 bool IsWithinDistInMap(const WorldObject* obj, const float dist2compare, const bool is3D = true) const;
421 bool IsWithinLOS(const float x, const float y, const float z ) const;
422 bool IsWithinLOSInMap(const WorldObject* obj) const;
424 float GetAngle( const WorldObject* obj ) const;
425 float GetAngle( const float x, const float y ) const;
426 bool HasInArc( const float arcangle, const WorldObject* obj ) const;
428 virtual void SendMessageToSet(WorldPacket *data, bool self);
429 virtual void SendMessageToSetInRange(WorldPacket *data, float dist, bool self);
430 void BuildHeartBeatMsg( WorldPacket *data ) const;
431 void BuildTeleportAckMsg( WorldPacket *data, float x, float y, float z, float ang) const;
432 bool IsBeingTeleported() { return mSemaphoreTeleport; }
433 void SetSemaphoreTeleport(bool semphsetting) { mSemaphoreTeleport = semphsetting; }
435 void MonsterSay(const char* text, uint32 language, uint64 TargetGuid);
436 void MonsterYell(const char* text, uint32 language, uint64 TargetGuid);
437 void MonsterTextEmote(const char* text, uint64 TargetGuid, bool IsBossEmote = false);
438 void MonsterWhisper(const char* text, uint64 receiver, bool IsBossWhisper = false);
439 void MonsterSay(int32 textId, uint32 language, uint64 TargetGuid);
440 void MonsterYell(int32 textId, uint32 language, uint64 TargetGuid);
441 void MonsterTextEmote(int32 textId, uint64 TargetGuid, bool IsBossEmote = false);
442 void MonsterWhisper(int32 textId, uint64 receiver, bool IsBossWhisper = false);
443 void BuildMonsterChat(WorldPacket *data, uint8 msgtype, char const* text, uint32 language, char const* name, uint64 TargetGuid) const;
445 void SendObjectDeSpawnAnim(uint64 guid);
447 virtual void SaveRespawnTime() {}
449 uint32 GetInstanceId() const { return m_InstanceId; }
450 void SetInstanceId(uint32 val) { m_InstanceId = val; }
452 void AddObjectToRemoveList();
454 // main visibility check function in normal case (ignore grey zone distance check)
455 bool isVisibleFor(Player const* u) const { return isVisibleForInState(u,false); }
457 // low level function for visibility change code, must be define in all main world object subclasses
458 virtual bool isVisibleForInState(Player const* u, bool inVisibleList) const = 0;
460 Map * GetMap() const;
461 Map const* GetBaseMap() const;
462 Creature* SummonCreature(uint32 id, float x, float y, float z, float ang,TempSummonType spwtype,uint32 despwtime);
464 protected:
465 explicit WorldObject();
466 std::string m_name;
468 private:
469 uint32 m_mapId;
471 float m_positionX;
472 float m_positionY;
473 float m_positionZ;
474 float m_orientation;
476 bool mSemaphoreTeleport;
478 uint32 m_InstanceId;
480 #endif