AI for Computer Game Developers Finite state machines

  • Slides: 57
Download presentation
AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path

AI for Computer Game Developers Finite state machines Path finding and waypoints A-Star path finding 1

Finite State Machine n n n An abstract machine that can exist in one

Finite State Machine n n n 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 n n An Example n n Ghosts in Pac Man Finite states n n n roaming freely, chasing the player, or evading the player Chasing The player eats a power pill n Easy to understand Easy to implement and debug Evading 2

Outline n Basic State Machine Model n n n Finite State Machine Design n

Outline n Basic State Machine Model n n n Finite State Machine Design n Generic finite state machine Example- Ghost finite state machine Structures and classes Behavior and transition functions Ant Example 3

Generic Finite State Machine Diagram S 1 t 1 Si t 2 S 2

Generic Finite State Machine Diagram S 1 t 1 Si t 2 S 2 t 4 t 3 t 5 S 3 Four possible states: {Si, S 1, S 2, S 3} Transition functions: {t 1, t 2, t 3, t 4, t 5} 4

Ghost finite state machine diagram See true: Blue true: Ghost see the player The

Ghost finite state machine diagram See true: Blue true: Ghost see the player The player eats a power pill Three possible states: roam, evade, chase Four transition functions: see true, see false, blue true, blue false 5

Model Ghost Behavior (1) Roam blue = true? Yes Evade No see. Player? Yes

Model Ghost Behavior (1) Roam blue = true? Yes Evade No see. Player? Yes Chase No 6

Model Ghost Behavior (2&3) Current state: Roam Yes Current state: Chase Evade Chase blue

Model Ghost Behavior (2&3) Current state: Roam Yes Current state: Chase Evade Chase blue = true? Yes see. Player? Yes No Evade No No blue = true? Chase Roam Yes see. Player? No Roam 7

Finite State Machine to describe Avatar behaviors 8

Finite State Machine to describe Avatar behaviors 8

Network or Hierarchical of FSM to describe the logic of the whole game Uncertainty

Network or Hierarchical of FSM to describe the logic of the whole game Uncertainty by random numbers Attack or Retreat? 9

Finite State Machines n Advantages n n n Fast data structure, dynamic memory or

Finite State Machines n Advantages n n n Fast data structure, dynamic memory or current state Non-deterministic FSM can make behavior unpredictable (by random numbers) Dis-advantages n n Number of states can How may states can I go from here? grow very fast Number of arcs can grow even faster 10

Pathfinding n Problem Dependent n n n Destination: moving or stationary? Obstacles? Terrain shape

Pathfinding n Problem Dependent n n n Destination: moving or stationary? Obstacles? Terrain shape Shortest path Aim: move around or reach a destination? 11

Outline n n n n Basic Pathfinding Random Movement and Obstacle Avoidance Tracing Around

Outline n n n n Basic Pathfinding Random Movement and Obstacle Avoidance Tracing Around Obstacles Breadcrumb Pathfinding Path Following Wall Tracing Waypoint Navigation 12

Basic Pathfinding Simple path movement Line-of-Sight path movement Destination Starting point Unnatural-looking path Destination

Basic Pathfinding Simple path movement Line-of-Sight path movement Destination Starting point Unnatural-looking path Destination Starting point More natural-looking path 13

Basic Pathfinding-Problems with Obstacles The function of obstacle avoidance is necessary element in pathfinding

Basic Pathfinding-Problems with Obstacles The function of obstacle avoidance is necessary element in pathfinding 14

Random Movement and Obstacle Avoidance n n Simple and Effective Method Suitable Environment n

Random Movement and Obstacle Avoidance n n Simple and Effective Method Suitable Environment n n Few obstacles Example: A game environment with sparsely placed trees Player’s position Player in Line of Sight? Yes Follow Straight Path to Player No Move in Random Direction 15

Tracing Around Obstacles n n Find a Path around Large Obstacles Suitable Environment q

Tracing Around Obstacles n n Find a Path around Large Obstacles Suitable Environment q q n Large obstacles Example: a mountain range in a strategy or role-playing game Methods q q q Basic Tracing Improved Tracing with Line of Sight 16

Basic Tracing - when to stop tracing? Destination Follow the edge of the obstacle

Basic Tracing - when to stop tracing? Destination Follow the edge of the obstacle in an attempt to work way around it 17

Improved Tracing Destination q Calculate a line from the tracing starts and the desired

Improved Tracing Destination q Calculate a line from the tracing starts and the desired destination. q Continue tracing until the line is crossed q Switch to simple path finding state Avoid looping back to the starting point 18

Tracing with Line of Sight Algorithm 19

