summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2009-06-10 22:05:01 +0000
committersmatz <smatz@openttd.org>2009-06-10 22:05:01 +0000
commitbea3fe2b8b2352d471149a4c914f93c58aa11b83 (patch)
tree69bd9a4b24333a7da3f2f3d11af4aafff5c92583 /src
parentbc7e9514d247581e085b80a71dabbd6e635e5ded (diff)
downloadopenttd-bea3fe2b8b2352d471149a4c914f93c58aa11b83.tar.xz
(svn r16559) -Codechange: introduce Company::IsValidAiID() and Company::IsValidHumanID(), don't use IsHumanCompany() where possible
Diffstat (limited to 'src')
-rw-r--r--src/ai/ai.hpp2
-rw-r--r--src/ai/ai_core.cpp9
-rw-r--r--src/ai/ai_gui.cpp17
-rw-r--r--src/ai/ai_instance.cpp2
-rw-r--r--src/company_base.h18
-rw-r--r--src/company_cmd.cpp10
-rw-r--r--src/company_func.h2
-rw-r--r--src/company_gui.cpp2
-rw-r--r--src/economy.cpp6
-rw-r--r--src/network/network_server.cpp2
-rw-r--r--src/newgrf_house.cpp5
-rw-r--r--src/saveload/ai_sl.cpp6
-rw-r--r--src/saveload/company_sl.cpp2
-rw-r--r--src/strings.cpp2
14 files changed, 47 insertions, 38 deletions
diff --git a/src/ai/ai.hpp b/src/ai/ai.hpp
index 7837edee0..267a4b6d1 100644
--- a/src/ai/ai.hpp
+++ b/src/ai/ai.hpp
@@ -54,7 +54,7 @@ public:
/**
* Stop a company to be controlled by an AI.
* @param company The company from which the AI needs to detach.
- * @pre !IsHumanCompany(company).
+ * @pre Company::IsValidAiID(company)
*/
static void Stop(CompanyID company);
diff --git a/src/ai/ai_core.cpp b/src/ai/ai_core.cpp
index 0eb4fe29d..b89d2064f 100644
--- a/src/ai/ai_core.cpp
+++ b/src/ai/ai_core.cpp
@@ -64,7 +64,7 @@
const Company *c;
FOR_ALL_COMPANIES(c) {
- if (!IsHumanCompany(c->index)) {
+ if (c->is_ai) {
_current_company = c->index;
c->ai_instance->GameLoop();
}
@@ -74,8 +74,7 @@
* Effectively collecting garbage once every two months per AI. */
if ((AI::frame_counter & 255) == 0) {
CompanyID cid = (CompanyID)GB(AI::frame_counter, 8, 4);
- Company *com = Company::GetIfValid(cid);
- if (com != NULL && !IsHumanCompany(cid)) com->ai_instance->CollectGarbage();
+ if (Company::IsValidAiID(cid)) Company::Get(cid)->ai_instance->CollectGarbage();
}
_current_company = OWNER_NONE;
@@ -109,7 +108,7 @@
const Company *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 */
- if (!Company::IsValidID(company) || IsHumanCompany(company)) {
+ if (!Company::IsValidAiID(company)) {
event->Release();
return;
}
diff --git a/src/ai/ai_gui.cpp b/src/ai/ai_gui.cpp
index bf12ce6ba..33585059e 100644
--- a/src/ai/ai_gui.cpp
+++ b/src/ai/ai_gui.cpp
@@ -642,8 +642,7 @@ struct AIDebugWindow : public Window {
{
/* Disable the companies who are not active or not an AI */
for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
- Company *c = Company::GetIfValid(i);
- this->SetWidgetDisabledState(i + AID_WIDGET_COMPANY_BUTTON_START, c == NULL || !c->is_ai);
+ this->SetWidgetDisabledState(i + AID_WIDGET_COMPANY_BUTTON_START, !Company::IsValidAiID(i));
}
this->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);
@@ -661,7 +660,7 @@ struct AIDebugWindow : public Window {
virtual void OnPaint()
{
/* 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) {
/* Raise and disable the widget for the previous selection. */
this->RaiseWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
@@ -670,13 +669,13 @@ struct AIDebugWindow : public Window {
ai_debug_company = INVALID_COMPANY;
}
- for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
- Company *c = Company::GetIfValid(i);
- if (c != NULL && c->is_ai) {
+ const Company *c;
+ FOR_ALL_COMPANIES(c) {
+ if (c->is_ai) {
/* 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;
}
}
@@ -696,7 +695,7 @@ struct AIDebugWindow : public Window {
/* Background is grey by default, will be changed to red for dead AIs */
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) {
/* Check if we have the company as an active company */
if (!this->IsWidgetDisabled(i + AID_WIDGET_COMPANY_BUTTON_START)) {
diff --git a/src/ai/ai_instance.cpp b/src/ai/ai_instance.cpp
index 6d264abea..8c70cf177 100644
--- a/src/ai/ai_instance.cpp
+++ b/src/ai/ai_instance.cpp
@@ -363,7 +363,7 @@ void AIInstance::CollectGarbage()
/* 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;
}
diff --git a/src/company_base.h b/src/company_base.h
index e1049bad5..6d0f2abd0 100644
--- a/src/company_base.h
+++ b/src/company_base.h
@@ -79,6 +79,18 @@ struct Company : CompanyPool::PoolItem<&_company_pool> {
EngineRenewList engine_renew_list; ///< Defined later
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)
+
+ 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)
@@ -86,6 +98,12 @@ struct Company : CompanyPool::PoolItem<&_company_pool> {
Money CalculateCompanyValue(const Company *c);
+static inline bool IsHumanCompany(CompanyID company)
+{
+ return !Company::Get(company)->is_ai;
+}
+
+
extern uint _next_competitor_start;
extern uint _cur_company_tick_index;
diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp
index 58ff40a97..777a00bc7 100644
--- a/src/company_cmd.cpp
+++ b/src/company_cmd.cpp
@@ -83,12 +83,6 @@ void SetLocalCompany(CompanyID new_company)
MarkWholeScreenDirty();
}
-bool IsHumanCompany(CompanyID company)
-{
- return !Company::Get(company)->is_ai;
-}
-
-
uint16 GetDrawStringCompanyColour(CompanyID company)
{
/* Get the colour for DrawString-subroutines which matches the colour
@@ -276,7 +270,7 @@ set_name:;
MarkWholeScreenDirty();
- if (!IsHumanCompany(c->index)) {
+ if (c->is_ai) {
CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
cni->FillData(c);
SetDParam(0, STR_NEWS_COMPANY_LAUNCH_TITLE);
@@ -725,7 +719,7 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
/* Remove the company */
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;
delete c;
diff --git a/src/company_func.h b/src/company_func.h
index 78dcd9eab..e97789643 100644
--- a/src/company_func.h
+++ b/src/company_func.h
@@ -21,8 +21,6 @@ extern CompanyByte _current_company;
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
-bool IsHumanCompany(CompanyID company);
-
static inline bool IsLocalCompany()
{
return _local_company == _current_company;
diff --git a/src/company_gui.cpp b/src/company_gui.cpp
index 0c0776cc9..04f280a73 100644
--- a/src/company_gui.cpp
+++ b/src/company_gui.cpp
@@ -1592,7 +1592,7 @@ struct CompanyWindow : Window
this->SetWidgetHiddenState(CW_WIDGET_SELL_SHARE, local);
this->SetWidgetHiddenState(CW_WIDGET_COMPANY_PASSWORD, !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 (_settings_game.economy.allow_shares) { // Shares are allowed
diff --git a/src/economy.cpp b/src/economy.cpp
index dd925a380..194cc97d8 100644
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -487,7 +487,7 @@ static void CompanyCheckBankrupt(Company *c)
case 3: {
/* XXX - In multiplayer, should we ask other companies if it wants to take
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(1, STR_NEWS_COMPANY_IN_TROUBLE_DESCRIPTION);
SetDParamStr(2, cni->company_name);
@@ -532,7 +532,7 @@ static void CompanyCheckBankrupt(Company *c)
ChangeNetworkOwner(c->index, COMPANY_SPECTATOR);
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;
delete c;
@@ -1528,7 +1528,7 @@ static void DoAcquireCompany(Company *c)
}
_current_company = old_company;
- if (!IsHumanCompany(c->index)) AI::Stop(c->index);
+ if (c->is_ai) AI::Stop(c->index);
DeleteCompanyWindows(ci);
InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp
index d0bdb4fd4..cd1fd0c1c 100644
--- a/src/network/network_server.cpp
+++ b/src/network/network_server.cpp
@@ -686,7 +686,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
}
break;
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);
return;
}
diff --git a/src/newgrf_house.cpp b/src/newgrf_house.cpp
index f1585f97e..b1e5c64fc 100644
--- a/src/newgrf_house.cpp
+++ b/src/newgrf_house.cpp
@@ -519,8 +519,9 @@ bool CanDeleteHouse(TileIndex tile)
/* Humans are always allowed to remove buildings, as is water and
* anyone using the scenario editor. */
- if ((Company::IsValidID(_current_company) && IsHumanCompany(_current_company))
- || _current_company == OWNER_WATER || _current_company == OWNER_NONE) return true;
+ if (Company::IsValidHumanID(_current_company) || _current_company == OWNER_WATER || _current_company == OWNER_NONE) {
+ return true;
+ }
if (HasBit(hs->callback_mask, CBM_HOUSE_DENY_DESTRUCTION)) {
uint16 callback_res = GetHouseCallback(CBID_HOUSE_DENY_DESTRUCTION, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
diff --git a/src/saveload/ai_sl.cpp b/src/saveload/ai_sl.cpp
index 4001fb81f..d5687db54 100644
--- a/src/saveload/ai_sl.cpp
+++ b/src/saveload/ai_sl.cpp
@@ -43,7 +43,7 @@ static void SaveReal_AIPL(int *index_ptr)
SlObject(NULL, _ai_company);
/* 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()
@@ -59,7 +59,7 @@ static void Load_AIPL()
SlObject(NULL, _ai_company);
if (_networking && !_network_server) {
- if (Company::IsValidID(index) && !IsHumanCompany(index)) AIInstance::LoadEmpty();
+ if (Company::IsValidAiID(index)) AIInstance::LoadEmpty();
continue;
}
@@ -86,7 +86,7 @@ static void Load_AIPL()
config->StringToSettings(_ai_saveload_settings);
/* 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::Load(index, _ai_saveload_version);
}
diff --git a/src/saveload/company_sl.cpp b/src/saveload/company_sl.cpp
index 940b561be..9040b7521 100644
--- a/src/saveload/company_sl.cpp
+++ b/src/saveload/company_sl.cpp
@@ -227,7 +227,7 @@ static void SaveLoad_PLYR(Company *c)
SlObject(c, _company_desc);
/* 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;
char nothing;
diff --git a/src/strings.cpp b/src/strings.cpp
index 9f2f5261a..880137b9f 100644
--- a/src/strings.cpp
+++ b/src/strings.cpp
@@ -990,7 +990,7 @@ static char *FormatString(char *buff, const char *str, int64 *argv, uint casei,
CompanyID company = (CompanyID)GetInt32(&argv);
/* Nothing is added for AI or inactive companies */
- if (Company::IsValidID(company) && IsHumanCompany(company)) {
+ if (!Company::IsValidHumanID(company)) {
int64 args[1];
args[0] = company + 1;
buff = GetStringWithArgs(buff, STR_COMPANY_NUM, args, last);