Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for Client-Server Games

For stand alone games, the basic game loop is (source: wikipedia)

while( user doesn't exit )
  check for user input
  run AI
  move enemies
  resolve collisions
  draw graphics
  play sounds
end while

But what if I develop client-server-like games, like Quake, Ragnarock, Trackmania, etc,

What the Loop/Algorithm for client and the server parts of the game?

like image 996
Click Ok Avatar asked May 01 '09 15:05

Click Ok


2 Answers

It would be something like

Client:

while( user does not exit )
    check for user input
    send commands to the server
    receive updates about the game from the server
    draw graphics
    play sounds
end

Server:

while( true )
    check for client commands
    run AI
    move all entities
    resolve collisions
    send updates about the game to the clients
end
like image 132
Dave Avatar answered Feb 11 '23 12:02

Dave


Client:

connect to server
while( user does not exit && connection live)
    check for user input
    send commands to the server
    estimate outcome and update world data with 'best guess'
    draw graphics
    play sounds
    receive updates about the game from the server
    correct any errors in world data
    draw graphics
    play sounds
end

Server:

while( true )
    check for and handle new player connections
    check for client commands
    sanity check client commands
    run AI
    move all entities
    resolve collisions
    sanity check world data
    send updates about the game to the clients
    handle client disconnects
end

The sanity checks on the client commands and world data are to remove any 'impossible' situations caused either by deliberate cheating (moving too fast, through walls etc) or lag (going through a door that the client thinks is open, but the server knows is closed, etc).

In order to handle lag between the client and server the client has to make a best guess about what will happen next (using it's current world data and the client commands) - the client will then have to handle any discrepancies between what it predicted would happen and what the server later tells it actually happened. Normally this will be close enough that the player doesn't notice the difference - but if lag is significant, or the client and server are out of synch (for example due to cheating), then the client will need to make an abrupt correction when it receives data back from the server.

There are also lots of issues regarding splitting sections of these processes out into separate threads to optimise response times.

One of the best ways to start is to grab an SDK from one of the games that has an active modding community - delving into how that works will provide a good overview of how it should be done.

like image 38
Stringent Software Avatar answered Feb 11 '23 11:02

Stringent Software