diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -1200,3 +1200,31 @@ CommandCost CmdGiveMoney(DoCommandFlag f /* Subtract money from local-company */ return amount; } + +/** + * Get the index of the first available company. It attempts, + * from first to last, and as soon as the attempt succeeds, + * to get the index of the company: + * 1st - get the first existing human company. + * 2nd - get the first non-existing company. + * 3rd - get COMPANY_FIRST. + * @return the index of the first available company. + */ +CompanyID GetFirstPlayableCompanyID() +{ + for (Company *c : Company::Iterate()) { + if (Company::IsHumanID(c->index)) { + return c->index; + } + } + + if (Company::CanAllocateItem()) { + for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) { + if (!Company::IsValidID(c)) { + return c; + } + } + } + + return COMPANY_FIRST; +}