(svn r16559) -Codechange: introduce Company::IsValidAiID() and Company::IsValidHumanID(), don't use IsHumanCompany() where possible

pull/155/head
smatz 16 years ago
parent 0b66eb938f
commit a2567c84a0

@ -54,7 +54,7 @@ public:
/** /**
* Stop a company to be controlled by an AI. * Stop a company to be controlled by an AI.
* @param company The company from which the AI needs to detach. * @param company The company from which the AI needs to detach.
* @pre !IsHumanCompany(company). * @pre Company::IsValidAiID(company)
*/ */
static void Stop(CompanyID company); static void Stop(CompanyID company);

@ -64,7 +64,7 @@
const Company *c; const Company *c;
FOR_ALL_COMPANIES(c) { FOR_ALL_COMPANIES(c) {
if (!IsHumanCompany(c->index)) { if (c->is_ai) {
_current_company = c->index; _current_company = c->index;
c->ai_instance->GameLoop(); c->ai_instance->GameLoop();
} }
@ -74,8 +74,7 @@
* Effectively collecting garbage once every two months per AI. */ * Effectively collecting garbage once every two months per AI. */
if ((AI::frame_counter & 255) == 0) { if ((AI::frame_counter & 255) == 0) {
CompanyID cid = (CompanyID)GB(AI::frame_counter, 8, 4); CompanyID cid = (CompanyID)GB(AI::frame_counter, 8, 4);
Company *com = Company::GetIfValid(cid); if (Company::IsValidAiID(cid)) Company::Get(cid)->ai_instance->CollectGarbage();
if (com != NULL && !IsHumanCompany(cid)) com->ai_instance->CollectGarbage();
} }
_current_company = OWNER_NONE; _current_company = OWNER_NONE;
@ -109,7 +108,7 @@
const Company *c; const Company *c;
FOR_ALL_COMPANIES(c) { FOR_ALL_COMPANIES(c) {
if (!IsHumanCompany(c->index)) AI::Stop(c->index); if (c->is_ai) AI::Stop(c->index);
} }
} }
@ -179,7 +178,7 @@
} }
/* Only AIs can have an event-queue */ /* Only AIs can have an event-queue */
if (!Company::IsValidID(company) || IsHumanCompany(company)) { if (!Company::IsValidAiID(company)) {
event->Release(); event->Release();
return; return;
} }

