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.58k stars 359 forks source link

Teach AI to garrison slower troops in the castle at the end of turn #8207

Closed zenseii closed 6 months ago

zenseii commented 6 months ago

Preliminary checks

Describe the problem requiring a solution

Currently when the AI ends the turn and it has a hero visiting a castle, it will not garrison any troops, which means that the next day their movement points will be based on a potentially slow troop in that army. Instead, if it would garrison these troops, it would get a single-turn movement points boost that can be good for many reasons, like having a greater chance to reach nearby objects and get back to the castle in one turn, among many other things.

Additionally, since by default you get more detailed information about troops in a hero's army than a castle's garrison, not garrisoning is suboptimal. One exception being the times when you own 2 thieves guilds. Therefore, garrisoning troops would then improve this by hiding what troops the AI has to the human player.

This is an example of the hero visiting the castle and you can see that they have garrisoned 0 troops, keeping the very slow Golems in their army. The best way here would be to only keep the very fast boars in their army.

image image

Describe the possible solution

Teach the AI to garrison troops by leaving only the fastest troops in its army, whether that is a single troop type or several. The AI will need to garrison troops in the corresponding castle slot they would have wanted the troops to be in their army in case they get attacked.

I suggest to avoid making a very complex logic for it, since even just a simple one would bring big benefits. For example, there can be special cases where you could optimize more, like if your castle has ultra fast troops that you don't intend to keep in your army the next day, but you know that you are in no threat to be attacked, so you move them to your army just to get the movement points, and replace them with desirable troops the next day. However, I suggest not diving into such cases - They are few, and just doing the basic variant of this optimization will be much better than the average heroes 2 player from my experience (I've never seen a streamer do or be aware of this trick).

Ideally, the AI should keep the one, or the several troops, with the highest speed in its army, and "move up" in the garrison the troops that are slower, starting with the slowest one in case the castle has slots filled with creatures already.

Note that this should only be done at the end of the player turn for every hero the AI currently has in a castle, not for every visit to a castle. I suppose it would even be beneficial for heroes that are permanently stationed in a castle for the reason of concealing its actual army.

Additional info

Relates to this https://github.com/ihhub/fheroes2/issues/6755

fheroes2bugs commented 6 months ago

Agree with the issue, just wanted to add an example.

In this video, after I lose the battle, red computer decides to hire 3 new heroes, but he has no creatures to hire, so he is travelling with starting army... There is no reason to travel with halfings - you will be beaten anyway (this is month 4)... With only sprites, boars and halfings you can't kill neutral creatues. Even on 1 week you kill only few level-1 creatures.

https://github.com/ihhub/fheroes2/assets/145407420/2df77347-be40-4c63-b680-389f5378ffa9

It would be better to leave slow forces at the castle or give them to some "main hero", so those secondary heroes would get more movement points. In this case, every new hero could travel with 1 boar, all the sprites and battle dwarfes could go to "main hero" . Version: 1.0.11 Save: suicide.zip

zenseii commented 6 months ago

Hi, @fheroes2bugs. Please avoid adding more issues to this issue thread. What you're describing goes outside of the issue described here. It is better to work towards improvement in small steps instead of trying to improving everything in one go.

What I descirbed is a much more specific area of improvement compared to what you're describing which is about another part of the AI logic.

Feel free to open a new issue, granted it doesn't already exist, with some very specific and clear goals.

oleg-derevenetz commented 6 months ago

Hi @zenseii

I've never seen a streamer do or be aware of this trick

I don't know about HoMM2 streamers, but pro tournament HoMM3 players use this all the time, as well as a number of other techniques - for example, during combat, forcing powerful, but relatively slow units to go back and forth, luring them with the help of fast flying stacks, usually consisting of 1 unit. BTW this works just fine in fheroes2 too, because AI-controlled units here also prefer to attack a relatively weak, but reachable on the current turn target, rather than a more dangerous, but distant one. Consider:

https://github.com/ihhub/fheroes2/assets/32623900/6827df91-5e48-4cf7-bdf2-fb2c936c2d52

That's why in #8206 I say that AI vs AI battles are not indicative at all for pre-evaluating results of AI vs human battles. In AI vs AI battle, the attacker will lose with a crushing score and defender will lose 2-3 Ogres.

zenseii commented 6 months ago

@oleg-derevenetz,

I don't know about HoMM2 streamers, but pro tournament HoMM3 players use this all the time,

Yes, someday I hope to see a professional Heroes 2 community take form. This is also why I specifically mentioned average heroes 2 players in the part quoted below, because, right now, partly due to online homm2 lacking, there aren't any players which you could call "professional" in the sense that they actively compete in big very competitive tournaments.

this optimization will be much better than the average heroes 2 player from my experience

Most skilled Heroes 2 players that I see could play better but choose to play more leisurely because optimizing every move is not necessary to beat the current AI, in most maps, but those days might be numbered now...

BTW this works just fine in fheroes2 too

In the end, I think with this issue being solved, we are moving closer to an AI that will on average prove challenging to the common Heroes 2 player, which has been our long term goal. I think that if the new AI will force players to start playing more like they would if they were up against a real player, then that would be a great accomplishment, compared to how many players have become overly accustomed to the predictable, easily exploitable and bad AI decisions of the original Homm2 AI.

I never really liked the well-known exploit of making the walkers chase single fairies around the battlefield, because it feels too cheap. It wouldn't work on most humans. It could be compared to tricking a cat to run around in circles trying to catch a laser pen's red dot.

oleg-derevenetz commented 6 months ago

