mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-17 22:39:19 +02:00
multiplayer: whoever drives a train runs it, and their ride is the smooth one
Until now the server ran the physics of everything and a client re-ran it from the server's frame times, corrected a few times a second. That is the worst of both: the client's own train never moved smoothly, its controls answered a round trip late, and the two copies still sat metres apart between corrections. Simulation authority now follows the train: - the first person aboard a train runs its physics on their own machine, and what they produce is what the rest of the session is told about it. A train with nobody in it belongs to the host. Every peer works the owner out the same way, from the crew of its lowest numbered car, so nobody has to be told - a peer never applies corrections to a train it runs itself. Its own physics is the authority on it, and overwriting that is precisely what made the owner's ride stutter - a client publishes the state of its own trains upstream, ten times a second. The server takes it, filtered so a peer can only move the trains it is entitled to move, and passes it on to everybody else in its own stream - so the picture is: your train is smooth for you and may stutter for the others; their trains may stutter for you and are smooth for them Two things follow from it: - commands for the train you run take effect the moment you press the key instead of after a trip to the server and back. They are still sent, so everybody else sees them, and the echo is dropped when it returns - the client no longer replays the server's frame times out of a jitter buffer. It runs on its own clock, like the host does, and simply carries out the commands as they arrive. Playing back somebody else's frame times meant the world ran at the wrong speed whenever the two machines drew at different rates, and every buffered frame sat between the player and their controls Also: the server only sends a frame when it has commands in it, or every tenth one to carry the tick, instead of one per rendered frame.
This commit is contained in:
@@ -32,9 +32,17 @@ command_queue::commands_map network::server_manager::pop_commands()
|
||||
|
||||
void network::server_manager::push_delta(double render_dt, double dt, uint64_t tick, uint64_t state_hash, const command_queue::commands_map &commands)
|
||||
{
|
||||
if (dt == 0.0 && commands.empty())
|
||||
if (dt == 0.0)
|
||||
return;
|
||||
|
||||
// a client no longer replays these frame by frame, so an empty one is only worth
|
||||
// sending now and then, to carry the tick and the digest
|
||||
if (commands.empty() && (--heartbeat_countdown > 0))
|
||||
return;
|
||||
|
||||
if (commands.empty())
|
||||
heartbeat_countdown = HEARTBEAT_INTERVAL_FRAMES;
|
||||
|
||||
frame_info msg;
|
||||
msg.render_dt = render_dt;
|
||||
msg.dt = dt;
|
||||
@@ -203,8 +211,12 @@ void network::manager::update()
|
||||
servers->publish_state();
|
||||
}
|
||||
|
||||
if (client)
|
||||
if (client) {
|
||||
client->update();
|
||||
|
||||
if (Global.simulation_loaded)
|
||||
client->publish_state();
|
||||
}
|
||||
}
|
||||
|
||||
void network::manager::create_server(const std::string &backend, const std::string &conf)
|
||||
|
||||
Reference in New Issue
Block a user