Skip to content

Commit

Permalink
Restore hunters ability to switch to melee attack (#325)
Browse files Browse the repository at this point in the history
* Restore hunters ability to switch to melee attack

Should help low level hunters attack with melee if can't escape or slow down enemy
Should also help if no way to slow down enemy and no pet or pet is dead and enemy is an NPC with hp lower than hunter's

* Use strict greater in switch to ranged

* Do not use separate strategy
  • Loading branch information
celguar authored Dec 19, 2023
1 parent 31f5c8f commit dc2220d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 41 deletions.
37 changes: 20 additions & 17 deletions playerbot/strategy/actions/CombatActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,31 @@ using namespace ai;

bool SwitchToMeleeAction::isUseful()
{
if (bot->getClass() == CLASS_HUNTER)
{
Unit* target = AI_VALUE(Unit*, "current target");
time_t lastFlee = AI_VALUE(LastMovement&, "last movement").lastFlee;
return ai->HasStrategy("ranged", BotState::BOT_STATE_COMBAT) && ((sServerFacade.IsInCombat(bot) && target && (target->GetVictim() == bot && (!bot->GetGroup() || lastFlee) &&
sServerFacade.IsDistanceLessOrEqualThan(AI_VALUE2(float, "distance", "current target"), 8.0f))) ||
(!sServerFacade.IsInCombat(bot)));
}

return ai->HasStrategy("ranged", BotState::BOT_STATE_COMBAT);
}

bool SwitchToRangedAction::isUseful()
bool SwitchToMeleeAction::Execute(Event &event)
{
if (bot->getClass() == CLASS_HUNTER)
if (Unit* target = AI_VALUE(Unit*, "current target"))
{
Unit* target = AI_VALUE(Unit*, "current target");
bool hasAmmo = AI_VALUE2(uint32, "item count", "ammo");
return ai->HasStrategy("close", BotState::BOT_STATE_COMBAT) && hasAmmo && ((sServerFacade.IsInCombat(bot) && target && ((target->GetVictim() != bot || target->GetTarget() != bot) ||
sServerFacade.IsDistanceGreaterThan(AI_VALUE2(float, "distance", "current target"), 8.0f))) ||
(!sServerFacade.IsInCombat(bot)));
bot->MeleeAttackStart(target);
return ChangeCombatStrategyAction::Execute(event);
}
return false;
}

bool SwitchToRangedAction::isUseful()
{

return ai->HasStrategy("close", BotState::BOT_STATE_COMBAT);
}
}

bool SwitchToRangedAction::Execute(Event &event)
{
if (Unit* target = AI_VALUE(Unit*, "current target"))
{
bot->MeleeAttackStop(target);
return ChangeCombatStrategyAction::Execute(event);
}
return false;
}
2 changes: 2 additions & 0 deletions playerbot/strategy/actions/CombatActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ namespace ai
public:
SwitchToMeleeAction(PlayerbotAI* ai) : ChangeCombatStrategyAction(ai, "-ranged,+close") {}
bool isUseful() override;
bool Execute(Event& event) override;
};

class SwitchToRangedAction : public ChangeCombatStrategyAction
{
public:
SwitchToRangedAction(PlayerbotAI* ai) : ChangeCombatStrategyAction(ai, "-close,+ranged") {}
bool isUseful() override;
bool Execute(Event& event) override;
};
}
15 changes: 11 additions & 4 deletions playerbot/strategy/actions/MovementActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1622,11 +1622,18 @@ bool MovementAction::Flee(Unit *target)
return false;
}

HostileReference *ref = sServerFacade.GetThreatManager(target).getCurrentVictim();
const bool isTarget = ref && ref->getTarget() == bot;

time_t lastFlee = AI_VALUE(LastMovement&, "last movement").lastFlee;
time_t now = time(0);
uint32 fleeDelay = urand(2, sPlayerbotAIConfig.returnDelay / 1000);

if (lastFlee && bot->IsMoving())
time_t now = time(0);
uint32 fleeDelay = urand(2, sPlayerbotAIConfig.returnDelay / 1000);

// let hunter kite mob
if (isTarget && bot->getClass() == CLASS_HUNTER)
fleeDelay = 1;

