17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-02 13:59:12 +01:00

multiplayer: a cab belongs to the peer it is on, and the first one aboard drives

Joining a crew did nothing. The cab was being built by a replicated entervehicle
that the authority posted only when it had no cab of its own - which is exactly
the case that never happens for the second person to board. The host claims a
vehicle, the server builds a cab, and from then on every further claim finds one
already standing over there, posts nothing, and the joining peer waits forever
for a command that is not coming.

A cab is not a shared object. Each participant has their own, with their own
camera and their own view of it, and nothing about it needs to travel. So it is
built locally by whoever needs one: the driver mode does it for the player when
their claim is granted, and the authority does it for itself, because it needs a
cab to address the AI handover commands to. That also stops a cab being created
on every peer for every claimed vehicle, which is what the entervehicle
broadcast was doing.

Who runs a shared train is no longer decided by car numbering. It is whoever
boarded first, and it stays with them: every crew member is stamped with a
session-wide counter when they sit down, and the smallest stamp on the whole
consist owns the physics. Coupling, uncoupling and renumbering do not move it,
and neither does somebody else joining - each handover is a stutter in the ride
of the person who has been driving, and they are the one who would notice it.
The stamps travel with CREW_UPDATE and in the snapshot, because a client works
out ownership for itself and has to arrive at the same answer.

Chat: the key that opens it is now read before imgui gets a look at the input.
Any window holding the keyboard - the lobby, most likely, which is precisely
where a client sits - would swallow it, so on that side the panel could not be
opened at all. The exception is text actually being typed, which is the chat box
itself; escape closes that. A client also logs what it says, so if a line goes
missing it is clear which half of the trip lost it.

Protocol version 6, snapshot version 7.
This commit is contained in:
2026-09-07 16:00:41 +00:00
parent 0112d81b77
commit 1bfa1644c9
14 changed files with 168 additions and 36 deletions

View File

@@ -894,6 +894,17 @@ eu07_application::get_input_hint( user_command const Command ) const {
void eu07_application::on_key(int const Key, int const Scancode, int const Action, int const Mods)
{
// the session chat opens on the key left of 1, and it is checked before imgui gets a
// look at the input: a lobby or any other window that happens to have the keyboard
// would otherwise swallow the key and the chat could never be opened at all. the one
// case where imgui does get it is when something is already taking typed text - the
// chat box itself, most of the time, which closes on escape instead
if ((Key == GLFW_KEY_GRAVE_ACCENT) && (Action == GLFW_PRESS) && (Mods == 0) && !m_modestack.empty() && network::is_multiplayer() &&
!ui_layer::wants_keyboard())
{
if (m_modes[m_modestack.top()]->toggle_chat())
return;
}
if (ui_layer::key_callback(Key, Scancode, Action, Mods))
return;

View File

@@ -51,6 +51,8 @@ public:
virtual void on_scroll( double Xoffset, double Yoffset ) = 0;
virtual void on_window_resize( int w, int h ) = 0;
virtual void on_event_poll() = 0;
// opens or closes the session chat; true when this mode has one to open
virtual bool toggle_chat() { return false; }
virtual bool is_command_processor() const = 0;
protected:

View File

@@ -31,6 +31,7 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/Logs.h"
#include "network/entities.h"
#include "network/snapshot.h"
#include "network/session.h"
/*
namespace input {
@@ -213,6 +214,17 @@ bool driver_mode::update()
if (!change_train.empty())
{
TTrain *train = simulation::Trains.find(change_train);
if ((train == nullptr) && network::is_multiplayer())
{
// a cab belongs to the peer it is on: each player has their own, with their own
// camera and their own view of it. waiting for a replicated command to build it
// only ever worked for the first person aboard a train - the second finds the
// cab already standing on the peer that got there first, so nothing is posted
// and the lobby button appears to do nothing at all
train = network::ensure_local_cab(simulation::Vehicles.find(change_train));
}
if (train)
{
Global.local_start_vehicle = change_train;
@@ -540,20 +552,6 @@ void driver_mode::on_key(int const Key, int const Scancode, int const Action, in
bool anyModifier = Mods & (GLFW_MOD_SHIFT | GLFW_MOD_CONTROL | GLFW_MOD_ALT);
// the key left of 1 opens and closes the session chat. it is only bound in a session,
// so single player keeps it for whatever else may want it later. while the chat has
// the keyboard this is never reached: imgui swallows the input before it gets here,
// which is exactly what stops typed letters from working the cab
if ((Key == GLFW_KEY_GRAVE_ACCENT) && (Action == GLFW_PRESS) && !anyModifier && network::is_multiplayer())
{
auto ui = std::dynamic_pointer_cast<driver_ui>(m_userinterface);
if (ui != nullptr)
{
ui->toggle_chat();
return;
}
}
// give the ui first shot at the input processing...
if (!anyModifier && true == m_userinterface->on_key(Key, Action))
{
@@ -629,6 +627,16 @@ void driver_mode::on_event_poll()
m_input.poll();
}
bool driver_mode::toggle_chat()
{
auto ui = std::dynamic_pointer_cast<driver_ui>(m_userinterface);
if (ui == nullptr)
return false;
ui->toggle_chat();
return true;
}
bool driver_mode::is_command_processor() const
{

View File

@@ -49,6 +49,8 @@ public:
void on_scroll( double Xoffset, double Yoffset ) override;
void on_window_resize( int w, int h ) override { ; }
void on_event_poll() override;
// opens or closes the session chat
bool toggle_chat() override;
bool is_command_processor() const override;
private:

View File

@@ -112,6 +112,11 @@ bool ui_layer::key_callback(int key, int scancode, int action, int mods)
return m_imguiio->WantCaptureKeyboard;
}
bool ui_layer::wants_keyboard()
{
return (m_imguiio != nullptr) && m_imguiio->WantCaptureKeyboard;
}
bool ui_layer::char_callback(unsigned int c)
{
ImGui_ImplGlfw_CharCallback(m_window, c);

View File

@@ -115,6 +115,9 @@ public:
// callback functions for imgui input
// returns true if input is consumed
static bool key_callback(int key, int scancode, int action, int mods);
// true when something on screen is taking typed input and the game must keep its
// hands off the keyboard
static bool wants_keyboard();
static bool char_callback(unsigned int c);
static bool scroll_callback(double xoffset, double yoffset);
static bool mouse_button_callback(int button, int action, int mods);