Tracing with Line of Sight Algorithm 19

Breadcrumb Pathfinding n n n Memorize the previous play’s steps Each member follow the

Breadcrumb Pathfinding n n n Memorize the previous play’s steps Each member follow the leader’s breadcrumb Suitable Environment n n Move groups of computer-controlled characters Advantages n n Seemed more intelligent More effective and efficient 20

Breadcrumb Trail Recording the player positions Record 15 steps Dropping breadcrumbs Finding and Following

Breadcrumb Trail Recording the player positions Record 15 steps Dropping breadcrumbs Finding and Following the breadcrumbs 21

Detect and Follow Breadcrumb Trail Current position Follow the breadcrumb Yes Detect Breadcrumbs Find

Detect and Follow Breadcrumb Trail Current position Follow the breadcrumb Yes Detect Breadcrumbs Find neighbors: Eight possible directions Are neighbors are breadcrumbs? 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. 22

Path Following-Containment Problem Destination n Pathfinding n n Moving from a starting point to

Path Following-Containment Problem Destination n Pathfinding n n Moving from a starting point to a desired destination Path following n n n 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 computercontrolled cars to navigate a roadway Starting point Pathfinding Path Following 23

Path Following-Direction Analysis It would look unnatural if the computer-controlled object simply moves randomly

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. n n Terrain Analysis Detect Possible Directions Weighting Directions Update Position 24

