Ask HN: Where to start on creating AI for games?

87 points by foxpc ↗ HN
Hello, I've been interested in AI creating for a while. I'd be most likely interested in bot making (for casual/learning purposes only, really). How does one start doing that?

I do have knowledge of programming but not really sure how to grasp the whole concept.

Interested in concepts of how AI works in games such as Starcraft (I suppose, Warcraft too), Hearthstone (and other card games), Counter-Strike work. While I guess the FPS AI (at least the official bots) are not as fair as it might see - they're probably rigged to fail at some point just like chess engines are when are presumably playing in lower ELO.

Are there any books, tutorials, challenges that would be a great starter for me?

48 comments

[ 2.4 ms ] story [ 79.2 ms ] thread
http://codecombat.com/ may be a good starting point for writing simple AI logic.
That does look like fun. There are many online programming contests that work similarly to that, and I would recommend starting with one of them.

I'd also add that while everyone suggesting AI resources is helpful, the best thing to do is just jump in and start coding. One of the problems with our formal education system is that it often ends up trying to present solutions to problems before the students actually understand the problems, resulting in students that learn fairly superficial understandings of the solution that the brain naturally ends up garbage-collecting. Just code things for a couple of weeks. You should notice you're sort of hitting a wall around that time. You should notice that you see behaviors in your opponents that you do not currently know how to implement. That is the time to start cracking into the AI texts and learning from those who preceded you, and everything will tend to make total sense why you'd want to do that, instead of being these abstract thoughts floating in space that tend to go in one ear and out the other.

