(svn r10569) [NoAI] -Add: added AIListRandomize as Valuator for all lists to attach a random value to all items noai
authortruelight
Sat, 14 Jul 2007 23:49:31 +0000
branchnoai
changeset 9660 d0a430e8310b
parent 9659 ff5908205170
child 9661 32ce5530b9e3
(svn r10569) [NoAI] -Add: added AIListRandomize as Valuator for all lists to attach a random value to all items
bin/ai/regression/regression.nut
bin/ai/regression/regression.txt
projects/openttd.vcproj
projects/openttd_vs80.vcproj
source.list
src/ai/ai_squirrel.cpp
src/ai/api/ai_base.cpp
src/ai/api/ai_base.hpp
src/ai/api/ai_base.hpp.sq
src/ai/api/ai_list_valuator.hpp.sq
src/ai/api/ai_townlist_valuator.cpp
src/ai/api/ai_townlist_valuator.hpp
src/ai/api/ai_townlist_valuator.hpp.sq
--- a/bin/ai/regression/regression.nut	Sat Jul 14 23:35:46 2007 +0000
+++ b/bin/ai/regression/regression.nut	Sat Jul 14 23:49:31 2007 +0000
@@ -219,6 +219,11 @@
 	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
 		print("    " + i + " => " + list.GetValue(i));
 	}
+	list.Valuate(AIListRandomize());
+	print("  Randomize ListDump:");
+	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+		print("    " + i + " => " + list.GetValue(i));
+	}
 	list.Clear();
 	print("  IsEmpty():     " + list.IsEmpty());
 }
--- a/bin/ai/regression/regression.txt	Sat Jul 14 23:35:46 2007 +0000
+++ b/bin/ai/regression/regression.txt	Sat Jul 14 23:49:31 2007 +0000
@@ -866,6 +866,108 @@
     1097 => 0
     1098 => 0
     1099 => 0
+  Randomize ListDump:
+    1 => -2039536299
+    2 => 796838333
+    1000 => 438558874
+    1001 => 179516226
+    1002 => -1007339217
+    1003 => 859527945
+    1004 => 124767077
+    1005 => 1693978408
+    1006 => -880974321
+    1007 => 559618342
+    1008 => 1569811314
+    1009 => 870205254
+    1010 => -1758318443
+    1011 => -444945639
+    1012 => -1608782669
+    1013 => 433700979
+    1014 => -855225247
+    1015 => 1879656391
+    1016 => 398529614
+    1017 => -2049334382
+    1018 => 840638691
+    1019 => -1384047520
+    1020 => 1955145790
+    1021 => 477697425
+    1022 => -1395473168
+    1023 => -1470736319
+    1024 => 1816531430
+    1025 => 469019242
+    1026 => 39077628
+    1027 => 664188731
+    1028 => 1750781759
+    1029 => -647481353
+    1030 => 696701854
+    1031 => -1413895498
+    1032 => 604542808
+    1033 => 1987508951
+    1034 => -1069250041
+    1035 => 173906611
+    1036 => 680670954
+    1037 => 2073579869
+    1038 => -1595475211
+    1039 => -1988008457
+    1040 => -869393758
+    1041 => -833288209
+    1042 => 97291907
+    1043 => -1979472169
+    1044 => -922114803
+    1045 => 321893130
+    1046 => -270105978
+    1047 => 1294387584
+    1048 => -1952039371
+    1049 => -1954676003
+    1051 => -1104910473
+    1052 => -422921716
+    1053 => 1224064537
+    1054 => 1710423508
+    1055 => -1618366898
+    1056 => -1972920327
+    1057 => -1228206217
+    1058 => 946938986
+    1059 => -1701157824
+    1060 => 1071969693
+    1061 => -809033431
+    1062 => -934111918
+    1063 => -1106636240
+    1064 => -1988056095
+    1065 => -1117013134
+    1066 => 621979826
+    1067 => -2004975443
+    1068 => 1540115053
+    1069 => 275963171
+    1070 => 889543466
+    1071 => 201714893
+    1072 => 1632614672
+    1073 => 769082751
+    1074 => 162800763
+    1075 => -373023425
+    1076 => -1966408356
+    1077 => 2064251247
+    1078 => 1139816730
+    1079 => 671778950
+    1080 => 23001506
+    1081 => -542275239
+    1082 => -688757755
+    1083 => -1599747459
+    1084 => 1599536138
+    1085 => 1610850417
+    1086 => 492239132
+    1087 => -1594652903
+    1088 => -1716035643
+    1089 => -211545671
+    1090 => 1644622460
+    1091 => 1941915278
+    1092 => -1801759945
+    1093 => -789334533
+    1094 => 588603837
+    1095 => 608572342
+    1096 => 886201348
+    1097 => -1491076971
+    1098 => 1172943384
+    1099 => 9895172
   IsEmpty():     true
 
 --PathFinderStupid--