Terrain Analysis and Direction Analysis Terrain analysis (x-1, y+1) (x+1, y+1) (x-1, y) (x+1,

Terrain Analysis and Direction Analysis Terrain analysis (x-1, y+1) (x+1, y+1) (x-1, y) (x+1, y) (x-1, y 1) (x, y-1) (x+1, y -1) Are the eight neighbors contained in the terrain? Consider tile-based environment Determine possible directions 1 2 3 4 8 5 7 6 25

Weighting Directions n (+2 )1 (+1 )2 Current direction n (+0 )3 n Weighting

Weighting Directions n (+2 )1 (+1 )2 Current direction n (+0 )3 n Weighting directions n Current direction Direction 1: +2 n 4(+0 ) (+1 8 ) 7 (+0 ) Up and left: direction 1 6 (+0 ) 5 (-1) n Directions 2 and 8: +1 n n n Continuing current direction is most preferred The next two best possibilities Directions 3 and 7: 0 Direction 5: -1 n Complete opposite direction is the most undesirable 26

Direction Determination n Among possible directions n n Choose one with maximum weight Move

Direction Determination n Among possible directions n n Choose one with maximum weight Move in the selected direction Update position Improve robustness n Examining more than just the adjacent tiles Road Path 27

Wall Tracing n n Exploration Technique Environment n n n Many small rooms Maze-like

Wall Tracing n n Exploration Technique Environment n n n Many small rooms Maze-like game Example: computer-controlled characters explore the environment to search of the player, weapons, treasures, … 28

Example Environment Randomly moving about the environment is one often used solution which offers

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. 29

Wall Tracing Methods n n Move in Random Directions Systematically Explore the Entire Environment

Wall Tracing Methods n n Move in Random Directions Systematically Explore the Entire Environment n Left-handed approach n n If possible, always move left Completely explore the environment Conceptually easy More effective 1 Left 2 Back 8 7 3 Straight 4 6 Right 5 Directions in view of players 30

Left-handed Strategy Straight ? Possible Next Step Impossible Left? Possible Next Step Impossible Right?

Left-handed Strategy Straight ? Possible Next Step Impossible Left? Possible Next Step Impossible Right? possible Next Step Impossible Back? Next Step 31

Wall-tracing Path n Left-handed movement method n n The character enters every room in

Wall-tracing Path n Left-handed movement method n n The character enters every room in the game environment The method is prevented from allowing the character to reach every single room 32

Waypoint Navigation n Pre-calculate Paths Reduce Pathfinding Time and CPU Load Method n n

Waypoint Navigation n Pre-calculate Paths Reduce Pathfinding Time and CPU Load Method n n Placing some nodes in the game environment Finding paths between nodes 33

Placing Nodes Labeling nodes n Rules of placing nodes n n Every point is

Placing Nodes Labeling nodes n Rules of placing nodes n n 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 34

Finding Path A path can be found from any room to any other room.

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? 35

Building a Path n Node Table n Establish the connections between nodes A B

Building a Path n Node Table n Establish the connections between nodes A B C D E n Find a Shortest Path between Two Nodes F G A B C D E F - B B B G B - 36

Completed Node Table End A B C - B B B B A -

Completed Node Table End A B C - B B B B A - C C C B B - D E E E D C C C - C C C E C C - F F F E E E - G G F F F - A Start D E F G The first step to take from node C to node F. Filling in the table is to determine the first node to visit when moving from any starting node to any ending node. 37

Finding the Path Goal B G B -> G C -> G Table intersection

Finding the Path Goal B G B -> G C -> G Table intersection ->C Table intersection ->E Move ->C Move ->E E -> G Table intersection ->F Move ->F F -> G Table intersection ->G Move ->G B -> C -> E -> F -> G 38

A* Algorithm n n One of the most widely used pathfinding algorithms It is

A* Algorithm n n 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 n n 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 39

Steps in A* Algorithm n n n Defining and Simplifying the Search Area Starting

Steps in A* Algorithm n n n 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 40

Defining the Search Area n Simplifying the Search Area n n Placing nodes Reduce

Defining the Search Area n Simplifying the Search Area n n Placing nodes Reduce the nodes Maintain a list of connections Simplifying the search area A* Algorithm is more suitable to tiled environment n Each tile serves as a node Tiled search area 41

Starting the Search n n Goal: Find the shortest path between any two nodes

Starting the Search n n Goal: Find the shortest path between any two nodes Suitable Environment n n A small tiled environment Each tile is a node Some nodes contain obstacles Search Method n Begin at the starting point and spread to the adjacent tiles 42

Search Strategy open list – keep track of the tiled needed to be checked

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; 43

Search Scenario A tiled search area Adjacent tiles to check ? ? ? ?

Search Scenario A tiled search area Adjacent tiles to check ? ? ? ? Obstacle area Starting point Destination 44

Searching Process Building a link open open After Checking all adjacent tiles open open

Searching Process Building a link open open After Checking all adjacent tiles open open Add to closed list 45

Scoring Each Tile n n Cost to move from the starting tile to any

Scoring Each Tile n n Cost to move from the starting tile to any given tile Cost to move from the given tile to the destination tile Determined by heuristic cost 1 Starting point cost 2 Given tile Destination Score = Cost from Start + Heuristic 46

Calculating Path Score c (cost) = s + h s – the number of

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 c=1+4=5 Minimum {c} = 4 open 47

Examining the Tiles with Minimum Cost The starting point is added in the closed

Examining the Tiles with Minimum Cost The starting point is added in the closed list. open closed open open open The tile with minimum cost is examined currently. There are three valid tiles in neighbors of current tile, which are added to open list. Currently considered tile 48

Spread to Adjacent Tiles open closed open closed closed open closed Current tile closed

Spread to Adjacent Tiles open closed open closed closed open closed Current tile closed open closed closed closed Current tile 49

Completed Path closed open closed closed closed closed closed The path is found when

Completed Path closed open closed closed closed closed closed The path is found when the destination is added in the open list. 50

Finding a Dead Path It is always that no valid path exists between any

Finding a Dead Path It is always that no valid path exists between any two given points. How do we know when we have reached a dead end? Monitor the open list – if no member in the open list to examine, then reach a dead path. 51

Terrain Cost n n Shortest Path = Fastest Path? Which is faster? n n

Terrain Cost n n Shortest Path = Fastest Path? Which is faster? n n n A long walk along a road A shorter walk through a swamp Consider Terrain Cost Total Cost from Start = Cost from Start + Terrain Cost Score = Total Cost from Start + Heuristic 52

Types of Terrains and Costs Previously, the cost of moving from one node to

Types of Terrains and Costs Previously, the cost of moving from one node to another was always 1. Open Terrain Cost = 1 Grassland Cost = 3 Swampland Cost = 5 Other terrains: hills, rivers, … 53

Finding Lowest-Cost Path Lowest cost Shortest Original path The lowest-cost path is not necessarily

Finding Lowest-Cost Path Lowest cost Shortest Original path The lowest-cost path is not necessarily the shortest path 54

Influence cost Mapping Influence cost mapping – a way to vary the cost of

Influence cost Mapping Influence cost mapping – a way to vary the cost of the A* nodes depending on what is happening in the game, such as enemy… Building a Influence Map n n Position Orientation The influence map cost will be added to each node’s value when calculating possible paths. 55

Building a Influence Map The tiles in the line of fire still are passable,

Building a Influence Map The tiles in the line of fire still are passable, just at a high cost. If no other path is possible, or if the alternate paths have a higher cost, the game character will pass through the line of fire. Influenced by the enemy firing zone 56

Influenced by What the Character Does Influenced by the number of kills Each time

Influenced by What the Character Does Influenced by the number of kills Each time the player makes a kill, that node increases in cost (e. g. , possible places of NPC appearance). 57