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
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
;
67 m_objectTypeId
= TYPEID_OBJECT
;
68 m_objectType
= TYPEMASK_OBJECT
;
71 m_uint32Values_mirror
= 0;
75 m_objectUpdated
= false;
78 m_PackGUID
.appendPackGUID(0);
84 ObjectAccessor::Instance().RemoveUpdateObject(this);
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());
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
);
121 m_PackGUID
.appendPackGUID(GetGUID());
124 void Object::BuildMovementUpdateBlock(UpdateData
* data
, uint32 flags
) const
128 buf
<< uint8( UPDATETYPE_MOVEMENT
);
131 _BuildMovementUpdate(&buf
, flags
, 0x00000000);
133 data
->AddUpdateBlock(buf
);
136 void Object::BuildCreateUpdateBlockForPlayer(UpdateData
*data
, Player
*target
) const
143 uint8 updatetype
= UPDATETYPE_CREATE_OBJECT
;
144 uint8 flags
= m_updateFlag
;
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;
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
;
178 case GAMEOBJECT_TYPE_TRANSPORT
:
179 flags
|= UPDATEFLAG_TRANSPORT
;
185 //sLog.outDebug("BuildCreateUpdate: update-type: %u, object-type: %u got flags: %X, flags2: %X", updatetype, m_objectTypeId, flags, flags2);
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
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
229 buf
<< (uint8
) UPDATETYPE_VALUES
;
230 //buf.append(GetPackGUID()); //client crashes when using this. but not have crash in debug mode
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
252 WorldPacket
data(SMSG_DESTROY_OBJECT
, 8);
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
265 if (flags
& UPDATEFLAG_LIVING
)
271 flags2
= ((Unit
*)this)->GetUnitMovementFlags();
276 flags2
= ((Player
*)this)->GetUnitMovementFlags();
278 if(((Player
*)this)->GetTransport())
279 flags2
|= MOVEMENTFLAG_ONTRANSPORT
;
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
);
295 *data
<< uint32(flags2
); // movement flags
296 *data
<< uint16(unk_flags
); // unknown 2.3.0
297 *data
<< uint32(getMSTime()); // time (in milliseconds)
301 if (flags
& UPDATEFLAG_HASPOSITION
)
304 if(flags
& UPDATEFLAG_TRANSPORT
&& ((GameObject
*)this)->GetGoType() == GAMEOBJECT_TYPE_MO_TRANSPORT
)
309 *data
<< ((WorldObject
*)this)->GetOrientation();
313 *data
<< ((WorldObject
*)this)->GetPositionX();
314 *data
<< ((WorldObject
*)this)->GetPositionY();
315 *data
<< ((WorldObject
*)this)->GetPositionZ();
316 *data
<< ((WorldObject
*)this)->GetOrientation();
321 if(flags
& UPDATEFLAG_LIVING
)
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
340 if((flags2
& (MOVEMENTFLAG_SWIMMING
| MOVEMENTFLAG_FLYING2
)) || (unk_flags
& 0x20))
342 if(GetTypeId() == TYPEID_PLAYER
)
343 *data
<< (float)((Player
*)this)->m_movementInfo
.s_pitch
;
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
;
351 *data
<< (uint32
)0; // last fall time
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
;
373 if(flags2
& MOVEMENTFLAG_SPLINE
)
375 if(GetTypeId() == TYPEID_PLAYER
)
376 *data
<< (float)((Player
*)this)->m_movementInfo
.u_unk1
;
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
);
392 if(flags2
& MOVEMENTFLAG_SPLINE2
)
394 if(GetTypeId() != TYPEID_PLAYER
)
396 sLog
.outDebug("_BuildMovementUpdate: MOVEMENTFLAG_SPLINE2 for non-player");
400 if(!((Player
*)this)->isInFlight())
402 sLog
.outDebug("_BuildMovementUpdate: MOVEMENTFLAG_SPLINE2 but not in flight");
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
421 if(flags3
& 0x20000) // probably guid there
426 if(flags3
& 0x40000) // may be orientation
431 Path
&path
= fmg
->GetPath();
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++)
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();*/
474 if(flags
& UPDATEFLAG_LOWGUID
)
480 case TYPEID_CONTAINER
:
481 case TYPEID_GAMEOBJECT
:
482 case TYPEID_DYNAMICOBJECT
:
484 *data
<< uint32(GetGUIDLow()); // GetGUIDLow()
487 *data
<< uint32(0x0000000B); // unk, can be 0xB or 0xC
490 if(flags
& UPDATEFLAG_SELF
)
491 *data
<< uint32(0x0000002F); // unk, can be 0x15 or 0x22
493 *data
<< uint32(0x00000008); // unk, can be 0x7 or 0x8
496 *data
<< uint32(0x00000000); // unk
502 if(flags
& UPDATEFLAG_HIGHGUID
)
508 case TYPEID_CONTAINER
:
509 case TYPEID_GAMEOBJECT
:
510 case TYPEID_DYNAMICOBJECT
:
512 *data
<< uint32(GetGUIDHigh()); // GetGUIDHigh()
518 if(flags
& UPDATEFLAG_SELF
)
519 *data
<< uint32(0x0000002F); // unk, can be 0x15 or 0x22
521 *data
<< uint32(-1); // unk, can be 0x7 or 0x8
524 *data
<< uint32(0x00000000); // unk
530 if(flags
& UPDATEFLAG_FULLGUID
)
532 *data
<< uint8(0); // packed guid (probably target guid)
536 if(flags
& UPDATEFLAG_TRANSPORT
)
538 *data
<< uint32(getMSTime()); // ms time
542 if(flags
& UPDATEFLAG_WOTLK
) // unused for now
549 void Object::_BuildValuesUpdate(uint8 updatetype
, ByteBuffer
* data
, UpdateMask
*updateMask
, Player
*target
) const
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
);
619 *data
<< (m_uint32Values
[ index
] & ~UNIT_DYNFLAG_OTHER_TAGGER
);
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
645 case GAMEOBJECT_TYPE_GOOBER
:
649 *data
<< uint32(0); //unknown. not happen.
654 *data
<< uint32(0); // disable quest object
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
];
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
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
)
712 Tokens::iterator iter
;
714 for (iter
= tokens
.begin(), index
= 0; index
< m_valuesCount
; ++iter
, ++index
)
716 m_uint32Values
[index
] = atol((*iter
).c_str());
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
;
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
;
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);
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
;
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 ) );
822 sLog
.outError("Object::SetByteValue: wrong offset %u", offset
);
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));
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 ) );
848 sLog
.outError("Object::SetUInt16Value: wrong offset %u", offset
);
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));
861 ObjectAccessor::Instance().AddUpdateObject(this);
862 m_objectUpdated
= true;
868 void Object::SetStatFloatValue( uint16 index
, float value
)
873 SetFloatValue(index
, value
);
876 void Object::SetStatInt32Value( uint16 index
, int32 value
)
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
);
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
);
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
;
924 m_uint32Values
[ index
] = newval
;
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
;
945 m_uint32Values
[ index
] = newval
;
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 ) );
964 sLog
.outError("Object::SetByteFlag: wrong offset %u", offset
);
968 if(!(uint8(m_uint32Values
[ index
] >> (offset
* 8)) & newFlag
))
970 m_uint32Values
[ index
] |= uint32(uint32(newFlag
) << (offset
* 8));
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 ) );
989 sLog
.outError("Object::RemoveByteFlag: wrong offset %u", offset
);
993 if(uint8(m_uint32Values
[ index
] >> (offset
* 8)) & oldFlag
)
995 m_uint32Values
[ index
] &= ~uint32(uint32(oldFlag
) << (offset
* 8));
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
1016 WorldObject::WorldObject()
1021 m_orientation
= 0.0f
;
1028 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
);
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
;
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;
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
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
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
;
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
)
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
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
);
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())
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
);
1242 class MessageChatLocaleCacheDo
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
)
1263 uint32 loc_idx
= p
->GetSession()->GetSessionDbLocaleIndex();
1264 uint32 cache_idx
= loc_idx
+1;
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
;
1283 data
= i_data_cache
[cache_idx
];
1285 p
->SendDirectMessage(data
);
1289 WorldObject
const& i_object
;
1293 uint64 i_targetGUID
;
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());
1304 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
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());
1319 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
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());
1334 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
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())
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);
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));
1377 data
->append("%s ",3);
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
))
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
;
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
))
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
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);
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();
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());
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());
1464 if (GetTypeId()==TYPEID_PLAYER
)
1465 team
= ((Player
*)this)->GetTeam();
1467 if (!pCreature
->Create(objmgr
.GenerateLowGuid(HIGHGUID_UNIT
), GetMap(), id
, team
))
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());
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
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
)
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();
1524 void operator()(T
* u
) const
1526 // skip self or target
1527 if(u
==i_searcher
|| u
==&i_object
)
1532 x
= u
->GetPositionX();
1533 y
= u
->GetPositionY();
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
)
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
)
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());
1563 WorldObject
const& i_object
;
1564 WorldObject
const* i_searcher
;
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
);
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
1593 // or remember first point
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()));
1605 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
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
))
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
);
1637 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1639 if(IsWithinLOS(x
,y
,z
))
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
);
1651 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1653 if(IsWithinLOS(x
,y
,z
))
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
)
1665 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
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
);
1676 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1678 if(IsWithinLOS(x
,y
,z
))
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
);
1691 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1693 if(IsWithinLOS(x
,y
,z
))
1697 // BAD BAD NEWS: all found pos (free and used) have LOS problem :(
1701 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available