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

Properly evaluate potential attack positions for wide units #8036

Merged
merged 13 commits into from
Nov 19, 2023

Conversation

oleg-derevenetz
Copy link
Collaborator

@oleg-derevenetz oleg-derevenetz commented Nov 10, 2023

fix #7786

fheroes2.engine.version_.1.0.9.2023-11-11.17-40-09.mp4

@oleg-derevenetz oleg-derevenetz added bug Something doesn't work AI Artificial intelligence behaviour labels Nov 10, 2023
@oleg-derevenetz oleg-derevenetz added this to the 1.0.10 milestone Nov 10, 2023
@oleg-derevenetz oleg-derevenetz marked this pull request as draft November 10, 2023 21:36
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Clang-Tidy found issue(s) with the introduced code (1/1)

src/fheroes2/ai/normal/ai_normal_battle.cpp Outdated Show resolved Hide resolved
@oleg-derevenetz oleg-derevenetz modified the milestones: 1.0.10, 1.1.1, 1.1.0 Nov 11, 2023
@oleg-derevenetz oleg-derevenetz marked this pull request as ready for review November 12, 2023 15:50
@Branikolog
Copy link
Collaborator

Hi, @oleg-derevenetz !
Before I start my tests, I have one question.
Does this PR fix only wide units logic? What about single cell troops? Or they are working just fine?

@oleg-derevenetz
Copy link
Collaborator Author

oleg-derevenetz commented Nov 12, 2023

Hi @Branikolog

Does this PR fix only wide units logic? What about single cell troops? Or they are working just fine?

This question is not so easy to answer :) Currently in master branch, battle AI uses the concept of "position value", and by "position" it means "cell". So in this case:

| |S| <- shooting unit
|+|+| 
| |M| <- melee unit
| | |
| |M| <- melee unit

cells adjacent to shooting units have better "value" than other cells, so AI prefers to use these cells to block archers, even if it doesn't want to attack them immediately, and this works just fine with non-wide attacking units (and with wide attacking units when potential targets are not so far away from each other). BUT, since "position" in master means "cell", it doesn't properly understand cases like this (just like in your video):

|S| | |M|

or this:

|S| | | |
| | | | |
| | | |M|

Although in both cases a wide unit is able to simultaneously attack one unit and block another, AI has no idea about that, because both units do not have common adjacent cells. That's why in your video AI is not able to block Trolls and attack Titans at the same time.

With this PR, "position" means "position", i.e. a collection of one to two cells which an attacking unit may occupy, and not a "cell", so now AI understands that it can take a position like this (A means a single cell of a wide attacking unit):

|S| | | |
| |A|A| |
| | | |M|

to attack one unit and block another. I must say that although the observable behavior is improved only for wide units, the position evaluation logic has been heavily rewritten, so the changes will indirectly affect the non-wide units as well (but they just should work as before, i.e. there should be no obvious regressions).

Copy link
Owner

@ihhub ihhub left a comment

Choose a reason for hiding this comment

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

Hi @oleg-derevenetz , I left 2 comments here. Could you please take a look once you have free time?

src/fheroes2/ai/normal/ai_normal_battle.cpp Show resolved Hide resolved
src/fheroes2/ai/normal/ai_normal_battle.cpp Show resolved Hide resolved
@ihhub
Copy link
Owner

ihhub commented Nov 16, 2023

Hi @drevoborod , if you have time could you please test these changes? @Branikolog is not available the next few days.

@oleg-derevenetz
Copy link
Collaborator Author

Hi @ihhub there is no hurry with this PR, it is not planned for the upcoming release.

@ihhub
Copy link
Owner

ihhub commented Nov 16, 2023

Hi @oleg-derevenetz , this is true. @Branikolog is going to be busy with another side-tasks for a couple of days after the release.

Moreover, I added 1.0.11 release as we are not ready to release 1.1.0 as the primary feature of the release is a working Editor. I am changing the target release for this PR for 1.0.11.

@drevoborod
Copy link
Contributor

At first glance, everything seems to be ok - the AI learned to block archers which are two or one cells away from meelee attackers in any direction.
But I noticed that the AI can not predict that response of its dragon on enemy attack can affect its another unit on the other side of the attacker because dragons have two-cells attack. I mean that my attacking unit (Titans) wasn't moving, it just attacked nearby dragons and the response affected minotaurs which were on the other side of the titans.

@oleg-derevenetz
Copy link
Collaborator Author

But I noticed that the AI can not predict that response of its dragon on enemy attack can affect its another unit on the other side of the attacker because dragons have two-cells attack.

The logic for performing evaluations of this type is not currently implemented. And in any case, this is not trivial, because there certainly will be cases in which AI will have to make a choice between blocking archers (or dealing maximum damage to multiple enemy units with a penetrating attack) and protecting a nearby friendly unit from a retaliatory penetrating attack.

@drevoborod
Copy link
Contributor

drevoborod commented Nov 18, 2023

Found some quite strange behaviour: the AI moved its black dragons to attack my dragons instead of blocking the shooters. Maybe it's ok, but explanation required :)
изображение
There are 4 red dragons and free space in front of shooters, but the AI does not try to block them with first dragon squad. On the picture it's Gargoulies turn now, so it's possible to fill this free space with them.
But shooters were attacked by another dragon squad later so it seems like AI takes moves order into consideration:
изображение

@oleg-derevenetz
Copy link
Collaborator Author

Found some quite strange behaviour: the AI moved its black dragons to attack my dragons instead of blocking the shooters.

If AI decided that it would be more profitable to attack your dragons at the moment, then it cannot block your shooters during this attack, because friendly dragons would be damaged by the penetrating attack from that angle.

@drevoborod
Copy link
Contributor

And here is same situation, I guess? Although shooters are open, all AI troops attack my dragons.
Screenshot_20231119_134507
Screenshot_20231119_134553

Actually, for me it seems that everything is OK and this MR can be merged.

@oleg-derevenetz
Copy link
Collaborator Author

And here is same situation, I guess? Although shooters are open, all AI troops attack my dragons.

AI will not try to block shooters at all costs. If there is more dangerous unit far away from shooters, AI will attack that unit, trying to eliminate a bigger threat. Blocking of archers is not an end in itself, but a nice bonus to attacking the most threatening unit (it can be the archers himself or one of his neighbors).

@drevoborod
Copy link
Contributor

Seems like AI does not know how to properly use Ghosts abilities :) In this case it attacks dragons instead of increase ghosts count by eliminating weaker units firstly:
изображение

@oleg-derevenetz
Copy link
Collaborator Author

Seems like AI does not know how to properly use Ghosts abilities :) In this case it attacks dragons instead of increase ghosts count by eliminating weaker units firstly

Yeah, there is already #7685 about that.

@ihhub
Copy link
Owner

ihhub commented Nov 19, 2023

Hi @drevoborod , are we good to go with this change?

@drevoborod
Copy link
Contributor

Hi @drevoborod , are we good to go with this change?

In my opinion, yes, we are.

@ihhub ihhub merged commit 718ed45 into ihhub:master Nov 19, 2023
20 checks passed
@ihhub
Copy link
Owner

ihhub commented Nov 19, 2023

@oleg-derevenetz , huge thanks for this improvement!

@drevoborod , thanks a lot for your help!

@oleg-derevenetz oleg-derevenetz deleted the ai-wide-unit-archer-block-fix branch November 19, 2023 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AI Artificial intelligence behaviour bug Something doesn't work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Combat screen, AI should consider and block the nearest ranged troops while attacking the target
4 participants