📄 ch15.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 15 -- Teaching Games to Think</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 15</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>Teaching Games to Think</FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>
<UL>
<LI><A HREF="#ArtificialIntelligenceFundamentals" >Artificial Intelligence Fundamentals</A>
<LI><A HREF="#TypesofGameAI" >Types of Game AI</A>
<UL>
<LI><A HREF="#RoamingAI" >Roaming AI</A>
<LI><A HREF="#BehavioralAI" >Behavioral AI</A>
<LI><A HREF="#StrategicAI" >Strategic AI</A>
</UL>
<LI><A HREF="#ImplementingYourOwnAI" >Implementing Your Own AI</A>
<LI><A HREF="#AIinCommercialGames" >AI in Commercial Games</A>
<UL>
<LI><A HREF="#Battlecruiser3000AD" >Battlecruiser: 3000AD</A>
<LI><A HREF="#CloakDaggerandDNA" >Cloak, Dagger, and DNA</A>
<LI><A HREF="#Destiny" >Destiny</A>
<LI><A HREF="#DungeonKeeper" >Dungeon Keeper</A>
<LI><A HREF="#GrandPrixII" >Grand Prix II</A>
</UL>
<LI><A HREF="#AIResourcesontheWeb" >AI Resources on the Web</A>
<UL>
<LI><A HREF="#WorldWideWebVirtualLibrary" >World Wide Web Virtual Library</A>
<LI><A HREF="#TheUniversityofChicagoAILab" >The University of Chicago AI Lab</A>
<LI><A HREF="#MachineLearninginGames" >Machine Learning in Games</A>
<LI><A HREF="#BibliographyonMachineLearninginStra" >Bibliography on Machine Learning in Strategic Game Playing</A>
</UL>
<LI><A HREF="#Summary" >Summary</A>
<LI><A HREF="#QA" >Q&A</A>
<LI><A HREF="#Workshop" >Workshop</A>
<UL>
<LI><A HREF="#Quiz" >Quiz</A>
<LI><A HREF="#Exercises" >Exercises</A>
</UL>
</UL>
<HR>
<P>
Creating truly engaging games is often a matter of effectively
mimicking human thought within the confines of software constructs.
Because you no doubt want your Java games to be engaging, you
need at least a basic understanding of how to give your games
some degree of brain power. So you begin this week by tackling
one of the most exciting and challenging areas of gaming: artificial
intelligence.
<P>
Today's focus is understanding the fundamental theories of artificial
intelligence and how they can be applied to games. If you're tired
of sifting through source code, you're in luck; today, I promise
to go very lightly on the use of Java code. Think of today's lesson
as a theoretical journey through artificial intelligence as applied
to games, complete with examples of popular commercial games and
the artificial intelligence algorithms they use to keep you coming
back for more. After today, you will have the fundamental knowledge
required to begin implementing artificial intelligence strategies
in your own games.
<P>
The following topics are covered in today's lesson:
<UL>
<LI>Artificial intelligence fundamentals
<LI>Types of game AI
<LI>Implementing your own AI
<LI>AI in commercial games
<LI>AI resources on the Web
</UL>
<H2><A NAME="ArtificialIntelligenceFundamentals"><B><FONT SIZE=5 COLOR=#FF0000>Artificial
Intelligence Fundamentals</FONT></B></A></H2>
<P>
<I>Artificial intelligence</I> (AI) is defined simply as techniques
used on a computer to emulate the human thought process.
<P>
This is a pretty general definition for AI, as it should be; AI
is a very broad research area, with game-related AI being a relatively
small subset of the whole of AI knowledge. Today's goal is not
to explore every area of AI, because that would take up the space
of the book in itself, but rather to cover much theoretical AI
territory as it applies to games.
<P>
As you might have already guessed, human thought is no simple
process to emulate, which explains why AI is such a diverse area
of research. Even though there are many different approaches to
AI, all of them basically boil down to attempting to make human
decisions within the limitations of a computer. Most traditional
AI systems use a variety of information-based algorithms to make
decisions, just as people use a variety of previous experiences
and mental rules to make a decision. In the past, the information-based
AI algorithms were completely deterministic, meaning that every
decision could be traced back to a predictable flow of logic.
Figure 15.1 shows an example of a purely logical human thought
process. Obviously, human thinking doesn't work this way at all;
if we were all this predictable, it would be quite a boring planet!
<P>
<A HREF="f15-1.gif" ><B>Figure 15.1 : </B><I>A completely logical human thought process.</I></A>
<P>
Eventually, AI researchers realized that the deterministic approaches
to AI weren't sufficient to accurately model human thought. Their
focus shifted from deterministic AI models to more realistic AI
models that attempted to factor in the subtle complexities of
human thought, such as best-guess decisions. In people, these
types of decisions can result from a combination of past experience,
personal bias, or the current state of emotion, in addition to
the completely logical decision making process. Figure 15.2 shows
an example of this type of thought process. The point is that
people don't always make scientifically predictable decisions
based on analyzing their surroundings and arriving at a logical
conclusion. The world would probably be a better place if we did
act like this, but again, it would be awfully boring!
<P>
<A HREF="f15-2.gif" ><B>Figure 15.2 : </B><I>A more realistic human thought process.</I></A>
<P>
The logic flow in Figure 15.1 is an ideal scenario where each
decision is made based on a totally objective logical evaluation
of the situation. Figure 15.2 shows a more realistic scenario,
which factors in the emotional state of the person, as well as
a financial angle (the question of whether the person has insurance).
Examining the second scenario from a completely logical angle,
it makes no sense for the person to throw the hammer, because
that only slows down the task at hand. However, this is a completely
plausible and fairly common human response to pain and frustration.
For an AI carpentry system to effectively model this situation,
there would definitely have to be some hammer throwing code in
there somewhere!
<P>
This hypothetical thought example is meant to give you a tiny
clue as to how many seemingly unrelated things go into forming
a human thought. Likewise, it only makes sense that it should
take an extremely complex AI system to effectively model human
thought. Most of the time this statement is true. However, the
word "effectively" allows for a certain degree of interpretation,
based on the context of the application requiring AI. For your
purposes, effective AI simply means AI that makes computer game
objects an engaging challenge.
<P>
More recent AI research has been focused at tackling problems
similar to the ones illustrated by the hypothetical carpentry
example. One particularly interesting area is fuzzy logic systems,
which attempt to make "best-guess" decisions, rather
than the concrete decisions of traditional AI systems.
<P>
A <I>fuzzy logic system</I> is an AI system that uses "best-guess"
evaluations to make decisions, which is more akin to how humans
make decisions.
<P>
Another interesting AI research area in relation to games is genetic
algorithms, which try to model evolved thought. A game using genetic
algorithms would theoretically have computer opponents that learn
as the game progresses, providing the human player with a seemingly
never ending series of challenges.
<P>
<I>Genetic algorithms</I> are algorithms that learn and evolve
in their ability to make decisions as they are run repeatedly.
<H2><A NAME="TypesofGameAI"><B><FONT SIZE=5 COLOR=#FF0000>Types
of Game AI</FONT></B></A></H2>
<P>
There are many different types of AI systems and even more specific
algorithms implementing those systems. Even when you limit AI
to the world of games, there is still a wide range of information
and options from which to choose when it comes to adding AI to
a game of your own. Many different AI solutions are geared toward
particular types of games, with a plethora of different possibilities
that can be applied in different situations.
<P>
What I'm getting at is that there is no way to just present a
bunch of AI algorithms and tell you which one goes with which
particular type of game. Rather, it makes more sense to give you
the theoretical background on a few of the most important types
of AI, and then let you figure out how they might apply to your
particular gaming needs. Having said all that, I've broken game-related
AI down into three fundamental types: roaming, behavioral, and
strategic.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
The three types of AI discussed here are in no way meant to encompass all the AI approaches used in games, they are simply the most common types I've seen. So, please feel free to do your own research and expand on these; some Web sites are included at the
end of today's lesson that contain very useful information about more advanced AI topics.</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<H3><A NAME="RoamingAI"><B>Roaming AI</B></A></H3>
<P>
<I>Roaming AI</I> refers to AI that models the movement of game
objects-that is, the decisions game objects make that determine
how they roam about the game world.
<P>
A good example of roaming AI is in shoot-em up space games, where
aliens often tend to track and go after the player. Similarly,
aliens that fly around in a predetermined pattern are also implemented
using roaming AI. Basically, roaming AI is used whenever a computer-controlled
object must make a decision to alter its current path, either
to achieve a desired result in the game or simply to conform to
a particular movement pattern. In the space shoot-em up example,
the desired result is colliding with and damaging the player's
ship.
<P>
Implementing roaming AI is usually very simple; it typically involves
altering an object's velocity or position (the alien) based on
the position of another object (the player's ship). The roaming
movement of the object can also be influenced by random or predetermined
pattern. There are three different types of roaming AI: chasing,
evading, and patterned.
<H4><B>Chasing</B></H4>
<P>
<I>Chasing</I> is a type of roaming AI in which a game object
tracks and goes after another game object or objects.
<P>
Chasing is the approach used in the space shoot-em up example,
where an alien is chasing the player's ship. It is implemented
simply by altering the alien's velocity or position based on the
current position of the player's ship. The following is a sample
Java implementation of a simple chasing algorithm:
<BLOCKQUOTE>
<TT>if (aX > pX)<BR>
aX--;<BR>
else if (aX < pX)<BR>
aX++;<BR>
if (aY > pY)<BR>
aY--;<BR>
else if (aY < cY)<BR>
aY++;</TT>
</BLOCKQUOTE>
<P>
As you can see, the X and Y position (<TT>aX</TT>
and <TT>aY</TT>) of the alien is altered
based on where the player is located (<TT>pX</TT>
and <TT>pY</TT>). The only potential
problem with this code is that it could work too well; the alien
will home in on the player with no hesitation, basically giving
the player no chance to dodge it. This might be what you want,
but more than likely, you want the alien to fly around a little
while it chases the player. You probably also want the chasing
to be a little imperfect, giving the player at least some chance
of out-maneuvering the alien.
<P>
One method of smoothing out the chasing algorithm is to throw
a little randomness into the calculation of the new position,
like this:
<BLOCKQUOTE>
<TT>if ((rand.nextInt() % 2) == 0) {<BR>
if (aX > pX)<BR>
aX--;<BR>
else if (aX < pX)<BR>
aX++;<BR>
}<BR>
if ((rand.nextInt() % 2) == 0) {<BR>
if (aY > pY)<BR>
aY--;<BR>
else if (aY < cY)<BR>
aY++;<BR>
}</TT>
</BLOCKQUOTE>
<P>
In this code, the alien has a one in three chance of tracking
the player in each direction. Even with only a one in three chance,
the alien will still tend to chase the player pretty effectively,
while allowing the player a fighting chance at getting out of
the way. You might think that a one in three chance doesn't sound
all that effective, but keep in mind that the alien only alters
its path to chase the player. A smart player will probably figure
this out and change directions frequently.
<P>
If you aren't too fired up about the random approach to leveling
off the chase, you probably need to look into patterned movement.
But you're getting a little ahead of yourself; let's take a look
at evading first.
<H4><B>Evading</B></H4>
<P>
<I>Evading</I> is the logical counterpart to chasing; it is another
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -