580 likes | 721 Views
AI for Computer Game Developers. Finite state machines Path finding and waypoints A-Star path finding. Finite State Machine. An abstract machine that can exist in one of several different and predefined states . Define a set of conditions that determine when the state should change.
E N D
AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding
Finite State Machine • An abstract machine that can exist in one of several different and predefined states. • Define a set of conditions that determine when the state should change. • Characteristics • Easy to understand • Easy to implement and debug Chasing • An Example • Ghosts in Pac Man • Finite states • roaming freely, • chasing the player, or • evading the player The player eats a power pill Evading
Outline • Basic State Machine Model • Generic finite state machine • Example- Ghost finite state machine • Finite State Machine Design • Structures and classes • Behavior and transition functions • Ant Example
Generic Finite State Machine Diagram t2 S1 S2 t4 t1 t3 t5 S3 Si Four possible states: {Si, S1, S2, S3} Transition functions: {t1, t2, t3, t4, t5}
Ghost finite state machine diagram See true: Ghost see the player Blue true: The player eats a power pill Three possible states: roam, evade, chase Four transition functions: see true, see false, blue true, blue false
Roam blue = true? Model Ghost Behavior (1) Yes Evade No Yes Chase seePlayer? No
Yes Evade Yes Roam Roam blue = true? blue = true? Evade Chase No No Yes Chase No seePlayer? seePlayer? No Yes Model Ghost Behavior (2&3) Current state: Roam Current state: Chase
Network or Hierarchical of FSM to describe the logic of the whole game Uncertainty by random numbers Attack or Retreat?
Finite State Machines • Advantages • Fast data structure, dynamic memory or current state • Non-deterministic FSM can make behavior unpredictable (by random numbers) • Dis-advantages • Number of states can grow very fast • Number of arcs can grow even faster How may states can I go from here?
Pathfinding • Problem Dependent • Destination: moving or stationary? • Obstacles? • Terrain shape • Shortest path • Aim: move around or reach a destination?
Outline • Basic Pathfinding • Random Movement and Obstacle Avoidance • Tracing Around Obstacles • Breadcrumb Pathfinding • Path Following • Wall Tracing • Waypoint Navigation
Basic Pathfinding Simple path movement Line-of-Sight path movement Destination Destination Starting point Starting point Unnatural-looking path More natural-looking path
Basic Pathfinding-Problems with Obstacles Obstacles The function of obstacle avoidance is necessary element in pathfinding
Random Movement and Obstacle Avoidance • Simple and Effective Method • Suitable Environment • Few obstacles • Example: A game environment with sparsely placed trees Yes Player’s position Follow Straight Path to Player Player in Line of Sight? No Move in Random Direction
Find a Path around Large Obstacles Suitable Environment Large obstacles Example: a mountain range in a strategy or role-playing game Methods Basic Tracing Improved Tracing Tracing with Line of Sight Tracing Around Obstacles
Basic Tracing - when to stop tracing? Destination Follow the edge of the obstacle in an attempt to work way around it
Improved Tracing Destination • Calculate a line from the tracing starts and the desired destination. • Continue tracing until the line is crossed • Switch to simple path finding state Avoid looping back to the starting point
Breadcrumb Pathfinding • Memorize the previous play’s steps • Each member follow the leader’s breadcrumb • Suitable Environment • Move groups of computer-controlled characters • Advantages • Seemed more intelligent • More effective and efficient
Breadcrumb Trail Recording the player positions Record 15 steps Dropping breadcrumbs Finding and Following the breadcrumbs
Detect and Follow Breadcrumb Trail Detect Breadcrumbs Current position Find neighbors: Eightpossible directions Yes Are neighbors are breadcrumbs? Follow the breadcrumb No Follow shortest path Randomly move one step Following the exact footsteps of the player would be rather unnatural. It is more intelligent to follow the detected shortest path.
Path Following-Containment Problem Destination • Pathfinding • Moving from a starting point to a desired destination • Path following • No obvious destination • More of a containment problem: containing the computer-controlled role to the road area • E.g., a car-racing game requires the computer-controlled cars to navigate a roadway Starting point Pathfinding Path Following
Path Following-Direction Analysis It would look unnatural if the computer-controlled object simply moves randomly on the road. We need to analyze the surrounding terrain and decide on the best move. • Terrain Analysis • Detect Possible Directions • Weighting Directions • Update Position
2 3 1 4 8 5 7 6 Terrain Analysis and Direction Analysis Terrain analysis Consider tile-based environment Determine possible directions Are the eight neighbors contained in the terrain?
(+1) (+2) (+0) 2 3 1 Current direction (+0) (+1) 4 8 7 5 (-1) (+0) 6 (+0) Weighting Directions • Current direction • Up and left: direction 1 • Weighting directions • Direction 1: +2 • Continuing current direction is most preferred • Directions 2 and 8: +1 • The next two best possibilities • Directions 3 and 7: 0 • Direction 5: -1 • Complete opposite direction is the most undesirable
Direction Determination • Among possible directions • Choose one with maximum weight • Move in the selected direction • Update position • Improve robustness • Examining more than just the adjacent tiles Road Path
Wall Tracing • Exploration Technique • Environment • Many small rooms • Maze-like game • Example: computer-controlled characters explore the environment to search of the player, weapons, treasures,…
Example Environment Randomly moving about the environment is one often used solution which offers a certain level of unpredictability. However, this also can result in getting stuck in small rooms for long periods of time.
Wall Tracing Methods • Move in Random Directions • Systematically Explore the Entire Environment • Left-handed approach • If possible, always move left • Completely explore the environment • Conceptually easy • More effective Left 2 3 1 Back Straight 8 4 7 5 6 Right Directions in view of players
Left-handed Strategy Possible Next Step Straight? Impossible Possible Next Step Left? Impossible possible Next Step Right? Impossible Next Step Back?
Wall-tracing Path • Left-handed movement method • The character enters every room in the game environment • The method is prevented from allowing the character to reach every single room
Waypoint Navigation • Pre-calculate Paths • Reduce Pathfinding Time and CPU Load • Method • Placing some nodes in the game environment • Finding paths between nodes
Placing Nodes Labeling nodes • Rules of placing nodes • Every point is in the line of sight of at least one node • Every node is in the line of sight of at least one other node
Finding Path A path can be found from any room to any other room. Step 1. Calculate which node is nearest and in the line of sight Step 2. Follow the connections From A to E: A -> B -> C -> E How does the computer know the connections among the nodes?
Building a Path A B C D E F G • Node Table • Establish the connections between nodes • Find a Shortest Path between Two Nodes A B C D E F G
Completed Node Table End A B C D E F G A The first step to take from node C to node F. B C Start D E F G Filling in the table is to determine the first node to visit when moving from any starting node to any ending node.
Finding the Path Goal B G B -> G Table intersection ->C Move ->C C -> G Table intersection ->E Move ->E E -> G Table intersection ->F Move ->F F -> G Table intersection ->G Move ->G B -> C -> E -> F -> G
A* Algorithm • One of the most widely used pathfinding algorithms • It is guaranteed to find the best path between any two points • Relatively efficient algorithm • Can be used whenever possible unless dealing with some special-case scenario • E.g., if a clear line of sight exists with no obstacles between the starting point and ending point, a faster line-of-sight movement algorithm would be better. • Difficult to be understood by new game developers
Steps in A* Algorithm • Defining and Simplifying the Search Area • Starting the Search among a reasonable number of nodes • Path Scoring to Determine the Best Path • Finding a Dead End • Incorporating the Terrain Cost • Influence Cost Mapping
Defining the Search Area • Simplifying the Search Area • Placing nodes • Reduce the nodes • Maintain a list of connections • A* Algorithm is more suitable to tiled environment • Each tile serves as a node Simplifying the search area Tiled search area
Starting the Search • Goal: Find the shortest path between any two nodes • Suitable Environment • A small tiled environment • Each tile is a node • Some nodes contain obstacles • Search Method • Begin at the starting point and spread to the adjacent tiles
Search Strategy open list – keep track of the tiled needed to be checked closed list – the tiles that already were checked and no longer to be examined Link – from neighbors to the currently considered tile open list <- starting point; For each element in open list Check each adjacent node of this element; If it is valid, add it to open list and calculate cost; Add the current element in open list to closed list; Build a link from current element to valid adjacent nodes;
Search Scenario Adjacent tiles to check A tiled search area Obstacle area Starting point Destination
Searching Process Building a link open open open open open open open open open open open open open open open open After Checking all adjacent tiles Add to closed list
Cost to move from the starting tile to any given tile Cost to move from the given tile to the destination tile Scoring Each Tile Determined by heuristic cost1 cost2 Given tile Destination Starting point Score = Cost from Start + Heuristic
Calculating Path Score c (cost) = s + h s – the number of steps from the starting tile to the given tile; h – the number of steps from given tile to the destination. open open open open open c = 1 + 4 = 5 open open open Minimum {c} = 4
Examining the Tiles with Minimum Cost The starting point is added in the closed list. The tile with minimum cost is examined currently. There are three valid tiles in neighbors of current tile, which are added to open list. open closed open open open open open open open open open Currently considered tile
Spread to Adjacent Tiles open open open open closed closed open open closed closed closed open closed closed closed open closed closed closed open closed closed closed open closed closed closed closed closed closed closed closed closed Current tile Current tile
Completed Path closed closed open closed closed open closed closed closed closed closed closed closed closed closed closed closed closed closed closed closed closed closed closed closed The path is found when the destination is added in the open list.