2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "SharedDefines.h"
21 #include "WorldPacket.h"
28 #include "ObjectMgr.h"
29 #include "WorldSession.h"
30 #include "UpdateData.h"
31 #include "UpdateMask.h"
33 #include "MapManager.h"
34 #include "ObjectAccessor.h"
36 #include "Transports.h"
37 #include "TargetedMovementGenerator.h"
38 #include "WaypointMovementGenerator.h"
39 #include "VMapFactory.h"
41 #include "GridNotifiers.h"
42 #include "GridNotifiersImpl.h"
44 #include "ObjectPosSelector.h"
46 #include "TemporarySummon.h"
48 uint32
GuidHigh2TypeId(uint32 guid_hi
)
52 case HIGHGUID_ITEM
: return TYPEID_ITEM
;
53 //case HIGHGUID_CONTAINER: return TYPEID_CONTAINER; HIGHGUID_CONTAINER==HIGHGUID_ITEM currently
54 case HIGHGUID_UNIT
: return TYPEID_UNIT
;
55 case HIGHGUID_PET
: return TYPEID_UNIT
;
56 case HIGHGUID_PLAYER
: return TYPEID_PLAYER
;
57 case HIGHGUID_GAMEOBJECT
: return TYPEID_GAMEOBJECT
;
58 case HIGHGUID_DYNAMICOBJECT
:return TYPEID_DYNAMICOBJECT
;
59 case HIGHGUID_CORPSE
: return TYPEID_CORPSE
;
60 case HIGHGUID_MO_TRANSPORT
: return TYPEID_GAMEOBJECT
;
61 case HIGHGUID_VEHICLE
: return TYPEID_UNIT
;
63 return MAX_TYPEID
; // unknown
68 m_objectTypeId
= TYPEID_OBJECT
;
69 m_objectType
= TYPEMASK_OBJECT
;
72 m_uint32Values_mirror
= 0;
76 m_objectUpdated
= false;
79 m_PackGUID
.appendPackGUID(0);
85 ObjectAccessor::Instance().RemoveUpdateObject(this);
91 ///- Do NOT call RemoveFromWorld here, if the object is a player it will crash
92 sLog
.outError("Object::~Object - guid="I64FMTD
", typeid=%d deleted but still in world!!", GetGUID(), GetTypeId());
96 //DEBUG_LOG("Object desctr 1 check (%p)",(void*)this);
97 delete [] m_uint32Values
;
98 delete [] m_uint32Values_mirror
;
99 //DEBUG_LOG("Object desctr 2 check (%p)",(void*)this);
103 void Object::_InitValues()
105 m_uint32Values
= new uint32
[ m_valuesCount
];
106 memset(m_uint32Values
, 0, m_valuesCount
*sizeof(uint32
));
108 m_uint32Values_mirror
= new uint32
[ m_valuesCount
];
109 memset(m_uint32Values_mirror
, 0, m_valuesCount
*sizeof(uint32
));
111 m_objectUpdated
= false;
114 void Object::_Create( uint32 guidlow
, uint32 entry
, HighGuid guidhigh
)
116 if(!m_uint32Values
) _InitValues();
118 uint64 guid
= MAKE_NEW_GUID(guidlow
, entry
, guidhigh
); // required more changes to make it working
119 SetUInt64Value( OBJECT_FIELD_GUID
, guid
);
120 SetUInt32Value( OBJECT_FIELD_TYPE
, m_objectType
);
122 m_PackGUID
.appendPackGUID(GetGUID());
125 void Object::BuildMovementUpdateBlock(UpdateData
* data
, uint32 flags
) const
129 buf
<< uint8( UPDATETYPE_MOVEMENT
);
132 _BuildMovementUpdate(&buf
, flags
, 0x00000000);
134 data
->AddUpdateBlock(buf
);
137 void Object::BuildCreateUpdateBlockForPlayer(UpdateData
*data
, Player
*target
) const
144 uint8 updatetype
= UPDATETYPE_CREATE_OBJECT
;
145 uint8 flags
= m_updateFlag
;
149 if(target
== this) // building packet for oneself
150 flags
|= UPDATEFLAG_SELF
;
152 if(flags
& UPDATEFLAG_HAS_POSITION
)
154 // UPDATETYPE_CREATE_OBJECT2 dynamic objects, corpses...
155 if(isType(TYPEMASK_DYNAMICOBJECT
) || isType(TYPEMASK_CORPSE
) || isType(TYPEMASK_PLAYER
))
156 updatetype
= UPDATETYPE_CREATE_OBJECT2
;
158 // UPDATETYPE_CREATE_OBJECT2 for pets...
159 if(target
->GetPetGUID() == GetGUID())
160 updatetype
= UPDATETYPE_CREATE_OBJECT2
;
162 // UPDATETYPE_CREATE_OBJECT2 for some gameobject types...
163 if(isType(TYPEMASK_GAMEOBJECT
))
165 switch(((GameObject
*)this)->GetGoType())
167 case GAMEOBJECT_TYPE_TRAP
:
168 case GAMEOBJECT_TYPE_DUEL_ARBITER
:
169 case GAMEOBJECT_TYPE_FLAGSTAND
:
170 case GAMEOBJECT_TYPE_FLAGDROP
:
171 updatetype
= UPDATETYPE_CREATE_OBJECT2
;
173 case GAMEOBJECT_TYPE_TRANSPORT
:
174 flags
|= UPDATEFLAG_TRANSPORT
;
179 if(isType(TYPEMASK_UNIT
))
181 if(((Unit
*)this)->getVictim())
182 flags
|= UPDATEFLAG_HAS_TARGET
;
186 //sLog.outDebug("BuildCreateUpdate: update-type: %u, object-type: %u got flags: %X, flags2: %X", updatetype, m_objectTypeId, flags, flags2);
189 buf
<< (uint8
)updatetype
;
190 //buf.append(GetPackGUID()); //client crashes when using this
191 buf
<< (uint8
)0xFF << GetGUID();
192 buf
<< (uint8
)m_objectTypeId
;
194 _BuildMovementUpdate(&buf
, flags
, flags2
);
196 UpdateMask updateMask
;
197 updateMask
.SetCount( m_valuesCount
);
198 _SetCreateBits( &updateMask
, target
);
199 _BuildValuesUpdate(updatetype
, &buf
, &updateMask
, target
);
200 data
->AddUpdateBlock(buf
);
203 void Object::BuildUpdate(UpdateDataMapType
&update_players
)
205 ObjectAccessor::_buildUpdateObject(this,update_players
);
206 ClearUpdateMask(true);
209 void Object::SendUpdateToPlayer(Player
* player
)
211 // send update to another players
212 SendUpdateObjectToAllExcept(player
);
214 // send create update to player
219 BuildCreateUpdateBlockForPlayer(&upd
, player
);
220 upd
.BuildPacket(&packet
);
221 player
->GetSession()->SendPacket(&packet
);
223 // now object updated/(create updated)
226 void Object::BuildValuesUpdateBlockForPlayer(UpdateData
*data
, Player
*target
) const
230 buf
<< (uint8
) UPDATETYPE_VALUES
;
231 //buf.append(GetPackGUID()); //client crashes when using this. but not have crash in debug mode
235 UpdateMask updateMask
;
236 updateMask
.SetCount( m_valuesCount
);
238 _SetUpdateBits( &updateMask
, target
);
239 _BuildValuesUpdate(UPDATETYPE_VALUES
, &buf
, &updateMask
, target
);
241 data
->AddUpdateBlock(buf
);
244 void Object::BuildOutOfRangeUpdateBlock(UpdateData
* data
) const
246 data
->AddOutOfRangeGUID(GetGUID());
249 void Object::DestroyForPlayer(Player
*target
) const
253 WorldPacket
data(SMSG_DESTROY_OBJECT
, 8);
255 data
<< uint8(0); // WotLK (bool)
256 target
->GetSession()->SendPacket( &data
);
259 void Object::_BuildMovementUpdate(ByteBuffer
* data
, uint8 flags
, uint32 flags2
) const
261 uint16 unk_flags
= ((GetTypeId() == TYPEID_PLAYER
) ? ((Player
*)this)->m_movementInfo
.unk1
: 0);
263 if(GetTypeId() == TYPEID_UNIT
)
264 if(((Creature
*)this)->isVehicle())
265 unk_flags
|= 0x20; // always allow pitch
267 *data
<< (uint8
)flags
; // update flags
270 if (flags
& UPDATEFLAG_LIVING
)
276 flags2
= ((Unit
*)this)->GetUnitMovementFlags();
281 flags2
= ((Player
*)this)->GetUnitMovementFlags();
283 if(((Player
*)this)->GetTransport())
284 flags2
|= MOVEMENTFLAG_ONTRANSPORT
;
286 flags2
&= ~MOVEMENTFLAG_ONTRANSPORT
;
288 // remove unknown, unused etc flags for now
289 flags2
&= ~MOVEMENTFLAG_SPLINE2
; // will be set manually
291 if(((Player
*)this)->isInFlight())
293 WPAssert(((Player
*)this)->GetMotionMaster()->GetCurrentMovementGeneratorType() == FLIGHT_MOTION_TYPE
);
294 flags2
= (MOVEMENTFLAG_FORWARD
| MOVEMENTFLAG_SPLINE2
);
300 *data
<< uint32(flags2
); // movement flags
301 *data
<< uint16(unk_flags
); // unknown 2.3.0
302 *data
<< uint32(getMSTime()); // time (in milliseconds)
306 if (flags
& UPDATEFLAG_HAS_POSITION
)
309 if(flags
& UPDATEFLAG_TRANSPORT
&& ((GameObject
*)this)->GetGoType() == GAMEOBJECT_TYPE_MO_TRANSPORT
)
314 *data
<< ((WorldObject
*)this)->GetOrientation();
318 *data
<< ((WorldObject
*)this)->GetPositionX();
319 *data
<< ((WorldObject
*)this)->GetPositionY();
320 *data
<< ((WorldObject
*)this)->GetPositionZ();
321 *data
<< ((WorldObject
*)this)->GetOrientation();
326 if(flags
& UPDATEFLAG_LIVING
)
329 if(flags2
& MOVEMENTFLAG_ONTRANSPORT
)
331 if(GetTypeId() == TYPEID_PLAYER
)
333 *data
<< (uint64
)((Player
*)this)->GetTransport()->GetGUID();
334 *data
<< (float)((Player
*)this)->GetTransOffsetX();
335 *data
<< (float)((Player
*)this)->GetTransOffsetY();
336 *data
<< (float)((Player
*)this)->GetTransOffsetZ();
337 *data
<< (float)((Player
*)this)->GetTransOffsetO();
338 *data
<< (uint32
)((Player
*)this)->GetTransTime();
339 *data
<< (int8
)((Player
*)this)->GetTransSeat();
341 //MaNGOS currently not have support for other than player on transport
345 if((flags2
& (MOVEMENTFLAG_SWIMMING
| MOVEMENTFLAG_FLYING2
)) || (unk_flags
& 0x20))
347 if(GetTypeId() == TYPEID_PLAYER
)
348 *data
<< (float)((Player
*)this)->m_movementInfo
.s_pitch
;
350 *data
<< (float)0; // is't part of movement packet, we must store and send it...
353 if(GetTypeId() == TYPEID_PLAYER
)
354 *data
<< (uint32
)((Player
*)this)->m_movementInfo
.fallTime
;
356 *data
<< (uint32
)0; // last fall time
359 if(flags2
& MOVEMENTFLAG_JUMPING
)
361 if(GetTypeId() == TYPEID_PLAYER
)
363 *data
<< (float)((Player
*)this)->m_movementInfo
.j_unk
;
364 *data
<< (float)((Player
*)this)->m_movementInfo
.j_sinAngle
;
365 *data
<< (float)((Player
*)this)->m_movementInfo
.j_cosAngle
;
366 *data
<< (float)((Player
*)this)->m_movementInfo
.j_xyspeed
;
378 if(flags2
& MOVEMENTFLAG_SPLINE
)
380 if(GetTypeId() == TYPEID_PLAYER
)
381 *data
<< (float)((Player
*)this)->m_movementInfo
.u_unk1
;
386 *data
<< ((Unit
*)this)->GetSpeed( MOVE_WALK
);
387 *data
<< ((Unit
*)this)->GetSpeed( MOVE_RUN
);
388 *data
<< ((Unit
*)this)->GetSpeed( MOVE_SWIM_BACK
);
389 *data
<< ((Unit
*)this)->GetSpeed( MOVE_SWIM
);
390 *data
<< ((Unit
*)this)->GetSpeed( MOVE_RUN_BACK
);
391 *data
<< ((Unit
*)this)->GetSpeed( MOVE_FLIGHT
);
392 *data
<< ((Unit
*)this)->GetSpeed( MOVE_FLIGHT_BACK
);
393 *data
<< ((Unit
*)this)->GetSpeed( MOVE_TURN_RATE
);
394 *data
<< ((Unit
*)this)->GetSpeed( MOVE_PITCH_RATE
);
397 if(flags2
& MOVEMENTFLAG_SPLINE2
)
399 if(GetTypeId() != TYPEID_PLAYER
)
401 sLog
.outDebug("_BuildMovementUpdate: MOVEMENTFLAG_SPLINE2 for non-player");
405 if(!((Player
*)this)->isInFlight())
407 sLog
.outDebug("_BuildMovementUpdate: MOVEMENTFLAG_SPLINE2 but not in flight");
411 WPAssert(((Player
*)this)->GetMotionMaster()->GetCurrentMovementGeneratorType() == FLIGHT_MOTION_TYPE
);
413 FlightPathMovementGenerator
*fmg
= (FlightPathMovementGenerator
*)(((Player
*)this)->GetMotionMaster()->top());
415 uint32 flags3
= 0x00000300;
417 *data
<< uint32(flags3
); // splines flag?
419 if(flags3
& 0x10000) // probably x,y,z coords there
426 if(flags3
& 0x20000) // probably guid there
431 if(flags3
& 0x40000) // may be orientation
436 Path
&path
= fmg
->GetPath();
439 ((Player
*)this)->GetPosition(x
, y
, z
);
441 uint32 inflighttime
= uint32(path
.GetPassedLength(fmg
->GetCurrentNode(), x
, y
, z
) * 32);
442 uint32 traveltime
= uint32(path
.GetTotalLength() * 32);
444 *data
<< uint32(inflighttime
); // passed move time?
445 *data
<< uint32(traveltime
); // full move time?
446 *data
<< uint32(0); // ticks count?
448 uint32 poscount
= uint32(path
.Size());
450 *data
<< uint32(poscount
); // points count
452 for(uint32 i
= 0; i
< poscount
; ++i
)
454 *data
<< path
.GetNodes()[i
].x
;
455 *data
<< path
.GetNodes()[i
].y
;
456 *data
<< path
.GetNodes()[i
].z
;
459 /*for(uint32 i = 0; i < poscount; i++)
467 *data
<< path
.GetNodes()[poscount
-1].x
;
468 *data
<< path
.GetNodes()[poscount
-1].y
;
469 *data
<< path
.GetNodes()[poscount
-1].z
;
471 // target position (path end)
472 /**data << ((Unit*)this)->GetPositionX();
473 *data << ((Unit*)this)->GetPositionY();
474 *data << ((Unit*)this)->GetPositionZ();*/
479 if(flags
& UPDATEFLAG_LOWGUID
)
485 case TYPEID_CONTAINER
:
486 case TYPEID_GAMEOBJECT
:
487 case TYPEID_DYNAMICOBJECT
:
489 *data
<< uint32(GetGUIDLow()); // GetGUIDLow()
492 *data
<< uint32(0x0000000B); // unk, can be 0xB or 0xC
495 if(flags
& UPDATEFLAG_SELF
)
496 *data
<< uint32(0x0000002F); // unk, can be 0x15 or 0x22
498 *data
<< uint32(0x00000008); // unk, can be 0x7 or 0x8
501 *data
<< uint32(0x00000000); // unk
507 if(flags
& UPDATEFLAG_HIGHGUID
)
513 case TYPEID_CONTAINER
:
514 case TYPEID_GAMEOBJECT
:
515 case TYPEID_DYNAMICOBJECT
:
517 *data
<< uint32(GetGUIDHigh()); // GetGUIDHigh()
520 *data
<< uint32(0x0000000B); // unk, can be 0xB or 0xC
523 if(flags
& UPDATEFLAG_SELF
)
524 *data
<< uint32(0x0000002F); // unk, can be 0x15 or 0x22
526 *data
<< uint32(0x00000008); // unk, can be 0x7 or 0x8
529 *data
<< uint32(0x00000000); // unk
535 if(flags
& UPDATEFLAG_HAS_TARGET
) // packed guid (current target guid)
537 if(Unit
*victim
= ((Unit
*)this)->getVictim())
538 data
->append(victim
->GetPackGUID());
544 if(flags
& UPDATEFLAG_TRANSPORT
)
546 *data
<< uint32(getMSTime()); // ms time
550 if(flags
& UPDATEFLAG_VEHICLE
) // unused for now
552 *data
<< uint32(((Vehicle
*)this)->GetVehicleId()); // vehicle id
553 *data
<< float(0); // facing adjustment
557 void Object::_BuildValuesUpdate(uint8 updatetype
, ByteBuffer
* data
, UpdateMask
*updateMask
, Player
*target
) const
562 bool IsActivateToQuest
= false;
563 if (updatetype
== UPDATETYPE_CREATE_OBJECT
|| updatetype
== UPDATETYPE_CREATE_OBJECT2
)
565 if (isType(TYPEMASK_GAMEOBJECT
) && !((GameObject
*)this)->IsTransport())
567 if ( ((GameObject
*)this)->ActivateToQuest(target
) || target
->isGameMaster())
569 IsActivateToQuest
= true;
570 updateMask
->SetBit(GAMEOBJECT_DYNAMIC
);
574 else //case UPDATETYPE_VALUES
576 if (isType(TYPEMASK_GAMEOBJECT
) && !((GameObject
*)this)->IsTransport())
578 if ( ((GameObject
*)this)->ActivateToQuest(target
) || target
->isGameMaster())
580 IsActivateToQuest
= true;
582 updateMask
->SetBit(GAMEOBJECT_DYNAMIC
);
583 updateMask
->SetBit(GAMEOBJECT_BYTES_1
);
587 WPAssert(updateMask
&& updateMask
->GetCount() == m_valuesCount
);
589 *data
<< (uint8
)updateMask
->GetBlockCount();
590 data
->append( updateMask
->GetMask(), updateMask
->GetLength() );
592 // 2 specialized loops for speed optimization in non-unit case
593 if(isType(TYPEMASK_UNIT
)) // unit (creature/player) case
595 for( uint16 index
= 0; index
< m_valuesCount
; index
++ )
597 if( updateMask
->GetBit( index
) )
599 // remove custom flag before send
600 if( index
== UNIT_NPC_FLAGS
)
601 *data
<< uint32(m_uint32Values
[ index
] & ~UNIT_NPC_FLAG_GUARD
);
602 // FIXME: Some values at server stored in float format but must be sent to client in uint32 format
603 else if(index
>= UNIT_FIELD_BASEATTACKTIME
&& index
<= UNIT_FIELD_RANGEDATTACKTIME
)
605 // convert from float to uint32 and send
606 *data
<< uint32(m_floatValues
[ index
] < 0 ? 0 : m_floatValues
[ index
]);
608 // there are some float values which may be negative or can't get negative due to other checks
609 else if(index
>= UNIT_FIELD_NEGSTAT0
&& index
<= UNIT_FIELD_NEGSTAT4
||
610 index
>= UNIT_FIELD_RESISTANCEBUFFMODSPOSITIVE
&& index
<= (UNIT_FIELD_RESISTANCEBUFFMODSPOSITIVE
+ 6) ||
611 index
>= UNIT_FIELD_RESISTANCEBUFFMODSNEGATIVE
&& index
<= (UNIT_FIELD_RESISTANCEBUFFMODSNEGATIVE
+ 6) ||
612 index
>= UNIT_FIELD_POSSTAT0
&& index
<= UNIT_FIELD_POSSTAT4
)
614 *data
<< uint32(m_floatValues
[ index
]);
616 // Gamemasters should be always able to select units - remove not selectable flag
617 else if(index
== UNIT_FIELD_FLAGS
&& target
->isGameMaster())
619 *data
<< (m_uint32Values
[ index
] & ~UNIT_FLAG_NOT_SELECTABLE
);
621 // hide lootable animation for unallowed players
622 else if(index
== UNIT_DYNAMIC_FLAGS
&& GetTypeId() == TYPEID_UNIT
)
624 if(!target
->isAllowedToLoot((Creature
*)this))
625 *data
<< (m_uint32Values
[ index
] & ~UNIT_DYNFLAG_LOOTABLE
);
627 *data
<< (m_uint32Values
[ index
] & ~UNIT_DYNFLAG_OTHER_TAGGER
);
631 // send in current format (float as float, uint32 as uint32)
632 *data
<< m_uint32Values
[ index
];
637 else if(isType(TYPEMASK_GAMEOBJECT
)) // gameobject case
639 for( uint16 index
= 0; index
< m_valuesCount
; index
++ )
641 if( updateMask
->GetBit( index
) )
643 // send in current format (float as float, uint32 as uint32)
644 if ( index
== GAMEOBJECT_DYNAMIC
)
646 if(IsActivateToQuest
)
648 switch(((GameObject
*)this)->GetGoType())
650 case GAMEOBJECT_TYPE_CHEST
:
651 *data
<< uint32(9); // enable quest object. Represent 9, but 1 for client before 2.3.0
653 case GAMEOBJECT_TYPE_GOOBER
:
657 *data
<< uint32(0); // unknown. not happen.
662 *data
<< uint32(0); // disable quest object
665 *data
<< m_uint32Values
[ index
]; // other cases
669 else // other objects case (no special index checks)
671 for( uint16 index
= 0; index
< m_valuesCount
; index
++ )
673 if( updateMask
->GetBit( index
) )
675 // send in current format (float as float, uint32 as uint32)
676 *data
<< m_uint32Values
[ index
];
682 void Object::ClearUpdateMask(bool remove
)
684 for( uint16 index
= 0; index
< m_valuesCount
; index
++ )
686 if(m_uint32Values_mirror
[index
]!= m_uint32Values
[index
])
687 m_uint32Values_mirror
[index
] = m_uint32Values
[index
];
692 ObjectAccessor::Instance().RemoveUpdateObject(this);
693 m_objectUpdated
= false;
697 // Send current value fields changes to all viewers
698 void Object::SendUpdateObjectToAllExcept(Player
* exceptPlayer
)
700 // changes will be send in create packet
708 ObjectAccessor::UpdateObject(this,exceptPlayer
);
711 bool Object::LoadValues(const char* data
)
713 if(!m_uint32Values
) _InitValues();
715 Tokens tokens
= StrSplit(data
, " ");
717 if(tokens
.size() != m_valuesCount
)
720 Tokens::iterator iter
;
722 for (iter
= tokens
.begin(), index
= 0; index
< m_valuesCount
; ++iter
, ++index
)
724 m_uint32Values
[index
] = atol((*iter
).c_str());
730 void Object::_SetUpdateBits(UpdateMask
*updateMask
, Player
* /*target*/) const
732 for( uint16 index
= 0; index
< m_valuesCount
; index
++ )
734 if(m_uint32Values_mirror
[index
]!= m_uint32Values
[index
])
735 updateMask
->SetBit(index
);
739 void Object::_SetCreateBits(UpdateMask
*updateMask
, Player
* /*target*/) const
741 for( uint16 index
= 0; index
< m_valuesCount
; index
++ )
743 if(GetUInt32Value(index
) != 0)
744 updateMask
->SetBit(index
);
748 void Object::SetInt32Value( uint16 index
, int32 value
)
750 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, true ) );
752 if(m_int32Values
[ index
] != value
)
754 m_int32Values
[ index
] = value
;
760 ObjectAccessor::Instance().AddUpdateObject(this);
761 m_objectUpdated
= true;
767 void Object::SetUInt32Value( uint16 index
, uint32 value
)
769 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, true ) );
771 if(m_uint32Values
[ index
] != value
)
773 m_uint32Values
[ index
] = value
;
779 ObjectAccessor::Instance().AddUpdateObject(this);
780 m_objectUpdated
= true;
786 void Object::SetUInt64Value( uint16 index
, const uint64
&value
)
788 ASSERT( index
+ 1 < m_valuesCount
|| PrintIndexError( index
, true ) );
789 if(*((uint64
*)&(m_uint32Values
[ index
])) != value
)
791 m_uint32Values
[ index
] = *((uint32
*)&value
);
792 m_uint32Values
[ index
+ 1 ] = *(((uint32
*)&value
) + 1);
798 ObjectAccessor::Instance().AddUpdateObject(this);
799 m_objectUpdated
= true;
805 void Object::SetFloatValue( uint16 index
, float value
)
807 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, true ) );
809 if(m_floatValues
[ index
] != value
)
811 m_floatValues
[ index
] = value
;
817 ObjectAccessor::Instance().AddUpdateObject(this);
818 m_objectUpdated
= true;
824 void Object::SetByteValue( uint16 index
, uint8 offset
, uint8 value
)
826 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, true ) );
830 sLog
.outError("Object::SetByteValue: wrong offset %u", offset
);
834 if(uint8(m_uint32Values
[ index
] >> (offset
* 8)) != value
)
836 m_uint32Values
[ index
] &= ~uint32(uint32(0xFF) << (offset
* 8));
837 m_uint32Values
[ index
] |= uint32(uint32(value
) << (offset
* 8));
843 ObjectAccessor::Instance().AddUpdateObject(this);
844 m_objectUpdated
= true;
850 void Object::SetUInt16Value( uint16 index
, uint8 offset
, uint16 value
)
852 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, true ) );
856 sLog
.outError("Object::SetUInt16Value: wrong offset %u", offset
);
860 if(uint8(m_uint32Values
[ index
] >> (offset
* 16)) != value
)
862 m_uint32Values
[ index
] &= ~uint32(uint32(0xFFFF) << (offset
* 16));
863 m_uint32Values
[ index
] |= uint32(uint32(value
) << (offset
* 16));
869 ObjectAccessor::Instance().AddUpdateObject(this);
870 m_objectUpdated
= true;
876 void Object::SetStatFloatValue( uint16 index
, float value
)
881 SetFloatValue(index
, value
);
884 void Object::SetStatInt32Value( uint16 index
, int32 value
)
889 SetUInt32Value(index
, uint32(value
));
892 void Object::ApplyModUInt32Value(uint16 index
, int32 val
, bool apply
)
894 int32 cur
= GetUInt32Value(index
);
895 cur
+= (apply
? val
: -val
);
898 SetUInt32Value(index
,cur
);
901 void Object::ApplyModInt32Value(uint16 index
, int32 val
, bool apply
)
903 int32 cur
= GetInt32Value(index
);
904 cur
+= (apply
? val
: -val
);
905 SetInt32Value(index
,cur
);
908 void Object::ApplyModSignedFloatValue(uint16 index
, float val
, bool apply
)
910 float cur
= GetFloatValue(index
);
911 cur
+= (apply
? val
: -val
);
912 SetFloatValue(index
,cur
);
915 void Object::ApplyModPositiveFloatValue(uint16 index
, float val
, bool apply
)
917 float cur
= GetFloatValue(index
);
918 cur
+= (apply
? val
: -val
);
921 SetFloatValue(index
,cur
);
924 void Object::SetFlag( uint16 index
, uint32 newFlag
)
926 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, true ) );
927 uint32 oldval
= m_uint32Values
[ index
];
928 uint32 newval
= oldval
| newFlag
;
932 m_uint32Values
[ index
] = newval
;
938 ObjectAccessor::Instance().AddUpdateObject(this);
939 m_objectUpdated
= true;
945 void Object::RemoveFlag( uint16 index
, uint32 oldFlag
)
947 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, true ) );
948 uint32 oldval
= m_uint32Values
[ index
];
949 uint32 newval
= oldval
& ~oldFlag
;
953 m_uint32Values
[ index
] = newval
;
959 ObjectAccessor::Instance().AddUpdateObject(this);
960 m_objectUpdated
= true;
966 void Object::SetByteFlag( uint16 index
, uint8 offset
, uint8 newFlag
)
968 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, true ) );
972 sLog
.outError("Object::SetByteFlag: wrong offset %u", offset
);
976 if(!(uint8(m_uint32Values
[ index
] >> (offset
* 8)) & newFlag
))
978 m_uint32Values
[ index
] |= uint32(uint32(newFlag
) << (offset
* 8));
984 ObjectAccessor::Instance().AddUpdateObject(this);
985 m_objectUpdated
= true;
991 void Object::RemoveByteFlag( uint16 index
, uint8 offset
, uint8 oldFlag
)
993 ASSERT( index
< m_valuesCount
|| PrintIndexError( index
, true ) );
997 sLog
.outError("Object::RemoveByteFlag: wrong offset %u", offset
);
1001 if(uint8(m_uint32Values
[ index
] >> (offset
* 8)) & oldFlag
)
1003 m_uint32Values
[ index
] &= ~uint32(uint32(oldFlag
) << (offset
* 8));
1007 if(!m_objectUpdated
)
1009 ObjectAccessor::Instance().AddUpdateObject(this);
1010 m_objectUpdated
= true;
1016 bool Object::PrintIndexError(uint32 index
, bool set
) const
1018 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
);
1020 // assert must fail after function call
1024 WorldObject::WorldObject()
1025 : m_mapId(0), m_InstanceId(0), m_phaseMask(PHASEMASK_NORMAL
),
1026 m_positionX(0.0f
), m_positionY(0.0f
), m_positionZ(0.0f
), m_orientation(0.0f
),
1027 mSemaphoreTeleport(false)
1031 void WorldObject::_Create( uint32 guidlow
, HighGuid guidhigh
, uint32 mapid
)
1033 Object::_Create(guidlow
, 0, guidhigh
);
1038 uint32
WorldObject::GetZoneId() const
1040 return MapManager::Instance().GetBaseMap(m_mapId
)->GetZoneId(m_positionX
,m_positionY
,m_positionZ
);
1043 uint32
WorldObject::GetAreaId() const
1045 return MapManager::Instance().GetBaseMap(m_mapId
)->GetAreaId(m_positionX
,m_positionY
,m_positionZ
);
1048 InstanceData
* WorldObject::GetInstanceData()
1050 Map
*map
= GetMap();
1051 return map
->IsDungeon() ? ((InstanceMap
*)map
)->GetInstanceData() : NULL
;
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 bool is3D
) const
1103 if (!obj
|| !IsInMap(obj
)) return false;
1105 float dx
= GetPositionX() - obj
->GetPositionX();
1106 float dy
= GetPositionY() - obj
->GetPositionY();
1107 float distsq
= dx
*dx
+ dy
*dy
;
1110 float dz
= GetPositionZ() - obj
->GetPositionZ();
1113 float sizefactor
= GetObjectSize() + obj
->GetObjectSize();
1114 float maxdist
= dist2compare
+ sizefactor
;
1116 return distsq
< maxdist
* maxdist
;
1119 bool WorldObject::IsWithinLOSInMap(const WorldObject
* obj
) const
1121 if (!IsInMap(obj
)) return false;
1123 obj
->GetPosition(ox
,oy
,oz
);
1124 return(IsWithinLOS(ox
, oy
, oz
));
1127 bool WorldObject::IsWithinLOS(const float ox
, const float oy
, const float oz
) const
1131 VMAP::IVMapManager
*vMapManager
= VMAP::VMapFactory::createOrGetVMapManager();
1132 return vMapManager
->isInLineOfSight(GetMapId(), x
, y
, z
+2.0f
, ox
, oy
, oz
+2.0f
);
1135 float WorldObject::GetAngle(const WorldObject
* obj
) const
1138 return GetAngle( obj
->GetPositionX(), obj
->GetPositionY() );
1141 // Return angle in range 0..2*pi
1142 float WorldObject::GetAngle( const float x
, const float y
) const
1144 float dx
= x
- GetPositionX();
1145 float dy
= y
- GetPositionY();
1147 float ang
= atan2(dy
, dx
);
1148 ang
= (ang
>= 0) ? ang
: 2 * M_PI
+ ang
;
1152 bool WorldObject::HasInArc(const float arcangle
, const WorldObject
* obj
) const
1154 float arc
= arcangle
;
1156 // move arc to range 0.. 2*pi
1157 while( arc
>= 2.0f
* M_PI
)
1162 float angle
= GetAngle( obj
);
1163 angle
-= m_orientation
;
1165 // move angle to range -pi ... +pi
1166 while( angle
> M_PI
)
1167 angle
-= 2.0f
* M_PI
;
1168 while(angle
< -M_PI
)
1169 angle
+= 2.0f
* M_PI
;
1171 float lborder
= -1 * (arc
/2.0f
); // in range -pi..0
1172 float rborder
= (arc
/2.0f
); // in range 0..pi
1173 return (( angle
>= lborder
) && ( angle
<= rborder
));
1176 void WorldObject::GetRandomPoint( float x
, float y
, float z
, float distance
, float &rand_x
, float &rand_y
, float &rand_z
) const
1186 // angle to face `obj` to `this`
1187 float angle
= rand_norm()*2*M_PI
;
1188 float new_dist
= rand_norm()*distance
;
1190 rand_x
= x
+ new_dist
* cos(angle
);
1191 rand_y
= y
+ new_dist
* sin(angle
);
1194 MaNGOS::NormalizeMapCoord(rand_x
);
1195 MaNGOS::NormalizeMapCoord(rand_y
);
1196 UpdateGroundPositionZ(rand_x
,rand_y
,rand_z
); // update to LOS height if available
1199 void WorldObject::UpdateGroundPositionZ(float x
, float y
, float &z
) const
1201 float new_z
= MapManager::Instance().GetBaseMap(GetMapId())->GetHeight(x
,y
,z
,true);
1202 if(new_z
> INVALID_HEIGHT
)
1203 z
= new_z
+ 0.05f
; // just to be sure that we are not a few pixel under the surface
1206 bool WorldObject::IsPositionValid() const
1208 return MaNGOS::IsValidMapCoord(m_positionX
,m_positionY
,m_positionZ
,m_orientation
);
1211 void WorldObject::MonsterSay(const char* text
, uint32 language
, uint64 TargetGuid
)
1213 WorldPacket
data(SMSG_MESSAGECHAT
, 200);
1214 BuildMonsterChat(&data
,CHAT_MSG_MONSTER_SAY
,text
,language
,GetName(),TargetGuid
);
1215 SendMessageToSetInRange(&data
,sWorld
.getConfig(CONFIG_LISTEN_RANGE_SAY
),true);
1218 void WorldObject::MonsterYell(const char* text
, uint32 language
, uint64 TargetGuid
)
1220 WorldPacket
data(SMSG_MESSAGECHAT
, 200);
1221 BuildMonsterChat(&data
,CHAT_MSG_MONSTER_YELL
,text
,language
,GetName(),TargetGuid
);
1222 SendMessageToSetInRange(&data
,sWorld
.getConfig(CONFIG_LISTEN_RANGE_YELL
),true);
1225 void WorldObject::MonsterTextEmote(const char* text
, uint64 TargetGuid
, bool IsBossEmote
)
1227 WorldPacket
data(SMSG_MESSAGECHAT
, 200);
1228 BuildMonsterChat(&data
,IsBossEmote
? CHAT_MSG_RAID_BOSS_EMOTE
: CHAT_MSG_MONSTER_EMOTE
,text
,LANG_UNIVERSAL
,GetName(),TargetGuid
);
1229 SendMessageToSetInRange(&data
,sWorld
.getConfig(CONFIG_LISTEN_RANGE_TEXTEMOTE
),true);
1232 void WorldObject::MonsterWhisper(const char* text
, uint64 receiver
, bool IsBossWhisper
)
1234 Player
*player
= objmgr
.GetPlayer(receiver
);
1235 if(!player
|| !player
->GetSession())
1238 WorldPacket
data(SMSG_MESSAGECHAT
, 200);
1239 BuildMonsterChat(&data
,IsBossWhisper
? CHAT_MSG_RAID_BOSS_WHISPER
: CHAT_MSG_MONSTER_WHISPER
,text
,LANG_UNIVERSAL
,GetName(),receiver
);
1241 player
->GetSession()->SendPacket(&data
);
1246 class MessageChatLocaleCacheDo
1249 MessageChatLocaleCacheDo(WorldObject
const& obj
, ChatMsg msgtype
, int32 textId
, uint32 language
, uint64 targetGUID
, float dist
)
1250 : i_object(obj
), i_msgtype(msgtype
), i_textId(textId
), i_language(language
),
1251 i_targetGUID(targetGUID
), i_dist(dist
)
1255 ~MessageChatLocaleCacheDo()
1257 for(int i
= 0; i
< i_data_cache
.size(); ++i
)
1258 delete i_data_cache
[i
];
1261 void operator()(Player
* p
)
1263 // skip far away players
1264 if(p
->GetDistance(&i_object
) > i_dist
)
1267 uint32 loc_idx
= p
->GetSession()->GetSessionDbLocaleIndex();
1268 uint32 cache_idx
= loc_idx
+1;
1271 // create if not cached yet
1272 if(i_data_cache
.size() < cache_idx
+1 || !i_data_cache
[cache_idx
])
1274 if(i_data_cache
.size() < cache_idx
+1)
1275 i_data_cache
.resize(cache_idx
+1);
1277 char const* text
= objmgr
.GetMangosString(i_textId
,loc_idx
);
1279 data
= new WorldPacket(SMSG_MESSAGECHAT
, 200);
1281 // TODO: i_object.GetName() also must be localized?
1282 i_object
.BuildMonsterChat(data
,i_msgtype
,text
,i_language
,i_object
.GetNameForLocaleIdx(loc_idx
),i_targetGUID
);
1284 i_data_cache
[cache_idx
] = data
;
1287 data
= i_data_cache
[cache_idx
];
1289 p
->SendDirectMessage(data
);
1293 WorldObject
const& i_object
;
1297 uint64 i_targetGUID
;
1299 std::vector
<WorldPacket
*> i_data_cache
; // 0 = default, i => i-1 locale index
1301 } // namespace MaNGOS
1303 void WorldObject::MonsterSay(int32 textId
, uint32 language
, uint64 TargetGuid
)
1305 CellPair p
= MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY());
1308 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1311 MaNGOS::MessageChatLocaleCacheDo
say_do(*this, CHAT_MSG_MONSTER_SAY
, textId
,language
,TargetGuid
,sWorld
.getConfig(CONFIG_LISTEN_RANGE_SAY
));
1312 MaNGOS::PlayerWorker
<MaNGOS::MessageChatLocaleCacheDo
> say_worker(this,say_do
);
1313 TypeContainerVisitor
<MaNGOS::PlayerWorker
<MaNGOS::MessageChatLocaleCacheDo
>, WorldTypeMapContainer
> message(say_worker
);
1314 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1315 cell_lock
->Visit(cell_lock
, message
, *GetMap());
1318 void WorldObject::MonsterYell(int32 textId
, uint32 language
, uint64 TargetGuid
)
1320 CellPair p
= MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY());
1323 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1326 MaNGOS::MessageChatLocaleCacheDo
say_do(*this, CHAT_MSG_MONSTER_YELL
, textId
,language
,TargetGuid
,sWorld
.getConfig(CONFIG_LISTEN_RANGE_YELL
));
1327 MaNGOS::PlayerWorker
<MaNGOS::MessageChatLocaleCacheDo
> say_worker(this,say_do
);
1328 TypeContainerVisitor
<MaNGOS::PlayerWorker
<MaNGOS::MessageChatLocaleCacheDo
>, WorldTypeMapContainer
> message(say_worker
);
1329 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1330 cell_lock
->Visit(cell_lock
, message
, *GetMap());
1333 void WorldObject::MonsterTextEmote(int32 textId
, uint64 TargetGuid
, bool IsBossEmote
)
1335 CellPair p
= MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY());
1338 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1341 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
));
1342 MaNGOS::PlayerWorker
<MaNGOS::MessageChatLocaleCacheDo
> say_worker(this,say_do
);
1343 TypeContainerVisitor
<MaNGOS::PlayerWorker
<MaNGOS::MessageChatLocaleCacheDo
>, WorldTypeMapContainer
> message(say_worker
);
1344 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1345 cell_lock
->Visit(cell_lock
, message
, *GetMap());
1348 void WorldObject::MonsterWhisper(int32 textId
, uint64 receiver
, bool IsBossWhisper
)
1350 Player
*player
= objmgr
.GetPlayer(receiver
);
1351 if(!player
|| !player
->GetSession())
1354 uint32 loc_idx
= player
->GetSession()->GetSessionDbLocaleIndex();
1355 char const* text
= objmgr
.GetMangosString(textId
,loc_idx
);
1357 WorldPacket
data(SMSG_MESSAGECHAT
, 200);
1358 BuildMonsterChat(&data
,IsBossWhisper
? CHAT_MSG_RAID_BOSS_WHISPER
: CHAT_MSG_MONSTER_WHISPER
,text
,LANG_UNIVERSAL
,GetName(),receiver
);
1360 player
->GetSession()->SendPacket(&data
);
1363 void WorldObject::BuildMonsterChat(WorldPacket
*data
, uint8 msgtype
, char const* text
, uint32 language
, char const* name
, uint64 targetGuid
) const
1365 bool pre
= (msgtype
==CHAT_MSG_MONSTER_EMOTE
|| msgtype
==CHAT_MSG_RAID_BOSS_EMOTE
);
1367 *data
<< (uint8
)msgtype
;
1368 *data
<< (uint32
)language
;
1369 *data
<< (uint64
)GetGUID();
1370 *data
<< (uint32
)0; //2.1.0
1371 *data
<< (uint32
)(strlen(name
)+1);
1373 *data
<< (uint64
)targetGuid
; //Unit Target
1374 if( targetGuid
&& !IS_PLAYER_GUID(targetGuid
) )
1376 *data
<< (uint32
)1; // target name length
1377 *data
<< (uint8
)0; // target name
1379 *data
<< (uint32
)(strlen(text
)+1+(pre
?3:0));
1381 data
->append("%s ",3);
1383 *data
<< (uint8
)0; // ChatTag
1386 void WorldObject::BuildHeartBeatMsg(WorldPacket
*data
) const
1388 //Heartbeat message cannot be used for non-units
1389 if (!isType(TYPEMASK_UNIT
))
1392 data
->Initialize(MSG_MOVE_HEARTBEAT
, 32);
1393 data
->append(GetPackGUID());
1394 *data
<< uint32(((Unit
*)this)->GetUnitMovementFlags()); // movement flags
1395 *data
<< uint16(0); // 2.3.0
1396 *data
<< getMSTime(); // time
1397 *data
<< m_positionX
;
1398 *data
<< m_positionY
;
1399 *data
<< m_positionZ
;
1400 *data
<< m_orientation
;
1404 void WorldObject::BuildTeleportAckMsg(WorldPacket
*data
, float x
, float y
, float z
, float ang
) const
1406 //TeleportAck message cannot be used for non-units
1407 if (!isType(TYPEMASK_UNIT
))
1410 data
->Initialize(MSG_MOVE_TELEPORT_ACK
, 41);
1411 data
->append(GetPackGUID());
1412 *data
<< uint32(0); // this value increments every time
1413 *data
<< uint32(((Unit
*)this)->GetUnitMovementFlags()); // movement flags
1414 *data
<< uint16(0); // 2.3.0
1415 *data
<< getMSTime(); // time
1423 void WorldObject::SendMessageToSet(WorldPacket
*data
, bool /*bToSelf*/)
1425 GetMap()->MessageBroadcast(this, data
);
1428 void WorldObject::SendMessageToSetInRange(WorldPacket
*data
, float dist
, bool /*bToSelf*/)
1430 GetMap()->MessageDistBroadcast(this, data
, dist
);
1433 void WorldObject::SendObjectDeSpawnAnim(uint64 guid
)
1435 WorldPacket
data(SMSG_GAMEOBJECT_DESPAWN_ANIM
, 8);
1437 SendMessageToSet(&data
, true);
1440 Map
* WorldObject::GetMap() const
1442 return MapManager::Instance().GetMap(GetMapId(), this);
1445 Map
const* WorldObject::GetBaseMap() const
1447 return MapManager::Instance().GetBaseMap(GetMapId());
1450 void WorldObject::AddObjectToRemoveList()
1452 Map
* map
= GetMap();
1455 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());
1459 map
->AddObjectToRemoveList(this);
1462 Creature
* WorldObject::SummonCreature(uint32 id
, float x
, float y
, float z
, float ang
,TempSummonType spwtype
,uint32 despwtime
)
1464 TemporarySummon
* pCreature
= new TemporarySummon(GetGUID());
1466 pCreature
->SetInstanceId(GetInstanceId());
1468 if (GetTypeId()==TYPEID_PLAYER
)
1469 team
= ((Player
*)this)->GetTeam();
1471 if (!pCreature
->Create(objmgr
.GenerateLowGuid(HIGHGUID_UNIT
), GetMap(), id
, team
))
1477 if (x
== 0.0f
&& y
== 0.0f
&& z
== 0.0f
)
1478 GetClosePoint(x
, y
, z
, pCreature
->GetObjectSize());
1480 pCreature
->Relocate(x
, y
, z
, ang
);
1482 if(!pCreature
->IsPositionValid())
1484 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());
1489 pCreature
->Summon(spwtype
, despwtime
);
1491 if(GetTypeId()==TYPEID_UNIT
&& ((Creature
*)this)->AI())
1492 ((Creature
*)this)->AI()->JustSummoned(pCreature
);
1494 //return the creature therewith the summoner has access to it
1503 NearUsedPosDo(WorldObject
const& obj
, WorldObject
const* searcher
, float angle
, ObjectPosSelector
& selector
)
1504 : i_object(obj
), i_searcher(searcher
), i_angle(angle
), i_selector(selector
) {}
1506 void operator()(Corpse
*) const {}
1507 void operator()(DynamicObject
*) const {}
1509 void operator()(Creature
* c
) const
1511 // skip self or target
1512 if(c
==i_searcher
|| c
==&i_object
)
1517 if( !c
->isAlive() || c
->hasUnitState(UNIT_STAT_ROOT
| UNIT_STAT_STUNNED
| UNIT_STAT_DISTRACTED
) ||
1518 !c
->GetMotionMaster()->GetDestination(x
,y
,z
) )
1520 x
= c
->GetPositionX();
1521 y
= c
->GetPositionY();
1528 void operator()(T
* u
) const
1530 // skip self or target
1531 if(u
==i_searcher
|| u
==&i_object
)
1536 x
= u
->GetPositionX();
1537 y
= u
->GetPositionY();
1542 // we must add used pos that can fill places around center
1543 void add(WorldObject
* u
, float x
, float y
) const
1545 // dist include size of u
1546 float dist2d
= i_object
.GetDistance2d(x
,y
);
1548 // u is too nearest to i_object
1549 if(dist2d
+ i_object
.GetObjectSize() + u
->GetObjectSize() < i_selector
.m_dist
- i_selector
.m_size
)
1552 // u is too far away from i_object
1553 if(dist2d
+ i_object
.GetObjectSize() - u
->GetObjectSize() > i_selector
.m_dist
+ i_selector
.m_size
)
1556 float angle
= i_object
.GetAngle(u
)-i_angle
;
1558 // move angle to range -pi ... +pi
1559 while( angle
> M_PI
)
1560 angle
-= 2.0f
* M_PI
;
1561 while(angle
< -M_PI
)
1562 angle
+= 2.0f
* M_PI
;
1564 i_selector
.AddUsedPos(u
->GetObjectSize(),angle
,dist2d
+ i_object
.GetObjectSize());
1567 WorldObject
const& i_object
;
1568 WorldObject
const* i_searcher
;
1570 ObjectPosSelector
& i_selector
;
1572 } // namespace MaNGOS
1574 //===================================================================================================
1576 void WorldObject::GetNearPoint2D(float &x
, float &y
, float distance2d
, float absAngle
) const
1578 x
= GetPositionX() + (GetObjectSize() + distance2d
) * cos(absAngle
);
1579 y
= GetPositionY() + (GetObjectSize() + distance2d
) * sin(absAngle
);
1581 MaNGOS::NormalizeMapCoord(x
);
1582 MaNGOS::NormalizeMapCoord(y
);
1585 void WorldObject::GetNearPoint(WorldObject
const* searcher
, float &x
, float &y
, float &z
, float searcher_size
, float distance2d
, float absAngle
) const
1587 GetNearPoint2D(x
,y
,distance2d
+searcher_size
,absAngle
);
1590 // if detection disabled, return first point
1591 if(!sWorld
.getConfig(CONFIG_DETECT_POS_COLLISION
))
1593 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1597 // or remember first point
1600 bool first_los_conflict
= false; // first point LOS problems
1602 // prepare selector for work
1603 ObjectPosSelector
selector(GetPositionX(),GetPositionY(),GetObjectSize(),distance2d
+searcher_size
);
1605 // adding used positions around object
1607 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
1609 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1612 MaNGOS::NearUsedPosDo
u_do(*this,searcher
,absAngle
,selector
);
1613 MaNGOS::WorldObjectWorker
<MaNGOS::NearUsedPosDo
> worker(this,u_do
);
1615 TypeContainerVisitor
<MaNGOS::WorldObjectWorker
<MaNGOS::NearUsedPosDo
>, GridTypeMapContainer
> grid_obj_worker(worker
);
1616 TypeContainerVisitor
<MaNGOS::WorldObjectWorker
<MaNGOS::NearUsedPosDo
>, WorldTypeMapContainer
> world_obj_worker(worker
);
1618 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1619 cell_lock
->Visit(cell_lock
, grid_obj_worker
, *GetMap());
1620 cell_lock
->Visit(cell_lock
, world_obj_worker
, *GetMap());
1623 // maybe can just place in primary position
1624 if( selector
.CheckOriginal() )
1626 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1628 if(IsWithinLOS(x
,y
,z
))
1631 first_los_conflict
= true; // first point have LOS problems
1634 float angle
; // candidate of angle for free pos
1636 // special case when one from list empty and then empty side preferred
1637 if(selector
.FirstAngle(angle
))
1639 GetNearPoint2D(x
,y
,distance2d
,absAngle
+angle
);
1641 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1643 if(IsWithinLOS(x
,y
,z
))
1647 // set first used pos in lists
1648 selector
.InitializeAngle();
1650 // select in positions after current nodes (selection one by one)
1651 while(selector
.NextAngle(angle
)) // angle for free pos
1653 GetNearPoint2D(x
,y
,distance2d
,absAngle
+angle
);
1655 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1657 if(IsWithinLOS(x
,y
,z
))
1661 // BAD NEWS: not free pos (or used or have LOS problems)
1662 // Attempt find _used_ pos without LOS problem
1664 if(!first_los_conflict
)
1669 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1673 // special case when one from list empty and then empty side preferred
1674 if( selector
.IsNonBalanced() )
1676 if(!selector
.FirstAngle(angle
)) // _used_ pos
1678 GetNearPoint2D(x
,y
,distance2d
,absAngle
+angle
);
1680 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1682 if(IsWithinLOS(x
,y
,z
))
1687 // set first used pos in lists
1688 selector
.InitializeAngle();
1690 // select in positions after current nodes (selection one by one)
1691 while(selector
.NextUsedAngle(angle
)) // angle for used pos but maybe without LOS problem
1693 GetNearPoint2D(x
,y
,distance2d
,absAngle
+angle
);
1695 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1697 if(IsWithinLOS(x
,y
,z
))
1701 // BAD BAD NEWS: all found pos (free and used) have LOS problem :(
1705 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available