/** Start new competitor companies if possible. */
void MaybeStartNewCompany()
{
/* Count number of total existing companies. */
uint current_companies = (uint)Company::GetNumItems();
/* Count number of existing AI only companies. */
uint current_ais = 0;
Company *c;
FOR_ALL_COMPANIES(c) {
if (c->is_ai) current_ais++;
}
uint max_network_companies = _networking ? _settings_client.network.max_companies : MAX_COMPANIES;
/* Compute number of AI companies to start. */
uint ais_to_start = 0;
for (CompanyID cid = COMPANY_FIRST; cid < MAX_COMPANIES && current_companies < max_network_companies && current_ais < (uint)_settings_game.difficulty.max_no_competitors; cid++) {
if (!Company::IsValidID(cid)) {
/* Check if the next AI is also scheduled to start immediately. */
if (AIConfig::GetConfig(cid, AIConfig::SSS_FORCE_GAME)->GetSetting("start_date") == 0) {
ais_to_start++;
current_companies++;
current_ais++;
} else {
break;
}
}
}
if (ais_to_start > 0) {
/* Send a command to all clients to start up new AI(s).
* Works fine for Multiplayer and Singleplayer */
DoCommandP(0, CCA_NEW_AI | INVALID_COMPANY << 16 | ais_to_start << 24, 0, CMD_COMPANY_CTRL);
}
}