From 455e202e0395bf88cc61faaa13d502cc49baf1b2 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 5 May 2024 19:53:21 +0200 Subject: [PATCH] Fix: server's client is shown incorrectly in some cases --- src/network/network.cpp | 26 +++++++++++++++++++++++++- src/network/network_func.h | 2 ++ src/openttd.cpp | 11 ++--------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/network/network.cpp b/src/network/network.cpp index 5eb64da827..899d2a7396 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -884,7 +884,7 @@ static void NetworkInitGameInfo() /* There should be always space for the server. */ assert(NetworkClientInfo::CanAllocateItem()); NetworkClientInfo *ci = new NetworkClientInfo(CLIENT_ID_SERVER); - ci->client_playas = _network_dedicated ? COMPANY_SPECTATOR : COMPANY_FIRST; + ci->client_playas = COMPANY_SPECTATOR; ci->client_name = _settings_client.network.client_name; } @@ -987,6 +987,30 @@ bool NetworkServerStart() return true; } +/** + * Perform tasks when the server is started. This consists of things + * like putting the server's client in a valid company and resetting the restart time. + */ +void NetworkOnGameStart() +{ + if (!_network_server) return; + + /* Update the static game info to set the values from the new game. */ + NetworkServerUpdateGameInfo(); + + ChangeNetworkRestartTime(true); + + if (!_network_dedicated) { + Company *c = Company::GetIfValid(GetFirstPlayableCompanyID()); + NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER); + if (c != nullptr && ci != nullptr) { + ci->client_playas = c->index; + } + + ShowClientList(); + } +} + /* The server is rebooting... * The only difference with NetworkDisconnect, is the packets that is sent */ void NetworkReboot() diff --git a/src/network/network_func.h b/src/network/network_func.h index 4a31943e63..91af613c04 100644 --- a/src/network/network_func.h +++ b/src/network/network_func.h @@ -64,6 +64,8 @@ void NetworkPrintClients(); std::string_view NetworkGetPublicKeyOfClient(ClientID client_id); void NetworkHandlePauseChange(PauseMode prev_mode, PauseMode changed_mode); +void NetworkOnGameStart(); + /*** Commands ran by the server ***/ void NetworkServerSendConfigUpdate(); void NetworkServerUpdateGameInfo(); diff --git a/src/openttd.cpp b/src/openttd.cpp index 0d9ef39c0d..10f7e0727b 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -34,7 +34,6 @@ #include "console_func.h" #include "screenshot.h" #include "network/network.h" -#include "network/network_server.h" #include "network/network_func.h" #include "ai/ai.hpp" #include "ai/ai_config.hpp" @@ -859,8 +858,8 @@ static void OnStartGame(bool dedicated_server) * or in the case of a dedicated server, a spectator */ SetLocalCompany(dedicated_server ? COMPANY_SPECTATOR : GetFirstPlayableCompanyID()); - /* Update the static game info to set the values from the new game. */ - NetworkServerUpdateGameInfo(); + NetworkOnGameStart(); + /* Execute the game-start script */ IConsoleCmdExec("exec scripts/game_start.scr 0"); } @@ -911,12 +910,6 @@ static void MakeNewGameDone() CheckEngines(); CheckIndustries(); MarkWholeScreenDirty(); - - if (_network_server) { - ChangeNetworkRestartTime(true); - - if (!_network_dedicated) ShowClientList(); - } } static void MakeNewGame(bool from_heightmap, bool reset_settings)