[7916] Fixed pet action bar setup.
[getmangos.git] / src / game / Object.cpp
blob5b3d9ef4fedfa774ed01b3bd44c1d7c1f398364c
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 #include "Common.h"
20 #include "SharedDefines.h"
21 #include "WorldPacket.h"
22 #include "Opcodes.h"
23 #include "Log.h"
24 #include "World.h"
25 #include "Object.h"
26 #include "Creature.h"
27 #include "Player.h"
28 #include "Vehicle.h"
29 #include "ObjectMgr.h"
30 #include "UpdateData.h"
31 #include "UpdateMask.h"
32 #include "Util.h"
33 #include "MapManager.h"
34 #include "ObjectAccessor.h"
35 #include "Log.h"
36 #include "Transports.h"
37 #include "TargetedMovementGenerator.h"
38 #include "WaypointMovementGenerator.h"
39 #include "VMapFactory.h"
40 #include "CellImpl.h"
41 #include "GridNotifiers.h"
42 #include "GridNotifiersImpl.h"
44 #include "ObjectPosSelector.h"
46 #include "TemporarySummon.h"
48 uint32 GuidHigh2TypeId(uint32 guid_hi)
50 switch(guid_hi)
52 case HIGHGUID_ITEM: return TYPEID_ITEM;
53 //case HIGHGUID_CONTAINER: return TYPEID_CONTAINER; HIGHGUID_CONTAINER==HIGHGUID_ITEM currently
54 case HIGHGUID_UNIT: return TYPEID_UNIT;
55 case HIGHGUID_PET: return TYPEID_UNIT;
56 case HIGHGUID_PLAYER: return TYPEID_PLAYER;
57 case HIGHGUID_GAMEOBJECT: return TYPEID_GAMEOBJECT;
58 case HIGHGUID_DYNAMICOBJECT:return TYPEID_DYNAMICOBJECT;
59 case HIGHGUID_CORPSE: return TYPEID_CORPSE;
60 case HIGHGUID_MO_TRANSPORT: return TYPEID_GAMEOBJECT;
61 case HIGHGUID_VEHICLE: return TYPEID_UNIT;
63 return NUM_CLIENT_OBJECT_TYPES; // unknown
66 Object::Object( )
68 m_objectTypeId = TYPEID_OBJECT;
69 m_objectType = TYPEMASK_OBJECT;
71 m_uint32Values = 0;
72 m_uint32Values_mirror = 0;
73 m_valuesCount = 0;
75 m_inWorld = false;
76 m_objectUpdated = false;
78 m_PackGUID.clear();
79 m_PackGUID.appendPackGUID(0);
82 Object::~Object( )
84 if(m_objectUpdated)
85 ObjectAccessor::Instance().RemoveUpdateObject(this);
87 if(m_uint32Values)
89 if(IsInWorld())
91 ///- Do NOT call RemoveFromWorld here, if the object is a player it will crash
92 sLog.outError("Object::~Object - guid="I64FMTD", typeid=%d deleted but still in world!!", GetGUID(), GetTypeId());
93 //assert(0);
96 //DEBUG_LOG("Object desctr 1 check (%p)",(void*)this);
97 delete [] m_uint32Values;
98 delete [] m_uint32Values_mirror;
99 //DEBUG_LOG("Object desctr 2 check (%p)",(void*)this);
103 void Object::_InitValues()
105 m_uint32Values = new uint32[ m_valuesCount ];
106 memset(m_uint32Values, 0, m_valuesCount*sizeof(uint32));
108 m_uint32Values_mirror = new uint32[ m_valuesCount ];
109 memset(m_uint32Values_mirror, 0, m_valuesCount*sizeof(uint32));
111 m_objectUpdated = false;
114 void Object::_Create( uint32 guidlow, uint32 entry, HighGuid guidhigh )
116 if(!m_uint32Values) _InitValues();
118 uint64 guid = MAKE_NEW_GUID(guidlow, entry, guidhigh);
119 SetUInt64Value( OBJECT_FIELD_GUID, guid );
120 SetUInt32Value( OBJECT_FIELD_TYPE, m_objectType );
121 m_PackGUID.clear();
122 m_PackGUID.appendPackGUID(GetGUID());
125 void Object::BuildMovementUpdateBlock(UpdateData * data, uint32 flags ) const
127 ByteBuffer buf(50);
129 buf << uint8( UPDATETYPE_MOVEMENT );
130 buf.append(GetPackGUID());
132 _BuildMovementUpdate(&buf, flags, 0x00000000);
134 data->AddUpdateBlock(buf);
137 void Object::BuildCreateUpdateBlockForPlayer(UpdateData *data, Player *target) const
139 if(!target)
140 return;
142 uint8 updatetype = UPDATETYPE_CREATE_OBJECT;
143 uint8 flags = m_updateFlag;
144 uint32 flags2 = 0;
146 /** lower flag1 **/
147 if(target == this) // building packet for yourself
148 flags |= UPDATEFLAG_SELF;
150 if(flags & UPDATEFLAG_HAS_POSITION)
152 // UPDATETYPE_CREATE_OBJECT2 dynamic objects, corpses...
153 if(isType(TYPEMASK_DYNAMICOBJECT) || isType(TYPEMASK_CORPSE) || isType(TYPEMASK_PLAYER))
154 updatetype = UPDATETYPE_CREATE_OBJECT2;
156 // UPDATETYPE_CREATE_OBJECT2 for pets...
157 if(target->GetPetGUID() == GetGUID())
158 updatetype = UPDATETYPE_CREATE_OBJECT2;
160 // UPDATETYPE_CREATE_OBJECT2 for some gameobject types...
161 if(isType(TYPEMASK_GAMEOBJECT))
163 switch(((GameObject*)this)->GetGoType())
165 case GAMEOBJECT_TYPE_TRAP:
166 case GAMEOBJECT_TYPE_DUEL_ARBITER:
167 case GAMEOBJECT_TYPE_FLAGSTAND:
168 case GAMEOBJECT_TYPE_FLAGDROP:
169 updatetype = UPDATETYPE_CREATE_OBJECT2;
170 break;
171 case GAMEOBJECT_TYPE_TRANSPORT:
172 flags |= UPDATEFLAG_TRANSPORT;
173 break;
174 default:
175 break;
179 if(isType(TYPEMASK_UNIT))
181 if(((Unit*)this)->getVictim())
182 flags |= UPDATEFLAG_HAS_TARGET;
186 //sLog.outDebug("BuildCreateUpdate: update-type: %u, object-type: %u got flags: %X, flags2: %X", updatetype, m_objectTypeId, flags, flags2);
188 ByteBuffer buf(50);
189 buf << (uint8)updatetype;
190 buf.append(GetPackGUID());
191 buf << (uint8)m_objectTypeId;
193 _BuildMovementUpdate(&buf, flags, flags2);
195 UpdateMask updateMask;
196 updateMask.SetCount( m_valuesCount );
197 _SetCreateBits( &updateMask, target );
198 _BuildValuesUpdate(updatetype, &buf, &updateMask, target);
199 data->AddUpdateBlock(buf);
202 void Object::BuildUpdate(UpdateDataMapType &update_players)
204 ObjectAccessor::_buildUpdateObject(this,update_players);
205 ClearUpdateMask(true);
208 void Object::SendUpdateToPlayer(Player* player)
210 // send update to another players
211 SendUpdateObjectToAllExcept(player);
213 // send create update to player
214 UpdateData upd;
215 WorldPacket packet;
217 upd.Clear();
218 BuildCreateUpdateBlockForPlayer(&upd, player);
219 upd.BuildPacket(&packet);
220 player->GetSession()->SendPacket(&packet);
223 void Object::BuildValuesUpdateBlockForPlayer(UpdateData *data, Player *target) const
225 ByteBuffer buf(50);
227 buf << (uint8) UPDATETYPE_VALUES;
228 buf.append(GetPackGUID());
230 UpdateMask updateMask;
231 updateMask.SetCount( m_valuesCount );
233 _SetUpdateBits( &updateMask, target );
234 _BuildValuesUpdate(UPDATETYPE_VALUES, &buf, &updateMask, target);
236 data->AddUpdateBlock(buf);
239 void Object::BuildOutOfRangeUpdateBlock(UpdateData * data) const
241 data->AddOutOfRangeGUID(GetGUID());
244 void Object::DestroyForPlayer(Player *target) const
246 ASSERT(target);
248 WorldPacket data(SMSG_DESTROY_OBJECT, 8);
249 data << uint64(GetGUID());
250 data << uint8(0); // WotLK (bool)
251 target->GetSession()->SendPacket( &data );
254 void Object::_BuildMovementUpdate(ByteBuffer * data, uint8 flags, uint32 flags2) const
256 uint16 unk_flags = ((GetTypeId() == TYPEID_PLAYER) ? ((Player*)this)->m_movementInfo.unk1 : 0);
258 if(GetTypeId() == TYPEID_UNIT)
259 if(((Creature*)this)->isVehicle())
260 unk_flags |= 0x20; // always allow pitch
262 *data << (uint8)flags; // update flags
264 // 0x20
265 if (flags & UPDATEFLAG_LIVING)
267 switch(GetTypeId())
269 case TYPEID_UNIT:
271 flags2 = ((Unit*)this)->GetUnitMovementFlags();
273 break;
274 case TYPEID_PLAYER:
276 flags2 = ((Player*)this)->GetUnitMovementFlags();
278 if(((Player*)this)->GetTransport())
279 flags2 |= MOVEMENTFLAG_ONTRANSPORT;
280 else
281 flags2 &= ~MOVEMENTFLAG_ONTRANSPORT;
283 // remove unknown, unused etc flags for now
284 flags2 &= ~MOVEMENTFLAG_SPLINE2; // will be set manually
286 if(((Player*)this)->isInFlight())
288 WPAssert(((Player*)this)->GetMotionMaster()->GetCurrentMovementGeneratorType() == FLIGHT_MOTION_TYPE);
289 flags2 = (MOVEMENTFLAG_FORWARD | MOVEMENTFLAG_SPLINE2);
292 break;
295 *data << uint32(flags2); // movement flags
296 *data << uint16(unk_flags); // unknown 2.3.0
297 *data << uint32(getMSTime()); // time (in milliseconds)
300 // 0x40
301 if (flags & UPDATEFLAG_HAS_POSITION)
303 // 0x02
304 if(flags & UPDATEFLAG_TRANSPORT && ((GameObject*)this)->GetGoType() == GAMEOBJECT_TYPE_MO_TRANSPORT)
306 *data << (float)0;
307 *data << (float)0;
308 *data << (float)0;
309 *data << ((WorldObject *)this)->GetOrientation();
311 else
313 *data << ((WorldObject *)this)->GetPositionX();
314 *data << ((WorldObject *)this)->GetPositionY();
315 *data << ((WorldObject *)this)->GetPositionZ();
316 *data << ((WorldObject *)this)->GetOrientation();
320 // 0x20
321 if(flags & UPDATEFLAG_LIVING)
323 // 0x00000200
324 if(flags2 & MOVEMENTFLAG_ONTRANSPORT)
326 if(GetTypeId() == TYPEID_PLAYER)
328 *data << (uint64)((Player*)this)->GetTransport()->GetGUID();
329 *data << (float)((Player*)this)->GetTransOffsetX();
330 *data << (float)((Player*)this)->GetTransOffsetY();
331 *data << (float)((Player*)this)->GetTransOffsetZ();
332 *data << (float)((Player*)this)->GetTransOffsetO();
333 *data << (uint32)((Player*)this)->GetTransTime();
334 *data << (int8)((Player*)this)->GetTransSeat();
336 //MaNGOS currently not have support for other than player on transport
339 // 0x02200000
340 if((flags2 & (MOVEMENTFLAG_SWIMMING | MOVEMENTFLAG_FLYING2)) || (unk_flags & 0x20))
342 if(GetTypeId() == TYPEID_PLAYER)
343 *data << (float)((Player*)this)->m_movementInfo.s_pitch;
344 else
345 *data << (float)0; // is't part of movement packet, we must store and send it...
348 if(GetTypeId() == TYPEID_PLAYER)
349 *data << (uint32)((Player*)this)->m_movementInfo.fallTime;
350 else
351 *data << (uint32)0; // last fall time
353 // 0x00001000
354 if(flags2 & MOVEMENTFLAG_JUMPING)
356 if(GetTypeId() == TYPEID_PLAYER)
358 *data << (float)((Player*)this)->m_movementInfo.j_unk;
359 *data << (float)((Player*)this)->m_movementInfo.j_sinAngle;
360 *data << (float)((Player*)this)->m_movementInfo.j_cosAngle;
361 *data << (float)((Player*)this)->m_movementInfo.j_xyspeed;
363 else
365 *data << (float)0;
366 *data << (float)0;
367 *data << (float)0;
368 *data << (float)0;
372 // 0x04000000
373 if(flags2 & MOVEMENTFLAG_SPLINE)
375 if(GetTypeId() == TYPEID_PLAYER)
376 *data << (float)((Player*)this)->m_movementInfo.u_unk1;
377 else
378 *data << (float)0;
381 *data << ((Unit*)this)->GetSpeed( MOVE_WALK );
382 *data << ((Unit*)this)->GetSpeed( MOVE_RUN );
383 *data << ((Unit*)this)->GetSpeed( MOVE_SWIM_BACK );
384 *data << ((Unit*)this)->GetSpeed( MOVE_SWIM );
385 *data << ((Unit*)this)->GetSpeed( MOVE_RUN_BACK );
386 *data << ((Unit*)this)->GetSpeed( MOVE_FLIGHT );
387 *data << ((Unit*)this)->GetSpeed( MOVE_FLIGHT_BACK );
388 *data << ((Unit*)this)->GetSpeed( MOVE_TURN_RATE );
389 *data << ((Unit*)this)->GetSpeed( MOVE_PITCH_RATE );
391 // 0x08000000
392 if(flags2 & MOVEMENTFLAG_SPLINE2)
394 if(GetTypeId() != TYPEID_PLAYER)
396 sLog.outDebug("_BuildMovementUpdate: MOVEMENTFLAG_SPLINE2 for non-player");
397 return;
400 if(!((Player*)this)->isInFlight())
402 sLog.outDebug("_BuildMovementUpdate: MOVEMENTFLAG_SPLINE2 but not in flight");
403 return;
406 WPAssert(((Player*)this)->GetMotionMaster()->GetCurrentMovementGeneratorType() == FLIGHT_MOTION_TYPE);
408 FlightPathMovementGenerator *fmg = (FlightPathMovementGenerator*)(((Player*)this)->GetMotionMaster()->top());
410 uint32 flags3 = 0x00000300;
412 *data << uint32(flags3); // splines flag?
414 if(flags3 & 0x10000) // probably x,y,z coords there
416 *data << (float)0;
417 *data << (float)0;
418 *data << (float)0;
421 if(flags3 & 0x20000) // probably guid there
423 *data << uint64(0);
426 if(flags3 & 0x40000) // may be orientation
428 *data << (float)0;
431 Path &path = fmg->GetPath();
433 float x, y, z;
434 ((Player*)this)->GetPosition(x, y, z);
436 uint32 inflighttime = uint32(path.GetPassedLength(fmg->GetCurrentNode(), x, y, z) * 32);
437 uint32 traveltime = uint32(path.GetTotalLength() * 32);
439 *data << uint32(inflighttime); // passed move time?
440 *data << uint32(traveltime); // full move time?
441 *data << uint32(0); // ticks count?
443 uint32 poscount = uint32(path.Size());
445 *data << uint32(poscount); // points count
447 for(uint32 i = 0; i < poscount; ++i)
449 *data << path.GetNodes()[i].x;
450 *data << path.GetNodes()[i].y;
451 *data << path.GetNodes()[i].z;
454 *data << uint8(0); // added in 3.0.8
456 /*for(uint32 i = 0; i < poscount; i++)
458 // path points
459 *data << (float)0;
460 *data << (float)0;
461 *data << (float)0;
464 *data << path.GetNodes()[poscount-1].x;
465 *data << path.GetNodes()[poscount-1].y;
466 *data << path.GetNodes()[poscount-1].z;
468 // target position (path end)
469 /**data << ((Unit*)this)->GetPositionX();
470 *data << ((Unit*)this)->GetPositionY();
471 *data << ((Unit*)this)->GetPositionZ();*/
475 // 0x8
476 if(flags & UPDATEFLAG_LOWGUID)
478 switch(GetTypeId())
480 case TYPEID_OBJECT:
481 case TYPEID_ITEM:
482 case TYPEID_CONTAINER:
483 case TYPEID_GAMEOBJECT:
484 case TYPEID_DYNAMICOBJECT:
485 case TYPEID_CORPSE:
486 *data << uint32(GetGUIDLow()); // GetGUIDLow()
487 break;
488 case TYPEID_UNIT:
489 *data << uint32(0x0000000B); // unk, can be 0xB or 0xC
490 break;
491 case TYPEID_PLAYER:
492 if(flags & UPDATEFLAG_SELF)
493 *data << uint32(0x0000002F); // unk, can be 0x15 or 0x22
494 else
495 *data << uint32(0x00000008); // unk, can be 0x7 or 0x8
496 break;
497 default:
498 *data << uint32(0x00000000); // unk
499 break;
503 // 0x10
504 if(flags & UPDATEFLAG_HIGHGUID)
506 switch(GetTypeId())
508 case TYPEID_OBJECT:
509 case TYPEID_ITEM:
510 case TYPEID_CONTAINER:
511 case TYPEID_GAMEOBJECT:
512 case TYPEID_DYNAMICOBJECT:
513 case TYPEID_CORPSE:
514 *data << uint32(GetGUIDHigh()); // GetGUIDHigh()
515 break;
516 case TYPEID_UNIT:
517 *data << uint32(0x0000000B); // unk, can be 0xB or 0xC
518 break;
519 case TYPEID_PLAYER:
520 if(flags & UPDATEFLAG_SELF)
521 *data << uint32(0x0000002F); // unk, can be 0x15 or 0x22
522 else
523 *data << uint32(0x00000008); // unk, can be 0x7 or 0x8
524 break;
525 default:
526 *data << uint32(0x00000000); // unk
527 break;
531 // 0x4
532 if(flags & UPDATEFLAG_HAS_TARGET) // packed guid (current target guid)
534 if(Unit *victim = ((Unit*)this)->getVictim())
535 data->append(victim->GetPackGUID());
536 else
537 *data << uint8(0);
540 // 0x2
541 if(flags & UPDATEFLAG_TRANSPORT)
543 *data << uint32(getMSTime()); // ms time
546 // 0x80
547 if(flags & UPDATEFLAG_VEHICLE) // unused for now
549 *data << uint32(((Vehicle*)this)->GetVehicleId()); // vehicle id
550 *data << float(0); // facing adjustment
554 void Object::_BuildValuesUpdate(uint8 updatetype, ByteBuffer * data, UpdateMask *updateMask, Player *target) const
556 if(!target)
557 return;
559 bool IsActivateToQuest = false;
560 if (updatetype == UPDATETYPE_CREATE_OBJECT || updatetype == UPDATETYPE_CREATE_OBJECT2)
562 if (isType(TYPEMASK_GAMEOBJECT) && !((GameObject*)this)->IsTransport())
564 if ( ((GameObject*)this)->ActivateToQuest(target) || target->isGameMaster())
566 IsActivateToQuest = true;
567 updateMask->SetBit(GAMEOBJECT_DYNAMIC);
571 else // case UPDATETYPE_VALUES
573 if (isType(TYPEMASK_GAMEOBJECT) && !((GameObject*)this)->IsTransport())
575 if ( ((GameObject*)this)->ActivateToQuest(target) || target->isGameMaster())
577 IsActivateToQuest = true;
579 updateMask->SetBit(GAMEOBJECT_DYNAMIC);
580 updateMask->SetBit(GAMEOBJECT_BYTES_1);
584 WPAssert(updateMask && updateMask->GetCount() == m_valuesCount);
586 *data << (uint8)updateMask->GetBlockCount();
587 data->append( updateMask->GetMask(), updateMask->GetLength() );
589 // 2 specialized loops for speed optimization in non-unit case
590 if(isType(TYPEMASK_UNIT)) // unit (creature/player) case
592 for( uint16 index = 0; index < m_valuesCount; index ++ )
594 if( updateMask->GetBit( index ) )
596 if( index == UNIT_NPC_FLAGS )
598 // remove custom flag before sending
599 uint32 appendValue = m_uint32Values[ index ] & ~UNIT_NPC_FLAG_GUARD;
601 if (GetTypeId() == TYPEID_UNIT && !target->canSeeSpellClickOn((Creature*)this))
602 appendValue &= ~UNIT_NPC_FLAG_SPELLCLICK;
604 *data << uint32(appendValue);
606 // FIXME: Some values at server stored in float format but must be sent to client in uint32 format
607 else if(index >= UNIT_FIELD_BASEATTACKTIME && index <= UNIT_FIELD_RANGEDATTACKTIME)
609 // convert from float to uint32 and send
610 *data << uint32(m_floatValues[ index ] < 0 ? 0 : m_floatValues[ index ]);
612 // there are some float values which may be negative or can't get negative due to other checks
613 else if ((index >= UNIT_FIELD_NEGSTAT0 && index <= UNIT_FIELD_NEGSTAT4) ||
614 (index >= UNIT_FIELD_RESISTANCEBUFFMODSPOSITIVE && index <= (UNIT_FIELD_RESISTANCEBUFFMODSPOSITIVE + 6)) ||
615 (index >= UNIT_FIELD_RESISTANCEBUFFMODSNEGATIVE && index <= (UNIT_FIELD_RESISTANCEBUFFMODSNEGATIVE + 6)) ||
616 (index >= UNIT_FIELD_POSSTAT0 && index <= UNIT_FIELD_POSSTAT4))
618 *data << uint32(m_floatValues[ index ]);
620 // Gamemasters should be always able to select units - remove not selectable flag
621 else if(index == UNIT_FIELD_FLAGS && target->isGameMaster())
623 *data << (m_uint32Values[ index ] & ~UNIT_FLAG_NOT_SELECTABLE);
625 // hide lootable animation for unallowed players
626 else if(index == UNIT_DYNAMIC_FLAGS && GetTypeId() == TYPEID_UNIT)
628 if(!target->isAllowedToLoot((Creature*)this))
629 *data << (m_uint32Values[ index ] & ~UNIT_DYNFLAG_LOOTABLE);
630 else
631 *data << (m_uint32Values[ index ] & ~UNIT_DYNFLAG_OTHER_TAGGER);
633 else
635 // send in current format (float as float, uint32 as uint32)
636 *data << m_uint32Values[ index ];
641 else if(isType(TYPEMASK_GAMEOBJECT)) // gameobject case
643 for( uint16 index = 0; index < m_valuesCount; index ++ )
645 if( updateMask->GetBit( index ) )
647 // send in current format (float as float, uint32 as uint32)
648 if ( index == GAMEOBJECT_DYNAMIC )
650 if(IsActivateToQuest )
652 switch(((GameObject*)this)->GetGoType())
654 case GAMEOBJECT_TYPE_CHEST:
655 *data << uint32(9); // enable quest object. Represent 9, but 1 for client before 2.3.0
656 break;
657 case GAMEOBJECT_TYPE_GOOBER:
658 *data << uint32(1);
659 break;
660 default:
661 *data << uint32(0); // unknown, not happen.
662 break;
665 else
666 *data << uint32(0); // disable quest object
668 else
669 *data << m_uint32Values[ index ]; // other cases
673 else // other objects case (no special index checks)
675 for( uint16 index = 0; index < m_valuesCount; index ++ )
677 if( updateMask->GetBit( index ) )
679 // send in current format (float as float, uint32 as uint32)
680 *data << m_uint32Values[ index ];
686 void Object::ClearUpdateMask(bool remove)
688 for( uint16 index = 0; index < m_valuesCount; index ++ )
690 if(m_uint32Values_mirror[index]!= m_uint32Values[index])
691 m_uint32Values_mirror[index] = m_uint32Values[index];
693 if(m_objectUpdated)
695 if(remove)
696 ObjectAccessor::Instance().RemoveUpdateObject(this);
697 m_objectUpdated = false;
701 // Send current value fields changes to all viewers
702 void Object::SendUpdateObjectToAllExcept(Player* exceptPlayer)
704 // changes will be send in create packet
705 if(!IsInWorld())
706 return;
708 // nothing do
709 if(!m_objectUpdated)
710 return;
712 ObjectAccessor::UpdateObject(this,exceptPlayer);
715 bool Object::LoadValues(const char* data)
717 if(!m_uint32Values) _InitValues();
719 Tokens tokens = StrSplit(data, " ");
721 if(tokens.size() != m_valuesCount)
722 return false;
724 Tokens::iterator iter;
725 int index;
726 for (iter = tokens.begin(), index = 0; index < m_valuesCount; ++iter, ++index)
728 m_uint32Values[index] = atol((*iter).c_str());
731 return true;
734 void Object::_SetUpdateBits(UpdateMask *updateMask, Player* /*target*/) const
736 for( uint16 index = 0; index < m_valuesCount; index ++ )
738 if(m_uint32Values_mirror[index]!= m_uint32Values[index])
739 updateMask->SetBit(index);
743 void Object::_SetCreateBits(UpdateMask *updateMask, Player* /*target*/) const
745 for( uint16 index = 0; index < m_valuesCount; index++ )
747 if(GetUInt32Value(index) != 0)
748 updateMask->SetBit(index);
752 void Object::SetInt32Value( uint16 index, int32 value )
754 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
756 if(m_int32Values[ index ] != value)
758 m_int32Values[ index ] = value;
760 if(m_inWorld)
762 if(!m_objectUpdated)
764 ObjectAccessor::Instance().AddUpdateObject(this);
765 m_objectUpdated = true;
771 void Object::SetUInt32Value( uint16 index, uint32 value )
773 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
775 if(m_uint32Values[ index ] != value)
777 m_uint32Values[ index ] = value;
779 if(m_inWorld)
781 if(!m_objectUpdated)
783 ObjectAccessor::Instance().AddUpdateObject(this);
784 m_objectUpdated = true;
790 void Object::SetUInt64Value( uint16 index, const uint64 &value )
792 ASSERT( index + 1 < m_valuesCount || PrintIndexError( index , true ) );
793 if(*((uint64*)&(m_uint32Values[ index ])) != value)
795 m_uint32Values[ index ] = *((uint32*)&value);
796 m_uint32Values[ index + 1 ] = *(((uint32*)&value) + 1);
798 if(m_inWorld)
800 if(!m_objectUpdated)
802 ObjectAccessor::Instance().AddUpdateObject(this);
803 m_objectUpdated = true;
809 void Object::SetFloatValue( uint16 index, float value )
811 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
813 if(m_floatValues[ index ] != value)
815 m_floatValues[ index ] = value;
817 if(m_inWorld)
819 if(!m_objectUpdated)
821 ObjectAccessor::Instance().AddUpdateObject(this);
822 m_objectUpdated = true;
828 void Object::SetByteValue( uint16 index, uint8 offset, uint8 value )
830 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
832 if(offset > 4)
834 sLog.outError("Object::SetByteValue: wrong offset %u", offset);
835 return;
838 if(uint8(m_uint32Values[ index ] >> (offset * 8)) != value)
840 m_uint32Values[ index ] &= ~uint32(uint32(0xFF) << (offset * 8));
841 m_uint32Values[ index ] |= uint32(uint32(value) << (offset * 8));
843 if(m_inWorld)
845 if(!m_objectUpdated)
847 ObjectAccessor::Instance().AddUpdateObject(this);
848 m_objectUpdated = true;
854 void Object::SetUInt16Value( uint16 index, uint8 offset, uint16 value )
856 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
858 if(offset > 2)
860 sLog.outError("Object::SetUInt16Value: wrong offset %u", offset);
861 return;
864 if(uint8(m_uint32Values[ index ] >> (offset * 16)) != value)
866 m_uint32Values[ index ] &= ~uint32(uint32(0xFFFF) << (offset * 16));
867 m_uint32Values[ index ] |= uint32(uint32(value) << (offset * 16));
869 if(m_inWorld)
871 if(!m_objectUpdated)
873 ObjectAccessor::Instance().AddUpdateObject(this);
874 m_objectUpdated = true;
880 void Object::SetStatFloatValue( uint16 index, float value)
882 if(value < 0)
883 value = 0.0f;
885 SetFloatValue(index, value);
888 void Object::SetStatInt32Value( uint16 index, int32 value)
890 if(value < 0)
891 value = 0;
893 SetUInt32Value(index, uint32(value));
896 void Object::ApplyModUInt32Value(uint16 index, int32 val, bool apply)
898 int32 cur = GetUInt32Value(index);
899 cur += (apply ? val : -val);
900 if(cur < 0)
901 cur = 0;
902 SetUInt32Value(index, cur);
905 void Object::ApplyModInt32Value(uint16 index, int32 val, bool apply)
907 int32 cur = GetInt32Value(index);
908 cur += (apply ? val : -val);
909 SetInt32Value(index, cur);
912 void Object::ApplyModSignedFloatValue(uint16 index, float val, bool apply)
914 float cur = GetFloatValue(index);
915 cur += (apply ? val : -val);
916 SetFloatValue(index, cur);
919 void Object::ApplyModPositiveFloatValue(uint16 index, float val, bool apply)
921 float cur = GetFloatValue(index);
922 cur += (apply ? val : -val);
923 if(cur < 0)
924 cur = 0;
925 SetFloatValue(index, cur);
928 void Object::SetFlag( uint16 index, uint32 newFlag )
930 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
931 uint32 oldval = m_uint32Values[ index ];
932 uint32 newval = oldval | newFlag;
934 if(oldval != newval)
936 m_uint32Values[ index ] = newval;
938 if(m_inWorld)
940 if(!m_objectUpdated)
942 ObjectAccessor::Instance().AddUpdateObject(this);
943 m_objectUpdated = true;
949 void Object::RemoveFlag( uint16 index, uint32 oldFlag )
951 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
952 uint32 oldval = m_uint32Values[ index ];
953 uint32 newval = oldval & ~oldFlag;
955 if(oldval != newval)
957 m_uint32Values[ index ] = newval;
959 if(m_inWorld)
961 if(!m_objectUpdated)
963 ObjectAccessor::Instance().AddUpdateObject(this);
964 m_objectUpdated = true;
970 void Object::SetByteFlag( uint16 index, uint8 offset, uint8 newFlag )
972 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
974 if(offset > 4)
976 sLog.outError("Object::SetByteFlag: wrong offset %u", offset);
977 return;
980 if(!(uint8(m_uint32Values[ index ] >> (offset * 8)) & newFlag))
982 m_uint32Values[ index ] |= uint32(uint32(newFlag) << (offset * 8));
984 if(m_inWorld)
986 if(!m_objectUpdated)
988 ObjectAccessor::Instance().AddUpdateObject(this);
989 m_objectUpdated = true;
995 void Object::RemoveByteFlag( uint16 index, uint8 offset, uint8 oldFlag )
997 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
999 if(offset > 4)
1001 sLog.outError("Object::RemoveByteFlag: wrong offset %u", offset);
1002 return;
1005 if(uint8(m_uint32Values[ index ] >> (offset * 8)) & oldFlag)
1007 m_uint32Values[ index ] &= ~uint32(uint32(oldFlag) << (offset * 8));
1009 if(m_inWorld)
1011 if(!m_objectUpdated)
1013 ObjectAccessor::Instance().AddUpdateObject(this);
1014 m_objectUpdated = true;
1020 bool Object::PrintIndexError(uint32 index, bool set) const
1022 sLog.outError("Attempt %s non-existed value field: %u (count: %u) for object typeid: %u type mask: %u",(set ? "set value to" : "get value from"),index,m_valuesCount,GetTypeId(),m_objectType);
1024 // assert must fail after function call
1025 return false;
1028 WorldObject::WorldObject()
1029 : m_mapId(0), m_InstanceId(0), m_phaseMask(PHASEMASK_NORMAL),
1030 m_positionX(0.0f), m_positionY(0.0f), m_positionZ(0.0f), m_orientation(0.0f)
1034 void WorldObject::_Create( uint32 guidlow, HighGuid guidhigh, uint32 mapid, uint32 phaseMask )
1036 Object::_Create(guidlow, 0, guidhigh);
1038 m_mapId = mapid;
1039 m_phaseMask = phaseMask;
1042 uint32 WorldObject::GetZoneId() const
1044 return MapManager::Instance().GetBaseMap(m_mapId)->GetZoneId(m_positionX, m_positionY, m_positionZ);
1047 uint32 WorldObject::GetAreaId() const
1049 return MapManager::Instance().GetBaseMap(m_mapId)->GetAreaId(m_positionX, m_positionY, m_positionZ);
1052 void WorldObject::GetZoneAndAreaId(uint32& zoneid, uint32& areaid) const
1054 MapManager::Instance().GetBaseMap(m_mapId)->GetZoneAndAreaId(zoneid, areaid, m_positionX, m_positionY, m_positionZ);
1057 InstanceData* WorldObject::GetInstanceData()
1059 Map *map = GetMap();
1060 return map->IsDungeon() ? ((InstanceMap*)map)->GetInstanceData() : NULL;
1063 //slow
1064 float WorldObject::GetDistance(const WorldObject* obj) const
1066 float dx = GetPositionX() - obj->GetPositionX();
1067 float dy = GetPositionY() - obj->GetPositionY();
1068 float dz = GetPositionZ() - obj->GetPositionZ();
1069 float sizefactor = GetObjectSize() + obj->GetObjectSize();
1070 float dist = sqrt((dx*dx) + (dy*dy) + (dz*dz)) - sizefactor;
1071 return ( dist > 0 ? dist : 0);
1074 float WorldObject::GetDistance2d(float x, float y) const
1076 float dx = GetPositionX() - x;
1077 float dy = GetPositionY() - y;
1078 float sizefactor = GetObjectSize();
1079 float dist = sqrt((dx*dx) + (dy*dy)) - sizefactor;
1080 return ( dist > 0 ? dist : 0);
1083 float WorldObject::GetDistance(float x, float y, float z) const
1085 float dx = GetPositionX() - x;
1086 float dy = GetPositionY() - y;
1087 float dz = GetPositionZ() - z;
1088 float sizefactor = GetObjectSize();
1089 float dist = sqrt((dx*dx) + (dy*dy) + (dz*dz)) - sizefactor;
1090 return ( dist > 0 ? dist : 0);
1093 float WorldObject::GetDistance2d(const WorldObject* obj) const
1095 float dx = GetPositionX() - obj->GetPositionX();
1096 float dy = GetPositionY() - obj->GetPositionY();
1097 float sizefactor = GetObjectSize() + obj->GetObjectSize();
1098 float dist = sqrt((dx*dx) + (dy*dy)) - sizefactor;
1099 return ( dist > 0 ? dist : 0);
1102 float WorldObject::GetDistanceZ(const WorldObject* obj) const
1104 float dz = fabs(GetPositionZ() - obj->GetPositionZ());
1105 float sizefactor = GetObjectSize() + obj->GetObjectSize();
1106 float dist = dz - sizefactor;
1107 return ( dist > 0 ? dist : 0);
1110 bool WorldObject::IsWithinDist3d(float x, float y, float z, float dist2compare) const
1112 float dx = GetPositionX() - x;
1113 float dy = GetPositionY() - y;
1114 float dz = GetPositionZ() - z;
1115 float distsq = dx*dx + dy*dy + dz*dz;
1117 float sizefactor = GetObjectSize();
1118 float maxdist = dist2compare + sizefactor;
1120 return distsq < maxdist * maxdist;
1123 bool WorldObject::IsWithinDist2d(float x, float y, float dist2compare) const
1125 float dx = GetPositionX() - x;
1126 float dy = GetPositionY() - y;
1127 float distsq = dx*dx + dy*dy;
1129 float sizefactor = GetObjectSize();
1130 float maxdist = dist2compare + sizefactor;
1132 return distsq < maxdist * maxdist;
1135 bool WorldObject::_IsWithinDist(WorldObject const* obj, float dist2compare, bool is3D) const
1137 float dx = GetPositionX() - obj->GetPositionX();
1138 float dy = GetPositionY() - obj->GetPositionY();
1139 float distsq = dx*dx + dy*dy;
1140 if(is3D)
1142 float dz = GetPositionZ() - obj->GetPositionZ();
1143 distsq += dz*dz;
1145 float sizefactor = GetObjectSize() + obj->GetObjectSize();
1146 float maxdist = dist2compare + sizefactor;
1148 return distsq < maxdist * maxdist;
1151 bool WorldObject::IsWithinLOSInMap(const WorldObject* obj) const
1153 if (!IsInMap(obj)) return false;
1154 float ox,oy,oz;
1155 obj->GetPosition(ox,oy,oz);
1156 return(IsWithinLOS(ox, oy, oz ));
1159 bool WorldObject::IsWithinLOS(float ox, float oy, float oz) const
1161 float x,y,z;
1162 GetPosition(x,y,z);
1163 VMAP::IVMapManager *vMapManager = VMAP::VMapFactory::createOrGetVMapManager();
1164 return vMapManager->isInLineOfSight(GetMapId(), x, y, z+2.0f, ox, oy, oz+2.0f);
1167 bool WorldObject::GetDistanceOrder(WorldObject const* obj1, WorldObject const* obj2, bool is3D /* = true */) const
1169 float dx1 = GetPositionX() - obj1->GetPositionX();
1170 float dy1 = GetPositionY() - obj1->GetPositionY();
1171 float distsq1 = dx1*dx1 + dy1*dy1;
1172 if(is3D)
1174 float dz1 = GetPositionZ() - obj1->GetPositionZ();
1175 distsq1 += dz1*dz1;
1178 float dx2 = GetPositionX() - obj2->GetPositionX();
1179 float dy2 = GetPositionY() - obj2->GetPositionY();
1180 float distsq2 = dx2*dx2 + dy2*dy2;
1181 if(is3D)
1183 float dz2 = GetPositionZ() - obj2->GetPositionZ();
1184 distsq2 += dz2*dz2;
1187 return distsq1 < distsq2;
1190 bool WorldObject::IsInRange(WorldObject const* obj, float minRange, float maxRange, bool is3D /* = true */) const
1192 float dx = GetPositionX() - obj->GetPositionX();
1193 float dy = GetPositionY() - obj->GetPositionY();
1194 float distsq = dx*dx + dy*dy;
1195 if(is3D)
1197 float dz = GetPositionZ() - obj->GetPositionZ();
1198 distsq += dz*dz;
1201 float sizefactor = GetObjectSize() + obj->GetObjectSize();
1203 // check only for real range
1204 if(minRange > 0.0f)
1206 float mindist = minRange + sizefactor;
1207 if(distsq < mindist * mindist)
1208 return false;
1211 float maxdist = maxRange + sizefactor;
1212 return distsq < maxdist * maxdist;
1215 bool WorldObject::IsInRange2d(float x, float y, float minRange, float maxRange) const
1217 float dx = GetPositionX() - x;
1218 float dy = GetPositionY() - y;
1219 float distsq = dx*dx + dy*dy;
1221 float sizefactor = GetObjectSize();
1223 // check only for real range
1224 if(minRange > 0.0f)
1226 float mindist = minRange + sizefactor;
1227 if(distsq < mindist * mindist)
1228 return false;
1231 float maxdist = maxRange + sizefactor;
1232 return distsq < maxdist * maxdist;
1235 bool WorldObject::IsInRange3d(float x, float y, float z, float minRange, float maxRange) const
1237 float dx = GetPositionX() - x;
1238 float dy = GetPositionY() - y;
1239 float dz = GetPositionZ() - z;
1240 float distsq = dx*dx + dy*dy + dz*dz;
1242 float sizefactor = GetObjectSize();
1244 // check only for real range
1245 if(minRange > 0.0f)
1247 float mindist = minRange + sizefactor;
1248 if(distsq < mindist * mindist)
1249 return false;
1252 float maxdist = maxRange + sizefactor;
1253 return distsq < maxdist * maxdist;
1256 float WorldObject::GetAngle(const WorldObject* obj) const
1258 if(!obj) return 0;
1259 return GetAngle( obj->GetPositionX(), obj->GetPositionY() );
1262 // Return angle in range 0..2*pi
1263 float WorldObject::GetAngle( const float x, const float y ) const
1265 float dx = x - GetPositionX();
1266 float dy = y - GetPositionY();
1268 float ang = atan2(dy, dx);
1269 ang = (ang >= 0) ? ang : 2 * M_PI + ang;
1270 return ang;
1273 bool WorldObject::HasInArc(const float arcangle, const WorldObject* obj) const
1275 // always have self in arc
1276 if(obj == this)
1277 return true;
1279 float arc = arcangle;
1281 // move arc to range 0.. 2*pi
1282 while( arc >= 2.0f * M_PI )
1283 arc -= 2.0f * M_PI;
1284 while( arc < 0 )
1285 arc += 2.0f * M_PI;
1287 float angle = GetAngle( obj );
1288 angle -= m_orientation;
1290 // move angle to range -pi ... +pi
1291 while( angle > M_PI)
1292 angle -= 2.0f * M_PI;
1293 while(angle < -M_PI)
1294 angle += 2.0f * M_PI;
1296 float lborder = -1 * (arc/2.0f); // in range -pi..0
1297 float rborder = (arc/2.0f); // in range 0..pi
1298 return (( angle >= lborder ) && ( angle <= rborder ));
1301 void WorldObject::GetRandomPoint( float x, float y, float z, float distance, float &rand_x, float &rand_y, float &rand_z) const
1303 if(distance==0)
1305 rand_x = x;
1306 rand_y = y;
1307 rand_z = z;
1308 return;
1311 // angle to face `obj` to `this`
1312 float angle = rand_norm()*2*M_PI;
1313 float new_dist = rand_norm()*distance;
1315 rand_x = x + new_dist * cos(angle);
1316 rand_y = y + new_dist * sin(angle);
1317 rand_z = z;
1319 MaNGOS::NormalizeMapCoord(rand_x);
1320 MaNGOS::NormalizeMapCoord(rand_y);
1321 UpdateGroundPositionZ(rand_x,rand_y,rand_z); // update to LOS height if available
1324 void WorldObject::UpdateGroundPositionZ(float x, float y, float &z) const
1326 float new_z = MapManager::Instance().GetBaseMap(GetMapId())->GetHeight(x,y,z,true);
1327 if(new_z > INVALID_HEIGHT)
1328 z = new_z+ 0.05f; // just to be sure that we are not a few pixel under the surface
1331 bool WorldObject::IsPositionValid() const
1333 return MaNGOS::IsValidMapCoord(m_positionX,m_positionY,m_positionZ,m_orientation);
1336 void WorldObject::MonsterSay(const char* text, uint32 language, uint64 TargetGuid)
1338 WorldPacket data(SMSG_MESSAGECHAT, 200);
1339 BuildMonsterChat(&data,CHAT_MSG_MONSTER_SAY,text,language,GetName(),TargetGuid);
1340 SendMessageToSetInRange(&data,sWorld.getConfig(CONFIG_LISTEN_RANGE_SAY),true);
1343 void WorldObject::MonsterYell(const char* text, uint32 language, uint64 TargetGuid)
1345 WorldPacket data(SMSG_MESSAGECHAT, 200);
1346 BuildMonsterChat(&data,CHAT_MSG_MONSTER_YELL,text,language,GetName(),TargetGuid);
1347 SendMessageToSetInRange(&data,sWorld.getConfig(CONFIG_LISTEN_RANGE_YELL),true);
1350 void WorldObject::MonsterTextEmote(const char* text, uint64 TargetGuid, bool IsBossEmote)
1352 WorldPacket data(SMSG_MESSAGECHAT, 200);
1353 BuildMonsterChat(&data,IsBossEmote ? CHAT_MSG_RAID_BOSS_EMOTE : CHAT_MSG_MONSTER_EMOTE,text,LANG_UNIVERSAL,GetName(),TargetGuid);
1354 SendMessageToSetInRange(&data,sWorld.getConfig(CONFIG_LISTEN_RANGE_TEXTEMOTE),true);
1357 void WorldObject::MonsterWhisper(const char* text, uint64 receiver, bool IsBossWhisper)
1359 Player *player = objmgr.GetPlayer(receiver);
1360 if(!player || !player->GetSession())
1361 return;
1363 WorldPacket data(SMSG_MESSAGECHAT, 200);
1364 BuildMonsterChat(&data,IsBossWhisper ? CHAT_MSG_RAID_BOSS_WHISPER : CHAT_MSG_MONSTER_WHISPER,text,LANG_UNIVERSAL,GetName(),receiver);
1366 player->GetSession()->SendPacket(&data);
1369 namespace MaNGOS
1371 class MonsterChatBuilder
1373 public:
1374 MonsterChatBuilder(WorldObject const& obj, ChatMsg msgtype, int32 textId, uint32 language, uint64 targetGUID)
1375 : i_object(obj), i_msgtype(msgtype), i_textId(textId), i_language(language), i_targetGUID(targetGUID) {}
1376 void operator()(WorldPacket& data, int32 loc_idx)
1378 char const* text = objmgr.GetMangosString(i_textId,loc_idx);
1380 // TODO: i_object.GetName() also must be localized?
1381 i_object.BuildMonsterChat(&data,i_msgtype,text,i_language,i_object.GetNameForLocaleIdx(loc_idx),i_targetGUID);
1384 private:
1385 WorldObject const& i_object;
1386 ChatMsg i_msgtype;
1387 int32 i_textId;
1388 uint32 i_language;
1389 uint64 i_targetGUID;
1391 } // namespace MaNGOS
1393 void WorldObject::MonsterSay(int32 textId, uint32 language, uint64 TargetGuid)
1395 CellPair p = MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY());
1397 Cell cell(p);
1398 cell.data.Part.reserved = ALL_DISTRICT;
1399 cell.SetNoCreate();
1401 MaNGOS::MonsterChatBuilder say_build(*this, CHAT_MSG_MONSTER_SAY, textId,language,TargetGuid);
1402 MaNGOS::LocalizedPacketDo<MaNGOS::MonsterChatBuilder> say_do(say_build);
1403 MaNGOS::PlayerDistWorker<MaNGOS::LocalizedPacketDo<MaNGOS::MonsterChatBuilder> > say_worker(this,sWorld.getConfig(CONFIG_LISTEN_RANGE_SAY),say_do);
1404 TypeContainerVisitor<MaNGOS::PlayerDistWorker<MaNGOS::LocalizedPacketDo<MaNGOS::MonsterChatBuilder> >, WorldTypeMapContainer > message(say_worker);
1405 CellLock<GridReadGuard> cell_lock(cell, p);
1406 cell_lock->Visit(cell_lock, message, *GetMap());
1409 void WorldObject::MonsterYell(int32 textId, uint32 language, uint64 TargetGuid)
1411 CellPair p = MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY());
1413 Cell cell(p);
1414 cell.data.Part.reserved = ALL_DISTRICT;
1415 cell.SetNoCreate();
1417 MaNGOS::MonsterChatBuilder say_build(*this, CHAT_MSG_MONSTER_YELL, textId,language,TargetGuid);
1418 MaNGOS::LocalizedPacketDo<MaNGOS::MonsterChatBuilder> say_do(say_build);
1419 MaNGOS::PlayerDistWorker<MaNGOS::LocalizedPacketDo<MaNGOS::MonsterChatBuilder> > say_worker(this,sWorld.getConfig(CONFIG_LISTEN_RANGE_YELL),say_do);
1420 TypeContainerVisitor<MaNGOS::PlayerDistWorker<MaNGOS::LocalizedPacketDo<MaNGOS::MonsterChatBuilder> >, WorldTypeMapContainer > message(say_worker);
1421 CellLock<GridReadGuard> cell_lock(cell, p);
1422 cell_lock->Visit(cell_lock, message, *GetMap());
1425 void WorldObject::MonsterYellToZone(int32 textId, uint32 language, uint64 TargetGuid)
1427 MaNGOS::MonsterChatBuilder say_build(*this, CHAT_MSG_MONSTER_YELL, textId,language,TargetGuid);
1428 MaNGOS::LocalizedPacketDo<MaNGOS::MonsterChatBuilder> say_do(say_build);
1430 uint32 zoneid = GetZoneId();
1432 Map::PlayerList const& pList = GetMap()->GetPlayers();
1433 for(Map::PlayerList::const_iterator itr = pList.begin(); itr != pList.end(); ++itr)
1434 if(itr->getSource()->GetZoneId()==zoneid)
1435 say_do(itr->getSource());
1438 void WorldObject::MonsterTextEmote(int32 textId, uint64 TargetGuid, bool IsBossEmote)
1440 CellPair p = MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY());
1442 Cell cell(p);
1443 cell.data.Part.reserved = ALL_DISTRICT;
1444 cell.SetNoCreate();
1446 MaNGOS::MonsterChatBuilder say_build(*this, IsBossEmote ? CHAT_MSG_RAID_BOSS_EMOTE : CHAT_MSG_MONSTER_EMOTE, textId,LANG_UNIVERSAL,TargetGuid);
1447 MaNGOS::LocalizedPacketDo<MaNGOS::MonsterChatBuilder> say_do(say_build);
1448 MaNGOS::PlayerDistWorker<MaNGOS::LocalizedPacketDo<MaNGOS::MonsterChatBuilder> > say_worker(this,sWorld.getConfig(CONFIG_LISTEN_RANGE_TEXTEMOTE),say_do);
1449 TypeContainerVisitor<MaNGOS::PlayerDistWorker<MaNGOS::LocalizedPacketDo<MaNGOS::MonsterChatBuilder> >, WorldTypeMapContainer > message(say_worker);
1450 CellLock<GridReadGuard> cell_lock(cell, p);
1451 cell_lock->Visit(cell_lock, message, *GetMap());
1454 void WorldObject::MonsterWhisper(int32 textId, uint64 receiver, bool IsBossWhisper)
1456 Player *player = objmgr.GetPlayer(receiver);
1457 if(!player || !player->GetSession())
1458 return;
1460 uint32 loc_idx = player->GetSession()->GetSessionDbLocaleIndex();
1461 char const* text = objmgr.GetMangosString(textId,loc_idx);
1463 WorldPacket data(SMSG_MESSAGECHAT, 200);
1464 BuildMonsterChat(&data,IsBossWhisper ? CHAT_MSG_RAID_BOSS_WHISPER : CHAT_MSG_MONSTER_WHISPER,text,LANG_UNIVERSAL,GetNameForLocaleIdx(loc_idx),receiver);
1466 player->GetSession()->SendPacket(&data);
1469 void WorldObject::BuildMonsterChat(WorldPacket *data, uint8 msgtype, char const* text, uint32 language, char const* name, uint64 targetGuid) const
1471 bool pre = (msgtype==CHAT_MSG_MONSTER_EMOTE || msgtype==CHAT_MSG_RAID_BOSS_EMOTE);
1473 *data << (uint8)msgtype;
1474 *data << (uint32)language;
1475 *data << (uint64)GetGUID();
1476 *data << (uint32)0; // 2.1.0
1477 *data << (uint32)(strlen(name)+1);
1478 *data << name;
1479 *data << (uint64)targetGuid; // Unit Target
1480 if( targetGuid && !IS_PLAYER_GUID(targetGuid) )
1482 *data << (uint32)1; // target name length
1483 *data << (uint8)0; // target name
1485 *data << (uint32)(strlen(text)+1+(pre?3:0));
1486 if(pre)
1487 data->append("%s ",3);
1488 *data << text;
1489 *data << (uint8)0; // ChatTag
1492 void WorldObject::BuildHeartBeatMsg(WorldPacket *data) const
1494 //Heartbeat message cannot be used for non-units
1495 if (!isType(TYPEMASK_UNIT))
1496 return;
1498 data->Initialize(MSG_MOVE_HEARTBEAT, 32);
1499 data->append(GetPackGUID());
1500 *data << uint32(((Unit*)this)->GetUnitMovementFlags()); // movement flags
1501 *data << uint16(0); // 2.3.0
1502 *data << uint32(getMSTime()); // time
1503 *data << m_positionX;
1504 *data << m_positionY;
1505 *data << m_positionZ;
1506 *data << m_orientation;
1507 *data << uint32(0);
1510 void WorldObject::BuildTeleportAckMsg(WorldPacket *data, float x, float y, float z, float ang) const
1512 //TeleportAck message cannot be used for non-units
1513 if (!isType(TYPEMASK_UNIT))
1514 return;
1516 data->Initialize(MSG_MOVE_TELEPORT_ACK, 41);
1517 data->append(GetPackGUID());
1518 *data << uint32(0); // this value increments every time
1519 *data << uint32(((Unit*)this)->GetUnitMovementFlags()); // movement flags
1520 *data << uint16(0); // 2.3.0
1521 *data << uint32(getMSTime()); // time
1522 *data << x;
1523 *data << y;
1524 *data << z;
1525 *data << ang;
1526 *data << uint32(0);
1529 void WorldObject::SendMessageToSet(WorldPacket *data, bool /*bToSelf*/)
1531 GetMap()->MessageBroadcast(this, data);
1534 void WorldObject::SendMessageToSetInRange(WorldPacket *data, float dist, bool /*bToSelf*/)
1536 GetMap()->MessageDistBroadcast(this, data, dist);
1539 void WorldObject::SendObjectDeSpawnAnim(uint64 guid)
1541 WorldPacket data(SMSG_GAMEOBJECT_DESPAWN_ANIM, 8);
1542 data << guid;
1543 SendMessageToSet(&data, true);
1546 Map* WorldObject::GetMap() const
1548 return MapManager::Instance().GetMap(GetMapId(), this);
1551 Map const* WorldObject::GetBaseMap() const
1553 return MapManager::Instance().GetBaseMap(GetMapId());
1556 void WorldObject::AddObjectToRemoveList()
1558 Map* map = GetMap();
1559 if(!map)
1561 sLog.outError("Object (TypeId: %u Entry: %u GUID: %u) at attempt add to move list not have valid map (Id: %u).",GetTypeId(),GetEntry(),GetGUIDLow(),GetMapId());
1562 return;
1565 map->AddObjectToRemoveList(this);
1568 Creature* WorldObject::SummonCreature(uint32 id, float x, float y, float z, float ang,TempSummonType spwtype,uint32 despwtime)
1570 TemporarySummon* pCreature = new TemporarySummon(GetGUID());
1572 pCreature->SetInstanceId(GetInstanceId());
1573 uint32 team = 0;
1574 if (GetTypeId()==TYPEID_PLAYER)
1575 team = ((Player*)this)->GetTeam();
1577 if (!pCreature->Create(objmgr.GenerateLowGuid(HIGHGUID_UNIT), GetMap(), GetPhaseMask(), id, team))
1579 delete pCreature;
1580 return NULL;
1583 if (x == 0.0f && y == 0.0f && z == 0.0f)
1584 GetClosePoint(x, y, z, pCreature->GetObjectSize());
1586 pCreature->Relocate(x, y, z, ang);
1588 if(!pCreature->IsPositionValid())
1590 sLog.outError("Creature (guidlow %d, entry %d) not summoned. Suggested coordinates isn't valid (X: %f Y: %f)",pCreature->GetGUIDLow(),pCreature->GetEntry(),pCreature->GetPositionX(),pCreature->GetPositionY());
1591 delete pCreature;
1592 return NULL;
1595 pCreature->Summon(spwtype, despwtime);
1597 if(GetTypeId()==TYPEID_UNIT && ((Creature*)this)->AI())
1598 ((Creature*)this)->AI()->JustSummoned(pCreature);
1600 // return the creature therewith the summoner has access to it
1601 return pCreature;
1604 namespace MaNGOS
1606 class NearUsedPosDo
1608 public:
1609 NearUsedPosDo(WorldObject const& obj, WorldObject const* searcher, float angle, ObjectPosSelector& selector)
1610 : i_object(obj), i_searcher(searcher), i_angle(angle), i_selector(selector) {}
1612 void operator()(Corpse*) const {}
1613 void operator()(DynamicObject*) const {}
1615 void operator()(Creature* c) const
1617 // skip self or target
1618 if(c==i_searcher || c==&i_object)
1619 return;
1621 float x,y,z;
1623 if( !c->isAlive() || c->hasUnitState(UNIT_STAT_ROOT | UNIT_STAT_STUNNED | UNIT_STAT_DISTRACTED) ||
1624 !c->GetMotionMaster()->GetDestination(x,y,z) )
1626 x = c->GetPositionX();
1627 y = c->GetPositionY();
1630 add(c,x,y);
1633 template<class T>
1634 void operator()(T* u) const
1636 // skip self or target
1637 if(u==i_searcher || u==&i_object)
1638 return;
1640 float x,y;
1642 x = u->GetPositionX();
1643 y = u->GetPositionY();
1645 add(u,x,y);
1648 // we must add used pos that can fill places around center
1649 void add(WorldObject* u, float x, float y) const
1651 // u is too nearest/far away to i_object
1652 if(!i_object.IsInRange2d(x,y,i_selector.m_dist - i_selector.m_size,i_selector.m_dist + i_selector.m_size))
1653 return;
1655 float angle = i_object.GetAngle(u)-i_angle;
1657 // move angle to range -pi ... +pi
1658 while( angle > M_PI)
1659 angle -= 2.0f * M_PI;
1660 while(angle < -M_PI)
1661 angle += 2.0f * M_PI;
1663 // dist include size of u
1664 float dist2d = i_object.GetDistance2d(x,y);
1665 i_selector.AddUsedPos(u->GetObjectSize(),angle,dist2d + i_object.GetObjectSize());
1667 private:
1668 WorldObject const& i_object;
1669 WorldObject const* i_searcher;
1670 float i_angle;
1671 ObjectPosSelector& i_selector;
1673 } // namespace MaNGOS
1675 //===================================================================================================
1677 void WorldObject::GetNearPoint2D(float &x, float &y, float distance2d, float absAngle ) const
1679 x = GetPositionX() + (GetObjectSize() + distance2d) * cos(absAngle);
1680 y = GetPositionY() + (GetObjectSize() + distance2d) * sin(absAngle);
1682 MaNGOS::NormalizeMapCoord(x);
1683 MaNGOS::NormalizeMapCoord(y);
1686 void WorldObject::GetNearPoint(WorldObject const* searcher, float &x, float &y, float &z, float searcher_size, float distance2d, float absAngle ) const
1688 GetNearPoint2D(x,y,distance2d+searcher_size,absAngle);
1689 z = GetPositionZ();
1691 // if detection disabled, return first point
1692 if(!sWorld.getConfig(CONFIG_DETECT_POS_COLLISION))
1694 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1695 return;
1698 // or remember first point
1699 float first_x = x;
1700 float first_y = y;
1701 bool first_los_conflict = false; // first point LOS problems
1703 // prepare selector for work
1704 ObjectPosSelector selector(GetPositionX(),GetPositionY(),GetObjectSize(),distance2d+searcher_size);
1706 // adding used positions around object
1708 CellPair p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
1709 Cell cell(p);
1710 cell.data.Part.reserved = ALL_DISTRICT;
1711 cell.SetNoCreate();
1713 MaNGOS::NearUsedPosDo u_do(*this,searcher,absAngle,selector);
1714 MaNGOS::WorldObjectWorker<MaNGOS::NearUsedPosDo> worker(this,u_do);
1716 TypeContainerVisitor<MaNGOS::WorldObjectWorker<MaNGOS::NearUsedPosDo>, GridTypeMapContainer > grid_obj_worker(worker);
1717 TypeContainerVisitor<MaNGOS::WorldObjectWorker<MaNGOS::NearUsedPosDo>, WorldTypeMapContainer > world_obj_worker(worker);
1719 CellLock<GridReadGuard> cell_lock(cell, p);
1720 cell_lock->Visit(cell_lock, grid_obj_worker, *GetMap());
1721 cell_lock->Visit(cell_lock, world_obj_worker, *GetMap());
1724 // maybe can just place in primary position
1725 if( selector.CheckOriginal() )
1727 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1729 if(IsWithinLOS(x,y,z))
1730 return;
1732 first_los_conflict = true; // first point have LOS problems
1735 float angle; // candidate of angle for free pos
1737 // special case when one from list empty and then empty side preferred
1738 if(selector.FirstAngle(angle))
1740 GetNearPoint2D(x,y,distance2d,absAngle+angle);
1741 z = GetPositionZ();
1742 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1744 if(IsWithinLOS(x,y,z))
1745 return;
1748 // set first used pos in lists
1749 selector.InitializeAngle();
1751 // select in positions after current nodes (selection one by one)
1752 while(selector.NextAngle(angle)) // angle for free pos
1754 GetNearPoint2D(x,y,distance2d,absAngle+angle);
1755 z = GetPositionZ();
1756 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1758 if(IsWithinLOS(x,y,z))
1759 return;
1762 // BAD NEWS: not free pos (or used or have LOS problems)
1763 // Attempt find _used_ pos without LOS problem
1765 if(!first_los_conflict)
1767 x = first_x;
1768 y = first_y;
1770 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1771 return;
1774 // special case when one from list empty and then empty side preferred
1775 if( selector.IsNonBalanced() )
1777 if(!selector.FirstAngle(angle)) // _used_ pos
1779 GetNearPoint2D(x,y,distance2d,absAngle+angle);
1780 z = GetPositionZ();
1781 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1783 if(IsWithinLOS(x,y,z))
1784 return;
1788 // set first used pos in lists
1789 selector.InitializeAngle();
1791 // select in positions after current nodes (selection one by one)
1792 while(selector.NextUsedAngle(angle)) // angle for used pos but maybe without LOS problem
1794 GetNearPoint2D(x,y,distance2d,absAngle+angle);
1795 z = GetPositionZ();
1796 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1798 if(IsWithinLOS(x,y,z))
1799 return;
1802 // BAD BAD NEWS: all found pos (free and used) have LOS problem :(
1803 x = first_x;
1804 y = first_y;
1806 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1809 void WorldObject::SetPhaseMask(uint32 newPhaseMask, bool update)
1811 m_phaseMask = newPhaseMask;
1813 if(update && IsInWorld())
1814 ObjectAccessor::UpdateObjectVisibility(this);
1817 void WorldObject::PlayDistanceSound( uint32 sound_id, Player* target /*= NULL*/ )
1819 WorldPacket data(SMSG_PLAY_OBJECT_SOUND,4+8);
1820 data << uint32(sound_id);
1821 data << uint64(GetGUID());
1822 if (target)
1823 target->SendDirectMessage( &data );
1824 else
1825 SendMessageToSet( &data, true );
1828 void WorldObject::PlayDirectSound( uint32 sound_id, Player* target /*= NULL*/ )
1830 WorldPacket data(SMSG_PLAY_SOUND, 4);
1831 data << uint32(sound_id);
1832 if (target)
1833 target->SendDirectMessage( &data );
1834 else
1835 SendMessageToSet( &data, true );