diff --git a/src/server/game/Entities/Object/MovementInfo.h b/src/server/game/Entities/Object/MovementInfo.h index b7c2e39e73..8c8f96625c 100644 --- a/src/server/game/Entities/Object/MovementInfo.h +++ b/src/server/game/Entities/Object/MovementInfo.h @@ -29,6 +29,7 @@ struct MovementInfo uint16 flags2; Position pos; uint32 time; + uint32 movementCounter; // transport struct TransportInfo @@ -76,7 +77,7 @@ struct MovementInfo bool heightChangeFailed; MovementInfo() : - flags(0), flags2(0), time(0), pitch(0.0f), splineElevation(0.0f), heightChangeFailed(false) + flags(0), flags2(0), time(0), movementCounter(0), pitch(0.0f), splineElevation(0.0f), heightChangeFailed(false) { pos.Relocate(0.0f, 0.0f, 0.0f, 0.0f); transport.Reset(); diff --git a/src/server/game/Entities/Player/Player.cpp b/src/server/game/Entities/Player/Player.cpp index afdc577bbf..9762c6434d 100644 --- a/src/server/game/Entities/Player/Player.cpp +++ b/src/server/game/Entities/Player/Player.cpp @@ -1627,6 +1627,9 @@ bool Player::TeleportTo(uint32 mapid, float x, float y, float z, float orientati if (oldmap) oldmap->RemovePlayerFromMap(this, false); + // players on mount will be dismounted. the speed and height change should not require an ACK and should be applied directly + PurgeAndApplyPendingMovementChanges(false); + m_teleport_dest = WorldLocation(mapid, x, y, z, orientation); m_teleport_transport = transport; m_teleport_options = options; @@ -23413,7 +23416,7 @@ void Player::SendInitialPacketsBeforeAddToMap(bool firstLogin /*= false*/) // SMSG_UPDATE_WORLD_STATE // SMSG_POWER_UPDATE - GetSession()->GetGameClient()->AddAllowedMover(this); + GetGameClient()->SetMovedUnit(this, true); } void Player::SendInitialPacketsAfterAddToMap() @@ -27643,7 +27646,7 @@ void Player::SendMovementSetCollisionHeight(float height, UpdateCollisionHeightR WorldPackets::Movement::MoveSetCollisionHeight packet; packet.Height = height; packet.MoverGUID = GetGUID(); - packet.SequenceIndex = m_movementCounter++; + //packet.SequenceIndex = m_movementCounter++; packet.Reason = reason; SendDirectMessage(packet.Write()); } @@ -27813,6 +27816,21 @@ void Player::ValidateMovementInfo(MovementInfo* mi) mi->RemoveMovementFlag((maskToRemove)); #endif + if (mi->guid.IsEmpty()) + { + TC_LOG_ERROR("entities.unit", "WorldSession::ReadMovementInfo: mi->guid is empty"); + return; + } + + Unit* mover = GetCharmedOrSelf()->GetGUID() == mi->guid ? GetCharmedOrSelf() : ObjectAccessor::GetUnit(*GetCharmedOrSelf(), mi->guid); + if (!mover) + { + TC_LOG_ERROR("entities.unit", "WorldSession::ReadMovementInfo: If the server allows the unit (GUID %s) to be moved by the client of player %s, the unit should still exist!", + mi->guid.ToString().c_str(), + GetCharmedOrSelf()->GetGUID().ToString().c_str()); + return; + } + if (!GetCharmedOrSelf()->GetVehicleBase() || !(GetCharmedOrSelf()->GetVehicle()->GetVehicleInfo()->Flags & VEHICLE_FLAG_FIXED_POSITION)) REMOVE_VIOLATING_FLAGS(mi->HasMovementFlag(MOVEMENTFLAG_ROOT), MOVEMENTFLAG_ROOT); @@ -27824,7 +27842,7 @@ void Player::ValidateMovementInfo(MovementInfo* mi) MOVEMENTFLAG_MASK_MOVING); //! Cannot hover without SPELL_AURA_HOVER - REMOVE_VIOLATING_FLAGS(mi->HasMovementFlag(MOVEMENTFLAG_HOVER) && !GetCharmedOrSelf()->HasAuraType(SPELL_AURA_HOVER), + REMOVE_VIOLATING_FLAGS(mi->HasMovementFlag(MOVEMENTFLAG_HOVER) && !mover->HasAuraType(SPELL_AURA_HOVER), MOVEMENTFLAG_HOVER); //! Cannot ascend and descend at the same time @@ -27849,8 +27867,8 @@ void Player::ValidateMovementInfo(MovementInfo* mi) //! Cannot walk on water without SPELL_AURA_WATER_WALK except for ghosts REMOVE_VIOLATING_FLAGS(mi->HasMovementFlag(MOVEMENTFLAG_WATERWALKING) && - !GetCharmedOrSelf()->HasAuraType(SPELL_AURA_WATER_WALK) && - !GetCharmedOrSelf()->HasAuraType(SPELL_AURA_GHOST), + !mover->HasAuraType(SPELL_AURA_WATER_WALK) && + !mover->HasAuraType(SPELL_AURA_GHOST), MOVEMENTFLAG_WATERWALKING); //! Cannot feather fall without SPELL_AURA_FEATHER_FALL @@ -27864,8 +27882,8 @@ void Player::ValidateMovementInfo(MovementInfo* mi) */ REMOVE_VIOLATING_FLAGS(mi->HasMovementFlag(MOVEMENTFLAG_FLYING | MOVEMENTFLAG_CAN_FLY) && GetSession()->GetSecurity() == SEC_PLAYER && - !GetCharmedOrSelf()->HasAuraType(SPELL_AURA_FLY) && - !GetCharmedOrSelf()->HasAuraType(SPELL_AURA_MOD_INCREASE_MOUNTED_FLIGHT_SPEED), + !mover->HasAuraType(SPELL_AURA_FLY) && + !mover->HasAuraType(SPELL_AURA_MOD_INCREASE_MOUNTED_FLIGHT_SPEED), MOVEMENTFLAG_FLYING | MOVEMENTFLAG_CAN_FLY); REMOVE_VIOLATING_FLAGS(mi->HasMovementFlag(MOVEMENTFLAG_DISABLE_GRAVITY | MOVEMENTFLAG_CAN_FLY) && mi->HasMovementFlag(MOVEMENTFLAG_FALLING), @@ -28080,7 +28098,7 @@ void Player::ReadMovementInfo(WorldPacket& data, MovementInfo* mi, Movement::Ext data >> mi->splineElevation; break; case MSECounter: - data.read_skip(); /// @TODO: Maybe compare it with m_movementCounter to verify that packets are sent & received in order? + data >> mi->movementCounter; break; case MSEZeroBit: case MSEOneBit: diff --git a/src/server/game/Entities/Unit/Unit.cpp b/src/server/game/Entities/Unit/Unit.cpp index 8a49a378b8..ffc5934277 100644 --- a/src/server/game/Entities/Unit/Unit.cpp +++ b/src/server/game/Entities/Unit/Unit.cpp @@ -52,6 +52,7 @@ #include "MovementStructures.h" #include "MoveSpline.h" #include "MoveSplineInit.h" +#include "MovementPacketSender.h" #include "ObjectAccessor.h" #include "ObjectMgr.h" #include "Opcodes.h" @@ -399,9 +400,6 @@ Unit::Unit(bool isWorldObject) : for (uint8 i = 0; i < MAX_MOVE_TYPE; ++i) m_speed_rate[i] = 1.0f; - for (uint8 i = 0; i < MAX_MOVE_TYPE; ++i) - m_forced_speed_changes[i] = 0; - m_charmInfo = nullptr; _gameClientMovingMe = nullptr; @@ -469,6 +467,8 @@ void Unit::Update(uint32 p_time) // Or else we may have some SPELL_STATE_FINISHED spells stalled in pointers, that is bad. m_Events.Update(p_time); + CheckPendingMovementAcks(); + if (!IsInWorld()) return; @@ -9042,38 +9042,37 @@ void Unit::SetSpeedRate(UnitMoveType mtype, float rate) rate = 0.0f; // Update speed only on change - if (m_speed_rate[mtype] == rate) + MovementChangeType changeType = MovementPacketSender::GetChangeTypeByMoveType(mtype); + if (m_speed_rate[mtype] == rate && !HasPendingMovementChange(changeType)) //todo: is the "!HasPendingMovementChange" part necessary here? return; - m_speed_rate[mtype] = rate; - - PropagateSpeedChange(); - - static OpcodeServer const moveTypeToOpcode[MAX_MOVE_TYPE][2] = + float newSpeedFlat = rate * (IsControlledByPlayer() ? playerBaseMoveSpeed[mtype] : baseMoveSpeed[mtype]); + if (IsMovedByClient() && IsInWorld()) + MovementPacketSender::SendSpeedChangeToMover(this, mtype, newSpeedFlat); + else if (IsMovedByClient() && !IsInWorld()) // (1) + SetSpeedRateReal(mtype, rate); + else // <=> if(!IsMovedByPlayer()) { - {SMSG_SPLINE_MOVE_SET_WALK_SPEED, SMSG_MOVE_SET_WALK_SPEED }, - {SMSG_SPLINE_MOVE_SET_RUN_SPEED, SMSG_MOVE_SET_RUN_SPEED }, - {SMSG_SPLINE_MOVE_SET_RUN_BACK_SPEED, SMSG_MOVE_SET_RUN_BACK_SPEED }, - {SMSG_SPLINE_MOVE_SET_SWIM_SPEED, SMSG_MOVE_SET_SWIM_SPEED }, - {SMSG_SPLINE_MOVE_SET_SWIM_BACK_SPEED, SMSG_MOVE_SET_SWIM_BACK_SPEED }, - {SMSG_SPLINE_MOVE_SET_TURN_RATE, SMSG_MOVE_SET_TURN_RATE, }, - {SMSG_SPLINE_MOVE_SET_FLIGHT_SPEED, SMSG_MOVE_SET_FLIGHT_SPEED, }, - {SMSG_SPLINE_MOVE_SET_FLIGHT_BACK_SPEED, SMSG_MOVE_SET_FLIGHT_BACK_SPEED, }, - {SMSG_SPLINE_MOVE_SET_PITCH_RATE, SMSG_MOVE_SET_PITCH_RATE, } - }; - - if (GetTypeId() == TYPEID_PLAYER) - { - // register forced speed changes for WorldSession::HandleForceSpeedChangeAck - // and do it only for real sent packets and use run for run/mounted as client expected - ++m_forced_speed_changes[mtype]; + SetSpeedRateReal(mtype, rate); + MovementPacketSender::SendSpeedChangeToAll(this, mtype, newSpeedFlat); } - static MovementStatusElements const speedVal = MSEExtraFloat; - Movement::ExtraMovementStatusElement extra(&speedVal); - extra.Data.floatData = GetSpeed(mtype); + // explaination of (1): + // If the player is not in the world yet, it won't reply to the packets requiring an ack. And once the player is in the world, next time a movement + // packet which requires an ack is sent to the client (change of speed for example), the client is kicked from the + // server on the ground that it should have replied to the first packet first. That line is a hacky fix + // in the sense that it doesn't work like that in retail since buffs are applied only after the player has been + // initialized in the world. cf description of PR #18771 +} - Movement::PacketSender(IsMovedByClient() ? GetGameClientMovingMe()->GetBasePlayer() : this, moveTypeToOpcode[mtype][0], moveTypeToOpcode[mtype][1], NULL_OPCODE, &extra).Send(); +void Unit::SetSpeedRateReal(UnitMoveType mtype, float rate) +{ + if (!IsInCombat() && ToPlayer()) + if (Pet* pet = ToPlayer()->GetPet()) + pet->SetSpeedRate(mtype, rate); + + m_speed_rate[mtype] = rate; + PropagateSpeedChange(); } void Unit::FollowTarget(Unit* target) @@ -11973,6 +11972,10 @@ void Unit::SetFeared(bool apply) { if (apply) { + // block control to real player in control (eg charmer) + if (GetCharmerOrSelfPlayer()) + GetCharmerOrSelfPlayer()->SetClientControl(this, false); + SetTarget(ObjectGuid::Empty); Unit* caster = nullptr; @@ -11992,17 +11995,21 @@ void Unit::SetFeared(bool apply) if (GetVictim()) SetTarget(EnsureVictim()->GetGUID()); } - } - // block / allow control to real player in control (eg charmer) - if (GetCharmerOrSelfPlayer()) - GetCharmerOrSelfPlayer()->SetClientControl(this, !apply); + // allow control to real player in control (eg charmer) + if (GetCharmerOrSelfPlayer()) + GetCharmerOrSelfPlayer()->SetClientControl(this, true); + } } void Unit::SetConfused(bool apply) { if (apply) { + // block control to real player in control (eg charmer) + if (GetCharmerOrSelfPlayer()) + GetCharmerOrSelfPlayer()->SetClientControl(this, false); + SetTarget(ObjectGuid::Empty); GetMotionMaster()->MoveConfused(); } @@ -12015,11 +12022,11 @@ void Unit::SetConfused(bool apply) if (GetVictim()) SetTarget(EnsureVictim()->GetGUID()); } - } - // block / allow control to real player in control (eg charmer) - if (GetCharmerOrSelfPlayer()) - GetCharmerOrSelfPlayer()->SetClientControl(this, !apply); + // allow control to real player in control (eg charmer) + if (GetCharmerOrSelfPlayer()) + GetCharmerOrSelfPlayer()->SetClientControl(this, true); + } } bool Unit::SetCharmedBy(Unit* charmer, CharmType type, AuraApplication const* aurApp) @@ -13483,7 +13490,7 @@ void Unit::NearTeleportTo(Position const& pos, bool casting /*= false*/) } } -void Unit::WriteMovementInfo(WorldPacket& data, Movement::ExtraMovementStatusElement* extras /*= nullptr*/) +void Unit::WriteMovementInfo(WorldPacket& data, Movement::ExtraMovementStatusElement* extras /*= nullptr*/, uint32* movementCounter /*= nullptr*/) { MovementInfo const& mi = m_movementInfo; @@ -13687,7 +13694,7 @@ void Unit::WriteMovementInfo(WorldPacket& data, Movement::ExtraMovementStatusEle data << mi.splineElevation; break; case MSECounter: - data << m_movementCounter++; + data << uint32(movementCounter ? *movementCounter : 0); break; case MSEZeroBit: data.WriteBit(0); @@ -13800,6 +13807,101 @@ void Unit::UpdateHeight(float newZ) GetVehicleKit()->RelocatePassengers(); } +PlayerMovementPendingChange& Unit::PeakFirstPendingMovementChange() +{ + return m_pendingMovementChanges.front(); +} + +PlayerMovementPendingChange Unit::PopPendingMovementChange() +{ + PlayerMovementPendingChange result = m_pendingMovementChanges.front(); + m_pendingMovementChanges.pop_front(); + return result; +} + +void Unit::PushPendingMovementChange(PlayerMovementPendingChange newChange) +{ + m_pendingMovementChanges.emplace_back(std::move(newChange)); +} + +bool Unit::HasPendingMovementChange(MovementChangeType changeType) const +{ + return std::find_if(m_pendingMovementChanges.begin(), m_pendingMovementChanges.end(), + [changeType](PlayerMovementPendingChange const& pendingChange) + { + return pendingChange.movementChangeType == changeType; + }) != m_pendingMovementChanges.end(); +} + +void Unit::CheckPendingMovementAcks() +{ + if (sWorld->getIntConfig(CONFIG_PENDING_MOVE_CHANGES_TIMEOUT) == 0) + return; + + if (!HasPendingMovementChange()) + return; + + PlayerMovementPendingChange const& oldestChangeToAck = m_pendingMovementChanges.front(); + if (GameTime::GetGameTimeMS() > oldestChangeToAck.time + sWorld->getIntConfig(CONFIG_PENDING_MOVE_CHANGES_TIMEOUT)) + { + /* + when players are teleported from one corner of a map to an other (example: from Dragonblight to the entrance of Naxxramas, both in the same map: Northend), + is it done through what is called a 'near' teleport. A near teleport always involve teleporting a player from one point to an other in the same map, even if + the distance is huge. When that distance is big enough, a loading screen appears on the client side. During that time, the client loads the surrounding zone + of the new location (and everything it contains). The problem is that, as long as the client hasn't finished loading the new zone, it will NOT ack the near + teleport. So if the server sends a near teleport order at a certain time and the client takes 20s to load the new zone (let's imagine a very slow computer), + even with zero latency, the server will receive an ack from the client only after 20s. + + For this reason and because the current implementation is simple (you dear reader, feel free to improve it if you can), we will just ignore checking for + near teleport acks (for now. @todo). + */ + if (oldestChangeToAck.movementChangeType == MovementChangeType::TELEPORT) + return; + + GameClient* controller = GetGameClientMovingMe(); + controller->GetWorldSession()->KickPlayer(); + TC_LOG_INFO("cheat", "Unit::CheckPendingMovementAcks: Player GUID: %s took too long to acknowledge a movement change. He was therefore kicked.", controller->GetBasePlayer()->GetGUID().ToString().c_str()); + } +} + +void Unit::PurgeAndApplyPendingMovementChanges(bool informObservers /* = true */) +{ + for (auto pendingChange = m_pendingMovementChanges.cbegin(); pendingChange != m_pendingMovementChanges.cend(); ++pendingChange) + { + float speedFlat = pendingChange->newValue; + MovementChangeType changeType = pendingChange->movementChangeType; + UnitMoveType moveType; + switch (changeType) + { + case MovementChangeType::SPEED_CHANGE_WALK: moveType = MOVE_WALK; break; + case MovementChangeType::SPEED_CHANGE_RUN: moveType = MOVE_RUN; break; + case MovementChangeType::SPEED_CHANGE_RUN_BACK: moveType = MOVE_RUN_BACK; break; + case MovementChangeType::SPEED_CHANGE_SWIM: moveType = MOVE_SWIM; break; + case MovementChangeType::SPEED_CHANGE_SWIM_BACK: moveType = MOVE_SWIM_BACK; break; + case MovementChangeType::RATE_CHANGE_TURN: moveType = MOVE_TURN_RATE; break; + case MovementChangeType::SPEED_CHANGE_FLIGHT_SPEED: moveType = MOVE_FLIGHT; break; + case MovementChangeType::SPEED_CHANGE_FLIGHT_BACK_SPEED: moveType = MOVE_FLIGHT_BACK; break; + case MovementChangeType::RATE_CHANGE_PITCH: moveType = MOVE_PITCH_RATE; break; + default: + ASSERT(false); + return; + } + + float newSpeedRate = speedFlat / (IsControlledByPlayer() ? playerBaseMoveSpeed[moveType] : baseMoveSpeed[moveType]); + SetSpeedRateReal(moveType, newSpeedRate); + + if (informObservers) + MovementPacketSender::SendSpeedChangeToObservers(this, moveType, speedFlat); + } + + m_pendingMovementChanges.clear(); +} + +PlayerMovementPendingChange::PlayerMovementPendingChange() +{ + time = GameTime::GetGameTimeMS(); +} + // baseRage means damage taken when attacker = false void Unit::RewardRage(uint32 baseRage, bool attacker) { @@ -14090,7 +14192,6 @@ bool Unit::SetDisableGravity(bool disable, bool packetOnly /*= false*/, bool /*u WriteMovementInfo(data); - return true; } diff --git a/src/server/game/Entities/Unit/Unit.h b/src/server/game/Entities/Unit/Unit.h index 34cc8b560a..959be557a1 100644 --- a/src/server/game/Entities/Unit/Unit.h +++ b/src/server/game/Entities/Unit/Unit.h @@ -32,6 +32,7 @@ #include #include #include +#include #define WORLD_TRIGGER 12999 @@ -266,24 +267,55 @@ enum UnitState : uint32 UNIT_STATE_ALL_STATE = 0xffffffff }; -enum UnitMoveType -{ - MOVE_WALK = 0, - MOVE_RUN = 1, - MOVE_RUN_BACK = 2, - MOVE_SWIM = 3, - MOVE_SWIM_BACK = 4, - MOVE_TURN_RATE = 5, - MOVE_FLIGHT = 6, - MOVE_FLIGHT_BACK = 7, - MOVE_PITCH_RATE = 8 -}; - -#define MAX_MOVE_TYPE 9 - TC_GAME_API extern float baseMoveSpeed[MAX_MOVE_TYPE]; TC_GAME_API extern float playerBaseMoveSpeed[MAX_MOVE_TYPE]; +enum class MovementChangeType : uint8 +{ + INVALID, + + ROOT, + WATER_WALK, + SET_HOVER, + SET_CAN_FLY, + SET_CAN_TRANSITION_BETWEEN_SWIM_AND_FLY, + FEATHER_FALL, + GRAVITY_DISABLE, + + SPEED_CHANGE_WALK, + SPEED_CHANGE_RUN, + SPEED_CHANGE_RUN_BACK, + SPEED_CHANGE_SWIM, + SPEED_CHANGE_SWIM_BACK, + RATE_CHANGE_TURN, + SPEED_CHANGE_FLIGHT_SPEED, + SPEED_CHANGE_FLIGHT_BACK_SPEED, + RATE_CHANGE_PITCH, + + SET_COLLISION_HGT, + TELEPORT, + KNOCK_BACK +}; + +struct PlayerMovementPendingChange +{ + PlayerMovementPendingChange(); + + uint32 movementCounter = 0; + MovementChangeType movementChangeType = MovementChangeType::INVALID; + uint32 time; + + float newValue = 0.0f; // used if speed or height change + bool apply = false; // used if movement flag change + struct KnockbackInfo + { + float vcos = 0.0f; + float vsin = 0.0f; + float speedXY = 0.0f; + float speedZ = 0.0f; + } knockbackInfo; // used if knockback +}; + enum CombatRating { CR_WEAPON_SKILL = 0, @@ -763,6 +795,7 @@ struct PendingSpellCastRequest class TC_GAME_API Unit : public WorldObject { + friend class WorldSession; public: typedef std::set AttackerSet; typedef std::set ControlList; @@ -1622,7 +1655,10 @@ class TC_GAME_API Unit : public WorldObject float GetSpeedRate(UnitMoveType mtype) const { return m_speed_rate[mtype]; } void SetSpeed(UnitMoveType mtype, float newValue); void SetSpeedRate(UnitMoveType mtype, float rate); + private: + void SetSpeedRateReal(UnitMoveType mtype, float rate); + public: float ApplyEffectModifiers(SpellInfo const* spellProto, uint8 effect_index, float value) const; int32 CalculateSpellDamage(Unit const* target, SpellInfo const* spellProto, uint8 effect_index, int32 const* basePoints = nullptr) const; int32 ModSpellDuration(SpellInfo const* spellProto, Unit const* target, int32 duration, bool positive, uint32 effectMask); @@ -1719,7 +1755,7 @@ class TC_GAME_API Unit : public WorldObject void _ExitVehicle(Position const* exitPosition = nullptr); void _EnterVehicle(Vehicle* vehicle, int8 seatId, AuraApplication const* aurApp = nullptr); - void WriteMovementInfo(WorldPacket& data, Movement::ExtraMovementStatusElement* extras = nullptr); + void WriteMovementInfo(WorldPacket& data, Movement::ExtraMovementStatusElement* extras = nullptr, uint32* movementCounter = nullptr); bool isMoving() const { return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_MASK_MOVING); } bool isTurning() const { return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_MASK_TURNING); } @@ -1734,6 +1770,15 @@ class TC_GAME_API Unit : public WorldObject return HasUnitMovementFlag(MOVEMENTFLAG_HOVER) ? GetFloatValue(UNIT_FIELD_HOVERHEIGHT) : 0.0f; } + uint32 GetMovementCounterAndInc() { return m_movementCounter++; } + uint32 GetMovementCounter() { return m_movementCounter; } + PlayerMovementPendingChange& PeakFirstPendingMovementChange(); + PlayerMovementPendingChange PopPendingMovementChange(); + void PushPendingMovementChange(PlayerMovementPendingChange newChange); + bool HasPendingMovementChange() const { return !m_pendingMovementChanges.empty(); } + bool HasPendingMovementChange(MovementChangeType changeType) const; + void PurgeAndApplyPendingMovementChanges(bool informObservers = true); + void RewardRage(uint32 baseRage, bool attacker); void OutDebugInfo() const; @@ -1755,8 +1800,6 @@ class TC_GAME_API Unit : public WorldObject // Movement info Movement::MoveSpline * movespline; - uint8 m_forced_speed_changes[MAX_MOVE_TYPE]; - // Part of Evade mechanics time_t GetLastDamagedTime() const { return _lastDamagedTime; } void UpdateLastDamagedTime(SpellInfo const* spellProto); @@ -1879,6 +1922,7 @@ class TC_GAME_API Unit : public WorldObject virtual void AtDisengage() {} void InterruptMovementBasedAuras(); + void CheckPendingMovementAcks(); uint32 GetPowerUpdateInterval() const { return IsPlayer() ? PLAYER_POWER_UPDATE_INTERVAL : UNIT_POWER_UPDATE_INTERVAL; } private: @@ -1898,10 +1942,7 @@ class TC_GAME_API Unit : public WorldObject void SetStunned(bool apply); void SetRooted(bool apply, bool packetOnly = false); - uint32 m_movementCounter; ///< Incrementing counter used in movement packets - private: - uint32 m_state; // Even derived shouldn't modify TimeTrackerSmall m_splineSyncTimer; @@ -1946,6 +1987,14 @@ class TC_GAME_API Unit : public WorldObject void ProcessPendingSpellCastRequest(); void ProcessItemCast(PendingSpellCastRequest const& castRequest, SpellCastTargets const& targets); bool CanExecutePendingSpellCastRequest(SpellInfo const* spellInfo) const; + + /* Player Movement fields START*/ + + // when a player controls this unit, and when change is made to this unit which requires an ack from the client to be acted (change of speed for example), this movementCounter is incremented + uint32 m_movementCounter; + std::deque m_pendingMovementChanges; + + /* Player Movement fields END*/ }; namespace Trinity diff --git a/src/server/game/Entities/Unit/UnitDefines.h b/src/server/game/Entities/Unit/UnitDefines.h index 91367c2e4c..8a337e2654 100644 --- a/src/server/game/Entities/Unit/UnitDefines.h +++ b/src/server/game/Entities/Unit/UnitDefines.h @@ -241,7 +241,7 @@ enum MovementFlags : uint32 MOVEMENTFLAG_PENDING_STRAFE_RIGHT = 0x00040000, MOVEMENTFLAG_PENDING_ROOT = 0x00080000, MOVEMENTFLAG_SWIMMING = 0x00100000, // appears with fly flag also - MOVEMENTFLAG_ASCENDING = 0x00200000, // press "space" when flying + MOVEMENTFLAG_ASCENDING = 0x00200000, // press "space" when flying or swimming MOVEMENTFLAG_DESCENDING = 0x00400000, MOVEMENTFLAG_CAN_FLY = 0x00800000, // Appears when unit can fly AND also walk MOVEMENTFLAG_FLYING = 0x01000000, // unit is actually flying. pretty sure this is only used for players. creatures use disable_gravity @@ -298,6 +298,21 @@ enum MovementFlags2 : uint32 MOVEMENTFLAG2_INTERPOLATED_PITCHING = 0x00008000 }; +enum UnitMoveType +{ + MOVE_WALK = 0, + MOVE_RUN = 1, + MOVE_RUN_BACK = 2, + MOVE_SWIM = 3, + MOVE_SWIM_BACK = 4, + MOVE_TURN_RATE = 5, + MOVE_FLIGHT = 6, + MOVE_FLIGHT_BACK = 7, + MOVE_PITCH_RATE = 8 +}; + +#define MAX_MOVE_TYPE 9 + enum HitInfo { HITINFO_NORMALSWING = 0x00000000, diff --git a/src/server/game/Handlers/MovementHandler.cpp b/src/server/game/Handlers/MovementHandler.cpp index 4b05935092..c6d557c1f2 100644 --- a/src/server/game/Handlers/MovementHandler.cpp +++ b/src/server/game/Handlers/MovementHandler.cpp @@ -25,6 +25,7 @@ #include "MapManager.h" #include "MotionMaster.h" #include "MovementGenerator.h" +#include "MovementPacketSender.h" #include "MovementPackets.h" #include "MovementStructures.h" #include "Transport.h" @@ -465,106 +466,95 @@ void WorldSession::HandleForceSpeedChangeAck(WorldPacket &recvData) Movement::ExtraMovementStatusElement extras(&speedElement); GetPlayer()->ReadMovementInfo(recvData, &movementInfo, &extras); - if (!IsRightUnitBeingMoved(movementInfo.guid)) - return; - GameClient* client = GetGameClient(); - Unit* mover = client->GetActivelyMovedUnit(); - float newspeed = extras.Data.floatData; - /*----------------*/ - - // client ACK send one packet for mounted/run case and need skip all except last from its - // in other cases anti-cheat check can be fail in false case - UnitMoveType move_type; - - static char const* const move_type_name[MAX_MOVE_TYPE] = + // ACK handlers should call GameClient::IsAllowedToMove instead of WorldSession::IsRightUnitBeingMoved + // because the ACK could be coming from a unit that is under the control of that client but is not the 'Active Mover' unit. + // Example: Get a speed buff on yourself, then mount a vehicle before the end of the buff. When the buff expires, + // a force message will be sent to the client regarding the player and the client is required to respond with an ACK. + // But the vehicle will be the active mover unit at that time. + if (!client->IsAllowedToMove(movementInfo.guid)) { - "Walk", - "Run", - "RunBack", - "Swim", - "SwimBack", - "TurnRate", - "Flight", - "FlightBack", - "PitchRate" - }; + TC_LOG_ERROR("entities.unit", "Ignoring ACK. Bad or outdated movement data by Player %s", _player->GetName().c_str()); + return; + } - OpcodeServer moveUpdateOpcode = OpcodeServer(NULL_OPCODE); + Unit* mover = ObjectAccessor::GetUnit(*_player, movementInfo.guid); + + UnitMoveType move_type; switch (recvData.GetOpcode()) { - case CMSG_MOVE_FORCE_WALK_SPEED_CHANGE_ACK: - move_type = MOVE_WALK; - moveUpdateOpcode = SMSG_MOVE_UPDATE_WALK_SPEED; - break; - case CMSG_MOVE_FORCE_RUN_SPEED_CHANGE_ACK: - move_type = MOVE_RUN; - moveUpdateOpcode = SMSG_MOVE_UPDATE_RUN_SPEED; - break; - case CMSG_MOVE_FORCE_RUN_BACK_SPEED_CHANGE_ACK: - move_type = MOVE_RUN_BACK; - moveUpdateOpcode = SMSG_MOVE_UPDATE_RUN_BACK_SPEED; - break; - case CMSG_MOVE_FORCE_SWIM_SPEED_CHANGE_ACK: - move_type = MOVE_SWIM; - moveUpdateOpcode = SMSG_MOVE_UPDATE_SWIM_SPEED; - break; - case CMSG_MOVE_FORCE_SWIM_BACK_SPEED_CHANGE_ACK: - move_type = MOVE_SWIM_BACK; - moveUpdateOpcode = SMSG_MOVE_UPDATE_SWIM_BACK_SPEED; - break; - case CMSG_MOVE_FORCE_TURN_RATE_CHANGE_ACK: - move_type = MOVE_TURN_RATE; - moveUpdateOpcode = SMSG_MOVE_UPDATE_TURN_RATE; - break; - case CMSG_MOVE_FORCE_FLIGHT_SPEED_CHANGE_ACK: - move_type = MOVE_FLIGHT; - moveUpdateOpcode = SMSG_MOVE_UPDATE_FLIGHT_SPEED; - break; - case CMSG_MOVE_FORCE_FLIGHT_BACK_SPEED_CHANGE_ACK: - move_type = MOVE_FLIGHT_BACK; - moveUpdateOpcode = SMSG_MOVE_UPDATE_FLIGHT_BACK_SPEED; - break; - case CMSG_MOVE_FORCE_PITCH_RATE_CHANGE_ACK: - move_type = MOVE_PITCH_RATE; - moveUpdateOpcode = SMSG_MOVE_UPDATE_PITCH_RATE; - break; + case CMSG_MOVE_FORCE_WALK_SPEED_CHANGE_ACK: move_type = MOVE_WALK; break; + case CMSG_MOVE_FORCE_RUN_SPEED_CHANGE_ACK: move_type = MOVE_RUN; break; + case CMSG_MOVE_FORCE_RUN_BACK_SPEED_CHANGE_ACK: move_type = MOVE_RUN_BACK; break; + case CMSG_MOVE_FORCE_SWIM_SPEED_CHANGE_ACK: move_type = MOVE_SWIM; break; + case CMSG_MOVE_FORCE_SWIM_BACK_SPEED_CHANGE_ACK: move_type = MOVE_SWIM_BACK; break; + case CMSG_MOVE_FORCE_TURN_RATE_CHANGE_ACK: move_type = MOVE_TURN_RATE; break; + case CMSG_MOVE_FORCE_FLIGHT_SPEED_CHANGE_ACK: move_type = MOVE_FLIGHT; break; + case CMSG_MOVE_FORCE_FLIGHT_BACK_SPEED_CHANGE_ACK: move_type = MOVE_FLIGHT_BACK; break; + case CMSG_MOVE_FORCE_PITCH_RATE_CHANGE_ACK: move_type = MOVE_PITCH_RATE; break; default: - TC_LOG_ERROR("network", "WorldSession::HandleForceSpeedChangeAck: Unknown move type opcode: %u", recvData.GetOpcode()); + TC_LOG_ERROR("network", "WorldSession::HandleForceSpeedChangeAck: Unknown move type opcode: %s", GetOpcodeNameForLogging(static_cast(recvData.GetOpcode())).c_str()); return; } - static MovementStatusElements const speedVal = MSEExtraFloat; - Movement::ExtraMovementStatusElement extra(&speedVal); - extra.Data.floatData = newspeed; + float speedReceived = extras.Data.floatData; - Movement::PacketSender(mover, NULL_OPCODE, NULL_OPCODE, moveUpdateOpcode, &extra).Send(); - - // skip all forced speed changes except last and unexpected - // in run/mounted case used one ACK and it must be skipped. m_forced_speed_changes[MOVE_RUN] store both. - if (mover->m_forced_speed_changes[move_type] > 0) + // verify that indeed the client is replying with the changes that were send to him + if (!mover->HasPendingMovementChange() || mover->PeakFirstPendingMovementChange().movementCounter > movementInfo.movementCounter) { - --mover->m_forced_speed_changes[move_type]; - if (mover->m_forced_speed_changes[move_type] > 0) - return; + TC_LOG_DEBUG("entities.unit", "Ignoring ACK. Bad or outdated movement data by Player %s", _player->GetName().c_str()); + return; } - if (!mover->GetTransport() && std::fabs(mover->GetSpeed(move_type) - newspeed) > 0.01f) + PlayerMovementPendingChange pendingChange = mover->PopPendingMovementChange(); + float speedSent = pendingChange.newValue; + MovementChangeType changeType = pendingChange.movementChangeType; + UnitMoveType moveTypeSent; + switch (changeType) { - if (mover->GetSpeed(move_type) > newspeed) // must be greater - just correct - { - TC_LOG_ERROR("network", "%sSpeedChange player %s is NOT correct (must be %f instead %f), force set to correct value", - move_type_name[move_type], mover->GetName().c_str(), mover->GetSpeed(move_type), newspeed); - mover->SetSpeedRate(move_type, mover->GetSpeedRate(move_type)); - } - else // must be lesser - cheating - { - TC_LOG_DEBUG("misc", "Player %s from account id %u kicked for incorrect speed (must be %f instead %f)", - _player->GetName().c_str(), _player->GetSession()->GetAccountId(), _player->GetSpeed(move_type), newspeed); + case MovementChangeType::SPEED_CHANGE_WALK: moveTypeSent = MOVE_WALK; break; + case MovementChangeType::SPEED_CHANGE_RUN: moveTypeSent = MOVE_RUN; break; + case MovementChangeType::SPEED_CHANGE_RUN_BACK: moveTypeSent = MOVE_RUN_BACK; break; + case MovementChangeType::SPEED_CHANGE_SWIM: moveTypeSent = MOVE_SWIM; break; + case MovementChangeType::SPEED_CHANGE_SWIM_BACK: moveTypeSent = MOVE_SWIM_BACK; break; + case MovementChangeType::RATE_CHANGE_TURN: moveTypeSent = MOVE_TURN_RATE; break; + case MovementChangeType::SPEED_CHANGE_FLIGHT_SPEED: moveTypeSent = MOVE_FLIGHT; break; + case MovementChangeType::SPEED_CHANGE_FLIGHT_BACK_SPEED: moveTypeSent = MOVE_FLIGHT_BACK; break; + case MovementChangeType::RATE_CHANGE_PITCH: moveTypeSent = MOVE_PITCH_RATE; break; + default: + TC_LOG_INFO("cheat", "WorldSession::HandleForceSpeedChangeAck: Player %s from account id %u kicked for incorrect data returned in an ack", + _player->GetName().c_str(), _player->GetSession()->GetAccountId()); _player->GetSession()->KickPlayer(); - } + return; } + + if (pendingChange.movementCounter != movementInfo.movementCounter || std::fabs(speedSent - speedReceived) > 0.01f || moveTypeSent!= move_type) + { + TC_LOG_INFO("cheat", "WorldSession::HandleForceSpeedChangeAck: Player %s from account id %u kicked for incorrect data returned in an ack", + _player->GetName().c_str(), _player->GetSession()->GetAccountId()); + _player->GetSession()->KickPlayer(); + return; + } + + /* the client data has been verified. let's do the actual change now */ + int64 movementTime = (int64)movementInfo.time + _timeSyncClockDelta; + if (_timeSyncClockDelta == 0 || movementTime < 0 || movementTime > 0xFFFFFFFF) + { + TC_LOG_WARN("misc", "The computed movement time using clockDelta is erronous. Using fallback instead"); + movementInfo.time = GameTime::GetGameTimeMS(); + } + else + { + movementInfo.time = (uint32)movementTime; + } + + mover->m_movementInfo = movementInfo; + mover->UpdatePosition(movementInfo.pos); + + float newSpeedRate = speedSent / (mover->IsControlledByPlayer() ? playerBaseMoveSpeed[move_type] : baseMoveSpeed[move_type]); + mover->SetSpeedRateReal(move_type, newSpeedRate); + MovementPacketSender::SendSpeedChangeToObservers(mover, move_type, speedSent); } void WorldSession::HandleSetActiveMoverOpcode(WorldPackets::Movement::SetActiveMover& packet) @@ -597,8 +587,7 @@ void WorldSession::HandleMoveNotActiveMover(WorldPacket &recvData) if (client->GetActivelyMovedUnit() == nullptr || client->GetActivelyMovedUnit()->GetGUID() != movementInfo.guid) { - // this shouldn't never happen in theory - TC_LOG_WARN("entities.unit", "unset active mover FAILED for client of player %s. GUID %s.", _player->GetName().c_str(), movementInfo.guid.ToString().c_str()); + TC_LOG_DEBUG("entities.unit", "unset active mover FAILED for client of player %s. GUID %s.", _player->GetName().c_str(), movementInfo.guid.ToString().c_str()); return; } @@ -634,14 +623,31 @@ void WorldSession::HandleMoveKnockBackAck(WorldPacket& recvData) MovementInfo movementInfo; GetPlayer()->ReadMovementInfo(recvData, &movementInfo); - if (!IsRightUnitBeingMoved(movementInfo.guid)) + GameClient* client = GetGameClient(); + + // ACK handlers should call GameClient::IsAllowedToMove instead of WorldSession::IsRightUnitBeingMoved + // because the ACK could be coming from a unit that is under the control of that client but is not the 'Active Mover' unit. + // Example: Get a speed buff on yourself, then mount a vehicle before the end of the buff. When the buff expires, + // a force message will be sent to the client regarding the player and the client is required to respond with an ACK. + // But the vehicle will be the active mover unit at that time. + if (!client->IsAllowedToMove(movementInfo.guid)) return; - GameClient* client = GetGameClient(); - Unit* mover = client->GetActivelyMovedUnit(); + Unit* mover = ObjectAccessor::GetUnit(*_player, movementInfo.guid); + + int64 movementTime = (int64)movementInfo.time + _timeSyncClockDelta; + if (_timeSyncClockDelta == 0 || movementTime < 0 || movementTime > 0xFFFFFFFF) + { + TC_LOG_WARN("misc", "The computed movement time using clockDelta is erronous. Using fallback instead"); + movementInfo.time = GameTime::GetGameTimeMS(); + } + else + { + movementInfo.time = (uint32)movementTime; + } mover->m_movementInfo = movementInfo; - + mover->UpdatePosition(movementInfo.pos); WorldPacket data(SMSG_MOVE_UPDATE_KNOCK_BACK, 66); mover->WriteMovementInfo(data); diff --git a/src/server/game/Movement/MovementPacketSender.cpp b/src/server/game/Movement/MovementPacketSender.cpp new file mode 100644 index 0000000000..3451c4d051 --- /dev/null +++ b/src/server/game/Movement/MovementPacketSender.cpp @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2008-2017 TrinityCore + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "GameClient.h" +#include "MovementPacketSender.h" +#include "MovementStructures.h" +#include "Player.h" +#include "WorldPacket.h" +#include "WorldSession.h" + +// SMSG_SPLINE_SET_* messages are for units controlled by the server. +// SMSG_MOVE_SET_*_SPEED messages are for units controlled by a player and require the client to respond with an ACK message. +// SMSG_MOVE_UPDATE_*_SPEED is used to broadcast changes to observers of units controlled by a player +OpcodeServer const MovementPacketSender::moveTypeToOpcode[MAX_MOVE_TYPE][3] = +{ + { SMSG_SPLINE_MOVE_SET_WALK_SPEED, SMSG_MOVE_SET_WALK_SPEED, SMSG_MOVE_UPDATE_WALK_SPEED }, + { SMSG_SPLINE_MOVE_SET_RUN_SPEED, SMSG_MOVE_SET_RUN_SPEED, SMSG_MOVE_UPDATE_RUN_SPEED }, + { SMSG_SPLINE_MOVE_SET_RUN_BACK_SPEED, SMSG_MOVE_SET_RUN_BACK_SPEED, SMSG_MOVE_UPDATE_RUN_BACK_SPEED }, + { SMSG_SPLINE_MOVE_SET_SWIM_SPEED, SMSG_MOVE_SET_SWIM_SPEED, SMSG_MOVE_UPDATE_SWIM_SPEED }, + { SMSG_SPLINE_MOVE_SET_SWIM_BACK_SPEED, SMSG_MOVE_SET_SWIM_BACK_SPEED, SMSG_MOVE_UPDATE_SWIM_BACK_SPEED }, + { SMSG_SPLINE_MOVE_SET_TURN_RATE, SMSG_MOVE_SET_TURN_RATE, SMSG_MOVE_UPDATE_TURN_RATE }, + { SMSG_SPLINE_MOVE_SET_FLIGHT_SPEED, SMSG_MOVE_SET_FLIGHT_SPEED, SMSG_MOVE_UPDATE_FLIGHT_SPEED }, + { SMSG_SPLINE_MOVE_SET_FLIGHT_BACK_SPEED, SMSG_MOVE_SET_FLIGHT_BACK_SPEED, SMSG_MOVE_UPDATE_FLIGHT_BACK_SPEED }, + { SMSG_SPLINE_MOVE_SET_PITCH_RATE, SMSG_MOVE_SET_PITCH_RATE, SMSG_MOVE_UPDATE_PITCH_RATE }, +}; + +MovementChangeType MovementPacketSender::GetChangeTypeByMoveType(UnitMoveType moveType) +{ + switch (moveType) + { + case MOVE_WALK: return MovementChangeType::SPEED_CHANGE_WALK; + case MOVE_RUN: return MovementChangeType::SPEED_CHANGE_RUN; + case MOVE_RUN_BACK: return MovementChangeType::SPEED_CHANGE_RUN_BACK; + case MOVE_SWIM: return MovementChangeType::SPEED_CHANGE_SWIM; + case MOVE_SWIM_BACK: return MovementChangeType::SPEED_CHANGE_SWIM_BACK; + case MOVE_TURN_RATE: return MovementChangeType::RATE_CHANGE_TURN; + case MOVE_FLIGHT: return MovementChangeType::SPEED_CHANGE_FLIGHT_SPEED; + case MOVE_FLIGHT_BACK: return MovementChangeType::SPEED_CHANGE_FLIGHT_BACK_SPEED; + case MOVE_PITCH_RATE: return MovementChangeType::RATE_CHANGE_PITCH; + default: + ASSERT(false, "MovementPacketSender::SendSpeedChangeToMover Unsupported UnitMoveType"); + } +} + +void MovementPacketSender::SendSpeedChangeToMover(Unit* unit, UnitMoveType mtype, float newSpeedFlat) +{ + GameClient* controller = unit->GetGameClientMovingMe(); + + ASSERT(controller); + + uint32 mCounter = unit->GetMovementCounterAndInc(); + PlayerMovementPendingChange pendingChange; + pendingChange.movementCounter = mCounter; + pendingChange.newValue = newSpeedFlat; + pendingChange.movementChangeType = MovementPacketSender::GetChangeTypeByMoveType(mtype); + unit->PushPendingMovementChange(pendingChange); + + WorldPacket data; + data.Initialize(moveTypeToOpcode[mtype][1]); + + static MovementStatusElements const speedVal = MSEExtraFloat; + Movement::ExtraMovementStatusElement extra(&speedVal); + extra.Data.floatData = newSpeedFlat; + + unit->WriteMovementInfo(data, &extra, &mCounter); + controller->SendDirectMessage(&data); +} + +void MovementPacketSender::SendSpeedChangeToObservers(Unit* unit, UnitMoveType mtype, float newSpeedFlat) +{ + GameClient* controller = unit->GetGameClientMovingMe(); + + ASSERT(controller); + + WorldPacket data; + data.Initialize(moveTypeToOpcode[mtype][2]); + static MovementStatusElements const speedVal = MSEExtraFloat; + Movement::ExtraMovementStatusElement extra(&speedVal); + extra.Data.floatData = newSpeedFlat; + + unit->WriteMovementInfo(data, &extra); + unit->SendMessageToSet(&data, controller->GetBasePlayer()); +} + +void MovementPacketSender::SendSpeedChangeToAll(Unit* unit, UnitMoveType mtype, float newSpeedFlat) +{ + GameClient* controller = unit->GetGameClientMovingMe(); + + ASSERT(!controller); + + WorldPacket data; + data.Initialize(moveTypeToOpcode[mtype][0]); + static MovementStatusElements const speedVal = MSEExtraFloat; + Movement::ExtraMovementStatusElement extra(&speedVal); + extra.Data.floatData = newSpeedFlat; + + unit->WriteMovementInfo(data, &extra); + unit->SendMessageToSet(&data, true); +} + diff --git a/src/server/game/Movement/MovementPacketSender.h b/src/server/game/Movement/MovementPacketSender.h new file mode 100644 index 0000000000..779f3bc9d2 --- /dev/null +++ b/src/server/game/Movement/MovementPacketSender.h @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2008-2017 TrinityCore + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MOVEMENT_SENDER_H +#define MOVEMENT_SENDER_H + +#include "UnitDefines.h" + +class Player; +class Unit; +struct MovementInfo; +enum class MovementChangeType : uint8; + +//! +//! movement flag changes for player controlled units: +//! >>> APPLY +//! { +//! // Step1 Sent by the server to the mover's client // Step2 Sent back by the mover's client to the server // Step3 Sent to observers (all of these should be renamed to SMSG! Confirmed by sniff analysis) +//! { SMSG_FORCE_MOVE_ROOT, CMSG_FORCE_MOVE_ROOT_ACK, MSG_MOVE_ROOT }, +//! { SMSG_MOVE_WATER_WALK, CMSG_MOVE_WATER_WALK_ACK, MSG_MOVE_WATER_WALK }, +//! { SMSG_MOVE_SET_HOVER, CMSG_MOVE_HOVER_ACK, MSG_MOVE_HOVER }, +//! { SMSG_MOVE_SET_CAN_FLY, CMSG_MOVE_SET_CAN_FLY_ACK, MSG_MOVE_UPDATE_CAN_FLY }, +//! { SMSG_MOVE_SET_CAN_TRANSITION_BETWEEN_SWIM_AND_FLY, CMSG_MOVE_SET_CAN_TRANSITION_BETWEEN_SWIM_AND_FLY_ACK, MSG_MOVE_UPDATE_CAN_TRANSITION_BETWEEN_SWIM_AND_FLY }, +//! { SMSG_MOVE_FEATHER_FALL, CMSG_MOVE_FEATHER_FALL_ACK, MSG_MOVE_FEATHER_FALL }, +//! { SMSG_MOVE_GRAVITY_DISABLE, CMSG_MOVE_GRAVITY_DISABLE_ACK, MSG_MOVE_GRAVITY_CHNG } +//! }, +//! >>> UNAPPLY +//! { +//! { SMSG_FORCE_MOVE_UNROOT, CMSG_FORCE_MOVE_UNROOT_ACK, MSG_MOVE_UNROOT }, +//! { SMSG_MOVE_LAND_WALK, CMSG_MOVE_WATER_WALK_ACK, MSG_MOVE_WATER_WALK }, +//! { SMSG_MOVE_UNSET_HOVER, CMSG_MOVE_HOVER_ACK, MSG_MOVE_HOVER }, +//! { SMSG_MOVE_UNSET_CAN_FLY, CMSG_MOVE_SET_CAN_FLY_ACK, MSG_MOVE_UPDATE_CAN_FLY }, +//! { SMSG_MOVE_UNSET_CAN_TRANSITION_BETWEEN_SWIM_AND_FLY, CMSG_MOVE_SET_CAN_TRANSITION_BETWEEN_SWIM_AND_FLY_ACK, MSG_MOVE_UPDATE_CAN_TRANSITION_BETWEEN_SWIM_AND_FLY }, +//! { SMSG_MOVE_NORMAL_FALL, CMSG_MOVE_FEATHER_FALL_ACK, MSG_MOVE_FEATHER_FALL }, +//! { SMSG_MOVE_GRAVITY_ENABLE, CMSG_MOVE_GRAVITY_ENABLE_ACK, MSG_MOVE_GRAVITY_CHNG } +//! } +//! +//! ---------------------- +//! Speed changes on player controlled units: +//! // Step1 Sent by the server to the mover's client // Step2 Sent back by the mover's client to the server // Step3 Sent to observers +//! { SMSG_FORCE_WALK_SPEED_CHANGE, CMSG_FORCE_WALK_SPEED_CHANGE_ACK, MSG_MOVE_SET_WALK_SPEED }, +//! { SMSG_FORCE_RUN_SPEED_CHANGE, CMSG_FORCE_RUN_SPEED_CHANGE_ACK, MSG_MOVE_SET_RUN_SPEED }, +//! { SMSG_FORCE_RUN_BACK_SPEED_CHANGE, CMSG_FORCE_RUN_BACK_SPEED_CHANGE_ACK, MSG_MOVE_SET_RUN_BACK_SPEED }, +//! { SMSG_FORCE_SWIM_SPEED_CHANGE, CMSG_FORCE_SWIM_SPEED_CHANGE_ACK, MSG_MOVE_SET_SWIM_SPEED }, +//! { SMSG_FORCE_SWIM_BACK_SPEED_CHANGE, CMSG_FORCE_SWIM_BACK_SPEED_CHANGE_ACK, MSG_MOVE_SET_SWIM_BACK_SPEED }, +//! { SMSG_FORCE_TURN_RATE_CHANGE, CMSG_FORCE_TURN_RATE_CHANGE_ACK, MSG_MOVE_SET_TURN_RATE }, +//! { SMSG_FORCE_FLIGHT_SPEED_CHANGE, CMSG_FORCE_FLIGHT_SPEED_CHANGE_ACK, MSG_MOVE_SET_FLIGHT_SPEED }, +//! { SMSG_FORCE_FLIGHT_BACK_SPEED_CHANGE, CMSG_FORCE_FLIGHT_BACK_SPEED_CHANGE_ACK, MSG_MOVE_SET_FLIGHT_BACK_SPEED }, +//! { SMSG_FORCE_PITCH_RATE_CHANGE, CMSG_FORCE_PITCH_RATE_CHANGE_ACK, MSG_MOVE_SET_PITCH_RATE }, +//! +//! ---------------------- +//! Other type of changes +//! // Step1 Sent by the server to the mover's client // Step2 Sent back by the mover's client to the server // Step3 Sent to observers +//! { SMSG_MOVE_SET_COLLISION_HGT, CMSG_MOVE_SET_COLLISION_HGT_ACK, MSG_MOVE_SET_COLLISION_HGT }, +//! { MSG_MOVE_TELEPORT_ACK, MSG_MOVE_TELEPORT_ACK, MSG_MOVE_TELEPORT }, +//! { SMSG_MOVE_KNOCK_BACK, CMSG_MOVE_KNOCK_BACK_ACK, MSG_MOVE_KNOCK_BACK }, +//! +//! ---------------------- +//! movement flag changes for server controlled units: (these player movement flags have no equivalent in spline: SET_CAN_FLY and SET_CAN_TRANSITION_BETWEEN_SWIM_AND_FLY) +//! apply +//! { +//! SMSG_SPLINE_MOVE_GRAVITY_DISABLE, +//! SMSG_SPLINE_MOVE_ROOT, +//! SMSG_SPLINE_MOVE_WATER_WALK, +//! SMSG_SPLINE_MOVE_FEATHER_FALL, +//! SMSG_SPLINE_MOVE_SET_HOVER, +//! } +//! unapply +//! { +//! SMSG_SPLINE_MOVE_GRAVITY_ENABLE, +//! SMSG_SPLINE_MOVE_UNROOT, +//! SMSG_SPLINE_MOVE_LAND_WALK, +//! SMSG_SPLINE_MOVE_NORMAL_FALL, +//! SMSG_SPLINE_MOVE_UNSET_HOVER, +//! } + +/* +xxxxxToMover() and xxxxxToObservers() methods should be only used on a unit controlled & moved by a player (as in direct client control: possess, vehicule,..). +ToMover() to send a packet to the client (asking for confirmation before acting the change) and ToObservers once the change has been acted and should be broadcasted to the other players around (the observers). + +xxxxxToAll() method should be used on a unit controlled & moved by the server (@todo note to self: does a player moved unit under the control of a temporary disorient (Scatter Shot eg) or fear fall into this category? EDIT: by looking at the effects of Psychic Scream (10890), the answer is yes) +*/ +class MovementPacketSender +{ + public: + ///* height change */ + //static void SendHeightChangeToMover(Unit* unit, float newRate); + //static void SendHeightChangeToObservers(Unit* unit, float newRate); + + ///* teleport */ + //static void SendTeleportAckPacket(Unit* unit, MovementInfo const& movementInfo); // rename to SendTeleportToMover? + //static void SendTeleportPacket(Unit* unit, MovementInfo const& movementInfo); // rename to SendTeleportToobservers? + + /* speed change */ + static void SendSpeedChangeToMover(Unit* unit, UnitMoveType mtype, float newRate); + static void SendSpeedChangeToObservers(Unit* unit, UnitMoveType mtype, float newRate); + static void SendSpeedChangeToAll(Unit* unit, UnitMoveType mtype, float newRate); + + ///* knocback */ + //static void SendKnockBackToMover(Unit* unit, float vcos, float vsin, float speedXY, float speedZ); + //static void SendKnockBackToObservers(Unit* unit, float vcos, float vsin, float speedXY, float speedZ); + + ///* movement flag change */ + //static void SendMovementFlagChangeToMover(Unit* unit, MovementFlags mFlag, bool apply); + //static void SendMovementFlagChangeToMover(Unit* unit, MovementFlags2 mFlag, bool apply); + //static void SendMovementFlagChangeToObservers(Unit* unit, MovementFlags mFlag, bool apply); + //static void SendMovementFlagChangeToObservers(Unit* unit, MovementFlags2 mFlag); + + //static void SendMovementFlagChangeToAll(Unit* unit, MovementFlags mFlag, bool apply); + + // utility method + static MovementChangeType GetChangeTypeByMoveType(UnitMoveType moveType); + + private: + static OpcodeServer const moveTypeToOpcode[MAX_MOVE_TYPE][3]; +}; + +#endif diff --git a/src/server/game/Server/GameClient.cpp b/src/server/game/Server/GameClient.cpp index 9dde28b3bf..d75ee938a4 100644 --- a/src/server/game/Server/GameClient.cpp +++ b/src/server/game/Server/GameClient.cpp @@ -28,8 +28,7 @@ GameClient::GameClient(WorldSession* sessionToServer) void GameClient::AddAllowedMover(Unit* unit) { - if (GameClient* previousController = unit->GetGameClientMovingMe()) - previousController->RemoveAllowedMover(unit); + ASSERT(!unit->GetGameClientMovingMe() || unit->GetGameClientMovingMe() == this); _allowedMovers.insert(unit->GetGUID()); unit->SetGameClientMovingMe(this); @@ -37,6 +36,7 @@ void GameClient::AddAllowedMover(Unit* unit) void GameClient::RemoveAllowedMover(Unit* unit) { + unit->PurgeAndApplyPendingMovementChanges(); _allowedMovers.erase(unit->GetGUID()); if (unit->GetGameClientMovingMe() == this) { diff --git a/src/server/game/Server/GameClient.h b/src/server/game/Server/GameClient.h index 4baf5015dd..d44f162571 100644 --- a/src/server/game/Server/GameClient.h +++ b/src/server/game/Server/GameClient.h @@ -38,6 +38,7 @@ class TC_GAME_API GameClient void SetActivelyMovedUnit(Unit* activelyMovedUnit) { _activelyMovedUnit = activelyMovedUnit; } Player* GetBasePlayer() const { return _sessionToServer->GetPlayer(); } + WorldSession* GetWorldSession() const { return _sessionToServer; } void SendDirectMessage(WorldPacket const* data) const; private: diff --git a/src/server/game/World/World.cpp b/src/server/game/World/World.cpp index 7c9fa23c70..3a7bf34bae 100644 --- a/src/server/game/World/World.cpp +++ b/src/server/game/World/World.cpp @@ -1610,6 +1610,9 @@ void World::LoadConfigSettings(bool reload) // Allow to cache data queries m_bool_configs[CONFIG_CACHE_DATA_QUERIES] = sConfigMgr->GetBoolDefault("CacheDataQueries", true); + // Anti movement cheat measure. Time each client have to acknowledge a movement change until they are kicked + m_int_configs[CONFIG_PENDING_MOVE_CHANGES_TIMEOUT] = sConfigMgr->GetIntDefault("AntiCheat.PendingMoveChangesTimeoutTime", 0); + // call ScriptMgr if we're reloading the configuration if (reload) sScriptMgr->OnConfigLoad(reload); diff --git a/src/server/game/World/World.h b/src/server/game/World/World.h index 7dda5f7f16..6a81822643 100644 --- a/src/server/game/World/World.h +++ b/src/server/game/World/World.h @@ -417,6 +417,7 @@ enum WorldIntConfigs CONFIG_RESPAWN_DYNAMICMINIMUM_GAMEOBJECT, CONFIG_RESPAWN_GUIDWARNING_FREQUENCY, CONFIG_RATED_BATTLEGROUND_ENABLE, + CONFIG_PENDING_MOVE_CHANGES_TIMEOUT, INT_CONFIG_VALUE_COUNT }; diff --git a/src/server/worldserver/worldserver.conf.dist b/src/server/worldserver/worldserver.conf.dist index 9c82fcb035..b26019dc37 100644 --- a/src/server/worldserver/worldserver.conf.dist +++ b/src/server/worldserver/worldserver.conf.dist @@ -37,6 +37,7 @@ # LOGGING SYSTEM SETTINGS # CURRENCIES SETTINGS # PACKET SPOOF PROTECTION SETTINGS +# MISC ANTI-CHEAT SETTINGS # METRIC SETTINGS # ################################################################################################### @@ -4213,6 +4214,24 @@ PacketSpoof.BanDuration = 86400 # ################################################################################################### +################################################################################################### +# MISC ANTI-CHEAT SETTINGS +# +# Settings to tweak various anti cheat measures. +# +# AntiCheats.PendingMoveChangesTimeoutTime +# Description: Time each client has to acknowledge a movement change until they are kicked. +# Note: A lower time will give cheaters less advantage when using a very specific +# type of hack. However, the cost is that innocent but laggy players might +# sometimes get kicked. Disabled if 0. +# Default: 0 - (Disabled) +# 3000 - (3 seconds, Recommended) + +AntiCheat.PendingMoveChangesTimeoutTime = 0 + +# +################################################################################################### + ################################################################################################### # IPC SETTINGS #