17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-28 20:27:28 +01:00

multiplayer: correct a train by sliding it, not by putting it down again

Reversing on the host turned the whole set around on the client, and the
locomotive ended up looking like something derailed making its way across the
scenery. Both come from the same place: a routine correction was calling
place_on_track(), which sets the axles from scratch and works from the
vehicle's own idea of which way it faces. That is right for a peer joining with
nothing to go on, and wrong for a train that is already running - the placement
reorients it.

A correction now slides the vehicle along the track it is already on, by the
difference between the two bogie offsets, which is the same operation the
physics performs on every step. When the two sides disagree about which piece
of track the vehicle is on, it is left alone until they agree; guessing there
is what put a locomotive beside the rails. place_on_track() is kept for the one
case it suits, a peer joining the session.

Also:

- taking a seat works with the game paused. The whole thing sat inside the
  "has any time passed" branch, so with the world stopped the lobby button
  changed the roster and nothing else happened
- the debug panel gets a Network section, shown only in a session: role, peer,
  tick, bytes and messages each way with a running rate, two charts of kB/s so
  the cost of a session can be read off, how far the worst vehicle is out of
  place, and whether the state digest agrees. The separate "network" overlay
  window is gone - everything it said lives there now, together with the lobby
  and refusal messages that had nowhere to be seen
This commit is contained in:
2026-09-07 07:49:47 +00:00
parent c0dbf99f1a
commit a0537356bb
11 changed files with 234 additions and 69 deletions

View File

@@ -416,29 +416,54 @@ void apply_controls(TDynamicObject &Vehicle, vehicle_state const &State)
Vehicle.Mechanik->TakeControl(State.aiactive);
}
// puts a vehicle back where the authority says it is. place_on_track() wants the distance
// of the vehicle's nose along the track, while what travels in the update is the offset of
// its leading bogie; this inverts the arithmetic that function itself does
void reposition(TDynamicObject &Vehicle, vehicle_state const &State)
// puts a vehicle where the authority says it is.
//
// there are two ways to do that and they are not interchangeable. place_on_track() sets
// the axles from scratch, which is right for a peer that is joining and has nothing to go
// on - but doing it to a running train turns the whole set around, because the placement
// works from the vehicle's own idea of which way it faces and that is not what a moving
// consist looks like from outside. So a routine correction slides the vehicle along the
// track it is already on, by the difference between the two bogie offsets. That is the
// very same operation the physics performs on every step, so nothing gets reoriented and
// nothing ends up beside the rails
void correct_position(TDynamicObject &Vehicle, vehicle_state const &State, bool const Absolute)
{
TTrack *track = track_by_id(State.track);
if (track == nullptr)
if (Absolute)
{
static bool reported{false};
if (!reported)
TTrack *track = track_by_id(State.track);
if (track == nullptr)
{
reported = true;
ErrorLog("net: cannot place " + Vehicle.name() + ", track " + std::to_string(State.track) + " is unknown here", logtype::net);
static bool reported{false};
if (!reported)
{
reported = true;
ErrorLog("net: cannot place " + Vehicle.name() + ", track " + std::to_string(State.track) + " is unknown here", logtype::net);
}
return;
}
double const half = Vehicle.fAxleDist * 0.5;
double const axleoffset = (State.axlefirst ? -half : half);
double const sign = (State.direction ? 1.0 : -1.0);
double const nose = (State.translation - axleoffset) * sign + 0.5 * Vehicle.MoverParameters->Dim.L;
Vehicle.place_on_track(track, nose, false);
return;
}
double const half = Vehicle.fAxleDist * 0.5;
double const axleoffset = (State.axlefirst ? -half : half);
double const sign = (State.direction ? 1.0 : -1.0);
double const nose = (State.translation - axleoffset) * sign + 0.5 * Vehicle.MoverParameters->Dim.L;
// both sides have to be talking about the same piece of track for the difference of
// the offsets to mean anything. when they are not, the vehicle is left alone until it
// gets there - a wrong guess here is what puts a locomotive next to the rails
if (track_id_of(Vehicle.RaTrackGet()) != State.track)
return;
Vehicle.place_on_track(track, nose, false);
double const delta = State.translation - Vehicle.RaTranslationGet();
if (std::abs(delta) < 0.01)
return;
// the offset runs from the track's first point, the move runs along the vehicle, and
// the axle's own sense of direction is what converts between the two
Vehicle.Move(delta * Vehicle.RaDirectionGet());
}
std::string write_vehicles(bool const Full, bool const OwnedOnly)
@@ -590,7 +615,7 @@ void read_vehicles(std::istream &Stream, network::snapshot_mode const Mode, netw
for (TDynamicObject *member : group)
{
reposition(*member, states.at(member));
correct_position(*member, states.at(member), Mode == network::snapshot_mode::join);
++Result.repositioned;
}
}