17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-18 03:19:19 +02:00

multiplayer stage 6: a logical simulation tick and a real state digest

The protocol hung off whatever frame the renderer happened to be drawing, and
the only thing standing in for synchronisation was the sum of every vehicle's
coordinates with a random number added on top - a value that cannot be equal on
two machines and therefore reported a desync on every single step.

- Global.simulation_tick: the authority counts the steps, a client takes the
  number from the authoritative frame it is executing. FRAME_INFO and the
  command requests carry it, so the network timeline has one unambiguous order
  independent of frame rates
- network/statehash: a versioned FNV digest of the gameplay critical state -
  vehicle position, speed, direction, brake pipe and cylinder pressure, plus the
  clock. Values are quantized so that float noise is not mistaken for drift, and
  vehicles are folded in under their own identity and summed, so the digest does
  not depend on the order the scenario created them in. Switches, memcells and
  the event queue are the next candidates; they go in when stage 7 pins down
  what a snapshot contains
- a client compares its own digest against the authoritative one and reports the
  drift when it starts and then only now and then, rather than every step; the
  overlay says how long it has been out of step and at which tick
- refusing to compare at all when the two builds disagree on the digest layout

EU07_NETWORK_VERSION is now 4.
This commit is contained in:
2026-09-06 19:21:12 +00:00
parent 4870935983
commit b255c8120a
13 changed files with 223 additions and 47 deletions

View File

@@ -12,7 +12,9 @@
// 2 - legacy lockstep protocol
// 3 - handshake carries build identification and an explicit rejection message
std::uint32_t const EU07_NETWORK_VERSION = 3;
// 4 - peer identity, vehicle roster and crew handshake, command source,
// logical simulation tick and a versioned state digest instead of the position sum
std::uint32_t const EU07_NETWORK_VERSION = 4;
namespace network {
@@ -279,7 +281,7 @@ void network::client::update()
}
// client
std::tuple<double, double, command_queue::commands_map> network::client::get_next_delta(int counter)
network::frame_delta network::client::get_next_delta(int counter)
{
auto now = std::chrono::high_resolution_clock::now();
if (counter == 1) {
@@ -289,8 +291,7 @@ std::tuple<double, double, command_queue::commands_map> network::client::get_nex
if (delta_queue.empty()) {
// buffer underflow
return std::tuple<double, double,
command_queue::commands_map>(0.0, 0.0, command_queue::commands_map());
return frame_delta();
}
@@ -316,13 +317,20 @@ std::tuple<double, double, command_queue::commands_map> network::client::get_nex
consume_counter = std::clamp(consume_counter - mult, -MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
}
frame_delta delta;
delta.dt = entry.dt;
delta.tick = entry.tick;
delta.state_hash = entry.state_hash;
delta.state_hash_version = entry.state_hash_version;
delta.commands = entry.commands;
delta.valid = true;
delta_queue.pop();
return std::make_tuple(entry.dt, entry.sync, entry.commands);
return delta;
} else {
// nothing to push
return std::tuple<double, double,
command_queue::commands_map>(0.0, 0.0, command_queue::commands_map());
return frame_delta();
}
}
@@ -333,6 +341,7 @@ void network::client::send_commands(command_queue::commands_map commands)
// eh, maybe queue lost messages
request_command msg;
msg.tick = Global.simulation_tick;
msg.commands = commands;
conn->send_message(msg);