--- a/bin/ai/regression/regression.nut Fri Jul 13 13:49:39 2007 +0000
+++ b/bin/ai/regression/regression.nut Fri Jul 13 14:14:26 2007 +0000
@@ -131,6 +131,28 @@
print(" GetIndustryCount(): " + industry.GetIndustryCount());
}
+function Regression::IndustryList()
+{
+ local list = AIIndustryList();
+
+ print("");
+ print("--IndustryList--");
+ print(" Count(): " + list.Count());
+ list.Valuate(AIIndustryListLocation());
+ print(" Location ListDump:");
+ for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+ print(" " + i + " => " + list.GetValue(i));
+ }
+ list.Valuate(AIIndustryListProduction(1));
+ list.KeepAboveValue(50);
+ print(" KeepAboveValue(50): done");
+ print(" Count(): " + list.Count());
+ print(" Production ListDump:");
+ for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+ print(" " + i + " => " + list.GetValue(i));
+ }
+}
+
function Regression::List()
{
local list = AIList();
@@ -464,11 +486,16 @@
print("");
print("--TownList--");
print(" Count(): " + list.Count());
+ list.Valuate(AITownListLocation());
+ print(" Location ListDump:");
+ for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+ print(" " + i + " => " + list.GetValue(i));
+ }
list.Valuate(AITownListPopulation());
list.KeepAboveValue(500);
print(" KeepAboveValue(500): done");
print(" Count(): " + list.Count());
- print(" ListDump:");
+ print(" Population ListDump:");
for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
print(" " + i + " => " + list.GetValue(i));
}
@@ -603,6 +630,7 @@
this.Cargo();
this.Company();
this.Industry();
+ this.IndustryList();
this.Map();
this.List();
this.Road();
--- a/bin/ai/regression/regression.txt Fri Jul 13 13:49:39 2007 +0000
+++ b/bin/ai/regression/regression.txt Fri Jul 13 14:14:26 2007 +0000
@@ -479,6 +479,89 @@
Valid Industries: 69
GetIndustryCount(): 69
+--IndustryList--
+ Count(): 69
+ Location ListDump:
+ 42 => 60866
+ 9 => 60050
+ 59 => 59867
+ 46 => 59604
+ 50 => 57432
+ 19 => 57429
+ 5 => 55429
+ 60 => 55360
+ 10 => 54184
+ 65 => 53096
+ 61 => 52953
+ 45 => 52931
+ 15 => 51854
+ 12 => 51419
+ 11 => 48499
+ 53 => 48206
+ 25 => 48182
+ 40 => 47838
+ 64 => 46278
+ 1 => 45122
+ 3 => 44640
+ 68 => 44181
+ 31 => 44160
+ 23 => 43035
+ 2 => 41929
+ 57 => 41813
+ 43 => 41155
+ 36 => 36908
+ 48 => 36847
+ 34 => 36728
+ 67 => 34375
+ 37 => 34237
+ 26 => 33934
+ 21 => 29147
+ 30 => 29022
+ 49 => 28287
+ 22 => 27822
+ 7 => 27609
+ 39 => 25545
+ 62 => 24893
+ 47 => 24366
+ 54 => 24005
+ 56 => 23727
+ 27 => 23714
+ 18 => 23682
+ 51 => 23519
+ 55 => 21107
+ 63 => 20819
+ 28 => 20170
+ 14 => 20001
+ 0 => 19695
+ 44 => 19529
+ 24 => 17943
+ 38 => 17742
+ 8 => 17318
+ 13 => 15950
+ 66 => 14818
+ 32 => 14701
+ 35 => 14361
+ 17 => 13947
+ 16 => 11734
+ 52 => 10773
+ 58 => 8852
+ 4 => 8793
+ 41 => 8763
+ 20 => 8562
+ 29 => 6685
+ 6 => 6533
+ 33 => 5659
+ KeepAboveValue(50): done
+ Count(): 7
+ Production ListDump:
+ 17 => 80
+ 15 => 72
+ 20 => 68
+ 18 => 68
+ 22 => 57
+ 16 => 56
+ 21 => 52
+
--Map--
GetMapSize(): 65536
GetMapSizeX(): 256
@@ -977,9 +1060,38 @@
--TownList--
Count(): 28
+ Location ListDump:
+ 23 => 59622
+ 7 => 59234
+ 5 => 55007
+ 22 => 51891
+ 14 => 51298
+ 8 => 51267
+ 1 => 46751
+ 27 => 45525
+ 15 => 42338
+ 3 => 41895
+ 4 => 41450
+ 26 => 39505
+ 6 => 38200
+ 12 => 32740
+ 2 => 28365
+ 17 => 24252
+ 20 => 22585
+ 24 => 19596
+ 16 => 17345
+ 25 => 16433
+ 0 => 15508
+ 11 => 14935
+ 18 => 10574
+ 21 => 9634
+ 13 => 9595
+ 19 => 6511
+ 10 => 6446
+ 9 => 5825
KeepAboveValue(500): done
Count(): 11
- ListDump:
+ Population ListDump:
12 => 766
10 => 702
15 => 699
--- a/source.list Fri Jul 13 13:49:39 2007 +0000
+++ b/source.list Fri Jul 13 14:14:26 2007 +0000
@@ -339,6 +339,8 @@
ai/api/ai_controller.hpp
ai/api/ai_execmode.hpp
ai/api/ai_industry.hpp
+ai/api/ai_industrylist.hpp
+ai/api/ai_industrylist_valuator.hpp
ai/api/ai_list.hpp
ai/api/ai_map.hpp
ai/api/ai_object.hpp
@@ -370,6 +372,8 @@
ai/api/ai_controller.cpp
ai/api/ai_execmode.cpp
ai/api/ai_industry.cpp
+ai/api/ai_industrylist.cpp
+ai/api/ai_industrylist_valuator.cpp
ai/api/ai_list.cpp
ai/api/ai_map.cpp
ai/api/ai_object.cpp
--- a/src/ai/ai_squirrel.cpp Fri Jul 13 13:49:39 2007 +0000
+++ b/src/ai/ai_squirrel.cpp Fri Jul 13 14:14:26 2007 +0000
@@ -29,6 +29,8 @@
#include "api/ai_controller.hpp.sq"
#include "api/ai_execmode.hpp.sq"
#include "api/ai_industry.hpp.sq"
+#include "api/ai_industrylist.hpp.sq"
+#include "api/ai_industrylist_valuator.hpp.sq"
#include "api/ai_list.hpp.sq"
#include "api/ai_map.hpp.sq"
#include "api/ai_order.hpp.sq"
@@ -208,6 +210,9 @@
SQAICompanyRegister(this->engine);
SQAIControllerRegister(this->engine);
SQAIExecModeRegister(this->engine);
+ SQAIIndustryListLocationRegister(this->engine);
+ SQAIIndustryListProductionRegister(this->engine);
+ SQAIIndustryListRegister(this->engine);
SQAIIndustryRegister(this->engine);
SQAIListRegister(this->engine);
SQAIMapRegister(this->engine);
--- a/src/ai/api/ai_industry.cpp Fri Jul 13 13:49:39 2007 +0000
+++ b/src/ai/api/ai_industry.cpp Fri Jul 13 14:14:26 2007 +0000
@@ -3,6 +3,7 @@
/** @file ai_industry.cpp handles the query-related of the AIIndustry class */
#include "ai_industry.hpp"
+#include "ai_cargo.hpp"
#include "../../industry.h"
#include "../../strings.h"
#include "../../variables.h" /* For SetDParam */
@@ -35,9 +36,21 @@
return industry_name;
}
-TileIndex AIIndustry::GetLocation(IndustryID industry_id)
+/* static */ TileIndex AIIndustry::GetProduction(IndustryID industry_id, CargoID cargo_id)
{
- if (!this->IsValidIndustry(industry_id)) return INVALID_TILE;
+ if (!AIIndustry::IsValidIndustry(industry_id)) return -1;
+ if (!AICargo::IsValidCargo(cargo_id)) return -1;
+
const Industry *i = ::GetIndustry(industry_id);
- return i->xy;
+ const IndustrySpec *indsp = GetIndustrySpec(i->type);
+
+ if (indsp->produced_cargo[0] == cargo_id) return i->this_month_production[0];
+ if (indsp->produced_cargo[1] == cargo_id) return i->this_month_production[1];
+ return -1;
}
+
+/* static */ TileIndex AIIndustry::GetLocation(IndustryID industry_id)
+{
+ if (!AIIndustry::IsValidIndustry(industry_id)) return INVALID_TILE;
+ return ::GetIndustry(industry_id)->xy;
+}
--- a/src/ai/api/ai_industry.hpp Fri Jul 13 13:49:39 2007 +0000
+++ b/src/ai/api/ai_industry.hpp Fri Jul 13 14:14:26 2007 +0000
@@ -50,13 +50,23 @@
char *GetName(IndustryID industry_id);
/**
+ * Gets the production of a cargo of the industry.
+ * @param industry_id the index of the industry.
+ * @param cargo_id the index of the cargo.
+ * @pre industry_id has to be valid (use IsValidIndustry()).
+ * @pre cargo_id has to be valid (use IsValidCargo()).
+ * @return the production of the cargo for this industry.
+ */
+ static TileIndex GetProduction(IndustryID industry_id, CargoID cargo_id);
+
+ /**
* Gets the location of the industry.
- * @param industry_id the location of the industry.
+ * @param industry_id the index of the industry.
* @pre industry_id has to be valid (use IsValidIndustry()).
* @return the location of the industry.
* @post return value is always positive and below AIMap::GetMapSize().
*/
- TileIndex GetLocation(IndustryID industry_id);
+ static TileIndex GetLocation(IndustryID industry_id);
};
#endif /* AI_INDUSTRY_HPP */
--- a/src/ai/api/ai_industry.hpp.sq Fri Jul 13 13:49:39 2007 +0000
+++ b/src/ai/api/ai_industry.hpp.sq Fri Jul 13 14:14:26 2007 +0000
@@ -15,11 +15,12 @@
SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetClassName, "GetClassName", 1, "x");
SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::IsValidIndustry, "IsValidIndustry", 2, "xi");
+ SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetProduction, "GetProduction", 3, "xii");
+ SQAIIndustry.DefSQStaticMethod(engine, &AIIndustry::GetLocation, "GetLocation", 2, "xi");
SQAIIndustry.DefSQMethod(engine, &AIIndustry::GetMaxIndustryID, "GetMaxIndustryID", 1, "x");
SQAIIndustry.DefSQMethod(engine, &AIIndustry::GetIndustryCount, "GetIndustryCount", 1, "x");
SQAIIndustry.DefSQMethod(engine, &AIIndustry::GetName, "GetName", 2, "xi");
- SQAIIndustry.DefSQMethod(engine, &AIIndustry::GetLocation, "GetLocation", 2, "xi");
SQAIIndustry.PostRegister(engine);
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_industrylist.cpp Fri Jul 13 14:14:26 2007 +0000
@@ -0,0 +1,10 @@
+#include "ai_industrylist.hpp"
+#include "../../industry.h"
+
+AIIndustryList::AIIndustryList()
+{
+ Industry *i;
+ FOR_ALL_INDUSTRIES(i) {
+ this->AddItem(i->index);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_industrylist.hpp Fri Jul 13 14:14:26 2007 +0000
@@ -0,0 +1,26 @@
+/* $Id$ */
+
+/** @file ai_industrylist.hpp list all the industries */
+
+#ifndef AI_INDUSTRYLIST_HPP
+#define AI_INDUSTRYLIST_HPP
+
+#include "ai_abstractlist.hpp"
+
+/**
+ * Class that creates a list of current industries.
+ */
+class AIIndustryList : public AIAbstractList {
+public:
+ /**
+ * The name of the class, needed by several sub-processes.
+ */
+ static const char *GetClassName() { return "AIIndustryList"; }
+
+ /**
+ * The constructor to make a list of industries.
+ */
+ AIIndustryList();
+};
+
+#endif /* AI_INDUSTRYLIST_HPP */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_industrylist.hpp.sq Fri Jul 13 14:14:26 2007 +0000
@@ -0,0 +1,19 @@
+#include "ai_industrylist.hpp"
+
+namespace SQConvert {
+ /* Allow AIIndustryList to be used as Squirrel parameter */
+ template <> AIIndustryList *GetParam(ForceType<AIIndustryList *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIIndustryList *)instance; }
+ template <> AIIndustryList &GetParam(ForceType<AIIndustryList &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIIndustryList *)instance; }
+ template <> const AIIndustryList *GetParam(ForceType<const AIIndustryList *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIIndustryList *)instance; }
+ template <> const AIIndustryList &GetParam(ForceType<const AIIndustryList &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIIndustryList *)instance; }
+}; // namespace SQConvert
+
+void SQAIIndustryListRegister(Squirrel *engine) {
+ DefSQClass <AIIndustryList> SQAIIndustryList("AIIndustryList");
+ SQAIIndustryList.PreRegister(engine, "AIAbstractList");
+ SQAIIndustryList.AddConstructor<void (AIIndustryList::*)(), 1>(engine, "x");
+
+ SQAIIndustryList.DefSQStaticMethod(engine, &AIIndustryList::GetClassName, "GetClassName", 1, "x");
+
+ SQAIIndustryList.PostRegister(engine);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_industrylist_valuator.cpp Fri Jul 13 14:14:26 2007 +0000
@@ -0,0 +1,12 @@
+#include "ai_industrylist_valuator.hpp"
+#include "ai_industry.hpp"
+
+int32 AIIndustryListProduction::Valuate(int32 industry) const
+{
+ return AIIndustry::GetProduction(industry, this->cargo_type);
+}
+
+int32 AIIndustryListLocation::Valuate(int32 industry) const
+{
+ return AIIndustry::GetLocation(industry);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_industrylist_valuator.hpp Fri Jul 13 14:14:26 2007 +0000
@@ -0,0 +1,49 @@
+/* $Id$ */
+
+/** @file ai_industrylist_valuator.hpp all the valuators for industrylist */
+
+#ifndef AI_INDUSTRYLIST_VALUATOR_HPP
+#define AI_INDUSTRYLIST_VALUATOR_HPP
+
+#include "ai_abstractlist.hpp"
+
+/**
+ * Get the production of the cargo for entries in an AIIndustryList instance.
+ * @note resulting items are of the type int32
+ * @note the input items are of the type IndustryID
+ */
+class AIIndustryListProduction : public AIAbstractList::Valuator {
+public:
+ /**
+ * The name of the class, needed by several sub-processes.
+ */
+ static const char *GetClassName() { return "AIIndustryListProduction"; }
+
+ /**
+ * Custom constructor, we want a cargo-type as parameter.
+ */
+ AIIndustryListProduction(CargoID cargo_type) { this->cargo_type = cargo_type; }
+
+private:
+ CargoID cargo_type;
+
+ int32 Valuate(int32 industry) const;
+};
+
+/**
+ * Get the location for entries in an AIIndustryList instance.
+ * @note resulting items are of the type TileIndex
+ * @note the input items are of the type IndustryID
+ */
+class AIIndustryListLocation : public AIAbstractList::Valuator {
+public:
+ /**
+ * The name of the class, needed by several sub-processes.
+ */
+ static const char *GetClassName() { return "AIIndustryListGetLocation"; }
+
+private:
+ int32 Valuate(int32 industry) const;
+};
+
+#endif /* AI_INDUSTRYLIST_VALUATOR_HPP */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/api/ai_industrylist_valuator.hpp.sq Fri Jul 13 14:14:26 2007 +0000
@@ -0,0 +1,37 @@
+#include "ai_industrylist_valuator.hpp"
+
+namespace SQConvert {
+ /* Allow AIIndustryListProduction to be used as Squirrel parameter */
+ template <> AIIndustryListProduction *GetParam(ForceType<AIIndustryListProduction *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIIndustryListProduction *)instance; }
+ template <> AIIndustryListProduction &GetParam(ForceType<AIIndustryListProduction &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIIndustryListProduction *)instance; }
+ template <> const AIIndustryListProduction *GetParam(ForceType<const AIIndustryListProduction *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIIndustryListProduction *)instance; }
+ template <> const AIIndustryListProduction &GetParam(ForceType<const AIIndustryListProduction &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIIndustryListProduction *)instance; }
+}; // namespace SQConvert
+
+void SQAIIndustryListProductionRegister(Squirrel *engine) {
+ DefSQClass <AIIndustryListProduction> SQAIIndustryListProduction("AIIndustryListProduction");
+ SQAIIndustryListProduction.PreRegister(engine);
+ SQAIIndustryListProduction.AddConstructor<void (AIIndustryListProduction::*)(CargoID cargo_type), 2>(engine, "xi");
+
+ SQAIIndustryListProduction.DefSQStaticMethod(engine, &AIIndustryListProduction::GetClassName, "GetClassName", 1, "x");
+
+ SQAIIndustryListProduction.PostRegister(engine);
+}
+
+namespace SQConvert {
+ /* Allow AIIndustryListLocation to be used as Squirrel parameter */
+ template <> AIIndustryListLocation *GetParam(ForceType<AIIndustryListLocation *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIIndustryListLocation *)instance; }
+ template <> AIIndustryListLocation &GetParam(ForceType<AIIndustryListLocation &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIIndustryListLocation *)instance; }
+ template <> const AIIndustryListLocation *GetParam(ForceType<const AIIndustryListLocation *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIIndustryListLocation *)instance; }
+ template <> const AIIndustryListLocation &GetParam(ForceType<const AIIndustryListLocation &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIIndustryListLocation *)instance; }
+}; // namespace SQConvert
+
+void SQAIIndustryListLocationRegister(Squirrel *engine) {
+ DefSQClass <AIIndustryListLocation> SQAIIndustryListLocation("AIIndustryListLocation");
+ SQAIIndustryListLocation.PreRegister(engine);
+ SQAIIndustryListLocation.AddConstructor<void (AIIndustryListLocation::*)(), 1>(engine, "x");
+
+ SQAIIndustryListLocation.DefSQStaticMethod(engine, &AIIndustryListLocation::GetClassName, "GetClassName", 1, "x");
+
+ SQAIIndustryListLocation.PostRegister(engine);
+}