96
1 Artificial Intelligence in Games Week 3 Steve Rabin [email protected] www.aiwisdom.com/digipen TA: Chi-Hao Kuo ([email protected]) TA: Allan Deutsch ([email protected])

1 Artificial Intelligence in Games Week 3 Steve Rabin [email protected] TA: Chi-Hao Kuo ([email protected]) TA: Allan

Embed Size (px)

Citation preview

Page 1: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

1

Artificial Intelligence in GamesWeek 3

Steve [email protected]

www.aiwisdom.com/digipen

TA: Chi-Hao Kuo ([email protected])TA: Allan Deutsch ([email protected])

Page 2: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

2

Questions1. Name 3 procedural AI architectures.2. Name 3 declarative AI architectures.3. Name the 4 AI architecture “Power Design Patterns”.4. What behavior isn’t captured in a UML State Machine

diagram?5. If multiple states exist within a single state of a finite-state

machine, what is the name of this architecture?6. Name four key EVENTS that a state in the State Machine

Language (SML) might respond to.7. What is the key benefit of the global message response

feature in SML?8. How does SML support load balancing?9. Name 2 ways that SML supports Option Stacks.

Page 3: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

3

Behavior Trees

Page 4: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

4

Behavior Trees Combines

Hierarchical state machines Scheduling Planning Action Execution

Halo 2 had 50 different behaviors Tree structure of behaviors

Power comes from composability Hierarchies without having to know details of how things

get done Non-leaf behaviors decide which children should run Leaf behaviors get things done (task)

Analogy: State is to FSM as Task is to Behavior Tree

Page 5: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

5

Halo 2Behavior Tree

Page 6: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

6

Selector Node(execute tasks in order until success)

Attack Taunt Stare

?

Succeed or Fail? Succeed or Fail? Succeed or Fail?

Page 7: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

7

Selector Node(execute random tasks until success)

Attack Taunt Stare

~?

Succeed or Fail? Succeed or Fail? Succeed or Fail?

Page 8: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

8

Sequence Node(each task in order, unless fail)

Enemy

Visible?

Turn

away

Run

away

Page 9: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

9

Sequence Node(each in order, unless fail)

Enemy

Visible?

Turn

away

Run

away

Succeed or Fail?

Page 10: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

10

Sequence Node(each in order, unless fail)

Enemy

Visible?

Turn

away

Run

away

Succeed! Succeed or Fail?

Page 11: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

11

Sequence Node(each in order, unless fail)

Enemy

Visible?

Turn

away

Run

away

Succeed! Succeed! Succeed or Fail?

Page 12: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

12

Sequence Node(each in order, unless fail)

Enemy

Visible?

Turn

away

Run

away

Succeed! Succeed! Succeed!

Page 13: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

13

Sequence Node(each in order, unless fail)

Enemy

Visible?

Turn

away

Run

away

Succeed! Succeed! Succeed!

Succeed!

Page 14: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

14

Sequence Node(each in order, unless fail)

Enemy

Visible?

Turn

away

Run

away

Succeed! Succeed! Fail

Page 15: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

15

Sequence Node(each in order, unless fail)

Enemy

Visible?

Turn

away

Run

away

Succeed! Succeed! Fail

Fail!

Page 16: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

16

Nondeterministic Sequence Node(random until all executed, unless fail)

Get

matches

Get

gasolineSucceed or Fail? Succeed or Fail?

Page 17: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

17

Parallel Node(all tasks in parallel, until fail)

Run to

Enemy

Battle

crySucceed or Fail? Succeed or Fail?

Page 18: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

18

Parallel Node and Decorator Node(for condition checking)

Trash

Visible?Yes or no? Succeed or Fail?

Until fail

Tidy

trash

Fail?

(DecoratorNode)

Page 19: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

Decorator Nodeshttp://aigamedev.com/open/article/decorator/

Filters Limit the # of times it can run Prevent from firing too often with a timer Temporarily deactivate Restrict number of simultaneous running behaviors

Managers & Handlers Deal with the error status code and restart

planning or execution Store information for multiple child nodes to access

as a blackboard

19

Page 20: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

Decorator Nodes Control Modifiers

Force a return status (always fail or succeed)

Fake a certain behavior (keep running instead of failing or succeeding)

Meta Operations Debug breakpoint

(pause execution and prompt the user) Logging

(track this node execution and print to the console)

20

Page 21: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

21

Behavior Tree:Node Recap

?