--- a/projects/openttd.vcproj	Sat Jul 14 23:35:46 2007 +0000
+++ b/projects/openttd.vcproj	Sat Jul 14 23:49:31 2007 +0000
@@ -1111,6 +1111,9 @@
 				RelativePath=".\..\src\ai\api\ai_list.hpp">
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_list_valuator.hpp">
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_map.hpp">
 			</File>
 			<File
@@ -1217,6 +1220,9 @@
 				RelativePath=".\..\src\ai\api\ai_list.cpp">
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_list_valuator.cpp">
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_map.cpp">
 			</File>
 			<File
--- a/projects/openttd_vs80.vcproj	Sat Jul 14 23:35:46 2007 +0000
+++ b/projects/openttd_vs80.vcproj	Sat Jul 14 23:49:31 2007 +0000
@@ -1700,6 +1700,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_list_valuator.hpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_map.hpp"
 				>
 			</File>
@@ -1840,6 +1844,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\ai\api\ai_list_valuator.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\ai\api\ai_map.cpp"
 				>
 			</File>
--- a/source.list	Sat Jul 14 23:35:46 2007 +0000
+++ b/source.list	Sat Jul 14 23:49:31 2007 +0000
@@ -343,6 +343,7 @@
 ai/api/ai_industrylist.hpp
 ai/api/ai_industrylist_valuator.hpp
 ai/api/ai_list.hpp
+ai/api/ai_list_valuator.hpp
 ai/api/ai_map.hpp
 ai/api/ai_object.hpp
 ai/api/ai_order.hpp
@@ -379,6 +380,7 @@
 ai/api/ai_industrylist.cpp
 ai/api/ai_industrylist_valuator.cpp
 ai/api/ai_list.cpp
+ai/api/ai_list_valuator.cpp
 ai/api/ai_map.cpp
 ai/api/ai_object.cpp
 ai/api/ai_order.cpp
--- a/src/ai/ai_squirrel.cpp	Sat Jul 14 23:35:46 2007 +0000
+++ b/src/ai/ai_squirrel.cpp	Sat Jul 14 23:49:31 2007 +0000
@@ -33,6 +33,7 @@
 #include "api/ai_industrylist.hpp.sq"
 #include "api/ai_industrylist_valuator.hpp.sq"
 #include "api/ai_list.hpp.sq"
+#include "api/ai_list_valuator.hpp.sq"
 #include "api/ai_map.hpp.sq"
 #include "api/ai_order.hpp.sq"
 #include "api/ai_pathfinder.hpp.sq"
@@ -220,6 +221,7 @@
 	SQAIIndustryListProductionRegister(this->engine);
 	SQAIIndustryListRegister(this->engine);
 	SQAIIndustryRegister(this->engine);
+	SQAIListRandomizeRegister(this->engine);
 	SQAIListRegister(this->engine);
 	SQAIMapRegister(this->engine);
 	SQAIOrderRegister(this->engine);
@@ -251,6 +253,7 @@
 	SQAITownListDistanceSquareToTileRegister(this->engine);
 	SQAITownListLocationRegister(this->engine);
 	SQAITownListPopulationRegister(this->engine);
+	SQAITownListRandomizeRegister(this->engine);
 	SQAITownListRegister(this->engine);
 	SQAITownRegister(this->engine);
 	SQAITransactionModeRegister(this->engine);
