Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core/SmartAI: AI change on charmed #26065

Merged
merged 3 commits into from
Feb 14, 2021

Conversation

engilas
Copy link
Contributor

@engilas engilas commented Feb 9, 2021

Changes proposed:

  • SmartAI calls base method UnitAI::OnCharmed to update AI on charmed

Without this change, charmed creature can't update AI.

Target branch(es): 3.3.5/master

  • 3.3.5
  • master

Issues addressed:

Closes #25982 (melee attack works correctly)
Partially #23582 (part 2: Enslaved demons should be able to attcak)

Tests performed:

Built and tested with warlock's 'Enslave Demon' spell, priest's 'Mind control' spell.

src/server/game/Entities/Unit/Unit.cpp Outdated Show resolved Hide resolved
@jackpoz
Copy link
Member

jackpoz commented Feb 9, 2021

one thing that I wonder is if for some reason SmartAI should still stay even when charmed.
@offl do you see any issue with this PR ? is it ok if a SmartAI creature loses the SmartAI while being charmed and then regains it when the charm goes away ?

@offl
Copy link
Contributor

offl commented Feb 9, 2021

is it ok if a SmartAI creature loses the SmartAI while being charmed and then regains it when the charm goes away ?

We have SMART_EVENT_FLAG_WHILE_CHARMED (Event can occur while player controlled)
Means we want SmartAI being active even if creature is charmed

And since people used that flag in every script for years without any reason and that flag was added by mass update without any reason, we don't have a clear list of creatures which indeed should have that flag, but that's a different story

@engilas
Copy link
Contributor Author

engilas commented Feb 9, 2021

It's too many logic in PetAI for commands, I think I can close this PR.

@engilas engilas closed this Feb 9, 2021
@jackpoz jackpoz reopened this Feb 9, 2021
@jackpoz
Copy link
Member

jackpoz commented Feb 9, 2021

we can leave it open for a while as a why to discuss about the issue. it makes no sense to me that every single SAI creature doesn't change AI when charmed but it might take some time before we can merge this

@jackpoz
Copy link
Member

jackpoz commented Feb 9, 2021

@engilas we came up with a compromise: SmartAI::OnCharmed() should schedule an AI change if there are no events with SMART_EVENT_FLAG_WHILE_CHARMED flag (since there are quests that require SAI to trigger some script stuff when charmed).

This should fix the issue in most of the cases, except for some cases where we still need SAI.

I don't know if there is some sort of "IsThereAnyEventWithFlag(flag)" that we can use or if we need to implement it, is that something you would like to work on or should I assign it to myself ?

@engilas
Copy link
Contributor Author

engilas commented Feb 9, 2021

@jackpoz Yes, I can try to implement it

@engilas engilas requested a review from jackpoz February 10, 2021 07:19
@engilas
Copy link
Contributor Author

engilas commented Feb 10, 2021

@jackpoz added check for flag SMART_EVENT_FLAG_WHILE_CHARMED

@engilas engilas changed the title Core/Unit: Unit AI refresh on charmed Core/SmartAI: AI change on charmed Feb 10, 2021
@ccrs
Copy link
Member

ccrs commented Feb 10, 2021

SmartAI cant go, must stay, this way smart scripts can keep control on what happens.

The problem is that, as discussed here and a couple years ago, there is no implementation for PetAI-like features, like commands and such. I remember rising this myself. Nothing was finally done, left as is.

@ccrs
Copy link
Member

ccrs commented Feb 10, 2021

@engilas we came up with a compromise: SmartAI::OnCharmed() should schedule an AI change if there are no events with SMART_EVENT_FLAG_WHILE_CHARMED flag (since there are quests that require SAI to trigger some script stuff when charmed).

This should fix the issue in most of the cases, except for some cases where we still need SAI.

I don't know if there is some sort of "IsThereAnyEventWithFlag(flag)" that we can use or if we need to implement it, is that something you would like to work on or should I assign it to myself ?

Aaaaand I didnt read that, sorry. Feels ok I guess.

@BAndysc
Copy link
Contributor

BAndysc commented Feb 10, 2021

@jackpoz @engilas

SmartAI::OnCharmed() should schedule an AI change if there are no events with SMART_EVENT_FLAG_WHILE_CHARMED flag (since there are quests that require SAI to trigger some script stuff when charmed).

IMHO this introduces hard to follow behaviour when writing script. A small flag in one single event can change the behaviour of the whole script! I do not like it. I can imagine someone copies an event from another script which has that flag and unintentionally changes the behaviour when charmed (even if event seems unrelated). Or the other way around: someone fix the script by changing the events and unintentionally removes the event with the flag, because it is hard to spot.

Wouldn't it be better to add action SMART_ACTION_USE_SMART_AI_WHEN_CHARMED that you would use in AIIntialize method? Thanks to this, there is no way someone will miss the fact the script has such behaviour.

