(svn r9622) [NoAI] -Add: added AITownList, which lists all towns (and which you can iterate)
[NoAI] -Add: added AITownListValuators, to get, for example, all population of the townlist (see regression-test for example)
[NoAI] -Fix: fixed AIList to allow Valuators in a simple and safe way (tnx a lot KUDr)
[NoAI] -Update: updated regression to test above changes
[NoAI] -Fix: Valuate() of valuators are no longer public (Rubidium)
--- a/bin/ai/regression/regression.nut Fri Apr 13 09:52:18 2007 +0000
+++ b/bin/ai/regression/regression.nut Sat Apr 14 09:44:18 2007 +0000
@@ -324,6 +324,23 @@
print(" GetTownCount(): " + town.GetTownCount());
}
+function Regression::TownList()
+{
+ local list = AITownList();
+
+ print("");
+ print("--TownList--");
+ print(" Count(): " + list.Count());
+ list.Valuate(AITownListGetPopulation());
+ list.KeepAboveValue(500);
+ print(" KeepAboveValue(500): done");
+ print(" Count(): " + list.Count());
+ print(" ListDump:");
+ for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+ print(" " + i + " => " + list.GetValue(i));
+ }
+}
+
function Regression::Vehicle()
{
local accounting = AIAccounting();
@@ -402,6 +419,7 @@
this.Road();
this.Sign();
this.Town();
+ this.TownList();
this.Vehicle();
/* Order has to be after Vehicle */
this.Order();
--- a/bin/ai/regression/regression.txt Fri Apr 13 09:52:18 2007 +0000
+++ b/bin/ai/regression/regression.txt Sat Apr 14 09:44:18 2007 +0000
@@ -869,6 +869,23 @@
Valid Towns: 28
GetTownCount(): 28
+--TownList--
+ Count(): 28
+ KeepAboveValue(500): done
+ Count(): 11
+ ListDump:
+ 12 => 766
+ 10 => 702
+ 15 => 699
+ 16 => 659
+ 23 => 657
+ 7 => 652
+ 24 => 624
+ 21 => 598
+ 6 => 566
+ 25 => 548
+ 0 => 538
+
--Vehicle--
Engine -1
IsValidEngine(): false
--- a/projects/openttd.vcproj Fri Apr 13 09:52:18 2007 +0000
+++ b/projects/openttd.vcproj Sat Apr 14 09:44:18 2007 +0000
@@ -1075,6 +1075,12 @@
RelativePath=".\..\src\ai\api\ai_town.hpp">
</File>
<File
+ RelativePath=".\..\src\ai\api\ai_townlist.hpp">
+ </File>
+ <File
+ RelativePath=".\..\src\ai\api\ai_townlist_valuator.hpp">
+ </File>
+ <File
RelativePath=".\..\src\ai\api\ai_transactionmode.hpp">
</File>
<File
@@ -1133,6 +1139,12 @@
RelativePath=".\..\src\ai\api\ai_town.cpp">
</File>
<File
+ RelativePath=".\..\src\ai\api\ai_townlist.cpp">
+ </File>
+ <File
+ RelativePath=".\..\src\ai\api\ai_townlist_valuator.cpp">
+ </File>
+ <File
RelativePath=".\..\src\ai\api\ai_transactionmode.cpp">
</File>
<File
--- a/projects/openttd_vs80.vcproj Fri Apr 13 09:52:18 2007 +0000
+++ b/projects/openttd_vs80.vcproj Sat Apr 14 09:44:18 2007 +0000
@@ -1652,6 +1652,14 @@
>
</File>
<File
+ RelativePath=".\..\src\ai\api\ai_townlist.hpp"
+ >
+ </File>
+ <File
+ RelativePath=".\..\src\ai\api\ai_townlist_valuator.hpp"
+ >
+ </File>
+ <File
RelativePath=".\..\src\ai\api\ai_transactionmode.hpp"
>
</File>
@@ -1728,6 +1736,14 @@
>
</File>
<File
+ RelativePath=".\..\src\ai\api\ai_townlist.cpp"
+ >
+ </File>
+ <File
+ RelativePath=".\..\src\ai\api\ai_townlist_valuator.cpp"
+ >
+ </File>
+ <File
RelativePath=".\..\src\ai\api\ai_transactionmode.cpp"
>
</File>
--- a/source.list Fri Apr 13 09:52:18 2007 +0000
+++ b/source.list Sat Apr 14 09:44:18 2007 +0000
@@ -330,6 +330,8 @@
ai/api/ai_sign.hpp
ai/api/ai_testmode.hpp
ai/api/ai_town.hpp
+ai/api/ai_townlist.hpp
+ai/api/ai_townlist_valuator.hpp
ai/api/ai_transactionmode.hpp
ai/api/ai_vehicle.hpp
@@ -350,6 +352,8 @@
ai/api/ai_sign.cpp
ai/api/ai_testmode.cpp
ai/api/ai_town.cpp
+ai/api/ai_townlist.cpp
+ai/api/ai_townlist_valuator.cpp
ai/api/ai_transactionmode.cpp
ai/api/ai_vehicle.cpp
--- a/src/ai/ai_squirrel.cpp Fri Apr 13 09:52:18 2007 +0000
+++ b/src/ai/ai_squirrel.cpp Sat Apr 14 09:44:18 2007 +0000
@@ -36,6 +36,8 @@
#include "api/ai_sign.hpp"
#include "api/ai_testmode.hpp"
#include "api/ai_town.hpp"
+#include "api/ai_townlist.hpp"
+#include "api/ai_townlist_valuator.hpp"
#include "api/ai_transactionmode.hpp"
#include "api/ai_vehicle.hpp"
#undef DEFINE_SQUIRREL_CLASS
@@ -205,6 +207,9 @@
SQAISettingsRegister(this->engine);
SQAISignRegister(this->engine);
SQAITestModeRegister(this->engine);
+ SQAITownListGetLocationRegister(this->engine);
+ SQAITownListGetPopulationRegister(this->engine);
+ SQAITownListRegister(this->engine);
SQAITownRegister(this->engine);
SQAITransactionModeRegister(this->engine);
SQAIVehicleRegister(this->engine);
--- a/src/ai/api/ai_list.cpp Fri Apr 13 09:52:18 2007 +0000
+++ b/src/ai/api/ai_list.cpp Sat Apr 14 09:44:18 2007 +0000
@@ -367,11 +367,11 @@
}
}
-void AIList::Valuate(AIList::Valuator *proc)
+void AIList::Valuate(const AIList::Valuator &proc)
{
this->buckets.clear();
for (AIListMap::iterator iter = this->items.begin(); iter != this->items.end(); iter++) {
- int32 value = proc->Valuate((*iter).first);
+ int32 value = proc.Valuate((*iter).first);
(*iter).second = value;
this->buckets[value].insert((*iter).first);
}
--- a/src/ai/api/ai_list.hpp Fri Apr 13 09:52:18 2007 +0000
+++ b/src/ai/api/ai_list.hpp Sat Apr 14 09:44:18 2007 +0000
@@ -175,23 +175,25 @@
* The definition how valuators should look.
*/
class Valuator {
+ friend class AIList;
public:
virtual ~Valuator() {}
- virtual int32 Valuate(const int32 item) = 0;
+ private:
+ virtual int32 Valuate(int32 item) const = 0;
};
/**
* Give all items a value defined by the valuator you give.
* @note the valuator should be a valid instance.
*/
- void Valuate(AIList::Valuator *proc);
+ void Valuate(const AIList::Valuator &proc);
};
#ifdef DEFINE_SQUIRREL_CLASS
namespace SQConvert {
/* Allow inner classes/structs to be used as Squirrel parameters */
- template <> AIList::Valuator *GetParam(ForceType<AIList::Valuator *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIList::Valuator *)instance; }
+ template <> const AIList::Valuator &GetParam(ForceType<const AIList::Valuator &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIList::Valuator *)instance; }
/* Allow AIList to be used as Squirrel parameter */
template <> AIList *GetParam(ForceType<AIList *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIList *)instance; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_townlist.cpp Sat Apr 14 09:44:18 2007 +0000
@@ -0,0 +1,10 @@
+#include "ai_townlist.hpp"
+#include "../../town.h"
+
+AITownList::AITownList()
+{
+ Town *t;
+ FOR_ALL_TOWNS(t) {
+ this->AddItem(t->index);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_townlist.hpp Sat Apr 14 09:44:18 2007 +0000
@@ -0,0 +1,43 @@
+/* $Id$ */
+
+/** @file ai_townlist.hpp list all the towns */
+
+#ifndef AI_TOWNLIST_HPP
+#define AI_TOWNLIST_HPP
+
+#include "ai_list.hpp"
+
+/**
+ * Class that creates a list of current towns.
+ */
+class AITownList : public AIList {
+public:
+ /**
+ * The name of the class, needed by several sub-processes.
+ */
+ static const char *GetClassName() { return "AITownList"; }
+
+ /**
+ * The constructor to make a list of towns.
+ */
+ AITownList();
+};
+
+#ifdef DEFINE_SQUIRREL_CLASS
+namespace SQConvert {
+ /* Allow AITownList to be used as Squirrel parameter */
+ template <> AITownList *GetParam(ForceType<AITownList *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AITownList *)instance; }
+}; // namespace SQConvert
+
+void SQAITownListRegister(Squirrel *engine) {
+ DefSQClass <AITownList> SQAITownList("AITownList");
+ SQAITownList.PreRegister(engine, "AIList");
+ SQAITownList.AddConstructor(engine);
+
+ SQAITownList.DefSQStaticMethod(engine, &AITownList::GetClassName, "GetClassName", 1, "x");
+
+ SQAITownList.PostRegister(engine);
+}
+#endif /* DEFINE_SQUIRREL_CLASS */
+
+#endif /* AI_TOWNLIST_HPP */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_townlist_valuator.cpp Sat Apr 14 09:44:18 2007 +0000
@@ -0,0 +1,12 @@
+#include "ai_townlist_valuator.hpp"
+#include "ai_town.hpp"
+
+int32 AITownListGetPopulation::Valuate(int32 town) const
+{
+ return AITown::GetPopulation(town);
+}
+
+int32 AITownListGetLocation::Valuate(int32 town) const
+{
+ return AITown::GetLocation(town);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_townlist_valuator.hpp Sat Apr 14 09:44:18 2007 +0000
@@ -0,0 +1,76 @@
+/* $Id$ */
+
+/** @file ai_townlist.hpp all the valuators for townlist */
+
+#ifndef AI_TOWNLIST_VALUATOR_HPP
+#define AI_TOWNLIST_VALUATOR_HPP
+
+#include "ai_list.hpp"
+
+/**
+ * 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
+ */
+class AITownListGetPopulation : public AIList::Valuator {
+public:
+ /**
+ * The name of the class, needed by several sub-processes.
+ */
+ static const char *GetClassName() { return "AITownListGetPopulation"; }
+
+private:
+ int32 Valuate(int32 town) const;
+};
+
+#ifdef DEFINE_SQUIRREL_CLASS
+namespace SQConvert {
+ /* Allow AITownListGetPopulation to be used as Squirrel parameter */
+ template <> AITownListGetPopulation *GetParam(ForceType<AITownListGetPopulation *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AITownListGetPopulation *)instance; }
+}; // namespace SQConvert
+
+void SQAITownListGetPopulationRegister(Squirrel *engine) {
+ DefSQClass <AITownListGetPopulation> SQAITownListGetPopulation("AITownListGetPopulation");
+ SQAITownListGetPopulation.PreRegister(engine);
+ SQAITownListGetPopulation.AddConstructor(engine);
+
+ SQAITownListGetPopulation.DefSQStaticMethod(engine, &AITownListGetPopulation::GetClassName, "GetClassName", 1, "x");
+
+ SQAITownListGetPopulation.PostRegister(engine);
+}
+#endif /* DEFINE_SQUIRREL_CLASS */
+
+/**
+ * Get the location for entries in an AITownList instance.
+ * @note resulting items are of the type TileIndex
+ * @note the input items are of the type TownID
+ */
+class AITownListGetLocation : public AIList::Valuator {
+public:
+ /**
+ * The name of the class, needed by several sub-processes.
+ */
+ static const char *GetClassName() { return "AITownListGetLocation"; }
+
+private:
+ int32 Valuate(int32 town) const;
+};
+
+#ifdef DEFINE_SQUIRREL_CLASS
+namespace SQConvert {
+ /* Allow AITownListGetLocation to be used as Squirrel parameter */
+ template <> AITownListGetLocation *GetParam(ForceType<AITownListGetLocation *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AITownListGetLocation *)instance; }
+}; // namespace SQConvert
+
+void SQAITownListGetLocationRegister(Squirrel *engine) {
+ DefSQClass <AITownListGetLocation> SQAITownListGetLocation("AITownListGetLocation");
+ SQAITownListGetLocation.PreRegister(engine);
+ SQAITownListGetLocation.AddConstructor(engine);
+
+ SQAITownListGetLocation.DefSQStaticMethod(engine, &AITownListGetLocation::GetClassName, "GetClassName", 1, "x");
+
+ SQAITownListGetLocation.PostRegister(engine);
+}
+#endif /* DEFINE_SQUIRREL_CLASS */
+
+#endif /* AI_TOWNLIST_VALUATOR_HPP */