changeset 10758:2027f714bb47 draft

(svn r15091) -Add [NoAI] [API CHANGE]: introduce GetCategory() as a requirement for every library.nut, to indicate in which category it belongs. Currently the directory indicates the category, but this doesn't allow planned future additions
author truebrain <truebrain@openttd.org>
date Thu, 15 Jan 2009 15:56:10 +0000
parents dcc20859477f
children 54b74d104a09
files bin/ai/library/graph/aystar/library.nut bin/ai/library/pathfinder/rail/library.nut bin/ai/library/pathfinder/road/library.nut bin/ai/library/queue/binary_heap/library.nut bin/ai/library/queue/fibonacci_heap/library.nut bin/ai/library/queue/priority_queue/library.nut src/ai/ai_info.cpp src/ai/ai_info.hpp
diffstat 8 files changed, 30 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/bin/ai/library/graph/aystar/library.nut
+++ b/bin/ai/library/graph/aystar/library.nut
@@ -8,6 +8,7 @@
 	function GetVersion()     { return 4; }
 	function GetDate()        { return "2008-06-11"; }
 	function CreateInstance() { return "AyStar"; }
+	function GetCategory()    { return "Graph"; }
 }
 
 RegisterLibrary(AyStar());
--- a/bin/ai/library/pathfinder/rail/library.nut
+++ b/bin/ai/library/pathfinder/rail/library.nut
@@ -8,6 +8,7 @@
 	function GetVersion()     { return 1; }
 	function GetDate()        { return "2008-09-22"; }
 	function CreateInstance() { return "Rail"; }
+	function GetCategory()    { return "Pathfinder"; }
 }
 
 RegisterLibrary(Rail());
--- a/bin/ai/library/pathfinder/road/library.nut
+++ b/bin/ai/library/pathfinder/road/library.nut
@@ -8,6 +8,7 @@
 	function GetVersion()     { return 3; }
 	function GetDate()        { return "2008-06-18"; }
 	function CreateInstance() { return "Road"; }
+	function GetCategory()    { return "Pathfinder"; }
 }
 
 RegisterLibrary(Road());
--- a/bin/ai/library/queue/binary_heap/library.nut
+++ b/bin/ai/library/queue/binary_heap/library.nut
@@ -8,6 +8,7 @@
 	function GetVersion()     { return 1; }
 	function GetDate()        { return "2008-06-10"; }
 	function CreateInstance() { return "BinaryHeap"; }
+	function GetCategory()    { return "Queue"; }
 }
 
 RegisterLibrary(BinaryHeap());
--- a/bin/ai/library/queue/fibonacci_heap/library.nut
+++ b/bin/ai/library/queue/fibonacci_heap/library.nut
@@ -8,6 +8,7 @@
 	function GetVersion()     { return 1; }
 	function GetDate()        { return "2008-08-22"; }
 	function CreateInstance() { return "FibonacciHeap"; }
+	function GetCategory()    { return "Queue"; }
 }
 
 RegisterLibrary(FibonacciHeap());
--- a/bin/ai/library/queue/priority_queue/library.nut
+++ b/bin/ai/library/queue/priority_queue/library.nut
@@ -8,6 +8,7 @@
 	function GetVersion()     { return 2; }
 	function GetDate()        { return "2008-06-10"; }
 	function CreateInstance() { return "PriorityQueue"; }
+	function GetCategory()    { return "Queue"; }
 }
 
 RegisterLibrary(PriorityQueue());
--- a/src/ai/ai_info.cpp
+++ b/src/ai/ai_info.cpp
@@ -118,7 +118,7 @@
 	}
 }
 
-/* static */ SQInteger AIFileInfo::Constructor(HSQUIRRELVM vm, AIFileInfo *info)
+/* static */ SQInteger AIFileInfo::Constructor(HSQUIRRELVM vm, AIFileInfo *info, bool library)
 {
 	SQInteger res = 0;
 
@@ -138,6 +138,9 @@
 	info->CheckMethods(&res, "GetVersion");
 	info->CheckMethods(&res, "GetDate");
 	info->CheckMethods(&res, "CreateInstance");
+	if (library) {
+		info->CheckMethods(&res, "GetCategory");
+	}
 
 	/* Abort if one method was missing */
 	if (res != 0) return res;
@@ -155,7 +158,7 @@
 	sq_getinstanceup(vm, 2, &instance, 0);
 	AIInfo *info = (AIInfo *)instance;
 
-	SQInteger res = AIFileInfo::Constructor(vm, info);
+	SQInteger res = AIFileInfo::Constructor(vm, info, false);
 	if (res != 0) return res;
 
 	AIConfigItem config;
@@ -191,7 +194,7 @@
 	sq_getinstanceup(vm, 2, &instance, 0);
 	AIInfo *info = (AIInfo *)instance;
 
-	SQInteger res = AIFileInfo::Constructor(vm, info);
+	SQInteger res = AIFileInfo::Constructor(vm, info, false);
 	if (res != 0) return res;
 
 	/* Remove the link to the real instance, else it might get deleted by RegisterAI() */
@@ -356,7 +359,7 @@
 	/* Create a new AIFileInfo */
 	AILibrary *library = new AILibrary();
 
-	SQInteger res = AIFileInfo::Constructor(vm, library);
+	SQInteger res = AIFileInfo::Constructor(vm, library, true);
 	if (res != 0) return res;
 
 	/* Register the Library to the base system */
@@ -365,6 +368,12 @@
 	return 0;
 }
 
+const char *AILibrary::GetCategory()
+{
+	if (this->category == NULL) this->category = this->engine->CallStringMethodStrdup(*this->SQ_instance, "GetCategory");
+	return this->category;
+}
+
 /* static */ SQInteger AILibrary::Import(HSQUIRRELVM vm)
 {
 	SQConvert::SQAutoFreePointers ptr;
--- a/src/ai/ai_info.hpp
+++ b/src/ai/ai_info.hpp
@@ -101,7 +101,7 @@
 	/**
 	 * Process the creation of a FileInfo object.
 	 */
-	static SQInteger Constructor(HSQUIRRELVM vm, AIFileInfo *info);
+	static SQInteger Constructor(HSQUIRRELVM vm, AIFileInfo *info, bool library);
 
 private:
 	class Squirrel *engine;
@@ -155,12 +155,22 @@
 
 class AILibrary : public AIFileInfo {
 public:
+	AILibrary() : AIFileInfo(), category(NULL) {};
+
 	/**
 	 * Create an AI, using this AIInfo as start-template.
 	 */
 	static SQInteger Constructor(HSQUIRRELVM vm);
 
 	static SQInteger Import(HSQUIRRELVM vm);
+
+	/**
+	 * Get the category this library is in.
+	 */
+	const char *GetCategory();
+
+private:
+	const char *category;
 };
 
 #endif /* AI_INFO */