~?

Selector Node: Each in order until one has success

Selector Node: Random until one has success

Nondeterministic Selector: Random until all have success

Sequence Node: Each in order unless fail

Parallel Node: All task in parallel until fail

Page 22: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

22

Behavior Tree(to enter a room)

Move

(into room)

?

Door

open?

Move

(to door)

Move

(into room)?

Barge

door

Door

open?

Open

door

Door

locked?

Page 23: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

23

Behavior Tree:Activity Open door if unlocked Otherwise either (doesn’t matter

which): Barge the door Light door on fire

But you first need matches and gasoline You need to douse gasoline on the door You need to ignite door

Page 24: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

24

Behavior Tree:Node Recap

?

~?

Selector Node: Each in order until one has success

Selector Node: Random until one has success

Nondeterministic Selector: Random until all have success

Sequence Node: Each in order unless fail

Parallel Node: All task in parallel until fail

Page 25: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

25

Behavior Tree(to enter a room)

Entering... Open door...

Barge door...

?

~?

Douse door Ignite door

Get matches Get gasoline

Page 26: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

26

Behavior Treecommunicating with blackboard

Enemy

visible? ?Select enemy

(write to blackboard)

Engage enemy

(read from blackboard)

Always

succeed

High ground

available?

Move to

high ground

Page 27: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

Example from Game AI ProBehaviorTree* bt = BehaviorTreeBuilder() .activeSelector() .sequence() //Attack the player if seen! .condition(IsPlayerVisible) .activeSelector() .sequence() .condition(IsPlayerInRange) .filter(Repeat, 3) .action(FireAtPlayer) .action(MoveTowardsPlayer) .sequence() //Search near last known position. .condition(HaveWeGotASuspectedLocation) .action(MoveToPlayersLastKnownPosition) .action(LookAround) .sequence() //Randomly scanning nearby. .action(MoveToRandomPosition) .action(LookAround) .end();

27

Page 28: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

28

Behavior Trees More scalable than simple FSMs Similar to HFSMs but with rule-based

transitions More information:

Halo 2 article http://www.gamasutra.com/gdc2005/features/200

50311/isla_01.shtml Behavior Tree Overview

http://aigamedev.com/hierarchical-logic/bt-overview

Page 29: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

29

Behavior Trees Criticism Clunky with state-based behavior

Hard to design in term of states AI based on behavior trees tends to avoid

this behavior

Doesn't deal nicely with interruptions Cumbersome to implement Examples:

Interrupting patrol route to hide or raise alarm Switching strategies when ammo low

Page 30: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

SWITCH SLIDES!

30

Page 31: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

31

Utility

Page 32: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

32

Decision Making with Utility Score choices to choose the best Utility score is NOT value

Do I want: $4.99 cat toy – 2 days to be delivered $2.99 cat toy – 5 days to be delivered

Page 33: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

33

Decision Making with Utility:Normalized & Expected Utility Utility Scores are Normalized: [0, 1]

Makes scores comparable

Expected Utility Utility * Probability

Maximum Expected Utility Choose one with highest Expected

Utility

Page 34: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

34

Decision Making with Utility:Maximum Expected Utility Attack with dagger

Utility: 0.6 Probability of hit: 85%

Attack with two handed sword Utility: 0.9 Probability of hit: 60%

Attack with dagger: 0.6 x 0.85 = 0.51 Attack with sword: 0.9 x 0.60 = 0.54

Page 35: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

35

Combining Utility Scores:Mix and Match

Page 36: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

36

Transforming Utility with Functions

Page 37: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

37

Transforming Utility with Functions:Hunger in The Sims

Page 38: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

38

Smart Terrain

Page 39: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

39

Smart Terrain Accredited to Will Wright and The Sims Intelligence resides in terrain/objects in

addition to characters Behaviors and possible actions associated with an

object are embedded in that object Example: TV object

File for TV model also contains instructions for Watching it, Turning it on and off Conditions under which a particular Sim might want to

watch it How the Sim reacts while watching it

Page 40: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

40

Smart Terrain Characters have motivations and needs

Terrain offers various ways to satisfy those needs

Each piece of terrain broadcasts to nearby characters what it has to offer

For example, when a hungry character walks near a refrigerator, the refrigerator's "I have food" broadcast allows the character to decide to get some food from it

Page 41: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

41

Smart Terrain Character is guided from action to action

realistically, driven only by simple, object-level programming

Refrigerator "I have food"

