Monday, January 2, 2012

Does the AI cheat?

I noticed that there was a comment on both Android market as well as on Amazon appstore about AI cheating with battle results. I'll try to clear out the confusion about this.

Short explanation: No, AI really does not try to cheat. But apparently, on some devices (quite rare, it seems!) the random number generator in Android does not provide numbers that are random enough, but is occasionally "stuck" with giving out similar numbers over and over again ... A bit like imbalanced dice, which show always the same face up when thrown. This is a software or hardware bug which I can't directly fix. However, I'm making a workaround for these devices, and will upload it as version 1.2.1 when I have done a little smoke test for the release (will be available on Android Market immediately after upload, on Amazon Appstore in about one week as it needs to be approved first).

Long, boring and technical explanation: The battle resolver in the game does not even know or care, which side is controlled by AI and which by human. It is completely separate from AI logic. It just adds attacker and defender strength together, we shall call this number the "total strength" of the battle. Then the battle resolver retrieves the next random number between 0.0 (inclusive) and 1.0 (exclusive) from a random number generator, and multiplies it by the total strength of battle. If the resulting number is less than attacker strength, attacker is declared to be the winner. Now then ... if the random number generator consistently gives out numbers that are closer to 1.0 than 0.0, the battle results start to be skewed, in favor of the defender (being stuck with numbers near 0.0 would favor attacker instead). Now I'm switching away from ordinary "Random" to use the "SecureRandom" random number generator, which should provide randomness good enough to be cryptographically secure, so should be perfectly fine for this purpose also. As additional measure, I also added now code to shuffle that random number around by using the millisecond part from system current time. This has no effect on an already correctly working random number generation, but should provide a bit of "emergency" randomization in the case where even the SecureRandom would by some miracle fail to provide decent enough level of randomness.

If there's anyone who had encountered the "cheating AI" problem and happens to read this, please confirm after updating, if this workaround in v1.2.1 cures it for good, I'd really appreciate it! Just post a comment here or send email to poprominfo AT gmail.com.

