-
Notifications
You must be signed in to change notification settings - Fork 91
Network mechanics
Alexander Grund edited this page Nov 27, 2015
·
6 revisions
The network code with the GameFrames (GF) and NetWorkFrames (NWF) works like that:
- GF (happens every n ms):
- Advance Simulation/Game by one step
* not directly influenced by user action, e.g. no new building added or new flag set - Increase GF number
later (in the GameLoop): - Collect GameCommands (GC)
- Possibly save game
- Advance Simulation/Game by one step
- NWF (happens before every nth GF):
- Wait for GC-Message from all players (happens only when a player lags)
- Execute 1 GC-Message from each player, record those GCs to replay and discard that Message (Here new buildings etc. are placed)
- Send all accumulated GCs as new GC-Message to server (which forwards it to all players)
- Clear list of accumulated GCs to start new collection
So obviously
- GCs are executed with a delay of 1-2 NWFs
(maximum if send right after a NWF -> Wait 1 NWF till it is sent to server and 1 NWF till it is executed) - Each client must send an (empty) GC-Message at the start of the game
(or the game would wait forever on the next NWF) - The length of a NWF must be at least as long as the ping (round-trip over network) to the "slowest" player
(the GC-Message must be received within a NWF to avoid having players to wait for others)
The server is in charge for:
- Starting the game
- Distributing Messages (mostly GC-Messages and Chat)
- Executing the AI
- Detecting when a player quit or lags to much behind
- Detecting asyncs (Game differs between players)
Therefore the general algorithm is like this:
- Send StartGame-Message
- Tells players to prepare the map and which NWF length to use
- Initiates sending the first (empty) GC-Message from each player
- Send RealStart-Message (actually a special Done-Message)
- Contains number of first GF to execute
- Contains length of 1 GF
- Execute the following in a loop till game is closed:
- If time for 1 GF has passed:
- If it is a NWF:
- Search for lagging players by checking if a GC-Message (from last NWF) is missing
If one is found, kick or warn him and skip the rest of step i. (try again a bit later) - Send GCs of AI-players
- Send a NWF-Done-Message with next GF number to execute
- Search for lagging players by checking if a GC-Message (from last NWF) is missing
- Run AI for 1 Step
- Increase GF number
- If it is a NWF:
- Relay messages
- If time for 1 GF has passed:
On the clients side it is now pretty straight-forward:
- When StartGame-Message is received, load map, send empty GC-Message
- When RealStart-Message is received, set/check first GF# and set GF-Length, then Start the game:
- Repeat the following every n ms according to GF length:
- If it is a NWF:
- Search for lagging players (non GC-Message received)
Skip the rest if one is found (try again later) - Execute the rest of the NWF (see Basics above)
- Search for lagging players (non GC-Message received)
- If it is a NWF:
