The Recipe for Good AI
The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company.
Iâ€™ve worked on AI in a number of games over the years. Every nugget of wisdom Iâ€™ve picked up along the way points to one basic, possibly depressing lesson: creating a good AI is exceptionally difficult. Not only is it tough to make one that simply meets the incredibly low bar ofÂ not completely falling on its face -Â but even if you make a really,Â reallyÂ smart AI it doesnâ€™t mean youâ€™ve made aÂ goodÂ AI. After all, not everyone wants to be ruthlessly eviscerated by a merciless opponent. Some people are looking to roleplay, or just would prefer the AI keep the gloves on even when it has an advantage. Even those who want a serious challenge wouldnâ€™t enjoy losingÂ everyÂ game.
So what is the first step for a designer or AI programmer in this quest to prepare a five-star meal? No matter your task, the answer is always the same: identify your goals.
What Really Matters
Okay, so the lesson of â€śwriting AI is hard!â€ť is neither particularly useful nor surprising, but there is another so important developersÂ mustÂ embrace it:Â allÂ that matters is what players see and believe. Really. Thatâ€™s it. The AI can cheat, act in or out of character, make dumb decisions, whatever â€“ as long as the player is having fun thatâ€™s what counts. Developers are often surprised by which features players get the most enjoyment out of. â€śWait, your favorite part of the game is theÂ hats?Â That only took a day to implement!â€ť Getting the most bang for your buck is important in all aspects of game development, but itâ€™s absolutely essential with AI â€“ the biggest meal of the year calls for the best ingredients money can buy.
AI is just like any other system in that there isÂ zero valueÂ in it being well-engineered if the player doesnâ€™t actuallyÂ get something out of it. Itâ€™s incredibly easy to get bogged down in the details of how to solve the myriad of difficult problems that pop up, and never step back and realize youâ€™re spending a ton of effort on a feature that doesnâ€™t really contribute to the actual experience of playing the game. The risk of this occurring is particularly high with AI because much of the time itâ€™s hard to tell if your code is even doing what youthink it is, let alone doing it well.
Okay, so youâ€™ve laid down some goals and have vowed to dutifully follow through on them. The next thing our recipe calls for is to identify what can go wrong with a gameâ€™s AI.Â The AI has failed if a player considers it to be either 1)Â too random,Â or 2)Â dumb. The job of an AI developer is to sidestep these pitfalls inÂ anyÂ way possible. Letâ€™s first talk about how to make an AI appear rational, and not simply random.
Preventing Random AI
A strategy for making an AI look less random is toÂ actuallyÂ make it less random. Sure, it sounds obvious, but how predictable or wacky an AI should behave is an important design decision. Perhaps instead of doing a random roll each time an AI unit can attack, aÂ singleroll could be done at the beginning of the war to determine how aggressiveÂ allÂ of the AIâ€™s units will be. Techniques like this are always worth considering, but in the end the best approach will be dictated by the project goals and what kind of feel the designer wants the game to have.
Another tactic that, perhaps counter-intuitively, makes an AI seem much smarter is to have itÂ explain what itâ€™s doing. Is a leader building up a massive army that he plans on using to crush the human?Â Have him say so!
I know what youâ€™re thinking, and sure, this gives the player advanced warning and makes it harder for the AI to actually, you know, crush the player.Â But at the same time it alsoengagesÂ the player, gets him or her to worry about what the AIâ€™s up to, to start coming up with plans, toÂ adapt. When the AI does eventually show up with a big army, the players will then think to themselves: â€śAh ha! So the AIÂ wasÂ actually planning this for a while.â€ť Players will naturally pay more attention to what the AI says in the future. It also makes the AI appear more intelligent than if it had it simply executed a perfectly-timed sneak attack and never once made a peep about it.
Players have no idea what the AI is doing under the hood, and they donâ€™t even care. Itâ€™s on the developers to make sure the fruits of their labor are seen. It can be worthwhile to spend time on â€śfluffâ€ť AI which shows off what the computer knows, even if thereâ€™s no actual gameplay effect at work. A few lines of code that generate an illusion of a smart AI will often have a much greater impact than a massive, complex system which results in anÂ actuallyÂ smart AI.
Of course, the AI can brag all it wants, but at the end of the day once it shows up on the playerâ€™s doorstep it still needs to fight competently! Which brings us to our dishâ€™s next key ingredientâ€¦
PreventingÂ Dumb AI
Okay, so the AI army has actually come aâ€™knockingâ€¦ now what? Well, letâ€™s first establish what our priorities are.
Itâ€™s much more important for an AI to beÂ not dumbÂ than for it to beÂ smart. What do I mean by that?
An AI is labeled â€śbadâ€ť not when it fails to execute a flawless pincer maneuver, but when it fails to use its full-strengthÂ God UnitÂ to kill a wounded one belonging to the human. â€śThis AI is so stupid, a real player would never have let that unit get away!â€ť
Developers should always focus first and foremost on eliminating the number ofÂ obviousmistakes an AI makes. Itâ€™s awesome if the AI is able to pull off that pincer move, but itâ€™s nothing but a â€śnice to haveâ€ť feature until all of theÂ dumbnessÂ has been sifted out. If players spot justÂ oneÂ stupid move theyâ€™ll immediately lose all faith in their computer opponents, and once that happens thereâ€™s no going back â€“ the illusion is forever broken.
Okay, so a good AI isnâ€™t dumb â€“ fair enough. What about making it, you know,Â smart?Â I wonâ€™t go into specific algorithms or anything, but there are a few general principles worth noting.
The key is setting good priorities and following through on them (noticing a pattern?). Focus on making an AI that is generally simple, but strong in a few areas that will really stand out to players. Setting out to create artificial opponents that are unbeatable in every way is a foolâ€™s errand (unless you have five years during which the gameplay rules never change â€“ good luck with that!). The fewer moving parts an AI has under the hood, the easier it will be for a programmer to add new features and improve existing ones.
Build a system that is designed to change â€“ a lot. A gameâ€™s AI is one of the pieces that needs the most iteration and you want it to be easy to jump in and change the behavior in ways that are obvious. The more fiddling that is necessary, the less likely it is that developers will be able to â€“ or worse,Â wantÂ to â€“ go in and change things. If opening up the AI code always results in a hearty sigh, it becomes much easier to give up and proclaim â€śehhh, itâ€™s fine the way it is!â€ť
Itâ€™s crucial to get the AI performing simple tasks competently as quickly as possible. Once the basics are in place, complexity can always then be added. AI is one of the very few areas of game development where doingÂ too muchÂ design can be harmful. The design itself is rarely the problem, but spending too much time on it before anyone sees in-game results often leads to superfluous complexity and a diminished focus on the high-level objectives.
Advanced AI techniques like neural networks and genetic algorithms are incredibly powerful and can do some amazing things â€“ unfortunately, theyâ€™re terrible for most games, especially those which undergo significant iteration (and typically, the more iteration you do the better). To emphasize my point from above: theÂ simplerÂ an AI is, the more likely it is to work and to be easily improved. When the development team has less control over whatâ€™s going on it becomes much,Â muchÂ harder to achieve focused, narrowly-defined goals.
The Designer and the AI
The odds of your AI entrĂ©eÂ being a hit withÂ any complex game (particularly those in the strategy genre) Â is dramatically reduced if the game designer and AI programmer arenâ€™t the same person.
At its core, the AI is just one gameplay system among many. Do you want a non-designer making the plan for one ofÂ theÂ most important and salient features in the entire game? A designer needs to spend significant time and effort establishing what the goals and focus should be for all systems â€“ the AI is no exception. Simply handing off this to a programmer is usually a recipe for disaster.Â The job of a programmer is to write code that is efficient, robust, and easy to maintain â€“ itâ€™s a designerâ€™s job to ensure the in-game experience isÂ fun. Those goals do not naturally align. When no direction is given, programmers will typically architect and code a system just like they always would.
Thatâ€™s not to say a game is outright doomed to failure should the game design be done by one person or team, and the AI by another. However, this does require both groups to be extremely organized and vigilant. The design team needs to make sure that all goals for expected AI behavior are clearly outlined, just as they would do with any other system.
Even so, designing an AI presents significant challenges that other gameplay systems lack, which is why itâ€™s often preferable for the game designer and AI programmer to be one in the same. There are a number of problems that AI simply cannot solve in an environment where an answer is needed within seconds, at the very most. There are some tasks computers naturally excel at, and others that it finds nearly impossible â€“ managing multiple scout units simultaneous is a piece of cakeâ€¦ building and launching a three-pronged, multi-theatre invasion in exactly 27 turns is, uh, not. While not always feasible, when the individual designing the AI also understands the technical limitations and opportunities in playÂ itâ€™s much more likely a projectâ€™s AI goals will be achieved.
Being an AI programmer is a tough job. The results of oneâ€™s work are often nebulous, and when theyâ€™reÂ notÂ itâ€™s usually because of some big, obvious problem. But that grey area also leaves plenty of room for creativity and interesting problems to solve.Â A splash ofunwavering focus on the end goalÂ and a pinch ofÂ avoiding getting bogged down in complexity for complexityâ€™s sakeÂ should ultimately make for a rather tasty dish. Good luck in the AI kitchen!