mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-28 22:47:28 +01: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:
@@ -101,41 +101,40 @@ namespace network
|
||||
size_t reconnect_delay = 0;
|
||||
|
||||
const size_t RECONNECT_DELAY_FRAMES = 60;
|
||||
// the queue of authoritative frames waiting to be played out. every frame sitting
|
||||
// in here is a frame of delay between pressing a key and seeing it happen, so it
|
||||
// is kept as short as the connection allows rather than a comfortable second deep
|
||||
const float MAX_BUFFER_SIZE = 12.0f;
|
||||
const float JITTERINESS_MIX = 0.998f;
|
||||
const float TARGET_MIN = 1.0f;
|
||||
const float TARGET_MIX = 0.98f;
|
||||
const float JITTERINESS_MULTIPIER = 2.0f;
|
||||
const float CONSUME_MULTIPIER = 0.05f;
|
||||
|
||||
// authoritative frames that have arrived and not been handed to the simulation yet.
|
||||
// they are taken in full every render: nothing is held back on purpose. the old
|
||||
// design played them out one per frame at the server's pace, which meant the world
|
||||
// ran at the wrong speed whenever the two machines drew at different rates, and
|
||||
// every frame kept in reserve was a frame of delay on the player's own controls
|
||||
std::queue<frame_info> delta_queue;
|
||||
|
||||
// how far out of place a correction found us, in a row. a full resync is only
|
||||
// worth asking for when the routine stream is not catching up on its own
|
||||
int bad_corrections = 0;
|
||||
uint64_t last_resync_tick = 0;
|
||||
int state_countdown = 0;
|
||||
int state_updates = 0;
|
||||
static constexpr int STATE_INTERVAL_FRAMES = 6;
|
||||
static constexpr int STATE_FULL_EVERY = 100;
|
||||
static constexpr double RESYNC_POSITION_ERROR = 25.0;
|
||||
static constexpr int RESYNC_BAD_CORRECTIONS = 5;
|
||||
// a correction needs time to take hold; asking again before it has is pointless
|
||||
static constexpr uint64_t RESYNC_COOLDOWN_TICKS = 600;
|
||||
|
||||
float last_target = 2.0f;
|
||||
float jitteriness = 1.0f;
|
||||
float consume_counter = 0.0f;
|
||||
|
||||
std::chrono::high_resolution_clock::time_point last_rcv;
|
||||
std::chrono::high_resolution_clock::time_point last_frame;
|
||||
std::chrono::high_resolution_clock::duration frame_time;
|
||||
|
||||
public:
|
||||
void update();
|
||||
frame_delta get_next_delta(int counter);
|
||||
// hands over everything the authority has sent since the last call, merging the
|
||||
// commands into the map. returns what the newest of those frames said about itself
|
||||
frame_delta take_pending(command_queue::commands_map &commands);
|
||||
void send_commands(command_queue::commands_map commands);
|
||||
// tells the server the scenario is loaded and a snapshot can be applied
|
||||
void send_ready();
|
||||
// publishes what our own trains are doing, so that the rest of the session can see
|
||||
// them. we run their physics, so this is the authority on them
|
||||
void publish_state();
|
||||
// our world has drifted too far to carry on; ask to be put back in line
|
||||
void send_resync_request(uint64_t tick, uint64_t state_hash);
|
||||
// lobby requests; the server is the one that decides
|
||||
|
||||
Reference in New Issue
Block a user