
Ask HN: How does an online game with AIs manage so many AIs? - maruhan2
Each AI should use up a pretty significant resource of the server running the AI. How do the games possibly manage so many of them? Most education on AI is how to make them smarter, not how to make them cheaper. There has to be well known tips and tricks about scaling AI. What are they?
======
flohofwoe
It's not 'AI' (what actually is?), but mostly hand-crafted state-machines
trying to achieve game-specific goals, which directly inspect the current game
state instead of trying to build their own 'world model' (of course with
restrictions like fog-of-war, visibility-checks and so on so that the cheating
isn't too obvious).

Really no _that_ much different from what the Pacman ghosts did, just
developed further from there (especially during the Golden Age of strategy
games during the late 90's and early 00's).

With this base, scalability is achieved through fairly traditional performance
optimizations (mostly using the right algorithms and data layout, especially
for path-finding and 'visibility checks').

Also in strategy games, AI is often layered like a military chain of command,
where the "commander AI" only makes high level strategic decisions and only
has a very 'sparse' world model, while the lowest level 'soldier' AIs are
mostly occupied with pathfinding but only know about their immediate
surroundings.

Also, each game genre has their own highly specialised, hand-crafted "AI"
algorithms. A first-person-shooter AI is completely different from a car-
racing-game or realtime-strategy game.

~~~
louthy
Exactly this. It's also worth mentioning that 'true AI' would be an absolute
nightmare in terms of delivering a game, the QA testing burden would be so
enormous for any reasonably large game that it'd be difficult to get it out
the door.

I remember working at a games studio years ago and a kid straight out of
university joined us after doing a degree in AI - assuming that games used
real AI. Nope, it's all smoke and mirrors.

~~~
skocznymroczny
No one has managed to create a real AI (as in strong AI). Machine learning,
deep learning is smoke and mirrors too.

~~~
endymi0n
...and in games, you actually _want_ smoke and mirrors:

[http://www.techradar.com/news/gaming/shouldn-t-our-
enemies-b...](http://www.techradar.com/news/gaming/shouldn-t-our-enemies-be-
smarter-by-now-gaming-ai-s-tricky-problem-of-smoke-and-mirrors-1245180)

If you really had hard and strong AI, a lot of games certainly wouldn't be as
much fun to play anymore for casual players (spare some games you want to be
frustrating). You want the protagonist to eventually conquer the enemies and
win.

~~~
netzone
Yup. Imagine a boss in a game decides it's not in his best interest to be
predictable, or take matters in his own hands and attack the players when they
aren't ready. Or any number of things that sounds cool on paper, but would be
exceedingly frustrating when actually playing the game.

~~~
StavrosK
Imagine a boss in a game tired of fighting every day, and just giving up and
not fighting back.

~~~
mathgeek
That's essentially the plot of Wreck-It Ralph.

------
miga
Most of the work needed for decisions is shared between AI instances using a
few tricks:

1\. Precomputing information for all agents, like using global shortest path
(Bellman-Ford).

2\. Abandoning per-unit subjectivity (a lot of units will share same view) or
in other words: limiting internal unit state, using a shared state between
groups of units.

3\. Decoupling expensive algorithms into multiple simple steps. (Like state
machine simplifies regular expression.)

4\. Using separate AI and visualization thread, and using low amortized cost
data structures (priority queues etc.).

You may observe all these three rules by anomalies in unit behaviour. Play
[http://www.screeps.com](http://www.screeps.com) to learn a lot about modern
RTS AIs :-).

~~~
amelius
For an online game, an additional trick could be to run the AI code on the
users' computers.

------
pjc50
"AI" is a ridiculously general term. People use it to talk about everything
from line-following robots to machine vision and speech / natural language
recognition.

Most game "AI" has nothing to do with the currently trendy "AI" based on
linear algebra and neural nets. It's much, much simpler, partly because it has
direct access to the game state and doesn't have to do any sensor processing.

"GameAI" tends to be a bag of puppetry tricks. You don't even need the
strategic depth of chess, just something that gives you an interesting enough
and varied enough move/fire pattern.

~~~
jrimbault
I vaguely remember something like an interview where a dev told the story of
how by making their bots cleverer the player satisfaction had gone down.

~~~
amelius
If the relation was monotone, this would mean that playing against real humans
is less fun than playing against a stupid AI. Which is not the case, so I
guess we have something like an "uncanny valley" here.

So making the bots not a little bit more clever, but _really_ a lot more
clever, would improve the game.

~~~
jrimbault
In what I remember, they made their bots so clever it wasn't fun because it
overpowered any player in their tests. Humans want to win, so losing every
time wasn't fun.

I'm trying to find that interview.

~~~
Radim

      Humans want to win, so losing every time wasn't fun.
    

This needs to be a bit more qualified.

I personally love games where the skill gap is SO HUGE it seems impossible to
win at first. I get my ass kicked for months and years before becoming any
good. Go, Quake, Elastomania... Losing doesn't trouble me at all.

On the other hand, "reasonable" games where advancement is assured, the skill
curve mostly flat (I can beat top players with a bit of luck or the right
items / grind / setup), hold no appeal to me whatsoever. Diablo & co.

~~~
reificator
> Losing is fun ~ Dwarf Fortress Motto

I'm on basically the same boat. One of my biggest frustrations with my gaming
friends is that they can't ever lose. Sometimes they can't even win unless
they win by enough.

When I play games I'm there to have fun. Trying to win is fun, winning is fun,
losing is also fun. It's all fun.

DotA, LoL, etc would be games I'd play a lot if the community weren't the
shining example of "can't lose, or even win by less than they `should have`".

------
60654
There's a number of tech solutions to scaling mob AI. Most of these are also
applicable to non-online games as well:

\- Running AI at a different frequency than the game. The network event loop
might run at 30Hz (33ms tick delta) but AI doesn't need to make decisions that
often, maybe every 100ms or 500ms etc.

\- Separate decision making from performance. A cheap execution system can run
at a high frequency, while an expensive decision making system runs at a lower
one.

\- AI LOD. Mobs that are far away run much simpler AI that doesn't try to
perform detailed animations etc. Mobs that are in unpopulated areas get
frozen, and thawed once somebody enters.

\- In instanced dungeons, mobs don't even exist until the players enter, and
once players leave, the mobs just get unceremoniously killed.

\- Offloading. Mobs are just game agents so in online games it's possible to
offload them to other servers that get load balancer as players move about,
areas get frozen etc.

Basically the big question is: if nobody is interacting with a mob, does it
need to exist? In most online games they do not. And even in sim heavy games,
maybe they don't need to run the same high fidelity AI when nobody is
watching.

Then the last tip is:

\- Make the AI as cheap as possible for the task at hand, but no cheaper. A
mob in MMO that is all aggro and easy to kite doesn't need to have complex
strategic cognition, and it would probably be a detriment. Everything need a
to match it's context.

Source: I make games. :)

