* Fix some warlock talent work
[getmangos.git] / src / game / Object.cpp
blobb8f758cbd6c14340fa7b0fbc9bc5ff02d1b593ae
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 target->GetSession()->SendPacket( &data );
257 void Object::_BuildMovementUpdate(ByteBuffer * data, uint8 flags, uint32 flags2 ) const
259 *data << (uint8)flags; // update flags
261 // 0x20
262 if (flags & UPDATEFLAG_LIVING)
264 switch(GetTypeId())
266 case TYPEID_UNIT:
268 flags2 = ((Unit*)this)->GetUnitMovementFlags();
270 break;
271 case TYPEID_PLAYER:
273 flags2 = ((Player*)this)->GetUnitMovementFlags();
275 if(((Player*)this)->GetTransport())
276 flags2 |= MOVEMENTFLAG_ONTRANSPORT;
277 else
278 flags2 &= ~MOVEMENTFLAG_ONTRANSPORT;
280 // remove unknown, unused etc flags for now
281 flags2 &= ~MOVEMENTFLAG_SPLINE2; // will be set manually
283 if(((Player*)this)->isInFlight())
285 WPAssert(((Player*)this)->GetMotionMaster()->GetCurrentMovementGeneratorType() == FLIGHT_MOTION_TYPE);
286 flags2 = (MOVEMENTFLAG_FORWARD | MOVEMENTFLAG_SPLINE2);
289 break;
292 *data << uint32(flags2); // movement flags
293 *data << uint8(0); // unk 2.3.0
294 *data << uint32(getMSTime()); // time (in milliseconds)
297 // 0x40
298 if (flags & UPDATEFLAG_HASPOSITION)
300 // 0x02
301 if(flags & UPDATEFLAG_TRANSPORT && ((GameObject*)this)->GetGoType() == GAMEOBJECT_TYPE_MO_TRANSPORT)
303 *data << (float)0;
304 *data << (float)0;
305 *data << (float)0;
306 *data << ((WorldObject *)this)->GetOrientation();
308 else
310 *data << ((WorldObject *)this)->GetPositionX();
311 *data << ((WorldObject *)this)->GetPositionY();
312 *data << ((WorldObject *)this)->GetPositionZ();
313 *data << ((WorldObject *)this)->GetOrientation();
317 // 0x20
318 if(flags & UPDATEFLAG_LIVING)
320 // 0x00000200
321 if(flags2 & MOVEMENTFLAG_ONTRANSPORT)
323 if(GetTypeId() == TYPEID_PLAYER)
325 *data << (uint64)((Player*)this)->GetTransport()->GetGUID();
326 *data << (float)((Player*)this)->GetTransOffsetX();
327 *data << (float)((Player*)this)->GetTransOffsetY();
328 *data << (float)((Player*)this)->GetTransOffsetZ();
329 *data << (float)((Player*)this)->GetTransOffsetO();
330 *data << (uint32)((Player*)this)->GetTransTime();
332 //MaNGOS currently not have support for other than player on transport
335 // 0x02200000
336 if(flags2 & (MOVEMENTFLAG_SWIMMING | MOVEMENTFLAG_FLYING2))
338 if(GetTypeId() == TYPEID_PLAYER)
339 *data << (float)((Player*)this)->m_movementInfo.s_pitch;
340 else
341 *data << (float)0; // is't part of movement packet, we must store and send it...
344 if(GetTypeId() == TYPEID_PLAYER)
345 *data << (uint32)((Player*)this)->m_movementInfo.fallTime;
346 else
347 *data << (uint32)0; // last fall time
349 // 0x00001000
350 if(flags2 & MOVEMENTFLAG_JUMPING)
352 if(GetTypeId() == TYPEID_PLAYER)
354 *data << (float)((Player*)this)->m_movementInfo.j_unk;
355 *data << (float)((Player*)this)->m_movementInfo.j_sinAngle;
356 *data << (float)((Player*)this)->m_movementInfo.j_cosAngle;
357 *data << (float)((Player*)this)->m_movementInfo.j_xyspeed;
359 else
361 *data << (float)0;
362 *data << (float)0;
363 *data << (float)0;
364 *data << (float)0;
368 // 0x04000000
369 if(flags2 & MOVEMENTFLAG_SPLINE)
371 if(GetTypeId() == TYPEID_PLAYER)
372 *data << (float)((Player*)this)->m_movementInfo.u_unk1;
373 else
374 *data << (float)0;
377 *data << ((Unit*)this)->GetSpeed( MOVE_WALK );
378 *data << ((Unit*)this)->GetSpeed( MOVE_RUN );
379 *data << ((Unit*)this)->GetSpeed( MOVE_SWIM_BACK );
380 *data << ((Unit*)this)->GetSpeed( MOVE_SWIM );
381 *data << ((Unit*)this)->GetSpeed( MOVE_RUN_BACK );
382 *data << ((Unit*)this)->GetSpeed( MOVE_FLIGHT );
383 *data << ((Unit*)this)->GetSpeed( MOVE_FLIGHT_BACK );
384 *data << ((Unit*)this)->GetSpeed( MOVE_TURN_RATE );
386 // 0x08000000
387 if(flags2 & MOVEMENTFLAG_SPLINE2)
389 if(GetTypeId() != TYPEID_PLAYER)
391 sLog.outDebug("_BuildMovementUpdate: MOVEMENTFLAG_SPLINE2 for non-player");
392 return;
395 if(!((Player*)this)->isInFlight())
397 sLog.outDebug("_BuildMovementUpdate: MOVEMENTFLAG_SPLINE2 but not in flight");
398 return;
401 WPAssert(((Player*)this)->GetMotionMaster()->GetCurrentMovementGeneratorType() == FLIGHT_MOTION_TYPE);
403 FlightPathMovementGenerator *fmg = (FlightPathMovementGenerator*)(((Player*)this)->GetMotionMaster()->top());
405 uint32 flags3 = 0x00000300;
407 *data << uint32(flags3); // splines flag?
409 if(flags3 & 0x10000) // probably x,y,z coords there
411 *data << (float)0;
412 *data << (float)0;
413 *data << (float)0;
416 if(flags3 & 0x20000) // probably guid there
418 *data << uint64(0);
421 if(flags3 & 0x40000) // may be orientation
423 *data << (float)0;
426 Path &path = fmg->GetPath();
428 float x, y, z;
429 ((Player*)this)->GetPosition(x, y, z);
431 uint32 inflighttime = uint32(path.GetPassedLength(fmg->GetCurrentNode(), x, y, z) * 32);
432 uint32 traveltime = uint32(path.GetTotalLength() * 32);
434 *data << uint32(inflighttime); // passed move time?
435 *data << uint32(traveltime); // full move time?
436 *data << uint32(0); // ticks count?
438 uint32 poscount = uint32(path.Size());
440 *data << uint32(poscount); // points count
442 for(uint32 i = 0; i < poscount; ++i)
444 *data << path.GetNodes()[i].x;
445 *data << path.GetNodes()[i].y;
446 *data << path.GetNodes()[i].z;
449 /*for(uint32 i = 0; i < poscount; i++)
451 // path points
452 *data << (float)0;
453 *data << (float)0;
454 *data << (float)0;
457 *data << path.GetNodes()[poscount-1].x;
458 *data << path.GetNodes()[poscount-1].y;
459 *data << path.GetNodes()[poscount-1].z;
461 // target position (path end)
462 /**data << ((Unit*)this)->GetPositionX();
463 *data << ((Unit*)this)->GetPositionY();
464 *data << ((Unit*)this)->GetPositionZ();*/
468 // 0x8
469 if(flags & UPDATEFLAG_LOWGUID)
471 switch(GetTypeId())
473 case TYPEID_OBJECT:
474 case TYPEID_ITEM:
475 case TYPEID_CONTAINER:
476 case TYPEID_GAMEOBJECT:
477 case TYPEID_DYNAMICOBJECT:
478 case TYPEID_CORPSE:
479 *data << uint32(GetGUIDLow()); // GetGUIDLow()
480 break;
481 case TYPEID_UNIT:
482 *data << uint32(0x0000000B); // unk, can be 0xB or 0xC
483 break;
484 case TYPEID_PLAYER:
485 if(flags & UPDATEFLAG_SELF)
486 *data << uint32(0x00000015); // unk, can be 0x15 or 0x22
487 else
488 *data << uint32(0x00000008); // unk, can be 0x7 or 0x8
489 break;
490 default:
491 *data << uint32(0x00000000); // unk
492 break;
496 // 0x10
497 if(flags & UPDATEFLAG_HIGHGUID)
499 switch(GetTypeId())
501 case TYPEID_OBJECT:
502 case TYPEID_ITEM:
503 case TYPEID_CONTAINER:
504 case TYPEID_GAMEOBJECT:
505 case TYPEID_DYNAMICOBJECT:
506 case TYPEID_CORPSE:
507 *data << uint32(GetGUIDHigh()); // GetGUIDHigh()
508 break;
509 default:
510 *data << uint32(0x00000000); // unk
511 break;
515 // 0x4
516 if(flags & UPDATEFLAG_FULLGUID)
518 *data << uint8(0); // packed guid (probably target guid)
521 // 0x2
522 if(flags & UPDATEFLAG_TRANSPORT)
524 *data << uint32(getMSTime()); // ms time
528 void Object::_BuildValuesUpdate(uint8 updatetype, ByteBuffer * data, UpdateMask *updateMask, Player *target) const
530 if(!target)
531 return;
533 bool IsActivateToQuest = false;
534 if (updatetype == UPDATETYPE_CREATE_OBJECT || updatetype == UPDATETYPE_CREATE_OBJECT2)
536 if (isType(TYPEMASK_GAMEOBJECT) && !((GameObject*)this)->IsTransport())
538 if ( ((GameObject*)this)->ActivateToQuest(target) || target->isGameMaster())
540 IsActivateToQuest = true;
541 updateMask->SetBit(GAMEOBJECT_DYN_FLAGS);
545 else //case UPDATETYPE_VALUES
547 if (isType(TYPEMASK_GAMEOBJECT) && !((GameObject*)this)->IsTransport())
549 if ( ((GameObject*)this)->ActivateToQuest(target) || target->isGameMaster())
551 IsActivateToQuest = true;
553 updateMask->SetBit(GAMEOBJECT_DYN_FLAGS);
554 updateMask->SetBit(GAMEOBJECT_ANIMPROGRESS);
558 WPAssert(updateMask && updateMask->GetCount() == m_valuesCount);
560 *data << (uint8)updateMask->GetBlockCount();
561 data->append( updateMask->GetMask(), updateMask->GetLength() );
563 // 2 specialized loops for speed optimization in non-unit case
564 if(isType(TYPEMASK_UNIT)) // unit (creature/player) case
566 for( uint16 index = 0; index < m_valuesCount; index ++ )
568 if( updateMask->GetBit( index ) )
570 // remove custom flag before send
571 if( index == UNIT_NPC_FLAGS )
572 *data << uint32(m_uint32Values[ index ] & ~UNIT_NPC_FLAG_GUARD);
573 // FIXME: Some values at server stored in float format but must be sent to client in uint32 format
574 else if(index >= UNIT_FIELD_BASEATTACKTIME && index <= UNIT_FIELD_RANGEDATTACKTIME)
576 // convert from float to uint32 and send
577 *data << uint32(m_floatValues[ index ] < 0 ? 0 : m_floatValues[ index ]);
579 // there are some float values which may be negative or can't get negative due to other checks
580 else if(index >= UNIT_FIELD_NEGSTAT0 && index <= UNIT_FIELD_NEGSTAT4 ||
581 index >= UNIT_FIELD_RESISTANCEBUFFMODSPOSITIVE && index <= (UNIT_FIELD_RESISTANCEBUFFMODSPOSITIVE + 6) ||
582 index >= UNIT_FIELD_RESISTANCEBUFFMODSNEGATIVE && index <= (UNIT_FIELD_RESISTANCEBUFFMODSNEGATIVE + 6) ||
583 index >= UNIT_FIELD_POSSTAT0 && index <= UNIT_FIELD_POSSTAT4)
585 *data << uint32(m_floatValues[ index ]);
587 // Gamemasters should be always able to select units - remove not selectable flag
588 else if(index == UNIT_FIELD_FLAGS && target->isGameMaster())
590 *data << (m_uint32Values[ index ] & ~UNIT_FLAG_NOT_SELECTABLE);
592 // hide lootable animation for unallowed players
593 else if(index == UNIT_DYNAMIC_FLAGS && GetTypeId() == TYPEID_UNIT)
595 if(!target->isAllowedToLoot((Creature*)this))
596 *data << (m_uint32Values[ index ] & ~UNIT_DYNFLAG_LOOTABLE);
597 else
598 *data << (m_uint32Values[ index ] & ~UNIT_DYNFLAG_OTHER_TAGGER);
600 else
602 // send in current format (float as float, uint32 as uint32)
603 *data << m_uint32Values[ index ];
608 else if(isType(TYPEMASK_GAMEOBJECT)) // gameobject case
610 for( uint16 index = 0; index < m_valuesCount; index ++ )
612 if( updateMask->GetBit( index ) )
614 // send in current format (float as float, uint32 as uint32)
615 if ( index == GAMEOBJECT_DYN_FLAGS )
617 if(IsActivateToQuest )
619 switch(((GameObject*)this)->GetGoType())
621 case GAMEOBJECT_TYPE_CHEST:
622 *data << uint32(9); // enable quest object. Represent 9, but 1 for client before 2.3.0
623 break;
624 case GAMEOBJECT_TYPE_GOOBER:
625 *data << uint32(1);
626 break;
627 default:
628 *data << uint32(0); // unknown. not happen.
629 break;
632 else
633 *data << uint32(0); // disable quest object
635 else
636 *data << m_uint32Values[ index ]; // other cases
640 else // other objects case (no special index checks)
642 for( uint16 index = 0; index < m_valuesCount; index ++ )
644 if( updateMask->GetBit( index ) )
646 // send in current format (float as float, uint32 as uint32)
647 *data << m_uint32Values[ index ];
653 void Object::ClearUpdateMask(bool remove)
655 for( uint16 index = 0; index < m_valuesCount; index ++ )
657 if(m_uint32Values_mirror[index]!= m_uint32Values[index])
658 m_uint32Values_mirror[index] = m_uint32Values[index];
660 if(m_objectUpdated)
662 if(remove)
663 ObjectAccessor::Instance().RemoveUpdateObject(this);
664 m_objectUpdated = false;
668 // Send current value fields changes to all viewers
669 void Object::SendUpdateObjectToAllExcept(Player* exceptPlayer)
671 // changes will be send in create packet
672 if(!IsInWorld())
673 return;
675 // nothing do
676 if(!m_objectUpdated)
677 return;
679 ObjectAccessor::UpdateObject(this,exceptPlayer);
682 bool Object::LoadValues(const char* data)
684 if(!m_uint32Values) _InitValues();
686 Tokens tokens = StrSplit(data, " ");
688 if(tokens.size() != m_valuesCount)
689 return false;
691 Tokens::iterator iter;
692 int index;
693 for (iter = tokens.begin(), index = 0; index < m_valuesCount; ++iter, ++index)
695 m_uint32Values[index] = atol((*iter).c_str());
698 return true;
701 void Object::_SetUpdateBits(UpdateMask *updateMask, Player* /*target*/) const
703 for( uint16 index = 0; index < m_valuesCount; index ++ )
705 if(m_uint32Values_mirror[index]!= m_uint32Values[index])
706 updateMask->SetBit(index);
710 void Object::_SetCreateBits(UpdateMask *updateMask, Player* /*target*/) const
712 for( uint16 index = 0; index < m_valuesCount; index++ )
714 if(GetUInt32Value(index) != 0)
715 updateMask->SetBit(index);
719 void Object::SetInt32Value( uint16 index, int32 value )
721 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
723 if(m_int32Values[ index ] != value)
725 m_int32Values[ index ] = value;
727 if(m_inWorld)
729 if(!m_objectUpdated)
731 ObjectAccessor::Instance().AddUpdateObject(this);
732 m_objectUpdated = true;
738 void Object::SetUInt32Value( uint16 index, uint32 value )
740 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
742 if(m_uint32Values[ index ] != value)
744 m_uint32Values[ index ] = value;
746 if(m_inWorld)
748 if(!m_objectUpdated)
750 ObjectAccessor::Instance().AddUpdateObject(this);
751 m_objectUpdated = true;
757 void Object::SetUInt64Value( uint16 index, const uint64 &value )
759 ASSERT( index + 1 < m_valuesCount || PrintIndexError( index , true ) );
760 if(*((uint64*)&(m_uint32Values[ index ])) != value)
762 m_uint32Values[ index ] = *((uint32*)&value);
763 m_uint32Values[ index + 1 ] = *(((uint32*)&value) + 1);
765 if(m_inWorld)
767 if(!m_objectUpdated)
769 ObjectAccessor::Instance().AddUpdateObject(this);
770 m_objectUpdated = true;
776 void Object::SetFloatValue( uint16 index, float value )
778 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
780 if(m_floatValues[ index ] != value)
782 m_floatValues[ index ] = value;
784 if(m_inWorld)
786 if(!m_objectUpdated)
788 ObjectAccessor::Instance().AddUpdateObject(this);
789 m_objectUpdated = true;
795 void Object::SetByteValue( uint16 index, uint8 offset, uint8 value )
797 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
799 if(offset > 4)
801 sLog.outError("Object::SetByteValue: wrong offset %u", offset);
802 return;
805 if(uint8(m_uint32Values[ index ] >> (offset * 8)) != value)
807 m_uint32Values[ index ] &= ~uint32(uint32(0xFF) << (offset * 8));
808 m_uint32Values[ index ] |= uint32(uint32(value) << (offset * 8));
810 if(m_inWorld)
812 if(!m_objectUpdated)
814 ObjectAccessor::Instance().AddUpdateObject(this);
815 m_objectUpdated = true;
821 void Object::SetUInt16Value( uint16 index, uint8 offset, uint16 value )
823 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
825 if(offset > 2)
827 sLog.outError("Object::SetUInt16Value: wrong offset %u", offset);
828 return;
831 if(uint8(m_uint32Values[ index ] >> (offset * 16)) != value)
833 m_uint32Values[ index ] &= ~uint32(uint32(0xFFFF) << (offset * 16));
834 m_uint32Values[ index ] |= uint32(uint32(value) << (offset * 16));
836 if(m_inWorld)
838 if(!m_objectUpdated)
840 ObjectAccessor::Instance().AddUpdateObject(this);
841 m_objectUpdated = true;
847 void Object::SetStatFloatValue( uint16 index, float value)
849 if(value < 0)
850 value = 0.0f;
852 SetFloatValue(index, value);
855 void Object::SetStatInt32Value( uint16 index, int32 value)
857 if(value < 0)
858 value = 0;
860 SetUInt32Value(index, uint32(value));
863 void Object::ApplyModUInt32Value(uint16 index, int32 val, bool apply)
865 int32 cur = GetUInt32Value(index);
866 cur += (apply ? val : -val);
867 if(cur < 0)
868 cur = 0;
869 SetUInt32Value(index,cur);
872 void Object::ApplyModInt32Value(uint16 index, int32 val, bool apply)
874 int32 cur = GetInt32Value(index);
875 cur += (apply ? val : -val);
876 SetInt32Value(index,cur);
879 void Object::ApplyModSignedFloatValue(uint16 index, float val, bool apply)
881 float cur = GetFloatValue(index);
882 cur += (apply ? val : -val);
883 SetFloatValue(index,cur);
886 void Object::ApplyModPositiveFloatValue(uint16 index, float val, bool apply)
888 float cur = GetFloatValue(index);
889 cur += (apply ? val : -val);
890 if(cur < 0)
891 cur = 0;
892 SetFloatValue(index,cur);
895 void Object::SetFlag( uint16 index, uint32 newFlag )
897 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
898 uint32 oldval = m_uint32Values[ index ];
899 uint32 newval = oldval | newFlag;
901 if(oldval != newval)
903 m_uint32Values[ index ] = newval;
905 if(m_inWorld)
907 if(!m_objectUpdated)
909 ObjectAccessor::Instance().AddUpdateObject(this);
910 m_objectUpdated = true;
916 void Object::RemoveFlag( uint16 index, uint32 oldFlag )
918 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
919 uint32 oldval = m_uint32Values[ index ];
920 uint32 newval = oldval & ~oldFlag;
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::SetByteFlag( uint16 index, uint8 offset, uint8 newFlag )
939 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
941 if(offset > 4)
943 sLog.outError("Object::SetByteFlag: wrong offset %u", offset);
944 return;
947 if(!(uint8(m_uint32Values[ index ] >> (offset * 8)) & newFlag))
949 m_uint32Values[ index ] |= uint32(uint32(newFlag) << (offset * 8));
951 if(m_inWorld)
953 if(!m_objectUpdated)
955 ObjectAccessor::Instance().AddUpdateObject(this);
956 m_objectUpdated = true;
962 void Object::RemoveByteFlag( uint16 index, uint8 offset, uint8 oldFlag )
964 ASSERT( index < m_valuesCount || PrintIndexError( index , true ) );
966 if(offset > 4)
968 sLog.outError("Object::RemoveByteFlag: wrong offset %u", offset);
969 return;
972 if(uint8(m_uint32Values[ index ] >> (offset * 8)) & oldFlag)
974 m_uint32Values[ index ] &= ~uint32(uint32(oldFlag) << (offset * 8));
976 if(m_inWorld)
978 if(!m_objectUpdated)
980 ObjectAccessor::Instance().AddUpdateObject(this);
981 m_objectUpdated = true;
987 bool Object::PrintIndexError(uint32 index, bool set) const
989 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);
991 // assert must fail after function call
992 return false;
995 WorldObject::WorldObject()
997 m_positionX = 0.0f;
998 m_positionY = 0.0f;
999 m_positionZ = 0.0f;
1000 m_orientation = 0.0f;
1002 m_mapId = 0;
1003 m_InstanceId = 0;
1005 m_name = "";
1007 mSemaphoreTeleport = false;
1010 void WorldObject::_Create( uint32 guidlow, HighGuid guidhigh, uint32 mapid )
1012 Object::_Create(guidlow, 0, guidhigh);
1014 m_mapId = mapid;
1017 uint32 WorldObject::GetZoneId() const
1019 return MapManager::Instance().GetBaseMap(m_mapId)->GetZoneId(m_positionX,m_positionY);
1022 uint32 WorldObject::GetAreaId() const
1024 return MapManager::Instance().GetBaseMap(m_mapId)->GetAreaId(m_positionX,m_positionY);
1027 InstanceData* WorldObject::GetInstanceData()
1029 Map *map = GetMap();
1030 return map->IsDungeon() ? ((InstanceMap*)map)->GetInstanceData() : NULL;
1033 //slow
1034 float WorldObject::GetDistance(const WorldObject* obj) const
1036 float dx = GetPositionX() - obj->GetPositionX();
1037 float dy = GetPositionY() - obj->GetPositionY();
1038 float dz = GetPositionZ() - obj->GetPositionZ();
1039 float sizefactor = GetObjectSize() + obj->GetObjectSize();
1040 float dist = sqrt((dx*dx) + (dy*dy) + (dz*dz)) - sizefactor;
1041 return ( dist > 0 ? dist : 0);
1044 float WorldObject::GetDistance2d(float x, float y) const
1046 float dx = GetPositionX() - x;
1047 float dy = GetPositionY() - y;
1048 float sizefactor = GetObjectSize();
1049 float dist = sqrt((dx*dx) + (dy*dy)) - sizefactor;
1050 return ( dist > 0 ? dist : 0);
1053 float WorldObject::GetDistance(const float x, const float y, const float z) const
1055 float dx = GetPositionX() - x;
1056 float dy = GetPositionY() - y;
1057 float dz = GetPositionZ() - z;
1058 float sizefactor = GetObjectSize();
1059 float dist = sqrt((dx*dx) + (dy*dy) + (dz*dz)) - sizefactor;
1060 return ( dist > 0 ? dist : 0);
1063 float WorldObject::GetDistance2d(const WorldObject* obj) const
1065 float dx = GetPositionX() - obj->GetPositionX();
1066 float dy = GetPositionY() - obj->GetPositionY();
1067 float sizefactor = GetObjectSize() + obj->GetObjectSize();
1068 float dist = sqrt((dx*dx) + (dy*dy)) - sizefactor;
1069 return ( dist > 0 ? dist : 0);
1072 float WorldObject::GetDistanceZ(const WorldObject* obj) const
1074 float dz = fabs(GetPositionZ() - obj->GetPositionZ());
1075 float sizefactor = GetObjectSize() + obj->GetObjectSize();
1076 float dist = dz - sizefactor;
1077 return ( dist > 0 ? dist : 0);
1080 bool WorldObject::IsWithinDistInMap(const WorldObject* obj, const float dist2compare, const bool is3D) const
1082 if (!obj || !IsInMap(obj)) return false;
1084 float dx = GetPositionX() - obj->GetPositionX();
1085 float dy = GetPositionY() - obj->GetPositionY();
1086 float distsq = dx*dx + dy*dy;
1087 if(is3D)
1089 float dz = GetPositionZ() - obj->GetPositionZ();
1090 distsq += dz*dz;
1092 float sizefactor = GetObjectSize() + obj->GetObjectSize();
1093 float maxdist = dist2compare + sizefactor;
1095 return distsq < maxdist * maxdist;
1098 bool WorldObject::IsWithinLOSInMap(const WorldObject* obj) const
1100 if (!IsInMap(obj)) return false;
1101 float ox,oy,oz;
1102 obj->GetPosition(ox,oy,oz);
1103 return(IsWithinLOS(ox, oy, oz ));
1106 bool WorldObject::IsWithinLOS(const float ox, const float oy, const float oz ) const
1108 float x,y,z;
1109 GetPosition(x,y,z);
1110 VMAP::IVMapManager *vMapManager = VMAP::VMapFactory::createOrGetVMapManager();
1111 return vMapManager->isInLineOfSight(GetMapId(), x, y, z+2.0f, ox, oy, oz+2.0f);
1114 float WorldObject::GetAngle(const WorldObject* obj) const
1116 if(!obj) return 0;
1117 return GetAngle( obj->GetPositionX(), obj->GetPositionY() );
1120 // Return angle in range 0..2*pi
1121 float WorldObject::GetAngle( const float x, const float y ) const
1123 float dx = x - GetPositionX();
1124 float dy = y - GetPositionY();
1126 float ang = atan2(dy, dx);
1127 ang = (ang >= 0) ? ang : 2 * M_PI + ang;
1128 return ang;
1131 bool WorldObject::HasInArc(const float arcangle, const WorldObject* obj) const
1133 float arc = arcangle;
1135 // move arc to range 0.. 2*pi
1136 while( arc >= 2.0f * M_PI )
1137 arc -= 2.0f * M_PI;
1138 while( arc < 0 )
1139 arc += 2.0f * M_PI;
1141 float angle = GetAngle( obj );
1142 angle -= m_orientation;
1144 // move angle to range -pi ... +pi
1145 while( angle > M_PI)
1146 angle -= 2.0f * M_PI;
1147 while(angle < -M_PI)
1148 angle += 2.0f * M_PI;
1150 float lborder = -1 * (arc/2.0f); // in range -pi..0
1151 float rborder = (arc/2.0f); // in range 0..pi
1152 return (( angle >= lborder ) && ( angle <= rborder ));
1155 void WorldObject::GetRandomPoint( float x, float y, float z, float distance, float &rand_x, float &rand_y, float &rand_z) const
1157 if(distance==0)
1159 rand_x = x;
1160 rand_y = y;
1161 rand_z = z;
1162 return;
1165 // angle to face `obj` to `this`
1166 float angle = rand_norm()*2*M_PI;
1167 float new_dist = rand_norm()*distance;
1169 rand_x = x + new_dist * cos(angle);
1170 rand_y = y + new_dist * sin(angle);
1171 rand_z = z;
1173 MaNGOS::NormalizeMapCoord(rand_x);
1174 MaNGOS::NormalizeMapCoord(rand_y);
1175 UpdateGroundPositionZ(rand_x,rand_y,rand_z); // update to LOS height if available
1178 void WorldObject::UpdateGroundPositionZ(float x, float y, float &z) const
1180 float new_z = MapManager::Instance().GetBaseMap(GetMapId())->GetHeight(x,y,z,true);
1181 if(new_z > INVALID_HEIGHT)
1182 z = new_z+ 0.05f; // just to be sure that we are not a few pixel under the surface
1185 bool WorldObject::IsPositionValid() const
1187 return MaNGOS::IsValidMapCoord(m_positionX,m_positionY,m_positionZ,m_orientation);
1190 void WorldObject::MonsterSay(const char* text, uint32 language, uint64 TargetGuid)
1192 WorldPacket data(SMSG_MESSAGECHAT, 200);
1193 BuildMonsterChat(&data,CHAT_MSG_MONSTER_SAY,text,language,GetName(),TargetGuid);
1194 SendMessageToSetInRange(&data,sWorld.getConfig(CONFIG_LISTEN_RANGE_SAY),true);
1197 void WorldObject::MonsterYell(const char* text, uint32 language, uint64 TargetGuid)
1199 WorldPacket data(SMSG_MESSAGECHAT, 200);
1200 BuildMonsterChat(&data,CHAT_MSG_MONSTER_YELL,text,language,GetName(),TargetGuid);
1201 SendMessageToSetInRange(&data,sWorld.getConfig(CONFIG_LISTEN_RANGE_YELL),true);
1204 void WorldObject::MonsterTextEmote(const char* text, uint64 TargetGuid, bool IsBossEmote)
1206 WorldPacket data(SMSG_MESSAGECHAT, 200);
1207 BuildMonsterChat(&data,IsBossEmote ? CHAT_MSG_RAID_BOSS_EMOTE : CHAT_MSG_MONSTER_EMOTE,text,LANG_UNIVERSAL,GetName(),TargetGuid);
1208 SendMessageToSetInRange(&data,sWorld.getConfig(CONFIG_LISTEN_RANGE_TEXTEMOTE),true);
1211 void WorldObject::MonsterWhisper(const char* text, uint64 receiver, bool IsBossWhisper)
1213 Player *player = objmgr.GetPlayer(receiver);
1214 if(!player || !player->GetSession())
1215 return;
1217 WorldPacket data(SMSG_MESSAGECHAT, 200);
1218 BuildMonsterChat(&data,IsBossWhisper ? CHAT_MSG_RAID_BOSS_WHISPER : CHAT_MSG_MONSTER_WHISPER,text,LANG_UNIVERSAL,GetName(),receiver);
1220 player->GetSession()->SendPacket(&data);
1223 namespace MaNGOS
1225 class MessageChatLocaleCacheDo
1227 public:
1228 MessageChatLocaleCacheDo(WorldObject const& obj, ChatMsg msgtype, int32 textId, uint32 language, uint64 targetGUID, float dist)
1229 : i_object(obj), i_msgtype(msgtype), i_textId(textId), i_language(language),
1230 i_targetGUID(targetGUID), i_dist(dist)
1234 ~MessageChatLocaleCacheDo()
1236 for(int i = 0; i < i_data_cache.size(); ++i)
1237 delete i_data_cache[i];
1240 void operator()(Player* p)
1242 // skip far away players
1243 if(p->GetDistance(&i_object) > i_dist)
1244 return;
1246 uint32 loc_idx = p->GetSession()->GetSessionDbLocaleIndex();
1247 uint32 cache_idx = loc_idx+1;
1248 WorldPacket* data;
1250 // create if not cached yet
1251 if(i_data_cache.size() < cache_idx+1 || !i_data_cache[cache_idx])
1253 if(i_data_cache.size() < cache_idx+1)
1254 i_data_cache.resize(cache_idx+1);
1256 char const* text = objmgr.GetMangosString(i_textId,loc_idx);
1258 data = new WorldPacket(SMSG_MESSAGECHAT, 200);
1260 // TODO: i_object.GetName() also must be localized?
1261 i_object.BuildMonsterChat(data,i_msgtype,text,i_language,i_object.GetNameForLocaleIdx(loc_idx),i_targetGUID);
1263 i_data_cache[cache_idx] = data;
1265 else
1266 data = i_data_cache[cache_idx];
1268 p->SendDirectMessage(data);
1271 private:
1272 WorldObject const& i_object;
1273 ChatMsg i_msgtype;
1274 int32 i_textId;
1275 uint32 i_language;
1276 uint64 i_targetGUID;
1277 float i_dist;
1278 std::vector<WorldPacket*> i_data_cache; // 0 = default, i => i-1 locale index
1280 } // namespace MaNGOS
1282 void WorldObject::MonsterSay(int32 textId, uint32 language, uint64 TargetGuid)
1284 CellPair p = MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY());
1286 Cell cell(p);
1287 cell.data.Part.reserved = ALL_DISTRICT;
1288 cell.SetNoCreate();
1290 MaNGOS::MessageChatLocaleCacheDo say_do(*this, CHAT_MSG_MONSTER_SAY, textId,language,TargetGuid,sWorld.getConfig(CONFIG_LISTEN_RANGE_SAY));
1291 MaNGOS::PlayerWorker<MaNGOS::MessageChatLocaleCacheDo> say_worker(say_do);
1292 TypeContainerVisitor<MaNGOS::PlayerWorker<MaNGOS::MessageChatLocaleCacheDo>, WorldTypeMapContainer > message(say_worker);
1293 CellLock<GridReadGuard> cell_lock(cell, p);
1294 cell_lock->Visit(cell_lock, message, *GetMap());
1297 void WorldObject::MonsterYell(int32 textId, uint32 language, uint64 TargetGuid)
1299 CellPair p = MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY());
1301 Cell cell(p);
1302 cell.data.Part.reserved = ALL_DISTRICT;
1303 cell.SetNoCreate();
1305 MaNGOS::MessageChatLocaleCacheDo say_do(*this, CHAT_MSG_MONSTER_YELL, textId,language,TargetGuid,sWorld.getConfig(CONFIG_LISTEN_RANGE_YELL));
1306 MaNGOS::PlayerWorker<MaNGOS::MessageChatLocaleCacheDo> say_worker(say_do);
1307 TypeContainerVisitor<MaNGOS::PlayerWorker<MaNGOS::MessageChatLocaleCacheDo>, WorldTypeMapContainer > message(say_worker);
1308 CellLock<GridReadGuard> cell_lock(cell, p);
1309 cell_lock->Visit(cell_lock, message, *GetMap());
1312 void WorldObject::MonsterTextEmote(int32 textId, uint64 TargetGuid, bool IsBossEmote)
1314 CellPair p = MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY());
1316 Cell cell(p);
1317 cell.data.Part.reserved = ALL_DISTRICT;
1318 cell.SetNoCreate();
1320 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));
1321 MaNGOS::PlayerWorker<MaNGOS::MessageChatLocaleCacheDo> say_worker(say_do);
1322 TypeContainerVisitor<MaNGOS::PlayerWorker<MaNGOS::MessageChatLocaleCacheDo>, WorldTypeMapContainer > message(say_worker);
1323 CellLock<GridReadGuard> cell_lock(cell, p);
1324 cell_lock->Visit(cell_lock, message, *GetMap());
1327 void WorldObject::MonsterWhisper(int32 textId, uint64 receiver, bool IsBossWhisper)
1329 Player *player = objmgr.GetPlayer(receiver);
1330 if(!player || !player->GetSession())
1331 return;
1333 uint32 loc_idx = player->GetSession()->GetSessionDbLocaleIndex();
1334 char const* text = objmgr.GetMangosString(textId,loc_idx);
1336 WorldPacket data(SMSG_MESSAGECHAT, 200);
1337 BuildMonsterChat(&data,IsBossWhisper ? CHAT_MSG_RAID_BOSS_WHISPER : CHAT_MSG_MONSTER_WHISPER,text,LANG_UNIVERSAL,GetName(),receiver);
1339 player->GetSession()->SendPacket(&data);
1342 void WorldObject::BuildMonsterChat(WorldPacket *data, uint8 msgtype, char const* text, uint32 language, char const* name, uint64 targetGuid) const
1344 bool pre = (msgtype==CHAT_MSG_MONSTER_EMOTE || msgtype==CHAT_MSG_RAID_BOSS_EMOTE);
1346 *data << (uint8)msgtype;
1347 *data << (uint32)language;
1348 *data << (uint64)GetGUID();
1349 *data << (uint32)0; //2.1.0
1350 *data << (uint32)(strlen(name)+1);
1351 *data << name;
1352 *data << (uint64)targetGuid; //Unit Target
1353 if( targetGuid && !IS_PLAYER_GUID(targetGuid) )
1355 *data << (uint32)1; // target name length
1356 *data << (uint8)0; // target name
1358 *data << (uint32)(strlen(text)+1+(pre?3:0));
1359 if(pre)
1360 data->append("%s ",3);
1361 *data << text;
1362 *data << (uint8)0; // ChatTag
1365 void WorldObject::BuildHeartBeatMsg(WorldPacket *data) const
1367 //Heartbeat message cannot be used for non-units
1368 if (!isType(TYPEMASK_UNIT))
1369 return;
1371 data->Initialize(MSG_MOVE_HEARTBEAT, 32);
1372 data->append(GetPackGUID());
1373 *data << uint32(((Unit*)this)->GetUnitMovementFlags()); // movement flags
1374 *data << uint8(0); // 2.3.0
1375 *data << getMSTime(); // time
1376 *data << m_positionX;
1377 *data << m_positionY;
1378 *data << m_positionZ;
1379 *data << m_orientation;
1380 *data << uint32(0);
1383 void WorldObject::BuildTeleportAckMsg(WorldPacket *data, float x, float y, float z, float ang) const
1385 //TeleportAck message cannot be used for non-units
1386 if (!isType(TYPEMASK_UNIT))
1387 return;
1389 data->Initialize(MSG_MOVE_TELEPORT_ACK, 41);
1390 data->append(GetPackGUID());
1391 *data << uint32(0); // this value increments every time
1392 *data << uint32(((Unit*)this)->GetUnitMovementFlags()); // movement flags
1393 *data << uint8(0); // 2.3.0
1394 *data << getMSTime(); // time
1395 *data << x;
1396 *data << y;
1397 *data << z;
1398 *data << ang;
1399 *data << uint32(0);
1402 void WorldObject::SendMessageToSet(WorldPacket *data, bool /*bToSelf*/)
1404 GetMap()->MessageBroadcast(this, data);
1407 void WorldObject::SendMessageToSetInRange(WorldPacket *data, float dist, bool /*bToSelf*/)
1409 GetMap()->MessageDistBroadcast(this, data, dist);
1412 void WorldObject::SendObjectDeSpawnAnim(uint64 guid)
1414 WorldPacket data(SMSG_GAMEOBJECT_DESPAWN_ANIM, 8);
1415 data << guid;
1416 SendMessageToSet(&data, true);
1419 Map* WorldObject::GetMap() const
1421 return MapManager::Instance().GetMap(GetMapId(), this);
1424 Map const* WorldObject::GetBaseMap() const
1426 return MapManager::Instance().GetBaseMap(GetMapId());
1429 void WorldObject::AddObjectToRemoveList()
1431 Map* map = GetMap();
1432 if(!map)
1434 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());
1435 return;
1438 map->AddObjectToRemoveList(this);
1441 Creature* WorldObject::SummonCreature(uint32 id, float x, float y, float z, float ang,TempSummonType spwtype,uint32 despwtime)
1443 TemporarySummon* pCreature = new TemporarySummon(GetGUID());
1445 pCreature->SetInstanceId(GetInstanceId());
1446 uint32 team = 0;
1447 if (GetTypeId()==TYPEID_PLAYER)
1448 team = ((Player*)this)->GetTeam();
1450 if (!pCreature->Create(objmgr.GenerateLowGuid(HIGHGUID_UNIT), GetMap(), id, team))
1452 delete pCreature;
1453 return NULL;
1456 if (x == 0.0f && y == 0.0f && z == 0.0f)
1457 GetClosePoint(x, y, z, pCreature->GetObjectSize());
1459 pCreature->Relocate(x, y, z, ang);
1461 if(!pCreature->IsPositionValid())
1463 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());
1464 delete pCreature;
1465 return NULL;
1468 pCreature->Summon(spwtype, despwtime);
1470 if(GetTypeId()==TYPEID_UNIT && ((Creature*)this)->AI())
1471 ((Creature*)this)->AI()->JustSummoned(pCreature);
1473 //return the creature therewith the summoner has access to it
1474 return pCreature;
1477 namespace MaNGOS
1479 class NearUsedPosDo
1481 public:
1482 NearUsedPosDo(WorldObject const& obj, WorldObject const* searcher, float angle, ObjectPosSelector& selector)
1483 : i_object(obj), i_searcher(searcher), i_angle(angle), i_selector(selector) {}
1485 void operator()(Corpse*) const {}
1486 void operator()(DynamicObject*) const {}
1488 void operator()(Creature* c) const
1490 // skip self or target
1491 if(c==i_searcher || c==&i_object)
1492 return;
1494 float x,y,z;
1496 if( !c->isAlive() || c->hasUnitState(UNIT_STAT_ROOT | UNIT_STAT_STUNNED | UNIT_STAT_DISTRACTED) ||
1497 !c->GetMotionMaster()->GetDestination(x,y,z) )
1499 x = c->GetPositionX();
1500 y = c->GetPositionY();
1503 add(c,x,y);
1506 template<class T>
1507 void operator()(T* u) const
1509 // skip self or target
1510 if(u==i_searcher || u==&i_object)
1511 return;
1513 float x,y;
1515 x = u->GetPositionX();
1516 y = u->GetPositionY();
1518 add(u,x,y);
1521 // we must add used pos that can fill places around center
1522 void add(WorldObject* u, float x, float y) const
1524 // dist include size of u
1525 float dist2d = i_object.GetDistance2d(x,y);
1527 // u is too nearest to i_object
1528 if(dist2d + i_object.GetObjectSize() + u->GetObjectSize() < i_selector.m_dist - i_selector.m_size)
1529 return;
1531 // u is too far away from i_object
1532 if(dist2d + i_object.GetObjectSize() - u->GetObjectSize() > i_selector.m_dist + i_selector.m_size)
1533 return;
1535 float angle = i_object.GetAngle(u)-i_angle;
1537 // move angle to range -pi ... +pi
1538 while( angle > M_PI)
1539 angle -= 2.0f * M_PI;
1540 while(angle < -M_PI)
1541 angle += 2.0f * M_PI;
1543 i_selector.AddUsedPos(u->GetObjectSize(),angle,dist2d + i_object.GetObjectSize());
1545 private:
1546 WorldObject const& i_object;
1547 WorldObject const* i_searcher;
1548 float i_angle;
1549 ObjectPosSelector& i_selector;
1551 } // namespace MaNGOS
1553 //===================================================================================================
1555 void WorldObject::GetNearPoint2D(float &x, float &y, float distance2d, float absAngle ) const
1557 x = GetPositionX() + (GetObjectSize() + distance2d) * cos(absAngle);
1558 y = GetPositionY() + (GetObjectSize() + distance2d) * sin(absAngle);
1560 MaNGOS::NormalizeMapCoord(x);
1561 MaNGOS::NormalizeMapCoord(y);
1564 void WorldObject::GetNearPoint(WorldObject const* searcher, float &x, float &y, float &z, float searcher_size, float distance2d, float absAngle ) const
1566 GetNearPoint2D(x,y,distance2d+searcher_size,absAngle);
1567 z = GetPositionZ();
1569 // if detection disabled, return first point
1570 if(!sWorld.getConfig(CONFIG_DETECT_POS_COLLISION))
1572 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1573 return;
1576 // or remember first point
1577 float first_x = x;
1578 float first_y = y;
1579 bool first_los_conflict = false; // first point LOS problems
1581 // prepare selector for work
1582 ObjectPosSelector selector(GetPositionX(),GetPositionY(),GetObjectSize(),distance2d+searcher_size);
1584 // adding used positions around object
1586 CellPair p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
1587 Cell cell(p);
1588 cell.data.Part.reserved = ALL_DISTRICT;
1589 cell.SetNoCreate();
1591 MaNGOS::NearUsedPosDo u_do(*this,searcher,absAngle,selector);
1592 MaNGOS::WorldObjectWorker<MaNGOS::NearUsedPosDo> worker(u_do);
1594 TypeContainerVisitor<MaNGOS::WorldObjectWorker<MaNGOS::NearUsedPosDo>, GridTypeMapContainer > grid_obj_worker(worker);
1595 TypeContainerVisitor<MaNGOS::WorldObjectWorker<MaNGOS::NearUsedPosDo>, WorldTypeMapContainer > world_obj_worker(worker);
1597 CellLock<GridReadGuard> cell_lock(cell, p);
1598 cell_lock->Visit(cell_lock, grid_obj_worker, *GetMap());
1599 cell_lock->Visit(cell_lock, world_obj_worker, *GetMap());
1602 // maybe can just place in primary position
1603 if( selector.CheckOriginal() )
1605 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1607 if(IsWithinLOS(x,y,z))
1608 return;
1610 first_los_conflict = true; // first point have LOS problems
1613 float angle; // candidate of angle for free pos
1615 // special case when one from list empty and then empty side preferred
1616 if(selector.FirstAngle(angle))
1618 GetNearPoint2D(x,y,distance2d,absAngle+angle);
1619 z = GetPositionZ();
1620 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1622 if(IsWithinLOS(x,y,z))
1623 return;
1626 // set first used pos in lists
1627 selector.InitializeAngle();
1629 // select in positions after current nodes (selection one by one)
1630 while(selector.NextAngle(angle)) // angle for free pos
1632 GetNearPoint2D(x,y,distance2d,absAngle+angle);
1633 z = GetPositionZ();
1634 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1636 if(IsWithinLOS(x,y,z))
1637 return;
1640 // BAD NEWS: not free pos (or used or have LOS problems)
1641 // Attempt find _used_ pos without LOS problem
1643 if(!first_los_conflict)
1645 x = first_x;
1646 y = first_y;
1648 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1649 return;
1652 // special case when one from list empty and then empty side preferred
1653 if( selector.IsNonBalanced() )
1655 if(!selector.FirstAngle(angle)) // _used_ pos
1657 GetNearPoint2D(x,y,distance2d,absAngle+angle);
1658 z = GetPositionZ();
1659 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1661 if(IsWithinLOS(x,y,z))
1662 return;
1666 // set first used pos in lists
1667 selector.InitializeAngle();
1669 // select in positions after current nodes (selection one by one)
1670 while(selector.NextUsedAngle(angle)) // angle for used pos but maybe without LOS problem
1672 GetNearPoint2D(x,y,distance2d,absAngle+angle);
1673 z = GetPositionZ();
1674 UpdateGroundPositionZ(x,y,z); // update to LOS height if available
1676 if(IsWithinLOS(x,y,z))
1677 return;
1680 // BAD BAD NEWS: all found pos (free and used) have LOS problem :(
1681 x = first_x;
1682 y = first_y;
1684 UpdateGroundPositionZ(x,y,z); // update to LOS height if available