system
Given the following post, respond as if you're commenting on Reddit:
human
ELI5: How are video game AIs programmed? Is it a just a long series of "If Then" statements? Why are some AIs good and others terrible?

EDIT: I know I added an extra "a"

Sorry, world.

EDIT 2: Thank you for the gold! I won't spend it all in one place!
gpt
This really depends on what type of video game it is (genre).

The most important thing about AIs is their behavior should be believable, an AI that takes 20 seconds to calculate the best action for a monster to move in a First Person Shooter would not be believable. AIs in such games usually use some sort of flow chart that is quick to follow (which is essentially a series of If Thens), but more advanced ones use Finite State Machines (FSM) along with flow charts. For example, a monster could be in Idle state, there is a condition it will check for to enter the Alert state, and on visual line of sight, the monster could enter Combat state. Behavior (aggressive vs defensive) could be coded by weighting different options differently (calculate a "score" for advancing, vs ducking behind nearest cover position) and picking the option with higher score. Even more advanced monsters could use group tactics (flanking, cover fire, etc).

For turn based games, these types of games often follow very fixed and known rules, with a limited number of moves available to players on each turn. Examples such as Chess, card games, etc. Some simpler games can Brute force and calculate out every possible move for the next X moves and scoring the outcome of the playing field based on a formula, the best ones prune entire decision trees as being completely worse off by using a technique called Alpha-Beta Pruning. Storing results of previous calculations in memory help improve speed, or even storing databases of them on disk (eg, chess opening books). To prevent taking forever on calculating a move, these is typically a time limit where it stops looking for a better move after some time and just uses the best move found. Scrabble AIs tends to just brute force a single move, since it cannot really predict what the other players have in their hands - strategy will vary whether players have all the information available (checkers), or if some information is hidden (eg poker hands).

But there are also advanced turn based games like Civilization, or complicated games like Starcraft. These types typically use an AI called the Blackboard AI, which uses sub-AIs. So there is a "Economics AI" that says "we need more minerals", and a "Tech Tree AI" that says "we should research this tech", and a "Exploration AI" that says we need to see what's over here (more than any other location), etc, or a "Relations AI" that thinks we need to improve relations with this neighbour, and a "War AI" that says we should move these units here and there. All these subsystems feedback to an overall master AI called the Blackboard that takes in all these requests, and then figures out how to allocate a finite amount of resources to a large amount of requests, by ranking each request based on how important it thinks that request is (how early the game is, stone age, modern age, number of bases/cities etc, importance of a location). These types of AIs are the *hardest* to balance since scoring each request is really just based on arbitrary formulas, modified by the "aggressiveness" setting of that AI or the difficulty level of the game.

It is also important to know some AIs perform great because they cheat. In some games, the computer AI knew exactly where your army was, even though in theory the AI shouldn't know this due to fog of war. Starcraft is guilty of this, but Starcraft 2 made a genuine effort not to cheat. Civilization "cheats" by increasing the rate of production/resource gathering on higher difficulty levels for the AI. The "Insane AI" on Starcraft 1 gains 7 minerals instead of 5 per trip.

Some "AIs" aren't even AIs at all. Level X of this campaign in Starcraft might seem intelligent, but all it is doing is just following a designer script that says "spawn this set of units every 5 minutes and throw it at the player" and "15 minutes into the level, spawn this set of units and attack from the backdoor". Still, if scripting specific events and narrative into a game results in good gameplay, then that is probably ok.

Look at say the classic Super Mario, the Goombas simply walk in one direction, can fall off ledges and turn around if they bump into a wall. Turtles behave exactly the same, but don't fall off ledges. They still qualify as AI, it just isn't particularly smart, since such AIs are extremely simple If-Thens; that doesn't mean the game is bad, simple AIs lead to predictable monster behavior that the player can take advantage of (eg, timing Mario's jumps at the right time). Most monsters in Diablo just make a straight beeline for you, a few might flee away on low health, but they are predictable. With that many monsters in a level, it is important to make every monster have AIs that perform very quickly, most monsters only run their AI once every 0.5 seconds, or even every few seconds, rather than recalculate every frame. This also leads to believability, no humans react instantaneously, neither should AI.

Minecraft used to have very simple monster AIs, the zombies and creepers just make a beeline for you, often falling into pits in the way, or just running into that tree trunk on the way. It was patched a few years later to have actual pathfinding (the Pigmen still have original beeline AI). While this was great on single player, this became very problematic on large servers with a lot of areas loaded in memory - the servers slowed to a crawl because there were large amounts of monsters spawning across the loaded areas, running a lot of pathfinding where in the past, it was just running in a direction towards the player. Some servers opted to mod their servers and have the monsters go back to the simplified beeline AI to reduce server load.

TLDR: Different games require different kinds of AI. What comes down to whether an AI is considered good or terrible simply boils down to whether it made a choice/move that is **believable** to the player, and there are computation constraints to calculating the best move.

Edit: Added a paragraph on cheating AIs, designer scripted AIs, and another on Minecraft.