I'm experimenting with writing a multiplayer game, almost from scratch (I'm using C++, OpenGL, ENet, and platform-specific APIs) with a client/server networking model where the server is authoritative.
The server and clients each run their own deterministic simulation of the game. The server sends periodic snapshots of the entire game state to clients. When a client receives the snapshot, it forgets everything it knows about the game (except for the player's input after the snapshot was taken), loads the snapshot, and replays the player input that was generated after the snapshot was taken.
Now, say I want to send deltas instead of full snapshots. The server needs to remember the previous snapshot it sent, so that it can compare the current snapshot against the previous snapshot. Clients also need to remember the previous snapshot to reverse the delta. Each snapshot is a copy of the complete game state, and the server and clients now need to handle at least two of them (the previous snapshot, and the current game state).
A game state conceptually follows this pseudo-code:
class GameState {
vector<unique_ptr<GameObject>> objects;
vector<unique_ptr<Player>> players;
TerrainTile terrain[TERRAIN_WIDTH][TERRAIN_HEIGHT];
}
abstract class GameObject {
vec3 position;
vec3 velocity;
abstract void doPhysicsTick();
abstract void render();
}
class Player {
GameObject controlledObject;
string username;
}
class HumanCharacter extends GameObject {
void doPhysicsTick() {...}
void render() {...}
}
... etc ...
Is there a pattern to store multiple independent copies of the game state from different times, so that they can be easily compared?
What if I wanted the server to run an identical simulation to each client (delayed by network latency)? The server would then need to manage at least N+1 game states, where N is the number of clients - and they would be active simulations, rather than static copies. (One practical use for such a system might be to only send values that the client has predicted incorrectly, rather than all values that have changed).