[7986] MaNGOS 0.13 release.
[getmangos.git] / src / game / MovementHandler.cpp
blobde51a14733f457721f49c5d83f37f3c4fafd554e
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.x,loc.y,loc.z,loc.o))
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()->SetMapId(loc.mapid);
69 GetPlayer()->Relocate(loc.x, loc.y, loc.z, loc.o);
71 // since the MapId is set before the GetInstance call, the InstanceId must be set to 0
72 // to let GetInstance() determine the proper InstanceId based on the player's binds
73 GetPlayer()->SetInstanceId(0);
75 // check this before Map::Add(player), because that will create the instance save!
76 bool reset_notify = (GetPlayer()->GetBoundInstance(GetPlayer()->GetMapId(), GetPlayer()->GetDifficulty()) == NULL);
78 GetPlayer()->SendInitialPacketsBeforeAddToMap();
79 // the CanEnter checks are done in TeleporTo but conditions may change
80 // while the player is in transit, for example the map may get full
81 if(!GetPlayer()->GetMap()->Add(GetPlayer()))
83 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);
84 // teleport the player home
85 if(!GetPlayer()->TeleportTo(GetPlayer()->m_homebindMapId, GetPlayer()->m_homebindX, GetPlayer()->m_homebindY, GetPlayer()->m_homebindZ, GetPlayer()->GetOrientation()))
87 // the player must always be able to teleport home
88 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());
89 assert(false);
91 return;
94 // battleground state prepare (in case join to BG), at relogin/tele player not invited
95 // only add to bg group and object, if the player was invited (else he entered through command)
96 if(_player->InBattleGround())
98 // cleanup seting if outdated
99 if(!mEntry->IsBattleGroundOrArena())
101 _player->SetBattleGroundId(0, BATTLEGROUND_TYPE_NONE); // We're not in BG.
102 // reset destination bg team
103 _player->SetBGTeam(0);
105 // join to bg case
106 else if(BattleGround *bg = _player->GetBattleGround())
108 if(_player->IsInvitedForBattleGroundInstance(_player->GetBattleGroundId()))
109 bg->AddPlayer(_player);
113 GetPlayer()->SendInitialPacketsAfterAddToMap();
115 // flight fast teleport case
116 if(GetPlayer()->GetMotionMaster()->GetCurrentMovementGeneratorType()==FLIGHT_MOTION_TYPE)
118 if(!_player->InBattleGround())
120 // short preparations to continue flight
121 FlightPathMovementGenerator* flight = (FlightPathMovementGenerator*)(GetPlayer()->GetMotionMaster()->top());
122 flight->Initialize(*GetPlayer());
123 return;
126 // battleground state prepare, stop flight
127 GetPlayer()->GetMotionMaster()->MovementExpired();
128 GetPlayer()->m_taxi.ClearTaxiDestinations();
131 // resurrect character at enter into instance where his corpse exist after add to map
132 Corpse *corpse = GetPlayer()->GetCorpse();
133 if (corpse && corpse->GetType() != CORPSE_BONES && corpse->GetMapId() == GetPlayer()->GetMapId())
135 if( mEntry->IsDungeon() )
137 GetPlayer()->ResurrectPlayer(0.5f);
138 GetPlayer()->SpawnCorpseBones();
139 GetPlayer()->SaveToDB();
143 if(mEntry->IsRaid() && mInstance)
145 if(reset_notify)
147 uint32 timeleft = sInstanceSaveManager.GetResetTimeFor(GetPlayer()->GetMapId()) - time(NULL);
148 GetPlayer()->SendInstanceResetWarning(GetPlayer()->GetMapId(), timeleft); // greeting at the entrance of the resort raid instance
152 // mount allow check
153 if(!mEntry->IsMountAllowed())
154 _player->RemoveSpellsCausingAura(SPELL_AURA_MOUNTED);
156 // honorless target
157 if(GetPlayer()->pvpInfo.inHostileArea)
158 GetPlayer()->CastSpell(GetPlayer(), 2479, true);
160 // resummon pet
161 GetPlayer()->ResummonPetTemporaryUnSummonedIfAny();
164 void WorldSession::HandleMoveTeleportAck(WorldPacket& recv_data)
166 CHECK_PACKET_SIZE(recv_data,8+4);
168 sLog.outDebug("MSG_MOVE_TELEPORT_ACK");
169 uint64 guid;
170 uint32 flags, time;
172 recv_data >> guid;
173 recv_data >> flags >> time;
174 DEBUG_LOG("Guid " UI64FMTD,guid);
175 DEBUG_LOG("Flags %u, time %u",flags, time/IN_MILISECONDS);
177 Unit *mover = _player->m_mover;
178 Player *plMover = mover->GetTypeId()==TYPEID_PLAYER ? (Player*)mover : NULL;
180 if(!plMover || !plMover->IsBeingTeleportedNear())
181 return;
183 if(guid != plMover->GetGUID())
184 return;
186 plMover->SetSemaphoreTeleportNear(false);
188 uint32 old_zone = plMover->GetZoneId();
190 WorldLocation const& dest = plMover->GetTeleportDest();
192 plMover->SetPosition(dest.x, dest.y, dest.z, dest.o, true);
194 uint32 newzone, newarea;
195 plMover->GetZoneAndAreaId(newzone,newarea);
196 plMover->UpdateZone(newzone,newarea);
198 // new zone
199 if(old_zone != newzone)
201 // honorless target
202 if(plMover->pvpInfo.inHostileArea)
203 plMover->CastSpell(plMover, 2479, true);
206 // resummon pet
207 GetPlayer()->ResummonPetTemporaryUnSummonedIfAny();
210 void WorldSession::HandleMovementOpcodes( WorldPacket & recv_data )
212 uint32 opcode = recv_data.GetOpcode();
213 sLog.outDebug("WORLD: Recvd %s (%u, 0x%X) opcode", LookupOpcodeName(opcode), opcode, opcode);
215 Unit *mover = _player->m_mover;
216 Player *plMover = mover->GetTypeId()==TYPEID_PLAYER ? (Player*)mover : NULL;
218 // ignore, waiting processing in WorldSession::HandleMoveWorldportAckOpcode and WorldSession::HandleMoveTeleportAck
219 if(plMover && plMover->IsBeingTeleported())
220 return;
222 /* extract packet */
223 MovementInfo movementInfo;
224 ReadMovementInfo(recv_data, &movementInfo);
225 /*----------------*/
227 if(recv_data.size() != recv_data.rpos())
229 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());
230 KickPlayer();
231 return;
234 if (!MaNGOS::IsValidMapCoord(movementInfo.x, movementInfo.y, movementInfo.z, movementInfo.o))
235 return;
237 /* handle special cases */
238 if (movementInfo.flags & MOVEMENTFLAG_ONTRANSPORT)
240 // transports size limited
241 // (also received at zeppelin leave by some reason with t_* as absolute in continent coordinates, can be safely skipped)
242 if( movementInfo.t_x > 50 || movementInfo.t_y > 50 || movementInfo.t_z > 50 )
243 return;
245 if( !MaNGOS::IsValidMapCoord(movementInfo.x+movementInfo.t_x, movementInfo.y+movementInfo.t_y,
246 movementInfo.z+movementInfo.t_z, movementInfo.o+movementInfo.t_o) )
247 return;
249 // if we boarded a transport, add us to it
250 if (plMover && !plMover->m_transport)
252 // elevators also cause the client to send MOVEMENTFLAG_ONTRANSPORT - just unmount if the guid can be found in the transport list
253 for (MapManager::TransportSet::const_iterator iter = MapManager::Instance().m_Transports.begin(); iter != MapManager::Instance().m_Transports.end(); ++iter)
255 if ((*iter)->GetGUID() == movementInfo.t_guid)
257 plMover->m_transport = (*iter);
258 (*iter)->AddPassenger(plMover);
259 break;
264 else if (plMover && plMover->m_transport) // if we were on a transport, leave
266 plMover->m_transport->RemovePassenger(plMover);
267 plMover->m_transport = NULL;
268 movementInfo.t_x = 0.0f;
269 movementInfo.t_y = 0.0f;
270 movementInfo.t_z = 0.0f;
271 movementInfo.t_o = 0.0f;
272 movementInfo.t_time = 0;
273 movementInfo.t_seat = -1;
276 // fall damage generation (ignore in flight case that can be triggered also at lags in moment teleportation to another map).
277 if (opcode == MSG_MOVE_FALL_LAND && plMover && !plMover->isInFlight())
278 plMover->HandleFall(movementInfo);
280 if (plMover && ((movementInfo.flags & MOVEMENTFLAG_SWIMMING) != 0) != plMover->IsInWater())
282 // now client not include swimming flag in case jumping under water
283 plMover->SetInWater( !plMover->IsInWater() || plMover->GetBaseMap()->IsUnderWater(movementInfo.x, movementInfo.y, movementInfo.z) );
286 /*----------------------*/
288 /* process position-change */
289 recv_data.put<uint32>(6, getMSTime()); // fix time, offset flags(4) + unk(2)
290 WorldPacket data(recv_data.GetOpcode(), (mover->GetPackGUID().size()+recv_data.size()));
291 data.append(mover->GetPackGUID()); // use mover guid
292 data.append(recv_data.contents(), recv_data.size());
293 GetPlayer()->SendMessageToSet(&data, false);
295 if(plMover) // nothing is charmed, or player charmed
297 plMover->SetPosition(movementInfo.x, movementInfo.y, movementInfo.z, movementInfo.o);
298 plMover->m_movementInfo = movementInfo;
299 plMover->SetUnitMovementFlags(movementInfo.flags);
300 plMover->UpdateFallInformationIfNeed(movementInfo,recv_data.GetOpcode());
302 if(plMover->isMovingOrTurning())
303 plMover->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
305 if(movementInfo.z < -500.0f)
307 if(plMover->InBattleGround()
308 && plMover->GetBattleGround()
309 && plMover->GetBattleGround()->HandlePlayerUnderMap(_player))
311 // do nothing, the handle already did if returned true
313 else
315 // NOTE: this is actually called many times while falling
316 // even after the player has been teleported away
317 // TODO: discard movement packets after the player is rooted
318 if(plMover->isAlive())
320 plMover->EnvironmentalDamage(DAMAGE_FALL_TO_VOID, GetPlayer()->GetMaxHealth());
321 // pl can be alive if GM/etc
322 if(!plMover->isAlive())
324 // change the death state to CORPSE to prevent the death timer from
325 // starting in the next player update
326 plMover->KillPlayer();
327 plMover->BuildPlayerRepop();
331 // cancel the death timer here if started
332 plMover->RepopAtGraveyard();
336 else // creature charmed
338 if(Map *map = mover->GetMap())
339 map->CreatureRelocation((Creature*)mover, movementInfo.x, movementInfo.y, movementInfo.z, movementInfo.o);
340 mover->SetUnitMovementFlags(movementInfo.flags);
344 void WorldSession::HandleForceSpeedChangeAck(WorldPacket &recv_data)
346 sLog.outDebug("WORLD: Recvd %s (%u, 0x%X) opcode", LookupOpcodeName(recv_data.GetOpcode()), recv_data.GetOpcode(), recv_data.GetOpcode());
348 CHECK_PACKET_SIZE(recv_data, recv_data.rpos()+8+4);
350 /* extract packet */
351 uint64 guid;
352 uint32 unk1;
353 float newspeed;
355 recv_data >> guid;
357 // now can skip not our packet
358 if(_player->GetGUID() != guid)
359 return;
361 // continue parse packet
363 recv_data >> unk1; // counter or moveEvent
365 MovementInfo movementInfo;
366 ReadMovementInfo(recv_data, &movementInfo);
368 // recheck
369 CHECK_PACKET_SIZE(recv_data, recv_data.rpos()+4);
371 recv_data >> newspeed;
372 /*----------------*/
374 // client ACK send one packet for mounted/run case and need skip all except last from its
375 // in other cases anti-cheat check can be fail in false case
376 UnitMoveType move_type;
377 UnitMoveType force_move_type;
379 static char const* move_type_name[MAX_MOVE_TYPE] = { "Walk", "Run", "RunBack", "Swim", "SwimBack", "TurnRate", "Flight", "FlightBack", "PitchRate" };
381 uint16 opcode = recv_data.GetOpcode();
382 switch(opcode)
384 case CMSG_FORCE_WALK_SPEED_CHANGE_ACK: move_type = MOVE_WALK; force_move_type = MOVE_WALK; break;
385 case CMSG_FORCE_RUN_SPEED_CHANGE_ACK: move_type = MOVE_RUN; force_move_type = MOVE_RUN; break;
386 case CMSG_FORCE_RUN_BACK_SPEED_CHANGE_ACK: move_type = MOVE_RUN_BACK; force_move_type = MOVE_RUN_BACK; break;
387 case CMSG_FORCE_SWIM_SPEED_CHANGE_ACK: move_type = MOVE_SWIM; force_move_type = MOVE_SWIM; break;
388 case CMSG_FORCE_SWIM_BACK_SPEED_CHANGE_ACK: move_type = MOVE_SWIM_BACK; force_move_type = MOVE_SWIM_BACK; break;
389 case CMSG_FORCE_TURN_RATE_CHANGE_ACK: move_type = MOVE_TURN_RATE; force_move_type = MOVE_TURN_RATE; break;
390 case CMSG_FORCE_FLIGHT_SPEED_CHANGE_ACK: move_type = MOVE_FLIGHT; force_move_type = MOVE_FLIGHT; break;
391 case CMSG_FORCE_FLIGHT_BACK_SPEED_CHANGE_ACK: move_type = MOVE_FLIGHT_BACK; force_move_type = MOVE_FLIGHT_BACK; break;
392 case CMSG_FORCE_PITCH_RATE_CHANGE_ACK: move_type = MOVE_PITCH_RATE; force_move_type = MOVE_PITCH_RATE; break;
393 default:
394 sLog.outError("WorldSession::HandleForceSpeedChangeAck: Unknown move type opcode: %u", opcode);
395 return;
398 // skip all forced speed changes except last and unexpected
399 // in run/mounted case used one ACK and it must be skipped.m_forced_speed_changes[MOVE_RUN} store both.
400 if(_player->m_forced_speed_changes[force_move_type] > 0)
402 --_player->m_forced_speed_changes[force_move_type];
403 if(_player->m_forced_speed_changes[force_move_type] > 0)
404 return;
407 if (!_player->GetTransport() && fabs(_player->GetSpeed(move_type) - newspeed) > 0.01f)
409 if(_player->GetSpeed(move_type) > newspeed) // must be greater - just correct
411 sLog.outError("%sSpeedChange player %s is NOT correct (must be %f instead %f), force set to correct value",
412 move_type_name[move_type], _player->GetName(), _player->GetSpeed(move_type), newspeed);
413 _player->SetSpeed(move_type,_player->GetSpeedRate(move_type),true);
415 else // must be lesser - cheating
417 sLog.outBasic("Player %s from account id %u kicked for incorrect speed (must be %f instead %f)",
418 _player->GetName(),_player->GetSession()->GetAccountId(),_player->GetSpeed(move_type), newspeed);
419 _player->GetSession()->KickPlayer();
424 void WorldSession::HandleSetActiveMoverOpcode(WorldPacket &recv_data)
426 sLog.outDebug("WORLD: Recvd CMSG_SET_ACTIVE_MOVER");
427 recv_data.hexlike();
429 CHECK_PACKET_SIZE(recv_data, 8);
431 uint64 guid;
432 recv_data >> guid;
434 if(_player->m_mover->GetGUID() != guid)
436 sLog.outError("HandleSetActiveMoverOpcode: incorrect mover guid: mover is " I64FMT " and should be " I64FMT, _player->m_mover->GetGUID(), guid);
437 return;
441 void WorldSession::HandleMoveNotActiveMover(WorldPacket &recv_data)
443 sLog.outDebug("WORLD: Recvd CMSG_MOVE_NOT_ACTIVE_MOVER");
444 recv_data.hexlike();
446 CHECK_PACKET_SIZE(recv_data, recv_data.rpos()+8);
448 uint64 old_mover_guid;
449 recv_data >> old_mover_guid;
451 if(_player->m_mover->GetGUID() == old_mover_guid)
453 sLog.outError("HandleMoveNotActiveMover: incorrect mover guid: mover is " I64FMT " and should be " I64FMT " instead of " I64FMT, _player->m_mover->GetGUID(), _player->GetGUID(), old_mover_guid);
454 return;
457 MovementInfo mi;
458 ReadMovementInfo(recv_data, &mi);
459 _player->m_movementInfo = mi;
462 void WorldSession::HandleDismissControlledVehicle(WorldPacket &recv_data)
464 sLog.outDebug("WORLD: Recvd CMSG_DISMISS_CONTROLLED_VEHICLE");
465 recv_data.hexlike();
467 uint64 vehicleGUID = _player->GetCharmGUID();
469 if(!vehicleGUID) // something wrong here...
470 return;
472 MovementInfo mi;
473 ReadMovementInfo(recv_data, &mi);
474 _player->m_movementInfo = mi;
476 // using charm guid, because we don't have vehicle guid...
477 if(Vehicle *vehicle = ObjectAccessor::GetVehicle(vehicleGUID))
479 // Aura::HandleAuraControlVehicle will call Player::ExitVehicle
480 vehicle->RemoveSpellsCausingAura(SPELL_AURA_CONTROL_VEHICLE);
484 void WorldSession::HandleMountSpecialAnimOpcode(WorldPacket& /*recvdata*/)
486 //sLog.outDebug("WORLD: Recvd CMSG_MOUNTSPECIAL_ANIM");
488 WorldPacket data(SMSG_MOUNTSPECIAL_ANIM, 8);
489 data << uint64(GetPlayer()->GetGUID());
491 GetPlayer()->SendMessageToSet(&data, false);
494 void WorldSession::HandleMoveKnockBackAck( WorldPacket & /*recv_data*/ )
496 // CHECK_PACKET_SIZE(recv_data,?);
497 sLog.outDebug("CMSG_MOVE_KNOCK_BACK_ACK");
498 // Currently not used but maybe use later for recheck final player position
499 // (must be at call same as into "recv_data >> x >> y >> z >> orientation;"
502 uint32 flags, time;
503 float x, y, z, orientation;
504 uint64 guid;
505 uint32 sequence;
506 uint32 ukn1;
507 float xdirection,ydirection,hspeed,vspeed;
509 recv_data >> guid;
510 recv_data >> sequence;
511 recv_data >> flags >> time;
512 recv_data >> x >> y >> z >> orientation;
513 recv_data >> ukn1; //unknown
514 recv_data >> vspeed >> xdirection >> ydirection >> hspeed;
516 // skip not personal message;
517 if(GetPlayer()->GetGUID()!=guid)
518 return;
520 // check code
524 void WorldSession::HandleMoveHoverAck( WorldPacket& /*recv_data*/ )
526 sLog.outDebug("CMSG_MOVE_HOVER_ACK");
529 void WorldSession::HandleMoveWaterWalkAck(WorldPacket& /*recv_data*/)
531 sLog.outDebug("CMSG_MOVE_WATER_WALK_ACK");
534 void WorldSession::HandleSummonResponseOpcode(WorldPacket& recv_data)
536 CHECK_PACKET_SIZE(recv_data,8+1);
538 if(!_player->isAlive() || _player->isInCombat() )
539 return;
541 uint64 summoner_guid;
542 bool agree;
543 recv_data >> summoner_guid;
544 recv_data >> agree;
546 _player->SummonIfPossible(agree);