changeset 12567:a2ab2f2e9af8 draft

(svn r17005) -Add [NoAI]: AIAbstractList::SORT_ASCENDING / SORT_DESCENDING
author yexo <yexo@openttd.org>
date Fri, 31 Jul 2009 19:50:01 +0000
parents 6596fafae9eb
children 895d8f06d5a8
files bin/ai/regression/regression.nut src/ai/api/ai_abstractlist.hpp src/ai/api/ai_abstractlist.hpp.sq src/ai/api/squirrel_export.awk src/script/squirrel.cpp src/script/squirrel.hpp
diffstat 6 files changed, 48 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/bin/ai/regression/regression.nut
+++ b/bin/ai/regression/regression.nut
@@ -58,7 +58,7 @@
 	}
 
 	list = AIList();
-	list.Sort(AIAbstractList.SORT_BY_VALUE, true);
+	list.Sort(AIAbstractList.SORT_BY_VALUE, AIAbstractList.SORT_ASCENDING);
 	print("");
 	print(" Value Ascending");
 	list.AddItem( 5, 10);
@@ -93,7 +93,7 @@
 	}
 
 	list = AIList();
-	list.Sort(AIAbstractList.SORT_BY_ITEM, false);
+	list.Sort(AIAbstractList.SORT_BY_ITEM, AIAbstractList.SORT_DESCENDING);
 	print("");
 	print(" Item Descending");
 	list.AddItem( 5, 10);
@@ -128,7 +128,7 @@
 	}
 
 	list = AIList();
-	list.Sort(AIAbstractList.SORT_BY_ITEM, true);
+	list.Sort(AIAbstractList.SORT_BY_ITEM, AIAbstractList.SORT_ASCENDING);
 	print("");
 	print(" Item Ascending");
 	list.AddItem( 5, 10);
@@ -545,7 +545,7 @@
 	print("--Industry--");
 	print("  GetIndustryCount():  " + AIIndustry.GetIndustryCount());
 	local list = AIIndustryList();
-	list.Sort(AIAbstractList.SORT_BY_ITEM, true);
+	list.Sort(AIAbstractList.SORT_BY_ITEM, AIAbstractList.SORT_ASCENDING);
 	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
 		if (AIIndustry.IsValidIndustry(i)) j++;
 		print("  Industry " + i);
@@ -667,7 +667,7 @@
 	print("  HasItem(1050): " + list.HasItem(1050));
 	print("  HasItem(1051): " + list.HasItem(1051));
 	print("  IsEmpty():     " + list.IsEmpty());
-	list.Sort(AIAbstractList.SORT_BY_ITEM, true);
+	list.Sort(AIAbstractList.SORT_BY_ITEM, AIAbstractList.SORT_ASCENDING);
 	print("  List Dump:");
 	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
 		print("    " + i + " => " + list.GetValue(i));
@@ -1088,7 +1088,7 @@
 	print("  RemoveSign(" + sign_id + "):                       " + AISign.RemoveSign(sign_id));
 	print("");
 	local list = AISignList();
-	list.Sort(AIAbstractList.SORT_BY_ITEM, true);
+	list.Sort(AIAbstractList.SORT_BY_ITEM, AIAbstractList.SORT_ASCENDING);
 	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
 		j++;
 		print("  Sign " + i);
@@ -1357,7 +1357,7 @@
 	print("--Town--");
 	print("  GetTownCount():    " + AITown.GetTownCount());
 	local list = AITownList();
-	list.Sort(AIAbstractList.SORT_BY_ITEM, true);
+	list.Sort(AIAbstractList.SORT_BY_ITEM, AIAbstractList.SORT_ASCENDING);
 	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
 		if (AITown.IsValidTown(i)) j++;
 		print("  Town " + i);
--- a/src/ai/api/ai_abstractlist.hpp
+++ b/src/ai/api/ai_abstractlist.hpp
@@ -25,6 +25,11 @@
 		SORT_BY_ITEM,  //!< Sort the list based on the item itself.
 	};
 
