(svn r23209) -Codechange: track the current active script instance directly, instead of assuming the current company points you to the right one.

This commit is contained in:
truebrain
2011-11-13 20:43:48 +00:00
parent 407514a590
commit b7a655bf4c
9 changed files with 138 additions and 52 deletions

View File

@@ -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)

View File

@@ -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.");
}
}

View File

@@ -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();
}

View File

@@ -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.

View File

@@ -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.");
}
}