------
opless
Most AI in games are simple.

Either state machines or behaviour trees. So they're designed to be quick to
execute by design :)

~~~
opless
Also they tend to have manager classes iterating over a list of NPC data,
rather than lots of NPC classes and executing "run" methods.

Edit: undo autocorrect typos

~~~
letharion
And while you're iterating the same code over many NPCs, you could take great
advantage of GPUs and gain significant performance by running all those NPCs
in parallel.

------
TeMPOraL
Two things:

1) Like 'opless wrote, AIs in most games are simple; they're designed to
execute in real-time.

2) From the point of view of a networked game, AIs and players are the same -
the server(s) only care about inputs. The difference between keyboard input
and AI input need not to be relevant, and this suggest a trivial approach -
execute AIs on clients, and send their inputs along with players' inputs.

~~~
omtose
>execute AIs on clients, and send their inputs along with players' inputs

That seems strange, you'd be sending multiple times the same AI's actions to
the server, in which case you better be certain that it's deterministic, not
to mention the security concerns. It seems far easier to execute the AI on the
server and send its actions to each player, especially if it's a trivial AI.

~~~
maffydub
I wonder what you might actually do is execute AIs on both, and send
occasional snapshots of the AI state from the server to the clients.

This gives you the benefit of low-latency on the client (don't have to wait
for the server to tell it what the AI is doing) but also avoids security and
non-determinism worries. (Although the AI might still be non-deterministic, it
probably can't diverge too much before the client receives the next snapshot
from the server).

This might be more effort than it's worth, though...

~~~
posterboy
> benefit of low-latency

you achieve the benefit by loosening consistency, i.e. the local ai will have
to react to local events before those are acknowledged by the server to see
any gain, and the server might come to different opinions because it sees a
different state of the game considering multiple clients who are constantly
out of sync.

That leads to warping. A rocket, to take an example of an extremely simple
entity, might fly a few meters on your screen only to explode right in front
of your face because the server sent a message that the flight path was
blocked by an online enemy that your client didn't predict.

------
new299
For many games (shooters etc) making a smart AI is relatively easy. What's
hard is making an AI that's provides a realistic and interesting challenge.
These "AI"s don't use much in the way of machine learning however.

I feel like this isn't what you're getting at though. Perhaps there's a
specific example you have in mind?

------
fulafel
Most comments here are explaining what's meant by game AI's and how they're
not closely related to "AI" as understood in other fields.

There definitely is lots of room for interesting applications of "real" AI in
games. Apart from NPCs, learning and building models about the player has lots
of potential too.

The interesting-game-ai field is of hard to keep track of, because the search
resuls are full of combat AI discussions, which is pretty uninteresting.

------
euphetar
Could you provide examples of games? The answer is very different for, say RTS
or Shooter, MMO or match-based, etc.

------
moomin
You'd be surprised at how devious some of the tricks are. I'll give you an
example from Startopia: visitors want particular services. These have queues.
If the queue is too long, the visitor looks for a shorter line. Except... this
led to them aimlessly wandering in circles. Oops.

So, instead, when they decide they want a service, they reserve their place
there and then and then just walk to their spot. A completely hidden variable,
cheap to compute, and the AI now behaves "sensibly".

In general, the AI of the average opponent in a computer game is way less
complex than that of a housefly. Primitive communication and responses to
external stimuli is about all there is.

------
hashmal
> Each AI should use up a pretty significant resource of the server running
> the AI.

The premise is wrong, in many games AI can be extremely cheap. It's usually
not the same "AI" as in other fields. Game dev is a completely different
dimension.

------
hypercluster
I've started playing Destiny 2 and wondered the same thing. There are enemies
seen by me and other players at the same time.

So the enemies have to be controlled server side not locally. Also there can
be (and will be) a lot of enemies around the "Destiny world" at the same time.
What kind of servers do you need for that? That's not just processing HTTP
requests and fetching data.

------
sidcool
Could there be a not mathematical model of AI

------
posterboy
besides the other sensible opinions on the engineering, if those fail, the far
simpler answer is to pay for server farms.

