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

multiplayer: names instead of peer numbers, a chat, and the exe as a dedicated server

Three things that turn out to be one thing: a session with people in it needs
to be able to say who they are, and a machine that only referees one should not
have to open a window to do it.

Names. A peer id is what the protocol addresses; "player 4" is no use to anybody
reading a lobby. CLIENT_HELLO carries a requested nickname now, and the server
decides what it actually is: trimmed of control characters and runs of spaces,
capped at 24, and made unique - two people called Marcin are two people and the
lobby has to say which is which. The result goes out as PEER_ROSTER whenever
somebody joins or leaves, and to a joining peer just before its snapshot, so the
crew section resolves to names rather than numbers. The name comes from
multiplayer.nickname in the ini or --nick on the command line, falling back to
the account name and, failing that, to the old numbering. The host is a peer
like any other here too, and registers its own name at startup.

Chat, on the key left of 1. Going up a line carries only its text: who said it
is decided by the connection it arrived on, never by the field in the packet,
which is what stops anybody putting words in anybody's mouth. The server tidies
and caps it, says it, and passes it on - a client waits to hear itself back, so
that everybody sees the conversation in the same order. While the box has the
keyboard imgui swallows the input before the driver mode sees it, so typing "d"
types "d" instead of opening the doors of whatever you are driving; that also
means the key that opened the panel cannot close it, hence escape.

And --nogui. No glfw, no window, no imgui, the null renderer, sound off, log to
the console it was started from, and nobody in a cab - the scenario is left to
its own drivers and this process is a referee rather than a participant. Every
window-dependent path is guarded on an empty window list rather than on a flag
scattered through the code, and the loop is paced at fifty steps a second
because nothing is waiting on a swapchain to slow it down. Chat lines go to the
log, which is what a machine with no window has instead of a chat panel.

    eu07.exe -s scenario.scn --host 0.0.0.0:7420 --nogui --nick Dispatcher
This commit is contained in:
2026-09-07 13:54:34 +00:00
parent a687e8f91f
commit 0112d81b77
22 changed files with 838 additions and 22 deletions

View File

