Modify UpdateAI to Allow Future Methods to Interrupt Bot Spells (#2295)

Note: Resubmitted because the prior PR had master for the source

<!--
Thank you for contributing to mod-playerbots, please make sure that
you...
1. Submit your PR to the test-staging branch, not master.
2. Read the guidelines below before submitting.
3. Don't delete parts of this template.

DESIGN PHILOSOPHY: We prioritize STABILITY, PERFORMANCE, AND
PREDICTABILITY over behavioral realism.

Every action and decision executes PER BOT AND PER TRIGGER. Small
increases in logic complexity scale
poorly across thousands of bots and negatively affect all. We prioritize
a stable system over a smarter
one. Bots don't need to behave perfectly; believable behavior is the
goal, not human simulation.
Default behavior must be cheap in processing; expensive behavior must be
opt-in.

Before submitting, make sure your changes aligns with these principles.
-->

## Pull Request Description
<!-- Describe what this change does and why it is needed -->

This PR adds to PlayerbotAI::UpdateAI a boolean variable,
pendingCastInterrupt, and a public function, RequestCastInterrupt(),
that toggles the variable and would then call InterruptSpells in
UpdateAI. This lets an external script hook into UpdateAI to interrupt a
spell that is in the process of being cast. This should allow
raid/dungeon strategies to actually interrupt spells, such as for
avoiding hazards, as you cannot do so by calling InterruptSpells() or
Reset() or anything else with a raid/dungeon action method.

## Feature Evaluation
<!--
If your PR is very minimal (comment typo, wrong ID reference, etc), and
it is very obvious it will not have
any impact on performance, you may skip these question. If necessary, a
maintainer may ask you for them later.
-->

<!-- Please answer the following: -->
- Describe the **minimum logic** required to achieve the intended
behavior.
- Describe the **processing cost** when this logic executes across many
bots.

There is no processing cost from this PR itself since it just adds a
simple boolean check that will always return false unless other methods
are implemented to call RequestCastInterrupt().

## How to Test the Changes
<!--
- Step-by-step instructions to test the change.
- Any required setup (e.g. multiple players, number of bots, specific
configuration).
- Expected behavior and how to verify it.
-->

This PR doesn't do anything by itself, but confirmation of no
performance impact can be tested by just playing with the PR merged. I
tested this by introducing scripts that called RequestCastInterrupt()
for Archimonde (included in the Hyjal PR now) and for Auchenai Crypts
(built on the strategy from flashtate's PR just for test purposes), and
spells were interrupted as intended.

## Impact Assessment
<!-- As a generic test, before and after measure of pmon (playerbot pmon
tick) can help you here. -->
- Does this change increase per-bot/per-tick processing or risk scaling
poorly with thousands of bots?
    - - [x] No, not at all
    - - [ ] Minimal impact (**explain below**)
    - - [ ] Moderate impact (**explain below**)



- Does this change modify default bot behavior?
    - - [x] No
    - - [ ] Yes (**explain why**)



- Does this change add new decision branches or increase maintenance
complexity?
    - - [ ] No
    - - [x] Yes (**explain below**)

I'm not sure if it counts as "yes" here, but this PR opens up the
ability for scripts to be added that would add real checks to UpdateAI.
The impact of such scripts will depend on how they are implemented,
however. It is possible (and intended) for the external calls to be
highly limited in scope (e.g., only bots in a particular circumstance in
a particular boss fight).

## AI Assistance
<!--
AI assistance is allowed, but all submitted code must be fully
understood, reviewed, and owned by the contributor.
We expect contributors to be honest about what they do and do not
understand.
-->
Was AI assistance used while working on this change?
- - [ ] No
- - [x] Yes (**explain below**)
<!--
If yes, please specify:
- Purpose of usage (e.g. brainstorming, refactoring, documentation, code
generation).
- Which parts of the change were influenced or generated, and whether it
was thoroughly reviewed.
-->

Claude Sonnet 4.6 was used to brainstorm different possibilities to
allow external scripts to interrupt spells. I considered different
options before settling on this one due to it not requiring core
changes, being easily usable in boss strategies, and not having any
performance impact on its own.

<!--
TRANSLATIONS:
Anything new that the bots say in chat must be in a translatable format.
This is done using GetBotTextOrDefault,
which you can search for in the codebase to find examples. Your code
needs to have English as the default fallback,
while the full translations need to be in an SQL update file. The
languages in the file are the nine language
options supported by AzerothCore: English, Korean, French, German,
Chinese, Taiwanese, Spanish, Spanish Mexico, and
Russian. See
data/sql/playerbots/updates/2025_12_27_ai_playerbot_fishing_text.sql as
an example of a translation SQL
update, whose content are called within the codebase at
src/strategy/actions/FishingAction.cpp
-->

## Final Checklist

- - [x] Stability is not compromised.
- - [x] Performance impact is understood, tested, and acceptable.
- - [x] Added logic complexity is justified and explained.
- - [x] Any new bot dialogue lines are translated.
- - [x] Documentation updated if needed (Conf comments, WiKi commands).

## Notes for Reviewers
<!-- Anything else that's helpful to review or test your pull request.
-->

---------

Co-authored-by: Keleborn <22352763+Celandriel@users.noreply.github.com>
Co-authored-by: bash <hermensb@gmail.com>
Co-authored-by: Revision <tkn963@gmail.com>
Co-authored-by: kadeshar <kadeshar@gmail.com>
This commit is contained in:
Crow 2026-04-17 13:27:11 -05:00 committed by GitHub
parent d01316fe64
commit a34681bd7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 92 additions and 50 deletions

View File

@ -266,15 +266,26 @@ void PlayerbotAI::UpdateAI(uint32 elapsed, bool minimal)
if (!CanUpdateAI()) if (!CanUpdateAI())
return; return;
// Handle the current spell // Handle a spell that is still in its preparing phase (including channeled spells).
Spell* currentSpell = bot->GetCurrentSpell(CURRENT_GENERIC_SPELL); Spell* currentSpell = bot->GetCurrentSpell(CURRENT_GENERIC_SPELL);
if (!currentSpell) if (!currentSpell)
currentSpell = bot->GetCurrentSpell(CURRENT_CHANNELED_SPELL); currentSpell = bot->GetCurrentSpell(CURRENT_CHANNELED_SPELL);
if (currentSpell) if (currentSpell)
{ {
if (currentSpell->getState() == SPELL_STATE_PREPARING)
{
// Allow external scripts to interrupt a cast in progress
if (spellInterruptRequested)
{
spellInterruptRequested = false;
InterruptSpell();
YieldThread(bot, GetReactDelay());
return;
}
const SpellInfo* spellInfo = currentSpell->GetSpellInfo(); const SpellInfo* spellInfo = currentSpell->GetSpellInfo();
if (spellInfo && currentSpell->getState() == SPELL_STATE_PREPARING) if (spellInfo)
{ {
Unit* spellTarget = currentSpell->m_targets.GetUnitTarget(); Unit* spellTarget = currentSpell->m_targets.GetUnitTarget();
// Interrupt if target is dead or spell can't target dead units // Interrupt if target is dead or spell can't target dead units
@ -338,6 +349,22 @@ void PlayerbotAI::UpdateAI(uint32 elapsed, bool minimal)
return; return;
} }
} }
}
if (spellInterruptRequested)
{
// At this point the preparing-cast branch above did not consume the request.
// Interrupt a current channel if one still exists; otherwise, clear the stale request.
if (bot->GetCurrentSpell(CURRENT_CHANNELED_SPELL))
{
spellInterruptRequested = false;
InterruptSpell();
YieldThread(bot, GetReactDelay());
return;
}
spellInterruptRequested = false;
}
// Handle transport check delay // Handle transport check delay
if (nextTransportCheck > elapsed) if (nextTransportCheck > elapsed)
@ -1598,7 +1625,7 @@ void PlayerbotAI::ApplyInstanceStrategies(uint32 mapId, bool tellMaster)
strategyName = "ssc"; // Serpentshrine Cavern strategyName = "ssc"; // Serpentshrine Cavern
break; break;
case 550: case 550:
strategyName = "tempestkeep"; // Tempest Keep strategyName = "tempestkeep"; // Tempest Keep: The Eye
break; break;
case 558: case 558:
strategyName = "tbc-ac"; // Auchindoun: Auchenai Crypts strategyName = "tbc-ac"; // Auchindoun: Auchenai Crypts
@ -4192,6 +4219,19 @@ void PlayerbotAI::RemoveAura(std::string const name)
bot->RemoveAurasDueToSpell(spellid); bot->RemoveAurasDueToSpell(spellid);
} }
void PlayerbotAI::RequestSpellInterrupt()
{
Spell* currentSpell = bot->GetCurrentSpell(CURRENT_GENERIC_SPELL);
if (currentSpell && currentSpell->getState() == SPELL_STATE_PREPARING)
{
spellInterruptRequested = true;
return;
}
if (bot->GetCurrentSpell(CURRENT_CHANNELED_SPELL))
spellInterruptRequested = true;
}
bool PlayerbotAI::IsInterruptableSpellCasting(Unit* target, std::string const spell) bool PlayerbotAI::IsInterruptableSpellCasting(Unit* target, std::string const spell)
{ {
if (!IsValidUnit(target)) if (!IsValidUnit(target))

View File

@ -3,8 +3,8 @@
* and/or modify it under version 3 of the License, or (at your option), any later version. * and/or modify it under version 3 of the License, or (at your option), any later version.
*/ */
#ifndef _PLAYERBOT_PLAYERbotAI_H #ifndef _PLAYERBOT_PLAYERBOTAI_H
#define _PLAYERBOT_PLAYERbotAI_H #define _PLAYERBOT_PLAYERBOTAI_H
#include <stack> #include <stack>
@ -471,6 +471,7 @@ public:
void SpellInterrupted(uint32 spellid); void SpellInterrupted(uint32 spellid);
int32 CalculateGlobalCooldown(uint32 spellid); int32 CalculateGlobalCooldown(uint32 spellid);
void InterruptSpell(); void InterruptSpell();
void RequestSpellInterrupt();
void RemoveAura(std::string const name); void RemoveAura(std::string const name);
void RemoveShapeshift(); void RemoveShapeshift();
void WaitForSpellCast(Spell* spell); void WaitForSpellCast(Spell* spell);
@ -647,6 +648,7 @@ protected:
BotCheatMask cheatMask = BotCheatMask::none; BotCheatMask cheatMask = BotCheatMask::none;
Position jumpDestination = Position(); Position jumpDestination = Position();
uint32 nextTransportCheck = 0; uint32 nextTransportCheck = 0;
bool spellInterruptRequested = false;
}; };
#endif #endif