17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 23:49:19 +02:00
Files
maszyna/application/application.h
Hirek193 9dc78a65dc multiplayer: stream the authoritative state, and let a client walk into a cab
First live test showed three things wrong at once.

A client could not take a vehicle. Walking up to a locomotive and pressing the
enter-vehicle key posts entervehicle, which stage 4 classed as a world-wide
command and refused outright ("reserved for the session host"), so the client
never got a cab and every later control command died locally for want of one.
Walking into a cab is a request for a seat, not a world command: the authority
now turns it into a crew claim, for the host through the same path as for a
remote peer, and answers it the way the lobby button is answered.

Nothing but commands was ever replicated. The server sent dt and the commands
it ran, and the client simulated the rest for itself - so a pantograph the host
raised before the client joined, the contents of the memory cells the signalling
reads, brake pressures and switch positions simply never crossed the wire. The
server now streams the authoritative state a few times a second: whatever has
changed since the last one, and the whole of it every so often so a peer heals
from anything it missed. The snapshot carries the vehicle's inputs - line
breaker, battery, converter and compressor permits, pantograph valves, cab,
lights, doors - and lets the receiving peer derive the rest, plus the memory
cells and the switches.

Desync detection demanded that two state digests be equal, on a design where
the client deliberately runs its own physics. They never can be, so it reported
a desync permanently and asked for a resync every five seconds forever, which
fixed nothing because the next step disagreed again. The digest is a log line
now; what is measured instead is how far out of place the routine correction
found us, and a full resync is only asked for when even that is not catching up.

Also: a consist is put back on the rails as a whole or not at all. Correcting
one vehicle while its neighbour stayed put is what was stretching couplers
until they tore - the last test moved six vehicles out of eight, every time.

And the lobby window collapsed to nothing, because its size_max was left at the
-1 default while size_min was set, so imgui was handed a maximum smaller than
the minimum.
2026-09-06 22:19:28 +00:00

141 lines
4.3 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 "scripting/PyInt.h"
#include "network/manager.h"
#include "utilities/headtrack.h"
#ifdef WITH_UART
#include "utilities/uart.h"
#endif
class eu07_application {
const int MAX_NETWORK_PER_FRAME = 1000;
public:
// types
enum mode {
launcher = 0,
scenarioloader,
driver,
editor,
count_
};
// constructors
eu07_application() = default;
// methods
int
init( int Argc, char *Argv[] );
int
run();
// issues request for a worker thread to perform specified task. returns: true if task was scheduled
#ifdef WITH_DISCORD_RPC
void DiscordRPCService(); // discord rich presence service function (runs as separate thread)
#endif
bool
request( python_taskqueue::task_request const &Task );
// ensures the main thread holds the python gil and can safely execute python calls
void
acquire_python_lock();
// frees the python gil and swaps out the main thread
void
release_python_lock();
void
exit();
void
render_ui();
void
begin_ui_frame();
// switches application to specified mode
bool
pop_mode();
bool
push_mode( eu07_application::mode const Mode );
void
set_title( std::string const &Title );
void
set_progress( float const Progress = 0.f, float const Subtaskprogress = 0.f );
void
set_tooltip( std::string const &Tooltip );
void
set_cursor( int const Mode );
void
set_cursor_pos( double const Horizontal, double const Vertical );
void queue_screenshot();
// input handlers
void on_key( int const Key, int const Scancode, int const Action, int const Mods );
void on_char( unsigned int const Char );
void on_cursor_pos( double const Horizontal, double const Vertical );
void on_mouse_button( int const Button, int const Action, int const Mods );
void on_scroll( double const Xoffset, double const Yoffset );
void on_focus_change(bool focus);
void on_window_resize(int w, int h);
// gives access to specified window, creates a new window if index == -1
GLFWwindow *
window( int const Windowindex = 0, bool visible = false, int width = 1, int height = 1, GLFWmonitor *monitor = nullptr, bool keep_ownership = true, bool share_ctx = true );
GLFWmonitor * find_monitor( const std::string &str ) const;
std::string describe_monitor( GLFWmonitor *monitor ) const;
void
queue_quit(bool direct);
bool
is_server() const;
bool
is_client() const;
// asks the session authority to put us on the crew of given vehicle, or take us off it
void
request_vehicle_claim( network::NetworkEntityId const Entity );
void
request_vehicle_leave( network::NetworkEntityId const Entity );
// tells the network layer the scenario finished loading
void
network_scenario_loaded();
private:
// types
using modeptr_array = std::array<std::shared_ptr<application_mode>, static_cast<std::size_t>( mode::count_ )>;
using mode_stack = std::stack<mode>;
// methods
bool needs_ogl() const;
void init_debug();
void init_console();
void init_files();
int init_settings( int Argc, char *Argv[] );
int init_locale();
int init_glfw();
void init_callbacks();
int init_ogl();
int init_ui();
int init_gfx();
int init_audio();
int init_data();
int init_modes();
bool init_network();
int run_crashgui();
// members
bool m_screenshot_queued = false;
// consecutive authoritative steps whose state digest did not match ours. purely a
// diagnostic: a client runs its own physics, so it is never expected to match exactly
std::uint64_t m_statemismatches { 0 };
modeptr_array m_modes { nullptr }; // collection of available application behaviour modes
mode_stack m_modestack; // current behaviour mode
python_taskqueue m_taskqueue;
std::vector<GLFWwindow *> m_windows;
int m_glfwversion;
std::optional<network::manager> m_network;
std::optional<headtrack> m_headtrack;
};
extern eu07_application Application;