--- a/src/ai/api/ai_base.cpp	Sat Jul 14 23:35:46 2007 +0000
+++ b/src/ai/api/ai_base.cpp	Sat Jul 14 23:49:31 2007 +0000
@@ -4,7 +4,7 @@
 
 #include "ai_base.hpp"
 
-uint32 AIBase::Rand()
+/* static */ uint32 AIBase::Rand()
 {
 	/* We pick RandomRange if we are in SP (so when saved, we do the same over and over)
 	 *   but we pick InteractiveRandomRange if we are a network_server or network-client. */
@@ -12,7 +12,7 @@
 	return ::Random();
 }
 
-uint AIBase::RandRange(uint max)
+/* static */ uint AIBase::RandRange(uint max)
 {
 	/* We pick RandomRange if we are in SP (so when saved, we do the same over and over)
 	 *   but we pick InteractiveRandomRange if we are a network_server or network-client. */
@@ -20,7 +20,7 @@
 	return ::RandomRange(max);
 }
 
-bool AIBase::Chance(uint out, uint max)
+/* static */ bool AIBase::Chance(uint out, uint max)
 {
-	return (uint16)this->Rand() <= (uint16)((65536 * out) / max);
+	return (uint16)AIBase::Rand() <= (uint16)((65536 * out) / max);
 }
--- a/src/ai/api/ai_base.hpp	Sat Jul 14 23:35:46 2007 +0000
+++ b/src/ai/api/ai_base.hpp	Sat Jul 14 23:49:31 2007 +0000
@@ -29,21 +29,21 @@
 	 * Get a random value.
 	 * @return a random value between 0 and MAX(uint32).
 	 */
-	uint32 Rand();
+	static uint32 Rand();
 
 	/**
 	 * Get a random value in a range.
 	 * @param max the maximum value it will return.
 	 * @return a random value between 0 .. max.
 	 */
-	uint RandRange(uint max);
+	static uint RandRange(uint max);
 
 	/**
 	 * Returns approximatelly 'out' times true when called 'max' times.
 	 *   After all, it is a random function.
 	 * @return true if the chance worked out.
 	 */
-	bool Chance(uint out, uint max);
+	static bool Chance(uint out, uint max);
 };
 
 #endif /* AI_BASE_HPP */
--- a/src/ai/api/ai_base.hpp.sq	Sat Jul 14 23:35:46 2007 +0000
+++ b/src/ai/api/ai_base.hpp.sq	Sat Jul 14 23:49:31 2007 +0000
@@ -14,10 +14,9 @@
 	SQAIBase.AddConstructor<void (AIBase::*)(), 1>(engine, "x");
 
 	SQAIBase.DefSQStaticMethod(engine, &AIBase::GetClassName, "GetClassName", 1, "x");
-
-	SQAIBase.DefSQMethod(engine, &AIBase::Rand,      "Rand",      1, "x");
-	SQAIBase.DefSQMethod(engine, &AIBase::RandRange, "RandRange", 2, "xi");
-	SQAIBase.DefSQMethod(engine, &AIBase::Chance,    "Chance",    3, "xii");
+	SQAIBase.DefSQStaticMethod(engine, &AIBase::Rand,         "Rand",         1, "x");
+	SQAIBase.DefSQStaticMethod(engine, &AIBase::RandRange,    "RandRange",    2, "xi");
+	SQAIBase.DefSQStaticMethod(engine, &AIBase::Chance,       "Chance",       3, "xii");
 
 	SQAIBase.PostRegister(engine);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_list_valuator.hpp.sq	Sat Jul 14 23:49:31 2007 +0000
