2 * Copyright (C) 2005-2010 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 "SpellAuras.h"
28 #include "MapManager.h"
29 #include "Transports.h"
30 #include "BattleGround.h"
31 #include "WaypointMovementGenerator.h"
32 #include "InstanceSaveMgr.h"
33 #include "ObjectMgr.h"
35 void WorldSession::HandleMoveWorldportAckOpcode( WorldPacket
& /*recv_data*/ )
37 sLog
.outDebug( "WORLD: got MSG_MOVE_WORLDPORT_ACK." );
38 HandleMoveWorldportAckOpcode();
41 void WorldSession::HandleMoveWorldportAckOpcode()
43 // ignore unexpected far teleports
44 if(!GetPlayer()->IsBeingTeleportedFar())
47 // get the teleport destination
48 WorldLocation
&loc
= GetPlayer()->GetTeleportDest();
50 // possible errors in the coordinate validity check
51 if(!MapManager::IsValidMapCoord(loc
.mapid
, loc
.coord_x
, loc
.coord_y
, loc
.coord_z
, loc
.orientation
))
53 sLog
.outError("WorldSession::HandleMoveWorldportAckOpcode: player %s (%d) was teleported far to a not valid location. (map:%u, x:%f, y:%f, "
54 "z:%f) We port him to his homebind instead..", GetPlayer()->GetName(), GetPlayer()->GetGUIDLow(), loc
.mapid
, loc
.coord_x
, loc
.coord_y
, loc
.coord_z
);
55 // stop teleportation else we would try this again and again in LogoutPlayer...
56 GetPlayer()->SetSemaphoreTeleportFar(false);
57 // and teleport the player to a valid place
58 GetPlayer()->TeleportToHomebind();
62 // get the destination map entry, not the current one, this will fix homebind and reset greeting
63 MapEntry
const* mEntry
= sMapStore
.LookupEntry(loc
.mapid
);
64 InstanceTemplate
const* mInstance
= ObjectMgr::GetInstanceTemplate(loc
.mapid
);
66 // reset instance validity, except if going to an instance inside an instance
67 if(GetPlayer()->m_InstanceValid
== false && !mInstance
)
68 GetPlayer()->m_InstanceValid
= true;
70 GetPlayer()->SetSemaphoreTeleportFar(false);
72 // relocate the player to the teleport destination
73 GetPlayer()->SetMap(sMapMgr
.CreateMap(loc
.mapid
, GetPlayer()));
74 GetPlayer()->Relocate(loc
.coord_x
, loc
.coord_y
, loc
.coord_z
, loc
.orientation
);
76 GetPlayer()->SendInitialPacketsBeforeAddToMap();
77 // the CanEnter checks are done in TeleporTo but conditions may change
78 // while the player is in transit, for example the map may get full
79 if(!GetPlayer()->GetMap()->Add(GetPlayer()))
81 //if player wasn't added to map, reset his map pointer!
82 GetPlayer()->ResetMap();
84 sLog
.outError("WorldSession::HandleMoveWorldportAckOpcode: player %s (%d) was teleported far but couldn't be added to map. (map:%u, x:%f, y:%f, "
85 "z:%f) We port him to his homebind instead..", GetPlayer()->GetName(), GetPlayer()->GetGUIDLow(), loc
.mapid
, loc
.coord_x
, loc
.coord_y
, loc
.coord_z
);
86 // teleport the player home
87 GetPlayer()->TeleportToHomebind();
91 // battleground state prepare (in case join to BG), at relogin/tele player not invited
92 // only add to bg group and object, if the player was invited (else he entered through command)
93 if(_player
->InBattleGround())
95 // cleanup setting if outdated
96 if(!mEntry
->IsBattleGroundOrArena())
99 _player
->SetBattleGroundId(0, BATTLEGROUND_TYPE_NONE
);
100 // reset destination bg team
101 _player
->SetBGTeam(0);
104 else if(BattleGround
*bg
= _player
->GetBattleGround())
106 if(_player
->IsInvitedForBattleGroundInstance(_player
->GetBattleGroundId()))
107 bg
->AddPlayer(_player
);
111 GetPlayer()->SendInitialPacketsAfterAddToMap();
113 // flight fast teleport case
114 if(GetPlayer()->GetMotionMaster()->GetCurrentMovementGeneratorType() == FLIGHT_MOTION_TYPE
)
116 if(!_player
->InBattleGround())
118 // short preparations to continue flight
119 FlightPathMovementGenerator
* flight
= (FlightPathMovementGenerator
*)(GetPlayer()->GetMotionMaster()->top());
120 flight
->Initialize(*GetPlayer());
124 // battleground state prepare, stop flight
125 GetPlayer()->GetMotionMaster()->MovementExpired();
126 GetPlayer()->m_taxi
.ClearTaxiDestinations();
129 // resurrect character at enter into instance where his corpse exist after add to map
130 Corpse
*corpse
= GetPlayer()->GetCorpse();
131 if (corpse
&& corpse
->GetType() != CORPSE_BONES
&& corpse
->GetMapId() == GetPlayer()->GetMapId())
133 if( mEntry
->IsDungeon() )
135 GetPlayer()->ResurrectPlayer(0.5f
);
136 GetPlayer()->SpawnCorpseBones();
142 Difficulty diff
= GetPlayer()->GetDifficulty(mEntry
->IsRaid());
143 if(MapDifficulty
const* mapDiff
= GetMapDifficultyData(mEntry
->MapID
,diff
))
145 if (mapDiff
->resetTime
)
147 if (uint32 timeReset
= sInstanceSaveMgr
.GetResetTimeFor(mEntry
->MapID
,diff
))
149 uint32 timeleft
= timeReset
- time(NULL
);
150 GetPlayer()->SendInstanceResetWarning(mEntry
->MapID
, diff
, timeleft
);
157 if(!mEntry
->IsMountAllowed())
158 _player
->RemoveSpellsCausingAura(SPELL_AURA_MOUNTED
);
161 if(GetPlayer()->pvpInfo
.inHostileArea
)
162 GetPlayer()->CastSpell(GetPlayer(), 2479, true);
165 GetPlayer()->ResummonPetTemporaryUnSummonedIfAny();
167 //lets process all delayed operations on successful teleport
168 GetPlayer()->ProcessDelayedOperations();
171 void WorldSession::HandleMoveTeleportAck(WorldPacket
& recv_data
)
173 sLog
.outDebug("MSG_MOVE_TELEPORT_ACK");
176 if(!recv_data
.readPackGUID(guid
))
180 recv_data
>> flags
>> time
;
181 DEBUG_LOG("Guid " UI64FMTD
, guid
);
182 DEBUG_LOG("Flags %u, time %u", flags
, time
/IN_MILISECONDS
);
184 Unit
*mover
= _player
->m_mover
;
185 Player
*plMover
= mover
->GetTypeId() == TYPEID_PLAYER
? (Player
*)mover
: NULL
;
187 if(!plMover
|| !plMover
->IsBeingTeleportedNear())
190 if(guid
!= plMover
->GetGUID())
193 plMover
->SetSemaphoreTeleportNear(false);
195 uint32 old_zone
= plMover
->GetZoneId();
197 WorldLocation
const& dest
= plMover
->GetTeleportDest();
199 plMover
->SetPosition(dest
.coord_x
, dest
.coord_y
, dest
.coord_z
, dest
.orientation
, true);
201 uint32 newzone
, newarea
;
202 plMover
->GetZoneAndAreaId(newzone
, newarea
);
203 plMover
->UpdateZone(newzone
, newarea
);
206 if(old_zone
!= newzone
)
209 if(plMover
->pvpInfo
.inHostileArea
)
210 plMover
->CastSpell(plMover
, 2479, true);
214 GetPlayer()->ResummonPetTemporaryUnSummonedIfAny();
216 //lets process all delayed operations on successful teleport
217 GetPlayer()->ProcessDelayedOperations();
220 void WorldSession::HandleMovementOpcodes( WorldPacket
& recv_data
)
222 uint32 opcode
= recv_data
.GetOpcode();
223 sLog
.outDebug("WORLD: Recvd %s (%u, 0x%X) opcode", LookupOpcodeName(opcode
), opcode
, opcode
);
226 Unit
*mover
= _player
->m_mover
;
227 Player
*plMover
= mover
->GetTypeId() == TYPEID_PLAYER
? (Player
*)mover
: NULL
;
229 // ignore, waiting processing in WorldSession::HandleMoveWorldportAckOpcode and WorldSession::HandleMoveTeleportAck
230 if(plMover
&& plMover
->IsBeingTeleported())
232 recv_data
.rpos(recv_data
.wpos()); // prevent warnings spam
239 if(!recv_data
.readPackGUID(guid
))
242 MovementInfo
movementInfo(recv_data
);
245 if (!MaNGOS::IsValidMapCoord(movementInfo
.GetPos()->x
, movementInfo
.GetPos()->y
, movementInfo
.GetPos()->z
, movementInfo
.GetPos()->o
))
247 recv_data
.rpos(recv_data
.wpos()); // prevent warnings spam
251 /* handle special cases */
252 if (movementInfo
.HasMovementFlag(MOVEFLAG_ONTRANSPORT
))
254 // transports size limited
255 // (also received at zeppelin/lift leave by some reason with t_* as absolute in continent coordinates, can be safely skipped)
256 if( movementInfo
.GetTransportPos()->x
> 50 || movementInfo
.GetTransportPos()->y
> 50 || movementInfo
.GetTransportPos()->z
> 100 )
258 recv_data
.rpos(recv_data
.wpos()); // prevent warnings spam
262 if( !MaNGOS::IsValidMapCoord(movementInfo
.GetPos()->x
+ movementInfo
.GetTransportPos()->x
, movementInfo
.GetPos()->y
+ movementInfo
.GetTransportPos()->y
,
263 movementInfo
.GetPos()->z
+ movementInfo
.GetTransportPos()->z
, movementInfo
.GetPos()->o
+ movementInfo
.GetTransportPos()->o
) )
265 recv_data
.rpos(recv_data
.wpos()); // prevent warnings spam
269 // if we boarded a transport, add us to it
270 if (plMover
&& !plMover
->m_transport
)
272 // elevators also cause the client to send MOVEFLAG_ONTRANSPORT - just unmount if the guid can be found in the transport list
273 for (MapManager::TransportSet::const_iterator iter
= sMapMgr
.m_Transports
.begin(); iter
!= sMapMgr
.m_Transports
.end(); ++iter
)
275 if ((*iter
)->GetGUID() == movementInfo
.GetTransportGuid())
277 plMover
->m_transport
= (*iter
);
278 (*iter
)->AddPassenger(plMover
);
284 else if (plMover
&& plMover
->m_transport
) // if we were on a transport, leave
286 plMover
->m_transport
->RemovePassenger(plMover
);
287 plMover
->m_transport
= NULL
;
288 movementInfo
.SetTransportData(0, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0, -1);
291 // fall damage generation (ignore in flight case that can be triggered also at lags in moment teleportation to another map).
292 if (opcode
== MSG_MOVE_FALL_LAND
&& plMover
&& !plMover
->isInFlight())
293 plMover
->HandleFall(movementInfo
);
295 if (plMover
&& (movementInfo
.HasMovementFlag(MOVEFLAG_SWIMMING
) != plMover
->IsInWater()))
297 // now client not include swimming flag in case jumping under water
298 plMover
->SetInWater( !plMover
->IsInWater() || plMover
->GetBaseMap()->IsUnderWater(movementInfo
.GetPos()->x
, movementInfo
.GetPos()->y
, movementInfo
.GetPos()->z
) );
301 /*----------------------*/
303 /* process position-change */
304 movementInfo
.UpdateTime(getMSTime());
306 WorldPacket
data(opcode
, recv_data
.size());
307 data
.appendPackGUID(mover
->GetGUID()); // write guid
308 movementInfo
.Write(data
); // write data
309 GetPlayer()->SendMessageToSet(&data
, false);
311 if(plMover
) // nothing is charmed, or player charmed
313 plMover
->SetPosition(movementInfo
.GetPos()->x
, movementInfo
.GetPos()->y
, movementInfo
.GetPos()->z
, movementInfo
.GetPos()->o
);
314 plMover
->m_movementInfo
= movementInfo
;
315 plMover
->UpdateFallInformationIfNeed(movementInfo
, opcode
);
317 // after move info set
318 if ((opcode
== MSG_MOVE_SET_WALK_MODE
|| opcode
== MSG_MOVE_SET_RUN_MODE
))
319 plMover
->UpdateWalkMode(plMover
, false);
321 if(plMover
->isMovingOrTurning())
322 plMover
->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH
);
324 if(movementInfo
.GetPos()->z
< -500.0f
)
326 if(plMover
->InBattleGround()
327 && plMover
->GetBattleGround()
328 && plMover
->GetBattleGround()->HandlePlayerUnderMap(_player
))
330 // do nothing, the handle already did if returned true
334 // NOTE: this is actually called many times while falling
335 // even after the player has been teleported away
336 // TODO: discard movement packets after the player is rooted
337 if(plMover
->isAlive())
339 plMover
->EnvironmentalDamage(DAMAGE_FALL_TO_VOID
, GetPlayer()->GetMaxHealth());
340 // pl can be alive if GM/etc
341 if(!plMover
->isAlive())
343 // change the death state to CORPSE to prevent the death timer from
344 // starting in the next player update
345 plMover
->KillPlayer();
346 plMover
->BuildPlayerRepop();
350 // cancel the death timer here if started
351 plMover
->RepopAtGraveyard();
355 else // creature charmed
357 if(mover
->IsInWorld())
358 mover
->GetMap()->CreatureRelocation((Creature
*)mover
, movementInfo
.GetPos()->x
, movementInfo
.GetPos()->y
, movementInfo
.GetPos()->z
, movementInfo
.GetPos()->o
);
362 void WorldSession::HandleForceSpeedChangeAck(WorldPacket
&recv_data
)
364 uint32 opcode
= recv_data
.GetOpcode();
365 sLog
.outDebug("WORLD: Recvd %s (%u, 0x%X) opcode", LookupOpcodeName(opcode
), opcode
, opcode
);
371 if(!recv_data
.readPackGUID(guid
))
374 // now can skip not our packet
375 if(_player
->GetGUID() != guid
)
377 recv_data
.rpos(recv_data
.wpos()); // prevent warnings spam
381 // continue parse packet
382 recv_data
>> unk1
; // counter or moveEvent
384 MovementInfo
movementInfo(recv_data
);
386 recv_data
>> newspeed
;
389 // client ACK send one packet for mounted/run case and need skip all except last from its
390 // in other cases anti-cheat check can be fail in false case
391 UnitMoveType move_type
;
392 UnitMoveType force_move_type
;
394 static char const* move_type_name
[MAX_MOVE_TYPE
] = { "Walk", "Run", "RunBack", "Swim", "SwimBack", "TurnRate", "Flight", "FlightBack", "PitchRate" };
398 case CMSG_FORCE_WALK_SPEED_CHANGE_ACK
: move_type
= MOVE_WALK
; force_move_type
= MOVE_WALK
; break;
399 case CMSG_FORCE_RUN_SPEED_CHANGE_ACK
: move_type
= MOVE_RUN
; force_move_type
= MOVE_RUN
; break;
400 case CMSG_FORCE_RUN_BACK_SPEED_CHANGE_ACK
: move_type
= MOVE_RUN_BACK
; force_move_type
= MOVE_RUN_BACK
; break;
401 case CMSG_FORCE_SWIM_SPEED_CHANGE_ACK
: move_type
= MOVE_SWIM
; force_move_type
= MOVE_SWIM
; break;
402 case CMSG_FORCE_SWIM_BACK_SPEED_CHANGE_ACK
: move_type
= MOVE_SWIM_BACK
; force_move_type
= MOVE_SWIM_BACK
; break;
403 case CMSG_FORCE_TURN_RATE_CHANGE_ACK
: move_type
= MOVE_TURN_RATE
; force_move_type
= MOVE_TURN_RATE
; break;
404 case CMSG_FORCE_FLIGHT_SPEED_CHANGE_ACK
: move_type
= MOVE_FLIGHT
; force_move_type
= MOVE_FLIGHT
; break;
405 case CMSG_FORCE_FLIGHT_BACK_SPEED_CHANGE_ACK
: move_type
= MOVE_FLIGHT_BACK
; force_move_type
= MOVE_FLIGHT_BACK
; break;
406 case CMSG_FORCE_PITCH_RATE_CHANGE_ACK
: move_type
= MOVE_PITCH_RATE
; force_move_type
= MOVE_PITCH_RATE
; break;
408 sLog
.outError("WorldSession::HandleForceSpeedChangeAck: Unknown move type opcode: %u", opcode
);
412 // skip all forced speed changes except last and unexpected
413 // in run/mounted case used one ACK and it must be skipped.m_forced_speed_changes[MOVE_RUN} store both.
414 if(_player
->m_forced_speed_changes
[force_move_type
] > 0)
416 --_player
->m_forced_speed_changes
[force_move_type
];
417 if(_player
->m_forced_speed_changes
[force_move_type
] > 0)
421 if (!_player
->GetTransport() && fabs(_player
->GetSpeed(move_type
) - newspeed
) > 0.01f
)
423 if(_player
->GetSpeed(move_type
) > newspeed
) // must be greater - just correct
425 sLog
.outError("%sSpeedChange player %s is NOT correct (must be %f instead %f), force set to correct value",
426 move_type_name
[move_type
], _player
->GetName(), _player
->GetSpeed(move_type
), newspeed
);
427 _player
->SetSpeedRate(move_type
,_player
->GetSpeedRate(move_type
),true);
429 else // must be lesser - cheating
431 sLog
.outBasic("Player %s from account id %u kicked for incorrect speed (must be %f instead %f)",
432 _player
->GetName(),_player
->GetSession()->GetAccountId(),_player
->GetSpeed(move_type
), newspeed
);
433 _player
->GetSession()->KickPlayer();
438 void WorldSession::HandleSetActiveMoverOpcode(WorldPacket
&recv_data
)
440 sLog
.outDebug("WORLD: Recvd CMSG_SET_ACTIVE_MOVER");
446 if(_player
->m_mover
->GetGUID() != guid
)
448 sLog
.outError("HandleSetActiveMoverOpcode: incorrect mover guid: mover is " I64FMT
" and should be " I64FMT
, _player
->m_mover
->GetGUID(), guid
);
453 void WorldSession::HandleMoveNotActiveMover(WorldPacket
&recv_data
)
455 sLog
.outDebug("WORLD: Recvd CMSG_MOVE_NOT_ACTIVE_MOVER");
458 uint64 old_mover_guid
;
460 if(!recv_data
.readPackGUID(old_mover_guid
))
463 if(_player
->m_mover
->GetGUID() == old_mover_guid
)
465 sLog
.outError("HandleMoveNotActiveMover: incorrect mover guid: mover is " I64FMT
" and should be " I64FMT
" instead of " UI64FMTD
, _player
->m_mover
->GetGUID(), _player
->GetGUID(), old_mover_guid
);
466 recv_data
.rpos(recv_data
.wpos()); // prevent warnings spam
470 MovementInfo
mi(recv_data
);
472 _player
->m_movementInfo
= mi
;
475 void WorldSession::HandleDismissControlledVehicle(WorldPacket
&recv_data
)
477 sLog
.outDebug("WORLD: Recvd CMSG_DISMISS_CONTROLLED_VEHICLE");
480 uint64 vehicleGUID
= _player
->GetCharmGUID();
482 if(!vehicleGUID
) // something wrong here...
484 recv_data
.rpos(recv_data
.wpos()); // prevent warnings spam
490 if(!recv_data
.readPackGUID(guid
))
493 MovementInfo
mi(recv_data
);
495 _player
->m_movementInfo
= mi
;
497 // using charm guid, because we don't have vehicle guid...
498 if(Vehicle
*vehicle
= _player
->GetMap()->GetVehicle(vehicleGUID
))
500 // Aura::HandleAuraControlVehicle will call Player::ExitVehicle
501 vehicle
->RemoveSpellsCausingAura(SPELL_AURA_CONTROL_VEHICLE
);
505 void WorldSession::HandleMountSpecialAnimOpcode(WorldPacket
& /*recvdata*/)
507 //sLog.outDebug("WORLD: Recvd CMSG_MOUNTSPECIAL_ANIM");
509 WorldPacket
data(SMSG_MOUNTSPECIAL_ANIM
, 8);
510 data
<< uint64(GetPlayer()->GetGUID());
512 GetPlayer()->SendMessageToSet(&data
, false);
515 void WorldSession::HandleMoveKnockBackAck( WorldPacket
& recv_data
)
517 sLog
.outDebug("CMSG_MOVE_KNOCK_BACK_ACK");
519 uint64 guid
; // guid - unused
520 if(!recv_data
.readPackGUID(guid
))
523 recv_data
.read_skip
<uint32
>(); // unk
525 MovementInfo
movementInfo(recv_data
);
528 void WorldSession::HandleMoveHoverAck( WorldPacket
& recv_data
)
530 sLog
.outDebug("CMSG_MOVE_HOVER_ACK");
532 uint64 guid
; // guid - unused
533 if(!recv_data
.readPackGUID(guid
))
536 recv_data
.read_skip
<uint32
>(); // unk
538 MovementInfo
movementInfo(recv_data
);
540 recv_data
.read_skip
<uint32
>(); // unk2
543 void WorldSession::HandleMoveWaterWalkAck(WorldPacket
& recv_data
)
545 sLog
.outDebug("CMSG_MOVE_WATER_WALK_ACK");
547 uint64 guid
; // guid - unused
548 if(!recv_data
.readPackGUID(guid
))
551 recv_data
.read_skip
<uint32
>(); // unk
553 MovementInfo
movementInfo(recv_data
);
555 recv_data
.read_skip
<uint32
>(); // unk2
558 void WorldSession::HandleSummonResponseOpcode(WorldPacket
& recv_data
)
560 if(!_player
->isAlive() || _player
->isInCombat() )
563 uint64 summoner_guid
;
565 recv_data
>> summoner_guid
;
568 _player
->SummonIfPossible(agree
);