Beispiel #1
0
AIInstance::AIInstance(AIInfo *info) :
	controller(NULL),
	storage(NULL),
	engine(NULL),
	instance(NULL),
	is_started(false),
	is_dead(false),
	is_save_data_on_stack(false),
	suspend(0),
	callback(NULL)
{
	/* Set the instance already, so we can use AIObject::Set commands */
	Company::Get(_current_company)->ai_instance = this;

	this->controller = new AIController();
	this->storage    = new AIStorage();
	this->engine     = new Squirrel();
	this->engine->SetPrintFunction(&PrintFunc);

	/* The import method is available at a very early stage */
	this->engine->AddMethod("import", &AILibrary::Import, 4, ".ssi");

	/* Register the AIController */
	SQAIController_Register(this->engine);

	/* Register the API functions and classes */
	this->RegisterAPI();

	if (!this->LoadCompatibilityScripts(info->GetAPIVersion())) {
		this->Died();
		return;
	}

	try {
		AIObject::SetAllowDoCommand(false);
		/* Load and execute the script for this AI */
		const char *main_script = info->GetMainScript();
		if (strcmp(main_script, "%_dummy") == 0) {
			extern void AI_CreateAIDummy(HSQUIRRELVM vm);
			AI_CreateAIDummy(this->engine->GetVM());
		} else if (!this->engine->LoadScript(main_script) || this->engine->IsSuspended()) {
			if (this->engine->IsSuspended()) AILog::Error("This AI took too long to load script. AI is not started.");
			this->Died();
			return;
		}

		/* Create the main-class */
		this->instance = MallocT<SQObject>(1);
		if (!this->engine->CreateClassInstance(info->GetInstanceName(), this->controller, this->instance)) {
			this->Died();
			return;
		}
		AIObject::SetAllowDoCommand(true);
	} catch (AI_FatalError e) {
		this->is_dead = true;
		this->engine->ThrowError(e.GetErrorMessage());
		this->engine->ResumeError();
		this->Died();
	}
}
void AIInstance::Initialize(AIInfo *info)
{
	this->versionAPI = info->GetAPIVersion();

	/* Register the AIController (including the "import" command) */
	SQAIController_Register(this->engine);

	ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), _current_company);
}