From 02be6ab6ba80ecb8e370622436d5584710bd6e35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guilloux?= Date: Fri, 11 Aug 2023 22:30:01 +0200 Subject: [PATCH] Fix #11179, a979d9c: Don't start more competitors than allowed (#11185) --- src/company_cmd.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index ff125b44ea..ca6ade6039 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -600,12 +600,12 @@ TimeoutTimer _new_competitor_timeout(0, []() { if (_networking && Company::GetNumItems() >= _settings_client.network.max_companies) return; /* count number of competitors */ - uint n = 0; + uint8_t n = 0; for (const Company *c : Company::Iterate()) { if (c->is_ai) n++; } - if (n >= (uint)_settings_game.difficulty.max_no_competitors) return; + if (n >= _settings_game.difficulty.max_no_competitors) return; /* Send a command to all clients to start up a new AI. * Works fine for Multiplayer and Singleplayer */ @@ -720,8 +720,15 @@ void OnTick_Companies() int32_t timeout = _settings_game.difficulty.competitors_interval * 60 * TICKS_PER_SECOND; /* If the interval is zero, start as many competitors as needed then check every ~10 minutes if a company went bankrupt and needs replacing. */ if (timeout == 0) { + /* count number of competitors */ + uint8_t n = 0; + for (const Company *cc : Company::Iterate()) { + if (cc->is_ai) n++; + } + for (auto i = 0; i < _settings_game.difficulty.max_no_competitors; i++) { if (_networking && Company::GetNumItems() >= _settings_client.network.max_companies) break; + if (n++ >= _settings_game.difficulty.max_no_competitors) break; Command::Post(CCA_NEW_AI, INVALID_COMPANY, CRR_NONE, INVALID_CLIENT_ID); } timeout = 10 * 60 * TICKS_PER_SECOND;