Skip to content

Commit

Permalink
Change: Allow AI companies to start immediately.
Browse files Browse the repository at this point in the history
Allow multiple AIs to possibly start in the same tick.
start_date = 0 becomes a special case, where random deviation does not occur.
If start_date was not already 0, then a minimum value of 1 must apply.
  • Loading branch information
SamuXarick authored and PeterN committed Feb 2, 2019
1 parent fa53abe commit 011257d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ai/ai.hpp
Expand Up @@ -32,7 +32,7 @@ class AI {
START_NEXT_EASY = DAYS_IN_YEAR * 2,
START_NEXT_MEDIUM = DAYS_IN_YEAR,
START_NEXT_HARD = DAYS_IN_YEAR / 2,
START_NEXT_MIN = 1,
START_NEXT_MIN = 0,
START_NEXT_MAX = 3600,
START_NEXT_DEVIATION = 60,
};
Expand Down
11 changes: 11 additions & 0 deletions src/ai/ai_config.cpp
Expand Up @@ -118,3 +118,14 @@ void AIConfig::SetSetting(const char *name, int value)

ScriptConfig::SetSetting(name, value);
}

void AIConfig::AddRandomDeviation()
{
int start_date = this->GetSetting("start_date");

ScriptConfig::AddRandomDeviation();

/* start_date = 0 is a special case, where random deviation does not occur.
* If start_date was not already 0, then a minimum value of 1 must apply. */
this->SetSetting("start_date", start_date != 0 ? max(1, this->GetSetting("start_date")) : 0);
}
1 change: 1 addition & 0 deletions src/ai/ai_config.hpp
Expand Up @@ -34,6 +34,7 @@ class AIConfig : public ScriptConfig {

/* virtual */ int GetSetting(const char *name) const;
/* virtual */ void SetSetting(const char *name, int value);
/* virtual */ void AddRandomDeviation();

/**
* When ever the AI Scanner is reloaded, all infos become invalid. This
Expand Down
18 changes: 12 additions & 6 deletions src/company_cmd.cpp
Expand Up @@ -595,10 +595,10 @@ void StartupCompanies()
}

/** Start a new competitor company if possible. */
static void MaybeStartNewCompany()
static bool MaybeStartNewCompany()
{
#ifdef ENABLE_NETWORK
if (_networking && Company::GetNumItems() >= _settings_client.network.max_companies) return;
if (_networking && Company::GetNumItems() >= _settings_client.network.max_companies) return false;
#endif /* ENABLE_NETWORK */

Company *c;
Expand All @@ -612,8 +612,10 @@ static void MaybeStartNewCompany()
if (n < (uint)_settings_game.difficulty.max_no_competitors) {
/* Send a command to all clients to start up a new AI.
* Works fine for Multiplayer and Singleplayer */
DoCommandP(0, 1 | INVALID_COMPANY << 16, 0, CMD_COMPANY_CTRL);
return DoCommandP(0, 1 | INVALID_COMPANY << 16, 0, CMD_COMPANY_CTRL);
}

return false;
}

/** Initialize the pool of companies. */
Expand Down Expand Up @@ -714,11 +716,15 @@ void OnTick_Companies()
}

if (_next_competitor_start == 0) {
_next_competitor_start = AI::GetStartNextTime() * DAY_TICKS;
/* AI::GetStartNextTime() can return 0. */
_next_competitor_start = max(1, AI::GetStartNextTime() * DAY_TICKS);
}

if (AI::CanStartNew() && _game_mode != GM_MENU && --_next_competitor_start == 0) {
MaybeStartNewCompany();
if (_game_mode != GM_MENU && AI::CanStartNew() && --_next_competitor_start == 0) {
/* Allow multiple AIs to possibly start in the same tick. */
do {
if (!MaybeStartNewCompany()) break;
} while (AI::GetStartNextTime() == 0);
}

_cur_company_tick_index = (_cur_company_tick_index + 1) % MAX_COMPANIES;
Expand Down
2 changes: 1 addition & 1 deletion src/script/script_config.hpp
Expand Up @@ -139,7 +139,7 @@ class ScriptConfig {
/**
* Randomize all settings the Script requested to be randomized.
*/
void AddRandomDeviation();
virtual void AddRandomDeviation();

/**
* Is this config attached to an Script? In other words, is there a Script
Expand Down

0 comments on commit 011257d

Please sign in to comment.