1 of 28

How Counterstrike Works

(it’s about time)

Copyright © 2018

Chris Colohan

2 of 28

3 of 28

How does it work?

?

4 of 28

Client-server model

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

LAG

5 of 28

6 of 28

When is now?

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

Time

Input

Now

Server

Now

Display

Now

Latency (lag)

7 of 28

Speculation

Time

Execution

if()

Now

if()

if()

if()

if()

8 of 28

Speculation

Time

Execution

if()

Now

if()

if()

if()

if()

9 of 28

Speculation

Time

Execution

if()

Now

if()

if()

if()

if()

if()

10 of 28

Speculation

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

Time

Display

Now

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

P1:3,1,4�P2:1,5,9

B1:2,6,5

4

P1:3,1,4�P2:1,5,9

B1:2,6,5

5

P1:3,1,4�P2:1,5,9

B1:2,6,5

6

Input

Now

Speculative

Display

Now

Speculative

Display

Now

Speculative

Display

Now

Speculative

Display

Now

Speculative

Display

Now

Speculative

Display

Now

Speculative Display and Input Now

11 of 28

Speculation

Display

Now

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

Time

Display

Now

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

P1:3,1,4�P2:1,5,9

B1:2,6,5

4

P1:3,1,4�P2:1,5,9

B1:2,6,5

5

P1:3,1,4�P2:1,5,9

B1:2,6,5

6

Speculative Display and Input Now

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

Display

Now

Display

Now

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

4’

P1:3,1,4�P2:1,5,9

B1:2,6,5

5’

P1:3,1,4�P2:1,5,9

B1:2,6,5

6’

Display

Now

Speculative Display and Input Now

P1:3,1,4�P2:1,5,9

B1:2,6,5

4

Display

Now

12 of 28

Progress

  • Client-server model was strongly consistent
    • Server is one true arbiter of what happened
  • Adding speculation makes us weakly consistent
    • Each player observes a slightly different game
    • Server is still one true arbiter of what happened
  • Problem: Server is lagged vs clients

13 of 28

What about the enemy?

  • Shooting at where players were,�not where they are
    • From the server’s perspective

14 of 28

Lag compensation

Server side:

  • Record history of player motion
  • Rewind position to t=t-lag

15 of 28

That’s It!

  • Input speculation + lag compensation =� how video games work today
  • Can we improve on this?
    • Also improve our understanding?

16 of 28

Shot by a ghost! (Server perspective)

Shot by player you never saw,

through a wall!

17 of 28

Shot by a ghost! (Shooter perspective)

Einstein’s theory of relativity

applies to CSGO?

18 of 28

Consistency is Good!

  • Lag compensation has made game inconsistent
    • Score is still consistent, player motion is not
  • Best of both worlds:
    • Consistent, and low lag?
  • Apply speculation to all players
    • Predict the next 200ms of behavior
    • Only of players in view
    • Better predictor = better gameplay

19 of 28

Predicting the future?

  • Linear extrapolation
  • Use derivative of motion
  • Use AI to predict motion
  • On misprediction:
    • Rewind speculation, replay with new data

20 of 28

Predicting the future?

  • Linear extrapolation
  • Use derivative of motion
  • Use AI to predict motion
  • On misprediction:
    • Rewind speculation, replay with new data

AI

21 of 28

Side-channel improves prediction

  • ½ the lag
  • n2 bandwidth
  • New way of cheating?

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

22 of 28

The Hard Parts

  • Is it fresh?
    • Keystroke newer than last update from server.
  • How new?
    • Applies to which speculative update?

Time

Display

Now

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

P1:3,1,4�P2:1,5,9

B1:2,6,5

4

P1:3,1,4�P2:1,5,9

B1:2,6,5

5

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

6

Speculative Display and Input Now

Is it fresh?

How new?

P1:3,1,4�P2:1,5,9

B1:2,6,5

4’

P1:3,1,4�P2:1,5,9

B1:2,6,5

5’

P1:3,1,4�P2:1,5,9

B1:2,6,5

6’

23 of 28

Broadcast Keystrokes Everywhere

Problems:

  • High bandwidth
  • How to deal with loss?

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

24 of 28

Use Time of Day

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

25 of 28

Clocks are Hard

  • Need
    • High resolution
    • Monotone
  • Synchronization over a network is hard
    • May be impossible to get accurate enough
  • GPS based synchronization?
    • Used by Google’s Spanner database

26 of 28

Sequence numbers

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

P1:3,1,4�P2:1,5,9

B1:2,6,5

3

P1:3,1,4�P2:1,5,9

B1:2,6,5

2

P1:3,1,4�P2:1,5,9

B1:2,6,5

1

4

5

6

7

8

9

5

4

6

7

8

4

5

6

7

8

9

4

5

6

7

8

0

1

2

1

3

3

3

3

0

1

2

1

27 of 28

Can we do better?

  • Better predictors
    • Use machine learning?
  • Eliminate the server!
    • Colyseus and Donnybrook
    • Makes cheating-resistance harder
  • Optimize communication
    • Area-of-interest filtering
    • Randomize gunfire
    • Matchmaking matters
    • Have a LAN party!

28 of 28

Want More?

Useful articles:

  • What Every Programmer Needs to Know About Game Networking (goo.gl/o7mc5f)
  • Source Multiplayer Networking (goo.gl/u9J9Ya)

�Read up on:

  • Lamport clocks
  • Vector clocks
  • Spanner