[7525] Prevent crashes at death controlled (including movement) creature.
[AHbot.git] / src / game / MovementHandler.cpp
blob65c3670a149b2cfde1d0ddc67acfad9f6b1024ad
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 "MapManager.h"
28 #include "Transports.h"
29 #include "BattleGround.h"
30 #include "WaypointMovementGenerator.h"
31 #include "InstanceSaveMgr.h"
32 #include "ObjectMgr.h"
34 void WorldSession::HandleMoveWorldportAckOpcode( WorldPacket & /*recv_data*/ )
36 sLog.outDebug( "WORLD: got MSG_MOVE_WORLDPORT_ACK." );
37 HandleMoveWorldportAckOpcode();
40 void WorldSession::HandleMoveWorldportAckOpcode()
42 // get the teleport destination
43 WorldLocation &loc = GetPlayer()->GetTeleportDest();
45 // possible errors in the coordinate validity check
46 if(!MapManager::IsValidMapCoord(loc.mapid,loc.x,loc.y,loc.z,loc.o))
48 LogoutPlayer(false);
49 return;
52 // get the destination map entry, not the current one, this will fix homebind and reset greeting
53 MapEntry const* mEntry = sMapStore.LookupEntry(loc.mapid);
54 InstanceTemplate const* mInstance = objmgr.GetInstanceTemplate(loc.mapid);
56 // reset instance validity, except if going to an instance inside an instance
57 if(GetPlayer()->m_InstanceValid == false && !mInstance)
58 GetPlayer()->m_InstanceValid = true;
60 GetPlayer()->SetSemaphoreTeleport(false);
62 // relocate the player to the teleport destination
63 GetPlayer()->SetMapId(loc.mapid);
64 GetPlayer()->Relocate(loc.x, loc.y, loc.z, loc.o);
66 // since the MapId is set before the GetInstance call, the InstanceId must be set to 0
67 // to let GetInstance() determine the proper InstanceId based on the player's binds
68 GetPlayer()->SetInstanceId(0);
70 // check this before Map::Add(player), because that will create the instance save!
71 bool reset_notify = (GetPlayer()->GetBoundInstance(GetPlayer()->GetMapId(), GetPlayer()->GetDifficulty()) == NULL);
73 GetPlayer()->SendInitialPacketsBeforeAddToMap();
74 // the CanEnter checks are done in TeleporTo but conditions may change
75 // while the player is in transit, for example the map may get full
76 if(!GetPlayer()->GetMap()->Add(GetPlayer()))
78 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);
79 // teleport the player home
80 GetPlayer()->SetDontMove(false);
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 seting if outdated
95 if(!mEntry->IsBattleGroundOrArena())
97 _player->SetBattleGroundId(0, BATTLEGROUND_TYPE_NONE); // We're not in BG.
98 // reset destination bg team
99 _player->SetBGTeam(0);
101 // join to bg case
102 else if(BattleGround *bg = _player->GetBattleGround())
104 if(_player->IsInvitedForBattleGroundInstance(_player->GetBattleGroundId()))
105 bg->AddPlayer(_player);
109 GetPlayer()->SendInitialPacketsAfterAddToMap();
111 // flight fast teleport case
112 if(GetPlayer()->GetMotionMaster()->GetCurrentMovementGeneratorType()==FLIGHT_MOTION_TYPE)
114 if(!_player->InBattleGround())
116 // short preparations to continue flight
117 GetPlayer()->SetDontMove(false);
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() && mInstance)
142 if(reset_notify)
144 uint32 timeleft = sInstanceSaveManager.GetResetTimeFor(GetPlayer()->GetMapId()) - time(NULL);
145 GetPlayer()->SendInstanceResetWarning(GetPlayer()->GetMapId(), timeleft); // greeting at the entrance of the resort raid instance
149 // mount allow check
150 if(!mEntry->IsMountAllowed())
151 _player->RemoveSpellsCausingAura(SPELL_AURA_MOUNTED);
153 // honorless target
154 if(GetPlayer()->pvpInfo.inHostileArea)
155 GetPlayer()->CastSpell(GetPlayer(), 2479, true);
157 // resummon pet
158 if(GetPlayer()->m_temporaryUnsummonedPetNumber)
160 Pet* NewPet = new Pet;
161 if(!NewPet->LoadPetFromDB(GetPlayer(), 0, GetPlayer()->m_temporaryUnsummonedPetNumber, true))
162 delete NewPet;
164 GetPlayer()->m_temporaryUnsummonedPetNumber = 0;
167 GetPlayer()->SetDontMove(false);
170 void WorldSession::HandleMovementOpcodes( WorldPacket & recv_data )
172 uint32 opcode = recv_data.GetOpcode();
173 sLog.outDebug("WORLD: Recvd %s (%u, 0x%X) opcode", LookupOpcodeName(opcode), opcode, opcode);
175 if(GetPlayer()->GetDontMove())
176 return;
178 /* extract packet */
179 MovementInfo movementInfo;
180 ReadMovementInfo(recv_data, &movementInfo);
181 /*----------------*/
183 if(recv_data.size() != recv_data.rpos())
185 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());
186 KickPlayer();
187 return;
190 if (!MaNGOS::IsValidMapCoord(movementInfo.x, movementInfo.y, movementInfo.z, movementInfo.o))
191 return;
193 /* handle special cases */
194 if (movementInfo.flags & MOVEMENTFLAG_ONTRANSPORT)
196 // transports size limited
197 // (also received at zeppelin leave by some reason with t_* as absolute in continent coordinates, can be safely skipped)
198 if( movementInfo.t_x > 50 || movementInfo.t_y > 50 || movementInfo.t_z > 50 )
199 return;
201 if( !MaNGOS::IsValidMapCoord(movementInfo.x+movementInfo.t_x, movementInfo.y+movementInfo.t_y,
202 movementInfo.z+movementInfo.t_z, movementInfo.o+movementInfo.t_o) )
203 return;
205 // if we boarded a transport, add us to it
206 if (!GetPlayer()->m_transport)
208 // elevators also cause the client to send MOVEMENTFLAG_ONTRANSPORT - just unmount if the guid can be found in the transport list
209 for (MapManager::TransportSet::iterator iter = MapManager::Instance().m_Transports.begin(); iter != MapManager::Instance().m_Transports.end(); ++iter)
211 if ((*iter)->GetGUID() == movementInfo.t_guid)
213 GetPlayer()->m_transport = (*iter);
214 (*iter)->AddPassenger(GetPlayer());
215 break;
220 else if (GetPlayer()->m_transport) // if we were on a transport, leave
222 GetPlayer()->m_transport->RemovePassenger(GetPlayer());
223 GetPlayer()->m_transport = NULL;
224 movementInfo.t_x = 0.0f;
225 movementInfo.t_y = 0.0f;
226 movementInfo.t_z = 0.0f;
227 movementInfo.t_o = 0.0f;
228 movementInfo.t_time = 0;
229 movementInfo.t_seat = -1;
232 // fall damage generation (ignore in flight case that can be triggered also at lags in moment teleportation to another map).
233 if (opcode == MSG_MOVE_FALL_LAND && !GetPlayer()->isInFlight())
234 GetPlayer()->HandleFall(movementInfo);
236 if(((movementInfo.flags & MOVEMENTFLAG_SWIMMING) != 0) != GetPlayer()->IsInWater())
238 // now client not include swimming flag in case jumping under water
239 GetPlayer()->SetInWater( !GetPlayer()->IsInWater() || GetPlayer()->GetBaseMap()->IsUnderWater(movementInfo.x, movementInfo.y, movementInfo.z) );
242 /*----------------------*/
244 /* process position-change */
245 Unit *mover = _player->m_mover;
246 recv_data.put<uint32>(6, getMSTime()); // fix time, offset flags(4) + unk(2)
247 WorldPacket data(recv_data.GetOpcode(), (mover->GetPackGUID().size()+recv_data.size()));
248 data.append(mover->GetPackGUID()); // use mover guid
249 data.append(recv_data.contents(), recv_data.size());
250 GetPlayer()->SendMessageToSet(&data, false);
252 if(!_player->GetCharmGUID()) // nothing is charmed
254 _player->SetPosition(movementInfo.x, movementInfo.y, movementInfo.z, movementInfo.o);
255 _player->m_movementInfo = movementInfo;
256 _player->SetUnitMovementFlags(movementInfo.flags);
258 else
260 if(mover->GetTypeId() != TYPEID_PLAYER) // unit, creature, pet, vehicle...
262 if(Map *map = mover->GetMap())
263 map->CreatureRelocation((Creature*)mover, movementInfo.x, movementInfo.y, movementInfo.z, movementInfo.o);
264 mover->SetUnitMovementFlags(movementInfo.flags);
266 else // player
268 ((Player*)mover)->SetPosition(movementInfo.x, movementInfo.y, movementInfo.z, movementInfo.o);
269 ((Player*)mover)->m_movementInfo = movementInfo;
270 ((Player*)mover)->SetUnitMovementFlags(movementInfo.flags);
274 if (GetPlayer()->m_lastFallTime >= movementInfo.fallTime || GetPlayer()->m_lastFallZ <=movementInfo.z || recv_data.GetOpcode() == MSG_MOVE_FALL_LAND)
275 GetPlayer()->SetFallInformation(movementInfo.fallTime, movementInfo.z);
277 if(GetPlayer()->isMovingOrTurning())
278 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
280 if(movementInfo.z < -500.0f)
282 if(GetPlayer()->InBattleGround()
283 && GetPlayer()->GetBattleGround()
284 && GetPlayer()->GetBattleGround()->HandlePlayerUnderMap(_player))
286 // do nothing, the handle already did if returned true
288 else
290 // NOTE: this is actually called many times while falling
291 // even after the player has been teleported away
292 // TODO: discard movement packets after the player is rooted
293 if(GetPlayer()->isAlive())
295 GetPlayer()->EnvironmentalDamage(GetPlayer()->GetGUID(),DAMAGE_FALL_TO_VOID, GetPlayer()->GetMaxHealth());
296 // change the death state to CORPSE to prevent the death timer from
297 // starting in the next player update
298 GetPlayer()->KillPlayer();
299 GetPlayer()->BuildPlayerRepop();
302 // cancel the death timer here if started
303 GetPlayer()->RepopAtGraveyard();
308 void WorldSession::HandleForceSpeedChangeAck(WorldPacket &recv_data)
310 sLog.outDebug("WORLD: Recvd %s (%u, 0x%X) opcode", LookupOpcodeName(recv_data.GetOpcode()), recv_data.GetOpcode(), recv_data.GetOpcode());
312 CHECK_PACKET_SIZE(recv_data, recv_data.rpos()+8+4);
314 /* extract packet */
315 uint64 guid;
316 uint32 unk1;
317 float newspeed;
319 recv_data >> guid;
321 // now can skip not our packet
322 if(_player->GetGUID() != guid)
323 return;
325 // continue parse packet
327 recv_data >> unk1; // counter or moveEvent
329 MovementInfo movementInfo;
330 ReadMovementInfo(recv_data, &movementInfo);
332 // recheck
333 CHECK_PACKET_SIZE(recv_data, recv_data.rpos()+4);
335 recv_data >> newspeed;
336 /*----------------*/
338 // client ACK send one packet for mounted/run case and need skip all except last from its
339 // in other cases anti-cheat check can be fail in false case
340 UnitMoveType move_type;
341 UnitMoveType force_move_type;
343 static char const* move_type_name[MAX_MOVE_TYPE] = { "Walk", "Run", "RunBack", "Swim", "SwimBack", "TurnRate", "Flight", "FlightBack", "PitchRate" };
345 uint16 opcode = recv_data.GetOpcode();
346 switch(opcode)
348 case CMSG_FORCE_WALK_SPEED_CHANGE_ACK: move_type = MOVE_WALK; force_move_type = MOVE_WALK; break;
349 case CMSG_FORCE_RUN_SPEED_CHANGE_ACK: move_type = MOVE_RUN; force_move_type = MOVE_RUN; break;
350 case CMSG_FORCE_RUN_BACK_SPEED_CHANGE_ACK: move_type = MOVE_RUN_BACK; force_move_type = MOVE_RUN_BACK; break;
351 case CMSG_FORCE_SWIM_SPEED_CHANGE_ACK: move_type = MOVE_SWIM; force_move_type = MOVE_SWIM; break;
352 case CMSG_FORCE_SWIM_BACK_SPEED_CHANGE_ACK: move_type = MOVE_SWIM_BACK; force_move_type = MOVE_SWIM_BACK; break;
353 case CMSG_FORCE_TURN_RATE_CHANGE_ACK: move_type = MOVE_TURN_RATE; force_move_type = MOVE_TURN_RATE; break;
354 case CMSG_FORCE_FLIGHT_SPEED_CHANGE_ACK: move_type = MOVE_FLIGHT; force_move_type = MOVE_FLIGHT; break;
355 case CMSG_FORCE_FLIGHT_BACK_SPEED_CHANGE_ACK: move_type = MOVE_FLIGHT_BACK; force_move_type = MOVE_FLIGHT_BACK; break;
356 case CMSG_FORCE_PITCH_RATE_CHANGE_ACK: move_type = MOVE_PITCH_RATE; force_move_type = MOVE_PITCH_RATE; break;
357 default:
358 sLog.outError("WorldSession::HandleForceSpeedChangeAck: Unknown move type opcode: %u", opcode);
359 return;
362 // skip all forced speed changes except last and unexpected
363 // in run/mounted case used one ACK and it must be skipped.m_forced_speed_changes[MOVE_RUN} store both.
364 if(_player->m_forced_speed_changes[force_move_type] > 0)
366 --_player->m_forced_speed_changes[force_move_type];
367 if(_player->m_forced_speed_changes[force_move_type] > 0)
368 return;
371 if (!_player->GetTransport() && fabs(_player->GetSpeed(move_type) - newspeed) > 0.01f)
373 if(_player->GetSpeed(move_type) > newspeed) // must be greater - just correct
375 sLog.outError("%sSpeedChange player %s is NOT correct (must be %f instead %f), force set to correct value",
376 move_type_name[move_type], _player->GetName(), _player->GetSpeed(move_type), newspeed);
377 _player->SetSpeed(move_type,_player->GetSpeedRate(move_type),true);
379 else // must be lesser - cheating
381 sLog.outBasic("Player %s from account id %u kicked for incorrect speed (must be %f instead %f)",
382 _player->GetName(),_player->GetSession()->GetAccountId(),_player->GetSpeed(move_type), newspeed);
383 _player->GetSession()->KickPlayer();
388 void WorldSession::HandleSetActiveMoverOpcode(WorldPacket &recv_data)
390 sLog.outDebug("WORLD: Recvd CMSG_SET_ACTIVE_MOVER");
391 recv_data.hexlike();
393 CHECK_PACKET_SIZE(recv_data, 8);
395 uint64 guid;
396 recv_data >> guid;
398 if(_player->m_mover->GetGUID() != guid)
400 sLog.outError("HandleSetActiveMoverOpcode: incorrect mover guid: mover is " I64FMT " and should be " I64FMT, _player->m_mover->GetGUID(), guid);
401 return;
405 void WorldSession::HandleMoveNotActiveMover(WorldPacket &recv_data)
407 sLog.outDebug("WORLD: Recvd CMSG_MOVE_NOT_ACTIVE_MOVER");
408 recv_data.hexlike();
410 CHECK_PACKET_SIZE(recv_data, recv_data.rpos()+8);
412 uint64 old_mover_guid;
413 recv_data >> old_mover_guid;
415 if(_player->m_mover->GetGUID() == old_mover_guid)
417 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);
418 return;
421 MovementInfo mi;
422 ReadMovementInfo(recv_data, &mi);
423 _player->m_movementInfo = mi;
426 void WorldSession::HandleDismissControlledVehicle(WorldPacket &recv_data)
428 sLog.outDebug("WORLD: Recvd CMSG_DISMISS_CONTROLLED_VEHICLE");
429 recv_data.hexlike();
431 uint64 vehicleGUID = _player->GetCharmGUID();
433 if(!vehicleGUID) // something wrong here...
434 return;
436 MovementInfo mi;
437 ReadMovementInfo(recv_data, &mi);
438 _player->m_movementInfo = mi;
440 // using charm guid, because we don't have vehicle guid...
441 if(Vehicle *vehicle = ObjectAccessor::GetVehicle(vehicleGUID))
443 _player->ExitVehicle(vehicle);
444 vehicle->Dismiss();
448 void WorldSession::HandleMountSpecialAnimOpcode(WorldPacket& /*recvdata*/)
450 //sLog.outDebug("WORLD: Recvd CMSG_MOUNTSPECIAL_ANIM");
452 WorldPacket data(SMSG_MOUNTSPECIAL_ANIM, 8);
453 data << uint64(GetPlayer()->GetGUID());
455 GetPlayer()->SendMessageToSet(&data, false);
458 void WorldSession::HandleMoveKnockBackAck( WorldPacket & /*recv_data*/ )
460 // CHECK_PACKET_SIZE(recv_data,?);
461 sLog.outDebug("CMSG_MOVE_KNOCK_BACK_ACK");
462 // Currently not used but maybe use later for recheck final player position
463 // (must be at call same as into "recv_data >> x >> y >> z >> orientation;"
466 uint32 flags, time;
467 float x, y, z, orientation;
468 uint64 guid;
469 uint32 sequence;
470 uint32 ukn1;
471 float xdirection,ydirection,hspeed,vspeed;
473 recv_data >> guid;
474 recv_data >> sequence;
475 recv_data >> flags >> time;
476 recv_data >> x >> y >> z >> orientation;
477 recv_data >> ukn1; //unknown
478 recv_data >> vspeed >> xdirection >> ydirection >> hspeed;
480 // skip not personal message;
481 if(GetPlayer()->GetGUID()!=guid)
482 return;
484 // check code
488 void WorldSession::HandleMoveHoverAck( WorldPacket& /*recv_data*/ )
490 sLog.outDebug("CMSG_MOVE_HOVER_ACK");
493 void WorldSession::HandleMoveWaterWalkAck(WorldPacket& /*recv_data*/)
495 sLog.outDebug("CMSG_MOVE_WATER_WALK_ACK");
498 void WorldSession::HandleSummonResponseOpcode(WorldPacket& recv_data)
500 CHECK_PACKET_SIZE(recv_data,8+1);
502 if(!_player->isAlive() || _player->isInCombat() )
503 return;
505 uint64 summoner_guid;
506 bool agree;
507 recv_data >> summoner_guid;
508 recv_data >> agree;
510 _player->SummonIfPossible(agree);