[8483] Implement glyph 43361.
[getmangos.git] / src / game / MovementHandler.cpp
bloba3bba87aeb34acc8fca6351314624afe1ff741d4
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "WorldPacket.h"
21 #include "WorldSession.h"
22 #include "Opcodes.h"
23 #include "Log.h"
24 #include "Corpse.h"
25 #include "Player.h"
26 #include "Vehicle.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())
45 return;
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 LogoutPlayer(false);
54 return;
57 // get the destination map entry, not the current one, this will fix homebind and reset greeting
58 MapEntry const* mEntry = sMapStore.LookupEntry(loc.mapid);
59 InstanceTemplate const* mInstance = objmgr.GetInstanceTemplate(loc.mapid);
61 // reset instance validity, except if going to an instance inside an instance
62 if(GetPlayer()->m_InstanceValid == false && !mInstance)
63 GetPlayer()->m_InstanceValid = true;
65 GetPlayer()->SetSemaphoreTeleportFar(false);
67 // relocate the player to the teleport destination
68 GetPlayer()->SetMap(MapManager::Instance().CreateMap(loc.mapid, GetPlayer())); GetPlayer()->Relocate(loc.coord_x, loc.coord_y, loc.coord_z, loc.orientation);
69 GetPlayer()->Relocate(loc.coord_x, loc.coord_y, loc.coord_z, loc.orientation);
71 GetPlayer()->SendInitialPacketsBeforeAddToMap();
72 // the CanEnter checks are done in TeleporTo but conditions may change
73 // while the player is in transit, for example the map may get full
74 if(!GetPlayer()->GetMap()->Add(GetPlayer()))
76 //if player wasn't added to map, reset his map pointer!
77 GetPlayer()->ResetMap();
79 sLog.outDebug("WORLD: teleport of player %s (%d) to location %d, %f, %f, %f, %f failed", GetPlayer()->GetName(), GetPlayer()->GetGUIDLow(), loc.mapid, loc.coord_x, loc.coord_y, loc.coord_z, loc.orientation);
80 // teleport the player home
81 if(!GetPlayer()->TeleportTo(GetPlayer()->m_homebindMapId, GetPlayer()->m_homebindX, GetPlayer()->m_homebindY, GetPlayer()->m_homebindZ, GetPlayer()->GetOrientation()))
83 // the player must always be able to teleport home
84 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());
85 assert(false);
87 return;
90 // battleground state prepare (in case join to BG), at relogin/tele player not invited
91 // only add to bg group and object, if the player was invited (else he entered through command)
92 if(_player->InBattleGround())
94 // cleanup setting if outdated
95 if(!mEntry->IsBattleGroundOrArena())
97 // We're not in BG
98 _player->SetBattleGroundId(0, BATTLEGROUND_TYPE_NONE);
99 // reset destination bg team
100 _player->SetBGTeam(0);
102 // join to bg case
103 else if(BattleGround *bg = _player->GetBattleGround())
105 if(_player->IsInvitedForBattleGroundInstance(_player->GetBattleGroundId()))
106 bg->AddPlayer(_player);
110 GetPlayer()->SendInitialPacketsAfterAddToMap();
112 // flight fast teleport case
113 if(GetPlayer()->GetMotionMaster()->GetCurrentMovementGeneratorType() == FLIGHT_MOTION_TYPE)
115 if(!_player->InBattleGround())
117 // short preparations to continue flight
118 FlightPathMovementGenerator* flight = (FlightPathMovementGenerator*)(GetPlayer()->GetMotionMaster()->top());
119 flight->Initialize(*GetPlayer());
120 return;
123 // battleground state prepare, stop flight
124 GetPlayer()->GetMotionMaster()->MovementExpired();
125 GetPlayer()->m_taxi.ClearTaxiDestinations();
128 // resurrect character at enter into instance where his corpse exist after add to map
129 Corpse *corpse = GetPlayer()->GetCorpse();
130 if (corpse && corpse->GetType() != CORPSE_BONES && corpse->GetMapId() == GetPlayer()->GetMapId())
132 if( mEntry->IsDungeon() )
134 GetPlayer()->ResurrectPlayer(0.5f);
135 GetPlayer()->SpawnCorpseBones();
136 GetPlayer()->SaveToDB();
140 if((mEntry->IsRaid() || (mEntry->IsNonRaidDungeon() && mEntry->SupportsHeroicMode() && GetPlayer()->IsHeroic())) && mInstance)
142 uint32 timeleft = sInstanceSaveManager.GetResetTimeFor(GetPlayer()->GetMapId()) - time(NULL);
143 GetPlayer()->SendInstanceResetWarning(GetPlayer()->GetMapId(), GetPlayer()->GetDifficulty(), timeleft);
146 // mount allow check
147 if(!mEntry->IsMountAllowed())
148 _player->RemoveSpellsCausingAura(SPELL_AURA_MOUNTED);
150 // honorless target
151 if(GetPlayer()->pvpInfo.inHostileArea)
152 GetPlayer()->CastSpell(GetPlayer(), 2479, true);
154 // resummon pet
155 GetPlayer()->ResummonPetTemporaryUnSummonedIfAny();
157 //lets process all delayed operations on successful teleport
158 GetPlayer()->ProcessDelayedOperations();
161 void WorldSession::HandleMoveTeleportAck(WorldPacket& recv_data)
163 sLog.outDebug("MSG_MOVE_TELEPORT_ACK");
164 uint64 guid;
165 uint32 flags, time;
167 recv_data >> guid;
168 recv_data >> flags >> time;
169 DEBUG_LOG("Guid " UI64FMTD, guid);
170 DEBUG_LOG("Flags %u, time %u", flags, time/IN_MILISECONDS);
172 Unit *mover = _player->m_mover;
173 Player *plMover = mover->GetTypeId() == TYPEID_PLAYER ? (Player*)mover : NULL;
175 if(!plMover || !plMover->IsBeingTeleportedNear())
176 return;
178 if(guid != plMover->GetGUID())
179 return;
181 plMover->SetSemaphoreTeleportNear(false);
183 uint32 old_zone = plMover->GetZoneId();
185 WorldLocation const& dest = plMover->GetTeleportDest();
187 plMover->SetPosition(dest.coord_x, dest.coord_y, dest.coord_z, dest.orientation, true);
189 uint32 newzone, newarea;
190 plMover->GetZoneAndAreaId(newzone, newarea);
191 plMover->UpdateZone(newzone, newarea);
193 // new zone
194 if(old_zone != newzone)
196 // honorless target
197 if(plMover->pvpInfo.inHostileArea)
198 plMover->CastSpell(plMover, 2479, true);
201 // resummon pet
202 GetPlayer()->ResummonPetTemporaryUnSummonedIfAny();
204 //lets process all delayed operations on successful teleport
205 GetPlayer()->ProcessDelayedOperations();
208 void WorldSession::HandleMovementOpcodes( WorldPacket & recv_data )
210 uint32 opcode = recv_data.GetOpcode();
211 sLog.outDebug("WORLD: Recvd %s (%u, 0x%X) opcode", LookupOpcodeName(opcode), opcode, opcode);
213 Unit *mover = _player->m_mover;
214 Player *plMover = mover->GetTypeId()==TYPEID_PLAYER ? (Player*)mover : NULL;
216 // ignore, waiting processing in WorldSession::HandleMoveWorldportAckOpcode and WorldSession::HandleMoveTeleportAck
217 if(plMover && plMover->IsBeingTeleported())
219 recv_data.rpos(recv_data.wpos()); // prevent warnings spam
220 return;
223 /* extract packet */
224 MovementInfo movementInfo;
225 ReadMovementInfo(recv_data, &movementInfo);
226 /*----------------*/
228 if(recv_data.size() != recv_data.rpos())
230 sLog.outError("MovementHandler: player %s (guid %d, account %u) sent a packet (opcode %u) that is " SIZEFMTD " 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());
231 KickPlayer();
232 recv_data.rpos(recv_data.wpos()); // prevent warnings spam
233 return;
236 if (!MaNGOS::IsValidMapCoord(movementInfo.x, movementInfo.y, movementInfo.z, movementInfo.o))
238 recv_data.rpos(recv_data.wpos()); // prevent warnings spam
239 return;
242 /* handle special cases */
243 if (movementInfo.HasMovementFlag(MOVEMENTFLAG_ONTRANSPORT))
245 // transports size limited
246 // (also received at zeppelin leave by some reason with t_* as absolute in continent coordinates, can be safely skipped)
247 if( movementInfo.t_x > 50 || movementInfo.t_y > 50 || movementInfo.t_z > 50 )
249 recv_data.rpos(recv_data.wpos()); // prevent warnings spam
250 return;
253 if( !MaNGOS::IsValidMapCoord(movementInfo.x+movementInfo.t_x, movementInfo.y + movementInfo.t_y,
254 movementInfo.z + movementInfo.t_z, movementInfo.o + movementInfo.t_o) )
256 recv_data.rpos(recv_data.wpos()); // prevent warnings spam
257 return;
260 // if we boarded a transport, add us to it
261 if (plMover && !plMover->m_transport)
263 // elevators also cause the client to send MOVEMENTFLAG_ONTRANSPORT - just unmount if the guid can be found in the transport list
264 for (MapManager::TransportSet::const_iterator iter = MapManager::Instance().m_Transports.begin(); iter != MapManager::Instance().m_Transports.end(); ++iter)
266 if ((*iter)->GetGUID() == movementInfo.t_guid)
268 plMover->m_transport = (*iter);
269 (*iter)->AddPassenger(plMover);
270 break;
275 else if (plMover && plMover->m_transport) // if we were on a transport, leave
277 plMover->m_transport->RemovePassenger(plMover);
278 plMover->m_transport = NULL;
279 movementInfo.t_x = 0.0f;
280 movementInfo.t_y = 0.0f;
281 movementInfo.t_z = 0.0f;
282 movementInfo.t_o = 0.0f;
283 movementInfo.t_time = 0;
284 movementInfo.t_seat = -1;
287 // fall damage generation (ignore in flight case that can be triggered also at lags in moment teleportation to another map).
288 if (opcode == MSG_MOVE_FALL_LAND && plMover && !plMover->isInFlight())
289 plMover->HandleFall(movementInfo);
291 if (plMover && (movementInfo.HasMovementFlag(MOVEMENTFLAG_SWIMMING) != plMover->IsInWater()))
293 // now client not include swimming flag in case jumping under water
294 plMover->SetInWater( !plMover->IsInWater() || plMover->GetBaseMap()->IsUnderWater(movementInfo.x, movementInfo.y, movementInfo.z) );
297 /*----------------------*/
299 /* process position-change */
300 recv_data.put<uint32>(6, getMSTime()); // fix time, offset flags(4) + unk(2)
301 WorldPacket data(recv_data.GetOpcode(), (mover->GetPackGUID().size()+recv_data.size()));
302 data.append(mover->GetPackGUID()); // use mover guid
303 data.append(recv_data.contents(), recv_data.size());
304 GetPlayer()->SendMessageToSet(&data, false);
306 if(plMover) // nothing is charmed, or player charmed
308 plMover->SetPosition(movementInfo.x, movementInfo.y, movementInfo.z, movementInfo.o);
309 plMover->m_movementInfo = movementInfo;
310 plMover->UpdateFallInformationIfNeed(movementInfo, recv_data.GetOpcode());
312 if(plMover->isMovingOrTurning())
313 plMover->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
315 if(movementInfo.z < -500.0f)
317 if(plMover->InBattleGround()
318 && plMover->GetBattleGround()
319 && plMover->GetBattleGround()->HandlePlayerUnderMap(_player))
321 // do nothing, the handle already did if returned true
323 else
325 // NOTE: this is actually called many times while falling
326 // even after the player has been teleported away
327 // TODO: discard movement packets after the player is rooted
328 if(plMover->isAlive())
330 plMover->EnvironmentalDamage(DAMAGE_FALL_TO_VOID, GetPlayer()->GetMaxHealth());
331 // pl can be alive if GM/etc
332 if(!plMover->isAlive())
334 // change the death state to CORPSE to prevent the death timer from
335 // starting in the next player update
336 plMover->KillPlayer();
337 plMover->BuildPlayerRepop();
341 // cancel the death timer here if started
342 plMover->RepopAtGraveyard();
346 else // creature charmed
348 if(mover->IsInWorld())
349 mover->GetMap()->CreatureRelocation((Creature*)mover, movementInfo.x, movementInfo.y, movementInfo.z, movementInfo.o);
353 void WorldSession::HandleForceSpeedChangeAck(WorldPacket &recv_data)
355 sLog.outDebug("WORLD: Recvd %s (%u, 0x%X) opcode", LookupOpcodeName(recv_data.GetOpcode()), recv_data.GetOpcode(), recv_data.GetOpcode());
357 /* extract packet */
358 uint64 guid;
359 uint32 unk1;
360 float newspeed;
362 recv_data >> guid;
364 // now can skip not our packet
365 if(_player->GetGUID() != guid)
367 recv_data.rpos(recv_data.wpos()); // prevent warnings spam
368 return;
371 // continue parse packet
373 recv_data >> unk1; // counter or moveEvent
375 MovementInfo movementInfo;
376 ReadMovementInfo(recv_data, &movementInfo);
378 recv_data >> newspeed;
379 /*----------------*/
381 // client ACK send one packet for mounted/run case and need skip all except last from its
382 // in other cases anti-cheat check can be fail in false case
383 UnitMoveType move_type;
384 UnitMoveType force_move_type;
386 static char const* move_type_name[MAX_MOVE_TYPE] = { "Walk", "Run", "RunBack", "Swim", "SwimBack", "TurnRate", "Flight", "FlightBack", "PitchRate" };
388 uint16 opcode = recv_data.GetOpcode();
389 switch(opcode)
391 case CMSG_FORCE_WALK_SPEED_CHANGE_ACK: move_type = MOVE_WALK; force_move_type = MOVE_WALK; break;
392 case CMSG_FORCE_RUN_SPEED_CHANGE_ACK: move_type = MOVE_RUN; force_move_type = MOVE_RUN; break;
393 case CMSG_FORCE_RUN_BACK_SPEED_CHANGE_ACK: move_type = MOVE_RUN_BACK; force_move_type = MOVE_RUN_BACK; break;
394 case CMSG_FORCE_SWIM_SPEED_CHANGE_ACK: move_type = MOVE_SWIM; force_move_type = MOVE_SWIM; break;
395 case CMSG_FORCE_SWIM_BACK_SPEED_CHANGE_ACK: move_type = MOVE_SWIM_BACK; force_move_type = MOVE_SWIM_BACK; break;
396 case CMSG_FORCE_TURN_RATE_CHANGE_ACK: move_type = MOVE_TURN_RATE; force_move_type = MOVE_TURN_RATE; break;
397 case CMSG_FORCE_FLIGHT_SPEED_CHANGE_ACK: move_type = MOVE_FLIGHT; force_move_type = MOVE_FLIGHT; break;
398 case CMSG_FORCE_FLIGHT_BACK_SPEED_CHANGE_ACK: move_type = MOVE_FLIGHT_BACK; force_move_type = MOVE_FLIGHT_BACK; break;
399 case CMSG_FORCE_PITCH_RATE_CHANGE_ACK: move_type = MOVE_PITCH_RATE; force_move_type = MOVE_PITCH_RATE; break;
400 default:
401 sLog.outError("WorldSession::HandleForceSpeedChangeAck: Unknown move type opcode: %u", opcode);
402 return;
405 // skip all forced speed changes except last and unexpected
406 // in run/mounted case used one ACK and it must be skipped.m_forced_speed_changes[MOVE_RUN} store both.
407 if(_player->m_forced_speed_changes[force_move_type] > 0)
409 --_player->m_forced_speed_changes[force_move_type];
410 if(_player->m_forced_speed_changes[force_move_type] > 0)
411 return;
414 if (!_player->GetTransport() && fabs(_player->GetSpeed(move_type) - newspeed) > 0.01f)
416 if(_player->GetSpeed(move_type) > newspeed) // must be greater - just correct
418 sLog.outError("%sSpeedChange player %s is NOT correct (must be %f instead %f), force set to correct value",
419 move_type_name[move_type], _player->GetName(), _player->GetSpeed(move_type), newspeed);
420 _player->SetSpeed(move_type,_player->GetSpeedRate(move_type),true);
422 else // must be lesser - cheating
424 sLog.outBasic("Player %s from account id %u kicked for incorrect speed (must be %f instead %f)",
425 _player->GetName(),_player->GetSession()->GetAccountId(),_player->GetSpeed(move_type), newspeed);
426 _player->GetSession()->KickPlayer();
431 void WorldSession::HandleSetActiveMoverOpcode(WorldPacket &recv_data)
433 sLog.outDebug("WORLD: Recvd CMSG_SET_ACTIVE_MOVER");
434 recv_data.hexlike();
436 uint64 guid;
437 recv_data >> guid;
439 if(_player->m_mover->GetGUID() != guid)
441 sLog.outError("HandleSetActiveMoverOpcode: incorrect mover guid: mover is " I64FMT " and should be " I64FMT, _player->m_mover->GetGUID(), guid);
442 return;
446 void WorldSession::HandleMoveNotActiveMover(WorldPacket &recv_data)
448 sLog.outDebug("WORLD: Recvd CMSG_MOVE_NOT_ACTIVE_MOVER");
449 recv_data.hexlike();
451 uint64 old_mover_guid;
452 recv_data >> old_mover_guid;
454 if(_player->m_mover->GetGUID() == old_mover_guid)
456 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);
457 recv_data.rpos(recv_data.wpos()); // prevent warnings spam
458 return;
461 MovementInfo mi;
462 ReadMovementInfo(recv_data, &mi);
463 _player->m_movementInfo = mi;
466 void WorldSession::HandleDismissControlledVehicle(WorldPacket &recv_data)
468 sLog.outDebug("WORLD: Recvd CMSG_DISMISS_CONTROLLED_VEHICLE");
469 recv_data.hexlike();
471 uint64 vehicleGUID = _player->GetCharmGUID();
473 if(!vehicleGUID) // something wrong here...
475 recv_data.rpos(recv_data.wpos()); // prevent warnings spam
476 return;
479 MovementInfo mi;
480 ReadMovementInfo(recv_data, &mi);
481 _player->m_movementInfo = mi;
483 // using charm guid, because we don't have vehicle guid...
484 if(Vehicle *vehicle = ObjectAccessor::GetVehicle(vehicleGUID))
486 // Aura::HandleAuraControlVehicle will call Player::ExitVehicle
487 vehicle->RemoveSpellsCausingAura(SPELL_AURA_CONTROL_VEHICLE);
491 void WorldSession::HandleMountSpecialAnimOpcode(WorldPacket& /*recvdata*/)
493 //sLog.outDebug("WORLD: Recvd CMSG_MOUNTSPECIAL_ANIM");
495 WorldPacket data(SMSG_MOUNTSPECIAL_ANIM, 8);
496 data << uint64(GetPlayer()->GetGUID());
498 GetPlayer()->SendMessageToSet(&data, false);
501 void WorldSession::HandleMoveKnockBackAck( WorldPacket & recv_data )
503 sLog.outDebug("CMSG_MOVE_KNOCK_BACK_ACK");
505 recv_data.read_skip<uint64>(); // guid
506 recv_data.read_skip<uint32>(); // unk
508 MovementInfo movementInfo;
509 ReadMovementInfo(recv_data, &movementInfo);
512 void WorldSession::HandleMoveHoverAck( WorldPacket& recv_data )
514 sLog.outDebug("CMSG_MOVE_HOVER_ACK");
516 recv_data.read_skip<uint64>(); // guid
517 recv_data.read_skip<uint32>(); // unk
519 MovementInfo movementInfo;
520 ReadMovementInfo(recv_data, &movementInfo);
522 recv_data.read_skip<uint32>(); // unk2
525 void WorldSession::HandleMoveWaterWalkAck(WorldPacket& recv_data)
527 sLog.outDebug("CMSG_MOVE_WATER_WALK_ACK");
529 recv_data.read_skip<uint64>(); // guid
530 recv_data.read_skip<uint32>(); // unk
532 MovementInfo movementInfo;
533 ReadMovementInfo(recv_data, &movementInfo);
535 recv_data.read_skip<uint32>(); // unk2
538 void WorldSession::HandleSummonResponseOpcode(WorldPacket& recv_data)
540 if(!_player->isAlive() || _player->isInCombat() )
541 return;
543 uint64 summoner_guid;
544 bool agree;
545 recv_data >> summoner_guid;
546 recv_data >> agree;
548 _player->SummonIfPossible(agree);