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 "WorldPacket.h"
21 #include "WorldSession.h"
27 #include "MapManager.h"
28 #include "Transports.h"
29 #include "BattleGround.h"
30 #include "WaypointMovementGenerator.h"
31 #include "InstanceSaveMgr.h"
33 void WorldSession::HandleMoveWorldportAckOpcode( WorldPacket
& /*recv_data*/ )
35 sLog
.outDebug( "WORLD: got MSG_MOVE_WORLDPORT_ACK." );
36 HandleMoveWorldportAckOpcode();
39 void WorldSession::HandleMoveWorldportAckOpcode()
41 // get the teleport destination
42 WorldLocation
&loc
= GetPlayer()->GetTeleportDest();
44 // possible errors in the coordinate validity check
45 if(!MapManager::IsValidMapCoord(loc
.mapid
,loc
.x
,loc
.y
,loc
.z
,loc
.o
))
51 // get the destination map entry, not the current one, this will fix homebind and reset greeting
52 MapEntry
const* mEntry
= sMapStore
.LookupEntry(loc
.mapid
);
53 InstanceTemplate
const* mInstance
= objmgr
.GetInstanceTemplate(loc
.mapid
);
55 // reset instance validity, except if going to an instance inside an instance
56 if(GetPlayer()->m_InstanceValid
== false && !mInstance
)
57 GetPlayer()->m_InstanceValid
= true;
59 GetPlayer()->SetSemaphoreTeleport(false);
61 // relocate the player to the teleport destination
62 GetPlayer()->SetMapId(loc
.mapid
);
63 GetPlayer()->Relocate(loc
.x
, loc
.y
, loc
.z
, loc
.o
);
65 // since the MapId is set before the GetInstance call, the InstanceId must be set to 0
66 // to let GetInstance() determine the proper InstanceId based on the player's binds
67 GetPlayer()->SetInstanceId(0);
69 // check this before Map::Add(player), because that will create the instance save!
70 bool reset_notify
= (GetPlayer()->GetBoundInstance(GetPlayer()->GetMapId(), GetPlayer()->GetDifficulty()) == NULL
);
72 GetPlayer()->SendInitialPacketsBeforeAddToMap();
73 // the CanEnter checks are done in TeleporTo but conditions may change
74 // while the player is in transit, for example the map may get full
75 if(!MapManager::Instance().GetMap(GetPlayer()->GetMapId(), GetPlayer())->Add(GetPlayer()))
77 sLog
.outDebug("WORLD: teleport of player %s (%d) to location %d,%f,%f,%f,%f failed", GetPlayer()->GetName(), GetPlayer()->GetGUIDLow(), loc
.mapid
, loc
.x
, loc
.y
, loc
.z
, loc
.o
);
78 // teleport the player home
79 GetPlayer()->SetDontMove(false);
80 if(!GetPlayer()->TeleportTo(GetPlayer()->m_homebindMapId
, GetPlayer()->m_homebindX
, GetPlayer()->m_homebindY
, GetPlayer()->m_homebindZ
, GetPlayer()->GetOrientation()))
82 // the player must always be able to teleport home
83 sLog
.outError("WORLD: failed to teleport player %s (%d) to homebind location %d,%f,%f,%f,%f!", GetPlayer()->GetName(), GetPlayer()->GetGUIDLow(), GetPlayer()->m_homebindMapId
, GetPlayer()->m_homebindX
, GetPlayer()->m_homebindY
, GetPlayer()->m_homebindZ
, GetPlayer()->GetOrientation());
88 GetPlayer()->SendInitialPacketsAfterAddToMap();
90 // flight fast teleport case
91 if(GetPlayer()->GetMotionMaster()->GetCurrentMovementGeneratorType()==FLIGHT_MOTION_TYPE
)
93 if(!_player
->InBattleGround())
95 // short preparations to continue flight
96 GetPlayer()->SetDontMove(false);
97 FlightPathMovementGenerator
* flight
= (FlightPathMovementGenerator
*)(GetPlayer()->GetMotionMaster()->top());
98 flight
->Initialize(*GetPlayer());
102 // battleground state prepare, stop flight
103 GetPlayer()->GetMotionMaster()->MovementExpired();
104 GetPlayer()->m_taxi
.ClearTaxiDestinations();
107 // resurrect character at enter into instance where his corpse exist after add to map
108 Corpse
*corpse
= GetPlayer()->GetCorpse();
109 if (corpse
&& corpse
->GetType() != CORPSE_BONES
&& corpse
->GetMapId() == GetPlayer()->GetMapId())
111 if( mEntry
->IsDungeon() )
113 GetPlayer()->ResurrectPlayer(0.5f
);
114 GetPlayer()->SpawnCorpseBones();
115 GetPlayer()->SaveToDB();
119 if(mEntry
->IsRaid() && mInstance
)
123 uint32 timeleft
= sInstanceSaveManager
.GetResetTimeFor(GetPlayer()->GetMapId()) - time(NULL
);
124 GetPlayer()->SendInstanceResetWarning(GetPlayer()->GetMapId(), timeleft
); // greeting at the entrance of the resort raid instance
129 if(!mEntry
->IsMountAllowed())
130 _player
->RemoveSpellsCausingAura(SPELL_AURA_MOUNTED
);
132 // battleground state prepare
133 if(_player
->InBattleGround())
135 BattleGround
*bg
= _player
->GetBattleGround();
138 if(bg
->GetMapId() == _player
->GetMapId()) // we teleported to bg
140 if(!bg
->GetBgRaid(_player
->GetTeam())) // first player joined
142 Group
*group
= new Group
;
143 bg
->SetBgRaid(_player
->GetTeam(), group
);
144 group
->Create(_player
->GetGUIDLow(), _player
->GetName());
146 else // raid already exist
148 bg
->GetBgRaid(_player
->GetTeam())->AddMember(_player
->GetGUID(), _player
->GetName());
155 if(GetPlayer()->pvpInfo
.inHostileArea
)
156 GetPlayer()->CastSpell(GetPlayer(), 2479, true);
159 if(GetPlayer()->m_temporaryUnsummonedPetNumber
)
161 Pet
* NewPet
= new Pet
;
162 if(!NewPet
->LoadPetFromDB(GetPlayer(), 0, GetPlayer()->m_temporaryUnsummonedPetNumber
, true))
165 GetPlayer()->m_temporaryUnsummonedPetNumber
= 0;
168 GetPlayer()->SetDontMove(false);
171 void WorldSession::HandleMovementOpcodes( WorldPacket
& recv_data
)
173 CHECK_PACKET_SIZE(recv_data
, 4+1+4+4+4+4+4);
175 if(GetPlayer()->GetDontMove())
179 MovementInfo movementInfo
;
180 uint32 MovementFlags
;
182 recv_data
>> MovementFlags
;
183 recv_data
>> movementInfo
.unk1
;
184 recv_data
>> movementInfo
.time
;
185 recv_data
>> movementInfo
.x
;
186 recv_data
>> movementInfo
.y
;
187 recv_data
>> movementInfo
.z
;
188 recv_data
>> movementInfo
.o
;
190 //Save movement flags
191 _player
->SetUnitMovementFlags(MovementFlags
);
193 if(MovementFlags
& MOVEMENTFLAG_ONTRANSPORT
)
196 CHECK_PACKET_SIZE(recv_data
, recv_data
.rpos()+8+4+4+4+4+4);
198 recv_data
>> movementInfo
.t_guid
;
199 recv_data
>> movementInfo
.t_x
;
200 recv_data
>> movementInfo
.t_y
;
201 recv_data
>> movementInfo
.t_z
;
202 recv_data
>> movementInfo
.t_o
;
203 recv_data
>> movementInfo
.t_time
;
206 if(MovementFlags
& (MOVEMENTFLAG_SWIMMING
| MOVEMENTFLAG_FLYING2
))
209 CHECK_PACKET_SIZE(recv_data
, recv_data
.rpos()+4);
211 recv_data
>> movementInfo
.s_pitch
; // pitch, -1.55=looking down, 0=looking straight forward, +1.55=looking up
215 CHECK_PACKET_SIZE(recv_data
, recv_data
.rpos()+4);
217 recv_data
>> movementInfo
.fallTime
; // duration of last jump (when in jump duration from jump begin to now)
219 if(MovementFlags
& MOVEMENTFLAG_JUMPING
)
222 CHECK_PACKET_SIZE(recv_data
, recv_data
.rpos()+4+4+4+4);
224 recv_data
>> movementInfo
.j_unk
; // constant, but different when jumping in water and on land?
225 recv_data
>> movementInfo
.j_sinAngle
; // sin of angle between orientation0 and players orientation
226 recv_data
>> movementInfo
.j_cosAngle
; // cos of angle between orientation0 and players orientation
227 recv_data
>> movementInfo
.j_xyspeed
; // speed of xy movement
230 if(MovementFlags
& MOVEMENTFLAG_SPLINE
)
233 CHECK_PACKET_SIZE(recv_data
, recv_data
.rpos()+4);
235 recv_data
>> movementInfo
.u_unk1
; // unknown
239 if(recv_data
.size() != recv_data
.rpos())
241 sLog
.outError("MovementHandler: player %s (guid %d, account %u) sent a packet (opcode %u) that is %u bytes larger than it should be. Kicked as cheater.", _player
->GetName(), _player
->GetGUIDLow(), _player
->GetSession()->GetAccountId(), recv_data
.GetOpcode(), recv_data
.size() - recv_data
.rpos());
246 if (!MaNGOS::IsValidMapCoord(movementInfo
.x
, movementInfo
.y
, movementInfo
.z
, movementInfo
.o
))
249 /* handle special cases */
250 if (MovementFlags
& MOVEMENTFLAG_ONTRANSPORT
)
252 // transports size limited
253 // (also received at zeppelin leave by some reason with t_* as absolute in continent coordinates, can be safely skipped)
254 if( movementInfo
.t_x
> 50 || movementInfo
.t_y
> 50 || movementInfo
.t_z
> 50 )
257 if( !MaNGOS::IsValidMapCoord(movementInfo
.x
+movementInfo
.t_x
, movementInfo
.y
+movementInfo
.t_y
,
258 movementInfo
.z
+movementInfo
.t_z
, movementInfo
.o
+movementInfo
.t_o
) )
261 // if we boarded a transport, add us to it
262 if (!GetPlayer()->m_transport
)
264 // elevators also cause the client to send MOVEMENTFLAG_ONTRANSPORT - just unmount if the guid can be found in the transport list
265 for (MapManager::TransportSet::iterator iter
= MapManager::Instance().m_Transports
.begin(); iter
!= MapManager::Instance().m_Transports
.end(); ++iter
)
267 if ((*iter
)->GetGUID() == movementInfo
.t_guid
)
269 // unmount before boarding
270 _player
->RemoveSpellsCausingAura(SPELL_AURA_MOUNTED
);
272 GetPlayer()->m_transport
= (*iter
);
273 (*iter
)->AddPassenger(GetPlayer());
279 else if (GetPlayer()->m_transport
) // if we were on a transport, leave
281 GetPlayer()->m_transport
->RemovePassenger(GetPlayer());
282 GetPlayer()->m_transport
= NULL
;
283 movementInfo
.t_x
= 0.0f
;
284 movementInfo
.t_y
= 0.0f
;
285 movementInfo
.t_z
= 0.0f
;
286 movementInfo
.t_o
= 0.0f
;
287 movementInfo
.t_time
= 0;
290 // fall damage generation (ignore in flight case that can be triggered also at lags in moment teleportation to another map).
291 if (recv_data
.GetOpcode() == MSG_MOVE_FALL_LAND
&& !GetPlayer()->isInFlight())
293 Player
*target
= GetPlayer();
295 //Players with Feather Fall or low fall time, or physical immunity (charges used) are ignored
296 if (movementInfo
.fallTime
> 1100 && !target
->isDead() && !target
->isGameMaster() &&
297 !target
->HasAuraType(SPELL_AURA_HOVER
) && !target
->HasAuraType(SPELL_AURA_FEATHER_FALL
) &&
298 !target
->HasAuraType(SPELL_AURA_FLY
) && !target
->IsImmunedToDamage(SPELL_SCHOOL_MASK_NORMAL
,true) )
300 //Safe fall, fall time reduction
301 int32 safe_fall
= target
->GetTotalAuraModifier(SPELL_AURA_SAFE_FALL
);
302 uint32 fall_time
= (movementInfo
.fallTime
> (safe_fall
*10)) ? movementInfo
.fallTime
- (safe_fall
*10) : 0;
304 if(fall_time
> 1100) //Prevent damage if fall time < 1100
306 //Fall Damage calculation
307 float fallperc
= float(fall_time
)/1100;
308 uint32 damage
= (uint32
)(((fallperc
*fallperc
-1) / 9 * target
->GetMaxHealth())*sWorld
.getRate(RATE_DAMAGE_FALL
));
310 float height
= movementInfo
.z
;
311 target
->UpdateGroundPositionZ(movementInfo
.x
,movementInfo
.y
,height
);
315 //Prevent fall damage from being more than the player maximum health
316 if (damage
> target
->GetMaxHealth())
317 damage
= target
->GetMaxHealth();
320 if (target
->GetDummyAura(43621))
321 damage
= target
->GetMaxHealth()/2;
323 target
->EnvironmentalDamage(target
->GetGUID(), DAMAGE_FALL
, damage
);
326 //Z given by moveinfo, LastZ, FallTime, WaterZ, MapZ, Damage, Safefall reduction
327 DEBUG_LOG("FALLDAMAGE z=%f sz=%f pZ=%f FallTime=%d mZ=%f damage=%d SF=%d" , movementInfo
.z
, height
, target
->GetPositionZ(), movementInfo
.fallTime
, height
, damage
, safe_fall
);
331 //handle fall and logout at the same time (logout started before fall finished)
332 /* outdated and create problems with sit at stun sometime
333 if (target->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_DISABLE_ROTATE))
335 target->SetStandState(PLAYER_STATE_SIT);
337 WorldPacket data( SMSG_FORCE_MOVE_ROOT, 12 );
338 data.append(target->GetPackGUID());
345 if(((MovementFlags
& MOVEMENTFLAG_SWIMMING
) != 0) != GetPlayer()->IsInWater())
347 // now client not include swimming flag in case jumping under water
348 GetPlayer()->SetInWater( !GetPlayer()->IsInWater() || GetPlayer()->GetBaseMap()->IsUnderWater(movementInfo
.x
, movementInfo
.y
, movementInfo
.z
) );
351 /*----------------------*/
353 /* process position-change */
354 recv_data
.put
<uint32
>(5, getMSTime()); // offset flags(4) + unk(1)
355 WorldPacket
data(recv_data
.GetOpcode(), (GetPlayer()->GetPackGUID().size()+recv_data
.size()));
356 data
.append(GetPlayer()->GetPackGUID());
357 data
.append(recv_data
.contents(), recv_data
.size());
358 GetPlayer()->SendMessageToSet(&data
, false);
360 GetPlayer()->SetPosition(movementInfo
.x
, movementInfo
.y
, movementInfo
.z
, movementInfo
.o
);
361 GetPlayer()->m_movementInfo
= movementInfo
;
363 if(GetPlayer()->isMovingOrTurning())
364 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH
);
366 if(movementInfo
.z
< -500.0f
)
368 // NOTE: this is actually called many times while falling
369 // even after the player has been teleported away
370 // TODO: discard movement packets after the player is rooted
371 if(GetPlayer()->isAlive())
373 GetPlayer()->EnvironmentalDamage(GetPlayer()->GetGUID(),DAMAGE_FALL_TO_VOID
, GetPlayer()->GetMaxHealth());
374 // change the death state to CORPSE to prevent the death timer from
375 // starting in the next player update
376 GetPlayer()->KillPlayer();
377 GetPlayer()->BuildPlayerRepop();
380 // cancel the death timer here if started
381 GetPlayer()->RepopAtGraveyard();
385 void WorldSession::HandleForceSpeedChangeAck(WorldPacket
&recv_data
)
387 CHECK_PACKET_SIZE(recv_data
, 8+4+4+1+4+4+4+4+4);
392 uint32 unk1
, flags
, time
, fallTime
;
393 float x
, y
, z
, orientation
;
396 float t_x
, t_y
, t_z
, t_o
;
399 float j_unk1
, j_sinAngle
, j_cosAngle
, j_xyspeed
;
405 // now can skip not our packet
406 if(_player
->GetGUID() != guid
)
409 // continue parse packet
412 recv_data
>> flags
>> unkB
>> time
;
413 recv_data
>> x
>> y
>> z
>> orientation
;
414 if (flags
& MOVEMENTFLAG_ONTRANSPORT
)
417 CHECK_PACKET_SIZE(recv_data
, recv_data
.rpos()+8+4+4+4+4+4);
420 recv_data
>> t_x
>> t_y
>> t_z
>> t_o
>> t_time
;
422 if (flags
& (MOVEMENTFLAG_SWIMMING
| MOVEMENTFLAG_FLYING2
))
425 CHECK_PACKET_SIZE(recv_data
, recv_data
.rpos()+4);
427 recv_data
>> s_pitch
; // pitch, -1.55=looking down, 0=looking straight forward, +1.55=looking up
431 CHECK_PACKET_SIZE(recv_data
, recv_data
.rpos()+4);
433 recv_data
>> fallTime
; // duration of last jump (when in jump duration from jump begin to now)
435 if ((flags
& MOVEMENTFLAG_JUMPING
) || (flags
& MOVEMENTFLAG_FALLING
))
438 CHECK_PACKET_SIZE(recv_data
, recv_data
.rpos()+4+4+4+4);
440 recv_data
>> j_unk1
; // ?constant, but different when jumping in water and on land?
441 recv_data
>> j_sinAngle
>> j_cosAngle
; // sin + cos of angle between orientation0 and players orientation
442 recv_data
>> j_xyspeed
; // speed of xy movement
445 if(flags
& MOVEMENTFLAG_SPLINE
)
448 CHECK_PACKET_SIZE(recv_data
, recv_data
.rpos()+4);
450 recv_data
>> u_unk1
; // unknown
454 CHECK_PACKET_SIZE(recv_data
, recv_data
.rpos()+4);
456 recv_data
>> newspeed
;
459 // client ACK send one packet for mounted/run case and need skip all except last from its
460 // in other cases anti-cheat check can be fail in false case
461 UnitMoveType move_type
;
462 UnitMoveType force_move_type
;
464 static char const* move_type_name
[MAX_MOVE_TYPE
] = { "Walk", "Run", "Walkback", "Swim", "Swimback", "Turn", "Fly", "Flyback" };
466 uint16 opcode
= recv_data
.GetOpcode();
469 case CMSG_FORCE_WALK_SPEED_CHANGE_ACK
: move_type
= MOVE_WALK
; force_move_type
= MOVE_WALK
; break;
470 case CMSG_FORCE_RUN_SPEED_CHANGE_ACK
: move_type
= MOVE_RUN
; force_move_type
= MOVE_RUN
; break;
471 case CMSG_FORCE_RUN_BACK_SPEED_CHANGE_ACK
: move_type
= MOVE_WALKBACK
; force_move_type
= MOVE_WALKBACK
; break;
472 case CMSG_FORCE_SWIM_SPEED_CHANGE_ACK
: move_type
= MOVE_SWIM
; force_move_type
= MOVE_SWIM
; break;
473 case CMSG_FORCE_SWIM_BACK_SPEED_CHANGE_ACK
: move_type
= MOVE_SWIMBACK
; force_move_type
= MOVE_SWIMBACK
; break;
474 case CMSG_FORCE_TURN_RATE_CHANGE_ACK
: move_type
= MOVE_TURN
; force_move_type
= MOVE_TURN
; break;
475 case CMSG_FORCE_FLIGHT_SPEED_CHANGE_ACK
: move_type
= MOVE_FLY
; force_move_type
= MOVE_FLY
; break;
476 case CMSG_FORCE_FLIGHT_BACK_SPEED_CHANGE_ACK
: move_type
= MOVE_FLYBACK
; force_move_type
= MOVE_FLYBACK
; break;
478 sLog
.outError("WorldSession::HandleForceSpeedChangeAck: Unknown move type opcode: %u", opcode
);
482 // skip all forced speed changes except last and unexpected
483 // in run/mounted case used one ACK and it must be skipped.m_forced_speed_changes[MOVE_RUN} store both.
484 if(_player
->m_forced_speed_changes
[force_move_type
] > 0)
486 --_player
->m_forced_speed_changes
[force_move_type
];
487 if(_player
->m_forced_speed_changes
[force_move_type
] > 0)
491 if (!_player
->GetTransport() && fabs(_player
->GetSpeed(move_type
) - newspeed
) > 0.01f
)
493 if(_player
->GetSpeed(move_type
) > newspeed
) // must be greater - just correct
495 sLog
.outError("%sSpeedChange player %s is NOT correct (must be %f instead %f), force set to correct value",
496 move_type_name
[move_type
], _player
->GetName(), _player
->GetSpeed(move_type
), newspeed
);
497 _player
->SetSpeed(move_type
,_player
->GetSpeedRate(move_type
),true);
499 else // must be lesser - cheating
501 sLog
.outBasic("Player %s from account id %u kicked for incorrect speed (must be %f instead %f)",
502 _player
->GetName(),_player
->GetSession()->GetAccountId(),_player
->GetSpeed(move_type
), newspeed
);
503 _player
->GetSession()->KickPlayer();
508 void WorldSession::HandleSetActiveMoverOpcode(WorldPacket
&recv_data
)
510 sLog
.outDebug("WORLD: Recvd CMSG_SET_ACTIVE_MOVER");
512 CHECK_PACKET_SIZE(recv_data
,8);
517 WorldPacket
data(SMSG_TIME_SYNC_REQ
, 4); // new 2.0.x, enable movement
518 data
<< uint32(0x00000000); // on blizz it increments periodically
522 void WorldSession::HandleMountSpecialAnimOpcode(WorldPacket
& /*recvdata*/)
524 //sLog.outDebug("WORLD: Recvd CMSG_MOUNTSPECIAL_ANIM");
526 WorldPacket
data(SMSG_MOUNTSPECIAL_ANIM
, 8);
527 data
<< uint64(GetPlayer()->GetGUID());
529 GetPlayer()->SendMessageToSet(&data
, false);
532 void WorldSession::HandleMoveKnockBackAck( WorldPacket
& /*recv_data*/ )
534 // CHECK_PACKET_SIZE(recv_data,?);
535 sLog
.outDebug("CMSG_MOVE_KNOCK_BACK_ACK");
536 // Currently not used but maybe use later for recheck final player position
537 // (must be at call same as into "recv_data >> x >> y >> z >> orientation;"
541 float x, y, z, orientation;
545 float xdirection,ydirection,hspeed,vspeed;
548 recv_data >> sequence;
549 recv_data >> flags >> time;
550 recv_data >> x >> y >> z >> orientation;
551 recv_data >> ukn1; //unknown
552 recv_data >> vspeed >> xdirection >> ydirection >> hspeed;
554 // skip not personal message;
555 if(GetPlayer()->GetGUID()!=guid)
562 void WorldSession::HandleMoveHoverAck( WorldPacket
& /*recv_data*/ )
564 sLog
.outDebug("CMSG_MOVE_HOVER_ACK");
567 void WorldSession::HandleMoveWaterWalkAck(WorldPacket
& /*recv_data*/)
569 sLog
.outDebug("CMSG_MOVE_WATER_WALK_ACK");
572 void WorldSession::HandleSummonResponseOpcode(WorldPacket
& recv_data
)
574 CHECK_PACKET_SIZE(recv_data
,8+1);
576 if(!_player
->isAlive() || _player
->isInCombat() )
579 uint64 summoner_guid
;
581 recv_data
>> summoner_guid
;
584 _player
->SummonIfPossible(agree
);