Food "Must be cooked"

Microwave "I can cook food"

Page 42: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

42

Smart Terrain:The Sims Architecture in Detail

Fix Dinner

Disposal (neat)Placement

Surface

SeatingCookPreparation

Page 43: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

43

Smart Terrain:The Sims Architecture in Detail

Data

-Needs

-Personality

-Skills

-Relationships

Person Physical

-Hunger

-Comfort

-Hygiene

-Bladder

Mental

-Energy

-Fun

-Social

-Room

Motive Engine

Personality

-Sloppy to Neat

-Shy to Outgoing

-Serious to Playful

-Lazy to Active

-Mean to Nice

Skills

-Cooking

-Mechanical

-Logic

-Body

-Etc

Page 44: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

44

Smart Terrain:The Sims Architecture in Detail

Stats

Hunger +20

Comfort -12

Hygiene -30

Bladder -75

Energy +80

Fun +40

Social +10

Room -60

Mood +18

PersonToilet

Mood+26

Urinate (+40 Bladder)

Clean (+30 Room)

Unclog (+40 Room)

Bathtub

Mood +20

Take Bath (+40 Hygiene, +30 Comfort)

Clean (+20 Room)

Find Best Action

Page 45: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

45

Smart Terrain:The Sims Architecture in Detail

Page 46: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

46

Smart Terrain:The Sims Architecture in Detail

"Happyscape"

Page 47: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

47

Smart Terrain:The Sims Architecture in Detail

"Happyscape"

Page 48: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

48

Smart Terrain:The Sims Architecture in Detail

"Happyscape"

Page 49: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

49

Subsumption Architecture

Page 50: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

50

Subsumption Architecture Term invented by Rodney Brooks

(MIT) in mid to late 1980s AI model invented by Alexandre Parodi Designed for behavior based robots

J. J. Nitao and A. M. Parodi (1986) "A Real-Time Reflexive Pilot for an Autonomous Land Vehicle", IEEE Control Systems, vol. 6, no. 1, February, pp.14-23.

R. A. Brooks (1986) "A Robust Layered Control System For A Mobile Robot", IEEE Journal Of Robotics And Automation, RA-2, April. pp. 14-23.

Page 51: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

51

Subsumption Architecture One school of thought is that

robots should model the world internally and operate on this internal model

Rodney Brooks argued World too complex to model internally

"The world is its own best model" Robots should operate by sensing the

changing environment

Page 52: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

52

Subsumption Architecture Cleanly separates the behavior of a

single character into concurrently running behaviors Each behavior could be a finite-state machine

Higher layers Goal determination and goal seeking

Lower layers Obstacle avoidance and other rudimentary

behaviors Priority is with lower layers

Can inhibit inputs or overrule outputs Lower layers form complete working system

Page 53: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

53

Subsumption Architecture:Rodney Brooks' "Allen" Robot Bottom layer: Avoidance

Avoids static and dynamic obstacles Close objects cause robot to scurry (steer) away Speed controlled by inverse square of distance to

nearest obstacle Middle layer: Wandering

Picks a destination and travels for 10 seconds Top layer: Exploring

Uses sonar to identify distance places to explore If found, it would go toward it and suppress the middle

wander layer (but summed with bottom layer)

Page 54: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

54

Subsumption Architecture:Robot Example

Avoid Obstacle

Highest Layer (goals)

Lowest Layer (reflexes)

Motors

Wander

Explore World

Create a Map

Sensor

Data

Page 55: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

55

Subsumption Architecture:Evaluation Key Benefits of Architecture

Robustness Goal preservation

Problems Can't be modularized easily Low flexibility at runtime

Page 56: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

56

Subsumption Architecture: Known games that used

subsumption: Halo series Oddworld series Jedi Knight: Dark Forces 2

Slightly different implementation, but still spirit of subsumption Increases flexibility and modularization

Page 57: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

57

Subsumption Architecture:Game Implementation (Oddworld series)

Movement

Action

Behavior

Tactics

Page 58: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

58

Subsumption Architecture:Game Implementation Movement layer forces animation

constraints Character can't go directly from "Falling

through Air" state to "Stand" state without first going to "Land" state

If hit by a punch, plays "Knock Back" animation and slides the character back a step

If character runs into a wall, it's stopped and a "Knock Back" is played

If character loses contact with ground, it falls Each state moves the character and plays the

correct animation

Page 59: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

59

Subsumption Architecture:Game Implementation Action layer

