From 5401ab1f7bdc69385dbf19479da1fbe3e0ab0ab2 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Mon, 10 Feb 2025 18:03:01 +0100 Subject: [PATCH] Codechange: use ReferenceThroughBaseContainer for containers that are index by CompanyID --- src/company_cmd.cpp | 2 +- src/company_func.h | 2 +- src/economy.cpp | 4 ++-- src/economy_func.h | 2 +- src/network/network_admin.cpp | 3 +-- src/network/network_func.h | 4 +++- src/network/network_server.cpp | 9 +++++---- src/script/api/script_object.cpp | 2 +- src/script/api/script_object.hpp | 3 ++- src/settings_type.h | 2 +- src/smallmap_gui.cpp | 2 +- src/station_cmd.cpp | 4 ++-- src/town.h | 4 ++-- 13 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index 395687c024..ba48fec40e 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -51,7 +51,7 @@ void UpdateObjectColours(const Company *c); CompanyID _local_company; ///< Company controlled by the human player at this client. Can also be #COMPANY_SPECTATOR. CompanyID _current_company; ///< Company currently doing an action. -Colours _company_colours[MAX_COMPANIES]; ///< NOSAVE: can be determined from company structs. +ReferenceThroughBaseContainer> _company_colours; ///< NOSAVE: can be determined from company structs. CompanyManagerFace _company_manager_face; ///< for company manager face storage in openttd.cfg uint _cur_company_tick_index; ///< used to generate a name for one company that doesn't have a name yet per tick diff --git a/src/company_func.h b/src/company_func.h index 5fc6c0d1df..870a292424 100644 --- a/src/company_func.h +++ b/src/company_func.h @@ -36,7 +36,7 @@ CommandCost CheckTileOwnership(TileIndex tile); extern CompanyID _local_company; extern CompanyID _current_company; -extern Colours _company_colours[MAX_COMPANIES]; +extern ReferenceThroughBaseContainer> _company_colours; extern CompanyManagerFace _company_manager_face; /** diff --git a/src/economy.cpp b/src/economy.cpp index 6417a788eb..f179bcb6c3 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -100,7 +100,7 @@ const ScoreInfo _score_info[] = { { 0, 0} // SCORE_TOTAL }; -int64_t _score_part[MAX_COMPANIES][SCORE_END]; +ReferenceThroughBaseContainer, MAX_COMPANIES>> _score_part; Economy _economy; Prices _price; static PriceMultipliers _price_base_multiplier; @@ -203,7 +203,7 @@ int UpdateCompanyRatingAndValue(Company *c, bool update) Owner owner = c->index; int score = 0; - memset(_score_part[owner], 0, sizeof(_score_part[owner])); + _score_part[owner] = {}; /* Count vehicles */ { diff --git a/src/economy_func.h b/src/economy_func.h index 993b88add2..48b8aeb8f2 100644 --- a/src/economy_func.h +++ b/src/economy_func.h @@ -23,7 +23,7 @@ void ResetPriceBaseMultipliers(); void SetPriceBaseMultiplier(Price price, int factor); extern const ScoreInfo _score_info[]; -extern int64_t _score_part[MAX_COMPANIES][SCORE_END]; +extern ReferenceThroughBaseContainer, MAX_COMPANIES>> _score_part; extern Economy _economy; /* Prices and also the fractional part. */ extern Prices _price; diff --git a/src/network/network_admin.cpp b/src/network/network_admin.cpp index e8f6b78a4a..4a9132381d 100644 --- a/src/network/network_admin.cpp +++ b/src/network/network_admin.cpp @@ -407,8 +407,7 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCompanyEconomy() NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCompanyStats() { /* Fetch the latest version of the stats. */ - NetworkCompanyStats company_stats[MAX_COMPANIES]; - NetworkPopulateCompanyStats(company_stats); + NetworkCompanyStatsArray company_stats = NetworkGetCompanyStats(); /* Go through all the companies. */ for (const Company *company : Company::Iterate()) { diff --git a/src/network/network_func.h b/src/network/network_func.h index df87ff71bc..037a0deea1 100644 --- a/src/network/network_func.h +++ b/src/network/network_func.h @@ -18,6 +18,7 @@ // #define DEBUG_FAILED_DUMP_COMMANDS #include "network_type.h" +#include "../core/convertible_through_base.hpp" #include "../console_type.h" #include "../gfx_type.h" #include "../openttd.h" @@ -44,7 +45,8 @@ void NetworkDisconnect(bool close_admins = true); void NetworkGameLoop(); void NetworkBackgroundLoop(); std::string_view ParseFullConnectionString(const std::string &connection_string, uint16_t &port, CompanyID *company_id = nullptr); -void NetworkPopulateCompanyStats(NetworkCompanyStats *stats); +using NetworkCompanyStatsArray = ReferenceThroughBaseContainer>; +NetworkCompanyStatsArray NetworkGetCompanyStats(); void NetworkUpdateClientInfo(ClientID client_id); void NetworkClientsToSpectators(CompanyID cid); diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 9cfecda341..adde5230fd 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -1485,12 +1485,11 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_MOVE(Packet &p) } /** - * Populate the company stats. - * @param stats the stats to update + * Get the company stats. */ -void NetworkPopulateCompanyStats(NetworkCompanyStats *stats) +NetworkCompanyStatsArray NetworkGetCompanyStats() { - memset(stats, 0, sizeof(*stats) * MAX_COMPANIES); + NetworkCompanyStatsArray stats = {}; /* Go through all vehicles and count the type of vehicles */ for (const Vehicle *v : Vehicle::Iterate()) { @@ -1518,6 +1517,8 @@ void NetworkPopulateCompanyStats(NetworkCompanyStats *stats) if (s->facilities.Test(StationFacility::Dock)) npi->num_station[NETWORK_VEH_SHIP]++; } } + + return stats; } /** diff --git a/src/script/api/script_object.cpp b/src/script/api/script_object.cpp index 507ea47888..961a766416 100644 --- a/src/script/api/script_object.cpp +++ b/src/script/api/script_object.cpp @@ -347,7 +347,7 @@ bool ScriptObject::DoCommandProcessResult(const CommandCost &res, Script_Suspend } -/* static */ Randomizer ScriptObject::random_states[OWNER_END]; +/* static */ ScriptObject::RandomizerArray ScriptObject::random_states; Randomizer &ScriptObject::GetRandomizer(Owner owner) { diff --git a/src/script/api/script_object.hpp b/src/script/api/script_object.hpp index 3ca05ece2f..796aed9459 100644 --- a/src/script/api/script_object.hpp +++ b/src/script/api/script_object.hpp @@ -330,7 +330,8 @@ private: static std::tuple DoCommandPrep(); static bool DoCommandProcessResult(const CommandCost &res, Script_SuspendCallbackProc *callback, bool estimate_only, bool asynchronous); static CommandCallbackData *GetDoCommandCallback(); - static Randomizer random_states[OWNER_END]; ///< Random states for each of the scripts (game script uses OWNER_DEITY) + using RandomizerArray = ReferenceThroughBaseContainer>; + static RandomizerArray random_states; ///< Random states for each of the scripts (game script uses OWNER_DEITY) }; namespace ScriptObjectInternal { diff --git a/src/settings_type.h b/src/settings_type.h index a013cdcdff..631f03d1dd 100644 --- a/src/settings_type.h +++ b/src/settings_type.h @@ -602,7 +602,7 @@ struct GameSettings { ConstructionSettings construction; ///< construction of things in-game AISettings ai; ///< what may the AI do? ScriptSettings script; ///< settings for scripts - class AIConfig *ai_config[MAX_COMPANIES]; ///< settings per company + ReferenceThroughBaseContainer> ai_config; ///< settings per company class GameConfig *game_config; ///< settings for gamescript PathfinderSettings pf; ///< settings for all pathfinders OrderSettings order; ///< settings related to orders diff --git a/src/smallmap_gui.cpp b/src/smallmap_gui.cpp index eb2d551dd4..a0cf7b01da 100644 --- a/src/smallmap_gui.cpp +++ b/src/smallmap_gui.cpp @@ -181,7 +181,7 @@ static IndustryType _smallmap_industry_highlight = IT_INVALID; /** State of highlight blinking */ static bool _smallmap_industry_highlight_state; /** For connecting company ID to position in owner list (small map legend) */ -static uint _company_to_list_pos[MAX_COMPANIES]; +static ReferenceThroughBaseContainer> _company_to_list_pos; /** * Fills an array for the industries legends. diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index c0c3eae90d..1199f23ba0 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -4426,8 +4426,8 @@ uint MoveGoodsToStation(CargoType type, uint amount, Source source, const Statio return UpdateStationWaiting(first_station, type, amount, source); } - uint company_best[OWNER_NONE + 1] = {}; // best rating for each company, including OWNER_NONE - uint company_sum[OWNER_NONE + 1] = {}; // sum of ratings for each company + ReferenceThroughBaseContainer> company_best = {}; // best rating for each company, including OWNER_NONE + ReferenceThroughBaseContainer> company_sum = {}; // sum of ratings for each company uint best_rating = 0; uint best_sum = 0; // sum of best ratings for each company diff --git a/src/town.h b/src/town.h index 674b6b335a..1e4ce1120d 100644 --- a/src/town.h +++ b/src/town.h @@ -69,10 +69,10 @@ struct Town : TownPool::PoolItem<&_town_pool> { /* Company ratings. */ CompanyMask have_ratings; ///< which companies have a rating - uint8_t unwanted[MAX_COMPANIES]; ///< how many months companies aren't wanted by towns (bribe) + ReferenceThroughBaseContainer> unwanted; ///< how many months companies aren't wanted by towns (bribe) CompanyID exclusivity; ///< which company has exclusivity uint8_t exclusive_counter; ///< months till the exclusivity expires - int16_t ratings[MAX_COMPANIES]; ///< ratings of each company for this town + ReferenceThroughBaseContainer> ratings; ///< ratings of each company for this town TransportedCargoStat supplied[NUM_CARGO]; ///< Cargo statistics about supplied cargo. TransportedCargoStat received[NUM_TAE]; ///< Cargo statistics about received cargotypes.