@@ -220,7 +220,7 @@ void eu07_application::DiscordRPCService()
discord_rpc.largeImageText = "MaSzyna";
// run loop
while (!glfwWindowShouldClose(m_windows.front()) && !m_modestack.empty() && !Global.applicationQuitOrder)
while (!should_close() && !m_modestack.empty() && !Global.applicationQuitOrder)
{
auto currentMode = m_modestack.top();
if (currentMode == mode::launcher)
@@ -363,7 +363,7 @@ int eu07_application::init(int Argc, char *Argv[])
return result;
}
if (crashreport_is_pending())
if (crashreport_is_pending() && !Global.headless)
{ // run crashgui as early as possible
if ((result = run_crashgui()) != 0)
{
@@ -429,7 +429,10 @@ void eu07_application::queue_quit(bool direct)
// down everybody else's session as well
if (direct || is_client() || !m_modes[m_modestack.top()]->is_command_processor())
{
glfwSetWindowShouldClose(m_windows[0], GLFW_TRUE);
if (Global.headless)
Global.applicationQuitOrder = true;
else
glfwSetWindowShouldClose(m_windows[0], GLFW_TRUE);
return;
}
@@ -475,14 +478,21 @@ void eu07_application::network_scenario_loaded()
m_network->notify_scenario_loaded();
}
void eu07_application::say(std::string const &Text)
{
if (m_network)
m_network->say(Text);
}
int eu07_application::run()
{
auto frame{0};
// main application loop
while (!glfwWindowShouldClose(m_windows.front()) && !m_modestack.empty())
while (!should_close() && !m_modestack.empty())
{
Timer::subsystem.mainloop_total.start();
glfwPollEvents();
if (!Global.headless)
glfwPollEvents();
if (m_headtrack)
m_headtrack->update();
@@ -645,7 +655,9 @@ int eu07_application::run()
if (m_modestack.empty())
break;
m_modes[m_modestack.top()]->on_event_poll();
// there is no keyboard, mouse or gamepad to ask when there is no window
if (!Global.headless)
m_modes[m_modestack.top()]->on_event_poll();
if (m_screenshot_queued)
{
@@ -718,15 +730,21 @@ void eu07_application::exit()
// SafeDelete( simulation::Train );
SafeDelete(simulation::Region);
ui_layer::shutdown();
for (auto *window : m_windows)
if (!Global.headless)
{
glfwDestroyWindow(window);
ui_layer::shutdown();
for (auto *window : m_windows)
{
glfwDestroyWindow(window);
}
}
m_taskqueue.exit();
glfwPollEvents(); // TODO: This fixes a segfault on Wayland when closing. Remove after updating glfw to 3.5.
glfwTerminate();
if (!Global.headless)
{
glfwPollEvents(); // TODO: This fixes a segfault on Wayland when closing. Remove after updating glfw to 3.5.
glfwTerminate();
}
if (!Global.exec_on_exit.empty())
system(Global.exec_on_exit.c_str());
@@ -750,6 +768,11 @@ void eu07_application::exit()
void eu07_application::render_ui()
{
if (Global.headless)
{
return;
}
if (m_modestack.empty())
{
@@ -761,6 +784,11 @@ void eu07_application::render_ui()
void eu07_application::begin_ui_frame()
{
// there is no imgui context without a window, and nothing would look at its output
if (Global.headless)
{
return;
}
if (m_modestack.empty())
{
@@ -811,6 +839,8 @@ bool eu07_application::push_mode(mode const Mode)
void eu07_application::set_title(std::string const &Title)
{
if (m_windows.empty())
return;
glfwSetWindowTitle(m_windows.front(), Title.c_str());
}
@@ -845,6 +875,8 @@ void eu07_application::set_cursor(int const Mode)
void eu07_application::set_cursor_pos(double const Horizontal, double const Vertical)
{
if (m_windows.empty())
return;
glfwSetCursorPos(m_windows.front(), Horizontal, Vertical);
}
@@ -1006,7 +1038,17 @@ std::string eu07_application::describe_monitor(GLFWmonitor *monitor) const
bool eu07_application::needs_ogl() const
{
return !Global.NvRenderer;
return !Global.NvRenderer && !Global.headless;
}
// there is no window to be closed when running without one, so the only way out is being
// asked to leave
bool eu07_application::should_close() const
{
if (Global.headless)
return Global.applicationQuitOrder;
return m_windows.empty() || glfwWindowShouldClose(m_windows.front());
}
void eu07_application::init_debug()
@@ -1173,6 +1215,9 @@ void print_usage(std::string const &Executable)
<< EU07_DEFAULT_NETWORK_PORT << ")\n"
<< " --connect <address:port> join a multiplayer session (default port "
<< EU07_DEFAULT_NETWORK_PORT << ")\n"
<< " --nick <name> name to be known by in the session\n"
<< " --nogui no window, no renderer, no cab: a dedicated\n"
<< " server that leaves the scenario to the AI\n"
<< " -h, --help this message"
<< std::endl;
}
@@ -1234,6 +1279,33 @@ int eu07_application::init_settings(int Argc, char *Argv[])
}
Global.network_client.emplace("tcp", network_endpoint(Argv[++i], "127.0.0.1"));
}
else if (token == "--nick")
{
if (i + 1 >= Argc)
{
std::cout << "--nick requires a name" << std::endl;
return -1;
}
Global.multiplayer_nickname = Argv[++i];
}
else if (token == "--nogui")
{
// no window, no renderer, no player. the log goes to the console it was
// started from, because there is nothing else to read it in
Global.headless = true;
Global.GfxRenderer = "null";
// nothing is waiting on a swapchain, so without this the loop would spin a
// core flat out for no benefit. fifty steps a second is more than the
// authoritative stream needs
Global.minframetime = std::chrono::duration<float>(1.0f / 50.0f);
Global.iWriteLogEnabled = 2;
Global.ShowSystemConsole = true;
Global.bSoundEnabled = false;
// the scenario is left to its own drivers: this process is a referee, not a
// participant, and it never walks into a cab
Global.local_start_vehicle = "ghostview";
Global.local_start_vehicle_override = true;
}
else if (token == "-h" || token == "--help")
{
print_usage(Argv[0]);
@@ -1259,6 +1331,14 @@ int eu07_application::init_locale()
int eu07_application::init_glfw()
{
if (Global.headless)
{
// no window system at all. everything downstream is guarded on an empty window
// list, which is what tells the rest of the application there is nothing to draw on
WriteLog("running without a window; this process is a dedicated server");
return 0;
}
{
int glfw_major, glfw_minor, glfw_rev;
glfwGetVersion(&glfw_major, &glfw_minor, &glfw_rev);
@@ -1440,6 +1520,9 @@ int eu07_application::init_ogl()
int eu07_application::init_ui()
{
if (Global.headless)
return 0;
if (false == ui_layer::init(m_windows.front()))
{
return -1;
@@ -1451,7 +1534,13 @@ int eu07_application::init_ui()
int eu07_application::init_gfx()
{
if (Global.GfxRenderer == "default")
if (Global.headless)
{
// draws nothing, allocates nothing, and lets the rest of the simulation run
// exactly as it otherwise would
GfxRenderer = gfx_renderer_factory::get_instance()->create("null");
}
else if (Global.GfxRenderer == "default")
{
// default render path
GfxRenderer = gfx_renderer_factory::get_instance()->create("modern");
@@ -1473,7 +1562,7 @@ int eu07_application::init_gfx()
return -1;
}
if (false == GfxRenderer->Init(m_windows.front()))
if (false == GfxRenderer->Init(m_windows.empty() ? nullptr : m_windows.front()))
{
return -1;
}
@@ -1555,7 +1644,11 @@ bool eu07_application::init_network()
// the host takes part in the session like any other peer, with an identity of its
// own, so that crew and permission handling needs no special case for it
if (!Global.network_client)
{
Global.network_peer_id = network::PEER_HOST;
auto const name = network::Peers.assign(network::PEER_HOST, network::local_nickname());
WriteLog("net: hosting as \"" + name + "\"", logtype::net);
}
}
for (auto const &pair : Global.network_servers)

View File

@@ -98,6 +98,9 @@ public:
// tells the network layer the scenario finished loading
void
network_scenario_loaded();
// says something to the rest of the session
void
say( std::string const &Text );
private:
// types
@@ -105,6 +108,9 @@ private:
using mode_stack = std::stack<mode>;
// methods
bool needs_ogl() const;
// true when the run is over: the window was closed, or, with no window, we were asked
// to stop
bool should_close() const;
void init_debug();
void init_console();
void init_files();

View File

@@ -540,6 +540,20 @@ 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))
{

View File

@@ -38,6 +38,7 @@ driver_ui::driver_ui()
add_external_panel(&m_perfgraphpanel);
add_external_panel(&m_cameraviewpanel);
add_external_panel(&m_multiplayerlobby);
add_external_panel(&m_chatpanel);
m_logpanel.is_open = false;
m_aidpanel.title = STR("Driving Aid");
@@ -54,6 +55,8 @@ driver_ui::driver_ui()
m_transcriptspanel.size_min = {435, 85};
m_transcriptspanel.size_max = {Global.fb_size.x * 0.95, Global.fb_size.y * 0.95};
m_chatpanel.title = STR("Chat");
m_multiplayerlobby.title = STR("Multiplayer lobby");
m_multiplayerlobby.size_min = {560, 320};
m_multiplayerlobby.size_max = {Global.fb_size.x * 0.95f, Global.fb_size.y * 0.95f};
@@ -93,7 +96,10 @@ void driver_ui::render_menu_contents()
m_timepanel.open();
if (Application.is_server() || Application.is_client())
{
ImGui::MenuItem(m_multiplayerlobby.name().c_str(), nullptr, &m_multiplayerlobby.is_open);
ImGui::MenuItem(m_chatpanel.name().c_str(), "`", &m_chatpanel.is_open);
}
ImGui::EndMenu();
}
@@ -104,6 +110,13 @@ void driver_ui::show_multiplayer_lobby(bool const Show)
m_multiplayerlobby.is_open = Show;
}
void driver_ui::toggle_chat()
{
m_chatpanel.is_open = !m_chatpanel.is_open;
if (m_chatpanel.is_open)
m_chatpanel.focus_input();
}
void driver_ui::showDebugUI()
{
m_debugpanel.is_open = !m_debugpanel.is_open;

View File

@@ -21,6 +21,7 @@ http://mozilla.org/MPL/2.0/.
#include "widgets/perfgraphs.h"
#include "widgets/cameraview_extcam.h"
#include "widgets/multiplayer_lobby.h"
#include "widgets/chat_panel.h"
class driver_ui : public ui_layer {
@@ -44,6 +45,9 @@ public:
// opens or closes the multiplayer lobby
void
show_multiplayer_lobby( bool const Show );
// opens or closes the session chat, putting the cursor in the box when it opens
void
toggle_chat();
protected:
void render_menu_contents() override;
@@ -74,4 +78,5 @@ private:
ui::time_panel m_timepanel;
ui::cameraview_panel m_cameraviewpanel;
ui::multiplayer_lobby_panel m_multiplayerlobby;
ui::chat_panel m_chatpanel;
};

View File

@@ -473,7 +473,9 @@ void ui_layer::render_hierarchy(){
void ui_layer::set_cursor(int const Mode)
{
glfwSetInputMode(m_window, GLFW_CURSOR, Mode);
// no window, no cursor to hide
if (m_window != nullptr)
glfwSetInputMode(m_window, GLFW_CURSOR, Mode);
m_cursorvisible = Mode != GLFW_CURSOR_DISABLED;
}