Short term goals over a finite period of time Example actions: Goto, Fight, and Die

Actions can succeed or fail (unlike motions) Goto action asks nav system for list of

waypoints and sends requests to movement layer

run, turn, run ,run, climb down ladder, run, stop Fight action might have a time limit

Failed if enemy can't be engaged Succeeded if enemy dies or engaged until time limit

Die action chooses between different death options in the movement layer

For example, die while falling, jumping, or fighting

Page 60: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

60

Subsumption Architecture:Game Implementation Behavior layer

Goals with indefinite time periods Constructed out of actions

Examples Patrol, Investigate Noise, Attack Enemy X

Patrol behavior is sequence of actions Go to location A Look around Go to location B Call in: Alpha Sector Secured! Go to location C Repeat

Attack behavior Actions like pursue character, collect nearby weapons,

look for good spots to attack from and move to them

Page 61: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

61

Subsumption Architecture:Game Implementation Tactical layer: Defend area example

Update() {if( low health ) {

request Retreat behavior; return;}if( behavior in progress ) && ( not timed out )

return;if( no enemy spotted ) {

if( suspicious noise ) request Investigate Noise behavior; return;else request Patrol behavior; return;

}else {

if( no alarm ) { if( close to enemies ) request Retreat behavior; return; else request Sound Alarm behavior; return;}request Attack Enemy behavior; return;

}}

Page 62: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

62

Subsumption Architecture:Game Implementation Upper layers make requests Each layer should not make its own

state change decisions Instead allow upper layers to make state

changes through requests If character is investigating a noise and

sees an enemy, it might retreat to sound an alarm or it might attack immediately The decision is not inside the "Investigate

Noise" behavior but is pushed up to the tactical layer

Page 63: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

63

Subsumption Architecture:Game Implementation Summary

Lower layers ignore wishes of upper layers in order to enforce rules of the game

Rule-enforcing code centralized, easy to debug, and hard to break with higher-level AI code

Character remembers what it was doing if setbacks occur

Because state is stored at every level

Page 64: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

64

Subsumption Architecture:Game Implementation Multiple modules per layer might be

needed Example: Need ability to shoot while

running Instead of adding states like "Running and

Shooting", "Standing and Throwing" One solution is to split the body into two

parts: legs and torso If animation needs both parts (like kicking)

then the torso module is suppressed

Page 65: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

65

Subsumption Architecture:Game Implementation

Body Movement

Action

Behavior

Tactics

Torso Movement

Page 66: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

66

Scripting Many games support AI scripting - Why? Pros

No recompiling of code base, designers empowered, extensible to players

Cons Harder to debug, non-programmers required to program,

time commitment and risk to create tools Choice of language

Existing languages: Python or Lua Make your own language

Dungeon Siege: Skrit Unreal Engine: UnrealScript Jak and Daxter: GOAL

Hybrid: State Machine Language Trigger system

Page 67: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

67

Scripting: Custom Scripting Languages ID Software: QuakeC

Quake series, Doom 3, games that use Quake Engine Epic Games: UnrealScript

Unreal Tournament and games that use Unreal Engine BioWare: NWscript

Neverwinter Nights Lucas Arts: SCUMM (Script Creation Utility for Maniac

Mansion) 50+ games

Maxis: Simantics The Sims, The Sims 2

Gas Powered Games: Skrit Dungeon Siege, Dungeon Siege 2, Supreme Commander

Naughty Dog: GOAL (Goal Oriented Action Language) Crash Bandicoot series and Jak and Daxter series

WizBang Studio Productions: Adlib Activision's HyperBlade, Microsoft Baseball series

Page 68: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

68

Scripting: Lua Currently most popular off-the-

shelf scripting language Games that have used Lua

World of Warcraft, Far Cry, Crysis, Psychonauts

Baulder's Gate, Homeworld 2, MDK2 Grim Fandango, Escape from Monkey Island

Page 69: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

69

Scripting:Degree of Scripting / Data-DrivenLevel 0: Hardcode everything in C/C++Level 1: Data in files specify stats/locationsLevel 2: Scripted cutscene sequencesLevel 3: Lightweight logic specified by tools or

scripts (trigger system)Level 4: Heavy logic in scripts that rely on core

C/C++ functionsLevel 5: Everything coded in scripts – full

alternative language to C/C++ (GOAL)

Page 70: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

70

Trigger System Simple

