Programming Multiplayer Game - WordPress.com · 2013-10-10 · Phoenix area game developer since...
Transcript of Programming Multiplayer Game - WordPress.com · 2013-10-10 · Phoenix area game developer since...
Multiplayer Game Programming
David Koontz@dkoontz
dkoontz.wordpress.com
● Phoenix area game developer since 2001● Teacher at Art Institute since 2006● Worked on a variety of educational and
commercial games● Author of iTween Visual Editor and
CameraFor2D components for Unity● Currently developing Star. Ship. Story., a
co-op space faring adventure○ Slightly crazy to be making a
multiplayer game
How the Internet worksSpecial considerations for gamesUsing a library vs writing your ownExisting tools for Flash, C#, HTMLQuestions
Content Packet Header
How the Internet worksSpecial considerations for gamesUsing a library vs writing your ownExisting tools for Flash, C#, HTMLQuestions
Content Packet Header
The Problem
A B
The Problem
A B
The Problem
A B
The Problem
A B
?
The Problem
A B
The Problem
A B
The Problem
A B
The Problem
A B
The Problem
A B
The Problem
A B
The Problem
A B
The Problem
A B
The Problem
A B
The Problem
A B
The Internet functions by being able to break data (usually cats) into “packets” and then (semi)reliably delivering those packets to their destination using whatever networks are available.
IP Protocol
Destinations are numbered addresses that look like this:
127.0.0.1192.168.0.10068.105.28.11
The “protocol” bit of the “IP Protocol” is just the expected actions of each party participating in the exchange and an agreed upon way of interpreting data sent between the systems
What is a protocol?
What is a protocol?
Shaking hands when meeting someone is a social protocol. Signalling before changing lanes is standard driving protocol. “Belay, on belay, belay on” is standard rock climbing protocol.
The “protocol” bit of the “IP Protocol” is just the expected actions of each party participating in the exchange and an agreed upon way of interpreting data sent between the systems
It’s protocols all the way down
TCP
ReliableOrdered
Maintains a connectionNeeds handshake
Fetching a web siteTransferring a fileChat messages
Turn based games
UDP
UnreliableUnordered
ConnectionlessLess overhead per packet
Voice chatMost high action games
Anything severely affected by latency
IP
Both TCP and UDP add another element to the “where” part of delivering a packet
Ports
68.105.28.11:3724
Both TCP and UDP add another element to the “where” part of delivering a packet
Ports
68.105.28.11:3724
IP address Port
Ports allow the receiving computer to differentiate the packets coming in for one application vs another
Ports
Ports
Think of how many applications you have receiving data at once through the same network connection
Ports
Web browser AIM PandoraSkype MSN SpotifySystem updates GChat Games
The anatomy of an IP packetTo
tal l
engt
h, p
roto
col
num
ber,
othe
r im
porta
nt
but b
orin
g va
lues
Sou
rce
IP a
ddre
ss
Des
tinat
ion
IP a
ddre
ss
Dat
a
Por
t, H
ands
hake
info
, se
quen
ce n
umbe
r, et
c.
Header
The anatomy of an IP packetTo
tal l
engt
h, p
roto
col
num
ber,
othe
r im
porta
nt
but b
orin
g va
lues
Sou
rce
IP a
ddre
ss
Des
tinat
ion
IP a
ddre
ss
Dat
a
IP Protocol Your application’s protocolTCP/UDP
Por
t, H
ands
hake
info
, se
quen
ce n
umbe
r, et
c.
16 bytes 16 bytes /4 bytes
The data that is sent mustbe converted to a format that can be sent across the Internet.
The process of converting your data to this format is called serialization
Serialization (aka marshalling)
Dat
a
The serialized form of your data is always a byte array
Serialization
int
bool
float
string
serializer byte[ ]
Deserialization is just the opposite process, extracting specific bits of data from the byte array as an int / float / string / etc.
Deserialization
int
bool
float
string
deserializerbyte[ ]
You must know the format of the data that was serialized so that you can deserialize it correctly
Deserialization
float
bool
int
string
deserializerbyte[ ]
You must know the format of the data that was serialized so that you can deserialize it correctly
Deserialization
float
bool
int
string
deserializerbyte[ ]
How the Internet worksSpecial considerations for gamesUsing a library vs writing your ownExisting tools for Flash, C#, HTMLQuestions
Content Packet Header
The game state is all the significant values needed in order for the game to operate.
Position / RotationPlayer itemsEnemy actionTime remaining
State
All input can be processed on the same frame/update
There is only one game state
Cheating is handled by the players themselves
Input arrives inconsistently from clients
There are (# of player) game states
Cheating must be handled by the game
Local vs Internet multiplayer
Unlike local multiplayer, each client has a unique but similar game state
Mostly synched game states
Each player’s game must simulate the actions of the other players
Mostly synched game states
Sometimes these simulations are wrong
Mostly synched game states
Life on the Internet == Latency (Lag)
The difficulty of keeping all the various clients synched up is directly related to the time sensitivity of your game.
Time sensitivity
The difficulty of keeping all the various clients synched up is directly related to the time sensitivity of your game.
Time sensitivity
Async TurnBased Game
Twitch FPS
Egads, are you daft?
A worthy challenge my lad
RTS Diablo / Torchlight
Tactical FPSMMORPG
Parlor games, chess, poker
I doff my hat do thee
A tale of two game statesPart 1
This seems pretty straightforward
A tale of two game states
A tale of two game states
A tale of two game states
A tale of two game states
A tale of two game states
A tale of two game states
A tale of two game statesPart 2
Or so I thought
A tale of two game states
A tale of two game states
A tale of two game states
A tale of two game states
Character moved, shot missed
Character was shot, movement is ignored
A tale of two game statesPart 3
A wild solution appears(It’s super effective)
A tale of two game states
A tale of two game states
A tale of two game states
A tale of two game states
A tale of two game states
A tale of two game statesFin
Ok, maybe not super effective
A tale of two game states
60ms
A tale of two game states
60ms
60ms
A tale of two game states
Waiting 120ms for your key press to be registered is a bad user experience
People expect immediate response times, especially in an action oriented game
Delayed input
Extrapolation
Extrapolation
Extrapolation
This technique allows the local machine to feel responsive, while still allowing the server to be authoritative
It may lead to snapping or “rubber banding” if the server’s version of the player’s position is different from what the local simulation predicted
Extrapolation
Interpolation
Interpolation
Interpolation
Allows the client to fix errors over a short time, largely hiding the error
When combined with extrapolation, can result in very smooth looking gameplay, even under higher lag conditions
Interpolation
Extrapolating other clients
Extrapolating other clients
Extrapolating other clients
Extrapolating other clients
Extrapolating other clients
Extrapolating other clients
Extrapolating other clients
Example videos
ClientImmediate processing of input
Easy to cheat
Can result in inconsistent states
ServerMust wait for server to acknowledge input
Client are told the result of their own input
Server must be able to simulate the game
Client vs server authority
● This means you can not trust ANYTHING the client sends you
● Clients are generally not allowed to communicate directly
● Clients make requests to the server to perform an action which the server carries out if that is ok○ Is it ok for this client to be requesting this action?○ Client A using item owned by Client B
The authoritative server
Keep the server game logic isolated from the server “network” logic.
Allows you to upgrade these parts in isolation from each other
Suggestion
One approach to server logic
Server network logic
A
B
C
Server game logic
byte[]
Player “Ub3r1337”
wants to jump
One approach to server logic
Server network logic
A
B
C
Server game logic
Ok
OkPlayer “Ub3r1337”
jumped
Player “Ub3r1337”
jumped
Depending on your game’s needs, that server might be a web server (http get/post), a socket server (real time communication) or some mixture of the two
It all depends
Depending on your game’s needs, that server might be a web server (http get/post), a socket server (real time communication) or some mixture of the two
It all depends
Async TurnBased Game
Twitch FPS
UDP, socket server
HTTP over TCPPHP, Ruby on Rails, MVC.NET
RTS Diablo / Torchlight
Tactical FPSMMORPG
Parlor games, chess, poker
Socket server over TCP or UDP
Depending on your game’s needs, that server might be a web server (http GET/POST), a socket server (real time communication) or some mixture of the two
It all depends
Async TurnBased Game
Twitch FPS
UDP, socket server
HTTP over TCPPHP, Ruby on Rails, MVC.NET
RTS Diablo / Torchlight
Tactical FPSMMORPG
Parlor games, chess, poker
Socket server over TCP or UDP
More off the shelf solutions Need custom solution
How the Internet worksSpecial considerations for gamesUsing a library vs writing your ownExisting tools for Flash, C#, HTMLQuestions
Content Packet Header
● Structure!
What does a library do for you?
● Structure!● Message protocol
What does a library do for you?
● Structure!● Message protocol● Serialization & deserialization
What does a library do for you?
● Structure!● Message protocol● Serialization & deserialization● A structure for client lifecycle events
What does a library do for you?
● Structure!● Message protocol● Serialization & deserialization● A structure for client lifecycle events
○ Connection / disconnection○ Authentication○ Chat○ Saving / retrieving user data○ Payments
What does a library do for you?
● Structure!● Message protocol● Serialization & deserialization● A structure for client lifecycle events
○ Connection / disconnection○ Authentication○ Chat○ Saving / retrieving user data○ Payments
● Can provide a tie-in to hosting solutions
What does a library do for you?
● You need to use their server and client libraries○ Both ends must be able to speak the same
protocol
What does a library impose on you?
● You need to use their server and client libraries○ Both ends must be able to speak the same
protocol● May only be available for certain
languages or platforms
What does a library impose on you?
● You need to use their server and client libraries○ Both ends must be able to speak the same
protocol● May only be available for certain
languages or platforms● May be closed source and / or commercial
What does a library impose on you?
● You need to use their server and client libraries○ Both ends must be able to speak the same
protocol● May only be available for certain
languages or platforms● May be closed source and / or commercial● May impose limitations on how you deploy
or structure your server
What does a library impose on you?
● Requires more familiarity with lower level network concepts (sockets, etc.)
Rolling your own
● Requires more familiarity with lower level network concepts (sockets, etc.)
● Requires you to put in place a structure for your server and client
Rolling your own
● Requires more familiarity with lower level network concepts (sockets, etc.)
● Requires you to put in place a structure for your server and client
● Requires you to develop your own message protocol and serialization / deserialization procedures
Rolling your own
It depends!
So which is better?
It depends!
No really, that’s my entire answer
So which is better?
How the Internet worksSpecial considerations for gamesUsing a library vs writing your ownExisting tools for Flash, C#, HTMLQuestions
Content Packet Header
Server as standalone
SmartFox (Java)Photon (C#)ElectroServer (Java)Player.IO (C#)Lidgren (C#)Flash Media ServerRed5 (Java)
Server as special client
uLink (Unity)Built in UnityBadumna (Unity)
Two types of solutions
● Can be written in a language more appropriate for writing a server
Standalone server
● Can be written in a language more appropriate for writing a server
● Can be used with any language / platform there is a client library for (or that you’re willing to develop a client library for)
Standalone server
● Can be written in a language more appropriate for writing a server
● Can be used with any language / platform there is a client library for (or that you’re willing to develop a client library for)
● Needs to implement all the important logic of your game, and may not be able to reuse the same code from your client
Standalone server
● Can be written in a language more appropriate for writing a server
● Can be used with any language / platform there is a client library for (or that you’re willing to develop a client library for)
● Needs to implement all the important logic of your game, and may not be able to reuse the same code from your client
● Can be as light or heavy weight as you need
Standalone server
● Only one place to put the logic
Client as server
● Only one place to put the logic● Both client and server are the same app
and can share logic
Client as server
● Only one place to put the logic● Both client and server are the same app
and can share logic● Allows players to host games easily as
well as running dedicated hosts
Client as server
● Only one place to put the logic● Both client and server are the same app
and can share logic● Allows players to host games easily as
well as running dedicated hosts● Must interface with some sort of front end
to manage user sessions
Client as server
● Only one place to put the logic● Both client and server are the same app
and can share logic● Allows players to host games easily as
well as running dedicated hosts● Must interface with some sort of front end
to manage user sessions● Might have high overhead of running your
game client when not all of it is needed
Client as server
● Only one place to put the logic● Both client and server are the same app
and can share logic● Allows players to host games easily as
well as running dedicated hosts● Must interface with some sort of front end
to manage user sessions● Might have high overhead of running your
game client when not all of it is needed● Keeping client and server code separate
can be tricky
Client as server
Family Guy Online
Hybrid
A
B
C
Photon
Unity
Unity
D
● The Internet is a modern marvel, but is not a panacea
● Internet multiplayer is a good way to make your game 10x more complex
● There are no general solutions, only specific answers to specific problems
Summary
How the Internet worksSpecial considerations for gamesUsing a library vs writing your ownExisting tools for Flash, C#, HTMLQuestions
Content Packet Header
David Koontz@[email protected]://dkoontz.wordpress.comhttp://happycamperstudios.com
Send complaints to: