artificial intelligence - ai game designing (advanced)

Upload: atul-vijay

Post on 08-Apr-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    1/32

    Top of Form

    Designing Artificial Intelligence for Games (Part 1)Bottom of Form

    By Donald KehoeOver the course of the last few decades, the gaming industry has seen great strides. Beginningwith simple games like Pong* and Pac-Man* which offered players a short escape from realityand growing into such involved games like World of Warcraft* and Call of Duty 4* which areserious hobbies to those that play them. Todays gamers, who according to the EntertainmentSoftware Association (ESA) have an average of 13 years of gaming under their belt, have grownaccustomed to seeing each new game become increasingly complex, engaging, and intelligent.For developers, the challenge becomes pushing the envelope to create games that areincreasingly compelling. Computer-controlled Artificial Intelligence (AI) has evolved in manyforms to meet the test. However, creating an adaptive foil for the player that can match theirmoves and encourage player growth is no simple task. This article begins a four part series that

    explores the following important AI concepts and how to optimize them to run on todays cuttingedge multi-core processors:

    Part 1: Design & Implementation

    Part 2: Path Finding & Perceptions

    Part 3: Tactical & Strategic AI

    Part 4: Using Threading to Apply AI

    Part 1: Design and Implementation

    What Is AI for Games?

    At its most basic level, artificial intelligence consists of emulating the behavior of other

    players or the entities (that is, all the elements of the game that can act or be acted uponfromplayers to missiles to health pickups) they represent. The key concept is that the behavior issimulated. In other words, AI for games is more artificial and less intelligence. The systemcan be as simple as a rules-based system or as complex as a system designed to challenge aplayer as the commander of an opposing army.

    How AI for Games Differs from Traditional Views on AI

    Traditional research in AI seeks to create a real intelligencealbeit through artificial means.Projects such as the Massachusetts Institute of Technologys (MIT) Kismet* are trying to createan AI that can learn and interact socially as well as exhibit emotions. As of this writing, MIT isworking on creating an AI that has the faculties of a young child, with promising results.

    For the purposes of todays games, true AI is above and beyond the requirements of a piece ofentertainment software. Game AI does not need to be sentient or self-aware (in fact, it is best if itisnt); it does not have to learn about anything beyond the scope of gameplay. The real goal ofAI in games is to simulate intelligent behavior, providing the player with a believable challengea challenge that the player can then overcome.

    The Purpose of AI in Games

    AI can play multiple roles in gaming. It can be a general set of rules used to govern the behaviorof entities in the game world. You could also consider the pre-scripted events that entities follow

    http://www.ai.mit.edu/projects/humanoid-robotics-group/kismet/http://www.ai.mit.edu/projects/humanoid-robotics-group/kismet/
  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    2/32

    a type of AI. For example, in the gameF.E.A.R*, the creepy little girl who appears to frightenplayers and foreshadow future events is a pre-scripted event. What comes to mind for mostpeople when they think of AI and games is the computer-controlled players in multiplayergames. However, all of these are different roles that AI can fulfill.

    Figure 1: F.E.A.R.'s (Vivendi Universal*) use of scripted events is a type of AI

    Basic Necessities for AI in Games

    Depending on the nature of the role that the AI is supposed to fill, there can be very little in theway of system needs. The more complex the system, the more requirements an AI will have.Basic needs are nothing more than the processing time needed to run the AI. More complexsystems require some means of perceiving the AIs environment, a record of player actions, andsome means of evaluating the success of previous decisions.

    Decision-making

    The core concept behind AI is decision making. To execute these choices, the intelligent system

    needs to be able to affect the entities using the AI system. You can organize this execution ineither an AI push or an entity pull strategy.

    AI push systems tend to isolate the AI system as a separate element of the game architecture.Such a strategy often takes on the form a separate thread or threads in which the AI spends itstime calculating the best choices given the game options. When the AI makes a decision, thatdecision is then broadcast to the entities involved. This approach works best in real-time strategygames, where the AI is concerned with the big picture.

    Entity pull systems work best for games with simple entities. In these games, the entities call onthe AI system when the entity thinks, or updates itself. This approach works very well insystems with large numbers of entities that do not need to think very often, such as shooters.This system can also benefit from multi-threading techniques, but it requires some extra planning

    (for details see the article on Multithreaded AI by Orion Granatir.Basic Perceptions

    For the AI to make meaningful decisions, it needs some way of perceiving its environment. Insimpler systems, this perception can be a simple check on the position of the player entity. Assystems become more demanding, entities need to identify key features of the game world, suchas viable paths to walk through, cover-providing terrain, and areas of conflict.The challenge for designers and developers is to come up with a way to identify key featuresimportant to the intelligence system. For example, cover can be predetermined by the level

    http://software.intel.com/en-us/articles/two-brains-are-better-than-one/http://software.intel.com/en-us/articles/two-brains-are-better-than-one/
  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    3/32

    designers or can be pre-computed when a map is loaded or compiled. Some elements must beevaluated on the fly, such as conflict maps and imminent threats.

    Rules-Based Systems

    The most basic form an intelligent system can take is that of a rules-based system. This systemstretches the term artificial intelligence. A set of preset behaviors is used to determine the

    behavior of game entities. With a variety of actions, the overall result can be a behavior systemthat is not obvious although there is very little actual intelligence involved.A good example of a rules-based system is a Black Jack dealer (either video Black Jack or realBlack Jack). The dealer has a simple rule that it follows: Always hit when the cards add up to 17or less. To the average player, the perception is that the dealer is playing competitively. Theplayer will imagine a more competent adversary than the one he or she faces (unless the houseadvertises the rule that the dealers play by).

    The classic application of this system is Pac-Man. Four ghosts plagued the player. Each ghostfollowed a simple rule set. One ghost was always to turn left, another was always to turn right,one turned in a random direction, and the last turned toward the player. Individually, the ghostswould be easy to figure out, and the player would be able to handily avoid them. As a group, the

    pattern of their movement appears to be a complex, coordinated search party hunting the player.In reality, the only one that even checks the player's position is the last one.

    Figure 2: Visual representation of the rule set governing Pac-Man ghosts, where arrowsrepresent the decisions that will be made.

    As this example suggests, rules do not need to be hard-coded: They can be based on perceivedstates (as the last ghost was) or on editable parameters of the entity. Variables such asaggression, courage, range of sight, and rate of thinking can all lead to more diverse entitybehavior, even within a rules-based system. Rules-based systems are the simplest structure for anAI. More complex intelligent systems are built upon and governed by a series of conditional

    rules. In tactical games, rules govern which tactics to use. In strategy games, rules govern buildorders and how to react to conflicts. Rules-based systems are the foundation of AI.

    Finite State Machines as AI

    A finite state machine (FMS) is a way of conceptualizing and implementing an entity that hasdistinct states throughout its life. A statecan represent physical conditions that the entity is in,or it can represent emotional states that the entity can exhibit. In this example, emotional statesare nothing like a true AIs emotional states but predetermined behavior models that fit into thecontext of the game.

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    4/32

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    5/32

    Dead. In some games, the state of death may not be completely idle. Death or dying canhave the entity cry out, alerting nearby entities, or go into a knocked-out state, where itcan later be revived by a medic (and returned to a state of Alert).

    There are at least two simple ways to implement an FMS within the entity system. One is to haveeach state be a variable that can be checked (often through a massive switch statement). The

    other is to use function pointers (in the C language) or virtual functions (in the C++ and otherobject-oriented languages).

    Adaptive AI

    The previous sections discussed methods for designing intelligence systems that fit into thepredefined events of a game. For most games, this is adequate as long as the designs werethorough and there is a clear understanding of the goals of the intelligent entities. When a gamecalls for more variability and a better, more dynamic adversary for the player, the AI may needto be able to grow and adapt on its own.

    Adaptive AI is used commonly in fighting games and strategy games, in which the mechanicsare deep and the options for gameplay are innumerable. To provide a constant challenge for theplayer without the player eventually figuring out the optimal strategy to defeat the computer, theAI needs to be able to learn and adapt.

    Prediction

    The ability to effectively anticipate an opponents next move is crucial in an adaptive system.Different methods can be used, such as past-pattern recognition (covered in a future article) orrandom guess, to determine the next action to take.

    One basic method for adaptation is to keep track of past decisions and evaluate their success. TheAI system keeps a record of choices a player has made in the past. Past decisions must beevaluated in some manner. (e.g. in fighting games, the advantage gained or losthealth lost ortime advantagecan be the measure for success.) Additional information about the situation canbe gathered to give the decisions some context, such as relative health, previous actions, and

    position in the level (people play differently when their backs are to the wall).

    This history can be evaluated to determine the success of previous actions and whether a changein tactics is required. Until the list of past actions is built, general tactics or random actions canbe used to guide the actions of the entity. This system can tie into rules-based systems anddifferent states.

    In a tactical game, past history can decide the best tactics to use against a player team, such asdefensive, offensive, berserk, or some balanced means of play. In a strategy game, the optimalcomposition of units in an army can be discovered on a per-player basis. In games where the AIis controlling supportive characters for the player, the adaptive AI can better complement theplayer's natural style by learning the way the player acts.

    LoginTop of Form

    true

    Login ID:

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    6/32

    Password:

    Remember Me?

    Login

    Bottom of Form

    New Registration?Forgot Login ID?Forgot Password?

    You are not logged-in Login/Register

    Home Articles

    Top of Form

    /en-us/articles/de

    Bottom of Form

    Top of Form

    20|334 3

    Designing Artificial Intelligence for Games (Part 2)Perceptions and Path Finding

    In the last article (part 1), I discussed ways to govern the basic decisions that an intelligent agent-as artificial intelligence (AI) research refers to entities that use AI-may make. In this article, Igive our hero (or monster or any type of game entity) some context to the decisions that will bemade. Intelligent agents need to identify points of interest in the game world, then figure out howto get there. Finally, this article shows how to optimize these methods and provides ways oforganizing them to account for multithreading.

    This article gets dangerously close to real artificial intelligence (AI). All intelligent agents need

    to have a basic ability to perceive their environment and some means of navigating and movingwithin the world around them-be it real or otherwise. Your entities will need to do the same,although with a much different approach. You can also cheat-which you will to make sureeverything runs nice and fast.

    How AI Perceives

    Having your agents make arbitrary decisions is fine for some games, but what if you need more?If your agent is going to make decent decisions, it is going to need to know what's going on

    https://ssl.software.intel.com/en-us/register/visual-adrenaline/https://welcome.intel.com/forgot_loginid.aspx?Lang=ENGhttps://welcome.intel.com/forgot_password.aspx?Lang=ENGhttp://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/http://software.intel.com/en-us/articles/all/1/http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-1/https://ssl.software.intel.com/en-us/register/visual-adrenaline/https://welcome.intel.com/forgot_loginid.aspx?Lang=ENGhttps://welcome.intel.com/forgot_password.aspx?Lang=ENGhttp://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/http://software.intel.com/en-us/articles/all/1/http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-1/
  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    7/32

    around it. In the robotic application of AI, a lot of research is being done on computer vision,giving robots the ability to perceive the world around them in true, stereo three-dimensional (3D)vision, just like humans do. That level of sophistication is complete overkill for our purposes.

    The virtual worlds in which most games take place have a huge advantage over real-world AIrobots. Everything in our world is a known quantity: There is a list somewhere in the game witheverything that exists in it. You can just search that list for any criteria, and then immediatelyhave information that your agent can use to make more meaningful decisions.

    Sight

    Faking entity sight is the most basic level of giving an agent perceptive ability. You can do thisby searching your list of entities for anything within a set range. You can either get the first thingthat interests your agent or you can get a list of things in range so that your agent can make theoptimal decision about its surroundings.

    This setup works well for simple games, but when you have a more complex style of game-such

    as a spying game or a tactical first-person shooter (FPS)-your agents will need to be a bit moreselective in what they "see." If you don't want your agents to have eyes in the back of theirheads, you can cull the list of potentials of anything outside the entity's sight range. You can dothis quickly with a bit of math:

    1. Calculate the vector between the agent and the entity in question by subtracting theposition of the target from the agent's position.

    2. Calculate the angle between that vector and the direction in which your agent is looking.(If it's not already a vector, you can calculate that value, too.)

    3. If the absolute value of the angle is greater than the agent's preset view angle, your agentdoes not see the entity.

    In more complex games, you may need to account for the player or other entities being hidden bysome sort of cover. For this type of game, you may need to perform a ray trace (sometimesreferred to as a ray cast) to see whether something has blocked the potential target. A ray trace isa mathematical way of checking if a ray intersects anything, starting from a single point andgoing in a set direction. The game engine provides ray tracing functionality, but if you want tosee how it's done, check out Two Brains Are Better Than One.

    The previous method tells you whether something has obscured the center of the target, but itmight not be enough to deter your agent. It's possible that the center of the agent is obscured butits head is poking out conveniently above the cover. Using multiple ray traces to specific pointsof interest on the target may help determine not only ifthe target can be hit but where the targetcan be hit.

    Sound

    At first blush, it may seem like sound is no different than sight. If you can see an entity, certainlyyou can hear it, too. It's true that if your agent has spotted an entity, the agent can actively detectanything that entity does until it is no longer in sight. However, adding an extra level of hearingto your agents can help make sight work more effectively. Tracking the noise that entities makeas a level of perception is key to any stealth-based game.

    http://software.intel.com/en-us/articles/two-brains-are-better-than-one/http://software.intel.com/en-us/articles/two-brains-are-better-than-one/
  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    8/32

    Just as in sight, you're going to need to get a list of nearby entities to check against. You can dothis again with a simple distance check, but what you do to cull this list is different.

    Every action that an entity can perform needs to have some sound levels associated with it. Youcan preset sound levels (to optimize game balance) or base them on the actual energy of thesound effects being played for the action (nice for realism but unnecessary). If the sound beinggenerated is within a threshold, your agent will perceive that entity.

    If you want to account for obstacles, you can once again cull this list by performing ray traceswith your environment to see whether anything would be in the way of the sound. Because veryfew materials are completely soundproof, you need to be more creative in how you removeentities from your list.

    Other Senses

    The basic functionality needed to give your agents sight and hearing can easily be applied to

    simulate other senses. Here is a list of other potential senses that you can add to a game if thedesigns call for it:

    Smell. The idea of having intelligent agents tracking players by smell has been added tosuch recent games as Call of Duty 4*. Adding the sense of smell to a game is relativelyeasy: Give each entity in the game a distinct smell number and strength. The strength ofthe smell determines two factors: the radius of the smell and the size of the scent trail leftbehind. Active player entities often keep track of their last few positions for a number ofreasons (more on trails later in this article). One reason could be to help entities withsmell. As the player entity updates the trail, the strength of the smell diminishes as thetrail grows "cold." When an agent with smell is updated, it needs to check for smells likeit would check for sound: radius and check for walls. With smell, the factor for success is

    then based on the power of the smell and the agent's sense of smell, which are checkedagainst the entity and the entity's trail.

    Radar. Some games give players individual radar, which makes sensing even easier. Allthat is required is a simple radius check. The AI can then verify the results for interest.For team games, the radar itself can become more interesting. To put together a team-based AI, each team needs a radar list of entities that have been found by radar. Eachmember of the team can then perform the radius check against just the list of knownentities to determine whether the team should react. The team can add to the list by usingradar equipment (such as in games like Enemy Territory: Quake Wars*) and by eachteam member adding anything that is "seen." This behavior helps entities working as aunit, as each alerts others of what it sees.

    Touch. This sense is kind of a "gimme," as the collision system of the game enginecovers it automatically. You just need to make sure that your intelligent agents react todamage and collision events.

    Taste. I'm not sure how this one would work. It would probably be a property of smellbut would require agents to actively "taste" things that they find.

    Being able to sense the world around us is all well and good, but what is it that the agents aresupposed to be sensing? You need to specify and be able to identify things that can be observed

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    9/32

    through your agents' settings. When you recognize what it is you're seeing, your agents can reactto it based on the rules that govern the entity.

    Temporary Entities

    Sometimes called particles, decals, orspecial effects, temporary entities are visual effects in thegame world. They are similar to entities in that one overall class structure defines any potentialtemporary entity, but they are distinct from entities in that they do not think, react to, or interactwith the game world's entities or each other. Their sole purpose is to look pretty, providingadditional detail to the world for a while, then die. Temporary entities are used for things likebullet trails, smoke, sparks, blood spurts, and even footprints.

    The nature of temporary entities implies very little processing and no collision detection (beyondvery simple world collisions). The problem is that some temporary entities give players a visualclue about things that may have transpired (bullet holes and burn marks indicate a recent battle,footprints in the snow can lead to a potential target), so why can't your intelligent agents use that,too?

    There are two ways to solve this problem: You can either enhance your temporary entity systemto allow for ray tracing (which would break the whole point of a temporary entity system), oryou can drop an empty entity in the general vicinity of your temporary entities. This blank entitywould have no ability to think and no graphic associated with it, but your agents would be able todetect it, and the temporary entity would have associated information to provide "intel" to youragent. So, when you drop a pool of blood decals on the floor, you can also drop an invisibleentity there to let your agents know something is up. For the problem of footprints, you alreadyhave a trail to cover that. Cover

    In many shooting-based games, it would be nice if your agents could be smart enough to duckbehind cover when it is available instead of just standing out in the open, getting shot at. Thisproblem is a bit more specialized than other problems that I have been covering so far. So, howdo your agents determine whether there is any viable cover to duck behind?

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    10/32

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    11/32

    Here, your agent has determined that the green star is a safe spot to hide from damage.

    AI Navigation

    So far, I've talked a lot about how AI makes decisions and how it can know what's going on (inorder to make better decisions). Now, let's look at how your AI can carry out those decisions.The next step is to figure out how to get from point A to point B. You can use several different

    approaches depending on the nature of the game and level of demand for performance.

    Crash and Turn

    Crash and turn is one of the most basic ways of generating movement for an entity. Here is howit works:

    1. Move in the direction of your target.

    2. If you hit a wall, turn the direction that puts you closest to the target. If no choice isobviously better, pick one at random.

    This approach works fairly well for simple games. More games than I can count have used this

    method to govern how monsters hunt the player. Crash and turn results in entities getting stuckbehind concave walls or corners as they hunt the player, so for games with zombies or withoutthat land feature, this method is ideal.

    If the game requires a bit more finesse from its agents, however, you can expand upon the simplecrash and turn and give your agents some memory. If the agents can keep track of where theyhave been already, they can begin to make more meaningful decisions about how to turn. When

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    12/32

    all turns have been exhausted, your agents can backtrack and make different choices. Thus, youragent will make a systematic search for a path to a target. Here is how it works:

    1. Move toward the target.

    2. Make a choice when presented with a fork.

    3. When dead ends are discovered, backtrack to the last choice and make another choice.4. If all possible paths are explored, give up.

    This method has the benefit of being lightweight in the processing category, which means thatyou can support large numbers of these guys without slowing the game down. This method canalso gain an optimal benefit from multithreading. The drawback is a huge waste of space, as eachagent can potentially keep track of an entire map of possible paths.

    Fortunately, you can avoid this waste by having your agents keep track of paths in a sharedmemory. A problem can then arise with threading conflicts, so keep your entity paths in aseparate module that all agents can then send requests to as they move and send updates to asthey discover new paths. The Path Map Module can then parse the findings to avoid conflict.

    Path Finding

    Crash and turn-generated path maps are a great way to adapt to changing maps. In strategygames, players cannot wait around for their units to find their bearings. Also, these path mapscan become gigantic, and searching through them for the correct path becomes the bottleneck.Path finding to the rescue.

    Path findingis essentially a solved problem in game development. Games as old as the originalStarcraft* (Blizzard Entertainment*) handled large numbers of game entities that were all able tofind their way in large, intricate maps.

    The core algorithm for path finding has been 'A*' (pronounced Eh-Star), which can be used todiscover an optimal path from any two points in a graph (in this case, the map). A simple onlinesearch will discover a clean algorithm that uses such descriptive terms as F, G, and H. Allow meto explain it more plainly.

    First, you must set up two lists: a list of nodes that have not been checked (Unchecked) and a listof nodes that have been checked (Checked). Each list is composed of a position node, theestimated distance to the goal, and a reference to its parent (the node that put it in the list). Thelists are initially empty.

    Next, add your starting position to the Unchecked list and nothing as the parent. Then, you enter

    the algorithm: Select the best-looking node from the list.

    If this node is the goal, you're done.

    If this node is not the goal, add it to the Checked list.

    For each node adjacent to this node:

    If the node is non-walkable, ignore it.

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    13/32

    If the node is already in a list (Checked or Unchecked), ignore it.

    All else, add it to the Unchecked list, setting this node as the parent and

    estimating the distance to the goal (a raw distance check is good enough).

    When entity reaches the goal tile, the path can be constructed by tracing the parents back to thenode that does not have a parent (the starting node). Doing so provides an optimal path that theentity can then follow. Because this process needs to occur only when an agent is either given anorder or decides on its own to move, it can really benefit from multithreading. An agent can senda request to the path thread get back a path when it has been discovered without affecting theperformance of the AI. For the most part, the system can get results quickly; but in the case oflarge loads of path requests, the agent can either wait around or just start heading in the rightdirection (maybe it can use the crash and turn method). In extremely large maps, the system canbe broken down into regions, and all possible paths between the regions (orway points) can bepre-computed. In that case, the path finder can just look up the best path and return the resultsimmediately. The path map thread can just keep a watch on changes in the map-such as when aplayer builds a wall-then re-run the path checks as needed. With it being in its own thread, thealgorithm can adapt without affecting the performance of the rest of the game.

    Within the path finding subsystem, multithreading can also improve the performance of thesystem. This is well utilized in any real-time strategy (RTS) game or a system with a largenumber of entities each seeking a potentially unique path. Multiple paths can be foundsimultaneously within different threads. Of course, the system will need to keep track of whichpaths are being discovered. The same path does not need to be discovered more than once.

    Code Sample

    Here is a sample of A* implemented simply in C. I've left out support functions in the sample forsimplicity's sake, and because they need to be tailored for different implementation styles. Thesample is based on a simple tile grid where each tile can either be walked on or not. This onlyallows for movement into an adjacent tile, but with minor modifications can be adjusted to allow

    diagonal movement or even a hex style game layout.

    - collapse sourceview plaincopy to clipboardprint?

    1. /*Get Path will return -1 on failure or a number on distance to path

    2. if a path is found, the array pointed to by path will be set with the path in Points*/

    3. int GetPath(int sx,int sy,int gx,int gy,int team,Point *path,int pathlen)

    4. {

    5. int u,i,p;

    6. memset(&Checked,0,sizeof(Checked));

    7. memset(&Unchecked,0,sizeof(Unchecked));8. Unchecked[0].s.x = sx;

    9. Unchecked[0].s.y = sy;

    10. Unchecked[0].d = abs(sx - gx) + abs(sy - gy);

    11. Unchecked[0].p.x = -1;

    12. Unchecked[0].p.y = -1;

    http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)
  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    14/32

    13. Unchecked[0].used = 1;

    14. Unchecked[0].steps = 0;

    15.

    /*Get Path will return -1 on failure or a number on distance to path

    if a path is found, the array pointed to by path will be set with the path in Points*/int GetPath(int sx,int sy,int gx,int gy,int team,Point *path,int pathlen){int u,i,p;memset(&Checked,0,sizeof(Checked));memset(&Unchecked,0,sizeof(Unchecked));Unchecked[0].s.x = sx;Unchecked[0].s.y = sy;Unchecked[0].d = abs(sx - gx) + abs(sy - gy);Unchecked[0].p.x = -1;Unchecked[0].p.y = -1;Unchecked[0].used = 1;

    Unchecked[0].steps = 0;

    The above section handles initialization of the checked and unchecked list and puts the startingnode into the unchecked list. With this set, the rest of the algorithm can be run in loop.

    - collapse sourceview plaincopy to clipboardprint?

    1. do

    2. {

    3. u = GetBestUnchecked();

    4. /*add */5. AddtoList(Checked,Unchecked[u]);

    6. if((Unchecked[u].s.x == gx)&&(Unchecked[u].s.y == gy))

    7. {

    8. break;

    9. }

    10.

    do{

    u = GetBestUnchecked();/*add */AddtoList(Checked,Unchecked[u]);if((Unchecked[u].s.x == gx)&&(Unchecked[u].s.y == gy)){break;

    }

    http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)
  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    15/32

    The above section takes a look at the node from the Unchecked list that is closest to the goal.GetBestUnchecked() is a function that checks each node's estimate distance to the goal. If the tileis the goal, the loop breaks and the process is complete.

    How the distance is calculated is seen below by taking the estimated distance to the goal in boththe X and Y directions and adding them together. On first thought, you may be tempted to usethe Pythagorean theorem (a^2 + b^2 = c^2 ), but it's completely unnecessary. You only need arelative distance, not an exact distance. Processors can handle adding and subtracting many timesfaster than they can handle multiplication, which in turn, is much faster than division. Becausethis section of the code will be run many times a frame, optimization is key.

    - collapse sourceview plaincopy to clipboardprint?

    1. /*tile to the left*/

    2. if((Unchecked[u].s.x - 1) >= 0)/*first, make sure we're on the map*/

    3. {

    4. if((IsInList(Unchecked,Unchecked[u].s.x - 1,Unchecked[u].s.y,NULL) == 0)&&(IsList(Checked,Unchecked[u].s.x - 1,Unchecked[u].s.y,NULL) == 0))

    5. /*make sure we don't repeat a search*/

    6. {

    7. if(TileValid(Unchecked[u].s.x - 1,Unchecked[u].s.y,team))

    8. NewtoList(Unchecked,Unchecked[u].s.x - 1,Unchecked[u].s.y, UncheckedUnchecked[u].s.y, abs((Unchecked[u].s.x - 1) - gx) + abs(Unchecked[u].s.y - gy), Unchecked[u].steps + 1);

    9. }

    10. }

    11.

    /*tile to the left*/if((Unchecked[u].s.x - 1) >= 0)/*first, make sure we're on the map*/{if((IsInList(Unchecked,Unchecked[u].s.x - 1,Unchecked[u].s.y,NULL) ==

    0)&&(IsInList(Checked,Unchecked[u].s.x - 1,Unchecked[u].s.y,NULL) == 0))/*make sure we don't repeat a search*/

    {if(TileValid(Unchecked[u].s.x - 1,Unchecked[u].s.y,team))

    NewtoList(Unchecked,Unchecked[u].s.x - 1,Unchecked[u].s.y, Unchecked[u].s.x,Unchecked[u].s.y, abs((Unchecked[u].s.x - 1) - gx) + abs(Unchecked[u].s.y - gy),Unchecked[u].steps + 1);

    }}

    In the section above, the function looks at the tile to the left of the current node. If it is not

    http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)
  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    16/32

    already in the list as Checked or Unchecked, then it will attempt to add it to the list. TileValid()is another function that needs to be tailored to fit your game. If it passes the TileValid() checkthen it will call NewToList(), which adds a new tile to the unchecked list. The following threesections carry out the same process, but cover the other directions: right, up, and down.

    - collapse sourceview plaincopy to clipboardprint?

    1. /*tile to the right*/

    2. if((Unchecked[u].s.x + 1) < WIDTH)/*first, make sure we're on the map*/

    3. {

    4. if((IsInList(Unchecked,Unchecked[u].s.x + 1,Unchecked[u].s.y,NULL) == 0)&&(InList(Checked,Unchecked[u].s.x + 1,Unchecked[u].s.y,NULL) == 0))

    5. /*make sure we don't repeat a search*/

    6. {

    7. if(TileValid(Unchecked[u].s.x + 1,Unchecked[u].s.y,team))

    8. NewtoList(Unchecked,Unchecked[u].s.x + 1,Unchecked[u].s.y, UncheckeUnchecked[u].s.y, abs((Unchecked[u].s.x + 1) - gx) + abs(Unchecked[u].s.y - gy), Unchecked[u].steps + 1);

    9. }

    10. }

    11. /*tile below*/

    12. if((Unchecked[u].s.y + 1) < HEIGHT)/*first, make sure we're on the map*/

    13. {

    14. if((IsInList(Unchecked,Unchecked[u].s.x ,Unchecked[u].s.y + 1,NULL) == 0)&&(nList(Checked,Unchecked[u].s.x,Unchecked[u].s.y + 1,NULL) == 0))

    15. /*make sure we don't repeat a search*/

    16. {

    17. if(TileValid(Unchecked[u].s.x,Unchecked[u].s.y + 1,team))

    18. NewtoList(Unchecked,Unchecked[u].s.x,Unchecked[u].s.y + 1, UncheckeUnchecked[u].s.y, abs(Unchecked[u].s.x - gx) + abs((Unchecked[u].s.y + 1) - gy), Unchecked[u].steps + 1);

    19. }

    20. }

    21. /*tile above*/

    22. if((Unchecked[u].s.y - 1) >= 0)/*first, make sure we're on the map*/

    23. {

    24. if((IsInList(Unchecked,Unchecked[u].s.x ,Unchecked[u].s.y - 1,NULL) == 0)&&(InList(Checked,Unchecked[u].s.x,Unchecked[u].s.y - 1,NULL) == 0))

    25. /*make sure we don't repeat a search*/

    26. {

    http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)
  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    17/32

    27. if(TileValid(Unchecked[u].s.x,Unchecked[u].s.y - 1,team))

    28. NewtoList(Unchecked,Unchecked[u].s.x,Unchecked[u].s.y - 1, UncheckedUnchecked[u].s.y, abs(Unchecked[u].s.x - gx) + abs((Unchecked[u].s.y - 1) - gy), Unchecked[u].steps + 1);

    29. }

    30. }

    31. memset(&Unchecked[u],0,sizeof(PNode));

    32.

    /*tile to the right*/if((Unchecked[u].s.x + 1) < WIDTH)/*first, make sure we're on the map*/{if((IsInList(Unchecked,Unchecked[u].s.x + 1,Unchecked[u].s.y,NULL) ==

    0)&&(IsInList(Checked,Unchecked[u].s.x + 1,Unchecked[u].s.y,NULL) == 0))/*make sure we don't repeat a search*/

    {

    if(TileValid(Unchecked[u].s.x + 1,Unchecked[u].s.y,team))NewtoList(Unchecked,Unchecked[u].s.x + 1,Unchecked[u].s.y, Unchecked[u].s.x,

    Unchecked[u].s.y, abs((Unchecked[u].s.x + 1) - gx) + abs(Unchecked[u].s.y - gy),Unchecked[u].steps + 1);

    }}/*tile below*/if((Unchecked[u].s.y + 1) < HEIGHT)/*first, make sure we're on the map*/{if((IsInList(Unchecked,Unchecked[u].s.x ,Unchecked[u].s.y + 1,NULL) ==

    0)&&(IsInList(Checked,Unchecked[u].s.x,Unchecked[u].s.y + 1,NULL) == 0))

    /*make sure we don't repeat a search*/{if(TileValid(Unchecked[u].s.x,Unchecked[u].s.y + 1,team))NewtoList(Unchecked,Unchecked[u].s.x,Unchecked[u].s.y + 1, Unchecked[u].s.x,

    Unchecked[u].s.y, abs(Unchecked[u].s.x - gx) + abs((Unchecked[u].s.y + 1) - gy),Unchecked[u].steps + 1);

    }}/*tile above*/if((Unchecked[u].s.y - 1) >= 0)/*first, make sure we're on the map*/{if((IsInList(Unchecked,Unchecked[u].s.x ,Unchecked[u].s.y - 1,NULL) ==

    0)&&(IsInList(Checked,Unchecked[u].s.x,Unchecked[u].s.y - 1,NULL) == 0))/*make sure we don't repeat a search*/

    {if(TileValid(Unchecked[u].s.x,Unchecked[u].s.y - 1,team))NewtoList(Unchecked,Unchecked[u].s.x,Unchecked[u].s.y - 1, Unchecked[u].s.x,

    Unchecked[u].s.y, abs(Unchecked[u].s.x - gx) + abs((Unchecked[u].s.y - 1) - gy),Unchecked[u].steps + 1);

    }

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    18/32

    }memset(&Unchecked[u],0,sizeof(PNode));

    The last thing to do in this iteration is to remove the current node from the unchecked list. There

    is no need to look at this tile again.- collapse sourceview plaincopy to clipboardprint?

    1. }

    2. while(1);

    3.

    }while(1);

    This final piece of code builds the path from the checked list by back-tracking from the goal. Theway back to the starting location can always be found, because each node in the path keeps trackof the node that put it in the list. The final path is then returned (through reference). The functionreturns the length of the new path.

    - collapse sourceview plaincopy to clipboardprint?

    1. IsInList(Checked,Unchecked[u].s.x,Unchecked[u].s.y,&u);

    2. p = Checked[u].steps;

    3. if(path != NULL)

    4. {

    5. for(i = (p - 1);i >= 0;i--)

    6. {

    7. path[i].x = Checked[u].s.x;

    8. path[i].y = Checked[u].s.y;

    9. IsInList(Checked,Checked[u].p.x,Checked[u].p.y,&u);

    10. }

    11. }

    12. return p;

    13. }

    14.

    IsInList(Checked,Unchecked[u].s.x,Unchecked[u].s.y,&u);p = Checked[u].steps;if(path != NULL){for(i = (p - 1);i >= 0;i--){path[i].x = Checked[u].s.x;

    http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/?wapkw=(Designing+Artificial+Intelligence+for+Games+Part+2)
  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    19/32

    path[i].y = Checked[u].s.y;IsInList(Checked,Checked[u].p.x,Checked[u].p.y,&u);

    }}return p;

    }

    Designing Artificial Intelligence for Games (Part 3)Tactical and Strategic AI

    When last we left out heroes (intelligent agents, or IAs), they had just been given the ability tosee what is around them and to figure out where they are going. In this article, I show you how togive your agents higher orders of intelligence. The agents can already deal with the immediatesituation they find themselves in: Now, you are working toward artificial intelligence (AI) thatdeals with broader goals and the bigger picture of whats going on.

    Tactical AI

    The role of tactical AI is to coordinate the efforts of groups of IAs in the game. Theimplementation of this type of AI is important for many styles of game: Squads in a tactical first-person shooter (FPS) game as well as groups of units in a real-time strategy game all use tacticalmethods. Groups are more effective, because they can support each other and act a single unit,all sharing information and the load of acquiring the information.

    The concept oftactical AIis built around group dynamics, which requires the game to keep trackof different groups of entities. Each group needs to be updated separately from the individuals.You can handle this updating with a dedicated update module that keeps track of differentgroups, their goals, and their composition. Because this method requires development of a

    separate system for the engine, however, I prefer to use the group captain method.A single unit of the group can be assigned the role of group captain. Every other member of thegroup keeps a link to this captain, and they get their behavioral cues from checking the orders ofthe group captain. The group captain handles all the tactical AI calculations for the whole group.

    Group Movement: Path Finding

    Entity movement is an area of implementation that can be improved with group dynamics.Movement can be made more efficient as well as more life-like when IAs function as a unit.

    Path finding can be a time-consuming process, even when sped up with pre-computed path mapsand multi-threaded AI. The stress put on the path-finding system can be greatly lessened withgroup dynamics.

    When a group of units is given a goal to move (either through the player or AI instructions), theunit closest to the goal is set as the group captain, and all other members are assigned to followthat captain. When the group captain is updated, it queries the path system. With path in hand,the group captain sets out to reach the goal. All the other units in the group merely have to followtheir group captain wherever he (or she or it) goes.

    Group Movement: Formations

    http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-2/
  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    20/32

    With your group moving as a unit, you have managed to reduce the load that your pathingsystem has to bear. Unfortunately, the way the units end up moving is more like a mob than anorganized unit. Enterformations. With formations, the group can move in a nice pattern such asa phalanx (in a historical simulation) or a triangle (how the minions work in CodemastersOverlord*, shown in Figure 1).

    2009 Codemasters*Figure 1. In Overlord, the minions (the red guys) work as a team and move in formation at thecommand of the player (the guy in armor).

    The setup for formations is really quite easy, and it extends the concept of the group captain. In aformation, every unit has a specific role that must be played. When formed, each member of thegroup will be assigned a spot in the formation, just like a single unit is assigned the role of groupcaptain. It is the goal of every unit in the group to keep its position a relative distance away fromother members of the formation.

    Take the example of the minions in Overlord. They move in the pyramid formation. In Figure 2,the group captain C only has to follow the path. Unit 1 will follow a set pace behind and a littleto the left of its target unit, C. Unit 2 stays aware of and keeps in step with unit 1, just off tothe side. Unit 3 does exactly what unit 1 does but follows unit 1 instead of the captain. Thispattern is followed for every unit in the group.

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    21/32

    Figure 2. A setup for a triangle movement formation

    Group Tactics

    There is more to tactics than merely marching in formation. There is also supporting and fightingas a team. The captain assumes the burden of planning and coordinating the team. After all, thelives of the captains unit are the responsibility of the commanding officer.

    The implementation of group tactics can be governed by the systems discussed in previousarticles, such as rules-based systems or finite state machines (FSMs). Here are some examples oftypical group behaviors (see Figure 3):

    Healing support. In games with healing support units (medics or clerics), it is importantfor the medics to keep aware of the health levels of the units in their group. This can becompounded with the intentions of the group captain. Medics can be ordered to stay nearunits that are likely to come under fire.

    Scouting. In games like Enemy Territory Quake Wars*, there are units that help providefor the rest of the team. The spy classes in the game can deploy radar to provideinformation on the movements of enemy units. Even without radar, any enemies spottedby one unit will be added to the group radar of the whole team. When approaching areaswith no coverage, its possible to send someone into an unknown area to scout for enemypositions. This is made easier for any spy classes in disguise.

    Covering fire. When an area is well defended, supporting units can provide covering fireand converge on the enemy defenses, engaging the defenses (hopefully) long enough forthe goal to be reached.

    Sacrifice. When under heavy attack and loss of units is inevitable, some units in the

    group may be considered expendable. The units in the group that are mission critical (likethe engineer, in some situations) need to be protected, both by prioritizing enemy unitsthat target your engineer and by getting in the line of fire to protect the engineer with theunit's life.

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    22/32

    Figure 3. In Enemy Territory Quake Wars by Id Software* and Splash Damage, Ltd.*, there arefive classes with distinct roles to play in the group dynamic.

    Another level of analysis that may help the group is a self-analysis of each member of thegroups capabilities. It is important for the captain to know which situations the group can beeffective in, when the group will have an advantage, and when the group should retreat.

    For example, in a strategy game like Blizzard's Starcraft*, there are flying units and groundunits. Not every ground unit can hit a flying unit. It is important for any group to know if it hasthis capability. If the group does not have any units that can hit a flying unit, then when a flyingunit is encountered, it would be best to run away. Even if the unit does not have that many unitsthat can hit flying units, as long as there are support units that can heal or improve the units that

    can hit the flying units, it would probably be best to stay and fight.Diversity of entity abilities and the numbers of units that have them can be used to weigh thecombat effectiveness of the group in different situations. Groups that take this into account willbe far more effective than groups that do not.

    Strategic AI

    So far, Ive covered how entities and groups of entities can handle themselves in toughsituations. Now, I look at the AI concerned with the big picture. Strategic AI is the higher-orderAI that commands the whole army and deals with the guiding strategies.

    The strategic AI itself is most common in real-time strategy (RTS) games but has been making

    its way more and more into tactical FPS games. The player-controlled commander can be its ownsystem or set up as an empty entityone that does not have a place or graphic in the world but isupdated and thinks.

    The commanders are going to be guided by hierarchical rules systems and FSMs, which governelements such as resource gathering, researching up the tech tree, building the army, and so on.For the most part, this basic upkeep of the game elements does not require much actual thought.What does require intelligence is the interactions with the other players.Governing these interactions (or warfare) is where the real meat and potatoes lie with strategic

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    23/32

    AI. The commander needs to explore the game map to find the player, identify key points ofinterest such as choke points, build defenses, and analyze other player's defenses. How onewould do this is not obvious but can be made easier with decision maps.

    Decision Maps

    Decision maps are two-dimensional arrays approximating the game map. Each cell in the array

    corresponds to a region in the game and is filled with important information about the region.These maps are used to help your strategic AI make meaningful decisions about the game as awhole.

    Resource Maps

    Resource maps contain information about where the resources are laid out in a strategy game.Knowing where on the maps resources are concentrated can influence many decisions that acommander will make. Where to deploy expansions or satellite bases (resources near thecommanders base), where the enemy is likely to deploy its expansions (resources near theirbase) and places likely to be contested (resources in the middle).

    Getting a count of potential available resources also influences which units are supported and

    how to deploy the army. If resources are scarce, each unit must be used more carefully, becausereplacements are less likely. If resources are plentiful, it opens up the strategies of massing cheapunits or building powerful units.

    Objective Maps

    These maps are filled with information about the goals of the commanderfor example,locations of enemy bases, the positions of map objectives (blow something up, protectsomething, hack something, etc.), and key units of the commanders army (main base, hero units,etc). Tracking this information guides the use of the commander's army. Points that need to beprotected should be surrounded with defensive structures, and a contingent of units shouldalways be near these points. Attack objectives will be sought out and defenses tested. Analysis ofdefenses around targets must be undertaken to figure out the optimal way to overcome them.

    This leads to the backbone of military gamesconflict maps.

    Conflict Maps

    Conflict maps (see Figure 4) are used and updated far more often than the previous maps.Conflict maps keep track of where battle occurs throughout the level. Whenever one of thecommanders units engages in a fight with an enemy, the unit will update the conflict map withkey information:

    Type of conflict. Units, buildings, or both

    Capabilities of units. Hits ground or air or both

    Numbers. How many where encountered

    Strength. How much damage potential is in this areaThis information can be analyzed to determine the following:

    When the enemy attacks, the AI will be able to determine whether the defenses deployedare effective, whether the defenses are engaged or ignored, and whether conflict arisesclose to defensive objectives. This can lead to the AI changing the layout andcomposition of its defenses.

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    24/32

    When attacking the enemy (preferably with scouts), weaknesses in their defense canquickly be identified. If paths exist to the goal that can avoid conflict or keep it reducedto a minimum, you have an optimal strategy to deploy the troops.

    Composition of the enemys military capabilities dictates the proper countermeasures.Because most strategy games attempt to keep some level of balance among the units, the

    computer will be able to determine the number and type of units needed to break throughdefenses at different points.

    Figure 4. An example of how a conflict map might look laid over a view of the terrain. Thedarker the red, the more conflicts encountered.

    Building and Applying Maps

    I mentioned earlier that these maps are built by the units of the commanders army. It will be partof the rules that govern the AI to send scouts out to explore as soon as possible in order to startbuilding the maps. Good AI should periodically make sure that these maps stay up to date. Earlyin the game, when only a few units are maintaining these maps, updating should be no problemto the game engine. Later in the game, with massive numbers of units all providing informationat the same time, it has the potential to affect performance.Ensuring that the decision maps are maintained quickly is not too hard. You can do so byputting the decision map system into its own thread. Indeed, each AI controlled player should

    have its own thread to handle its own set of decisions maps. The real performance gain happensif all of the entities are already broken up into multiple threads. The decision map threads onlyneed to handle the requests from the parallelized entity update messages.

    Designing Artificial Intelligence for Games (Part 4)Getting the Most out of Your AI: Threading

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    25/32

    All the previous articles in this series have been laying the foundation for this. Hopefully, younow have a good understanding of what artificial intelligence (AI) for games is all about and areasonable understanding of how to implement and apply it to your own games. Now, thechallenging part is maximizing the performance of your system.

    No matter how nice your system is, it will be worthless if it slows down the game. Efficientprogramming and optimization tricks can only bring you so far; when your aspirations reachbeyond the limits of a single core, you are just going to have to go parallel (see Figure 1).

    Figure 1: In Blizzard Entertainment's Starcraft II, massive numbers of units are all runningtheir AI at the same time. The best way to handle it all is with multithreading.

    When dealing with a system that has more than one processor or a processor with multiple cores,

    you can split the work across multiple processors. There are two approaches for this: taskparallelization and data parallelization.

    Task Parallelization

    The easiest way to multithread your application is to start breaking it down across specific tasks(see Figure 2). The different tasks that make up the game engine are often encapsulated withonly a few methods so that other systems can communicate with them.

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    26/32

    Figure 2: Functional parallelization lets each subsystem take its own thread and core.Unfortunately, systems with more cores than tasks are underutilized.

    A prime example is the audio system of the game engine. Audio does not need to interact withother systems: It just does what it's told-namely, play and mix sounds on demand. Thecommunication functions are calls to play and stop sounds, which makes audio autonomous andperfect for functional parallelization. Using threading analysis tools to assist with the effort, theaudio system can be broken off into its own thread, with a call before and after the section ofcode that you want to run in its own thread.

    So, let's see how your AI systems take advantage of this functional parallelization. Depending onthe needs of your game, you may have many distinct tasks that you can give their own thread.Here we look at three of them: path finding, strategy AI, and the entity system itself.

    Path Finding

    You might implement your path-finding system in such a way that each entity that seeks a pathcalls its own path whenever it needs one. Although this method will work, it means that theengine waits for the pathfinder whenever a path is requested. If you reorganize path finding as itsown system, you can break it into its own thread. The pathfinder will now function like aresource manager-one in which the new resource is paths.

    Any entity that wants to find a path can send out a path-finding request, then get a "ticket" backfrom the pathfinder immediately. The ticket is just a unique handle that the path-finding systemcan use to find a path. The entity can then continue on its merry way until the next frame of thegame loop. The entity can check to see whether the ticket is valid yet. If it is, the entity gets thepath back; otherwise, it can keep doing whatever it needs to while it continues to wait.

    Within the path-finding system, the ticket is used to keep track of path requests while the system

    http://software.intel.com/en-us/intel-vtune/http://software.intel.com/en-us/intel-vtune/
  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    27/32

    works on them without worrying about affecting the performance of the system. A positive sideeffect of this style of system is the automatic tracking of paths discovered. So, when a request fora previously discovered path comes in, the pathfinder can just give the ticket to the existing path.This method is great in any system with lots of entities pathing, because any path found once willlikely be needed again.

    Strategic AI

    As mentioned in the previous article, the AI system that manages the big picture of the game fitsnicely in its own thread. It can analyze the playing field and send out commands to the differententities, which can parse them when they get around to it.

    The entity system, in its own thread, will be hard at work gathering information for the decisionmaps. These findings can then be sent to the strategy AI system as update requests for thedecision maps. When the strategic AI updates, it can parse these requests, update the decisionmaps, and make judgment calls. It does not matter whether the two systems (strategic AI andentity) are in sync: They won't be far enough out of sync as to affect the decisions of the AI. (We

    are talking about 60ths of a second here anyway, and the player won't notice a single frame delayin AI reaction.)

    Data Parallelization

    Functional parallelization is great and takes advantage of systems with multiple cores. There is ahuge drawback, though: Functional parallelization may not take full advantage of all availablecores. The minute you have more cores than tasks, your program is no longer using all theprocessing power available (unless the platform your program is running on handles it for you,but I find it is best not to rely on features intended for generic applications ). Enter dataparallelization (see Figure 3).

    http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-3/http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-3/http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-3/
  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    28/32

    Figure 3: With data parallelization, a single function can take advantage of all available cores.

    In functional parallelization, you took a whole autonomous unit and gave it its own thread toplay with. Now, you are going to break a single job down and spread its work out amongdifferent threads. Doing so has the benefit of scaling up with the cores of the system. Have asystem with eight cores? Great. Have one with 64? Why not? Although functional parallelizationallows you to designate sections of code as threaded, then let them run free in the wild, dataparallelization may require a bit of extra work to get things running smoothly. For one, you coulduse a core thread (a "Master" thread) that keeps track of who is doing what. Sub-threads willneed to request "work" from the main thread to ensure that no one performs the same task twice.

    Use of a core thread to manage the data parallelization is actually a hybrid approach. The corethread is using functional parallelization, but then the thread breaks up the data across differentcores for data parallelization.

    Implementing

    Threading tools like OpenMP* (available for free in most flavors of operating systems) make theprocess of breaking your code down into separate threads easier than it has been in the past. Justmark sections of code that can be broken down with a compiler directive and OpenMP handlesthe rest. To break things down by work block, you can simply put the threading calls within theloop that goes through said resource.

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    29/32

    In the path-finding system example, the pathfinder maintains a list of requested paths. It thenloops through this list and runs the actual path-finding functions on the individual requests,storing them in a path list. This loop can be threaded so that each iteration of the loop is brokendown into different threads. These threads will be run in the first available core, allowing for themaximum use of the processing power available. The only time a core should ever be idle iswhen there is nothing left to do.

    There is potential in systems like these for multiple requests for the same job. If these requestsare spaced out, the pathfinder automatically checks to see whether the request has already beenhandled. When dealing with data parallelization, it's possible for multiple requests for the samepath to occur at the same time, which can lead to redundancies, breaking the whole point ofthreading.

    To resolve this and other redundancies, the system in question needs to keep track of which tasksare being worked on and only remove them from the request queue after they are complete. So,when a request comes in for a path that has already been requested, it needs to check this, then

    return the existing path that was assigned that ticket.

    Spawning new threads is not free. The process will involve system calls to the operating system(OS). When the OS gets around to it, it will wrap up the code needed and create the thread. Thismay take oceans of time (relative to processing speeds). This is why we don't want to spawnmore threads than are helpful. If the job being requested has already been handled, then, by allmeans, do not run the task. Also, if the task is so simple (like path finding from two very nearpoints) then it might not be worth breaking them down so finely.

    Here is a breakdown what the Path Finding functional thread will be doing and how it will breakup the work into data threads:

    RequestPath(start, goal). This function is called outside the pathfinder to get a thread.This function:

    goes through the completed requests list and determines whether the path has

    already been found (or one similar), then returns the ticket for that path;

    goes through the active request list (if the path has not been found) to find this

    path; if the path is in there, the function returns the ticket for the pending path;

    generates a new request and return a new ticket (if all else fails).

    CheckPath("ticket"). Using the ticket, this function goes through the completed requestlist and finds the path for which this ticket is valid. It returns whether the path was there,yet.

    UpdatePathFinder(). This is the shepherd function that handles the overhead for the path-finding threads. This function performs the following tasks:

    Parse new requests. It's possible for multiple requests for the same path to be

    generated at the same time from different cores. This section removesredundancies and assigns multiple tickets (from the different requests) to the samerequest.

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    30/32

    Loop through active requests. This function goes through all active requests and

    threads them. At the beginning and end of each loop, the code is marked as athread. Each thread is going to (1) find the requested path, (2) save it in thecompleted path list with the tickets it was given, and (3) remove the job from theactive list.

    Resolving Conflicts

    You may have noticed that there is potential for some calamity with this setup. Different threadsthat all need to write to the request queue or-in the data-threads that all need to add something tothe done pile can lead to writing conflicts, where one thread writes something in slot A whileanother writes something else in slot A at the same time. This conflict can lead to the well-known "race condition."

    To avoid writing conflicts, sections of the code can be marked as critical. When doing somethingmarked critical, only one thread will be able to access that section at a time: All other threadswanting to do the same thing (access the same memory) have to wait. This behavior can lead toHUGE problems, such as gridlock, when multiple threads are all blocking each other fromgetting to the memory. Gridlock is actually avoided with this setup. With the real work for thethread done, access to the critical section of memory can occur when available without tying upother sections that other threads may need.

    Keeping Things in Sync

    So, you have made all of separate AI subsystems autonomous and let them loose on the world totake advantage of every available ounce of computing they can find. Things are running quickly,but are they getting out of control?

    A game needs to be a structured experience. An engine must be able to keep things in sync. You

    can't have half of your game elements operating a frame or two ahead of everything else. Youdon't want units lying around idle waiting for a path when their counterparts are already on themove. Good parents need to keep things fair between their children.

    The main game engine loop takes care of two classes of actions: rendering and updating.Keeping these things in sync is easy in serial programming. First, all the updating runs, then therendering draws what was updated. In parallel, things might get a little hairy.

    Movement updates (often based on trajectory) might end up running a few frames faster than therenderer. The results will be jumpy animations, where the entities of the world appear to jumpand move faster than they should. The path finding, which may take into consideration asnapshot of the world entity positions, may be operating on invalid data.

    The solution to the problem of keeping your various systems in sync is fairly elegant in itssimplicity. In fact, it may already be in place for most engines. When the main game loopupdates, it keeps track of a global time index. All the various threads will make sure that theyonly need to handle updates for the current (and past, but not future) time index.

    When all of work for a given task is completed for the current time index, the thread can sleepuntil the new time index. Not only does this behavior help to ensure that things stay in sync with

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    31/32

  • 8/7/2019 Artificial Intelligence - AI Game Designing (Advanced)

    32/32

    reality of the hardware, not a limitation in taking advantage of it. With modern tools that makethreading easy to implement, there is no excuse not design your code to handle threading.

    Summary

    The field of AI is a complex area of research. AI for games takes on different forms depending

    on the needs of the game designed, ranging from simple sets of rules for computer-controlledentities to more advanced adaptive systems. Applying AI concepts to games is a necessary wayto increase the believability of the virtual characters created in electronic entertainment, but it isnot an impossible challenge. The next article in this series will discuss the challenges an AIfaces in perceiving and navigating a complex environment as well as how those challenges canbe addressed.

    About the Author

    Donald "DJ" Kehoe: As an instructor for New Jersey Institute of Technology's InformationTechnology Program, DJ developed the specialization in game development and teaches many ofthe program's courses on Game Architecture, Programming and Level Design, as well as coursesthat integrate 3D graphics with games. He is currently working on his PhD in BiomedicalEngineering where he applies game and virtual reality to enhance the effects of neuromuscularrehabilitation.