if (lastFlee && sServerFacade.isMoving(bot))
{
if ((now - lastFlee) <= fleeDelay)
{
Expand Down
20 changes: 7 additions & 13 deletions playerbot/strategy/hunter/HunterStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@ void HunterStrategy::InitCombatTriggers(std::list<TriggerNode*>& triggers)
{
ClassStrategy::InitCombatTriggers(triggers);

/*
triggers.push_back(new TriggerNode(
"switch to ranged",
NextAction::array(0, new NextAction("switch to ranged", ACTION_HIGH), NULL)));
NextAction::array(0, new NextAction("switch to ranged", ACTION_INTERRUPT + 3), NULL)));

triggers.push_back(new TriggerNode(
"switch to melee",
NextAction::array(0, new NextAction("switch to melee", ACTION_HIGH + 1), NULL)));
*/
NextAction::array(0, new NextAction("switch to melee", ACTION_MOVE), NULL)));

triggers.push_back(new TriggerNode(
"counterattack",
Expand Down Expand Up @@ -518,15 +516,13 @@ void HunterStrategy::InitCombatTriggers(std::list<TriggerNode*>& triggers)
{
ClassStrategy::InitCombatTriggers(triggers);

/*
triggers.push_back(new TriggerNode(
"switch to ranged",
NextAction::array(0, new NextAction("switch to ranged", ACTION_HIGH), NULL)));
NextAction::array(0, new NextAction("switch to ranged", ACTION_INTERRUPT + 3), NULL)));

triggers.push_back(new TriggerNode(
"switch to melee",
NextAction::array(0, new NextAction("switch to melee", ACTION_HIGH + 1), NULL)));
*/
NextAction::array(0, new NextAction("switch to melee", ACTION_MOVE), NULL)));

triggers.push_back(new TriggerNode(
"counterattack",
Expand Down Expand Up @@ -968,15 +964,13 @@ void HunterStrategy::InitCombatTriggers(std::list<TriggerNode*>& triggers)
{
ClassStrategy::InitCombatTriggers(triggers);

/*
triggers.push_back(new TriggerNode(
"switch to ranged",
NextAction::array(0, new NextAction("switch to ranged", ACTION_HIGH), NULL)));
NextAction::array(0, new NextAction("switch to ranged", ACTION_INTERRUPT + 3), NULL)));

triggers.push_back(new TriggerNode(
"switch to melee",
NextAction::array(0, new NextAction("switch to melee", ACTION_HIGH + 1), NULL)));
*/
NextAction::array(0, new NextAction("switch to melee", ACTION_MOVE), NULL)));

triggers.push_back(new TriggerNode(
"counterattack",
Expand Down Expand Up @@ -1462,4 +1456,4 @@ void HunterPetStrategy::InitNonCombatTriggers(std::list<TriggerNode*>& triggers)
triggers.push_back(new TriggerNode(
"often",
NextAction::array(0, new NextAction("initialize pet", ACTION_NORMAL + 1), NULL)));
}
}
2 changes: 1 addition & 1 deletion playerbot/strategy/hunter/HunterStrategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,4 @@ namespace ai
void InitCombatTriggers(std::list<TriggerNode*>& triggers) override;
void InitNonCombatTriggers(std::list<TriggerNode*>& triggers) override;
};
}
}
35 changes: 29 additions & 6 deletions playerbot/strategy/hunter/HunterTriggers.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,23 @@ namespace ai
public:
SwitchToRangedTrigger(PlayerbotAI* ai) : Trigger(ai, "switch to ranged", 1) {}

virtual bool IsActive()
bool IsActive() override
{
#ifdef MANGOSBOT_ZERO
bool hasAmmo = ai->HasCheat(BotCheatMask::item) || AI_VALUE2(uint32, "item count", "ammo");
#else
bool hasAmmo = ai->HasCheat(BotCheatMask::item) || bot->HasAura(46699) || AI_VALUE2(uint32, "item count", "ammo");
#endif
if (!hasAmmo)
return false;

Unit* target = AI_VALUE(Unit*, "current target");
return ai->HasStrategy("close", BotState::BOT_STATE_COMBAT) && target && (target->GetVictim() != bot ||
sServerFacade.IsDistanceGreaterThan(AI_VALUE2(float, "distance", "current target"), 8.0f));
float distance = AI_VALUE2(float, "distance", "current target");
return target && ai->HasStrategy("close", BotState::BOT_STATE_COMBAT) &&
(target->GetVictim() != bot ||
target->IsImmobilizedState() ||
(target->GetSpeed(MOVE_RUN) <= (bot->GetSpeed(MOVE_RUN) / 2) && !((!bot->GetPet() || bot->GetPet()->IsDead()) && target->IsCreature() && target->GetHealthPercent() < 50.f && target->GetHealth() < bot->GetHealth())) ||
distance > 8.0f);
}
};

Expand All @@ -232,11 +244,22 @@ namespace ai
public:
SwitchToMeleeTrigger(PlayerbotAI* ai) : Trigger(ai, "switch to melee", 1) {}

virtual bool IsActive()
bool IsActive() override
{
#ifdef MANGOSBOT_ZERO
bool hasAmmo = ai->HasCheat(BotCheatMask::item) || AI_VALUE2(uint32, "item count", "ammo");
#else
bool hasAmmo = ai->HasCheat(BotCheatMask::item) || bot->HasAura(46699) || AI_VALUE2(uint32, "item count", "ammo");
#endif
if (!hasAmmo)
return true;

Unit* target = AI_VALUE(Unit*, "current target");
return ai->HasStrategy("ranged", BotState::BOT_STATE_COMBAT) && target && (target->GetVictim() == bot ||
sServerFacade.IsDistanceLessOrEqualThan(AI_VALUE2(float, "distance", "current target"), 8.0f));
return target && ((target->GetSpeed(MOVE_RUN) > (bot->GetSpeed(MOVE_RUN) / 2)) || ((!bot->GetPet() || bot->GetPet()->IsDead()) && target->GetHealthPercent() < 50.f && target->IsCreature() && target->GetHealth() < bot->GetHealth())) &&
!target->IsImmobilizedState() &&
ai->HasStrategy("ranged", BotState::BOT_STATE_COMBAT) &&
target->GetVictim() == bot &&
sServerFacade.IsDistanceLessOrEqualThan(AI_VALUE2(float, "distance", "current target"), 8.0f);
}
};

Expand Down

0 comments on commit dc2220d

Please sign in to comment.