diff options
author | rubidium <rubidium@openttd.org> | 2008-12-22 18:40:57 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2008-12-22 18:40:57 +0000 |
commit | abc903d62539333ea5039ed152de07f174617a84 (patch) | |
tree | 68a45cb9d1b5a0a6ab29d8cdecc736cd6dbae7ea | |
parent | e6e32bad4d160c67f1c589edd25af423529845c2 (diff) | |
download | openttd-abc903d62539333ea5039ed152de07f174617a84.tar.xz |
(svn r14712) -Codechange: split server and client side w.r.t. the storage of network related company information.
-rw-r--r-- | src/company_cmd.cpp | 2 | ||||
-rw-r--r-- | src/console_cmds.cpp | 23 | ||||
-rw-r--r-- | src/network/core/core.cpp | 48 | ||||
-rw-r--r-- | src/network/core/core.h | 1 | ||||
-rw-r--r-- | src/network/network.cpp | 4 | ||||
-rw-r--r-- | src/network/network_client.cpp | 25 | ||||
-rw-r--r-- | src/network/network_func.h | 4 | ||||
-rw-r--r-- | src/network/network_gui.cpp | 80 | ||||
-rw-r--r-- | src/network/network_gui.h | 15 | ||||
-rw-r--r-- | src/network/network_internal.h | 2 | ||||
-rw-r--r-- | src/network/network_server.cpp | 154 | ||||
-rw-r--r-- | src/network/network_type.h | 17 | ||||
-rw-r--r-- | src/network/network_udp.cpp | 28 |
13 files changed, 210 insertions, 193 deletions
diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index 463f0194c..fd56806b6 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -863,7 +863,7 @@ CommandCost CmdCompanyCtrl(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) if (IsValidCompanyID(ci->client_playas)) { CompanyID company_backup = _local_company; - _network_company_info[c->index].months_empty = 0; + _network_company_states[c->index].months_empty = 0; /* XXX - When a client joins, we automatically set its name to the * client's name (for some reason). As it stands now only the server diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 02fe2de72..6c362d51f 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -1146,21 +1146,26 @@ DEF_CONSOLE_CMD(ConCompanies) IConsoleHelp("List the in-game details of all clients connected to the server. Usage 'companies'"); return true; } - NetworkPopulateCompanyInfo(); + NetworkCompanyStats company_stats[MAX_COMPANIES]; + NetworkPopulateCompanyStats(company_stats); FOR_ALL_COMPANIES(c) { - char buffer[512]; + /* Grab the company name */ + char company_name[NETWORK_COMPANY_NAME_LENGTH]; + SetDParam(0, c->index); + GetString(company_name, STR_COMPANY_NAME, lastof(company_name)); - const NetworkCompanyInfo *npi = &_network_company_info[c->index]; + char buffer[512]; + const NetworkCompanyStats *stats = &company_stats[c->index]; GetString(buffer, STR_00D1_DARK_BLUE + _company_colours[c->index], lastof(buffer)); IConsolePrintF(CC_INFO, "#:%d(%s) Company Name: '%s' Year Founded: %d Money: %" OTTD_PRINTF64 "d Loan: %" OTTD_PRINTF64 "d Value: %" OTTD_PRINTF64 "d (T:%d, R:%d, P:%d, S:%d) %sprotected", - c->index + 1, buffer, npi->company_name, c->inaugurated_year, (int64)c->money, (int64)c->current_loan, (int64)CalculateCompanyValue(c), - /* trains */ npi->num_vehicle[0], - /* lorry + bus */ npi->num_vehicle[1] + npi->num_vehicle[2], - /* planes */ npi->num_vehicle[3], - /* ships */ npi->num_vehicle[4], - /* protected */ StrEmpty(npi->password) ? "un" : ""); + c->index + 1, buffer, company_name, c->inaugurated_year, (int64)c->money, (int64)c->current_loan, (int64)CalculateCompanyValue(c), + /* trains */ stats->num_vehicle[0], + /* lorry + bus */ stats->num_vehicle[1] + stats->num_vehicle[2], + /* planes */ stats->num_vehicle[3], + /* ships */ stats->num_vehicle[4], + /* protected */ StrEmpty(_network_company_states[c->index].password) ? "un" : ""); } return true; diff --git a/src/network/core/core.cpp b/src/network/core/core.cpp index 1f60aba80..e989d0a50 100644 --- a/src/network/core/core.cpp +++ b/src/network/core/core.cpp @@ -8,9 +8,16 @@ #include "../../stdafx.h" #include "../../debug.h" +#include "../../company_base.h" +#include "../../strings_func.h" +#include "../../string_func.h" +#include "../../date_func.h" #include "os_abstraction.h" #include "core.h" #include "packet.h" +#include "../network_func.h" + +#include "table/strings.h" #ifdef __MORPHOS__ /* the library base is required here */ @@ -119,4 +126,45 @@ void NetworkSocketHandler::Recv_GRFIdentifier(Packet *p, GRFIdentifier *grf) } } +void NetworkSocketHandler::Send_CompanyInformation(Packet *p, const Company *c, const NetworkCompanyStats *stats) +{ + /* Grab the company name */ + char company_name[NETWORK_COMPANY_NAME_LENGTH]; + SetDParam(0, c->index); + GetString(company_name, STR_COMPANY_NAME, lastof(company_name)); + + /* Get the income */ + Money income = 0; + if (_cur_year - 1 == c->inaugurated_year) { + /* The company is here just 1 year, so display [2], else display[1] */ + for (uint i = 0; i < lengthof(c->yearly_expenses[2]); i++) { + income -= c->yearly_expenses[2][i]; + } + } else { + for (uint i = 0; i < lengthof(c->yearly_expenses[1]); i++) { + income -= c->yearly_expenses[1][i]; + } + } + + /* Send the information */ + p->Send_uint8 (c->index); + p->Send_string(company_name); + p->Send_uint32(c->inaugurated_year); + p->Send_uint64(c->old_economy[0].company_value); + p->Send_uint64(c->money); + p->Send_uint64(income); + p->Send_uint16(c->old_economy[0].performance_history); + + /* Send 1 if there is a passord for the company else send 0 */ + p->Send_bool (!StrEmpty(_network_company_states[c->index].password)); + + for (int i = 0; i < NETWORK_VEHICLE_TYPES; i++) { + p->Send_uint16(stats->num_vehicle[i]); + } + + for (int i = 0; i < NETWORK_STATION_TYPES; i++) { + p->Send_uint16(stats->num_station[i]); + } +} + #endif /* ENABLE_NETWORK */ diff --git a/src/network/core/core.h b/src/network/core/core.h index 4c5b3f9bd..272d44adc 100644 --- a/src/network/core/core.h +++ b/src/network/core/core.h @@ -74,6 +74,7 @@ public: void Send_GRFIdentifier(Packet *p, const GRFIdentifier *grf); void Recv_GRFIdentifier(Packet *p, GRFIdentifier *grf); + void Send_CompanyInformation(Packet *p, const struct Company *c, const struct NetworkCompanyStats *stats); }; #endif /* ENABLE_NETWORK */ diff --git a/src/network/network.cpp b/src/network/network.cpp index 44fd28b0b..92b002ff6 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -50,8 +50,8 @@ bool _network_available; ///< is network mode available? bool _network_dedicated; ///< are we a dedicated server? bool _is_network_server; ///< Does this client wants to be a network-server? NetworkServerGameInfo _network_game_info; -NetworkCompanyInfo _network_company_info[MAX_COMPANIES]; NetworkClientInfo _network_client_info[MAX_CLIENT_INFO]; +NetworkCompanyState _network_company_states[MAX_COMPANIES]; ClientID _network_own_client_id; ClientID _redirect_console_to_client; bool _network_need_advertise; @@ -691,7 +691,7 @@ static void NetworkInitialize() // Clean the client_info memory memset(&_network_client_info, 0, sizeof(_network_client_info)); - memset(&_network_company_info, 0, sizeof(_network_company_info)); + memset(&_network_company_states, 0, sizeof(_network_company_states)); _sync_frame = 0; _network_first_time = true; diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index 6e6006d57..d1ff4e078 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -85,7 +85,7 @@ void HashCurrentCompanyPassword(const char *password) strecpy(_password_server_unique_id, _settings_client.network.network_id, lastof(_password_server_unique_id)); const char *new_pw = GenerateCompanyPasswordHash(password); - strecpy(_network_company_info[_local_company].password, new_pw, lastof(_network_company_info[_local_company].password)); + strecpy(_network_company_states[_local_company].password, new_pw, lastof(_network_company_states[_local_company].password)); } @@ -359,19 +359,22 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO) CompanyID current = (Owner)p->Recv_uint8(); if (current >= MAX_COMPANIES) return NETWORK_RECV_STATUS_CLOSE_QUERY; - p->Recv_string(_network_company_info[current].company_name, sizeof(_network_company_info[current].company_name)); - _network_company_info[current].inaugurated_year = p->Recv_uint32(); - _network_company_info[current].company_value = p->Recv_uint64(); - _network_company_info[current].money = p->Recv_uint64(); - _network_company_info[current].income = p->Recv_uint64(); - _network_company_info[current].performance = p->Recv_uint16(); - _network_company_info[current].use_password = p->Recv_bool(); + NetworkCompanyInfo *company_info = GetLobbyCompanyInfo(current); + if (company_info == NULL) return NETWORK_RECV_STATUS_CLOSE_QUERY; + + p->Recv_string(company_info->company_name, sizeof(company_info->company_name)); + company_info->inaugurated_year = p->Recv_uint32(); + company_info->company_value = p->Recv_uint64(); + company_info->money = p->Recv_uint64(); + company_info->income = p->Recv_uint64(); + company_info->performance = p->Recv_uint16(); + company_info->use_password = p->Recv_bool(); for (int i = 0; i < NETWORK_VEHICLE_TYPES; i++) - _network_company_info[current].num_vehicle[i] = p->Recv_uint16(); + company_info->num_vehicle[i] = p->Recv_uint16(); for (int i = 0; i < NETWORK_STATION_TYPES; i++) - _network_company_info[current].num_station[i] = p->Recv_uint16(); + company_info->num_station[i] = p->Recv_uint16(); - p->Recv_string(_network_company_info[current].clients, sizeof(_network_company_info[current].clients)); + p->Recv_string(company_info->clients, sizeof(company_info->clients)); InvalidateWindow(WC_NETWORK_WINDOW, 0); diff --git a/src/network/network_func.h b/src/network/network_func.h index 688015263..0640c8a41 100644 --- a/src/network/network_func.h +++ b/src/network/network_func.h @@ -11,8 +11,8 @@ #include "../console_type.h" extern NetworkServerGameInfo _network_game_info; -extern NetworkCompanyInfo _network_company_info[MAX_COMPANIES]; extern NetworkClientInfo _network_client_info[MAX_CLIENT_INFO]; +extern NetworkCompanyState _network_company_states[MAX_COMPANIES]; extern ClientID _network_own_client_id; extern ClientID _redirect_console_to_client; @@ -34,7 +34,7 @@ void NetworkUDPGameLoop(); void NetworkUDPCloseAll(); void ParseConnectionString(const char **company, const char **port, char *connection_string); void NetworkStartDebugLog(const char *hostname, uint16 port); -void NetworkPopulateCompanyInfo(); +void NetworkPopulateCompanyStats(NetworkCompanyStats *stats); void NetworkUpdateClientInfo(ClientID client_id); bool NetworkClientConnectGame(const char *host, uint16 port); diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index df404966e..f9d972c6b 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -1136,18 +1136,6 @@ static void ShowNetworkStartServerWindow() new NetworkStartServerWindow(&_network_start_server_window_desc); } -static CompanyID NetworkLobbyFindCompanyIndex(byte pos) -{ - /* Scroll through all _network_company_info and get the 'pos' item that is not empty */ - for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) { - if (!StrEmpty(_network_company_info[i].company_name)) { - if (pos-- == 0) return i; - } - } - - return COMPANY_FIRST; -} - /** Enum for NetworkLobbyWindow, referring to _network_lobby_window_widgets */ enum NetworkLobbyWindowWidgets { NLWW_CLOSE = 0, ///< Close 'X' button @@ -1163,6 +1151,7 @@ enum NetworkLobbyWindowWidgets { struct NetworkLobbyWindow : public Window { CompanyID company; ///< Select company NetworkGameList *server; ///< Selected server + NetworkCompanyInfo company_info[MAX_COMPANIES]; NetworkLobbyWindow(const WindowDesc *desc, NetworkGameList *ngl) : Window(desc), company(INVALID_COMPANY), server(ngl) @@ -1172,6 +1161,18 @@ struct NetworkLobbyWindow : public Window { this->FindWindowPlacementAndResize(desc); } + CompanyID NetworkLobbyFindCompanyIndex(byte pos) + { + /* Scroll through all this->company_info and get the 'pos' item that is not empty */ + for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) { + if (!StrEmpty(this->company_info[i].company_name)) { + if (pos-- == 0) return i; + } + } + + return COMPANY_FIRST; + } + virtual void OnPaint() { const NetworkGameInfo *gi = &this->server->info; @@ -1197,11 +1198,11 @@ struct NetworkLobbyWindow : public Window { GfxFillRect(11, y - 1, 154, y + 10, 10); // show highlighted item with a different colour } - DoDrawStringTruncated(_network_company_info[company].company_name, 13, y, TC_BLACK, 135 - 13); - if (_network_company_info[company].use_password != 0) DrawSprite(SPR_LOCK, PAL_NONE, 135, y); + DoDrawStringTruncated(this->company_info[company].company_name, 13, y, TC_BLACK, 135 - 13); + if (this->company_info[company].use_password != 0) DrawSprite(SPR_LOCK, PAL_NONE, 135, y); /* If the company's income was positive puts a green dot else a red dot */ - if (_network_company_info[company].income >= 0) income = true; + if (this->company_info[company].income >= 0) income = true; DrawSprite(SPR_BLOT, income ? PALETTE_TO_GREEN : PALETTE_TO_RED, 145, y); pos++; @@ -1212,7 +1213,7 @@ struct NetworkLobbyWindow : public Window { /* Draw info about selected company when it is selected in the left window */ GfxFillRect(174, 39, 403, 75, 157); DrawStringCentered(290, 50, STR_NETWORK_COMPANY_INFO, TC_FROMSTRING); - if (this->company != INVALID_COMPANY) { + if (this->company != INVALID_COMPANY && !StrEmpty(this->company_info[this->company].company_name)) { const uint x = 183; const uint trunc_width = this->widget[NLWW_DETAILS].right - x; y = 80; @@ -1224,47 +1225,47 @@ struct NetworkLobbyWindow : public Window { DrawString(x, y, STR_NETWORK_CLIENTS, TC_GOLD); y += 10; - SetDParamStr(0, _network_company_info[this->company].company_name); + SetDParamStr(0, this->company_info[this->company].company_name); DrawStringTruncated(x, y, STR_NETWORK_COMPANY_NAME, TC_GOLD, trunc_width); y += 10; - SetDParam(0, _network_company_info[this->company].inaugurated_year); + SetDParam(0, this->company_info[this->company].inaugurated_year); DrawString(x, y, STR_NETWORK_INAUGURATION_YEAR, TC_GOLD); // inauguration year y += 10; - SetDParam(0, _network_company_info[this->company].company_value); + SetDParam(0, this->company_info[this->company].company_value); DrawString(x, y, STR_NETWORK_VALUE, TC_GOLD); // company value y += 10; - SetDParam(0, _network_company_info[this->company].money); + SetDParam(0, this->company_info[this->company].money); DrawString(x, y, STR_NETWORK_CURRENT_BALANCE, TC_GOLD); // current balance y += 10; - SetDParam(0, _network_company_info[this->company].income); + SetDParam(0, this->company_info[this->company].income); DrawString(x, y, STR_NETWORK_LAST_YEARS_INCOME, TC_GOLD); // last year's income y += 10; - SetDParam(0, _network_company_info[this->company].performance); + SetDParam(0, this->company_info[this->company].performance); DrawString(x, y, STR_NETWORK_PERFORMANCE, TC_GOLD); // performance y += 10; - SetDParam(0, _network_company_info[this->company].num_vehicle[0]); - SetDParam(1, _network_company_info[this->company].num_vehicle[1]); - SetDParam(2, _network_company_info[this->company].num_vehicle[2]); - SetDParam(3, _network_company_info[this->company].num_vehicle[3]); - SetDParam(4, _network_company_info[this->company].num_vehicle[4]); + SetDParam(0, this->company_info[this->company].num_vehicle[0]); + SetDParam(1, this->company_info[this->company].num_vehicle[1]); + SetDParam(2, this->company_info[this->company].num_vehicle[2]); + SetDParam(3, this->company_info[this->company].num_vehicle[3]); + SetDParam(4, this->company_info[this->company].num_vehicle[4]); DrawString(x, y, STR_NETWORK_VEHICLES, TC_GOLD); // vehicles y += 10; - SetDParam(0, _network_company_info[this->company].num_station[0]); - SetDParam(1, _network_company_info[this->company].num_station[1]); - SetDParam(2, _network_company_info[this->company].num_station[2]); - SetDParam(3, _network_company_info[this->company].num_station[3]); - SetDParam(4, _network_company_info[this->company].num_station[4]); + SetDParam(0, this->company_info[this->company].num_station[0]); + SetDParam(1, this->company_info[this->company].num_station[1]); + SetDParam(2, this->company_info[this->company].num_station[2]); + SetDParam(3, this->company_info[this->company].num_station[3]); + SetDParam(4, this->company_info[this->company].num_station[4]); DrawString(x, y, STR_NETWORK_STATIONS, TC_GOLD); // stations y += 10; - SetDParamStr(0, _network_company_info[this->company].clients); + SetDParamStr(0, this->company_info[this->company].clients); DrawStringTruncated(x, y, STR_NETWORK_PLAYERS, TC_GOLD, trunc_width); // players } } @@ -1306,6 +1307,8 @@ struct NetworkLobbyWindow : public Window { case NLWW_REFRESH: // Refresh NetworkTCPQueryServer(_settings_client.network.last_host, _settings_client.network.last_port); // company info NetworkUDPQueryServer(_settings_client.network.last_host, _settings_client.network.last_port); // general data + /* Clear the information so removed companies don't remain */ + memset(this->company_info, 0, sizeof(company_info)); break; } } @@ -1362,6 +1365,17 @@ static void ShowNetworkLobbyWindow(NetworkGameList *ngl) new NetworkLobbyWindow(&_network_lobby_window_desc, ngl); } +/** + * Get the company information of a given company to fill for the lobby. + * @param company the company to get the company info struct from. + * @return the company info struct to write the (downloaded) data to. + */ +NetworkCompanyInfo *GetLobbyCompanyInfo(CompanyID company) +{ + NetworkLobbyWindow *lobby = dynamic_cast<NetworkLobbyWindow*>(FindWindowById(WC_NETWORK_WINDOW, 0)); + return (lobby != NULL && company < MAX_COMPANIES) ? &lobby->company_info[company] : NULL; +} + // The window below gives information about the connected clients // and also makes able to give money to them, kick them (if server) // and stuff like that. diff --git a/src/network/network_gui.h b/src/network/network_gui.h index d0a5b4996..ee73cd0b9 100644 --- a/src/network/network_gui.h +++ b/src/network/network_gui.h @@ -18,6 +18,21 @@ void ShowNetworkGameWindow(); void ShowClientList(); void ShowNetworkCompanyPasswordWindow(Window *parent); + +/** Company information stored at the client side */ +struct NetworkCompanyInfo : NetworkCompanyStats { + char company_name[NETWORK_COMPANY_NAME_LENGTH]; ///< Company name + Year inaugurated_year; ///< What year the company started in + Money company_value; ///< The company value + Money money; ///< The amount of money the company has + Money income; ///< How much did the company earned last year + uint16 performance; ///< What was his performance last month? + bool use_password; ///< Is there a password + char clients[NETWORK_CLIENTS_LENGTH]; ///< The clients that control this company (Name1, name2, ..) +}; + +NetworkCompanyInfo *GetLobbyCompanyInfo(CompanyID company); + #else /* ENABLE_NETWORK */ /* Network function stubs when networking is disabled */ diff --git a/src/network/network_internal.h b/src/network/network_internal.h index 0508e4eb8..10107d852 100644 --- a/src/network/network_internal.h +++ b/src/network/network_internal.h @@ -91,8 +91,6 @@ enum NetworkLanguage { NETLANG_COUNT }; -extern NetworkCompanyInfo _network_company_info[MAX_COMPANIES]; - extern uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode extern uint32 _frame_counter_max; // To where we may go with our clients diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index b4c8ef9d5..eb371443d 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -69,40 +69,52 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_COMPANY_INFO) // Data: // - Company *company; - Packet *p; + /* Fetch the latest version of the stats */ + NetworkCompanyStats company_stats[MAX_COMPANIES]; + NetworkPopulateCompanyStats(company_stats); - NetworkPopulateCompanyInfo(); + /* Make a list of all clients per company */ + char clients[MAX_COMPANIES][NETWORK_CLIENTS_LENGTH]; + NetworkTCPSocketHandler *csi; + memset(clients, 0, sizeof(clients)); - FOR_ALL_COMPANIES(company) { - p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO); + /* Add the local player (if not dedicated) */ + const NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER); + if (ci != NULL && IsValidCompanyID(ci->client_playas)) { + strecpy(clients[ci->client_playas], ci->client_name, lastof(clients[ci->client_playas])); + } - p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION); - p->Send_bool (true); - p->Send_uint8 (company->index); + FOR_ALL_CLIENTS(csi) { + char client_name[NETWORK_CLIENT_NAME_LENGTH]; - p->Send_string(_network_company_info[company->index].company_name); - p->Send_uint32(_network_company_info[company->index].inaugurated_year); - p->Send_uint64(_network_company_info[company->index].company_value); - p->Send_uint64(_network_company_info[company->index].money); - p->Send_uint64(_network_company_info[company->index].income); - p->Send_uint16(_network_company_info[company->index].performance); + NetworkGetClientName(client_name, sizeof(client_name), csi); - /* Send 1 if there is a passord for the company else send 0 */ - p->Send_bool(!StrEmpty(_network_company_info[company->index].password)); + ci = DEREF_CLIENT_INFO(csi); + if (ci != NULL && IsValidCompanyID(ci->client_playas)) { + if (!StrEmpty(clients[ci->client_playas])) { + strecat(clients[ci->client_playas], ", ", lastof(clients[ci->client_playas])); + } - for (int i = 0; i < NETWORK_VEHICLE_TYPES; i++) { - p->Send_uint16(_network_company_info[company->index].num_vehicle[i]); + strecat(clients[ci->client_playas], client_name, lastof(clients[ci->client_playas])); } + } - for (int i = 0; i < NETWORK_STATION_TYPES; i++) { - p->Send_uint16(_network_company_info[company->index].num_station[i]); - } + /* Now send the data */ - if (StrEmpty(_network_company_info[company->index].clients)) { + Company *company; + Packet *p; + + FOR_ALL_COMPANIES(company) { + p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO); + + p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION); + p->Send_bool (true); + cs->Send_CompanyInformation(p, company, &company_stats[company->index]); + + if (StrEmpty(clients[company->index])) { p->Send_string("<none>"); } else { - p->Send_string(_network_company_info[company->index].clients); + p->Send_string(clients[company->index]); } cs->Send_Packet(p); @@ -603,7 +615,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED) if (!StrEmpty(_settings_client.network.server_password)) { SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_GAME_PASSWORD); } else { - if (IsValidCompanyID(ci->client_playas) && _network_company_info[ci->client_playas].password[0] != '\0') { + if (IsValidCompanyID(ci->client_playas) && !StrEmpty(_network_company_states[ci->client_playas].password)) { SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD); } else { SEND_COMMAND(PACKET_SERVER_WELCOME)(cs); @@ -681,7 +693,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN) ci->client_lang = client_lang; /* Make sure companies to which people try to join are not autocleaned */ - if (IsValidCompanyID(playas)) _network_company_info[playas].months_empty = 0; + if (IsValidCompanyID(playas)) _network_company_states[playas].months_empty = 0; if (_grfconfig == NULL) { RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)(cs, NULL); @@ -709,7 +721,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD) ci = DEREF_CLIENT_INFO(cs); - if (IsValidCompanyID(ci->client_playas) && _network_company_info[ci->client_playas].password[0] != '\0') { + if (IsValidCompanyID(ci->client_playas) && !StrEmpty(_network_company_states[ci->client_playas].password)) { SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD); return; } @@ -720,7 +732,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD) } else if (cs->status == STATUS_AUTHORIZING && type == NETWORK_COMPANY_PASSWORD) { ci = DEREF_CLIENT_INFO(cs); - if (strcmp(password, _network_company_info[ci->client_playas].password) != 0) { + if (strcmp(password, _network_company_states[ci->client_playas].password) != 0) { // Password is invalid SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD); return; @@ -1180,7 +1192,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD) ci = DEREF_CLIENT_INFO(cs); if (IsValidCompanyID(ci->client_playas)) { - strecpy(_network_company_info[ci->client_playas].password, password, lastof(_network_company_info[ci->client_playas].password)); + strecpy(_network_company_states[ci->client_playas].password, password, lastof(_network_company_states[ci->client_playas].password)); } } @@ -1281,54 +1293,18 @@ static NetworkServerPacket* const _network_server_packet[] = { // If this fails, check the array above with network_data.h assert_compile(lengthof(_network_server_packet) == PACKET_END); -// This update the company_info-stuff -void NetworkPopulateCompanyInfo() +/** + * Populate the company stats. + * @param stats the stats to update + */ +void NetworkPopulateCompanyStats(NetworkCompanyStats *stats) { - char password[NETWORK_PASSWORD_LENGTH]; - const Company *c; const Vehicle *v; const Station *s; - NetworkTCPSocketHandler *cs; - const NetworkClientInfo *ci; - uint i; - uint16 months_empty; - - for (CompanyID cid = COMPANY_FIRST; cid < MAX_COMPANIES; cid++) { - if (!IsValidCompanyID(cid)) memset(&_network_company_info[cid], 0, sizeof(NetworkCompanyInfo)); - } - - FOR_ALL_COMPANIES(c) { - // Clean the info but not the password - strecpy(password, _network_company_info[c->index].password, lastof(password)); - months_empty = _network_company_info[c->index].months_empty; - memset(&_network_company_info[c->index], 0, sizeof(NetworkCompanyInfo)); - _network_company_info[c->index].months_empty = months_empty; - strecpy(_network_company_info[c->index].password, password, lastof(_network_company_info[c->index].password)); - - // Grap the company name - SetDParam(0, c->index); - GetString(_network_company_info[c->index].company_name, STR_COMPANY_NAME, lastof(_network_company_info[c->index].company_name)); - - // Check the income - if (_cur_year - 1 == c->inaugurated_year) { - // The company is here just 1 year, so display [2], else display[1] - for (i = 0; i < lengthof(c->yearly_expenses[2]); i++) { - _network_company_info[c->index].income -= c->yearly_expenses[2][i]; - } - } else { - for (i = 0; i < lengthof(c->yearly_expenses[1]); i++) { - _network_company_info[c->index].income -= c->yearly_expenses[1][i]; - } - } - // Set some general stuff - _network_company_info[c->index].inaugurated_year = c->inaugurated_year; - _network_company_info[c->index].company_value = c->old_economy[0].company_value; - _network_company_info[c->index].money = c->money; - _network_company_info[c->index].performance = c->old_economy[0].performance_history; - } + memset(stats, 0, sizeof(*stats) * MAX_COMPANIES); - // Go through all vehicles and count the type of vehicles + /* Go through all vehicles and count the type of vehicles */ FOR_ALL_VEHICLES(v) { if (!IsValidCompanyID(v->owner) || !v->IsPrimaryVehicle()) continue; byte type = 0; @@ -1339,13 +1315,13 @@ void NetworkPopulateCompanyInfo() case VEH_SHIP: type = 4; break; default: continue; } - _network_company_info[v->owner].num_vehicle[type]++; + stats[v->owner].num_vehicle[type]++; } // Go through all stations and count the types of stations FOR_ALL_STATIONS(s) { if (IsValidCompanyID(s->owner)) { - NetworkCompanyInfo *npi = &_network_company_info[s->owner]; + NetworkCompanyStats *npi = &stats[s->owner]; if (s->facilities & FACIL_TRAIN) npi->num_station[0]++; if (s->facilities & FACIL_TRUCK_STOP) npi->num_station[1]++; @@ -1354,26 +1330,6 @@ void NetworkPopulateCompanyInfo() if (s->facilities & FACIL_DOCK) npi->num_station[4]++; } } - - ci = NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER); - // Register local company (if not dedicated) - if (ci != NULL && IsValidCompanyID(ci->client_playas)) - strecpy(_network_company_info[ci->client_playas].clients, ci->client_name, lastof(_network_company_info[ci->client_playas].clients)); - - FOR_ALL_CLIENTS(cs) { - char client_name[NETWORK_CLIENT_NAME_LENGTH]; - - NetworkGetClientName(client_name, sizeof(client_name), cs); - - ci = DEREF_CLIENT_INFO(cs); - if (ci != NULL && IsValidCompanyID(ci->client_playas)) { - if (!StrEmpty(_network_company_info[ci->client_playas].clients)) { - strecat(_network_company_info[ci->client_playas].clients, ", ", lastof(_network_company_info[ci->client_playas].clients)); - } - - strecat(_network_company_info[ci->client_playas].clients, client_name, lastof(_network_company_info[ci->client_playas].clients)); - } - } } // Send a packet to all clients with updated info about this client_id @@ -1433,24 +1389,24 @@ static void NetworkAutoCleanCompanies() if (!clients_in_company[c->index]) { /* The company is empty for one month more */ - _network_company_info[c->index].months_empty++; + _network_company_states[c->index].months_empty++; /* Is the company empty for autoclean_unprotected-months, and is there no protection? */ - if (_settings_client.network.autoclean_unprotected != 0 && _network_company_info[c->index].months_empty > _settings_client.network.autoclean_unprotected && StrEmpty(_network_company_info[c->index].password)) { + if (_settings_client.network.autoclean_unprotected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_unprotected && StrEmpty(_network_company_states[c->index].password)) { /* Shut the company down */ DoCommandP(0, 2, c->index, NULL, CMD_COMPANY_CTRL); IConsolePrintF(CC_DEFAULT, "Auto-cleaned company #%d", c->index + 1); } /* Is the company empty for autoclean_protected-months, and there is a protection? */ - if (_settings_client.network.autoclean_protected != 0 && _network_company_info[c->index].months_empty > _settings_client.network.autoclean_protected && !StrEmpty(_network_company_info[c->index].password)) { + if (_settings_client.network.autoclean_protected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_protected && !StrEmpty(_network_company_states[c->index].password)) { /* Unprotect the company */ - _network_company_info[c->index].password[0] = '\0'; + _network_company_states[c->index].password[0] = '\0'; IConsolePrintF(CC_DEFAULT, "Auto-removed protection from company #%d", c->index + 1); - _network_company_info[c->index].months_empty = 0; + _network_company_states[c->index].months_empty = 0; } } else { /* It is not empty, reset the date */ - _network_company_info[c->index].months_empty = 0; + _network_company_states[c->index].months_empty = 0; } } } diff --git a/src/network/network_type.h b/src/network/network_type.h index 1f14607d8..3c5365d74 100644 --- a/src/network/network_type.h +++ b/src/network/network_type.h @@ -41,18 +41,15 @@ enum ClientIndex { MAX_CLIENT_INFO = MAX_CLIENTS + 1, }; -struct NetworkCompanyInfo { - char company_name[NETWORK_COMPANY_NAME_LENGTH]; ///< Company name - char password[NETWORK_PASSWORD_LENGTH]; ///< The password for the company - Year inaugurated_year; ///< What year the company started in - Money company_value; ///< The company value - Money money; ///< The amount of money the company has - Money income; ///< How much did the company earned last year - uint16 performance; ///< What was his performance last month? - bool use_password; ///< Is there a password +/** Simple calculated statistics of a company */ +struct NetworkCompanyStats { uint16 num_vehicle[NETWORK_VEHICLE_TYPES]; ///< How many vehicles are there of this type? uint16 num_station[NETWORK_STATION_TYPES]; ///< How many stations are there of this type? - char clients[NETWORK_CLIENTS_LENGTH]; ///< The clients that control this company (Name1, name2, ..) +}; + +/** Some state information of a company, especially for servers */ +struct NetworkCompanyState { + char password[NETWORK_PASSWORD_LENGTH]; ///< The password for the company uint16 months_empty; ///< How many months the company is empty }; diff --git a/src/network/network_udp.cpp b/src/network/network_udp.cpp index 210a117ad..430eee925 100644 --- a/src/network/network_udp.cpp +++ b/src/network/network_udp.cpp @@ -118,35 +118,15 @@ DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_DETAIL_INFO) packet.Send_uint8 (NETWORK_COMPANY_INFO_VERSION); packet.Send_uint8 (ActiveCompanyCount()); - /* Fetch the latest version of everything */ - NetworkPopulateCompanyInfo(); + /* Fetch the latest version of the stats */ + NetworkCompanyStats company_stats[MAX_COMPANIES]; + NetworkPopulateCompanyStats(company_stats); Company *company; - byte current = 0; /* Go through all the companies */ FOR_ALL_COMPANIES(company) { - current++; - /* Send the information */ - packet.Send_uint8 (current); - - packet.Send_string(_network_company_info[company->index].company_name); - packet.Send_uint32(_network_company_info[company->index].inaugurated_year); - packet.Send_uint64(_network_company_info[company->index].company_value); - packet.Send_uint64(_network_company_info[company->index].money); - packet.Send_uint64(_network_company_info[company->index].income); - packet.Send_uint16(_network_company_info[company->index].performance); - - /* Send 1 if there is a passord for the company else send 0 */ - packet.Send_bool (!StrEmpty(_network_company_info[company->index].password)); - - for (int i = 0; i < NETWORK_VEHICLE_TYPES; i++) { - packet.Send_uint16(_network_company_info[company->index].num_vehicle[i]); - } - - for (int i = 0; i < NETWORK_STATION_TYPES; i++) { - packet.Send_uint16(_network_company_info[company->index].num_station[i]); - } + this->Send_CompanyInformation(&packet, company, &company_stats[company->index]); } this->SendPacket(&packet, client_addr); |