r/howdidtheycodeit • u/YoureFather • Aug 24 '22
Question XCOM2: How did they programm the enemy AI in XCOM?
Hey there, I'm currently programming a tile based deck building game. I have the gridmap, cards, etc. implemented but I'm struggling with the essential part, an interesting enemy AI. I kinda have an idea how to do it but it doesn't work as intended so I thought to look at how similar game solved that problem. A perfect example would be XCOM 2 but I can't find any videos or text about how they programmed their AI except that they gave each tile that can be reached a certain score, but I would love to hear from you how they did it If you have an idea. Oh and something about normalization and bias was written about it but thats all I know, thanks in advance.
47
u/hyperflare Aug 24 '22
It sounds like you'd best be served by understanding the basics of how an AI agent makes decision, for that I wouldn't go to XCOM, but look at chess engines etc instead. XCOM will just be a variation of that basic method.
A basic guide to game ai can be found at https://www.gamedev.net/tutorials/programming/artificial-intelligence/the-total-beginners-guide-to-game-ai-r4942/
Ultimately your agent wants to pick moves that benefit it. You have to know what beneficial is - and for that you need to assign value to game states. The way you do this is your "utility function", and there a many, many ways to do this. You can find examples easily.
The next step, if you don't want to just win but be an interesting enemy to play against for human will require more additions. A godlike AI that crushes you all the time isn't necessarily fun. That part is mostly up to you. What do you think makes for an interesting enemy?
5
u/TheRealBasilisk Aug 24 '22
I am working on a similar concept to yours it sounds like. What I do is first get all the possible tiles they could possibly move to on that turn. Then on each of those tiles determine what possible action they could take (attack, use consumable, harvest, position for next turn, etc) and return a score. For example if they could attack and do 10 damage to kill a unit then that would get a very high score. And if for example they cannot attack and there is no reason to use a consumable they would just choose an optimal new position (maybe behind cover just outside enemy range).
I've then added further complexity which introduces a state machine for the units in the process above. So if a unit is in an aggressive state they will do everything they can to attack the player units with little regard to other actions. If they are in a passive state they would be more likely to use a consumable or take a non combat action.
Hope that helps a bit. I'm sure there are way more complex ways of doing it but this system has been working fine for me so far.
1
u/AG4W Aug 24 '22
They probably generate a bunch of Dijkstra-maps based on a bunch of different parameters such as accessibility, cover, enemies, line of sight, etc - and then do some query using an A*-algorithm or similar.
There's usually a Director-layer to create coordinated tactics aswell.
2
u/[deleted] Aug 25 '22
Prioritize movements that will enhance ai unit defense and damage player units the most.
You could look at the player unit range and weapons to figure out the positions that would be better for defense and attack in the grid via a score system. Places that let you get flanked get a negative score and places that have the best chances to hit the player units get a positive score. You can add other things like cover to further change the score. In the end pick the square with the highest score.
2
u/tnz81 Aug 25 '22
One thing I read about a while ago, which is being used a lot (iirc), is basically using A* pathfinder, not only for movement, but also on a behavior tree. Imagine a behavior tree with all kinds of paths towards certain endgoals, then make the AI select an endgoal, and follow the path of small steps to achieve it.
0
u/megablast Aug 24 '22
AI is not easy.
Just do a bad AI and improve it. For example, moves towards enemy and fires.