
Ask HN: Where to start on creating AI for games? - foxpc
Hello,
I&#x27;ve been interested in AI creating for a while. I&#x27;d be most likely interested in bot making (for casual&#x2F;learning purposes only, really). How does one start doing that?<p>I do have knowledge of programming but not really sure how to grasp the whole concept.<p>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&#x27;re probably rigged to fail at some point just like chess engines are when are presumably playing in lower ELO.<p>Are there any books, tutorials, challenges that would be a great starter for me?
======
maccard
There's a few really good fun AI challenges that you can get stuck into - Even
though they're ocmpetitions and over, you can still get the tools for them
[http://www.marioai.org/](http://www.marioai.org/)
[http://www.sscaitournament.com/index.php?action=tutorial](http://www.sscaitournament.com/index.php?action=tutorial)
[http://www.slideshare.net/coboslab/demolition-
derby-2012-gec...](http://www.slideshare.net/coboslab/demolition-
derby-2012-gecco-competition-report)
[http://www.cloudball.se/](http://www.cloudball.se/)
[http://www.computerpokercompetition.org/](http://www.computerpokercompetition.org/)
[http://aichallenge.org/index.php](http://aichallenge.org/index.php)
[http://www.botprize.org/](http://www.botprize.org/)
[http://aigamedev.com/open/coverage/ctf-
report/](http://aigamedev.com/open/coverage/ctf-report/)
[http://aibirds.org/](http://aibirds.org/)
[http://geneura.ugr.es/cig2012/competitions.html](http://geneura.ugr.es/cig2012/competitions.html)

------
DaFranker
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/](http://robocode.sourceforge.net/)

~~~
tjbiddle
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.

~~~
regecks
Right. I wrote one of the earlier RuneScape 2 bots around 8 years ago (example
script [https://clbin.com/71NIX](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.

~~~
ashleyp
Holey shit it's Regex :). Old ARGA dev here :).

------
johnymontana
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...](http://www.raywenderlich.com/4946/introduction-to-a-pathfinding)

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

~~~
mirkules
I'll second this. Also, for board games like chess or othello, you should take
a look at the mini-max algorithm:

[https://en.wikipedia.org/wiki/Minimax](https://en.wikipedia.org/wiki/Minimax)

------
teamonkey
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/](https://www.unrealengine.com/)

[2] [http://www.unrealtournament.com/](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.

~~~
clebio
Well, I like the idea, but why do all their documentation pages link to a
Github 404:
[https://github.com/EpicGames/UnrealTournament](https://github.com/EpicGames/UnrealTournament)
?

~~~
teamonkey
Private repos. You need to link your GitHub account to your Unreal account.
See here
[https://wiki.unrealengine.com/Getting_Started_With_Unreal_To...](https://wiki.unrealengine.com/Getting_Started_With_Unreal_Tournament)

------
growingconcern
AI programmer here...generally "bots" are just AI that is intended to take the
place of a player (typically in a versus setting and not a co-op setting).

If anything bots are tougher to make then "normal" AI since their value is
measured by their performance against humans. Also the less you cheat (perfect
knowledge of the other players, perfect aim, etc) the better your bot. Having
a non-cheating bot beat a human is a major achievement.

The bots for a strategy game like StarCraft is fundamentally different than
the bot for an FPS. They use completely different techniques and it's much
harder to cheat in strategy game (though ignoring the fog of war is the most
egregious). Strategy game AI is more akin to chess AI - lots of search
(minimax and stuff). It's a whole field onto itself and you can only learn by
studying the AI for that type of particular game.

FPS bots (like counter strike) are much closer to traditional game AI
(shooters and such). Books on "Game AI" are a good place to start. Basically
the two major problems is where should I go (evaluating cover, analyzing line
of sight, estimating where enemies are and trying to pick a good place to
stand) and how to I get there (pathfinding and pathfollowing). What you do
when you get there is generally pretty straightforward (shooting at the
appropriate guy). If you have a cover system you need to be able to tell them
how to use cover and this involves a lot of animation (syncing my animations
and position up to the cover in the world). But in an FPS this is usually just
a question of crouching or not.

The big difference for FPS bots (versus single player enemy AI) is that bots
need to choose between conflicting goals (should I go get ammo, should I go
get health, should I attack the enemy). If you have a strict hierarchy of
behaviours your bots won't be competitive (enter fuzzy logic systems, utility
theory and the like - normal behaviour trees won't cut it).

Have fun!

~~~
DaFranker
> Having a non-cheating bot beat a human is a major achievement.

(for some categories of games!)

I'm almost nitpicking here, but it's important to understand the distinction
where when trying to build "human-like" AI that players feel are like the real
thing, you also want to emulate human shortcomings. Sometimes it might not be
cheating per the rules, but still be obviously outside the realm of current
human capabilities.

e.g.: An AI opponent with perfect arithmetical abilities in a game like
Scorched Earth:
[http://en.wikipedia.org/wiki/Scorched_Earth_(video_game)](http://en.wikipedia.org/wiki/Scorched_Earth_\(video_game\))

------
duckworthd
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](http://inst.eecs.berkeley.edu/~cs188/fa11/lectures.html)

~~~
zerr
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... :)

------
timme
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.

------
Ethan_Mick
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/](http://ants.aichallenge.org/)

------
andredublin
Here are some resources on AI programming

[http://gameai.com/](http://gameai.com/)

[http://aigamedev.com/](http://aigamedev.com/)

[http://www.gamedev.net/page/resources/_/technical/artificial...](http://www.gamedev.net/page/resources/_/technical/artificial-
intelligence/)

I highly recommend this book

[http://www.gameaipro.com/](http://www.gameaipro.com/)

------
vanmount
[http://codecombat.com/](http://codecombat.com/) may be a good starting point
for writing simple AI logic.

~~~
jerf
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!)

~~~
wnewman
"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.

------
logfromblammo
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.

------
wsloth514
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...](http://www.amazon.com/Behavioral-Mathematics-Game-AI-
Applied/dp/1584506849)

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...](http://www.valvesoftware.com/publications/2009/ai_systems_of_l4d_mike_booth.pdf)

Good luck, have fun

~~~
chatwinra
I second the Behavioural Mathematics book. Really good introduction to AI and
how to think about programming it.

------
dbish
I used this book for a Game AI class and the examples were easy to consume
with a good focus on using the methods in real games:
[http://www.amazon.com/Artificial-Intelligence-Games-Ian-
Mill...](http://www.amazon.com/Artificial-Intelligence-Games-Ian-Millington-
ebook/dp/B00AQNW0GI/ref=tmm_kin_title_0?_encoding=UTF8&sr=8-2&qid=1418319088)

A little pricey, but my favorite intro book for this subject.

~~~
drhayes9
Seconded. Covers a lot of ground, lots of great examples, the code is
language-agnostic (Python-ish). Hard to go wrong with this book on your shelf.

------
jmnicolas
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/](http://www.lavishsoft.com/)

------
clebio
Not exactly AI, but the Processing language has a 'flocking' tutorial:
[https://processing.org/examples/flocking.html](https://processing.org/examples/flocking.html).
There's also a version in ProcessingJS:
[http://processingjs.org/learning/topic/flocking/](http://processingjs.org/learning/topic/flocking/)

And, from HN a few days ago, Red Blog games has some awesome, in-depth
explanations of game-related programming:
[http://www.redblobgames.com/pathfinding/tower-
defense/](http://www.redblobgames.com/pathfinding/tower-defense/)

------
robmurrer
Maybe implement Minimax [1] in your favorite language and apply it to a simple
game like tic-tac-toe.

[1]:
[http://en.wikipedia.org/wiki/Minimax](http://en.wikipedia.org/wiki/Minimax)

------
ttalle
There were recently posts about a new Javascript based MMO AI strategy game
called Screeps [1]. They are on Indigogo [2] at the moment. Maybe this can be
a nice learning challenge...

[1] [http://www.screeps.com](http://www.screeps.com)

[2] [http://igg.me/at/screeps/x/9209812](http://igg.me/at/screeps/x/9209812)
(affiliate link, is that allowed? otherwise:
[https://www.indiegogo.com/projects/screeps](https://www.indiegogo.com/projects/screeps)).

------
dyeje
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...](http://www.amazon.com/Programming-Example-Wordware-Developers-
Library/dp/1556220782)

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.

------
nicolethenerd
Battlecode ([https://www.battlecode.org/](https://www.battlecode.org/)) is a
programming competition run by MIT every January for programming an AI to
compete in a game (the exact nature of the game changes every year. It's
vaguely Starcraft-like, except completely played by AIs)

It's open to non-MIT students, as well.

They also offer lectures in January which will be streamed on twich.tv - these
may be of some interest to you, even if you decide not to compete.

------
pcestrada
I'm currently working my way through this book Artificial Intelligence for
Games [1] and am finding it very approachable for someone with no background
in AI.

[1] [http://www.amazon.com/Artificial-Intelligence-Games-Ian-
Mill...](http://www.amazon.com/Artificial-Intelligence-Games-Ian-
Millington/dp/0123747317/ref=sr_1_12?s=books&ie=UTF8&qid=1418308148&sr=1-12&keywords=ai+games)

------
kephra
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.

------
Cthulhu_
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](https://wiki.openttd.org/AI:Main_Page)

------
NicoJuicy
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](http://goo.gl/aHZrXj)

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

------
clebio
There's also some relevant material in this book, The Nature of Code:
[http://natureofcode.com/book/chapter-6-autonomous-
agents/](http://natureofcode.com/book/chapter-6-autonomous-agents/)

------
lsjroberts
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/

------
THansenite
You should check out CodinGame. I go on there from time to time to mess around
with the challenges where you program the AI to do the task presented. Might
be a fun way to get in the AI mindset.

------
ikeellis
What I learned working for Brian Reynolds (Colonization, Civ2, Rise of
Natopms. etc...)

Step 1: int result = rand(100); switch (result) { //... do something simple }

Step 2:

Watch the AI until something dumb happens, improve the logic.

------
Immortalin
I would suggest learning how to use Sekuli to automate games.

------
zerr
Any resources for fighting game AI? (e.g. Mortal Kombat)

------
michaelochurch
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.

------
innguest
Back in the day I really enjoyed Matt Buckland's AI books.

