17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-18 14:59:19 +02:00
Files
maszyna/application/drivermode.h
Hirek193 1bfa1644c9 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.
2026-09-07 16:00:41 +00:00

128 lines
4.0 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 "application/applicationmode.h"
#include "input/driverkeyboardinput.h"
#include "input/drivermouseinput.h"
#include "input/gamepadinput.h"
#include "Console.h"
#include "vehicle/Camera.h"
#include "utilities/Classes.h"
#ifdef WITH_UART
#include "utilities/uart.h"
#endif
#ifdef WITH_HARDWARE_PROTOCOL_V2
#include "hardware/hardware_manager.h"
#endif
#ifdef WITH_ZMQ
#include "input/zmq_input.h"
#endif
class driver_mode : public application_mode {
public:
// constructors
driver_mode();
// methods
// initializes internal data structures of the mode. returns: true on success, false otherwise
bool init() override;
// mode-specific update of simulation data. returns: false on error, true otherwise
bool update() override;
// maintenance method, called when the mode is activated
void enter() override;
// maintenance method, called when the mode is deactivated
void exit() override;
// input handlers
void on_key( int Key, int Scancode, int Action, int Mods ) override;
void on_cursor_pos( double Horizontal, double Vertical ) override;
void on_mouse_button( int Button, int Action, int Mods ) override;
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:
// types
enum view {
consistfront,
consistrear,
bogie,
driveby,
count_
};
struct view_config {
TDynamicObject const *owner { nullptr };
glm::vec3 offset {};
glm::vec3 angle {};
};
struct drivermode_input {
gamepad_input gamepad;
drivermouse_input mouse;
glm::dvec2 mouse_pickmodepos; // stores last mouse position in control picking mode
driverkeyboard_input keyboard;
#ifdef _WIN32
Console console;
#endif
#ifdef WITH_UART
std::unique_ptr<uart_input> uart;
#endif
#ifdef WITH_HARDWARE_PROTOCOL_V2
std::unique_ptr<hardware::hardware_manager> hardware;
#endif
#ifdef WITH_ZMQ
std::unique_ptr<zmq_input> zmq;
#endif
std::unique_ptr<motiontelemetry> telemetry;
bool init();
void poll();
std::string
binding_hints( std::pair<user_command, user_command> const &Commands ) const;
std::pair<user_command, user_command>
command_fallback( user_command const Command ) const;
};
// methods
void update_camera( const double Deltatime );
// handles vehicle change flag
void OnKeyDown( int cKey );
void InOutKey();
void CabView();
void ExternalView();
void DistantView( bool const Near = false );
void set_picking( bool const Picking );
// members
drivermode_input m_input;
std::array<basic_event *, 10> KeyEvents { nullptr }; // eventy wyzwalane z klawiaury
TCamera Camera;
TCamera DebugCamera;
int m_externalviewmode { view::consistfront }; // selected external view mode
bool m_externalview { true };
std::array<view_config, view::count_> m_externalviewconfigs;
TDynamicObject *pDynamicNearest { nullptr }; // vehicle nearest to the active camera. TODO: move to camera
double fTime50Hz { 0.0 }; // bufor czasu dla komunikacji z PoKeys
double const m_primaryupdaterate { 1.0 / 100.0 };
double const m_secondaryupdaterate { 1.0 / 50.0 };
double m_primaryupdateaccumulator { m_secondaryupdaterate }; // keeps track of elapsed simulation time, for core fixed step routines
double m_secondaryupdateaccumulator { m_secondaryupdaterate }; // keeps track of elapsed simulation time, for less important fixed step routines
int iPause { 0 }; // wykrywanie zmian w zapauzowaniu
command_relay m_relay;
std::string change_train; // train name awaiting entering
};