📄 article784.asp.htm
字号:
<!--<TABLE>
<TR>
<TD>
</TD>
<TD>
See Also:
</TD>
</TR>
<TR>
<TD COLSPAN=2>-->
<P ALIGN="center"><SPAN CLASS="title">A Practical Guide to Building a Complete Game AI: Volume I</SPAN>
<BR><SPAN CLASS="author">by Geoff Howland</SPAN></P>
<P>Artificial Intelligence (AI) in games has taken the backseat in development for a long time for many reasons but the future of games is definitely going to be weighted heavily with increasingly detailed game AI. If your game's AI is not up to the current level that game player's expectations demand then your game will feel dated and suffer for it in their opinions.</P>
<P>Game AI is not just neural networks and learning systems and complex mathematical structures, although it can be, but primarily game AI is about creating an environment and the appearance of thought from units. Game AI is behavioral, not scientific.</P>
<P>The key to understanding how to create game AI is understanding what you want your final results to be and then building the system to provide those results. It all comes down to what the player can see; if they can't tell it's happening, then it might as well not be.</P>
<P><I> The examples and discussion will be given based on the format of Real-Time Strategy (RTS) games, however some of these concepts can be translated into being appropriate for other genres as well. All data examples are done in standard C format.</I></P>
<H1>State Machines</H1>
<H2>Finite State Machine</H2>
<P>A finite state machine (FSM) is a system that has a limited number of states of operation. A real world example could be a light switch which is either on or off, or an alarm clock that is either idling by telling time, ringing an alarm or having its time or alarm set. Any system that has a limited number of possibilities where something can be defined by one state (even combinations) can be represented as a finite state machine.</P>
<P>Finite state machines are natural for any type of computer program and understanding how to use them effectively to create game AI is only as hard as understanding the system you are trying to represent, which is as detailed or simple as you make it.</P>
<H2>Using Finite State Machines</H2>
<P>There are many purposes for using FSMs in games but one of the more intricate ones you have to deal with is trying to model unit behavior since trying to simulate human beings is the toughest simulation there is. As guaranteed hard as it is to simulate human behavior there have been many stories of detailed game AI's that were mistaken for human players and vice versa by other players and spectators of the games, especially some detailed FSMs systems. </P>
<P>While some other systems are designed to more accurately model the way humans think and learn, sometimes you can just never beat the simplicity of having a choice, weighing the factors and deciding, as a human, which one you would make given that choice. When learning more about AI decision and learning systems always keep this in mind as often the best system for the job is the simplest and not the most scientifically accurate.</P>
<P>Don抰 misunderstand this as opposition to Neural Network, Genetic Algorithms or any other artificial intelligence systems, just don抰 mistake clever routines and interesting algorithms as being a better solution if they wont give better results. Weigh your choices based off what you need to get your end result, not the latest trends.</P>
<H2>Game State Machines</H2>
<P>Creating a believable environment for your game means that you need to consider as many detailed elements that the player might possibly focus their attention on as you can. The more of these you anticipate by planning and testing, the more immersive the environment will be for the player when they are discovering your creation.</P>
<P>In your total game state there will be at least two division of state machines that you will need to keep your game going. The first state machine will deal with the game interface, which includes whether the game is paused, if there are different modes the player can be looking at the world in, then which one, what things the player can and can't see and any other flags you might use for your particular interface.</P>
<P>The second state machine will deal with what is actually going on in the game, the current state of the environment, objects in the level, objectives completed or failed in the mission and all other variables that you use to guide and challenge the player.</P>
<P>You may have an alert system where the enemies will be actively patrolling if the player has been spotted or has fired shots, or flags for whether certain critical pieces have been destroyed or not. All of these items can be contained inside of a structure such as the example one below.</P>
<BLOCKQUOTE><PRE CLASS="code">
struct GameLevelState {
int alert; // Alert status of enemies //
struct Positionshot; // Position of last shot fired //
int shotTime; // Game cycle last shot was fired //
int hostage; // Hostage rescued //
int explosives; // Explosives set or not //
int tank; // Tank destroyed //
int dialogue; // Dialogue variable //
int complete; // Mission completed //
};
</PRE></BLOCKQUOTE>
<H2>Flexibility</H2>
<P>Keeping your AI flexible is extremely important. The more modular you make your routines, the more you will be able to expand them as you go on. Its important to understand that designing a game AI is very much an iterative process, you need to try things out and build upon them.</P>
<P>The goal in creating a good AI is to have units that react in situations that seem realistic in an environment they seem to be interacting with. If you box in what your units will be able to do too early it will be difficult to expand the breadth of their actions later on when you decide to augment the game world to feel more complete or interactive.</P>
<H1>Unit Actions</H1>
<P>In a game where the player controls units, it is all important to have meaningful and well organized information on them. Without this, adapting the units to the players will become difficult and the users interface with the game could suffer. If the player doesn抰 feel he is controlling the units and getting appropriate information back from them, then all he is doing is clicking around in an interface and all immersive aspects the game held will be lost. Meaning the player won't be having any fun and could be becoming frustrated. </P>
<H2>Anatomy 101</H2>
<P>To get a sense of what kind of information you may want to provide to the player with let's take a look at a sample data structure.</P>
<BLOCKQUOTE><PRE CLASS="code">
struct Character {
struct Positionpos; // Map position //
int screenX, screenY; // Screen position //
int animDir, animAction, animNum; // Animation information, action and animation frame number //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -