Skip to content

Commit

Permalink
Fix: Enforce the max_no_competitors test before creating an AI compan…
Browse files Browse the repository at this point in the history
…y in multiplayer.
  • Loading branch information
SamuXarick committed Mar 16, 2019
1 parent c7b5f34 commit 6a222fc
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/company_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ static bool 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 */
return DoCommandP(0, CCA_NEW_AI | INVALID_COMPANY << 16, 0, CMD_COMPANY_CTRL);
return DoCommandP(0, CCA_NEW_AI | INVALID_COMPANY << 16, 1, CMD_COMPANY_CTRL);
}

return false;
Expand Down Expand Up @@ -818,6 +818,7 @@ void CompanyAdminRemove(CompanyID company_id, CompanyRemoveReason reason)
* - bits 16..24: CompanyID
* @param p2 various depending on CompanyCtrlAction
* - bits 0..31: ClientID (with CCA_NEW)
* - bit 0: If set to 1, enforce the max_no_competitors test (with CCA_NEW_AI)
* - bits 0..1: CompanyRemoveReason (with CCA_DELETE)
* @param text unused
* @return the cost of this operation or an error
Expand Down Expand Up @@ -881,6 +882,21 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
}

case CCA_NEW_AI: { // Make a new AI company
#ifdef ENABLE_NETWORK
/* Enforce the max_no_competitors test */
bool enforce = HasBit(p2, 0);
if (enforce) {
Company *c;

/* Count number of competitors */
uint n = 0;
FOR_ALL_COMPANIES(c) {
if (c->is_ai) n++;
}

if (n >= (uint)_settings_game.difficulty.max_no_competitors) return CMD_ERROR;
}
#endif /* ENABLE_NETWORK */
if (!(flags & DC_EXEC)) return CommandCost();

if (company_id != INVALID_COMPANY && (company_id >= MAX_COMPANIES || Company::IsValidID(company_id))) return CMD_ERROR;
Expand Down

0 comments on commit 6a222fc

Please sign in to comment.