From b7a655bf4cafc68e14cade593e8b1aca7f04f7dd Mon Sep 17 00:00:00 2001 From: truebrain Date: Sun, 13 Nov 2011 20:43:48 +0000 Subject: (svn r23209) -Codechange: track the current active script instance directly, instead of assuming the current company points you to the right one. --- src/ai/api/ai_controller.cpp | 4 ++-- src/ai/api/ai_execmode.cpp | 3 +-- src/ai/api/ai_object.cpp | 27 +++++++++++++++++++++++---- src/ai/api/ai_object.hpp | 30 ++++++++++++++++++++++++++---- src/ai/api/ai_testmode.cpp | 3 +-- 5 files changed, 53 insertions(+), 14 deletions(-) (limited to 'src/ai/api') diff --git a/src/ai/api/ai_controller.cpp b/src/ai/api/ai_controller.cpp index 0c478cf4a..78dee1d21 100644 --- a/src/ai/api/ai_controller.cpp +++ b/src/ai/api/ai_controller.cpp @@ -63,12 +63,12 @@ AIController::~AIController() /* static */ uint AIController::GetTick() { - return ::Company::Get(_current_company)->ai_instance->GetController()->ticks; + return AIObject::GetActiveInstance()->GetController()->ticks; } /* static */ int AIController::GetOpsTillSuspend() { - return ::Company::Get(_current_company)->ai_instance->GetOpsTillSuspend(); + return AIObject::GetActiveInstance()->GetOpsTillSuspend(); } /* static */ int AIController::GetSetting(const char *name) diff --git a/src/ai/api/ai_execmode.cpp b/src/ai/api/ai_execmode.cpp index e661a1068..f5e1e502b 100644 --- a/src/ai/api/ai_execmode.cpp +++ b/src/ai/api/ai_execmode.cpp @@ -32,9 +32,8 @@ AIExecMode::AIExecMode() AIExecMode::~AIExecMode() { if (this->GetDoCommandModeInstance() != this) { - AIInstance *instance = Company::Get(_current_company)->ai_instance; /* Ignore this error if the AI already died. */ - if (!instance->IsDead()) { + if (!AIObject::GetActiveInstance()->IsDead()) { throw AI_FatalError("AIExecMode object was removed while it was not the latest AI*Mode object created."); } } diff --git a/src/ai/api/ai_object.cpp b/src/ai/api/ai_object.cpp index 4c415524b..53f2f2b55 100644 --- a/src/ai/api/ai_object.cpp +++ b/src/ai/api/ai_object.cpp @@ -12,8 +12,6 @@ #include "../../stdafx.h" #include "../../script/squirrel.hpp" #include "../../command_func.h" -#include "../../company_base.h" -#include "../../company_func.h" #include "../../network/network.h" #include "../../tunnelbridge.h" @@ -27,9 +25,30 @@ */ static AIStorage *GetStorage() { - return AIInstance::GetStorage(); + return AIObject::GetActiveInstance()->GetStorage(); } + +/* static */ AIInstance *AIObject::ActiveInstance::active = NULL; + +AIObject::ActiveInstance::ActiveInstance(AIInstance *instance) +{ + this->last_active = AIObject::ActiveInstance::active; + AIObject::ActiveInstance::active = instance; +} + +AIObject::ActiveInstance::~ActiveInstance() +{ + AIObject::ActiveInstance::active = this->last_active; +} + +/* static */ AIInstance *AIObject::GetActiveInstance() +{ + assert(AIObject::ActiveInstance::active != NULL); + return AIObject::ActiveInstance::active; +} + + /* static */ void AIObject::SetDoCommandDelay(uint ticks) { assert(ticks > 0); @@ -179,7 +198,7 @@ static AIStorage *GetStorage() /* static */ bool AIObject::CanSuspend() { - Squirrel *squirrel = Company::Get(_current_company)->ai_instance->engine; + Squirrel *squirrel = AIObject::GetActiveInstance()->engine; return GetStorage()->allow_do_command && squirrel->CanSuspend(); } diff --git a/src/ai/api/ai_object.hpp b/src/ai/api/ai_object.hpp index 3df150bac..deac277dd 100644 --- a/src/ai/api/ai_object.hpp +++ b/src/ai/api/ai_object.hpp @@ -35,10 +35,26 @@ typedef bool (AIModeProc)(); * command processing, and command-validation checks. */ class AIObject : public SimpleCountedObject { -friend void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2); friend class AIInstance; -friend class AIController; #ifndef DOXYGEN_AI_DOCS +protected: + /** + * A class that handles the current active instance. By instantiating it at + * the beginning of a function with the current active instance, it remains + * active till the scope of the variable closes. It then automatically + * reverts to the active instance it was before instantiating. + */ + class ActiveInstance { + friend class AIObject; + public: + ActiveInstance(AIInstance *instance); + ~ActiveInstance(); + private: + AIInstance *last_active; ///< The active instance before we go instantiated. + + static AIInstance *active; ///< The global current active instance. + }; + public: /** * Store the latest result of a DoCommand per company. @@ -47,9 +63,10 @@ public: static void SetLastCommandRes(bool res); /** - * Get the pointer to store log message in. + * Get the currently active instance. + * @return The instance. */ - static void *&GetLogPointer(); + static class AIInstance *GetActiveInstance(); protected: /** @@ -197,6 +214,11 @@ protected: */ static void *&GetEventPointer(); + /** + * Get the pointer to store log message in. + */ + static void *&GetLogPointer(); + private: /** * Store a new_vehicle_id per company. diff --git a/src/ai/api/ai_testmode.cpp b/src/ai/api/ai_testmode.cpp index 996b27874..6e2f515b4 100644 --- a/src/ai/api/ai_testmode.cpp +++ b/src/ai/api/ai_testmode.cpp @@ -32,9 +32,8 @@ AITestMode::AITestMode() AITestMode::~AITestMode() { if (this->GetDoCommandModeInstance() != this) { - AIInstance *instance = Company::Get(_current_company)->ai_instance; /* Ignore this error if the AI already died. */ - if (!instance->IsDead()) { + if (!AIObject::GetActiveInstance()->IsDead()) { throw AI_FatalError("AITestmode object was removed while it was not the latest AI*Mode object created."); } } -- cgit v1.2.3-70-g09d2