ihhub / fheroes2

fheroes2 is a recreation of Heroes of Might and Magic II game engine.
https://ihhub.github.io/fheroes2/
GNU General Public License v2.0
2.77k stars 380 forks source link

AI spell usage in "I take you with me" case #8382

Open crypt47 opened 9 months ago

crypt47 commented 9 months ago

Preliminary checks

Describe the problem requiring a solution

In current save file an enemy in the castle has pretty high Spell Power and a destructive spell Meteor shower (or something like that). I know this because on some other conditions the spell was used with devastating effect. But currently (if I don't use Fenix for attacking and start by attacking trolls with my archers) the enemy hero uses Berserk spell which I simply canceled by Dispel. The enemy is then locked withing it's castle walls and eliminated with almost zero loses. Beltway_0051.sav.zip

Describe the possible solution

This is right, that Berserk is optimal for inverting damage. But it can be canceled too easily. The enemy should be more bitchy in presense of strong ranged attackers plus after castle walls. He should switch to "I take you with me" scenario. At least on some Difficalty levels, imho.

Additional info

No response

ihhub commented 9 months ago

Hi @oleg-derevenetz , I believe we need to adjust the priority for Berserk spells for situations when the opponent hasn't casted a spell and when the creature is not the next in a row to make an action. What do you think?

oleg-derevenetz commented 9 months ago

Hi @ihhub AFAIK the AI trend to use the non-damage spells in such a way that they could be easily dispelled by an opponent is well known (this applies not only to Berserk, but to other control spells as well - e.g. Slow, Blind, etc). I'm afraid that trying to solve this problem head-on by simply changing priorities will lead to the fact that AI will almost always use damage spells instead, because it always casts its spell during its first turn. Ideally, we need to implement some kind of "waiting for the right moment" mechanism, but I don't have any good ideas yet on how to formalize this very concept of the "right moment". In the pro gamer's tournaments, the process of choosing a spell and the right moment to apply it is somewhat similar to poker :) Everyone is guessing what spells the opponent have, if you cast a control spell too early, then it can be dispelled, and if it's too late, then you are risking that the opponent will cast Slow/Mass Slow.

crypt47 commented 9 months ago

... In the pro gamer's tournaments, the process of choosing a spell and the right moment to apply it is somewhat similar to poker :) Everyone is guessing what spells the opponent have, if you cast a control spell too early, then it can be dispelled, and if it's too late, then you are risking that the opponent will cast Slow/Mass Slow.

I'm really a novice regarding this topic and you're probably doing more cleaver things then I'm going to say, but it looks to me like the computer has to have some strategy too. General algorithm with ranking will not do here. The computer should be like player switching to patterns of strategies. For example it should accumulate rangers and high level of mage abilities to use some strategy #1. Or it may "see" that he is class barbarian with a lot of rangers against class sorcerer, and use different "right moment" according to his tactics #2. You surely know much more then me about statistics prediction and modern AI.) Some randomization in making decisions would make it more interesting. Ideally I guess we all want to play against skynet type AI with neural network.)

Alucard648 commented 9 months ago

What about using Armageddon spell as last ditch effort to end the battle with all creatures on battlefield dead? Especially in scenarios where defeating specific hero is instant win condition for AI.

oleg-derevenetz commented 9 months ago

What about using Armageddon spell as last ditch effort to end the battle with all creatures on battlefield dead? Especially in scenarios where defeating specific hero is instant win condition for AI.

Currently, AI does not use any "strategic knowledge" during battle. It can cast Armageddon if it is obvious that it's losing the battle, but it can't, say, use tactics like "specially send a hero with a single fast unit to cast Armageddon and win the map".

crypt47 commented 9 months ago

What about using Armageddon spell as last ditch effort to end the battle with all creatures on battlefield dead? Especially in scenarios where defeating specific hero is instant win condition for AI.

This is exactly the case in sample save file. I prevent this by keeping only the fast units to take the initiative and locking enemy's melee in castle walls. AI simply can't figure out he's loosing. Because I have a strategy and he just access only the current situation.

crypt47 commented 9 months ago

Currently, AI does not use any "strategic knowledge"

I've also noticed that the weak enemy hero often comes across my strong hero. It's very easy to leave an empty castle, wait and kill enemy heroes one by one. They doesn't try to avoid the danger or accumulate forces. Is it somehow related to the absence of strategic view in general?

oleg-derevenetz commented 9 months ago

Currently, AI does not use any "strategic knowledge"

I've also noticed that the weak enemy hero often comes across my strong hero. It's very easy to leave an empty castle, wait and kill enemy heroes one by one. They doesn't try to avoid the danger or accumulate forces. Is it somehow related to the absence of strategic view in general?

I mean, it doesn't use any strategic knowledge during battle itself. On the adventure map, there are so-called regions, and AI should avoid visiting regions that have strong enemy heroes, but it has its own ifs and buts, e.g. AI still can perform the fog discovery in the "dangerous" regions.

crypt47 commented 9 months ago

I mean, it doesn't use any strategic knowledge during battle itself. On the adventure map, there are so-called regions, and AI should avoid visiting regions that have strong enemy heroes, but it has its own ifs and buts, e.g. AI still can perform the fog discovery in the "dangerous" regions.

Yes, ok, but it looks like AI makes completely irrational movies, that can't be explained by the fog of war. It surely comes to the known territory but with a strong enemy. And I've also noticed that starting from some version AI doesn't pick resources anymore (on some maps?). Lot's of it stays on a ground. Should I file a report or is it a known thing?

oleg-derevenetz commented 9 months ago

It surely comes to the known territory but with a strong enemy.

The mechanics of all these regions was implemented not by me, but as I can see it applies additional penalties for "dangerous" regions, i.e. there is still a possibility that object with penalty can still be preferred to other objects. Also, as far as I understand, there is a possibility that a powerful enemy hero may, say, be close to the border of the region, and a weak AI hero will run up to the same border, but on the other hand. In general, yes, it would be good to improve this somehow, maybe there will be those who want to do this. There is a TODO comment BTW that suggests not to use regions for this, but to use distances to the enemy heroes instead.

And I've also noticed that starting from some version AI doesn't pick resources anymore (on some maps?). Lot's of it stays on a ground. Should I file a report or is it a known thing?

Sure, if there is a specific save file that demonstrates this issue, it would be nice.

crypt47 commented 9 months ago

Fine then. I'll go for the save files. Meanwhile here is Beltway 0037, Beltway_0037.sav.zip next move the enemy hero jumps towards Alchemy's Lab despite visible threat.

oleg-derevenetz commented 9 months ago

next move the enemy hero jumps towards Alchemy's Lab despite visible threat.

In this case AI hero is moving to the Watch Tower for the fog discovery. But in general I agree that it needs to be improved. For example, it can be done so that AI heroes generally cannot pass within direct reach of enemy heroes who are stronger. Something like this is now done in the AI pathfinder, but in regard to the wandering stacks (and partially enemy heroes in the sense that paving the path right through stronger heroes is not allowed). Anyone who interested can implement this, or maybe I'll take a look later when I have time.

crypt47 commented 9 months ago

Thanks for checking. Just to be clear, I've recaptured the Alchemy Lab just after it was recaptured by him. So there is no way the AI couldn't see me in the fog. And I also played a little HoMM3 where the enemy hero always tries to lurk away of stronger opponent. So it's was sometimes a real PITA to find where he managed to hide off the visible area.

idshibanov commented 9 months ago

Forcing an enemy to dispel a spell still beneficial especially when the game has such high impact spells such as Blind and Slow that can be cast by anyone. A proper solution to this problem is to teach Battle AI look and plan few turns ahead rather than evaluating only the current one.

However there's still improvements we can make to get halfway there. Currently as army numbers grow damage spells really fall out of favour. We can increase spell damage heuristic to a nlog2n for example, this way it will be more impactful for late game as spell power scales. Adding a simple check if enemy has already cast a spell this turn or not can help as well.

crypt47 commented 9 months ago

Forcing an enemy to dispel a spell still beneficial especially when the game has such high impact spells such as Blind and Slow that can be cast by anyone.

It all depends. I believe that balance is off with antimagic spell. It is rather low-level, can't be dispelled and makes caster toothless in that way he can't compensate for attack/defense disadvantages of it's class. If by dispel we mean Antimagic it's pretty powerful, but not in case of the army that is locked behind castle walls against archers for example.