+	/** Sort ascending */
+	static const bool SORT_ASCENDING = true;
+	/** Sort descnding */
+	static const bool SORT_DESCENDING = false;
+
 private:
 	AIAbstractListSorter *sorter;
 	SorterType sorter_type;
@@ -123,6 +128,7 @@
 	 * @param sorter    the type of sorter to use
 	 * @param ascending if true, lowest value is on top, else at bottom.
 	 * @note the current item stays at the same place.
+	 * @see SORT_ASCENDING SORT_DESCENDING
 	 */
 	void Sort(SorterType sorter, bool ascending);
 
--- a/src/ai/api/ai_abstractlist.hpp.sq
+++ b/src/ai/api/ai_abstractlist.hpp.sq
@@ -24,6 +24,9 @@
 	SQAIAbstractList.DefSQConst(engine, AIAbstractList::SORT_BY_VALUE, "SORT_BY_VALUE");
 	SQAIAbstractList.DefSQConst(engine, AIAbstractList::SORT_BY_ITEM,  "SORT_BY_ITEM");
 
+	SQAIAbstractList.DefSQConst(engine, AIAbstractList::SORT_ASCENDING,  "SORT_ASCENDING");
+	SQAIAbstractList.DefSQConst(engine, AIAbstractList::SORT_DESCENDING, "SORT_DESCENDING");
+
 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::Clear,              "Clear",              1, "x");
 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::HasItem,            "HasItem",            2, "xi");
 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::Begin,              "Begin",              1, "x");
--- a/src/ai/api/squirrel_export.awk
+++ b/src/ai/api/squirrel_export.awk
@@ -34,6 +34,7 @@
 	enum_value_size = 0
 	enum_string_to_error_size = 0
 	enum_error_to_string_size = 0
+	const_size = 0
 	struct_size = 0
 	method_size = 0
 	static_method_size = 0
@@ -217,6 +218,17 @@
 	}
 	if (enum_value_size != 0) print ""
 
+	# Const values
+	mlen = 0
+	for (i = 1; i <= const_size; i++) {
+		if (mlen <= length(const_value[i])) mlen = length(const_value[i])
+	}
+	for (i = 1; i <= const_size; i++) {
+		print "	SQ" cls ".DefSQConst(engine, " cls "::" const_value[i] ", " substr(spaces, 1, mlen - length(const_value[i])) "\""  const_value[i] "\");"
+		delete const_value[i]
+	}
+	if (const_size != 0) print ""
+
 	# Mapping of OTTD strings to errors
 	mlen = 0
 	for (i = 1; i <= enum_string_to_error_size; i++) {
@@ -320,6 +332,13 @@
 	}
 }
 
+# Add a const (non-enum) value
+/^[ 	]*static const \w+ \w+ = \w+;$/ {
+	const_size++
+	const_value[const_size] = $4
+	next
+}
+
 # Add a method to the list
 /^.*\(.*\).*$/ {
 	if (cls_level != 1) next
--- a/src/script/squirrel.cpp
+++ b/src/script/squirrel.cpp
@@ -131,6 +131,13 @@
 	sq_newslot(this->vm, -3, SQTrue);
 }
 
+void Squirrel::AddConst(const char *var_name, bool value)
+{
+	sq_pushstring(this->vm, OTTD2FS(var_name), -1);
+	sq_pushbool(this->vm, value);
+	sq_newslot(this->vm, -3, SQTrue);
+}
+
 void Squirrel::AddClassBegin(const char *class_name)
 {
 	sq_pushroottable(this->vm);
--- a/src/script/squirrel.hpp
+++ b/src/script/squirrel.hpp
@@ -78,6 +78,12 @@
 	void AddConst(const char *var_name, int value);
 
 	/**
+	 * Adds a const to the stack. Depending on the current state this means
+	 *  either a const to a class or to the global space.
+	 */
+	void AddConst(const char *var_name, bool value);
+
+	/**
 	 * Adds a class to the global scope. Make sure to call AddClassEnd when you
 	 *  are done adding methods.
 	 */