16 comments:

  1. Sometimes there is a spearman who is surrounded who just wont die... even at massive odds against. if i get two fails in a situation like this i move on or all five/six will fail..

    Si

    ReplyDelete
  2. Si2, yes, I recall you mentioning that problem earlier. Please update to version 1.2.1 to fix that!

    ReplyDelete
  3. Sometimes an AI controlled unit that has been skirmished is able to move and attack in the next round. This never seems to occur for non-AI units. Is this a bug?

    ReplyDelete
  4. J: Thanks for the tip, I'll have to check this. Do you by any chance remember if this happened so that:

    1) first you skirmished some units
    2) then returned to main menu or pressed back button to exit game (this effectively saves the game state)
    3) and finally continued the ongoing game again (loads the savegame)

    If it happened in a situation like this, then I seem to have a bug in savegame loader.

    ReplyDelete
  5. Skirmishing is too powerful now that there is no risk of losing health. It is easy to build a wall by skirmishing weak AI units and then funnel powerful AI units into an outflanked position kill zone. If the attacker loses by a significant amount it should lose a health point.

    I tend to outflank the enemy by slowly encircling while skirmishing. Although mounted units can do this more easily, it works just as well with infantry. Perhaps mounted units should be able to travel past enemy units without having to fight them so they are particularly good at outflanking.

    The AI does not appear to heal units in towns. The AI also doesn't build overwhelming force or attempt to outflank in any serious way. The AI also doesn't scout or try to sneak some units off-road to attack from the rear. AI units are too easily lured out into open areas and lured into outflanked positions.

    The AI favors hoplites, which aren't an efficient use of resources. When the AI does build bowman and spearman units, it doesn't use them effectively. They are best as supporting units. Chariots are way too powerful for their cost and horseman units are much to expensive. The cost of these two should be swapped. How can a chariot possibly be cheaper, anyway?

    ReplyDelete
  6. For AI units escaping a skirmish, I haven't noticed a difference between going to the menu and then coming back using the continue game button and just clicking the end turn button immediately. I have end turn confirmation enabled, if that matters. I'll see if there is a situation where I can reliably get it to happen.

    ReplyDelete
  7. Yes, I can get it to reliably happen when I come back from saving. Probably other times it has happened to me, my screen has one blank or something else (text message, etc) has triggered a save. Here's reliable reproduction steps:

    1) Start Volscians, Part 2 (scenario 4)
    2) Move equestris on road from Ferentinum to bridge next to Fabrateria Vetus
    3) Skirmish the bowman (may need to try 1-3 several times as odds are 51-49)
    4) Click menu, click continue game, click end turn

    Bowman is able to move immediately.

    ReplyDelete
  8. Thanks J for the good bugreport (and other comments)! I found where the unit skirmish status saving bug was and fixed it, the fix will be made available in the next update.

    Also swapped horseman and chariot unit prices now, have been thinking exactly the same thing myself also. The added value of faster movement speed isn't enough to warrant higher price for horseman.

    Correct powerfulness for skirmishing seems to be a constant issue for me :) Now it indeed seems to be too strong when used in creative ways, will tweak it so that there are 3 types of outcomes, "defender was hit", "defender avoided attack", and "attacker failed spectacularly" (which hurts the attacker).

    A few "AI smartness" kind of updates are on the way, but won't make it into the next release yet. About AI healing units, it actually does break units off from combat and returns to towns, but is very easily distracted from retreating depending on the overall threats/situation. I might need to tweak some thresholds a bit.

    ReplyDelete
  9. Another possible bug: in the Punic scenario, the Greek units become stuck after Syracuse is conquered. If the Romans then attack them, they wake up and fight back.

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Love this game, well done.

    So, what is the current version number, does it fix the random number thing, and is it available on kindle fire?

    There are a few times recently that I had an AI unit outflanked with at a few superior units and the AI unit has unexpectedly beat very long odds. For example an AI Hoplite, on open ground, surrounded on 5 sides attacked is attacked by a Praetorian Horseman, a Veteran Legion, and an Auxilla and the Hoplite wins all three.

    To some degree this may be a perception problem. Many turn based fantasy games are deterministic rather than probabilistic. Advance Wars (Nintendo DS) is a good example of a deterministic, turn-based strategy game where you know the outcome of a given fight before you engage in it and superior units always have the edge. In Populus Romanus, even with a perfectly working random number generator, it is possible for an inferior unit to beat the odds several times in row. That may surprise people who are used to more deterministic games.

    At some level, this 'bug' could be viewed as a feature, as long as it doesn't feature one side over the others. One inferior unit mucking up your plans forces you to be a bit more flexible, etc...

    P.S. Could you recode the game so it didn't automatically center on the unit you selected. This can be inconvenient. Maybe have it as an option you can turn off, that would be ideal.

    ReplyDelete
  12. Been playing the game for about a month and have noted as others have that the AI combat is skewed, mainly to the attacker and often to the lowly spear unit. There have been turns where I setup a defensive line to absorb the onslaught and I watch the spear units, one after another, score hits in melee combat. This might be known as a lucky turn but I observed the same behavior two turns in a row. No less than 5 attacks each turn!

    I am not sure which version of the game I have but I can try the 1.2.1 version if I don't have it. BTW, I am using the Kindle Fire right now.

    ReplyDelete
  13. I'd also like the auto centre feature to be toggled, I've movedca fair few units to the wrong place as the map moves beneath me.
    I love the random factor, I normally go 75/25 min oddsand i expect the occaisional defeat.
    Where are the new scenarios?
    I finally won the Punic wars after four months of trying and I'd like something new...
    Si

    ReplyDelete
  14. It may be a perception problem, but when 90/10 combats fail several times in a row and elite pretorians die like flies when attacked by spearmen then the game becomes nothing but frustrating.

    I reckon that some people appreciate the random nature of the fights. Nevertheless, for a strategy game (which rule-based games typically are) I'd expect the random factor to be lower.

    Suggestion: add a more deterministic mode as option which squares the probability, so that what currently is 1:9 would become 1:81 chance of success. Around the 50% the randomness would remain mostly unaffected, but really strong adavntages would more likely lead to a win.

    ReplyDelete
  15. A spearman is as good as two elite Pretorianer in the penultimate level

    ReplyDelete
  16. I have been playing this game on a Hudl android. I came looking for this blog as I have finally given up on this game as it is just too frustrating for words. It's a shame, because it has the potential to be truly great, but is ruined by the simple fact that the AI constantly and predictably cheats in combat. I would like to echo Arsene's first paragraph above - when you seem to lose every 50/50 battle, that's annoying...but when you also lose 90% of those with odds 2/1 in your favour the game just becomes unplayable. When every enemy unit is inherently superior to your best units it is not a Romans vs enemies game anymore - it has become a Romans vs Superhumans game which is something entirely different and no fun whatsoever. Who wants to play a game where they constantly get their arse kicked by a cheating AI? Not me any more, I'm afraid. Sort this out and the game will be great, but at the moment it is only for masochists!

    ReplyDelete