Fixed pvp flag
[getmangos.git] / src / game / Object.cpp
blob67b5d778217ab8810453c7d293fe5054d0ee4194
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 #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 "ObjectMgr.h"
29 #include "WorldSession.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;
62 return 10; // unknown
65 Object::Object( )
67 m_objectTypeId = TYPEID_OBJECT;
68 m_objectType = TYPEMASK_OBJECT;
70 m_uint32Values = 0;
71 m_uint32Values_mirror = 0;
72 m_valuesCount = 0;
74 m_inWorld = false;
75 m_objectUpdated = false;
77 m_PackGUID.clear();
78 m_PackGUID.appendPackGUID(0);
81 Object::~Object( )
83 if(m_objectUpdated)
84 ObjectAccessor::Instance().RemoveUpdateObject(this);
86 if(m_uint32Values)
88 if(IsInWorld())
90 ///- Do NOT call RemoveFromWorld here, if the object is a player it will crash
91 sLog.outError("Object::~Object - guid="I64FMTD", typeid=%d deleted but still in world!!", GetGUID(), GetTypeId());
92 //assert(0);
95 //DEBUG_LOG("Object desctr 1 check (%p)",(void*)this);
96 delete [] m_uint32Values;
97 delete [] m_uint32Values_mirror;
98 //DEBUG_LOG("Object desctr 2 check (%p)",(void*)this);
102 void Object::_InitValues()
104 m_uint32Values = new uint32[ m_valuesCount ];
105 memset(m_uint32Values, 0, m_valuesCount*sizeof(uint32));
107 m_uint32Values_mirror = new uint32[ m_valuesCount ];
108 memset(m_uint32Values_mirror, 0, m_valuesCount*sizeof(uint32));
110 m_objectUpdated = false;
113 void Object::_Create( uint32 guidlow, uint32 entry, HighGuid guidhigh )
115 if(!m_uint32Values) _InitValues();
117 uint64 guid = MAKE_NEW_GUID(guidlow, entry, guidhigh); // required more changes to make it working
118 SetUInt64Value( OBJECT_FIELD_GUID, guid );
119 SetUInt32Value( OBJECT_FIELD_TYPE, m_objectType );
120 m_PackGUID.clear();
121 m_PackGUID.appendPackGUID(GetGUID());
124 void Object::BuildMovementUpdateBlock(UpdateData * data, uint32 flags ) const
126 ByteBuffer buf(500);
128 buf << uint8( UPDATETYPE_MOVEMENT );
129 buf << GetGUID();
131 _BuildMovementUpdate(&buf, flags, 0x00000000);
133 data->AddUpdateBlock(buf);
136 void Object::BuildCreateUpdateBlockForPlayer(UpdateData *data, Player *target) const
138 if(!target)
140 return;
143 uint8 updatetype = UPDATETYPE_CREATE_OBJECT;
144 uint8 flags = m_updateFlag;
145 uint32 flags2 = 0;
147 /** lower flag1 **/
148 if(target == this) // building packet for oneself
150 flags |= UPDATEFLAG_SELF;
152 /*** temporary reverted - until real source of stack corruption will not found
153 updatetype = UPDATETYPE_CREATE_OBJECT2;
154 ****/
157 if(flags & UPDATEFLAG_HASPOSITION)
159 // UPDATETYPE_CREATE_OBJECT2 dynamic objects, corpses...
160 if(isType(TYPEMASK_DYNAMICOBJECT) || isType(TYPEMASK_CORPSE) || isType(TYPEMASK_PLAYER))
161 updatetype = UPDATETYPE_CREATE_OBJECT2;
163 // UPDATETYPE_CREATE_OBJECT2 for pets...
164 if(target->GetPetGUID() == GetGUID())
165 updatetype = UPDATETYPE_CREATE_OBJECT2;
167 // UPDATETYPE_CREATE_OBJECT2 for some gameobject types...
168 if(isType(TYPEMASK_GAMEOBJECT))
170 switch(((GameObject*)this)->GetGoType())
172 case GAMEOBJECT_TYPE_TRAP:
173 case GAMEOBJECT_TYPE_DUEL_ARBITER:
174 case GAMEOBJECT_TYPE_FLAGSTAND:
175 case GAMEOBJECT_TYPE_FLAGDROP:
176 updatetype = UPDATETYPE_CREATE_OBJECT2;
177 break;
178 case GAMEOBJECT_TYPE_TRANSPORT:
179 flags |= UPDATEFLAG_TRANSPORT;
180 break;
185 //sLog.outDebug("BuildCreateUpdate: update-type: %u, object-type: %u got flags: %X, flags2: %X", updatetype, m_objectTypeId, flags, flags2);
187 ByteBuffer buf(500);
188 buf << (uint8)updatetype;
189 //buf.append(GetPackGUID()); //client crashes when using this
190 buf << (uint8)0xFF << GetGUID();
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);
222 // now object updated/(create updated)
225 void Object::BuildValuesUpdateBlockForPlayer(UpdateData *data, Player *target) const
227 ByteBuffer buf(500);
229 buf << (uint8) UPDATETYPE_VALUES;
230 //buf.append(GetPackGUID()); //client crashes when using this. but not have crash in debug mode
231 buf << (uint8)0xFF;
232 buf << GetGUID();
234 UpdateMask updateMask;
235 updateMask.SetCount( m_valuesCount );
237 _SetUpdateBits( &updateMask, target );
238 _BuildValuesUpdate(UPDATETYPE_VALUES, &buf, &updateMask, target );
240 data->AddUpdateBlock(buf);
243 void Object::BuildOutOfRangeUpdateBlock(UpdateData * data) const
245 data->AddOutOfRangeGUID(GetGUID());
248 void Object::DestroyForPlayer(Player *target) const
250 ASSERT(target);
252 WorldPacket data(SMSG_DESTROY_OBJECT, 8);
253 data << GetGUID();
254 data << uint8(0); // WotLK (bool)
255 target->GetSession()->SendPacket( &data );
258 void Object::_BuildMovementUpdate(ByteBuffer * data, uint8 flags, uint32 flags2 ) const
260 uint16 unk_flags = ((GetTypeId() == TYPEID_PLAYER) ? ((Player*)this)->m_movementInfo.unk1 : 0);
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_HASPOSITION)
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 << (uint8)((Player*)this)->GetTransUnk();
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_SWIMBACK );
384 *data << ((Unit*)this)->GetSpeed( MOVE_SWIM );
385 *data << ((Unit*)this)->GetSpeed( MOVE_WALKBACK );
386 *data << ((Unit*)this)->GetSpeed( MOVE_FLY );
387 *data << ((Unit*)this)->GetSpeed( MOVE_FLYBACK );
388 *data << ((Unit*)this)->GetSpeed( MOVE_TURN );
389 *data << float(7);
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 /*for(uint32 i = 0; i < poscount; i++)
456 // path points
457 *data << (float)0;
458 *data << (float)0;
459 *data << (float)0;
462 *data << path.GetNodes()[poscount-1].x;
463 *data << path.GetNodes()[poscount-1].y;
464 *data << path.GetNodes()[poscount-1].z;
466 // target position (path end)
467 /**data << ((Unit*)this)->GetPositionX();
468 *data << ((Unit*)this)->GetPositionY();
469 *data << ((Unit*)this)->GetPositionZ();*/
473 // 0x8
474 if(flags & UPDATEFLAG_LOWGUID)
476 switch(GetTypeId())
478 case TYPEID_OBJECT:
479 case TYPEID_ITEM:
480 case TYPEID_CONTAINER:
481 case TYPEID_GAMEOBJECT:
482 case TYPEID_DYNAMICOBJECT:
483 case TYPEID_CORPSE:
484 *data << uint32(GetGUIDLow()); // GetGUIDLow()
485 break;
486 case TYPEID_UNIT:
487 *data << uint32(0x0000000B); // unk, can be 0xB or 0xC
488 break;
489 case TYPEID_PLAYER:
490 if(flags & UPDATEFLAG_SELF)
491 *data << uint32(0x0000002F); // unk, can be 0x15 or 0x22
492 else
493 *data << uint32(0x00000008); // unk, can be 0x7 or 0x8
494 break;
495 default:
496 *data << uint32(0x00000000); // unk
497 break;
501 // 0x10
502 if(flags & UPDATEFLAG_HIGHGUID)
504 switch(GetTypeId())
506 case TYPEID_OBJECT:
507 case TYPEID_ITEM:
508 case TYPEID_CONTAINER:
509 case TYPEID_GAMEOBJECT:
510 case TYPEID_DYNAMICOBJECT:
511 case TYPEID_CORPSE:
512 *data << uint32(GetGUIDHigh()); // GetGUIDHigh()
513 break;
514 case TYPEID_UNIT:
515 *data << uint32(-1);
516 break;
517 case TYPEID_PLAYER:
518 if(flags & UPDATEFLAG_SELF)
519 *data << uint32(0x0000002F); // unk, can be 0x15 or 0x22
520 else
521 *data << uint32(-1); // unk, can be 0x7 or 0x8
522 break;
523 default:
524 *data << uint32(0x00000000); // unk
525 break;
529 // 0x4
530 if(flags & UPDATEFLAG_FULLGUID)
532 *data << uint8(0); // packed guid (probably target guid)
535 // 0x2
536 if(flags & UPDATEFLAG_TRANSPORT)
538 *data << uint32(getMSTime()); // ms time
541 // 0x80
542 if(flags & UPDATEFLAG_WOTLK) // unused for now
544 *data << float(0);
545 *data << uint32(0);
549 void Object::_BuildValuesUpdate(uint8 updatetype, ByteBuffer * data, UpdateMask *updateMask, Player *target) const
551 if(!target)
552 return;
554 bool IsActivateToQuest = false;
555 if (updatetype == UPDATETYPE_CREATE_OBJECT || updatetype == UPDATETYPE_CREATE_OBJECT2)
557 if (isType(TYPEMASK_GAMEOBJECT) && !((GameObject*)this)->IsTransport())
559 if ( ((GameObject*)this)->ActivateToQuest(target) || target->isGameMaster())
561 IsActivateToQuest = true;
562 updateMask->SetBit(GAMEOBJECT_DYNAMIC);
566 else //case UPDATETYPE_VALUES
568 if (isType(TYPEMASK_GAMEOBJECT) && !((GameObject*)this)->IsTransport())
570 if ( ((GameObject*)this)->ActivateToQuest(target) || target->isGameMaster())
572 IsActivateToQuest = true;
574 updateMask->SetBit(GAMEOBJECT_DYNAMIC);
575 updateMask->SetBit(GAMEOBJECT_BYTES_1);
579 WPAssert(updateMask && updateMask->GetCount() == m_valuesCount);
581 *data << (uint8)updateMask->GetBlockCount();
582 data->append( updateMask->GetMask(), updateMask->GetLength() );
584 // 2 specialized loops for speed optimization in non-unit case
585 if(isType(TYPEMASK_UNIT)) // unit (creature/player) case
587 for( uint16 index = 0; index < m_valuesCount; index ++ )
589 if( updateMask->GetBit( index ) )
591 // remove custom flag before send
592 if( index == UNIT_NPC_FLAGS )
593 *data << uint32(m_uint32Values[ index ] & ~UNIT_NPC_FLAG_GUARD);
594 // FIXME: Some values at server stored in float format but must be sent to client in uint32 format
595 else if(index >= UNIT_FIELD_BASEATTACKTIME && index <= UNIT_FIELD_RANGEDATTACKTIME)
597 // convert from float to uint32 and send
598 *data << uint32(m_floatValues[ index ] < 0 ? 0 : m_floatValues[ index ]);
600 // there are some float values which may be negative or can't get negative due to other checks
601 else if(index >= UNIT_FIELD_NEGSTAT0 && index <= UNIT_FIELD_NEGSTAT4 ||
602 index >= UNIT_FIELD_RESISTANCEBUFFMODSPOSITIVE && index <= (UNIT_FIELD_RESISTANCEBUFFMODSPOSITIVE + 6) ||
603 index >= UNIT_FIELD_RESISTANCEBUFFMODSNEGATIVE && index <= (UNIT_FIELD_RESISTANCEBUFFMODSNEGATIVE + 6) ||
604 index >= UNIT_FIELD_POSSTAT0 && index <= UNIT_FIELD_POSSTAT4)
606 *data << uint32(m_floatValues[ index ]);
608 // Gamemasters should be always able to select units - remove not selectable flag
609 else if(index == UNIT_FIELD_FLAGS && target->isGameMaster())
611 *data << (m_uint32Values[ index ] & ~UNIT_FLAG_NOT_SELECTABLE);
613 // hide lootable animation for unallowed players
614 else if(index == UNIT_DYNAMIC_FLAGS && GetTypeId() == TYPEID_UNIT)
616 if(!target->isAllowedToLoot((Creature*)this))
617 *data << (m_uint32Values[ index ] & ~UNIT_DYNFLAG_LOOTABLE);
618 else
619 *data << (m_uint32Values[ index ] & ~UNIT_DYNFLAG_OTHER_TAGGER);
621 else
623 // send in current format (float as float, uint32 as uint32)
624 *data << m_uint32Values[ index ];
629 else if(isType(TYPEMASK_GAMEOBJECT)) // gameobject case
631 for( uint16 index = 0; index < m_valuesCount; index ++ )
633 if( updateMask->GetBit( index ) )
635 // send in current format (float as float, uint32 as uint32)
636 if ( index == GAMEOBJECT_DYNAMIC )
638 if(IsActivateToQuest )
640 switch(((GameObject*)this)->GetGoType())
642 case GAMEOBJECT_TYPE_CHEST:
643 *data << uint32(9); // enable quest object. Represent 9, but 1 for client before 2.3.0
644 break;
645 case GAMEOBJECT_TYPE_GOOBER:
646 *data << uint32(1);
647 break;
648 default:
649 *data << uint32(0); //unknown. not happen.
650 break;
653 else
654 *data << uint32(0); // disable quest object
656 else
657 *data << m_uint32Values[ index ]; // other cases
661 else // other objects case (no special index checks)
663 for( uint16 index = 0; index < m_valuesCount; index ++ )
665 if( updateMask->GetBit( index ) )
667 // send in current format (float as float, uint32 as uint32)
668 *data << m_uint32Values[ index ];
674 void Object::ClearUpdateMask(bool remove)
676 for( uint16 index = 0; index < m_valuesCount; index ++ )
678 if(m_uint32Values_mirror[index]!= m_uint32Values[index])
679 m_uint32Values_mirror[index] = m_uint32Values[index];
681 if(m_objectUpdated)
683 if(remove)
684 ObjectAccessor::Instance().RemoveUpdateObject(this);
685 m_objectUpdated = false;
689 // Send current value fields changes to all viewers
690 void Object::SendUpdateObjectToAllExcept(Player* exceptPlayer)
692 // changes will be send in create packet
693 if(!IsInWorld())
694 return;
696 // nothing do
697 if(!m_objectUpdated)
698 return;
700 ObjectAccessor::UpdateObject(this,exceptPlayer);
703 bool Object::LoadValues(const char* data)
705 if(!m_uint32Values) _InitValues();
707 Tokens tokens = StrSplit(data, " ");
709 if(tokens.size() != m_valuesCount)
710 return false;
712 Tokens::iterator iter;
713 int index;
714 for (iter = tokens.begin(), index = 0; index < m_valuesCount; ++iter, ++index)
716 m_uint32Values[index] = atol((*iter).c_str());
719 return true;
722 void Object::_SetUpdateBits(UpdateMask *updateMask, Player* /*target*/) const
724 for( uint16 index = 0; index < m_valuesCount; index ++ )
726 if(m_uint32Values_mirror[index]!= m_uint32Values[index])
727 updateMask->SetBit(index);
731 void Object::_SetCreateBits(UpdateMask *updateMask, Player* /*target*/) const
733 for( uint16 index = 0; index < m_valuesCount; index++ )
735 if(GetUInt32Value(index) != 0)
736 updateMask->SetBit(index);
740 void Object::SetInt32Value( uint16 index, int32 value )
742 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
744 if(m_int32Values[ index ] != value)
746 m_int32Values[ index ] = value;
748 if(m_inWorld)
750 if(!m_objectUpdated)
752 ObjectAccessor::Instance().AddUpdateObject(this);
753 m_objectUpdated = true;
759 void Object::SetUInt32Value( uint16 index, uint32 value )
761 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
763 if(m_uint32Values[ index ] != value)
765 m_uint32Values[ index ] = value;
767 if(m_inWorld)
769 if(!m_objectUpdated)
771 ObjectAccessor::Instance().AddUpdateObject(this);
772 m_objectUpdated = true;
778 void Object::SetUInt64Value( uint16 index, const uint64 &value )
780 ASSERT( index + 1 < m_valuesCount || PrintIndexError( index , true ) );
781 if(*((uint64*)&(m_uint32Values[ index ])) != value)
783 m_uint32Values[ index ] = *((uint32*)&value);
784 m_uint32Values[ index + 1 ] = *(((uint32*)&value) + 1);
786 if(m_inWorld)
788 if(!m_objectUpdated)
790 ObjectAccessor::Instance().AddUpdateObject(this);
791 m_objectUpdated = true;
797 void Object::SetFloatValue( uint16 index, float value )
799 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
801 if(m_floatValues[ index ] != value)
803 m_floatValues[ index ] = value;
805 if(m_inWorld)
807 if(!m_objectUpdated)
809 ObjectAccessor::Instance().AddUpdateObject(this);
810 m_objectUpdated = true;
816 void Object::SetByteValue( uint16 index, uint8 offset, uint8 value )
818 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
820 if(offset > 4)
822 sLog.outError("Object::SetByteValue: wrong offset %u", offset);
823 return;
826 if(uint8(m_uint32Values[ index ] >> (offset * 8)) != value)
828 m_uint32Values[ index ] &= ~uint32(uint32(0xFF) << (offset * 8));
829 m_uint32Values[ index ] |= uint32(uint32(value) << (offset * 8));
831 if(m_inWorld)
833 if(!m_objectUpdated)
835 ObjectAccessor::Instance().AddUpdateObject(this);
836 m_objectUpdated = true;
842 void Object::SetUInt16Value( uint16 index, uint8 offset, uint16 value )
844 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
846 if(offset > 2)
848 sLog.outError("Object::SetUInt16Value: wrong offset %u", offset);
849 return;
852 if(uint8(m_uint32Values[ index ] >> (offset * 16)) != value)
854 m_uint32Values[ index ] &= ~uint32(uint32(0xFFFF) << (offset * 16));
855 m_uint32Values[ index ] |= uint32(uint32(value) << (offset * 16));
857 if(m_inWorld)
859 if(!m_objectUpdated)
861 ObjectAccessor::Instance().AddUpdateObject(this);
862 m_objectUpdated = true;
868 void Object::SetStatFloatValue( uint16 index, float value)
870 if(value < 0)
871 value = 0.0f;
873 SetFloatValue(index, value);
876 void Object::SetStatInt32Value( uint16 index, int32 value)
878 if(value < 0)
879 value = 0;
881 SetUInt32Value(index, uint32(value));
884 void Object::ApplyModUInt32Value(uint16 index, int32 val, bool apply)
886 int32 cur = GetUInt32Value(index);
887 cur += (apply ? val : -val);
888 if(cur < 0)
889 cur = 0;
890 SetUInt32Value(index,cur);
893 void Object::ApplyModInt32Value(uint16 index, int32 val, bool apply)
895 int32 cur = GetInt32Value(index);
896 cur += (apply ? val : -val);
897 SetInt32Value(index,cur);
900 void Object::ApplyModSignedFloatValue(uint16 index, float val, bool apply)
902 float cur = GetFloatValue(index);
903 cur += (apply ? val : -val);
904 SetFloatValue(index,cur);
907 void Object::ApplyModPositiveFloatValue(uint16 index, float val, bool apply)
909 float cur = GetFloatValue(index);
910 cur += (apply ? val : -val);
911 if(cur < 0)
912 cur = 0;
913 SetFloatValue(index,cur);
916 void Object::SetFlag( uint16 index, uint32 newFlag )
918 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
919 uint32 oldval = m_uint32Values[ index ];
920 uint32 newval = oldval | newFlag;
922 if(oldval != newval)
924 m_uint32Values[ index ] = newval;
926 if(m_inWorld)
928 if(!m_objectUpdated)
930 ObjectAccessor::Instance().AddUpdateObject(this);
931 m_objectUpdated = true;
937 void Object::RemoveFlag( uint16 index, uint32 oldFlag )
939 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
940 uint32 oldval = m_uint32Values[ index ];
941 uint32 newval = oldval & ~oldFlag;
943 if(oldval != newval)
945 m_uint32Values[ index ] = newval;
947 if(m_inWorld)
949 if(!m_objectUpdated)
951 ObjectAccessor::Instance().AddUpdateObject(this);
952 m_objectUpdated = true;
958 void Object::SetByteFlag( uint16 index, uint8 offset, uint8 newFlag )
960 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
962 if(offset > 4)
964 sLog.outError("Object::SetByteFlag: wrong offset %u", offset);
965 return;
968 if(!(uint8(m_uint32Values[ index ] >> (offset * 8)) & newFlag))
970 m_uint32Values[ index ] |= uint32(uint32(newFlag) << (offset * 8));
972 if(m_inWorld)
974 if(!m_objectUpdated)
976 ObjectAccessor::Instance().AddUpdateObject(this);
977 m_objectUpdated = true;
983 void Object::RemoveByteFlag( uint16 index, uint8 offset, uint8 oldFlag )
985 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
987 if(offset > 4)
989 sLog.outError("Object::RemoveByteFlag: wrong offset %u", offset);
990 return;
993 if(uint8(m_uint32Values[ index ] >> (offset * 8)) & oldFlag)
995 m_uint32Values[ index ] &= ~uint32(uint32(oldFlag) << (offset * 8));
997 if(m_inWorld)
999 if(!m_objectUpdated)
1001 ObjectAccessor::Instance().AddUpdateObject(this);
1002 m_objectUpdated = true;
1008 bool Object::PrintIndexError(uint32 index, bool set) const
1010 sLog.outError("ERROR: 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);
1012 // assert must fail after function call
1013 return false;
1016 WorldObject::WorldObject()
1018 m_positionX = 0.0f;
1019 m_positionY = 0.0f;
1020 m_positionZ = 0.0f;
1021 m_orientation = 0.0f;
1023 m_mapId = 0;
1024 m_InstanceId = 0;
1026 m_name = "";
1028 mSemaphoreTeleport = false;
1031 void WorldObject::_Create( uint32 guidlow, HighGuid guidhigh, uint32 mapid )
1033 Object::_Create(guidlow, 0, guidhigh);
1035 m_mapId = mapid;
1038 uint32 WorldObject::GetZoneId() const
1040 return MapManager::Instance().GetBaseMap(m_mapId)->GetZoneId(m_positionX,m_positionY);
1043 uint32 WorldObject::GetAreaId() const
1045 return MapManager::Instance().GetBaseMap(m_mapId)->GetAreaId(m_positionX,m_positionY);
1048 InstanceData* WorldObject::GetInstanceData()
1050 Map *map = MapManager::Instance().GetMap(m_mapId, this);
1051 return map->IsDungeon() ? ((InstanceMap*)map)->GetInstanceData() : NULL;
1054 //slow
1055 float WorldObject::GetDistance(const WorldObject* obj) const
1057 float dx = GetPositionX() - obj->GetPositionX();
1058 float dy = GetPositionY() - obj->GetPositionY();
1059 float dz = GetPositionZ() - obj->GetPositionZ();
1060 float sizefactor = GetObjectSize() + obj->GetObjectSize();
1061 float dist = sqrt((dx*dx) + (dy*dy) + (dz*dz)) - sizefactor;
1062 return ( dist > 0 ? dist : 0);
1065 float WorldObject::GetDistance2d(float x, float y) const
1067 float dx = GetPositionX() - x;
1068 float dy = GetPositionY() - y;
1069 float sizefactor = GetObjectSize();
1070 float dist = sqrt((dx*dx) + (dy*dy)) - sizefactor;
1071 return ( dist > 0 ? dist : 0);
1074 float WorldObject::GetDistance(const float x, const float y, const float z) const
1076 float dx = GetPositionX() - x;
1077 float dy = GetPositionY() - y;
1078 float dz = GetPositionZ() - z;
1079 float sizefactor = GetObjectSize();
1080 float dist = sqrt((dx*dx) + (dy*dy) + (dz*dz)) - sizefactor;
1081 return ( dist > 0 ? dist : 0);
1084 float WorldObject::GetDistance2d(const WorldObject* obj) const
1086 float dx = GetPositionX() - obj->GetPositionX();
1087 float dy = GetPositionY() - obj->GetPositionY();
1088 float sizefactor = GetObjectSize() + obj->GetObjectSize();
1089 float dist = sqrt((dx*dx) + (dy*dy)) - sizefactor;
1090 return ( dist > 0 ? dist : 0);
1093 float WorldObject::GetDistanceZ(const WorldObject* obj) const
1095 float dz = fabs(GetPositionZ() - obj->GetPositionZ());
1096 float sizefactor = GetObjectSize() + obj->GetObjectSize();
1097 float dist = dz - sizefactor;
1098 return ( dist > 0 ? dist : 0);
1101 bool WorldObject::IsWithinDistInMap(const WorldObject* obj, const float dist2compare) const
1103 if (!obj || !IsInMap(obj)) return false;
1105 float dx = GetPositionX() - obj->GetPositionX();
1106 float dy = GetPositionY() - obj->GetPositionY();
1107 float dz = GetPositionZ() - obj->GetPositionZ();
1108 float distsq = dx*dx + dy*dy + dz*dz;
1109 float sizefactor = GetObjectSize() + obj->GetObjectSize();
1110 float maxdist = dist2compare + sizefactor;
1112 return distsq < maxdist * maxdist;
1115 bool WorldObject::IsWithinLOSInMap(const WorldObject* obj) const
1117 if (!IsInMap(obj)) return false;
1118 float ox,oy,oz;
1119 obj->GetPosition(ox,oy,oz);
1120 return(IsWithinLOS(ox, oy, oz ));
1123 bool WorldObject::IsWithinLOS(const float ox, const float oy, const float oz ) const
1125 float x,y,z;
1126 GetPosition(x,y,z);
1127 VMAP::IVMapManager *vMapManager = VMAP::VMapFactory::createOrGetVMapManager();
1128 return vMapManager->isInLineOfSight(GetMapId(), x, y, z+2.0f, ox, oy, oz+2.0f);
1131 float WorldObject::GetAngle(const WorldObject* obj) const
1133 if(!obj) return 0;
1134 return GetAngle( obj->GetPositionX(), obj->GetPositionY() );
1137 // Return angle in range 0..2*pi
1138 float WorldObject::GetAngle( const float x, const float y ) const
1140 float dx = x - GetPositionX();
1141 float dy = y - GetPositionY();
1143 float ang = atan2(dy, dx);
1144 ang = (ang >= 0) ? ang : 2 * M_PI + ang;
1145 return ang;
1148 bool WorldObject::HasInArc(const float arcangle, const WorldObject* obj) const
1150 float arc = arcangle;
1152 // move arc to range 0.. 2*pi
1153 while( arc >= 2.0f * M_PI )
1154 arc -= 2.0f * M_PI;
1155 while( arc < 0 )
1156 arc += 2.0f * M_PI;
1158 float angle = GetAngle( obj );
1159 angle -= m_orientation;
1161 // move angle to range -pi ... +pi
1162 while( angle > M_PI)
1163 angle -= 2.0f * M_PI;
1164 while(angle < -M_PI)
1165 angle += 2.0f * M_PI;
1167 float lborder = -1 * (arc/2.0f); // in range -pi..0
1168 float rborder = (arc/2.0f); // in range 0..pi
1169 return (( angle >= lborder ) && ( angle <= rborder ));
1172 void WorldObject::GetRandomPoint( float x, float y, float z, float distance, float &rand_x, float &rand_y, float &rand_z) const
1174 if(distance==0)
1176 rand_x = x;
1177 rand_y = y;
1178 rand_z = z;
1179 return;
1182 // angle to face `obj` to `this`
1183 float angle = rand_norm()*2*M_PI;
1184 float new_dist = rand_norm()*distance;
1186 rand_x = x + new_dist * cos(angle);
1187 rand_y = y + new_dist * sin(angle);
1188 rand_z = z;
1190 MaNGOS::NormalizeMapCoord(rand_x);
1191 MaNGOS::NormalizeMapCoord(rand_y);
1192 UpdateGroundPositionZ(rand_x,rand_y,rand_z); // update to LOS height if available
1195 void WorldObject::UpdateGroundPositionZ(float x, float y, float &z) const
1197 float new_z = MapManager::Instance().GetBaseMap(GetMapId())->GetHeight(x,y,z,true);
1198 if(new_z > INVALID_HEIGHT)
1199 z = new_z+ 0.05f; // just to be sure that we are not a few pixel under the surface
1202 bool WorldObject::IsPositionValid() const
1204 return MaNGOS::IsValidMapCoord(m_positionX,m_positionY,m_positionZ,m_orientation);
1207 void WorldObject::MonsterSay(const char* text, uint32 language, uint64 TargetGuid)
1209 WorldPacket data(SMSG_MESSAGECHAT, 200);
1210 BuildMonsterChat(&data,CHAT_MSG_MONSTER_SAY,text,language,GetName(),TargetGuid);
1211 SendMessageToSetInRange(&data,sWorld.getConfig(CONFIG_LISTEN_RANGE_SAY),true);
1214 void WorldObject::MonsterYell(const char* text, uint32 language, uint64 TargetGuid)
1216 WorldPacket data(SMSG_MESSAGECHAT, 200);
1217 BuildMonsterChat(&data,CHAT_MSG_MONSTER_YELL,text,language,GetName(),TargetGuid);
1218 SendMessageToSetInRange(&data,sWorld.getConfig(CONFIG_LISTEN_RANGE_YELL),true);
1221 void WorldObject::MonsterTextEmote(const char* text, uint64 TargetGuid, bool IsBossEmote)
1223 WorldPacket data(SMSG_MESSAGECHAT, 200);
1224 BuildMonsterChat(&data,IsBossEmote ? CHAT_MSG_RAID_BOSS_EMOTE : CHAT_MSG_MONSTER_EMOTE,text,LANG_UNIVERSAL,GetName(),TargetGuid);
1225 SendMessageToSetInRange(&data,sWorld.getConfig(CONFIG_LISTEN_RANGE_TEXTEMOTE),true);
1228 void WorldObject::MonsterWhisper(const char* text, uint64 receiver, bool IsBossWhisper)
1230 Player *player = objmgr.GetPlayer(receiver);
1231 if(!player || !player->GetSession())
1232 return;
1234 WorldPacket data(SMSG_MESSAGECHAT, 200);
1235 BuildMonsterChat(&data,IsBossWhisper ? CHAT_MSG_RAID_BOSS_WHISPER : CHAT_MSG_MONSTER_WHISPER,text,LANG_UNIVERSAL,GetName(),receiver);
1237 player->GetSession()->SendPacket(&data);
1240 namespace MaNGOS
1242 class MessageChatLocaleCacheDo
1244 public:
1245 MessageChatLocaleCacheDo(WorldObject const& obj, ChatMsg msgtype, int32 textId, uint32 language, uint64 targetGUID, float dist)
1246 : i_object(obj), i_msgtype(msgtype), i_textId(textId), i_language(language),
1247 i_targetGUID(targetGUID), i_dist(dist)
1251 ~MessageChatLocaleCacheDo()
1253 for(int i = 0; i < i_data_cache.size(); ++i)
1254 delete i_data_cache[i];
1257 void operator()(Player* p)
1259 // skip far away players
1260 if(p->GetDistance(&i_object) > i_dist)
1261 return;
1263 uint32 loc_idx = p->GetSession()->GetSessionDbLocaleIndex();
1264 uint32 cache_idx = loc_idx+1;
1265 WorldPacket* data;
1267 // create if not cached yet
1268 if(i_data_cache.size() < cache_idx+1 || !i_data_cache[cache_idx])
1270 if(i_data_cache.size() < cache_idx+1)
1271 i_data_cache.resize(cache_idx+1);
1273 char const* text = objmgr.GetMangosString(i_textId,loc_idx);
1275 data = new WorldPacket(SMSG_MESSAGECHAT, 200);
1277 // TODO: i_object.GetName() also must be localized?
1278 i_object.BuildMonsterChat(data,i_msgtype,text,i_language,i_object.GetName(),i_targetGUID);
1280 i_data_cache[cache_idx] = data;
1282 else
1283 data = i_data_cache[cache_idx];
1285 p->SendDirectMessage(data);
1288 private:
1289 WorldObject const& i_object;
1290 ChatMsg i_msgtype;
1291 int32 i_textId;
1292 uint32 i_language;
1293 uint64 i_targetGUID;
1294 float i_dist;
1295 std::vector<WorldPacket*> i_data_cache; // 0 = default, i => i-1 locale index
1297 } // namespace MaNGOS
1299 void WorldObject::MonsterSay(int32 textId, uint32 language, uint64 TargetGuid)
1301 CellPair p = MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY());
1303 Cell cell(p);
1304 cell.data.Part.reserved = ALL_DISTRICT;
1305 cell.SetNoCreate();
1307 MaNGOS::MessageChatLocaleCacheDo say_do(*this, CHAT_MSG_MONSTER_SAY, textId,language,TargetGuid,sWorld.getConfig(CONFIG_LISTEN_RANGE_SAY));
1308 MaNGOS::PlayerWorker<MaNGOS::MessageChatLocaleCacheDo> say_worker(say_do);
1309 TypeContainerVisitor<MaNGOS::PlayerWorker<MaNGOS::MessageChatLocaleCacheDo>, WorldTypeMapContainer > message(say_worker);
1310 CellLock<GridReadGuard> cell_lock(cell, p);
1311 cell_lock->Visit(cell_lock, message, *GetMap());
1314 void WorldObject::MonsterYell(int32 textId, uint32 language, uint64 TargetGuid)
1316 CellPair p = MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY());
1318 Cell cell(p);
1319 cell.data.Part.reserved = ALL_DISTRICT;
1320 cell.SetNoCreate();
1322 MaNGOS::MessageChatLocaleCacheDo say_do(*this, CHAT_MSG_MONSTER_YELL, textId,language,TargetGuid,sWorld.getConfig(CONFIG_LISTEN_RANGE_YELL));
1323 MaNGOS::PlayerWorker<MaNGOS::MessageChatLocaleCacheDo> say_worker(say_do);
1324 TypeContainerVisitor<MaNGOS::PlayerWorker<MaNGOS::MessageChatLocaleCacheDo>, WorldTypeMapContainer > message(say_worker);
1325 CellLock<GridReadGuard> cell_lock(cell, p);
1326 cell_lock->Visit(cell_lock, message, *GetMap());
1329 void WorldObject::MonsterTextEmote(int32 textId, uint64 TargetGuid, bool IsBossEmote)
1331 CellPair p = MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY());
1333 Cell cell(p);
1334 cell.data.Part.reserved = ALL_DISTRICT;
1335 cell.SetNoCreate();
1337 MaNGOS::MessageChatLocaleCacheDo say_do(*this, IsBossEmote ? CHAT_MSG_RAID_BOSS_EMOTE : CHAT_MSG_MONSTER_EMOTE, textId,LANG_UNIVERSAL,TargetGuid,sWorld.getConfig(CONFIG_LISTEN_RANGE_TEXTEMOTE));
1338 MaNGOS::PlayerWorker<MaNGOS::MessageChatLocaleCacheDo> say_worker(say_do);
1339 TypeContainerVisitor<MaNGOS::PlayerWorker<MaNGOS::MessageChatLocaleCacheDo>, WorldTypeMapContainer > message(say_worker);
1340 CellLock<GridReadGuard> cell_lock(cell, p);
1341 cell_lock->Visit(cell_lock, message, *GetMap());
1344 void WorldObject::MonsterWhisper(int32 textId, uint64 receiver, bool IsBossWhisper)
1346 Player *player = objmgr.GetPlayer(receiver);
1347 if(!player || !player->GetSession())
1348 return;
1350 uint32 loc_idx = player->GetSession()->GetSessionDbLocaleIndex();
1351 char const* text = objmgr.GetMangosString(textId,loc_idx);
1353 WorldPacket data(SMSG_MESSAGECHAT, 200);
1354 BuildMonsterChat(&data,IsBossWhisper ? CHAT_MSG_RAID_BOSS_WHISPER : CHAT_MSG_MONSTER_WHISPER,text,LANG_UNIVERSAL,GetName(),receiver);
1356 player->GetSession()->SendPacket(&data);
1359 void WorldObject::BuildMonsterChat(WorldPacket *data, uint8 msgtype, char const* text, uint32 language, char const* name, uint64 targetGuid) const
1361 bool pre = (msgtype==CHAT_MSG_MONSTER_EMOTE || msgtype==CHAT_MSG_RAID_BOSS_EMOTE);
1363 *data << (uint8)msgtype;
1364 *data << (uint32)language;
1365 *data << (uint64)GetGUID();
1366 *data << (uint32)0; //2.1.0
1367 *data << (uint32)(strlen(name)+1);
1368 *data << name;
1369 *data << (uint64)targetGuid; //Unit Target
1370 if( targetGuid && !IS_PLAYER_GUID(targetGuid) )
1372 *data << (uint32)1; // target name length
1373 *data << (uint8)0; // target name
1375 *data << (uint32)(strlen(text)+1+(pre?3:0));
1376 if(pre)
1377 data->append("%s ",3);
1378 *data << text;
1379 *data << (uint8)0; // ChatTag
1382 void WorldObject::BuildHeartBeatMsg(WorldPacket *data) const
1384 //Heartbeat message cannot be used for non-units
1385 if (!isType(TYPEMASK_UNIT))
1386 return;
1388 data->Initialize(MSG_MOVE_HEARTBEAT, 32);
1389 data->append(GetPackGUID());
1390 *data << uint32(((Unit*)this)->GetUnitMovementFlags()); // movement flags
1391 *data << uint16(0); // 2.3.0
1392 *data << getMSTime(); // time
1393 *data << m_positionX;
1394 *data << m_positionY;
1395 *data << m_positionZ;
1396 *data << m_orientation;
1397 *data << uint32(0);
1400 void WorldObject::BuildTeleportAckMsg(WorldPacket *data, float x, float y, float z, float ang) const
1402 //TeleportAck message cannot be used for non-units
1403 if (!isType(TYPEMASK_UNIT))
1404 return;
1406 data->Initialize(MSG_MOVE_TELEPORT_ACK, 41);
1407 data->append(GetPackGUID());
1408 *data << uint32(0); // this value increments every time
1409 *data << uint32(((Unit*)this)->GetUnitMovementFlags()); // movement flags
1410 *data << uint16(0); // 2.3.0
1411 *data << getMSTime(); // time
1412 *data << x;
1413 *data << y;
1414 *data << z;
1415 *data << ang;
1416 *data << uint32(0);
1419 void WorldObject::SendMessageToSet(WorldPacket *data, bool /*bToSelf*/)
1421 MapManager::Instance().GetMap(m_mapId, this)->MessageBroadcast(this, data);
1424 void WorldObject::SendMessageToSetInRange(WorldPacket *data, float dist, bool /*bToSelf*/)
1426 MapManager::Instance().GetMap(m_mapId, this)->MessageDistBroadcast(this, data, dist);
1429 void WorldObject::SendObjectDeSpawnAnim(uint64 guid)
1431 WorldPacket data(SMSG_GAMEOBJECT_DESPAWN_ANIM, 8);
1432 data << guid;
1433 SendMessageToSet(&data, true);
1436 Map* WorldObject::GetMap() const
1438 return MapManager::Instance().GetMap(GetMapId(), this);
1441 Map const* WorldObject::GetBaseMap() const
1443 return MapManager::Instance().GetBaseMap(GetMapId());
1446 void WorldObject::AddObjectToRemoveList()
1448 Map* map = GetMap();
1449 if(!map)
1451 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());
1452 return;
1455 map->AddObjectToRemoveList(this);
1458 Creature* WorldObject::SummonCreature(uint32 id, float x, float y, float z, float ang,TempSummonType spwtype,uint32 despwtime)
1460 TemporarySummon* pCreature = new TemporarySummon(GetGUID());
1462 pCreature->SetInstanceId(GetInstanceId());
1463 uint32 team = 0;
1464 if (GetTypeId()==TYPEID_PLAYER)
1465 team = ((Player*)this)->GetTeam();
1467 if (!pCreature->Create(objmgr.GenerateLowGuid(HIGHGUID_UNIT), GetMap(), id, team))
1469 delete pCreature;
1470 return NULL;
1473 if (x == 0.0f && y == 0.0f && z == 0.0f)
1474 GetClosePoint(x, y, z, pCreature->GetObjectSize());
1476 pCreature->Relocate(x, y, z, ang);
1478 if(!pCreature->IsPositionValid())
1480 sLog.outError("ERROR: Creature (guidlow %d, entry %d) not summoned. Suggested coordinates isn't valid (X: %f Y: %f)",pCreature->GetGUIDLow(),pCreature->GetEntry(),pCreature->GetPositionX(),pCreature->GetPositionY());
1481 delete pCreature;
1482 return NULL;
1485 pCreature->Summon(spwtype, despwtime);
1487 if(GetTypeId()==TYPEID_UNIT && ((Creature*)this)->AI())
1488 ((Creature*)this)->AI()->JustSummoned(pCreature);
1490 //return the creature therewith the summoner has access to it
1491 return pCreature;
1494 namespace MaNGOS
1496 class NearUsedPosDo
1498 public:
1499 NearUsedPosDo(WorldObject const& obj, WorldObject const* searcher, float angle, ObjectPosSelector& selector)
1500 : i_object(obj), i_searcher(searcher), i_angle(angle), i_selector(selector) {}
1502 void operator()(Corpse*) const {}
1503 void operator()(DynamicObject*) const {}
1505 void operator()(Creature* c) const
1507 // skip self or target
1508 if(c==i_searcher || c==&i_object)
1509 return;
1511 float x,y,z;
1513 if( !c->isAlive() || c->hasUnitState(UNIT_STAT_ROOT | UNIT_STAT_STUNNED | UNIT_STAT_DISTRACTED) ||
1514 !c->GetMotionMaster()->GetDestination(x,y,z) )
1516 x = c->GetPositionX();
1517 y = c->GetPositionY();
1520 add(c,x,y);
1523 template<class T>
1524 void operator()(T* u) const
1526 // skip self or target
1527 if(u==i_searcher || u==&i_object)
1528 return;
1530 float x,y;
1532 x = u->GetPositionX();
1533 y = u->GetPositionY();
1535 add(u,x,y);
1538 // we must add used pos that can fill places around center
1539 void add(WorldObject* u, float x, float y) const
1541 // dist include size of u
1542 float dist2d = i_object.GetDistance2d(x,y);
1544 // u is too nearest to i_object
1545 if(dist2d + i_object.GetObjectSize() + u->GetObjectSize() < i_selector.m_dist - i_selector.m_size)
1546 return;
1548 // u is too far away from i_object
1549 if(dist2d + i_object.GetObjectSize() - u->GetObjectSize() > i_selector.m_dist + i_selector.m_size)
1550 return;
1552 float angle = i_object.GetAngle(u)-i_angle;
1554 // move angle to range -pi ... +pi
1555 while( angle > M_PI)
1556 angle -= 2.0f * M_PI;
1557 while(angle < -M_PI)
1558 angle += 2.0f * M_PI;
1560 i_selector.AddUsedPos(u->GetObjectSize(),angle,dist2d + i_object.GetObjectSize());
1562 private:
1563 WorldObject const& i_object;
1564 WorldObject const* i_searcher;
1565 float i_angle;
1566 ObjectPosSelector& i_selector;
1568 } // namespace MaNGOS
1570 //===================================================================================================
1572 void WorldObject::GetNearPoint2D(float &x, float &y, float distance2d, float absAngle ) const
1574 x = GetPositionX() + (GetObjectSize() + distance2d) * cos(absAngle);
1575 y = GetPositionY() + (GetObjectSize() + distance2d) * sin(absAngle);
1577 MaNGOS::NormalizeMapCoord(x);
1578 MaNGOS::NormalizeMapCoord(y);
1581 void WorldObject::GetNearPoint(WorldObject const* searcher, float &x, float &y, float &z, float searcher_size, float distance2d, float absAngle ) const
1583 GetNearPoint2D(x,y,distance2d+searcher_size,absAngle);
1584 z = GetPositionZ();
1586 // if detection disabled, return first point
1587 if(!sWorld.getConfig(CONFIG_DETECT_POS_COLLISION))
1589 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1590 return;
1593 // or remember first point
1594 float first_x = x;
1595 float first_y = y;
1596 bool first_los_conflict = false; // first point LOS problems
1598 // prepare selector for work
1599 ObjectPosSelector selector(GetPositionX(),GetPositionY(),GetObjectSize(),distance2d+searcher_size);
1601 // adding used positions around object
1603 CellPair p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
1604 Cell cell(p);
1605 cell.data.Part.reserved = ALL_DISTRICT;
1606 cell.SetNoCreate();
1608 MaNGOS::NearUsedPosDo u_do(*this,searcher,absAngle,selector);
1609 MaNGOS::WorldObjectWorker<MaNGOS::NearUsedPosDo> worker(u_do);
1611 TypeContainerVisitor<MaNGOS::WorldObjectWorker<MaNGOS::NearUsedPosDo>, GridTypeMapContainer > grid_obj_worker(worker);
1612 TypeContainerVisitor<MaNGOS::WorldObjectWorker<MaNGOS::NearUsedPosDo>, WorldTypeMapContainer > world_obj_worker(worker);
1614 CellLock<GridReadGuard> cell_lock(cell, p);
1615 cell_lock->Visit(cell_lock, grid_obj_worker, *MapManager::Instance().GetMap(GetMapId(), this));
1616 cell_lock->Visit(cell_lock, world_obj_worker, *MapManager::Instance().GetMap(GetMapId(), this));
1619 // maybe can just place in primary position
1620 if( selector.CheckOriginal() )
1622 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1624 if(IsWithinLOS(x,y,z))
1625 return;
1627 first_los_conflict = true; // first point have LOS problems
1630 float angle; // candidate of angle for free pos
1632 // special case when one from list empty and then empty side preferred
1633 if(selector.FirstAngle(angle))
1635 GetNearPoint2D(x,y,distance2d,absAngle+angle);
1636 z = GetPositionZ();
1637 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1639 if(IsWithinLOS(x,y,z))
1640 return;
1643 // set first used pos in lists
1644 selector.InitializeAngle();
1646 // select in positions after current nodes (selection one by one)
1647 while(selector.NextAngle(angle)) // angle for free pos
1649 GetNearPoint2D(x,y,distance2d,absAngle+angle);
1650 z = GetPositionZ();
1651 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1653 if(IsWithinLOS(x,y,z))
1654 return;
1657 // BAD NEWS: not free pos (or used or have LOS problems)
1658 // Attempt find _used_ pos without LOS problem
1660 if(!first_los_conflict)
1662 x = first_x;
1663 y = first_y;
1665 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1666 return;
1669 // special case when one from list empty and then empty side preferred
1670 if( selector.IsNonBalanced() )
1672 if(!selector.FirstAngle(angle)) // _used_ pos
1674 GetNearPoint2D(x,y,distance2d,absAngle+angle);
1675 z = GetPositionZ();
1676 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1678 if(IsWithinLOS(x,y,z))
1679 return;
1683 // set first used pos in lists
1684 selector.InitializeAngle();
1686 // select in positions after current nodes (selection one by one)
1687 while(selector.NextUsedAngle(angle)) // angle for used pos but maybe without LOS problem
1689 GetNearPoint2D(x,y,distance2d,absAngle+angle);
1690 z = GetPositionZ();
1691 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1693 if(IsWithinLOS(x,y,z))
1694 return;
1697 // BAD BAD NEWS: all found pos (free and used) have LOS problem :(
1698 x = first_x;
1699 y = first_y;
1701 UpdateGroundPositionZ(x,y,z); // update to LOS height if available