src/ai/api/ai_controller.cpp
branchnoai
changeset 10958 65088d587094
parent 10902 57b380ee1607
child 10971 aaf89f8c59b9
equal deleted inserted replaced
10957:7a140b4cd91d 10958:65088d587094
     4 
     4 
     5 #include "../../stdafx.h"
     5 #include "../../stdafx.h"
     6 #include "../../openttd.h"
     6 #include "../../openttd.h"
     7 #include "../../player_func.h"
     7 #include "../../player_func.h"
     8 #include "../../core/alloc_func.hpp"
     8 #include "../../core/alloc_func.hpp"
       
     9 #include "../../string_func.h"
     9 #include "table/strings.h"
    10 #include "table/strings.h"
    10 
    11 
    11 #include <squirrel.h>
    12 #include <squirrel.h>
    12 #include "../../squirrel.hpp"
    13 #include "../../squirrel.hpp"
    13 #include "../../squirrel_helper.hpp"
    14 #include "../../squirrel_helper.hpp"
   150 {
   151 {
   151 	/* Convert to OpenTTD internal capable string */
   152 	/* Convert to OpenTTD internal capable string */
   152 	AIController::Print(error_msg, FS2OTTD(message));
   153 	AIController::Print(error_msg, FS2OTTD(message));
   153 }
   154 }
   154 
   155 
   155 AIController::AIController(const char *script, const char *class_name)
   156 AIController::AIController(const char *script, const char *class_name) :
   156 {
   157 	tick(0),
   157 	this->tick = 0;
   158 	engine(NULL),
   158 	this->SQ_instance = NULL;
   159 	SQ_instance(NULL),
       
   160 	loaded_library_count(0)
       
   161 {
       
   162 	this->script = strdup(script);
       
   163 	this->class_name = strdup(class_name);
       
   164 }
       
   165 
       
   166 void AIController::Start()
       
   167 {
       
   168 	assert(this->engine == NULL);
   159 
   169 
   160 	/* Create the Squirrel Engine and assign all required classes and settings */
   170 	/* Create the Squirrel Engine and assign all required classes and settings */
   161 	this->engine = new Squirrel();
   171 	this->engine = new Squirrel();
   162 	this->engine->SetPrintFunction(&PrintFunc);
   172 	this->engine->SetPrintFunction(&PrintFunc);
   163 	this->RegisterClasses();
   173 	this->RegisterClasses();
   164 	if (!this->engine->LoadScript(script)) {
   174 	if (!this->engine->LoadScript(this->script)) {
   165 		delete this->engine;
   175 		delete this->engine;
   166 		this->engine = NULL;
   176 		this->engine = NULL;
   167 		return;
   177 		return;
   168 	}
   178 	}
   169 	/* Create the main-class */
   179 	/* Create the main-class */
   170 	this->SQ_instance = MallocT<SQObject>(sizeof(SQObject));
   180 	this->SQ_instance = MallocT<SQObject>(sizeof(SQObject));
   171 	if (!this->engine->CreateClassInstance(class_name, this, this->SQ_instance)) {
   181 	if (!this->engine->CreateClassInstance(this->class_name, this, this->SQ_instance)) {
   172 		delete this->engine;
   182 		delete this->engine;
   173 		this->engine = NULL;
   183 		this->engine = NULL;
   174 		return;
   184 		return;
   175 	}
   185 	}
   176 }
       
   177 
       
   178 void AIController::Start()
       
   179 {
       
   180 	if (this->engine == NULL) return;
       
   181 
   186 
   182 	/* Run the constructor first */
   187 	/* Run the constructor first */
   183 	if (this->engine->MethodExists(*this->SQ_instance, "constructor")) {
   188 	if (this->engine->MethodExists(*this->SQ_instance, "constructor")) {
   184 		this->engine->CallMethod(*this->SQ_instance, "constructor");
   189 		this->engine->CallMethod(*this->SQ_instance, "constructor");
   185 	}
   190 	}
   189 
   194 
   190 AIController::~AIController()
   195 AIController::~AIController()
   191 {
   196 {
   192 	if (this->engine != NULL) delete this->engine;
   197 	if (this->engine != NULL) delete this->engine;
   193 	free(this->SQ_instance);
   198 	free(this->SQ_instance);
       
   199 	free((void *)this->script);
       
   200 	free((void *)this->class_name);
       
   201 
       
   202 	for (LoadedLibraryList::iterator iter = this->loaded_library.begin(); iter != this->loaded_library.end(); iter++) {
       
   203 		free((void *)(*iter).second);
       
   204 		free((void *)(*iter).first);
       
   205 	}
       
   206 
       
   207 	this->loaded_library.clear();
   194 }
   208 }
   195 
   209 
   196 void AIController::IncreaseTick()
   210 void AIController::IncreaseTick()
   197 {
   211 {
   198 	this->tick++;
   212 	this->tick++;
   200 
   214 
   201 uint AIController::GetTick()
   215 uint AIController::GetTick()
   202 {
   216 {
   203 	return this->tick;
   217 	return this->tick;
   204 }
   218 }
       
   219 
       
   220 bool AIController::LoadedLibrary(const char *library_name, int *next_number, char *fake_class_name, int fake_class_name_len)
       
   221 {
       
   222 	LoadedLibraryList::iterator iter = this->loaded_library.find(library_name);
       
   223 	if (iter == this->loaded_library.end()) {
       
   224 		*next_number = ++this->loaded_library_count;
       
   225 		return false;
       
   226 	}
       
   227 
       
   228 	ttd_strlcpy(fake_class_name, (*iter).second, fake_class_name_len);
       
   229 	return true;
       
   230 }
       
   231 
       
   232 void AIController::AddLoadedLibrary(const char *library_name, const char *fake_class_name)
       
   233 {
       
   234 	this->loaded_library[strdup(library_name)] = strdup(fake_class_name);
       
   235 }