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 target
->GetSession()->SendPacket( &data
);
257 void Object::_BuildMovementUpdate(ByteBuffer
* data
, uint8 flags
, uint32 flags2
) const
259 *data
<< (uint8
)flags
; // update flags
262 if (flags
& UPDATEFLAG_LIVING
)
268 flags2
= ((Unit
*)this)->GetUnitMovementFlags();
273 flags2
= ((Player
*)this)->GetUnitMovementFlags();
275 if(((Player
*)this)->GetTransport())
276 flags2
|= MOVEMENTFLAG_ONTRANSPORT
;
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
);
292 *data
<< uint32(flags2
); // movement flags
293 *data
<< uint8(0); // unk 2.3.0
294 *data
<< uint32(getMSTime()); // time (in milliseconds)
298 if (flags
& UPDATEFLAG_HASPOSITION
)
301 if(flags
& UPDATEFLAG_TRANSPORT
&& ((GameObject
*)this)->GetGoType() == GAMEOBJECT_TYPE_MO_TRANSPORT
)
306 *data
<< ((WorldObject
*)this)->GetOrientation();
310 *data
<< ((WorldObject
*)this)->GetPositionX();
311 *data
<< ((WorldObject
*)this)->GetPositionY();
312 *data
<< ((WorldObject
*)this)->GetPositionZ();
313 *data
<< ((WorldObject
*)this)->GetOrientation();
318 if(flags
& UPDATEFLAG_LIVING
)
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
336 if(flags2
& (MOVEMENTFLAG_SWIMMING
| MOVEMENTFLAG_FLYING2
))
338 if(GetTypeId() == TYPEID_PLAYER
)
339 *data
<< (float)((Player
*)this)->m_movementInfo
.s_pitch
;
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
;
347 *data
<< (uint32
)0; // last fall time
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
;
369 if(flags2
& MOVEMENTFLAG_SPLINE
)
371 if(GetTypeId() == TYPEID_PLAYER
)
372 *data
<< (float)((Player
*)this)->m_movementInfo
.u_unk1
;
377 *data
<< ((Unit
*)this)->GetSpeed( MOVE_WALK
);
378 *data
<< ((Unit
*)this)->GetSpeed( MOVE_RUN
);
379 *data
<< ((Unit
*)this)->GetSpeed( MOVE_SWIMBACK
);
380 *data
<< ((Unit
*)this)->GetSpeed( MOVE_SWIM
);
381 *data
<< ((Unit
*)this)->GetSpeed( MOVE_WALKBACK
);
382 *data
<< ((Unit
*)this)->GetSpeed( MOVE_FLY
);
383 *data
<< ((Unit
*)this)->GetSpeed( MOVE_FLYBACK
);
384 *data
<< ((Unit
*)this)->GetSpeed( MOVE_TURN
);
387 if(flags2
& MOVEMENTFLAG_SPLINE2
)
389 if(GetTypeId() != TYPEID_PLAYER
)
391 sLog
.outDebug("_BuildMovementUpdate: MOVEMENTFLAG_SPLINE2 for non-player");
395 if(!((Player
*)this)->isInFlight())
397 sLog
.outDebug("_BuildMovementUpdate: MOVEMENTFLAG_SPLINE2 but not in flight");
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
416 if(flags3
& 0x20000) // probably guid there
421 if(flags3
& 0x40000) // may be orientation
426 Path
&path
= fmg
->GetPath();
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++)
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();*/
469 if(flags
& UPDATEFLAG_LOWGUID
)
475 case TYPEID_CONTAINER
:
476 case TYPEID_GAMEOBJECT
:
477 case TYPEID_DYNAMICOBJECT
:
479 *data
<< uint32(GetGUIDLow()); // GetGUIDLow()
482 *data
<< uint32(0x0000000B); // unk, can be 0xB or 0xC
485 if(flags
& UPDATEFLAG_SELF
)
486 *data
<< uint32(0x00000015); // unk, can be 0x15 or 0x22
488 *data
<< uint32(0x00000008); // unk, can be 0x7 or 0x8
491 *data
<< uint32(0x00000000); // unk
497 if(flags
& UPDATEFLAG_HIGHGUID
)
503 case TYPEID_CONTAINER
:
504 case TYPEID_GAMEOBJECT
:
505 case TYPEID_DYNAMICOBJECT
:
507 *data
<< uint32(GetGUIDHigh()); // GetGUIDHigh()
510 *data
<< uint32(0x00000000); // unk
516 if(flags
& UPDATEFLAG_FULLGUID
)
518 *data
<< uint8(0); // packed guid (probably target guid)
522 if(flags
& UPDATEFLAG_TRANSPORT
)
524 *data
<< uint32(getMSTime()); // ms time
528 void Object::_BuildValuesUpdate(uint8 updatetype
, ByteBuffer
* data
, UpdateMask
*updateMask
, Player
*target
) const
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
);
598 *data
<< (m_uint32Values
[ index
] & ~UNIT_DYNFLAG_OTHER_TAGGER
);
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
624 case GAMEOBJECT_TYPE_GOOBER
:
628 *data
<< uint32(0); //unknown. not happen.
633 *data
<< uint32(0); // disable quest object
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
];
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
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
)
691 Tokens::iterator iter
;
693 for (iter
= tokens
.begin(), index
= 0; index
< m_valuesCount
; ++iter
, ++index
)
695 m_uint32Values
[index
] = atol((*iter
).c_str());
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
;
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
;
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);
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
;
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 ) );
801 sLog
.outError("Object::SetByteValue: wrong offset %u", offset
);
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));
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 ) );
827 sLog
.outError("Object::SetUInt16Value: wrong offset %u", offset
);
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));
840 ObjectAccessor::Instance().AddUpdateObject(this);
841 m_objectUpdated
= true;
847 void Object::SetStatFloatValue( uint16 index
, float value
)
852 SetFloatValue(index
, value
);
855 void Object::SetStatInt32Value( uint16 index
, int32 value
)
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
);
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
);
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
;
903 m_uint32Values
[ index
] = newval
;
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
;
924 m_uint32Values
[ index
] = newval
;
930 ObjectAccessor::Instance().AddUpdateObject(this);
931 m_objectUpdated
= true;
937 bool Object::PrintIndexError(uint32 index
, bool set
) const
939 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
);
941 // assert must fail after function call
945 WorldObject::WorldObject()
950 m_orientation
= 0.0f
;
957 mSemaphoreTeleport
= false;
960 void WorldObject::_Create( uint32 guidlow
, HighGuid guidhigh
, uint32 mapid
)
962 Object::_Create(guidlow
, 0, guidhigh
);
967 uint32
WorldObject::GetZoneId() const
969 return MapManager::Instance().GetBaseMap(m_mapId
)->GetZoneId(m_positionX
,m_positionY
);
972 uint32
WorldObject::GetAreaId() const
974 return MapManager::Instance().GetBaseMap(m_mapId
)->GetAreaId(m_positionX
,m_positionY
);
977 InstanceData
* WorldObject::GetInstanceData()
980 return map
->IsDungeon() ? ((InstanceMap
*)map
)->GetInstanceData() : NULL
;
984 float WorldObject::GetDistance(const WorldObject
* obj
) const
986 float dx
= GetPositionX() - obj
->GetPositionX();
987 float dy
= GetPositionY() - obj
->GetPositionY();
988 float dz
= GetPositionZ() - obj
->GetPositionZ();
989 float sizefactor
= GetObjectSize() + obj
->GetObjectSize();
990 float dist
= sqrt((dx
*dx
) + (dy
*dy
) + (dz
*dz
)) - sizefactor
;
991 return ( dist
> 0 ? dist
: 0);
994 float WorldObject::GetDistance2d(float x
, float y
) const
996 float dx
= GetPositionX() - x
;
997 float dy
= GetPositionY() - y
;
998 float sizefactor
= GetObjectSize();
999 float dist
= sqrt((dx
*dx
) + (dy
*dy
)) - sizefactor
;
1000 return ( dist
> 0 ? dist
: 0);
1003 float WorldObject::GetDistance(const float x
, const float y
, const float z
) const
1005 float dx
= GetPositionX() - x
;
1006 float dy
= GetPositionY() - y
;
1007 float dz
= GetPositionZ() - z
;
1008 float sizefactor
= GetObjectSize();
1009 float dist
= sqrt((dx
*dx
) + (dy
*dy
) + (dz
*dz
)) - sizefactor
;
1010 return ( dist
> 0 ? dist
: 0);
1013 float WorldObject::GetDistance2d(const WorldObject
* obj
) const
1015 float dx
= GetPositionX() - obj
->GetPositionX();
1016 float dy
= GetPositionY() - obj
->GetPositionY();
1017 float sizefactor
= GetObjectSize() + obj
->GetObjectSize();
1018 float dist
= sqrt((dx
*dx
) + (dy
*dy
)) - sizefactor
;
1019 return ( dist
> 0 ? dist
: 0);
1022 float WorldObject::GetDistanceZ(const WorldObject
* obj
) const
1024 float dz
= fabs(GetPositionZ() - obj
->GetPositionZ());
1025 float sizefactor
= GetObjectSize() + obj
->GetObjectSize();
1026 float dist
= dz
- sizefactor
;
1027 return ( dist
> 0 ? dist
: 0);
1030 bool WorldObject::IsWithinDistInMap(const WorldObject
* obj
, const float dist2compare
) const
1032 if (!obj
|| !IsInMap(obj
)) return false;
1034 float dx
= GetPositionX() - obj
->GetPositionX();
1035 float dy
= GetPositionY() - obj
->GetPositionY();
1036 float dz
= GetPositionZ() - obj
->GetPositionZ();
1037 float distsq
= dx
*dx
+ dy
*dy
+ dz
*dz
;
1038 float sizefactor
= GetObjectSize() + obj
->GetObjectSize();
1039 float maxdist
= dist2compare
+ sizefactor
;
1041 return distsq
< maxdist
* maxdist
;
1044 bool WorldObject::IsWithinLOSInMap(const WorldObject
* obj
) const
1046 if (!IsInMap(obj
)) return false;
1048 obj
->GetPosition(ox
,oy
,oz
);
1049 return(IsWithinLOS(ox
, oy
, oz
));
1052 bool WorldObject::IsWithinLOS(const float ox
, const float oy
, const float oz
) const
1056 VMAP::IVMapManager
*vMapManager
= VMAP::VMapFactory::createOrGetVMapManager();
1057 return vMapManager
->isInLineOfSight(GetMapId(), x
, y
, z
+2.0f
, ox
, oy
, oz
+2.0f
);
1060 float WorldObject::GetAngle(const WorldObject
* obj
) const
1063 return GetAngle( obj
->GetPositionX(), obj
->GetPositionY() );
1066 // Return angle in range 0..2*pi
1067 float WorldObject::GetAngle( const float x
, const float y
) const
1069 float dx
= x
- GetPositionX();
1070 float dy
= y
- GetPositionY();
1072 float ang
= atan2(dy
, dx
);
1073 ang
= (ang
>= 0) ? ang
: 2 * M_PI
+ ang
;
1077 bool WorldObject::HasInArc(const float arcangle
, const WorldObject
* obj
) const
1079 float arc
= arcangle
;
1081 // move arc to range 0.. 2*pi
1082 while( arc
>= 2.0f
* M_PI
)
1087 float angle
= GetAngle( obj
);
1088 angle
-= m_orientation
;
1090 // move angle to range -pi ... +pi
1091 while( angle
> M_PI
)
1092 angle
-= 2.0f
* M_PI
;
1093 while(angle
< -M_PI
)
1094 angle
+= 2.0f
* M_PI
;
1096 float lborder
= -1 * (arc
/2.0f
); // in range -pi..0
1097 float rborder
= (arc
/2.0f
); // in range 0..pi
1098 return (( angle
>= lborder
) && ( angle
<= rborder
));
1101 void WorldObject::GetRandomPoint( float x
, float y
, float z
, float distance
, float &rand_x
, float &rand_y
, float &rand_z
) const
1111 // angle to face `obj` to `this`
1112 float angle
= rand_norm()*2*M_PI
;
1113 float new_dist
= rand_norm()*distance
;
1115 rand_x
= x
+ new_dist
* cos(angle
);
1116 rand_y
= y
+ new_dist
* sin(angle
);
1119 MaNGOS::NormalizeMapCoord(rand_x
);
1120 MaNGOS::NormalizeMapCoord(rand_y
);
1121 UpdateGroundPositionZ(rand_x
,rand_y
,rand_z
); // update to LOS height if available
1124 void WorldObject::UpdateGroundPositionZ(float x
, float y
, float &z
) const
1126 float new_z
= MapManager::Instance().GetBaseMap(GetMapId())->GetHeight(x
,y
,z
,true);
1127 if(new_z
> INVALID_HEIGHT
)
1128 z
= new_z
+ 0.05f
; // just to be sure that we are not a few pixel under the surface
1131 bool WorldObject::IsPositionValid() const
1133 return MaNGOS::IsValidMapCoord(m_positionX
,m_positionY
,m_positionZ
,m_orientation
);
1136 void WorldObject::MonsterSay(const char* text
, uint32 language
, uint64 TargetGuid
)
1138 WorldPacket
data(SMSG_MESSAGECHAT
, 200);
1139 BuildMonsterChat(&data
,CHAT_MSG_MONSTER_SAY
,text
,language
,GetName(),TargetGuid
);
1140 SendMessageToSetInRange(&data
,sWorld
.getConfig(CONFIG_LISTEN_RANGE_SAY
),true);
1143 void WorldObject::MonsterYell(const char* text
, uint32 language
, uint64 TargetGuid
)
1145 WorldPacket
data(SMSG_MESSAGECHAT
, 200);
1146 BuildMonsterChat(&data
,CHAT_MSG_MONSTER_YELL
,text
,language
,GetName(),TargetGuid
);
1147 SendMessageToSetInRange(&data
,sWorld
.getConfig(CONFIG_LISTEN_RANGE_YELL
),true);
1150 void WorldObject::MonsterTextEmote(const char* text
, uint64 TargetGuid
, bool IsBossEmote
)
1152 WorldPacket
data(SMSG_MESSAGECHAT
, 200);
1153 BuildMonsterChat(&data
,IsBossEmote
? CHAT_MSG_RAID_BOSS_EMOTE
: CHAT_MSG_MONSTER_EMOTE
,text
,LANG_UNIVERSAL
,GetName(),TargetGuid
);
1154 SendMessageToSetInRange(&data
,sWorld
.getConfig(CONFIG_LISTEN_RANGE_TEXTEMOTE
),true);
1157 void WorldObject::MonsterWhisper(const char* text
, uint64 receiver
, bool IsBossWhisper
)
1159 Player
*player
= objmgr
.GetPlayer(receiver
);
1160 if(!player
|| !player
->GetSession())
1163 WorldPacket
data(SMSG_MESSAGECHAT
, 200);
1164 BuildMonsterChat(&data
,IsBossWhisper
? CHAT_MSG_RAID_BOSS_WHISPER
: CHAT_MSG_MONSTER_WHISPER
,text
,LANG_UNIVERSAL
,GetName(),receiver
);
1166 player
->GetSession()->SendPacket(&data
);
1171 class MessageChatLocaleCacheDo
1174 MessageChatLocaleCacheDo(WorldObject
const& obj
, ChatMsg msgtype
, int32 textId
, uint32 language
, uint64 targetGUID
, float dist
)
1175 : i_object(obj
), i_msgtype(msgtype
), i_textId(textId
), i_language(language
),
1176 i_targetGUID(targetGUID
), i_dist(dist
)
1180 ~MessageChatLocaleCacheDo()
1182 for(int i
= 0; i
< i_data_cache
.size(); ++i
)
1183 delete i_data_cache
[i
];
1186 void operator()(Player
* p
)
1188 // skip far away players
1189 if(p
->GetDistance(&i_object
) > i_dist
)
1192 uint32 loc_idx
= p
->GetSession()->GetSessionDbLocaleIndex();
1193 uint32 cache_idx
= loc_idx
+1;
1196 // create if not cached yet
1197 if(i_data_cache
.size() < cache_idx
+1 || !i_data_cache
[cache_idx
])
1199 if(i_data_cache
.size() < cache_idx
+1)
1200 i_data_cache
.resize(cache_idx
+1);
1202 char const* text
= objmgr
.GetMangosString(i_textId
,loc_idx
);
1204 data
= new WorldPacket(SMSG_MESSAGECHAT
, 200);
1206 // TODO: i_object.GetName() also must be localized?
1207 i_object
.BuildMonsterChat(data
,i_msgtype
,text
,i_language
,i_object
.GetNameForLocaleIdx(loc_idx
),i_targetGUID
);
1209 i_data_cache
[cache_idx
] = data
;
1212 data
= i_data_cache
[cache_idx
];
1214 p
->SendDirectMessage(data
);
1218 WorldObject
const& i_object
;
1222 uint64 i_targetGUID
;
1224 std::vector
<WorldPacket
*> i_data_cache
; // 0 = default, i => i-1 locale index
1226 } // namespace MaNGOS
1228 void WorldObject::MonsterSay(int32 textId
, uint32 language
, uint64 TargetGuid
)
1230 CellPair p
= MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY());
1233 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1236 MaNGOS::MessageChatLocaleCacheDo
say_do(*this, CHAT_MSG_MONSTER_SAY
, textId
,language
,TargetGuid
,sWorld
.getConfig(CONFIG_LISTEN_RANGE_SAY
));
1237 MaNGOS::PlayerWorker
<MaNGOS::MessageChatLocaleCacheDo
> say_worker(say_do
);
1238 TypeContainerVisitor
<MaNGOS::PlayerWorker
<MaNGOS::MessageChatLocaleCacheDo
>, WorldTypeMapContainer
> message(say_worker
);
1239 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1240 cell_lock
->Visit(cell_lock
, message
, *GetMap());
1243 void WorldObject::MonsterYell(int32 textId
, uint32 language
, uint64 TargetGuid
)
1245 CellPair p
= MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY());
1248 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1251 MaNGOS::MessageChatLocaleCacheDo
say_do(*this, CHAT_MSG_MONSTER_YELL
, textId
,language
,TargetGuid
,sWorld
.getConfig(CONFIG_LISTEN_RANGE_YELL
));
1252 MaNGOS::PlayerWorker
<MaNGOS::MessageChatLocaleCacheDo
> say_worker(say_do
);
1253 TypeContainerVisitor
<MaNGOS::PlayerWorker
<MaNGOS::MessageChatLocaleCacheDo
>, WorldTypeMapContainer
> message(say_worker
);
1254 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1255 cell_lock
->Visit(cell_lock
, message
, *GetMap());
1258 void WorldObject::MonsterTextEmote(int32 textId
, uint64 TargetGuid
, bool IsBossEmote
)
1260 CellPair p
= MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY());
1263 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1266 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
));
1267 MaNGOS::PlayerWorker
<MaNGOS::MessageChatLocaleCacheDo
> say_worker(say_do
);
1268 TypeContainerVisitor
<MaNGOS::PlayerWorker
<MaNGOS::MessageChatLocaleCacheDo
>, WorldTypeMapContainer
> message(say_worker
);
1269 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1270 cell_lock
->Visit(cell_lock
, message
, *GetMap());
1273 void WorldObject::MonsterWhisper(int32 textId
, uint64 receiver
, bool IsBossWhisper
)
1275 Player
*player
= objmgr
.GetPlayer(receiver
);
1276 if(!player
|| !player
->GetSession())
1279 uint32 loc_idx
= player
->GetSession()->GetSessionDbLocaleIndex();
1280 char const* text
= objmgr
.GetMangosString(textId
,loc_idx
);
1282 WorldPacket
data(SMSG_MESSAGECHAT
, 200);
1283 BuildMonsterChat(&data
,IsBossWhisper
? CHAT_MSG_RAID_BOSS_WHISPER
: CHAT_MSG_MONSTER_WHISPER
,text
,LANG_UNIVERSAL
,GetName(),receiver
);
1285 player
->GetSession()->SendPacket(&data
);
1288 void WorldObject::BuildMonsterChat(WorldPacket
*data
, uint8 msgtype
, char const* text
, uint32 language
, char const* name
, uint64 targetGuid
) const
1290 bool pre
= (msgtype
==CHAT_MSG_MONSTER_EMOTE
|| msgtype
==CHAT_MSG_RAID_BOSS_EMOTE
);
1292 *data
<< (uint8
)msgtype
;
1293 *data
<< (uint32
)language
;
1294 *data
<< (uint64
)GetGUID();
1295 *data
<< (uint32
)0; //2.1.0
1296 *data
<< (uint32
)(strlen(name
)+1);
1298 *data
<< (uint64
)targetGuid
; //Unit Target
1299 if( targetGuid
&& !IS_PLAYER_GUID(targetGuid
) )
1301 *data
<< (uint32
)1; // target name length
1302 *data
<< (uint8
)0; // target name
1304 *data
<< (uint32
)(strlen(text
)+1+(pre
?3:0));
1306 data
->append("%s ",3);
1308 *data
<< (uint8
)0; // ChatTag
1311 void WorldObject::BuildHeartBeatMsg(WorldPacket
*data
) const
1313 //Heartbeat message cannot be used for non-units
1314 if (!isType(TYPEMASK_UNIT
))
1317 data
->Initialize(MSG_MOVE_HEARTBEAT
, 32);
1318 data
->append(GetPackGUID());
1319 *data
<< uint32(((Unit
*)this)->GetUnitMovementFlags()); // movement flags
1320 *data
<< uint8(0); // 2.3.0
1321 *data
<< getMSTime(); // time
1322 *data
<< m_positionX
;
1323 *data
<< m_positionY
;
1324 *data
<< m_positionZ
;
1325 *data
<< m_orientation
;
1329 void WorldObject::BuildTeleportAckMsg(WorldPacket
*data
, float x
, float y
, float z
, float ang
) const
1331 //TeleportAck message cannot be used for non-units
1332 if (!isType(TYPEMASK_UNIT
))
1335 data
->Initialize(MSG_MOVE_TELEPORT_ACK
, 41);
1336 data
->append(GetPackGUID());
1337 *data
<< uint32(0); // this value increments every time
1338 *data
<< uint32(((Unit
*)this)->GetUnitMovementFlags()); // movement flags
1339 *data
<< uint8(0); // 2.3.0
1340 *data
<< getMSTime(); // time
1348 void WorldObject::SendMessageToSet(WorldPacket
*data
, bool /*bToSelf*/)
1350 GetMap()->MessageBroadcast(this, data
);
1353 void WorldObject::SendMessageToSetInRange(WorldPacket
*data
, float dist
, bool /*bToSelf*/)
1355 GetMap()->MessageDistBroadcast(this, data
, dist
);
1358 void WorldObject::SendObjectDeSpawnAnim(uint64 guid
)
1360 WorldPacket
data(SMSG_GAMEOBJECT_DESPAWN_ANIM
, 8);
1362 SendMessageToSet(&data
, true);
1365 Map
* WorldObject::GetMap() const
1367 return MapManager::Instance().GetMap(GetMapId(), this);
1370 Map
const* WorldObject::GetBaseMap() const
1372 return MapManager::Instance().GetBaseMap(GetMapId());
1375 void WorldObject::AddObjectToRemoveList()
1377 Map
* map
= GetMap();
1380 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());
1384 map
->AddObjectToRemoveList(this);
1387 Creature
* WorldObject::SummonCreature(uint32 id
, float x
, float y
, float z
, float ang
,TempSummonType spwtype
,uint32 despwtime
)
1389 TemporarySummon
* pCreature
= new TemporarySummon(GetGUID());
1391 pCreature
->SetInstanceId(GetInstanceId());
1393 if (GetTypeId()==TYPEID_PLAYER
)
1394 team
= ((Player
*)this)->GetTeam();
1396 if (!pCreature
->Create(objmgr
.GenerateLowGuid(HIGHGUID_UNIT
), GetMap(), id
, team
))
1402 if (x
== 0.0f
&& y
== 0.0f
&& z
== 0.0f
)
1403 GetClosePoint(x
, y
, z
, pCreature
->GetObjectSize());
1405 pCreature
->Relocate(x
, y
, z
, ang
);
1407 if(!pCreature
->IsPositionValid())
1409 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());
1414 pCreature
->Summon(spwtype
, despwtime
);
1416 if(GetTypeId()==TYPEID_UNIT
&& ((Creature
*)this)->AI())
1417 ((Creature
*)this)->AI()->JustSummoned(pCreature
);
1419 //return the creature therewith the summoner has access to it
1428 NearUsedPosDo(WorldObject
const& obj
, WorldObject
const* searcher
, float angle
, ObjectPosSelector
& selector
)
1429 : i_object(obj
), i_searcher(searcher
), i_angle(angle
), i_selector(selector
) {}
1431 void operator()(Corpse
*) const {}
1432 void operator()(DynamicObject
*) const {}
1434 void operator()(Creature
* c
) const
1436 // skip self or target
1437 if(c
==i_searcher
|| c
==&i_object
)
1442 if( !c
->isAlive() || c
->hasUnitState(UNIT_STAT_ROOT
| UNIT_STAT_STUNNED
| UNIT_STAT_DISTRACTED
) ||
1443 !c
->GetMotionMaster()->GetDestination(x
,y
,z
) )
1445 x
= c
->GetPositionX();
1446 y
= c
->GetPositionY();
1453 void operator()(T
* u
) const
1455 // skip self or target
1456 if(u
==i_searcher
|| u
==&i_object
)
1461 x
= u
->GetPositionX();
1462 y
= u
->GetPositionY();
1467 // we must add used pos that can fill places around center
1468 void add(WorldObject
* u
, float x
, float y
) const
1470 // dist include size of u
1471 float dist2d
= i_object
.GetDistance2d(x
,y
);
1473 // u is too nearest to i_object
1474 if(dist2d
+ i_object
.GetObjectSize() + u
->GetObjectSize() < i_selector
.m_dist
- i_selector
.m_size
)
1477 // u is too far away from i_object
1478 if(dist2d
+ i_object
.GetObjectSize() - u
->GetObjectSize() > i_selector
.m_dist
+ i_selector
.m_size
)
1481 float angle
= i_object
.GetAngle(u
)-i_angle
;
1483 // move angle to range -pi ... +pi
1484 while( angle
> M_PI
)
1485 angle
-= 2.0f
* M_PI
;
1486 while(angle
< -M_PI
)
1487 angle
+= 2.0f
* M_PI
;
1489 i_selector
.AddUsedPos(u
->GetObjectSize(),angle
,dist2d
+ i_object
.GetObjectSize());
1492 WorldObject
const& i_object
;
1493 WorldObject
const* i_searcher
;
1495 ObjectPosSelector
& i_selector
;
1497 } // namespace MaNGOS
1499 //===================================================================================================
1501 void WorldObject::GetNearPoint2D(float &x
, float &y
, float distance2d
, float absAngle
) const
1503 x
= GetPositionX() + (GetObjectSize() + distance2d
) * cos(absAngle
);
1504 y
= GetPositionY() + (GetObjectSize() + distance2d
) * sin(absAngle
);
1506 MaNGOS::NormalizeMapCoord(x
);
1507 MaNGOS::NormalizeMapCoord(y
);
1510 void WorldObject::GetNearPoint(WorldObject
const* searcher
, float &x
, float &y
, float &z
, float searcher_size
, float distance2d
, float absAngle
) const
1512 GetNearPoint2D(x
,y
,distance2d
+searcher_size
,absAngle
);
1515 // if detection disabled, return first point
1516 if(!sWorld
.getConfig(CONFIG_DETECT_POS_COLLISION
))
1518 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1522 // or remember first point
1525 bool first_los_conflict
= false; // first point LOS problems
1527 // prepare selector for work
1528 ObjectPosSelector
selector(GetPositionX(),GetPositionY(),GetObjectSize(),distance2d
+searcher_size
);
1530 // adding used positions around object
1532 CellPair
p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY()));
1534 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1537 MaNGOS::NearUsedPosDo
u_do(*this,searcher
,absAngle
,selector
);
1538 MaNGOS::WorldObjectWorker
<MaNGOS::NearUsedPosDo
> worker(u_do
);
1540 TypeContainerVisitor
<MaNGOS::WorldObjectWorker
<MaNGOS::NearUsedPosDo
>, GridTypeMapContainer
> grid_obj_worker(worker
);
1541 TypeContainerVisitor
<MaNGOS::WorldObjectWorker
<MaNGOS::NearUsedPosDo
>, WorldTypeMapContainer
> world_obj_worker(worker
);
1543 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1544 cell_lock
->Visit(cell_lock
, grid_obj_worker
, *GetMap());
1545 cell_lock
->Visit(cell_lock
, world_obj_worker
, *GetMap());
1548 // maybe can just place in primary position
1549 if( selector
.CheckOriginal() )
1551 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1553 if(IsWithinLOS(x
,y
,z
))
1556 first_los_conflict
= true; // first point have LOS problems
1559 float angle
; // candidate of angle for free pos
1561 // special case when one from list empty and then empty side preferred
1562 if(selector
.FirstAngle(angle
))
1564 GetNearPoint2D(x
,y
,distance2d
,absAngle
+angle
);
1566 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1568 if(IsWithinLOS(x
,y
,z
))
1572 // set first used pos in lists
1573 selector
.InitializeAngle();
1575 // select in positions after current nodes (selection one by one)
1576 while(selector
.NextAngle(angle
)) // angle for free pos
1578 GetNearPoint2D(x
,y
,distance2d
,absAngle
+angle
);
1580 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1582 if(IsWithinLOS(x
,y
,z
))
1586 // BAD NEWS: not free pos (or used or have LOS problems)
1587 // Attempt find _used_ pos without LOS problem
1589 if(!first_los_conflict
)
1594 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1598 // special case when one from list empty and then empty side preferred
1599 if( selector
.IsNonBalanced() )
1601 if(!selector
.FirstAngle(angle
)) // _used_ pos
1603 GetNearPoint2D(x
,y
,distance2d
,absAngle
+angle
);
1605 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1607 if(IsWithinLOS(x
,y
,z
))
1612 // set first used pos in lists
1613 selector
.InitializeAngle();
1615 // select in positions after current nodes (selection one by one)
1616 while(selector
.NextUsedAngle(angle
)) // angle for used pos but maybe without LOS problem
1618 GetNearPoint2D(x
,y
,distance2d
,absAngle
+angle
);
1620 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available
1622 if(IsWithinLOS(x
,y
,z
))
1626 // BAD BAD NEWS: all found pos (free and used) have LOS problem :(
1630 UpdateGroundPositionZ(x
,y
,z
); // update to LOS height if available