diff --git a/src/server/game/AI/CoreAI/PetAI.cpp b/src/server/game/AI/CoreAI/PetAI.cpp index 7b0b285e2b..519764b26e 100644 --- a/src/server/game/AI/CoreAI/PetAI.cpp +++ b/src/server/game/AI/CoreAI/PetAI.cpp @@ -261,7 +261,7 @@ void PetAI::_AttackStart(Unit* target) return; // Only chase if not commanded to stay or if stay but commanded to attack - DoAttack(target, (!me->GetCharmInfo()->HasCommandState(COMMAND_STAY) || me->GetCharmInfo()->IsCommandAttack())); + DoAttack(target, (!me->GetCharmInfo()->HasStayCommand() || me->GetCharmInfo()->IsCommandAttack())); } void PetAI::OwnerAttackedBy(Unit* attacker) @@ -364,7 +364,7 @@ void PetAI::HandleReturnMovement() return; } - if (me->GetCharmInfo()->HasCommandState(COMMAND_STAY)) + if (me->GetCharmInfo()->HasStayCommand()) // Stay or Move To { if (!me->GetCharmInfo()->IsAtStay() && !me->GetCharmInfo()->IsReturning()) { @@ -506,8 +506,8 @@ bool PetAI::CanAttack(Unit* target) if (me->GetCharmInfo()->IsReturning()) return !me->GetCharmInfo()->IsCommandFollow(); - // Stay - can attack if target is within range or commanded to - if (me->GetCharmInfo()->HasCommandState(COMMAND_STAY)) + // Stay or Move To - can attack if target is within range or commanded to + if (me->GetCharmInfo()->HasStayCommand()) return (me->IsWithinMeleeRange(target) || me->GetCharmInfo()->IsCommandAttack()); // Pets attacking something (or chasing) should only switch targets if owner tells them to diff --git a/src/server/game/Entities/Pet/Pet.cpp b/src/server/game/Entities/Pet/Pet.cpp index 77fcf9d620..d434cffa94 100644 --- a/src/server/game/Entities/Pet/Pet.cpp +++ b/src/server/game/Entities/Pet/Pet.cpp @@ -802,7 +802,7 @@ bool Pet::CatchUpWithOwner() return false; if (owner->IsInFlight() || owner->IsBeingTeleported() || owner->IsPetNeedBeTemporaryUnsummoned()) return false; - if (GetCharmInfo() && GetCharmInfo()->HasCommandState(COMMAND_STAY)) + if (GetCharmInfo() && GetCharmInfo()->HasStayCommand()) return false; // told to stay there: dismissed as before // A fight it was left at stays behind, or the pet would run straight back to it and drop out of sight again. @@ -821,7 +821,7 @@ bool Pet::ParkForOwnerTravel() Player* owner = GetOwner(); if (!owner || !owner->IsInWorld() || !owner->IsAlive() || !IsInWorld() || !IsAlive() || m_removed || isPossessed() || isTemporarySummoned()) return false; - if (GetCharmInfo() && GetCharmInfo()->HasCommandState(COMMAND_STAY)) + if (GetCharmInfo() && GetCharmInfo()->HasStayCommand()) return false; // told to stay there: dismissed as before if (owner->GetMap() != GetMap()) return false; diff --git a/src/server/game/Entities/Unit/CharmInfo.h b/src/server/game/Entities/Unit/CharmInfo.h index caa34f7ca6..417cf1d1bc 100644 --- a/src/server/game/Entities/Unit/CharmInfo.h +++ b/src/server/game/Entities/Unit/CharmInfo.h @@ -96,6 +96,7 @@ struct TC_GAME_API CharmInfo void SetCommandState(CommandStates st) { _CommandState = st; } CommandStates GetCommandState() const { return _CommandState; } bool HasCommandState(CommandStates state) const { return (_CommandState == state); } + bool HasStayCommand() const { return IsPetStayCommand(_CommandState); } void InitPossessCreateSpells(); void InitCharmCreateSpells(); @@ -129,6 +130,7 @@ struct TC_GAME_API CharmInfo void SetIsReturning(bool val); bool IsReturning(); void SaveStayPosition(); + void SetStayPosition(float x, float y, float z) { _stayX = x; _stayY = y; _stayZ = z; } void GetStayPosition(float &x, float &y, float &z); private: diff --git a/src/server/game/Entities/Unit/UnitDefines.h b/src/server/game/Entities/Unit/UnitDefines.h index dd44fc98b6..9e2b660650 100644 --- a/src/server/game/Entities/Unit/UnitDefines.h +++ b/src/server/game/Entities/Unit/UnitDefines.h @@ -467,4 +467,11 @@ enum CommandStates : uint8 COMMAND_MOVE_TO = 4 }; +// "Move To" parks the pet at the clicked spot, so it behaves like Stay there: it fights what comes into reach or what +// it is told to attack, and goes back to that spot afterwards. +constexpr bool IsPetStayCommand(CommandStates state) +{ + return state == COMMAND_STAY || state == COMMAND_MOVE_TO; +} + #endif // UnitDefines_h__ diff --git a/src/server/game/Handlers/PetHandler.cpp b/src/server/game/Handlers/PetHandler.cpp index 9caa9c494f..bb07189267 100644 --- a/src/server/game/Handlers/PetHandler.cpp +++ b/src/server/game/Handlers/PetHandler.cpp @@ -265,7 +265,9 @@ void WorldSession::HandlePetActionHelper(Unit* pet, ObjectGuid guid1, uint32 spe charmInfo->SetIsAtStay(true); charmInfo->SetIsFollowing(false); charmInfo->SetIsReturning(false); - charmInfo->SaveStayPosition(); + // The point movement above only starts on the next motion update, so the spline still ends where + // the pet stands now: store the clicked spot itself, which is where the pet returns after a fight. + charmInfo->SetStayPosition(pos.GetPositionX(), pos.GetPositionY(), pos.GetPositionZ()); break; default: TC_LOG_ERROR("entities.pet", "WORLD: unknown PET flag Action {} and spellid {}.", uint32(flag), spellid); diff --git a/src/server/game/Spells/Auras/SpellAuraEffects.cpp b/src/server/game/Spells/Auras/SpellAuraEffects.cpp index eadfe21dc3..67c6ab9291 100644 --- a/src/server/game/Spells/Auras/SpellAuraEffects.cpp +++ b/src/server/game/Spells/Auras/SpellAuraEffects.cpp @@ -3327,7 +3327,7 @@ void AuraEffect::HandleModPossessPet(AuraApplication const* aurApp, uint8 mode, caster->ToPlayer()->PetSpellInitialize(); // TODO: remove this - if (!pet->GetVictim() && !pet->GetCharmInfo()->HasCommandState(COMMAND_STAY)) + if (!pet->GetVictim() && !pet->GetCharmInfo()->HasStayCommand()) pet->GetMotionMaster()->MoveFollow(caster, PET_FOLLOW_DIST, pet->GetFollowAngle()); } } diff --git a/src/server/game/Spells/SpellEffects.cpp b/src/server/game/Spells/SpellEffects.cpp index 576c93028c..5352677265 100644 --- a/src/server/game/Spells/SpellEffects.cpp +++ b/src/server/game/Spells/SpellEffects.cpp @@ -4465,7 +4465,7 @@ void Spell::EffectResurrectPet() { // In case the pet was at stay, we don't want it running back ci->SaveStayPosition(); - ci->SetIsAtStay(ci->HasCommandState(COMMAND_STAY)); + ci->SetIsAtStay(ci->HasStayCommand()); ci->SetIsFollowing(false); ci->SetIsCommandAttack(false); diff --git a/tests/game/PetStayCommand.cpp b/tests/game/PetStayCommand.cpp new file mode 100644 index 0000000000..2eadf6cdc0 --- /dev/null +++ b/tests/game/PetStayCommand.cpp @@ -0,0 +1,48 @@ +#include "tc_catch2.h" +#include "CharmInfo.h" +#include "Creature.h" + +// "Move To" must leave a pet in the same mode as "Stay" at the new spot. PetAI only knew COMMAND_STAY and +// COMMAND_FOLLOW, so after Move To the pet refused every target, even an explicit Attack, until it was re-summoned. + +TEST_CASE("Stay and Move To are stay commands; Follow, Attack and Abandon are not", "[PetStayCommand]") +{ + STATIC_CHECK(IsPetStayCommand(COMMAND_STAY)); + STATIC_CHECK(IsPetStayCommand(COMMAND_MOVE_TO)); + STATIC_CHECK_FALSE(IsPetStayCommand(COMMAND_FOLLOW)); + STATIC_CHECK_FALSE(IsPetStayCommand(COMMAND_ATTACK)); + STATIC_CHECK_FALSE(IsPetStayCommand(COMMAND_ABANDON)); +} + +TEST_CASE("A pet's charm info reports a stay command after Stay or Move To", "[PetStayCommand]") +{ + Creature pet(false); + CharmInfo charmInfo(&pet); + + // New pets start on Follow. + CHECK_FALSE(charmInfo.HasStayCommand()); + + charmInfo.SetCommandState(COMMAND_MOVE_TO); + CHECK(charmInfo.HasStayCommand()); + CHECK_FALSE(charmInfo.HasCommandState(COMMAND_STAY)); + + charmInfo.SetCommandState(COMMAND_STAY); + CHECK(charmInfo.HasStayCommand()); + + charmInfo.SetCommandState(COMMAND_FOLLOW); + CHECK_FALSE(charmInfo.HasStayCommand()); +} + +TEST_CASE("Move To stores the clicked spot as the stay position", "[PetStayCommand]") +{ + // The handler cannot read the spot from the spline: the point movement it just queued has not started yet. + Creature pet(false); + CharmInfo charmInfo(&pet); + + charmInfo.SetStayPosition(-8913.5f, 554.25f, 93.75f); + float x = 0.0f, y = 0.0f, z = 0.0f; + charmInfo.GetStayPosition(x, y, z); + CHECK(x == -8913.5f); + CHECK(y == 554.25f); + CHECK(z == 93.75f); +}