@ -642,8 +642,7 @@ struct AIDebugWindow : public Window {
{ {
/* Disable the companies who are not active or not an AI */ /* Disable the companies who are not active or not an AI */
for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) { for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
Company *c = Company::GetIfValid(i); this->SetWidgetDisabledState(i + AID_WIDGET_COMPANY_BUTTON_START, !Company::IsValidAiID(i));
this->SetWidgetDisabledState(i + AID_WIDGET_COMPANY_BUTTON_START, c == NULL || !c->is_ai);
} }
this->DisableWidget(AID_WIDGET_RELOAD_TOGGLE); this->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);
@ -661,7 +660,7 @@ struct AIDebugWindow : public Window {
virtual void OnPaint() virtual void OnPaint()
{ {
/* Check if the currently selected company is still active. */ /* Check if the currently selected company is still active. */
if (ai_debug_company == INVALID_COMPANY || !Company::IsValidID(ai_debug_company) || !Company::Get(ai_debug_company)->is_ai) { if (ai_debug_company == INVALID_COMPANY || !Company::IsValidAiID(ai_debug_company)) {
if (ai_debug_company != INVALID_COMPANY) { if (ai_debug_company != INVALID_COMPANY) {
/* Raise and disable the widget for the previous selection. */ /* Raise and disable the widget for the previous selection. */
this->RaiseWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START); this->RaiseWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
@ -670,13 +669,13 @@ struct AIDebugWindow : public Window {
ai_debug_company = INVALID_COMPANY; ai_debug_company = INVALID_COMPANY;
} }
for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) { const Company *c;
Company *c = Company::GetIfValid(i); FOR_ALL_COMPANIES(c) {
if (c != NULL && c->is_ai) { if (c->is_ai) {
/* Lower the widget corresponding to this company. */ /* Lower the widget corresponding to this company. */
this->LowerWidget(i + AID_WIDGET_COMPANY_BUTTON_START); this->LowerWidget(c->index + AID_WIDGET_COMPANY_BUTTON_START);
ai_debug_company = i; ai_debug_company = c->index;
break; break;
} }
} }
@ -696,7 +695,7 @@ struct AIDebugWindow : public Window {
/* Background is grey by default, will be changed to red for dead AIs */ /* Background is grey by default, will be changed to red for dead AIs */
this->widget[i + AID_WIDGET_COMPANY_BUTTON_START].colour = COLOUR_GREY; this->widget[i + AID_WIDGET_COMPANY_BUTTON_START].colour = COLOUR_GREY;
Company *c = Company::GetIfValid(i); const Company *c = Company::GetIfValid(i);
if (c == NULL || !c->is_ai) { if (c == NULL || !c->is_ai) {
/* Check if we have the company as an active company */ /* Check if we have the company as an active company */
if (!this->IsWidgetDisabled(i + AID_WIDGET_COMPANY_BUTTON_START)) { if (!this->IsWidgetDisabled(i + AID_WIDGET_COMPANY_BUTTON_START)) {

@ -363,7 +363,7 @@ void AIInstance::CollectGarbage()
/* static */ AIStorage *AIInstance::GetStorage() /* static */ AIStorage *AIInstance::GetStorage()
{ {
assert(Company::IsValidID(_current_company) && !IsHumanCompany(_current_company)); assert(Company::IsValidAiID(_current_company));
return Company::Get(_current_company)->ai_instance->storage; return Company::Get(_current_company)->ai_instance->storage;
} }

@ -79,6 +79,18 @@ struct Company : CompanyPool::PoolItem<&_company_pool> {
EngineRenewList engine_renew_list; ///< Defined later EngineRenewList engine_renew_list; ///< Defined later
CompanySettings settings; ///< settings specific for each company CompanySettings settings; ///< settings specific for each company
uint16 *num_engines; ///< caches the number of engines of each type the company owns (no need to save this) uint16 *num_engines; ///< caches the number of engines of each type the company owns (no need to save this)
static FORCEINLINE bool IsValidAiID(size_t index)
{
const Company *c = GetIfValid(index);
return c != NULL && c->is_ai;
}
static FORCEINLINE bool IsValidHumanID(size_t index)
{
const Company *c = GetIfValid(index);
return c != NULL && !c->is_ai;
}
}; };
#define FOR_ALL_COMPANIES_FROM(var, start) FOR_ALL_ITEMS_FROM(Company, company_index, var, start) #define FOR_ALL_COMPANIES_FROM(var, start) FOR_ALL_ITEMS_FROM(Company, company_index, var, start)
@ -86,6 +98,12 @@ struct Company : CompanyPool::PoolItem<&_company_pool> {
Money CalculateCompanyValue(const Company *c); Money CalculateCompanyValue(const Company *c);
static inline bool IsHumanCompany(CompanyID company)
{
return !Company::Get(company)->is_ai;
}
extern uint _next_competitor_start; extern uint _next_competitor_start;
extern uint _cur_company_tick_index; extern uint _cur_company_tick_index;

@ -83,12 +83,6 @@ void SetLocalCompany(CompanyID new_company)
MarkWholeScreenDirty(); MarkWholeScreenDirty();
} }
bool IsHumanCompany(CompanyID company)
{
return !Company::Get(company)->is_ai;
}
uint16 GetDrawStringCompanyColour(CompanyID company) uint16 GetDrawStringCompanyColour(CompanyID company)
{ {
/* Get the colour for DrawString-subroutines which matches the colour /* Get the colour for DrawString-subroutines which matches the colour
@ -276,7 +270,7 @@ set_name:;
MarkWholeScreenDirty(); MarkWholeScreenDirty();
if (!IsHumanCompany(c->index)) { if (c->is_ai) {
CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1); CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
cni->FillData(c); cni->FillData(c);
SetDParam(0, STR_NEWS_COMPANY_LAUNCH_TITLE); SetDParam(0, STR_NEWS_COMPANY_LAUNCH_TITLE);
@ -725,7 +719,7 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
/* Remove the company */ /* Remove the company */
ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER); ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
if (!IsHumanCompany(c->index)) AI::Stop(c->index); if (c->is_ai) AI::Stop(c->index);
CompanyID c_index = c->index; CompanyID c_index = c->index;
delete c; delete c;

@ -21,8 +21,6 @@ extern CompanyByte _current_company;
extern Colours _company_colours[MAX_COMPANIES]; ///< NOSAVE: can be determined from company structs extern Colours _company_colours[MAX_COMPANIES]; ///< NOSAVE: can be determined from company structs
extern CompanyManagerFace _company_manager_face; ///< for company manager face storage in openttd.cfg extern CompanyManagerFace _company_manager_face; ///< for company manager face storage in openttd.cfg
bool IsHumanCompany(CompanyID company);
static inline bool IsLocalCompany() static inline bool IsLocalCompany()
{ {
return _local_company == _current_company; return _local_company == _current_company;

@ -1592,7 +1592,7 @@ struct CompanyWindow : Window
this->SetWidgetHiddenState(CW_WIDGET_SELL_SHARE, local); this->SetWidgetHiddenState(CW_WIDGET_SELL_SHARE, local);
this->SetWidgetHiddenState(CW_WIDGET_COMPANY_PASSWORD, !local || !_networking); this->SetWidgetHiddenState(CW_WIDGET_COMPANY_PASSWORD, !local || !_networking);
this->SetWidgetHiddenState(CW_WIDGET_COMPANY_JOIN, local || !_networking); this->SetWidgetHiddenState(CW_WIDGET_COMPANY_JOIN, local || !_networking);
this->SetWidgetDisabledState(CW_WIDGET_COMPANY_JOIN, !IsHumanCompany(c->index)); this->SetWidgetDisabledState(CW_WIDGET_COMPANY_JOIN, c->is_ai);
if (!local) { if (!local) {
if (_settings_game.economy.allow_shares) { // Shares are allowed if (_settings_game.economy.allow_shares) { // Shares are allowed

@ -487,7 +487,7 @@ static void CompanyCheckBankrupt(Company *c)
case 3: { case 3: {
/* XXX - In multiplayer, should we ask other companies if it wants to take /* XXX - In multiplayer, should we ask other companies if it wants to take
over when it is a human company? -- TrueLight */ over when it is a human company? -- TrueLight */
if (IsHumanCompany(c->index)) { if (!c->is_ai) {
SetDParam(0, STR_NEWS_COMPANY_IN_TROUBLE_TITLE); SetDParam(0, STR_NEWS_COMPANY_IN_TROUBLE_TITLE);
SetDParam(1, STR_NEWS_COMPANY_IN_TROUBLE_DESCRIPTION); SetDParam(1, STR_NEWS_COMPANY_IN_TROUBLE_DESCRIPTION);
SetDParamStr(2, cni->company_name); SetDParamStr(2, cni->company_name);
@ -532,7 +532,7 @@ static void CompanyCheckBankrupt(Company *c)
ChangeNetworkOwner(c->index, COMPANY_SPECTATOR); ChangeNetworkOwner(c->index, COMPANY_SPECTATOR);
ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER); ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
if (!IsHumanCompany(c->index)) AI::Stop(c->index); if (c->is_ai) AI::Stop(c->index);
CompanyID c_index = c->index; CompanyID c_index = c->index;
delete c; delete c;
@ -1528,7 +1528,7 @@ static void DoAcquireCompany(Company *c)
} }
_current_company = old_company; _current_company = old_company;
if (!IsHumanCompany(c->index)) AI::Stop(c->index); if (c->is_ai) AI::Stop(c->index);
DeleteCompanyWindows(ci); DeleteCompanyWindows(ci);
InvalidateWindowClassesData(WC_TRAINS_LIST, 0); InvalidateWindowClassesData(WC_TRAINS_LIST, 0);

@ -686,7 +686,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
} }
break; break;
default: // Join another company (companies 1-8 (index 0-7)) default: // Join another company (companies 1-8 (index 0-7))
if (!Company::IsValidID(playas) || !IsHumanCompany(playas)) { if (!Company::IsValidHumanID(playas)) {
SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_COMPANY_MISMATCH); SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_COMPANY_MISMATCH);
return; return;
} }

@ -519,8 +519,9 @@ bool CanDeleteHouse(TileIndex tile)
/* Humans are always allowed to remove buildings, as is water and /* Humans are always allowed to remove buildings, as is water and
* anyone using the scenario editor. */ * anyone using the scenario editor. */
if ((Company::IsValidID(_current_company) && IsHumanCompany(_current_company)) if (Company::IsValidHumanID(_current_company) || _current_company == OWNER_WATER || _current_company == OWNER_NONE) {
|| _current_company == OWNER_WATER || _current_company == OWNER_NONE) return true; return true;
}
if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) { if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile); uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);

@ -43,7 +43,7 @@ static void SaveReal_AIPL(int *index_ptr)
SlObject(NULL, _ai_company); SlObject(NULL, _ai_company);
/* If the AI was active, store his data too */ /* If the AI was active, store his data too */
if (Company::IsValidID(index) && !IsHumanCompany(index)) AI::Save(index); if (Company::IsValidAiID(index)) AI::Save(index);
} }
static void Load_AIPL() static void Load_AIPL()
@ -59,7 +59,7 @@ static void Load_AIPL()
SlObject(NULL, _ai_company); SlObject(NULL, _ai_company);
if (_networking && !_network_server) { if (_networking && !_network_server) {
if (Company::IsValidID(index) && !IsHumanCompany(index)) AIInstance::LoadEmpty(); if (Company::IsValidAiID(index)) AIInstance::LoadEmpty();
continue; continue;
} }
@ -86,7 +86,7 @@ static void Load_AIPL()
config->StringToSettings(_ai_saveload_settings); config->StringToSettings(_ai_saveload_settings);
/* Start the AI directly if it was active in the savegame */ /* Start the AI directly if it was active in the savegame */
if (Company::IsValidID(index) && !IsHumanCompany(index)) { if (Company::IsValidAiID(index)) {
AI::StartNew(index); AI::StartNew(index);
AI::Load(index, _ai_saveload_version); AI::Load(index, _ai_saveload_version);
} }

@ -227,7 +227,7 @@ static void SaveLoad_PLYR(Company *c)
SlObject(c, _company_desc); SlObject(c, _company_desc);
/* Keep backwards compatible for savegames, so load the old AI block */ /* Keep backwards compatible for savegames, so load the old AI block */
if (CheckSavegameVersion(107) && !IsHumanCompany(c->index)) { if (CheckSavegameVersion(107) && c->is_ai) {
CompanyOldAI old_ai; CompanyOldAI old_ai;
char nothing; char nothing;

@ -990,7 +990,7 @@ static char *FormatString(char *buff, const char *str, int64 *argv, uint casei,
CompanyID company = (CompanyID)GetInt32(&argv); CompanyID company = (CompanyID)GetInt32(&argv);
/* Nothing is added for AI or inactive companies */ /* Nothing is added for AI or inactive companies */
if (Company::IsValidID(company) && IsHumanCompany(company)) { if (!Company::IsValidHumanID(company)) {
int64 args[1]; int64 args[1];
args[0] = company + 1; args[0] = company + 1;
buff = GetStringWithArgs(buff, STR_COMPANY_NUM, args, last); buff = GetStringWithArgs(buff, STR_COMPANY_NUM, args, last);

Loading…
Cancel
Save