17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 22:39:19 +02:00
Files
maszyna/network/snapshot.h
Hirek193 fb94dcfa2b multiplayer: replicate the machinery you hear, not only the machinery you set
A bystander heard nothing of somebody else's converter, compressors, brake air
or relays. Two different reasons for that, and they need two different answers.

The devices whose sound runs for as long as they do - the converter, the main
compressor, the pantograph compressor, the releaser - are outputs of a physics
that a peer only half runs for a vehicle it does not own. Their running flags
travel now, alongside the permits that were already going.

The clicks and hisses are a harder case: MoverParameters::SoundFlag is raised
and wiped inside a single frame, so sampling it when an update happens to be
put together would catch almost none of them. They are collected as the vehicle
finishes with them, just before it clears the field, and the accumulation rides
along with the next update. On arrival they are added to whatever the receiving
peer's own physics produced, and its sound handling plays them once and wipes
the lot, exactly as it does for its own.
2026-09-07 08:48:30 +00:00

81 lines
2.8 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <cstdint>
#include <string>
#include "network/entities.h"
namespace network
{
// layout of the snapshot container. a reader refuses a blob it does not understand
constexpr uint32_t SNAPSHOT_VERSION = 4;
// the sections a snapshot is built from. a reader skips over any id it does not know,
// which is what makes the format extensible: a peer built before a section existed can
// still read a newer snapshot and simply ignores what it has no use for
enum snapshot_chunk : uint16_t
{
// tick, clock, weather and (in a full snapshot) the state of the random engine
SNAPSHOT_SESSION = 1,
// where every vehicle is, what its controls and appliances are set to, and what its
// brakes are doing
SNAPSHOT_VEHICLES = 2,
// who is working which vehicle
SNAPSHOT_CREWS = 3,
// contents of the memory cells - this is what signalling reads
SNAPSHOT_MEMCELLS = 4,
// which way the switches are thrown
SNAPSHOT_SWITCHES = 5
};
// what a peer is meant to do with the blob it received
enum class snapshot_mode
{
// the peer is coming in cold and takes the world as it is given
join,
// routine correction on top of a simulation that is already running
correction
};
struct snapshot_result
{
bool ok{false};
// how far the worst vehicle was from where the authority says it is
double worst_position_error{0.0};
uint32_t vehicles{0};
uint32_t repositioned{0};
};
// packs the state of the world. only ever called on the authority.
// a full snapshot carries everything and is what a joining peer gets; otherwise only what
// has changed since the last one goes out, which is what keeps the routine correction
// stream small
// OwnedOnly limits it to the trains this peer runs itself and leaves out everything that
// is not a vehicle; that is what a client sends upstream about its own train
std::string take_snapshot(bool Full, bool OwnedOnly = false);
// restores state handed to us. vehicles this peer runs itself are never touched - their
// physics is the authority, not the other way round. Owner, when given, limits the update
// to the trains that peer is entitled to move, so a client cannot shove anybody else's
snapshot_result apply_snapshot(std::string const &Blob, snapshot_mode Mode, PeerId Owner = PEER_NONE);
// forgets what was last sent, so that the next delta comes out complete
void reset_snapshot_history();
// remembers the one-shot sound events a vehicle produced. they are set and cleared inside
// a single frame, so by the time an update is put together they would be long gone; this
// is called from the vehicle's own sound handling, just before it wipes them
void note_sound_events(std::string const &Vehicle, int Events);
} // namespace network