Changeset - r12557:c2a1f0721f17
[Not reviewed]
master
0 6 0
yexo - 15 years ago 2009-07-31 19:50:01
yexo@openttd.org
(svn r17005) -Add [NoAI]: AIAbstractList::SORT_ASCENDING / SORT_DESCENDING
6 files changed with 48 insertions and 7 deletions:
0 comments (0 inline, 0 general)
bin/ai/regression/regression.nut
Show inline comments
 
@@ -55,13 +55,13 @@ function Regression::TestInit()
 
	print("");
 
	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
 
		print("   " + i);
 
	}
 

	
 
	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);
 
	list.AddItem(10, 10);
 
	list.AddItem(15, 20);
 
	list.AddItem(20, 20);
 
@@ -90,13 +90,13 @@ function Regression::TestInit()
 
	print("");
 
	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
 
		print("   " + i);
 
	}
 

	
 
	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);
 
	list.AddItem(10, 10);
 
	list.AddItem(15, 20);
 
	list.AddItem(20, 20);
 
@@ -125,13 +125,13 @@ function Regression::TestInit()
 
	print("");
 
	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
 
		print("   " + i);
 
	}
 

	
 
	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);
 
	list.AddItem(10, 10);
 
	list.AddItem(15, 20);
 
	list.AddItem(20, 20);
 
@@ -542,13 +542,13 @@ function Regression::Industry()
 
	local j = 0;
 

	
 
	print("");
 
	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);
 
		print("    IsValidIndustry(): " + AIIndustry.IsValidIndustry(i));
 
		print("    GetName():         " + AIIndustry.GetName(i));
 
		print("    GetLocation():     " + AIIndustry.GetLocation(i));
 
@@ -664,13 +664,13 @@ function Regression::List()
 
	list.RemoveItem(1150);
 
	list.ChangeItem(1051, 12);
 
	print("  Count():       " + list.Count());
 
	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));
 
	}
 
	list.Valuate(CustomValuator);
 
	print("  Custom ListDump:");
 
@@ -1085,13 +1085,13 @@ function Regression::Sign()
 
	print("  SetName(1, 'Test2'):                 " + AISign.SetName(1, "Test2"));
 
	local sign_id = AISign.BuildSign(33409, "Some other Sign");
 
	print("  BuildSign(33409, 'Some other Sign'): " + sign_id);
 
	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);
 
		print("    IsValidSign():   " + AISign.IsValidSign(i));
 
		print("    GetName():       " + AISign.GetName(i));
 
		print("    GetLocation():   " + AISign.GetLocation(i));
 
@@ -1354,13 +1354,13 @@ function Regression::Town()
 
	local j = 0;
 

	
 
	print("");
 
	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);
 
		print("    IsValidTown():   " + AITown.IsValidTown(i));
 
		print("    GetName():       " + AITown.GetName(i));
 
		print("    GetPopulation(): " + AITown.GetPopulation(i));
src/ai/api/ai_abstractlist.hpp
Show inline comments
 
@@ -22,12 +22,17 @@ public:
 
	/** Type of sorter */
 
	enum SorterType {
 
		SORT_BY_VALUE, //!< Sort the list based on the value of the item.
 
		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;
 
	bool sort_ascending;
 
	bool initialized;
 

	
 
@@ -120,12 +125,13 @@ public:
 

	
 
	/**
 
	 * Sort this list by the given sorter and direction.
 
	 * @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);
 

	
 
	/**
 
	 * Add one list to an other one.
 
	 * @param list The list that will be added to the caller.
src/ai/api/ai_abstractlist.hpp.sq
Show inline comments
 
@@ -21,12 +21,15 @@ void SQAIAbstractList_Register(Squirrel 
 
	SQAIAbstractList.PreRegister(engine);
 
	SQAIAbstractList.AddConstructor<void (AIAbstractList::*)(), 1>(engine, "x");
 

	
 
	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");
 
	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::Next,               "Next",               1, "x");
 
	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::IsEmpty,            "IsEmpty",            1, "x");
 
	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::HasNext,            "HasNext",            1, "x");
src/ai/api/squirrel_export.awk
Show inline comments
 
@@ -31,12 +31,13 @@ function dump_class_templates(name) {
 

	
 
BEGIN {
 
	enum_size = 0
 
	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
 
	virtual_class = "false"
 
	super_cls = ""
 
	cls = ""
 
@@ -214,12 +215,23 @@ BEGIN {
 
	for (i = 1; i <= enum_value_size; i++) {
 
		print "	SQ" cls ".DefSQConst(engine, " cls "::" enum_value[i] ", " substr(spaces, 1, mlen - length(enum_value[i])) "\""  enum_value[i] "\");"
 
		delete enum_value[i]
 
	}
 
	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++) {
 
		if (mlen <= length(enum_string_to_error_mapping_string[i])) mlen = length(enum_string_to_error_mapping_string[i])
 
	}
 
	for (i = 1; i <= enum_string_to_error_size; i++) {
 
@@ -317,12 +329,19 @@ BEGIN {
 
			}
 
		}
 
		next
 
	}
 
}
 

	
 
# 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
 
	if (match($0, "~")) next
 

	
 
	is_static = match($0, "static")
src/script/squirrel.cpp
Show inline comments
 
@@ -128,12 +128,19 @@ void Squirrel::AddConst(const char *var_
 
{
 
	sq_pushstring(this->vm, OTTD2FS(var_name), -1);
 
	sq_pushinteger(this->vm, value);
 
	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);
 
	sq_pushstring(this->vm, OTTD2FS(class_name), -1);
 
	sq_newclass(this->vm, SQFalse);
 
}
src/script/squirrel.hpp
Show inline comments
 
@@ -75,12 +75,18 @@ public:
 
	 * 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, 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.
 
	 */
 
	void AddClassBegin(const char *class_name);
 

	
 
	/**
0 comments (0 inline, 0 general)