@@ -0,0 +1,19 @@
+#include "ai_list_valuator.hpp"
+
+namespace SQConvert {
+	/* Allow AIListRandomize to be used as Squirrel parameter */
+	template <> AIListRandomize *GetParam(ForceType<AIListRandomize *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIListRandomize *)instance; }
+	template <> AIListRandomize &GetParam(ForceType<AIListRandomize &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIListRandomize *)instance; }
+	template <> const AIListRandomize *GetParam(ForceType<const AIListRandomize *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIListRandomize *)instance; }
+	template <> const AIListRandomize &GetParam(ForceType<const AIListRandomize &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIListRandomize *)instance; }
+}; // namespace SQConvert
+
+void SQAIListRandomizeRegister(Squirrel *engine) {
+	DefSQClass <AIListRandomize> SQAIListRandomize("AIListRandomize");
+	SQAIListRandomize.PreRegister(engine);
+	SQAIListRandomize.AddConstructor<void (AIListRandomize::*)(), 1>(engine, "x");
+
+	SQAIListRandomize.DefSQStaticMethod(engine, &AIListRandomize::GetClassName, "GetClassName", 1, "x");
+
+	SQAIListRandomize.PostRegister(engine);
+}
--- a/src/ai/api/ai_townlist_valuator.cpp	Sat Jul 14 23:35:46 2007 +0000
+++ b/src/ai/api/ai_townlist_valuator.cpp	Sat Jul 14 23:49:31 2007 +0000
@@ -1,6 +1,12 @@
 #include "ai_townlist_valuator.hpp"
 #include "ai_town.hpp"
 #include "ai_map.hpp"
+#include "ai_base.hpp"
+
+int32 AITownListRandomize::Valuate(int32 town) const
+{
+	return AIBase::Rand();
+}
 
 int32 AITownListPopulation::Valuate(int32 town) const
 {
--- a/src/ai/api/ai_townlist_valuator.hpp	Sat Jul 14 23:35:46 2007 +0000
+++ b/src/ai/api/ai_townlist_valuator.hpp	Sat Jul 14 23:49:31 2007 +0000
@@ -8,6 +8,22 @@
 #include "ai_abstractlist.hpp"
 
 /**
+ * Give a random value for the entries in an AITownList instance.
+ * @note resulting items are of the type int32
+ * @note the input items are of the type TownID
+ */
+class AITownListRandomize : public AIAbstractList::Valuator {
+public:
+	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AITownListRandomize"; }
+
+private:
+	int32 Valuate(int32 town) const;
+};
+
+/**
  * Get the population for entries in an AITownList instance.
  * @note resulting items are of the type int32
  * @note the input items are of the type TownID
--- a/src/ai/api/ai_townlist_valuator.hpp.sq	Sat Jul 14 23:35:46 2007 +0000
+++ b/src/ai/api/ai_townlist_valuator.hpp.sq	Sat Jul 14 23:49:31 2007 +0000
@@ -1,6 +1,24 @@
 #include "ai_townlist_valuator.hpp"
 
 namespace SQConvert {
+	/* Allow AITownListRandomize to be used as Squirrel parameter */
+	template <> AITownListRandomize *GetParam(ForceType<AITownListRandomize *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITownListRandomize *)instance; }
+	template <> AITownListRandomize &GetParam(ForceType<AITownListRandomize &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITownListRandomize *)instance; }
+	template <> const AITownListRandomize *GetParam(ForceType<const AITownListRandomize *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITownListRandomize *)instance; }
+	template <> const AITownListRandomize &GetParam(ForceType<const AITownListRandomize &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITownListRandomize *)instance; }
+}; // namespace SQConvert
+
+void SQAITownListRandomizeRegister(Squirrel *engine) {
+	DefSQClass <AITownListRandomize> SQAITownListRandomize("AITownListRandomize");
+	SQAITownListRandomize.PreRegister(engine);
+	SQAITownListRandomize.AddConstructor<void (AITownListRandomize::*)(), 1>(engine, "x");
+
+	SQAITownListRandomize.DefSQStaticMethod(engine, &AITownListRandomize::GetClassName, "GetClassName", 1, "x");
+
+	SQAITownListRandomize.PostRegister(engine);
+}
+
+namespace SQConvert {
 	/* Allow AITownListPopulation to be used as Squirrel parameter */
 	template <> AITownListPopulation *GetParam(ForceType<AITownListPopulation *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AITownListPopulation *)instance; }
 	template <> AITownListPopulation &GetParam(ForceType<AITownListPopulation &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITownListPopulation *)instance; }