Foundaons)of)Game)AI · Elizais)notintelligentbut...) •...
Transcript of Foundaons)of)Game)AI · Elizais)notintelligentbut...) •...
Founda'ons of Game AI
Level 7 – AI Scrip'ng
Prof Alexiei Dingli
What is AI scrip'ng?
• Simple programming language • Serves a specific task • Enable game designers (not programmers) to hand craE game
mechanics • Players can also modify the game • Scrip'ng languages such as Lua or Python can be used but also
graphical representa'ons • Scripts are edited using standard text editors • Some games allow real 'me scrip'ng during game play
Scrip'ng AJributes
• Easier to tweak AI opponents (no need to re-‐compile)
• Anything can be tweaked – Intelligence – Speed – Strength – Magic
Example AJribute Script
CREATURE=1; INTELLIGENCE=20; STRENGTH=75; SPEED=50; END
Example Condi'onal Script CREATURE=1; If (LEVEL<5)
BEGIN INTELLIGENCE=20; STRENGTH=75; SPEED=50; END
ELSE BEGIN INTELLIGENCE=40; STRENGTH=150; SPEED=100; END
END
Example Behavior Script
If (PlayerArmed==TRUE) BEGIN DoFlee(); END
ELSE BEGIN DoAJack(); END
Example AI Behavior Script
If ( PlayerChallenge == DIFFICULT ) BEGIN DoFlee(); END
ELSE BEGIN DoAJack(); END
Example Patrolling Behavior
If (creature.state==Patrol) Begin move(0,1); move(-‐1,0); … End
Example Verbal Interac'on If (PlayerArmed ==Dagger)
Say("What a cute liJle knife."); If (PlayerArmed ==Bow)
Say("Drop the bow now and I'll let you live."); If (PlayerArmed ==Sword)
Say("That sword will fit nicely in my collec'on."); If (PlayerArmed ==BaJleAxe)
Say("You're too weak to wield that baJle axe.");
Example Scrip'ng Events
If (PlayerLoca'on(120,76)) Trigger(kExposionTrap); PlaySound (kExplosion);
If (PlayerLoca'on(56,16)) Trigger(kPoisonTrap);
Example Keyword Scrip'ng
If (Ask("what") and Ask("name") ) begin Say("I am Merlin."); end
Scrip'ng a ChaJerbot • Eliza, the first chaJerbot : a computer program that mimics
human conversa'on – Joseph Weizenbaum, MassachuseJs Ins'tute of Technology
• User types in natural language
• ELIZA then analyzes the user's statement and generates some response which it types out
12
ELIZA Sample
conversa'on
Men are all alike. IN WHAT WAY? They're always bugging us about something or other. CAN YOU THINK OF A SPECIFIC EXAMPLE? Well, my boyfriend made me come here. YOUR BOYFRIEND MADE YOU COME HERE He says I'm depressed much of the Hme. I AM SORRY TO HEAR YOU ARE DEPRESSED It's true. I am unhappy DO YOU THINK COMING HERE WILL HELP YOU NOT TO BE UNHAPPY I need some help, that much seems certain. WHAT WOULD IT MEAN TO YOU IF YOU GOT SOME HELP Perhaps I could learn to get along with my mother. TELL ME MORE ABOUT YOUR FAMILY
13
Eliza's strategy Keywords
• Input is read and inspected for the presence of a keyword
• When keyword is found, the sentence is transformed according to a rule associated with the keyword
• Else a content-‐free remark or, under certain condi'ons, an earlier transforma'on is retrieved.
• The text so computed or retrieved is then printed out
14
ELIZA Example transforma'ons
"I am X" à "How long have you been X?«
I am very unhappy. HOW LONG HAVE YOU BEEN VERY UNHAPPY?
"X YOU Y ME" à "What makes you think I Y you?«
It seems that you hate me. WHAT MAKES YOU THINK THAT I HATE YOU?
15
ELIZA Why does it work?
• Rogerian psychotherapist à Can pretend to know
almost nothing of the real world "I went for a long boat ride" "Tell me about boats"
• Not a real dialog (no real communica'on)
– ELiza generates a reac'on (oEen a rather non commital one) to the user's statement
– The user cannot ask for informa'on. – The user cannot issue an order
16
ELIZA Does it really work?
Eliza: Hello. I am ELIZA. How can I help you? You: I went to Paris yesterday Eliza: Can you elaborate on that? You: I took the train Eliza: Can you elaborate on that? You: The train was late Eliza: Please go on. You: All the seats were full Eliza: Please go on. You: A liKle boy was having a tantrum Eliza: Please go on.
17
ELIZA Does it really work?
Indexicals (I, you, here, now) User: I am having fun right now. Eliza: Does it please you to believe that I am having fun right now.
World Knowledge User: I am a human. Eliza: How long have you been a human?
18
When Eliza fails • Yes/No QuesHons
User: Are you a good therapist? Eliza: Would you prefer it if I were a good therapist? User: I'm wondering about your skills as a therapist; Eliza: Does it please you to believe I am wondering about my skills of therapist?
• Wh-‐QuesHons User: Who are you? Eliza: Would you prefer it if I were not? User: What do you want me to tell? Eliza: Does that ques'on interest you?
• Complex AsserHons User: I don't have a problem. I'm quite happy, I have a girlfriend, I study. Everything is OK! Eliza: Why do you not have any problem, I'm quite happy, you have a girlfriend, you study. Everything is OK!
19
Eliza is not intelligent but...
• Regardless of what people believe or know about the inner workings of computers, they talk about them and interact with them as social en''es.
• Weizenbaum (1976) notes that many people con'nued to believe in ELIZA’s abili'es even aEer the program’s opera'on was explained to them.
• People act toward computers as if they were people and expect that computers should be able to understand their needs and be capable of interac'ng with them naturally.
• Given these predisposi'ons, speech-‐ and language-‐based system are not supposed to be intelligent
• But they may provide users with the most natural interface for many applicaHons
20
Exercise
• Have a chat with the Eliza Bot (index.html)
• Have a look at the rules (elizadata.js)
The Eliza Rules ["<key>", <rank>, [ [ // first decomposi'on paJern "<decomp>", ["<reasmb>", "<reasmb>", "<reasmb>"] ], [ // second decomposi'on paJern "<decomp>", ["<reasmb>", "<reasmb>", "<reasmb>"] ] ] ]
The Eliza Rules • Keywords with higher rank take precedence
• Decomposi'ons are matched in defini'on order
• Reasemblies are chosen by random or cycled through if the no-‐random flag is set
• The special keyword "xnone" holds the rules for default phrases (no match)
• Pres, posts, synonyms, keywords, decomposi'ons all in lower case
• All defini'ons are op'onal but at least elizaKeywords should be supplied
• If no keywords are found `transform()' returns the default string "I am at a loss for words.”
Decomposi'on Rules
• * ... any words (incl. none)
• $ ... (in first posi'on) reassemble for memory only
• @synon ... subs'tute entry with a synonymous expression
Reassambly
• (n) ... insert param of posi'on n (first is "1")
• posi'ons are any matchings of "*" or "@synon"
Eliza Examples
["sorry", 0, [ ["*", [ "Please don't apologise.", "Apologies are not necessary.", "I've told you that apologies are not required.", "It did not bother me. Please con'nue." ]]
Eliza Examples
["* i am* @sad *", [ "I am sorry to hear that you are (3).", "Do you think coming here will help you not to be (3) ?", "I'm sure it's not pleasant to be (3).", "Can you explain what made you (3) ?" ]],
Eliza Examples
["forget", 5, [ ["* i forget *", [ "Can you think of why you might forget (2) ?", "Why can't you remember (2) ?", "How oEen do you think of (2) ?", "Does it bother you to forget that ?", "Could it be a mental block ?", "Are you generally forge~ul ?", "Do you think you are suppressing (2) ?" ]], ["* did you forget *", [ "Why do you ask ?", "Are you sure you told me ?", "Would it bother you if I forgot (2) ?", "Why should I recall (2) just now ?", "goto what", "Tell me more about (2)." ]] ]],
Exercise
Create a bus booking Chatbot. 1. Gree'ng 2. From 3. To 4. Bye
Crea'ng a conversa'onal agent
• AIML – Ar'ficial Intelligence Markup Language – Used in ALICE (Ar'ficial Linguis'c Internet Computer En'ty)
– Won the • Leobner prize 3 'mes • ChaJerbox challenge
30
Cycle
Human inputs
sentence
Chatbot searches database
Chatbot produces response
31
Cycle
Human: “How are you
doing?”
Chatbot: “How are * … Template: I’m fine, thanks
Chatbot: I’m fine, thanks
32
PaJerns
<aiml> <category> <paJern> HELLO </paJern> <template> Hi! How are you? </template> </category>
… </aiml>
33
AIML shorthand
! A bot will not know this line is here p Hello t Hi there! I hope you are \ having a great day!
34
AIML Macros
! Remember...comments begin with the ! character ! The macro on the next line defines the bot's name $ my_name Larry p what is your name t My name is $$my_name.
35
AIML Macros
$ i_am_a robot p you are weird t Please don't hold my $$i_am_aness against me :-‐(
36
Loops pf hi hello "what is up" hola howdy "what is going on" t Hi, it's very nice to meet you! p hi t Hi, it's very nice to meet you! p hello t Hi, it's very nice to meet you! p what is up t Hi, it's very nice to meet you! p hola t Hi, it's very nice to meet you! …
37
Complex Loops
p I pf really " ” pf like love p being with pf dogs cats animals t Really? I enjoy being with animals, too!
38
Loops and Macros $ like_terms like love p i pf $$like_terms p fish t You like fish? I HATE fish! p i pf $$like_terms p dogs t I like dogs, too!
39
Advanced Loops p i pf " " really p love pf dogs cats fish animals t You love $$[1][*]? I love all kinds of $$[1][3]!
40
0 1 2 3
0 “ ” really
1 dogs cats fish animals
Random Lists
p hello t r l Hi! It's nice to meet you! l Hello, how are you today? l Gree'ngs! l Hi, what are you up to?
41
Random Lists
<paJern> HELLO </paJern><template><random> <li> Hi! It's nice to meet you! </li> <li> Hello, how are you today? </li> <li> Gree'ngs! </li> <li> Hi, what are you up to? </li> </random></template>
42
Condi'ons
p how do i look t I think you look cnv gender male handsome cnv gender female preJy t !
43
Case Study: Robo BaJle Pigs
Case Study: Robo BaJle Pigs
• Objec've
– To destroy your opponent's Robo BaJle Pig by inflic'ng five points of damage with your laser or powerful robot fist
Case Study: Robo BaJle Pigs
• Rules
– For two players, the pigs start the game on opposite sides of the board, just to the right of the center line, facing the center of the board
– Each round of the game is made of five moves. Both players plan their five moves. The players then reveal their "programs", and move their pigs simultaneously.
Case Study: Robo BaJle Pigs • AI Challenge
– Must create a string containing five encoded commands, separated by spaces
– Must use the standard command
– Must issue one Damage(X) command for each wound suffered by the pig
– Must ensure that the Repair(R) command is used correctly, which means that Repair(R) commands may only be given if all five commands are Repair(R)
Case Study: Robo BaJle Pigs • Variables
– X -‐ Your pig's x-‐coordinate – Y -‐ Your pig's y-‐coordinate – Health -‐ Your pig's health – Facing -‐ The direc'on your pig is facing, encoded as an integer, where N=0,
E=1, S=2, W=3 – opponentX -‐ Your opponent's x-‐coordinate – opponentY -‐ Your opponent's y-‐coordinate – opponentHealth -‐ Your opponent's health – opponentFacing -‐The direc'on your opponent is facing, encoded as above
Case Study: Robo BaJle Pigs • Command Encoding
– "^": Move the pig one square forward. – "v": Move the pig one square backward. – "/": Move the pig diagonally forward and right. – "\": Move the pig diagonally forward and leE. – "TL": Turn the pig to the leE. – "TR": Turn the pig to the right. – "F": Fire a laser beam in a straight line in the direc'on the pig is facing. – "H": Swing the massive robo baJle pig baJle fist, striking the three squares in front of
the pig. – "R": Repair one point of damage. – "X": Do nothing. One Damage(X) command must be issued for each point of damage
suffered by the pig.
Random AI
String randomCommand() { // Math.random() returns a random double between 0 and 0.999... // Mul'ply by 8 and cast to an int, and we should have a num in [0,7]. int n = (int)(Math.random() * 8); switch(n) { case 0: return "^ "; case 1: return "\\ "; case 2: return "/ "; case 3: return "TL "; case 4: return "TR "; case 5: return "v "; case 6: return "F "; case 7: return "H "; default: print("This should never happen."); } } // This will hold the result String result = ""; // Make sure we have one Damage(X) command for each wound int i; for (i=0; i<5-‐health; i++) result += "X "; // All other commands are random for (; i<5; i++) result += randomCommand(); // The last thing in the script must be the command string. result;
Sit and Shoot AI
// This will hold the result String result = ""; // Make sure we have one Damage(X) command for each wound int i; for (i=0; i<5-‐health; i++) result += "X "; // All other commands are Fire(F) for (; i<5; i++) result += "F "; // The last thing in the script must be the command string. result;
Exercise
• Try the
– Random AI – Sit and Shoot AI
Exercise
• Using the BeanShell scrip'ng language (hJp://www.beanshell.org) write your own AI
• Use your AI to fight against your neighboring classmate
Ques'ons?