From dc21fa9d412e8d6ddabf2a466e8b8e0fb3b6a07c Mon Sep 17 00:00:00 2001 From: Yunfan Li Date: Sun, 28 May 2023 10:55:56 +0800 Subject: [PATCH] pet attack and bot target selection --- src/PlayerbotAI.cpp | 3 +- src/strategy/actions/AttackAction.cpp | 19 +++++++----- src/strategy/actions/AttackAction.h | 2 +- src/strategy/actions/ChooseTargetActions.cpp | 1 + src/strategy/actions/EmoteAction.cpp | 2 +- src/strategy/actions/MovementActions.cpp | 30 ++++++++----------- src/strategy/actions/ReachTargetActions.cpp | 5 +++- .../GenericPaladinStrategyActionNodeFactory.h | 2 +- 8 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/PlayerbotAI.cpp b/src/PlayerbotAI.cpp index aa75b715..319b5f42 100644 --- a/src/PlayerbotAI.cpp +++ b/src/PlayerbotAI.cpp @@ -1718,7 +1718,6 @@ bool PlayerbotAI::CanCastSpell(uint32 spellid, Unit* target, bool checkHasSpell, } ObjectGuid oldSel = bot->GetTarget(); - bot->SetTarget(target->GetGUID()); Spell* spell = new Spell(bot, spellInfo, TRIGGERED_NONE); spell->m_targets.SetUnitTarget(target); @@ -1933,7 +1932,7 @@ bool PlayerbotAI::CastSpell(uint32 spellId, Unit* target, Item* itemTarget) } ObjectGuid oldSel = bot->GetTarget(); - bot->SetTarget(target->GetGUID()); + bot->SetSelection(target->GetGUID()); WorldObject* faceTo = target; if (!bot->HasInArc(CAST_ANGLE_IN_FRONT, faceTo)) diff --git a/src/strategy/actions/AttackAction.cpp b/src/strategy/actions/AttackAction.cpp index 8a6a12e4..c425cc2f 100644 --- a/src/strategy/actions/AttackAction.cpp +++ b/src/strategy/actions/AttackAction.cpp @@ -8,6 +8,7 @@ #include "Playerbots.h" #include "ServerFacade.h" #include "CreatureAI.h" +#include "Unit.h" bool AttackAction::Execute(Event event) { @@ -40,7 +41,7 @@ bool AttackMyTargetAction::Execute(Event event) return result; } -bool AttackAction::Attack(Unit* target) +bool AttackAction::Attack(Unit* target, bool with_pet /*true*/) { if (bot->GetMotionMaster()->GetCurrentMovementGeneratorType() == FLIGHT_MOTION_TYPE || bot->HasUnitState(UNIT_STATE_IN_FLIGHT)) { @@ -93,21 +94,25 @@ bool AttackAction::Attack(Unit* target) } ObjectGuid guid = target->GetGUID(); - bot->SetTarget(target->GetGUID()); + bot->SetSelection(target->GetGUID()); Unit* oldTarget = context->GetValue("current target")->Get(); context->GetValue("old target")->Set(oldTarget); context->GetValue("current target")->Set(target); context->GetValue("available loot")->Get()->Add(guid); - + if (Pet* pet = bot->GetPet()) { - if (CreatureAI* creatureAI = ((Creature*)pet)->AI()) - { - pet->SetReactState(REACT_PASSIVE); + pet->SetReactState(REACT_PASSIVE); + if (with_pet) { + pet->SetTarget(target->GetGUID()); pet->GetCharmInfo()->SetCommandState(COMMAND_ATTACK); - creatureAI->AttackStart(target); + pet->GetCharmInfo()->SetIsCommandAttack(true); + pet->AI()->AttackStart(target); + } else { + pet->GetCharmInfo()->SetCommandState(COMMAND_FOLLOW); + pet->GetCharmInfo()->SetIsCommandFollow(true); } } diff --git a/src/strategy/actions/AttackAction.h b/src/strategy/actions/AttackAction.h index ef415da1..b606eea2 100644 --- a/src/strategy/actions/AttackAction.h +++ b/src/strategy/actions/AttackAction.h @@ -17,7 +17,7 @@ class AttackAction : public MovementAction bool Execute(Event event) override; protected: - bool Attack(Unit* target); + bool Attack(Unit* target, bool with_pet = true); }; class AttackMyTargetAction : public AttackAction diff --git a/src/strategy/actions/ChooseTargetActions.cpp b/src/strategy/actions/ChooseTargetActions.cpp index bc0d1374..732fcc23 100644 --- a/src/strategy/actions/ChooseTargetActions.cpp +++ b/src/strategy/actions/ChooseTargetActions.cpp @@ -82,6 +82,7 @@ bool DropTargetAction::Execute(Event event) { pet->SetReactState(REACT_PASSIVE); pet->GetCharmInfo()->SetCommandState(COMMAND_FOLLOW); + pet->GetCharmInfo()->SetIsCommandFollow(true); pet->AttackStop(); } } diff --git a/src/strategy/actions/EmoteAction.cpp b/src/strategy/actions/EmoteAction.cpp index c1cb7596..99446cd1 100644 --- a/src/strategy/actions/EmoteAction.cpp +++ b/src/strategy/actions/EmoteAction.cpp @@ -94,7 +94,7 @@ bool EmoteActionBase::Emote(Unit* target, uint32 type, bool textEmote) ObjectGuid oldSelection = bot->GetTarget(); if (target) { - bot->SetTarget(target->GetGUID()); + bot->SetSelection(target->GetGUID()); Player* player = dynamic_cast(target); if (player) diff --git a/src/strategy/actions/MovementActions.cpp b/src/strategy/actions/MovementActions.cpp index 4978f83a..bc8bb1a6 100644 --- a/src/strategy/actions/MovementActions.cpp +++ b/src/strategy/actions/MovementActions.cpp @@ -636,7 +636,6 @@ bool MovementAction::MoveTo(Unit* target, float distance) float distanceToTarget = sServerFacade->GetDistance2d(bot, tx, ty); if (sServerFacade->IsDistanceGreaterThan(distanceToTarget, sPlayerbotAIConfig->targetPosRecalcDistance)) { - /* float angle = bot->GetAngle(tx, ty); float needToGo = distanceToTarget - distance; @@ -649,15 +648,10 @@ bool MovementAction::MoveTo(Unit* target, float distance) float dx = cos(angle) * needToGo + bx; float dy = sin(angle) * needToGo + by; float dz = bz + (tz - bz) * needToGo / distanceToTarget; - */ - - float dx = tx; - float dy = ty; - float dz = tz; return MoveTo(target->GetMapId(), dx, dy, dz); } - return true; + return false; } float MovementAction::GetFollowAngle() @@ -991,7 +985,7 @@ bool MovementAction::ChaseTo(WorldObject* obj, float distance, float angle) return false; //vehicle->GetMotionMaster()->Clear(); - vehicle->GetBase()->GetMotionMaster()->MoveChase((Unit*)obj, 30.0f, angle); + vehicle->GetBase()->GetMotionMaster()->MoveChase((Unit*)obj, 30.0f); return true; } @@ -1007,7 +1001,7 @@ bool MovementAction::ChaseTo(WorldObject* obj, float distance, float angle) } // bot->GetMotionMaster()->Clear(); - bot->GetMotionMaster()->MoveChase((Unit*) obj, distance, angle); + bot->GetMotionMaster()->MoveChase((Unit*) obj, distance); WaitForReach(bot->GetExactDist2d(obj) - distance); return true; } @@ -1224,15 +1218,15 @@ bool FleeAction::Execute(Event event) bool FleeWithPetAction::Execute(Event event) { - if (Pet* pet = bot->GetPet()) - { - if (CreatureAI* creatureAI = ((Creature*)pet)->AI()) - { - pet->SetReactState(REACT_PASSIVE); - pet->GetCharmInfo()->SetCommandState(COMMAND_FOLLOW); - pet->AttackStop(); - } - } + // if (Pet* pet = bot->GetPet()) + // { + // if (CreatureAI* creatureAI = ((Creature*)pet)->AI()) + // { + // pet->SetReactState(REACT_PASSIVE); + // pet->GetCharmInfo()->SetCommandState(COMMAND_FOLLOW); + // pet->AttackStop(); + // } + // } return Flee(AI_VALUE(Unit*, "current target")); } diff --git a/src/strategy/actions/ReachTargetActions.cpp b/src/strategy/actions/ReachTargetActions.cpp index 014ccd37..e9b76ded 100644 --- a/src/strategy/actions/ReachTargetActions.cpp +++ b/src/strategy/actions/ReachTargetActions.cpp @@ -4,6 +4,7 @@ #include "ReachTargetActions.h" #include "Event.h" +#include "PlayerbotAIConfig.h" #include "Playerbots.h" #include "ServerFacade.h" @@ -18,7 +19,8 @@ bool ReachTargetAction::Execute(Event event) float combatReach = bot->GetCombatReach() + target->GetCombatReach() + 4.0f / 3.0f; if (distance < std::max(5.0f, combatReach)) { - return ChaseTo(target, 0.0f, GetFollowAngle()); + // return MoveTo(target, 0.0f); + return ChaseTo(target, 0.0f, bot->GetAngle(target)); } else { @@ -27,6 +29,7 @@ bool ReachTargetAction::Execute(Event event) bool isFriend = bot->IsFriendlyTo(target); float chaseDist = inLos ? distance : isFriend ? distance / 2 : distance; return ChaseTo(target, chaseDist - sPlayerbotAIConfig->contactDistance, bot->GetAngle(target)); + // return MoveTo(target, chaseDist - sPlayerbotAIConfig->contactDistance); } } diff --git a/src/strategy/paladin/GenericPaladinStrategyActionNodeFactory.h b/src/strategy/paladin/GenericPaladinStrategyActionNodeFactory.h index cfacc166..b4bd6a6d 100644 --- a/src/strategy/paladin/GenericPaladinStrategyActionNodeFactory.h +++ b/src/strategy/paladin/GenericPaladinStrategyActionNodeFactory.h @@ -100,7 +100,7 @@ class GenericPaladinStrategyActionNodeFactory : public NamedObjectFactory