(I am convinced, incidentally, that this is why people can so confidently proclaim the uselessness of a degree in computer science to programming; what is taught is incredibly useful, but trying to jam it in for four years before finally turning you out into the real world to actually encounter the problems you've supposedly been learning the solutions to is a terrible way to go about learning. Take advantage of the ease of just hiking out into the coding wilderness and encountering these problems yourself before trying to learn their solutions. It is something the other engineering disciplines would kill to be able to do!)

"I'd also add that while everyone suggesting AI resources is helpful, the best thing to do is just jump in and start coding."

I'd modify that slightly: skim (an hour per chapter? something like that) one or more comprehensive books (for this subject, Russell and Norvig is good) enough to get some idea what is known, then jump in and hack seriously, then interrupt your hacking when appropriate by going back to study the particularly relevant stuff that you know is already known. Jumping in is good and important, but it tends to be a lot more efficient when you have some idea about the outlines of what has already been worked out for you.

The times I haven't been able to follow this advice --- notably not having physical access to university libraries when I was trying to write a C compiler for a Z-80 as a teen back in the 1980s, hence having no very practical way to learn about existing work on parsers and stuff --- have been better than nothing, interesting and educational but not as efficient as doing stuff when I had a lot of stuff to study as needed.

Incidentally, a similar strategy can be very helpful for formal study of nontrivial subjects, e.g. various college-level engineering courses. Skim the text, and maybe another text from the library too, and/or an online Wikipedia-level survey/tutorial/whatever before the course starts. Then you have a much better chance of seeing how things fit together and of finding ways to clarify things you're puzzled by.

Learning the A* pathfinding algorithm is a good starting point as this is common in simple tile games. These two links should get you started:

[1]: http://www.raywenderlich.com/4946/introduction-to-a-pathfind...

[2]: http://theory.stanford.edu/~amitp/GameProgramming/

Caveat/warning: Oftentimes, learning how to translate your game data into an A* -traversable graph is harder than implementing the A* algorithm itself.
(comment deleted)
I've personally did a school project some years ago writing an AI for Transport Tycoon Deluxe, at the time it was a straightforward api; IDK about tthe current status. At the time though, I quite enjoyed it, also because of the subject matter. Linky: https://wiki.openttd.org/AI:Main_Page
To create a good (i.e. actually useful) bot you want to start by spending a lot of time in whatever game you're targeting.

Most bots, or trivial helpers like macros, are written by the players that have spent a lot of time in a game and identified the elements most worthy of automation. The player's lack of in-depth technical knowledge then leads to the mass of crappy bots out there.

If you're coming from the opposite side, i.e. you have technological knowledge but don't know where to start, all you're missing is domain knowledge of an individual game or genre to figure out what you actually want to build.

Edit: it's also worth noting that game AI and bots are pretty different problems.

Back in the day I really enjoyed Matt Buckland's AI books.
I would suggest learning how to use Sekuli to automate games.
There are plenty of books online. I would recommend creating your own project and finding something specifically you would want to do with that topic. For a class project my group and I did something simple, like a minesweeper bot. It also included a screen scrapper to scrap from windows and created our own minesweeper game including the AI part. It had about a 30% failure rate because it would get stuck on the 50-50 chances of it picking the wrong square. But that was a lot of fun. We did a bunch of statistics on it also.

This is a very broad topic because it can range from everything from 2D to 3D and different genre games.

I read and enjoyed this one. I would recommend it to start: http://www.amazon.com/Behavioral-Mathematics-Game-AI-Applied...

It covers basics like game theory and goes into some of the mathematics and some code examples.

Here are some good slides from Valve on how they did Left For Dead http://www.valvesoftware.com/publications/2009/ai_systems_of...

Good luck, have fun

I second the Behavioural Mathematics book. Really good introduction to AI and how to think about programming it.
Funny (and probably a bit off topic) fact : if today I have a coding job it's because when World of Warcraft was all the rage, after a while I started "botting" then wanting to make my bots better I learned to code and a few months later I was enrolling in an IT school. The rest is history as they say.

If you want to interface with games, I would highly recommend looking at Innerspace (company was named Lavish something). My knowledge is from 10 years ago, it might be discontinued though (I'm not botting anymore).

edit : oh btw if someone at Blizzard read this, I would totally pay for 10 accounts on a botting friendly WOW server. I can always dream ;-)

edit 2 : yup they are still in business : http://www.lavishsoft.com/

Creating "AI/bots" for games comes in three parts/flavors/divisions:

First, and the most critical to most games, is basic behavioral AI. This includes things like pathfinding, locating targets, attacking, and otherwise making an entity in the game interact with the environment in the game.

Second, you've got the more abstract parts, the "high-level decisionmaking". This is the AI's strategies and tactics, such as communicating with other AI, coordinating with squad members to circle an enemy, deciding when to change weapons or throw a grenade or pull back to reload, etc. In most cases, developers put in many crude heuristics here either because they don't have time to think of anything better, or for performance reasons. This will look like "if known nearby enemies > 2 and ammo < 20%, find safe point to fall back, reload". The developer thought up this quick rule of thumb and told the AI to do this, without having to code the AI with the intelligence and information the developer used when making up this rule of thumb.

Third, bot creation (for the usual "hacker" version/meaning of the term, as in e.g. bots used by AFK players to farm) is significantly different than making an "official" AI as a developer or writing a replacement AI using available scripting in some games. For most games, bots will involve various hack techniques for retrieving information about the game world (occasionall literally involving visual pattern recognition, which is a whole other ball of yarn (and separate type of "AI") you don't want to mess with just yet). Bots will also usually involve indirect control of the game character by using some third-party program to "send" keys to the game using OS functionality. This is probably not what you're interested in, and you'd need to learn at least some basics of the first two to be able to achieve anything here.

One thing that was rather popular a while ago is to just dive into a game like Robocode[1] and work up your way until you can code one robot that will on its own beat various competition-grade robots without user input to adjust for the opponent's strategies.

[1]http://robocode.sourceforge.net/

Building off of your #3) An example for this would be the botting programs made for Runescape. Developers wrote an application that would sit on top of the game client and then use reflection in Java to see what's happening in the game: Various objects being created that are now on screen, the players direction, the map grid, etc.

The developers of this botting program would then expose their own API and scripting language that would allow other developers to interact with the game through their program. The could then write scripts such as: Go to this location via map coordinates, pick up some object, solve a puzzle, fight some boss, eat when below a certain health, etc.

Note: I used to run a VPS company that would specifically cater to those that were botting in Runescape; this allowed them to run their bots 24/7 without using their own hardware.

Right. I wrote one of the earlier RuneScape 2 bots around 8 years ago (example script https://clbin.com/71NIX).

It's worth noting that these were very easy identifiable as bots by behavior. Making AI for a game (that actually seems human and is fun to interact with, past just target selection and pathfinding) is another (much more difficult) set of problems entirely.

Holey shit it's Regex :). Old ARGA dev here :).
1. Grab Unreal Engine[1]. It's $20 per month, but you can cancel after the 1st month if you don't want to receive updates.

2. Download the Unreal Tournament project[2]

3. Examine the bot code and start tweaking

[1] https://www.unrealengine.com/

[2] http://www.unrealtournament.com/

Unreal is an industry-grade game engine that is also quite accessible. AI can be scripted in Blueprint (a visual dataflow scripting language) or C++.

Unreal Tournament 2014 is 100% crowdsourced. If you own the Unreal engine you can download, play it, modify it and - if your modifications are good enough - feed them back in to the game itself. The community is apparently very welcoming.

I plan to start looking into the Unreal engine soon after I get experience with a more basic engine. Have you found a lot of the resources available are able to get you started well? Or is there a large learning curve? My main experience is in writing Rails apps, Ruby gems, a couple basic Android apps, and everything DevOps.
It's pretty smooth. Epic have really polished the UX to make such a complex piece of software understandable and soften the barrier to entry as much as possible. There are all sorts of tutorials.

I'd say it's easier to learn than Unity at this point, but harder than 2D engines like GameMaker. I think some basic experience making simple 2D games will help a lot before the transition to 3D.

If you're already familiar with Javascript and you want to start making games at a lower level you could try the PhaserJS framework.

The best start I ever had to Artificial Intelligence was via UC Berkeley's CS188 course[1]. This course takes you through the very foundations of AI, from search to planning to basic probability theory. It's been imitated at universities around the world with its open lectures, slides, homework assignments and project code.

The projects are the very best part of this course. You start with a blank slate PacMan agent trying to pick up dots to something able to track & dodge ghosts without even seeing them (!). It's what inspired me to continue AI research 7 years down the road!

[1]: http://inst.eecs.berkeley.edu/~cs188/fa11/lectures.html

Hey there, thanks for the great link!

As for AI (in general, not only for games) - AFAIK nowadays most serious work is done in non-symbolic, statistics/stochastic direction (including all the Big Data and ML stuff)... So what do you think about pure symbolic AI? Is there any research/advancements done in this part? I know I'm oversimplifying it, but I think you got the point... :)

I took a course on AI at University. The course had a lot of projects around video games. I liked the book we used:

http://www.amazon.com/Programming-Example-Wordware-Developer...

Our final project was super cool. We got into groups. Then each group modified the AI of the soccer team in the book. Then we had a tournament to see who's team was the best. Winning the tournament was one of my prouder moments at school.

Google Ants[0] is an AI challenge that is over now, unfortunately, but you can still run through it.

"The AI Challenge is all about creating artificial intelligence, whether you are a beginning programmer or an expert. Using one of the easy-to-use starter kits, you will create a computer program (in any language) that controls a colony of ants which fight against other colonies for domination."

[0] http://ants.aichallenge.org/

If i'd be on the same quest, i'd first read a lot of articles on gamesutra concerning AI. Here's a quick google search - http://goo.gl/aHZrXj

There are a lot of "handson" examples there from real existing games (with code) and interviews with game developers.

I really recommend this article on behaviour trees, it gave me a good intro on how to start with the basic structure of AI - obviam.net/index.php/game-ai-an-introduction-to-behavior-trees/
Take a look at wildfiregames 0ad, if you like RTS. 0ad is an open source RTS, where its easy to write your own AI in JavaScript.

A much bigger challenge would be an AI that is able to walk an avatar in Secondlife. First perhaps easy walk on the roads in mainland, later walk on arbitrary land, and the real challenge is walk inside buildings. A cat bot that does not fall into the lake, might even be a good seller.

I would say that it's best to start with turn-based games, like Bridge or Chess or (shameless plug) Ambition (and if you take on Ambition, let me know how your AIs do). Some games, like Othello, aren't hard to beat. Or you could do one of the German board games if you want to take on a more heterogeneous environment than in, say, Checkers.

This will get you familiar with game AI as a topic. Then you want to take on RTS and FPS games, which are similar but have deadlines (frame rates) and more complexity in the environment. But it's important to get the basics down before taking on a complicated game.

You could start at the beginning. Some people are still unaware that the four ghosts from Pac-Man each have a different decision-making algorithm. The red ghost always targets the player. The pink ghost targets four spaces away from the player in the direction it faces. The cyan ghost uses a combination of player position, player facing, and the position of the red ghost to determine its target tile. The orange ghost targets the player when far away, and a home corner position when the player is close.

Those four strategies in combination, along with some global behaviors for pathfinding and behavior modes, are often enough to defeat an unaware player. (search: "Pac-Man dossier")

This introduces the concept of strategic archetypes. Human players have them, too. I, for instance, often favor stealthy sniper strategies. The various strategic archetypes often have a complex relationship web from who tends to defeat whom. The player that always tries to do an early rush to overwhelm slower opponents before they can mount a defense may be easily defeated by a trap-and-decoy opponent, whereas that player may lose to a gather-intel-then-send-ninjas player.

To create a competitive AI, first identify how humans play the game, identify the strategic archetypes, then model your bot to pursue archetypal goals. Then, most importantly, determine when to switch archetypes. An early rusher AI has no strategy for midgame or endgame, so perhaps that AI evolves into aggressive expansion, or an ambusher.

But also remember that the point of the game is for the humans to have fun. Avoid cheating, or overusing a game-breaking strategy. Your bot won't care if it loses, but a human may ragequit if he loses to a bot with a perceived unfair advantage.

Of course, there are some games with no human players. There's the "rock paper scissors programming competition" (rpscontest.com), where players submit algorithms, who then fight each other to the death for rank points. It is interesting to see new strategies rise and fall, including those who win by "cheating", such as by manipulating the match-generating program itself, setting the opponent's random seed, examining the opponent's heap or stack, or causing the opponent to be improperly disqualified for cheating.