Title: objectoriented simulation games
1object-oriented simulation(games!)
2This is a simplified version of the game code
- It makes it a lot easier to read and explain
- But its missing a lot of features
- Most of which you wont want to use anyway
- A few of which some of you will
- So were just covering the most important
features - So dont worry if you look at the real code and
see something different
3Adventure games
- One of the oldestcomputer game genres
- Based on
- wandering througha set of rooms
- interacting withobjects and characters
- Solving puzzlesor mysteries
- Subgenres
- Point-and-click adventures (Myst, Syberia)
- Text adventure games (Zork, Infocom games)
- RPGs (Final Fantasy, Everquest, Baldurs gate)
4Class hierarchy of adventure games
Things
Persons
Items
Key items
Player
Monsters
Supplies
Portals
NPCs
Equipment
Places
- Game state
- Spatiality
- Things have locations (places)
- Places have contents (sets of things)
- Inventory
- Equipment
- Consumables (health, gold, potions, etc.)
- Key items
- Unlock parts of the game
- Allowing the narrative to progress
Rooms
Inventory
5An adventure game in Meta
- Supports
- Pictures (for point-and-click style)
- Text output
- Sound
- You can decide which of these you want to use
- Basic ontology
- Things (have name, location)
- Places (have name, contents)
- Player
- Portals (have name, location, destination)
6The Thing class
- Things have these fields
- Name
- A string describing the object
- May be null
- Location
- Place in which the Thing can be found
- May be null
- And optionally adjectives
- List of strings describing the object
- May be null
7The code
- Things have
- Name
- A string describing the object
- May be null
- Location
- Place in which the Thing can be found
- May be null
- And optionally adjectives
- List of strings describing the object
- May be null
- define Thing class Thing name
location Object
adjectives
8Make a class (type) called Thing
- define Thing class Thing name
location Object
adjectives
9Thats a kind of Object
- define Thing class Thing name
location Object
adjectives
10That has these fields
- define Thing class Thing name
location Object
adjectives
11But you only specify values for these when you
make one with new
- define Thing class Thing name
location Object
adjectives
12The Place class
- Places should have these fields
- Name (like things)
- ContentsA list of Things inside the place
- And a bunch of optional fields
- Sound
- Sound to play when the user enters the room
- Can be null if no sound
- Sound-loop?
- If true, the sound plays continuously
- Image
- Bitmap to display when the user enters the room
- Hotspots
- A list of places in the image the player can
click and handlers (procedures to call) for when
theyre clicked - Adjectives (like Things)
- Location (like Things)
13The code
- Places have
- Name
- ContentsA list of Things inside them
- And
- Sound
- Buffer to play when the user enters the room
- Can be null if no sound
- Sound-loop?
- If true, the sound plays continuously
- Image
- Bitmap to display when the user enters the room
- Hotspots
- A list of places in the image the player can
click and handlers (procedures to call) for when
theyre clicked - Adjectives (inherited from Thing)
- Location (inherited from Thing)
- define Place class Place name
Thing contents hotspots
sound sound-loop?
image
14Make a class (type) called Place
- define Place class Place name
Thing contents hotspots
sound sound-loop?
image
15Thats a kind of Thing
- define Place class Place name
Thing contents hotspots
sound sound-loop?
image
16Thats a kind of Thing and so has all of
Things fields
- define Place class Place name
Thing contents hotspots
sound sound-loop?
image
17That also has these fields
- define Place class Place name
Thing contents hotspots
sound sound-loop?
image
18That also has these fields (well,
okay, Thing already has a name)
- define Place class Place name
Thing contents hotspots
sound sound-loop?
image
19But you only specify a value for name when you
make one with new
- define Place class Place name
Thing contents hotspots
sound sound-loop?
image
20Trivial example
- (Based on a randomly chosen picture from my hard
drive) - One room
- Two Things in it
- The Player
- A group of Nerds
21Trivial example
- define trivial-exampledefine great-hall
make Place the Great Hall
adjectives list big
pink
image image-file Aaron talk.jpg
sound sound-file
dog-bark4.wav
hotspots list list 640 320 760 450
Aaron says the-player ? new
Player Emily great-hallmake Nerds null
great-hall adjectives list
manystart-game Sample game
800 600 100 100
22Trivial example
Make the Place object
- define trivial-exampledefine great-hall
make Place the Great Hall
adjectives list big
pink
image image-file Aaron talk.jpg
sound sound-file
dog-bark4.wav
hotspots list list 640 320 760 450
Aaron says the-player ? new
Player Emily great-hallmake Nerds null
great-hall adjectives list
manystart-game Sample game
800 600 100 100
23Trivial example
Set up its attributes
- define trivial-exampledefine great-hall
make Place the Great Hall
adjectives list big
pink
image image-file Aaron talk.jpg
sound sound-file
dog-bark4.wav
hotspots list list 640 320 760 450
Aaron says the-player ? new
Player Emily great-hallmake Nerds null
great-hall adjectives list
manystart-game Sample game
800 600 100 100
24Trivial example
Make Player object
- define trivial-exampledefine great-hall
make Place the Great Hall
adjectives list big
pink
image image-file Aaron talk.jpg
sound sound-file
dog-bark4.wav
hotspots list list 640 320 760 450
Aaron says the-player ? new
Player Emily great-hallmake Nerds null
great-hall adjectives list
manystart-game Sample game
800 600 100 100
25Trivial example
Make Nerds object
- define trivial-exampledefine great-hall
make Place the Great Hall
adjectives list big
pink
image image-file Aaron talk.jpg
sound sound-file
dog-bark4.wav
hotspots list list 640 320 760 450
Aaron says the-player ? new
Player Emily great-hallmake Nerds null
great-hall adjectives list
manystart-game Sample game
800 600 100 100
26Wait a minute
- Were creating in-game objects in three different
ways
- define great-hall make Place the Great
Hall adjectives list big
pink image image-file
Aaron talk.jpg sound
sound-file dog-bark4.wav
hotspots list list 640 320
760 450 Aaron
says - the-player ? new Player Emily great-hall
- make Nerds null great-hall adjectives
list many
27Wait a minute
- Were creating in-game objects in three different
ways - Putting it in a local variable
- define great-hall make Place the Great
Hall adjectives list big
pink image image-file
Aaron talk.jpg sound
sound-file dog-bark4.wav
hotspots list list 640 320
760 450 Aaron
says - the-player ? new Player Emily great-hall
- make Nerds null great-hall adjectives
list many
28Wait a minute
- Were creating in-game objects in three different
ways - Putting it in a local variable
- Putting it in a previously defined global variable
- define great-hall make Place the Great
Hall adjectives list big
pink image image-file
Aaron talk.jpg sound
sound-file dog-bark4.wav
hotspots list list 640 320
760 450 Aaron
says - the-player ? new Player Emily great-hall
- make Nerds null great-hall adjectives
list many
29Wait a minute
- Were creating in-game objects in three different
ways - Putting it in a local variable
- Putting it in a previously defined global
variable - And not even bothering to name it
- How can this work?
- define great-hall make Place the Great
Hall adjectives list big
pink image image-file
Aaron talk.jpg sound
sound-file dog-bark4.wav
hotspots list list 640 320
760 450 Aaron
says - the-player ? new Player Emily great-hall
- make Nerds null great-hall adjectives
list many
30Wait a minute
- Variables are just a mechanism for naming objects
- Objects exist independently of variables
- An object can have 0, 1, or many names
- The names an object has change over time
- define great-hall make Place the Great
Hall adjectives list big
pink image image-file
Aaron talk.jpg sound
sound-file dog-bark4.wav
hotspots list list 640 320
760 450 Aaron
says - the-player ? new Player Emily great-hall
- make Nerds null great-hall adjectives
list many
31So how do I know how/whether to use a variable to
name my object?
- You name it if your code needs to refer to it
elsewhere - If you only need to refer to it in the procedure
that creates it, its best to use a local
variable - If other parts of the program need to refer to
it, you need to use a global variable - Because a procedures local variables are only
usable by that procedure. - Otherwise, theres no need to name it
- define great-hall make Place the Great
Hall adjectives list big
pink image image-file
Aaron talk.jpg sound
sound-file dog-bark4.wav
hotspots list list 640 320
760 450 Aaron
says - the-player ? new Player Emily great-hall
- make Nerds null great-hall adjectives
list many
32Trivial example
Open window and start game
- define trivial-exampledefine great-hall
make Place the Great Hall
adjectives list big
pink
image image-file Aaron talk.jpg
sound sound-file
dog-bark4.wav
hotspots list list 640 320 760 450
Aaron says the-player ? new
Player Emily great-hallmake Nerds null
great-hall adjectives list
manystart-game Sample game
800 600 100 100
33Moving objects
- We need to make sure that
- If a thing T says place P is its location
- Then P lists T in its contents
- And vice-versa
- Note however, some Things can have null (no
object) as their location - Then we have to make sure we dont try to use the
locations fields (since the location isnt
really an object)
- define move thing placeremove thing
thing.locationadd thing place - define remove thing placething.location ?
nullunless null? place
place.contents.Remove thing - define add thing placething.location ?
placeunless null? place
place.contents.Add thing
34Initializing the fields
- When we create Things and Places we need to make
sure their location and contents fields are set
properly - This is called initializing them
- define initialize-thing xadd x x.location
- define initialize-place pinitialize-thing
pp.contents ? new ArrayList
35Initializing the fields
- When we make a thing, tell its location to add
the thing to its contents list
- define initialize-thing xadd x x.location
- define initialize-place pinitialize-thing
pp.contents ? new ArrayList
36Initializing the fields
- And when we make a place, make sure it has a
contents list!
- define initialize-thing xadd x x.location
- define initialize-place pinitialize-thing
pp.contents ? new ArrayList
37Initializing the fields
- Oh, but remember that Places are Things too!
- define initialize-thing xadd x x.location
- define initialize-place pinitialize-thing
pp.contents ? new ArrayList
38Initializing the fields
- Now theres just one problem
- How does the system know to call these procedures
when you make a Thing or a Place?
- define initialize-thing xadd x x.location
- define initialize-place pinitialize-thing
pp.contents ? new ArrayList
39Initializing the fields
- It doesnt
- It calls one procedure called initialize,
regardless of what kind of object its making
- define initialize xif is? x Place
initialize-place x if is? x Thing - initialize-thing x
- etc
40Yuk! What a ing mess!
- Its completely crazy to have to have
- One big, huge initialize procedure
- That you have to modify every time you make a new
class
- define initialize xif is? x Place
initialize-place x if is? x Thing - initialize-thing x
- etc
41Generic procedures to the rescue!
- Initialize is a special kind of procedure called
a generic procedure, that - Runs different code
- Depending on what kind of inputs you give it
- Generic procedure have a little table of methods
to run for different types of inputs. - You can add methods (code) to the table using
define-method
- define initializegeneric-procedure
- define-method initialize Type1 x code for
Type1 - define-method initialize Type2 x code for
Type2 - define-method initialize Type3 x code for
Type3
42Defining initialize methods
- We just change our original initialization
procedures
- define initialize-thing xadd x x.location
- define initialize-place pinitialize-thing
pp.contents ? new ArrayList
43Defining initialize methods
- We just change our original initialization
procedures - Into method declarations
- define-method initialize Thing xadd x
x.location - define initialize Place pcall-next-method
p.contents ? new ArrayList
44How to read a method definition
- define-method initialize Thing xadd x
x.location
45How to read a method definition
Whenever initialize is called
- define-method initialize Thing xadd x
x.location
46How to read a method definition
And its input is a Thing
- define-method initialize Thing xadd x
x.location
47How to read a method definition
Name that input x
- define-method initialize Thing xadd x
x.location
48How to read a method definition
- define-method initialize Thing xadd x
x.location
And run this code
49How to read a method definition
- define-method initialize Place
pcall-next-methodp.contents ? new
ArrayList
50How to read a method definition
Whenever initialize is called
- define-method initialize Place
pcall-next-methodp.contents ? new
ArrayList
51How to read a method definition
And its input is a Place
- define-method initialize Place
pcall-next-methodp.contents ? new
ArrayList
52How to read a method definition
Name that input p
- define-method initialize Place
pcall-next-methodp.contents ? new
ArrayList
53How to read a method definition
- define-method initialize Place
pcall-next-methodp.contents ? new
ArrayList
And run this code
54How to read a method definition
- define-method initialize Place
pcall-next-methodp.contents ? new
ArrayList
In this case, starting with whatever code
Places parent class would run
55How to read a method definition
- define-method initialize Place
pcall-next-methodp.contents ? new
ArrayList
Places parent class is Thing, so this
runs Things initialize code
56Describing scenes
- ? view-description great-hall
- You are in a big, pink place named the Great
Hall. You see a many nerds. - The describe procedure is used to generate the
text the system prints to describe a place
57Describing scenes
- define view-description p concat You are in
description p .
You see comma-separate map
description p.contents
true . - The description procedure makes a string that
describes a Thing - So we get
- The description of the room
- The descriptions of all the objects in the room
- (which we get by mapping description over
p.contents) - And glue them together using concat
- And comma-separate, which takes a list of strings
and glues them together with commas
58Describing scenes
- define view-description pconcat You are in
description p .
You see comma-separate map
description -
filter t ? ? t the-player
p.contents
true . - Actually, that has some bugs
- First of all, the player is one of the objects in
the room so wherever the player goes, it will
tell them that they see themselves - Kind of stupid
- So we filter the player out from the list of
objects in the room before we map description
over it.
59Describing scenes
- define view-description pwith stuff-in-room
filter t ? ? t the-player Everything
but the player
p.contents concat You are in
description p
. if empty? stuff-in-room
concat You see
comma-separate map description
stuff-in-room
true
. - Also, if the player goes into a room with no
objects (other than the player), it says You are
in a room. You see . - Looks stupid
- So we have to check the number of objects in the
room and not print the You see part if there
isnt anything to see.
60Describing objects
- Okay, how do we describe anindividual Thing?
- Print out
- a
- Its adjectives (if any)
- Its type
- named its name (if any)
- Examples
- description new Cat
- a cat
- description new Cat Harry null
- a cat named Harry
- with h new Cat Harry null
h.adjectives ? list big black
describe h - a big, black cat named Harry
- Note
- Stuff in gray is black magic we havent taught
you about yet
- define description twith type-name
type-of t.Name.ToLower modifiers
maybe-pad
comma-separate t.adjectives
false
name-string if null? t.name
concat named t.name concat
a modifiers
type-name name-string
61Sound and vision
- You can tell the system what to show/play when
the player enters a room by setting its fields - make Place image image-file
filename.jpg - Tells the system to display the image
filename.jpg from the images directory of the
game. - make Place sound sound-file
filename.wav - Tells the system to play the sound filename.wav
from the sounds directory.
62Handling clicks of the mouse
- make Place image image-file
filename.jpg hotspots list hotspot1
hotspot2 - A hotspot specifies an area of the screen and
what do to when the user clicks there - Its specified as list
- list left top right bottom string
- Where
- Left and top give the coordinates of the
upper-left corner of an area of the image - Right and bottom give the coordinates of its
lower-right corner - When the user clicks within the hotspot, the
system prints string in the game window.
63Other kinds of hotspots
- make Place image image-file
filename.jpg hotspots list hotspot1
hotspot2 - Besides strings to print, you can use a hotspot
to specify a procedure to run or a Thing object
to click - list left top right bottom procedure
- When the user clicks the hotspot, the system runs
procedure (calls it with no inputs) - list left top right bottom Thing
- When the user clicks the hotspot, the system runs
the click procedure on Thing. - click is a generic-procedure, so you can add
methods to it - list left top right bottom string
- When the user clicks within the hotspot, the
system prints string in the game window.
64The Portal class
- Portals are objects that, when clicked on, move
the player to a specified destination (a Place
object) - However, it looks funny for the system to print
You see a portal - So theres a subclass called Door
- That just forces it to say you see a door named
blah rather than a portal named blah.
- define Portalclass Portal name location
destination Thing - define Doorclass Door name location
destination Portal
65Going through portals
- When you put a portal in a hotspot, clicking the
hotspot causes the system to run the click
generic procedure - The click method for Portal just
- Moves the player to the portals destination
- Tells the game to update the display
- define-method click Portal pmove
the-player p.destinationupdate-disp
lay!
66Handling text input
- define-pattern procedurepattern
- Tells the system to run procedure when the user
types something that looks like pattern
- define quit current-window.Close
- define-pattern quit quit
- Tells the system to run the quit procedure when
the player types quit
67Text patterns
- define-pattern procedurepattern
- Elements of pattern can be
- Strings (must be matched exactly)
- Type names
- String (the type)Matches whatever the next word
is - Thing, Place, etc.System searches the users
current location for an object of this type - Integer, Float, or Number
- define-pattern pick-uppick up Thing
- Example
- Suppose
- You wrote a procedure called pick-up that lets
the player collect objects - Takes a Thing as its input
- The player is in a room that has an object called
screwdriver - Then when the user types
- pick up screwdriver
- The system will call pick-up on that object
68Text patterns
- define-pattern procedurepattern
- Elements of pattern can be
- Strings (must be matched exactly)
- Type names
- String (the type)Matches whatever the next word
is - Thing, Place, etc.System searches the users
current location for an object of this type - Integer, Float, or Number
- define-pattern buybuy Thing for Number
dollars - Example
- Suppose
- You wrote a procedure called buy that lets the
player buy objects - Takes a Thing as its first input
- Takes a Number as its second input
- The player is in a room that has an object called
screwdriver - Then when the user types
- buy screwdriver for 10 dollars
- The system will execute
- buy screwdriver 10
- Where screwdriver is whatever the object is whose
name field says screwdriver.
69A fancier example
- This example sets the variable mouse-debug-mode?
- To true when the user types mouse debug on
- Or false if they type mouse debug off
- When mouse-debug-mode? is true
- The system prints the image coordinates of the
point you click on - Rather than running the click procedure
- This lets you easily find coordinates when youre
making hotspots in your images
- define set-mouse-mode mode ?
mouse-debug-mode? ? ? mode
off print-line if
mouse-debug-mode? Mouse hits
will display coordinates Mouse
hits will call click methods - define-pattern set-mouse-modemouse debug
String
70Other built-in handlers
- debug on/off
- Turns the variable debug-mode? true or false
- Not used by the system but might be useful for
you. - go back
- Brings the user to the place they were last
71A more interesting example
- Typing go to Thing will run click on the Thing,
as if you had put it in a hotspot and the user
had clicked the hotspot - You can customize the behavior of go to by adding
new methods to click
- define-pattern clickgo to Thing
- define-pattern clickgo Thing
72The sample game
- Brief highlights of my trip to AAAI last year
- AAAI is an Artificial Intelligence Conference
- Yes, I know this is a lame example
- To run it
- Load Sample game.meta
- Run sample-game
- define great-hall null
- define robot-room null
- define art-room null
- define origami-room null
- define sample-gameset-up-hallset-up-robot-
roomset-up-origami-roomset-up-art-roomset
-up-doorsstart-game Sample game - 800
600 100 100
73Example room the great hall
- Its best to write a separate procedure to
initialize each room - That keeps the top-level procedure from being too
big and hard to read - Set up sound and image
- Set misc-text field
- Which we didnt talk about, but which prints when
you enter the room - Set hotspots to various strings and procedures to
run - Make the player
- And put it in the magic variable the-player
- Make the nerds
- define set-up-hall
- Set up the great hall
- great-hall ? make Place the Video Games
Symposium - image image-file Aaron
talk.jpg - sound sound-file clap.wav
- misc-text Aaron Khoo is
giving a talk on Mythica. - hotspots list list 174 392
238 471 ouch - list 466
346 585 451 ouch - list 46
87 513 440 Stop that... - list 640
320 760 450 Aaron says ... - Objects within the great hall
- the-player ? new Player Emily great-hall
- make Nerds null great-hall
- determiner many
74The ouch procedure
- define ouch-counter 0
- define ouch-statementslist Ouch
Please don't do that. Ouch! Are
you having a good time? This is getting a
little infantile, isn't it? - define ouchprint-line item ouch-statements
ouch-counterwhen - length ouch-statements 1
ouch-counter ? ouch-counter 1
75The Eat procedure
- Lets you type Eat
- Simulates destroying the Thing by moving it to
null (i.e. nowhere) - Now its not in the room
- And so it appears to be destroyed
- Prints Mmmm, yumming Things
- Returns false
- Which tells the system not to redraw the display
- define eat thingmove thing
nullprint-line Mmmm, yummy
type-of thing.Name.ToLower ... - define-pattern eateat Thing
76The art and origami rooms
- The art room brings you to the origami room when
you click on it
- define set-up-art-room
- Set up the art room
- art-room ? make Place the art installation
- image
image-file Art installation.JPG - misc-text It
was make by art and
robotics students
from the
University of New Orleans. - hotspots list
list 130 50 323 360 -
origami-room - define set-up-origami-room
- Set up the origami room
- origami-room ? make Place null
- image
image-file Origami room.jpg - sound
sound-file dog-bark4.wav -
adjectives list origami filled
77Making doors
- Doors in this system are only one-way
- They live in one room
- And lead to the other
- But arent visible in the other
- So we need to make doors in pairs
- One from room A to room B
- One from room B to room A
- So its easiest to make a procedure to do this
for us
- define make-door-pairplace1 name1 place2 name2
? new Door name1 place1 place2 new Door
name2 place2 place1 - define set-up-doorsConnect the
roomsmake-door-pair great-hall null
robot-room symposium - make-door-pair art-room null
robot-room art
78Working with the code
- The code is divided into several files
- It is distributed as a .zip file
- Download it from the web
- Unzip it into the directory of your choice
- To load the code
- Click the file Adventure.meta
- Choose Execute All from the Execute menu
- The list of files is stored in the variable
source-files in Adventure.meta - To add a new file
- Add its name to the end of source-files
- Type Control-E to update the definition
- Run reload
79A tour of the code
- Adventure.meta
- List of source files
- Code to load other source files
- Code to find art assets (sound and image files)
- Parsing.meta
- Basic code for handling text input
- Basic class files
- Things.meta, Places.meta, Actors.meta,
Player.meta, Portals.meta
- Sound stuff.meta
- OS-dependent code for loading and playing .wav
files - User interface.meta
- OS-dependent code for window updating, and
handling of user events (clicks and text input) - String utilities.meta
- Stuff for handling text
- Sample game.meta
- Example game
You only need to worry about the basic class
files and the sample game
80Subdirectories
- Sounds
- Holds any sound files you want (.wav only, sorry)
- Files can be accessed using the sound-file
filename procedure - Images
- Holds any images files (.jpg, .gif, .bmp, or
.png) you want to use - Files can be accessed using the image-file
filename procedure - Note
- You dont have to include the directory names in
the filenames for sound-file and image-file.
They know to search in the Sounds and Images
directories, respectively.