Easy to write tools and hooks Can be entirely in GUI (StarCraft level editor)

Easy for designers and players Should be impossible to crash game

If CONDITIONS then RESPONSES If player steps

ON_FLOOR_TILE(54)_and_HOLDING(BAD_LUCK_MEDALLION) then MUSIC(EERIE)_and_SPAWN(SNAKES, 20)

Perfect for mission-based games like FPS, RTS or RPGs

Page 71: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

71

StarCraft Campaign Editor:Based on Trigger System Concept

Page 72: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

72

Trigger System:Centralized Trigger System Create a centralize system to organize

triggers Instead of scattered and ad hoc

Saves CPU time Agents can subscribe to triggers/events they

are interested in Triggers occur and register themselves Loop through agents to see if they are

listening to any triggers that occurred Promotes event-driven, rather than polling

Page 73: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

73

Trigger System:One Implementationstruct TriggerRecordStruct{ EnumTriggerType eTriggerType; //The condition unsigned long nTriggerID; //Unique id of this event unsigned long idSource; //What created the event Vector vPos; //Event position float fRadius; //Influence radius of event unsigned long nTimeStamp; //When the event occurred unsigned long nExpirationTime; //Fixed event duration};

enum EnumTriggerType{ kTrig_None = 0, kTrig_Explosion = (1 << 0), kTrig_EnemyNear = (1 << 1), kTrig_Gunfire = (1 << 2)};

Page 74: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

74

Trigger System:One Implementationenum EnumTriggerType{ kTrig_None = 0, kTrig_Explosion = (1 << 0), kTrig_EnemyNear = (1 << 1), kTrig_Gunfire = (1 << 2)};

EnumTriggerType is a bit field Efficient way to identify applicable triggers

For example, blind agent only aware of Explosions and Gunfire

dwTriggerFlags = kTrig_Explosion | kTrig_Gunfire; if( trigger->triggerType & dwTriggerFlags )

{ //Applicable trigger}

Page 75: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

75

Trigger System:One Implementation triggerID

Unique ID used to refer to trigger (to remove) sourceID

Game object that created trigger Might be character who fired weapon or a landmine

that exploded vPos, fRadius

Agent only reacts if within trigger's radius nExpirationTime

Only valid for a specified amount of time Zero expiration time means it lives forever

bDynamicPos Flag that specifies trigger is moving and position must

be updated

Page 76: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

76

Trigger System:One Implementation

class CTriggerSystem

{

public:

unsigned long RegisterTrigger( eTriggerType, nPriority,

idSource, vPos, fRadius,

fDuration, bDynamicPos );

void RemoveTrigger( unsigned long nTriggerID );

void Update();

private:

TRIGGER_MAP m_mapTriggerMap; //STL multimap sorted by priority

bool m_bTriggerCriticalSection;

};

Page 77: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

77

Trigger System:One Implementation

unsigned long RegisterTrigger( eTriggerType, nPriority, idSource, vPos, fRadius, fDuration, bDynamicPos ){ //Create a trigger record and fill it in TriggerRecordStruct* pTriggerRecord = new TriggerRecordStruct( eTriggerType, idSource, vPos, fRadius, fDuration, bDynamicPos );

//Trigger records are sorted by priority m_mapTriggerMap.insert( TRIGGER_MAP::value_type( nPriority, pTriggerRecord) );

//Return the unique identifier for this trigger return pTriggerRecord->nTriggerID;}

Page 78: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

78

Trigger System:One Implementation Update function (heart of the trigger system)

Removes expired triggers Refreshes dynamic positioning triggers Notifies agents of triggers that are relevant

Update(){ loop( triggers ) { if( trigger expired ) DeleteTrigger( trigger ); else if( trigger->bDynamicPos ) UpdatePos( trigger->vPos ); } loop( agents ) { if( time for Agent update ) { loop( triggers ) //From highest to lowest priority { if( Agent doesn't respond to trigger ) continue; if( this is the Agent ) continue; if( distance > trigger->fRadius ) continue; if( Agent->HandleTrigger( trigger ) ) break; } } }}

Page 79: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

79

Trigger System:Versus Scripting Languages Can be specified within a GUI Very accessible to players Constrained to prevent crashes Quicker to implement Easier to debug

Page 80: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

80

The Inverse Ninja Law The effectiveness of a group of ninja is

inversely proportional to the number of ninja in the group

e = effectiveness n = number of ninja k = as-yet-undetermined proportionality constant

n

ke

Page 81: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

81

Stormtrooper Effect Minor characters have no chance against

the main characters Despite

Being close range Having overwhelming numbers Full armor Military-grade weapons Professional military training

They can't hit anything

Page 82: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

82

Red Shirts Stock character whose sole purpose is to

violently die shortly after being introduced

Typically, a landing party would consist of several main characters Often Kirk, Spock, McCoy And one or two never-before-seen red-

shirted ensigns Who would be dead by the end of the mission,

usually within minutes

Page 83: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

83

What would happen if Imperial Stormtroopers and Redshirts got in a firefight?

Can't hit anything Destined to die

VS

Page 84: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

84

Rule-Based Architecture

Page 85: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

85

Rule-Based Architecture:Basic Cycle

Match

ActConflict

Resolution

Changes to

Working Memory

Selected Rule

Rule Instantiations that

Match Working Memory

Page 86: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

86

Rule-Based Architecture:Complete Picture

Match

ActConflict

Resolution

Changes to

Working Memory

Actions

Sensors

Page 87: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

87

Rule-Based Architecture:Example Rules and Working Memory

Rules Get Item Rule

Condition(s) visible = true type = <type>

Action Get Item( <type> )

Working Memory Self

health = 1000 weapon = blaster

Item visible = true type = health range = 400

Item visible = true type = hyperblaster range = 500

Page 88: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

88

Rule-Based Architecture:Example Rules (1)Rule: RetreatFromEnemyIfHealthLow

Condition(s) self health < 200 enemy visible = true, name <name>

Action RetreatFromEnemy( <name> )

Rule: RetreatFromEnemyIfOverwhelmed Condition(s)

enemygroup visible = true, groupsize > 3 Action

RetreatFromEnemy( <name1> )

Rule: WanderIfNoEnemyAndHealthLow Condition(s)

self health < 200 enemy visible = false

Action Wander()

Page 89: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

89

Rule-Based Architecture:Example Rules (2)Rule: GetWeaponAmmoLow

Condition(s) self ammo = low, weapon <weapon> item visible = true, type <weapon>

Action GetItem( <weapon> )

Rule: GetWeaponIfDifferent Condition(s)

self ammo = high, weapon <weapon> item visible = true, type <weapon> != <nweapon>

Action GetItem( <nweapon> )

Rule: GetWeaponToDeprive Condition(s)

enemy visible = true, weapon <weapon> item visible = true, weapon <weapon> != <nweapon> assessbetter <nweapon>, <weapon>

Action GetItem( <nweapon> )

Page 90: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

90

Rule-Based Architecture:Conflict Resolution Which rule that matches should fire? Which instantiation of a rule should fire?

Separate instantiation for every match of variables in rules

Page 91: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

91

Rule-Based Architecture:Conflict Resolution FiltersSelect between instantiations based on filters Refractory Inhibition

Don't fire same instantiation that has already fired Data Recency

Select instantiations that match most recent data Specificity

Select instantiations that match more working memory data

More reasons to do action Random

Select randomly between the remaining instantiations Rule Order

Pick the first rule that matches Rule Importance

Pick rule with highest priority

Page 92: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

92

Rule-Based Architecture:Specificity Example Explanation

Select instantiations that match more working memory data

More reasons to do action Example

if( "see weapon I don't have" && "have high ammo for weapon") then "get item"

if( "see item" ) then "get item"

Page 93: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

93

Rule-Based Architecture:Efficiency Where does the time go?

90-95% goes to Match Matching all rules against all of

working memory each frame is too slow

Key observation # of changes to working memory

each frame is small

Page 94: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

94

Rule-Based Architecture:Efficiency Only process changes to working

memory Save intermediate match information Recompute match for rules affected by

change Use hashing to speed up

Page 95: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

95

Rule-Based Architecture:SOAR Experience Competent Quake 2 SOAR bot

800+ rules Could fight, get weapons/health, and ambush

player Problems

Can be memory intensive Can be computationally intensive Hard to debug Hard to fix bugs without affecting other

behavior

Page 96: 1 Artificial Intelligence in Games Week 3 Steve Rabin steve.rabin@gmail.com  TA: Chi-Hao Kuo (chihao.kuo@digipen.edu) TA: Allan

96

Rule-Based Architecture:Microsoft Games Studio John Laird consulted for Microsoft Plan to make rule-based system

engine for all Microsoft team sports games However, Microsoft Sports Group

disbanded one year later...