And you can automatically at such action to all scripts where at least one event has the flag to mimic the behaviour. But in the future scripts, it will be clear user choice to decide

@offl
Copy link
Contributor

offl commented Feb 10, 2021

Personally I treat it as temp solution because it's indeed not really what we need simply because we are checking if creature has event with CHARMED flag but it's not correct in first place because that flag is linked to action id and not to creature entry. And there should be no such flag linked to creature entry because we already know some of those charmed creatures with SAI should attack in melee and their scripts should not be disabled if charmed.

SAI just should not break Charmed AI logic but since it does and breaks hundreds and thousands creatures, that solution is better than nothing.

@BAndysc
Copy link
Contributor

BAndysc commented Feb 10, 2021

Still, if we want to temporary hack, then imho it is better to do it explicitly (new action) than implicitly (with event flag).

@offl
Copy link
Contributor

offl commented Feb 10, 2021

What is the actual problem with making creature attack in melee and use SAI if charmed? Too hard to implement?
The thing is even if we remove SMART_EVENT_FLAG_WHILE_CHARMED from nearly all creatures(I'll remove it anyway today with or withot this PR) and fix melee attack for charmed SAI creatures, some of them will be broken because not all of them should attack in melee. However some of them will be fixed. The broken ones should be fixed manually by adding passive reactstate or their melee attack should be disabled, depends on case

This is not about flags and only about conflicts between SAI logic and Charmed logic

@ccrs
Copy link
Member

ccrs commented Feb 10, 2021

I agree with @offl , this temporal solution feels the most suitable

  • The flag is already present and nothing needs to be changed.
  • When it was introduced additional work was done in form of update scripts and manual updates to adapt all existing SAI scripts
  • Code modification is minimal

a new event is overkill imo

also the final goal is to introduce PetAI implementations into SAI, lets not forget that, just as @offl pointed out

offl added a commit that referenced this pull request Feb 10, 2021
Guardians are not charmed. Vehicle requires that flag only if player controls vehicle. It has absolutely no impact to creatures which are mind-controlable \ enslave-able. Their default AI is blocked during mind-control \ enslave as intended and you definitely don't want to make them cast spells from their SAI or execute any other action if they are mind-controlled or enslaved.

Ref #26065
}
mAllEventFlags |= scriptholder.event.event_flags;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should reset mAllEventFlags before this loop in case SAI table is reloaded at runtime

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't update instance of SmartScript, instead we create new instances

// creature entry / guid not found in storage, create empty event list for it and increase counters
if (mEventMap[source_type].find(temp.entryOrGuid) == mEventMap[source_type].end())
{
++count;
SmartAIEventList eventList;
mEventMap[source_type][temp.entryOrGuid] = eventList;
}
// store the new event
mEventMap[source_type][temp.entryOrGuid].push_back(temp);

I think the creatures still have the old version of SAI after reload

@jackpoz
Copy link
Member

jackpoz commented Feb 10, 2021

I honestly don't want to re-implement PetAI (and PossessedAI) in SmartAI, have 2 different implementation for the same logic.

Charming a creature is not just about "melee attacks", there's a lot more logic into it, which is why there are AI classes that do only that.

This PR is supposed to be a compromise for the time being until someone feels like untangling this mess that SmartAI is based on, with "let's reimplement everything in SmartAI implementation ignoring everything that CreatureAI already handles". I've seen this already with starting/pausing/stopping a waypoint path.

An alternative could be have SmartAI instantiate PossessedAI/PetAI and call its hooks and process the SMART_EVENT_FLAG_WHILE_CHARMED events, but it's just an idea. I'm not even sure if the scripters should be able to override PossessedAI/PetAI actions but that's another story.

@ccrs
Copy link
Member

ccrs commented Feb 10, 2021

we're on the same page, I agree

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

@jackpoz jackpoz merged commit f6e52f6 into TrinityCore:3.3.5 Feb 14, 2021
@jackpoz
Copy link
Member

jackpoz commented Feb 14, 2021

Thanks for the PR :)

Shauren pushed a commit that referenced this pull request Mar 6, 2022
Guardians are not charmed. Vehicle requires that flag only if player controls vehicle. It has absolutely no impact to creatures which are mind-controlable \ enslave-able. Their default AI is blocked during mind-control \ enslave as intended and you definitely don't want to make them cast spells from their SAI or execute any other action if they are mind-controlled or enslaved.

Ref #26065

(cherry picked from commit c00b055)
Shauren pushed a commit that referenced this pull request Mar 6, 2022
* Fix AI refresh on charmed

* Remove unnecessary refresh

* Check SMART_EVENT_FLAG_WHILE_CHARMED flag

(cherry picked from commit f6e52f6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants