src/ai/ai_squirrel.cpp
branchnoai
changeset 10650 30fc5395b1b8
parent 10649 9034b80fdbdb
child 10651 655c8b06f6d4
--- a/src/ai/ai_squirrel.cpp	Tue May 20 13:09:34 2008 +0000
+++ b/src/ai/ai_squirrel.cpp	Tue May 20 15:25:47 2008 +0000
@@ -64,13 +64,28 @@
 			*ext = '\0';
 		}
 
-		/* We look for the file 'main.nut' inside the AI dir.. if it doesn't exists, it isn't an AI */
-		ttd_strlcat(script_name, "main.nut", sizeof(script_name));
+		/* We look for the file 'info.nut' inside the AI dir.. if it doesn't exists, it isn't an AI */
+		ttd_strlcat(script_name, "info.nut", sizeof(script_name));
 		if (FioCheckFileExists(script_name, AI_DIR)) {
-			DEBUG(ai, 6, "[squirrel] Loading script '%s' for AI handling", script_name);
+			char load_script[MAX_PATH];
+			snprintf(load_script, sizeof(load_script), "%s", script_name);
+
+			/* Remove the 'info.nut' part and replace it with 'main.nut' */
+			script_name[strlen(script_name) - 8] = '\0';
+			ttd_strlcat(script_name, "main.nut", sizeof(script_name));
+
+			DEBUG(ai, 6, "[squirrel] Loading script '%s' for AI handling", load_script);
 			this->current_script = script_name;
 			this->current_dir = d_name;
-			this->engine->LoadScript(this->current_script);
+			this->engine->LoadScript(load_script);
+		} else {
+			/* Check if this person does have main.nut, but not yet info.nut, and assume it is an old AI
+			 *  Notice that the 'info' -> 'main' change is already done above. */
+			if (FioCheckFileExists(script_name, AI_DIR)) {
+				DEBUG(ai, 0, "You are using a deprecated way of registering your AI.");
+				DEBUG(ai, 0, "Please read http://wiki.openttd.org/index.php/AI:AIInfo for more info");
+				DEBUG(ai, 0, "Your AI will _not_ be loaded.");
+			}
 		}
 	}
 	closedir(dir);
@@ -80,14 +95,10 @@
 {
 	this->engine = new Squirrel();
 
-	/* Create the AIFactory class, and bind the constructor */
-	this->engine->AddClassBegin("AIFactory");
-	this->engine->AddMethod("constructor", &AIInfo::Constructor, 1, "x");
+	/* Create the AIInfo class, and add the RegisterAI function */
+	this->engine->AddClassBegin("AIInfo");
 	this->engine->AddClassEnd();
-
-	/* Set a dummy AIController, so script can load */
-	this->engine->AddClassBegin("AIController");
-	this->engine->AddClassEnd();
+	this->engine->AddMethod("RegisterAI", &AIInfo::Constructor, 2, "tx");
 
 	/* Mark this class as global pointer */
 	this->engine->SetGlobalPointer(this);
@@ -104,6 +115,12 @@
 
 AISquirrel::~AISquirrel()
 {
+	AIInfoList::iterator it = this->info_list.begin();
+	for (; it != this->info_list.end(); it++) {
+		AIInfo *i = (*it).second;
+		delete i;
+	}
+
 	delete this->engine;
 }
 
@@ -111,16 +128,17 @@
 {
 	const char *ai_name = info->GetDirName();
 
-	/* Check if we register twice; than the latest always wins */
+	/* Check if we register twice; than the first always wins */
 	if (this->info_list.find(ai_name) != this->info_list.end()) {
 		/* In case they are not the same dir, give a warning */
 		if (strcasecmp(info->GetScriptName(), this->info_list[ai_name]->GetScriptName()) != 0) {
 			DEBUG(ai, 0, "Registering two AIs with the same name");
 			DEBUG(ai, 0, "  1: %s", this->info_list[ai_name]->GetScriptName());
 			DEBUG(ai, 0, "  2: %s", info->GetScriptName());
-			DEBUG(ai, 0, "The latter is taking precedence");
+			DEBUG(ai, 0, "The first is taking precedence");
 		}
-		this->info_list[ai_name] = info;
+		/* Delete the new AIInfo, as we will be using the old one */
+		delete info;
 		return;
 	}
 	this->info_list[ai_name] = info;