I should note that HoMM3 battle AI is also not very sophisticated and plays by well-known rules that can be easily abused. HoMM3 tournament maps/templates are made in such a way that one human player does not have to fight with the another human player all the time - often there is only one PvP "final" battle per game at all. Mainly, a human player fights against AI, clearing the way to objects on the map to boost his heroes, collect most of valuable artifacts and assemble the largest possible army. The one who is better able to abuse battles against AI (pro HoMM3 players know all the tricks) and who is the fastest to boost his power (using hero chains to quickly deliver reinforcements and so on) will win. A certain amount of luck, of course, also matters.

oleg-derevenetz commented 6 months ago

I never really liked the well-known exploit of making the walkers chase single fairies around the battlefield, because it feels too cheap.

BTW recently HotA added a new faction (Factory) and a new campaign for this faction, and I suspect that this campaign just cannot be completed without using various tricks. The difference in losses with and without using tricks can be enormous. Here is an example video:

https://github.com/ihhub/fheroes2/assets/32623900/fcfbaddb-30e8-4906-a22c-70b067d8e2fd

I suspect that HotA developers are not making efforts to radically improve AI behavior so that such campaigns remain "passable". And I think that pro players will be very upset if the opportunity to use such tricks disappears :)

zenseii commented 6 months ago

@oleg-derevenetz

I should note that HoMM3 battle AI is also not very sophisticated and plays by well-known rules that can be easily abused.

It is typical of professional gaming communities that they reduce or restrict the game to be played in a certain way, abandoning some aspects, especially aspects that have high randomness, in order to have a winner based mostly on skill and not luck, though as you said this isn't always possible nor the case

A good example is the Super Smash Bros. fighting game community where the biggest tournaments' rules force you to play the game in a very stripped down way (I know that Heroes 3 pro tournaments have kept some of the randomness). I'm of course OK with people playing a game the way they find most fun and rewarding, I'm not complaining about this or saying it's the wrong way to play the game.

Another interesting subject is the speedrunning community (which BTW has taken notice of fheroes2 ). They also tend to prefer predictability and reproducibility of their "runs". As is touched upon in the discussion in that link, the nature of fheroes2's AI being WIP, and with no intention or set goal on when to stop developing it, speedruns can be quite difficult to compare especially if it means that you have to stay on a specific build.

This WR using fheroes2 in fact specifies that they used version 1.0.9 to make this record: https://www.youtube.com/watch?v=NbcfOYDxQdw&ab_channel=RafaelL%C3%B3pezGarc%C3%ADa

I suspect that HotA developers are not making efforts to radically improve AI behavior so that such campaigns remain "passable".

I've definitely heard the same argument about preserving the original AI from longtime Heroes 2 players, and one could argue that the same way a player expects the quote-on-quote AI of a goomba in Mario to walk in a straight line, players have become used to a certain predictable behavior from the AI in any of the Heroes games. I've heard people describe the original battles as puzzles that you need to figure out the way to lose the least amount of troops in.

One way I look at the original design and whether to preserve it is: "Would the original Heroes devs have made the AI any different, had they had the infinite time and resources to develop an AI just as they wanted, or would they have settled on exactly the same AI as we ended up getting?" I personally believe they would have made it more complex/challenging, but of course we might never know the answer to that.

To me it seems obvious that the Succession Wars campaigns were developed side-by-side as the original AI, and that's why it is much more open-ended with regards to what the AI can do, because they did not know what the final outcome of the AI would be. This is in contrast to the Price of Loyalty campaigns which seem much more catered to the then-finished AI.

Another related point I've been thinking about is that the reason none of the original maps are broken by the AI knowing Summon Boat, is because they didn't know whether the AI would know how to cast it or not by the time the game released, so they safe-guarded the maps against it. Not to mention that if the map was to be compatible with multiplayer then they would have to take Summon Boat into consideration.

BTW a little exploit-ish I found when testing the first kiting improvement PR is this tactic of using the Ammo Cart with a few very fast shooters to take out huge amounts of slow walkers:

https://github.com/ihhub/fheroes2/assets/12501091/2b4054d0-9238-4e3e-9641-a4cf5b626518

I know that this scenario is very unlikely to come up in "real-life" because the ogres would have split into several stacks. You can improve it quite easily by grabbing a few archers or halflings for free somewhere and keep their stack small enough that the ogres continue to chase the faster troop.

NOTE: Don't auto-resolve this one with such high numbers because it will take a looooong time for it to finish the battle. My PC took a few minutes... Retreat if you need to end the battle.

oleg-derevenetz commented 6 months ago

BTW a little exploit-ish I found when testing the first kiting improvement PR is this tactic of using the Ammo Cart with a few very fast shooters to take out huge amounts of slow walkers

But if both elves and ogres were controlled by human players (even the very skilled ones), nothing would have changed, and the ogres would have lost. I don't see what the "exploit" here is :) According to my understanding, "exploit" is when you use the imperfection of AI algorithms to win where a human player would have done something differently, but in this case with the same armies and artifacts a human player would have done nothing, and the result would have been the same.

NOTE: Don't auto-resolve this one with such high numbers because it will take a looooong time for it to finish the battle. My PC took a few minutes... Retreat if you need to end the battle.

That's because of debug build :) In release build it takes about 15 seconds:

https://github.com/ihhub/fheroes2/assets/32623900/bfbe1dcd-32b1-46e8-8fd6-91909d626182

zenseii commented 6 months ago

I don't see what the "exploit" here is :)

Yes, this is why I didn't report it back then because it is just a clever way of using, or exploiting some might say, how the game is designed, and there is no fault by the AI here. A human player would have the same fate against such archers.

exploit-ish

That's because of debug build :) In release build it takes about 15 seconds:

That's good to know for regular users, even if it's scary to think how fast those two elves are shooting those arrows that they are killing 5K ogres in 15 seconds.