summaryrefslogtreecommitdiff
path: root/src/ai
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2010-05-31 20:22:57 +0000
committerfrosch <frosch@openttd.org>2010-05-31 20:22:57 +0000
commit9db3cde73af598852da13b748eef31a8207cfcf2 (patch)
tree996d3e9c49ce7c3611a70ed4fa6f03774b4cafe9 /src/ai
parent1d05fbebbef72722d400ab4f7693dde11205a7f4 (diff)
downloadopenttd-9db3cde73af598852da13b748eef31a8207cfcf2.tar.xz
(svn r19914) -Codechange: Wrap a helper class around temporary assignments of _current_company to ensure proper restoration.
Diffstat (limited to 'src/ai')
-rw-r--r--src/ai/ai_core.cpp32
-rw-r--r--src/ai/ai_gui.cpp21
2 files changed, 23 insertions, 30 deletions
diff --git a/src/ai/ai_core.cpp b/src/ai/ai_core.cpp
index 02868e9bd..f61bc058c 100644
--- a/src/ai/ai_core.cpp
+++ b/src/ai/ai_core.cpp
@@ -10,6 +10,7 @@
/** @file ai_core.cpp Implementation of AI. */
#include "../stdafx.h"
+#include "../core/backup_type.hpp"
#include "../core/bitmath_func.hpp"
#include "../company_base.h"
#include "../company_func.h"
@@ -68,13 +69,15 @@
assert(_settings_game.difficulty.competitor_speed <= 4);
if ((AI::frame_counter & ((1 << (4 - _settings_game.difficulty.competitor_speed)) - 1)) != 0) return;
+ Backup<CompanyByte> cur_company(_current_company);
const Company *c;
FOR_ALL_COMPANIES(c) {
if (c->is_ai) {
- _current_company = c->index;
+ cur_company.Change(c->index);
c->ai_instance->GameLoop();
}
}
+ cur_company.Restore();
/* Occasionally collect garbage; every 255 ticks do one company.
* Effectively collecting garbage once every two months per AI. */
@@ -82,8 +85,6 @@
CompanyID cid = (CompanyID)GB(AI::frame_counter, 8, 4);
if (Company::IsValidAiID(cid)) Company::Get(cid)->ai_instance->CollectGarbage();
}
-
- _current_company = OWNER_NONE;
}
/* static */ uint AI::GetTick()
@@ -95,14 +96,13 @@
{
if (_networking && !_network_server) return;
- CompanyID old_company = _current_company;
- _current_company = company;
+ Backup<CompanyByte> cur_company(_current_company, company);
Company *c = Company::Get(company);
delete c->ai_instance;
c->ai_instance = NULL;
- _current_company = old_company;
+ cur_company.Restore();
InvalidateWindowData(WC_AI_DEBUG, 0, -1);
DeleteWindowById(WC_AI_SETTINGS, company);
@@ -112,11 +112,10 @@
{
if (_networking && !_network_server) return;
- CompanyID old_company = _current_company;
- _current_company = company;
+ Backup<CompanyByte> cur_company(_current_company, company);
Company::Get(company)->ai_instance->Suspend();
- _current_company = old_company;
+ cur_company.Restore();
}
/* static */ void AI::KillAll()
@@ -202,10 +201,9 @@
}
/* Queue the event */
- CompanyID old_company = _current_company;
- _current_company = company;
+ Backup<CompanyByte> cur_company(_current_company, company);
AIEventController::InsertEvent(event);
- _current_company = old_company;
+ cur_company.Restore();
event->Release();
}
@@ -249,10 +247,9 @@ void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
Company *c = Company::GetIfValid(company);
assert(c != NULL && c->ai_instance != NULL);
- CompanyID old_company = _current_company;
- _current_company = company;
+ Backup<CompanyByte> cur_company(_current_company, company);
c->ai_instance->Save();
- _current_company = old_company;
+ cur_company.Restore();
} else {
AIInstance::SaveEmpty();
}
@@ -264,10 +261,9 @@ void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
Company *c = Company::GetIfValid(company);
assert(c != NULL && c->ai_instance != NULL);
- CompanyID old_company = _current_company;
- _current_company = company;
+ Backup<CompanyByte> cur_company(_current_company, company);
c->ai_instance->Load(version);
- _current_company = old_company;
+ cur_company.Restore();
} else {
/* Read, but ignore, the load data */
AIInstance::LoadEmpty();
diff --git a/src/ai/ai_gui.cpp b/src/ai/ai_gui.cpp
index 57e8d0c24..afad432f6 100644
--- a/src/ai/ai_gui.cpp
+++ b/src/ai/ai_gui.cpp
@@ -26,6 +26,7 @@
#include "../textbuf_gui.h"
#include "../settings_func.h"
#include "../network/network_content.h"
+#include "../core/backup_type.hpp"
#include "ai.hpp"
#include "api/ai_log.hpp"
@@ -819,10 +820,9 @@ struct AIDebugWindow : public QueryStringBaseWindow {
DrawCompanyIcon(i, button->pos_x + button->current_x / 2 - 7 + offset, this->GetWidget<NWidgetBase>(AID_WIDGET_COMPANY_BUTTON_START + i)->pos_y + 2 + offset);
}
- CompanyID old_company = _current_company;
- _current_company = ai_debug_company;
+ Backup<CompanyByte> cur_company(_current_company, ai_debug_company);
AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
- _current_company = old_company;
+ cur_company.Restore();
int scroll_count = (log == NULL) ? 0 : log->used;
if (this->vscroll.GetCount() != scroll_count) {
@@ -875,10 +875,9 @@ struct AIDebugWindow : public QueryStringBaseWindow {
switch (widget) {
case AID_WIDGET_LOG_PANEL: {
- CompanyID old_company = _current_company;
- _current_company = ai_debug_company;
+ Backup<CompanyByte> cur_company(_current_company, ai_debug_company);
AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
- _current_company = old_company;
+ cur_company.Restore();
if (log == NULL) return;
int y = this->top_offset;
@@ -915,10 +914,9 @@ struct AIDebugWindow : public QueryStringBaseWindow {
this->RaiseWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
ai_debug_company = show_ai;
- CompanyID old_company = _current_company;
- _current_company = ai_debug_company;
+ Backup<CompanyByte> cur_company(_current_company, ai_debug_company);
AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
- _current_company = old_company;
+ cur_company.Restore();
this->vscroll.SetCount((log == NULL) ? 0 : log->used);
this->LowerWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START);
@@ -1011,8 +1009,7 @@ struct AIDebugWindow : public QueryStringBaseWindow {
/* If the log message is related to the active company tab, check the break string */
if (data == ai_debug_company && this->break_check_enabled && !StrEmpty(this->edit_str_buf)) {
/* Get the log instance of the active company */
- CompanyID old_company = _current_company;
- _current_company = ai_debug_company;
+ Backup<CompanyByte> cur_company(_current_company, ai_debug_company);
AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
if (log != NULL && case_sensitive_break_check?
@@ -1032,7 +1029,7 @@ struct AIDebugWindow : public QueryStringBaseWindow {
this->highlight_row = log->pos;
}
- _current_company = old_company;
+ cur_company.Restore();
}
}