--- a/bin/ai/regression/regression.nut Wed Jul 09 13:32:13 2008 +0000
+++ b/bin/ai/regression/regression.nut Thu Jul 17 23:45:52 2008 +0000
@@ -354,6 +354,24 @@
for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
print(" " + i + " => " + list.GetValue(i));
}
+
+ list = AICargoList_IndustryAccepting(8);
+ print("");
+ print("--CargoList_IndustryAccepting--");
+ print(" Count(): " + list.Count());
+ print(" ListDump:");
+ for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+ print(" " + i);
+ }
+
+ list = AICargoList_IndustryProducing(4);
+ print("");
+ print("--CargoList_IndustryProducing--");
+ print(" Count(): " + list.Count());
+ print(" ListDump:");
+ for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+ print(" " + i);
+ }
}
function Regression::Company()
--- a/bin/ai/regression/regression.txt Wed Jul 09 13:32:13 2008 +0000
+++ b/bin/ai/regression/regression.txt Thu Jul 17 23:45:52 2008 +0000
@@ -1025,6 +1025,16 @@
4 => 16
0 => 11
+--CargoList_IndustryAccepting--
+ Count(): 1
+ ListDump:
+ 7
+
+--CargoList_IndustryProducing--
+ Count(): 1
+ ListDump:
+ 7
+
--Company--
SetCompanyName(): true
SetCompanyName(): true
--- a/src/ai/api/ai_cargolist.cpp Wed Jul 09 13:32:13 2008 +0000
+++ b/src/ai/api/ai_cargolist.cpp Thu Jul 17 23:45:52 2008 +0000
@@ -3,8 +3,10 @@
/** @file ai_cargolist.cpp Implementation of AICargoList and friends. */
#include "ai_cargolist.hpp"
+#include "ai_industry.hpp"
#include "../../openttd.h"
#include "../../cargotype.h"
+#include "../../industry.h"
AICargoList::AICargoList()
{
@@ -16,3 +18,28 @@
}
}
+AICargoList_IndustryAccepting::AICargoList_IndustryAccepting(IndustryID industry_id)
+{
+ if (!AIIndustry::IsValidIndustry(industry_id)) return;
+
+ Industry *ind = ::GetIndustry(industry_id);
+ for (uint i = 0; i < lengthof(ind->accepts_cargo); i++) {
+ CargoID cargo_id = ind->accepts_cargo[i];
+ if (cargo_id != CT_INVALID) {
+ this->AddItem(cargo_id);
+ }
+ }
+}
+
+AICargoList_IndustryProducing::AICargoList_IndustryProducing(IndustryID industry_id)
+{
+ if (!AIIndustry::IsValidIndustry(industry_id)) return;
+
+ Industry *ind = ::GetIndustry(industry_id);
+ for (uint i = 0; i < lengthof(ind->produced_cargo); i++) {
+ CargoID cargo_id = ind->produced_cargo[i];
+ if (cargo_id != CT_INVALID) {
+ this->AddItem(cargo_id);
+ }
+ }
+}
--- a/src/ai/api/ai_cargolist.hpp Wed Jul 09 13:32:13 2008 +0000
+++ b/src/ai/api/ai_cargolist.hpp Thu Jul 17 23:45:52 2008 +0000
@@ -17,4 +17,32 @@
AICargoList();
};
+/**
+ * Creates a list of cargos that the given industry accepts.
+ * @ingroup AIList
+ */
+class AICargoList_IndustryAccepting : public AIAbstractList {
+public:
+ static const char *GetClassName() { return "AICargoList_IndustryAccepting"; }
+
+ /**
+ * @param industry_id The industry to get the list of cargos it accepts from.
+ */
+ AICargoList_IndustryAccepting(IndustryID industry_id);
+};
+
+/**
+ * Creates a list of cargos that the given industry can produce.
+ * @ingroup AIList
+ */
+class AICargoList_IndustryProducing : public AIAbstractList {
+public:
+ static const char *GetClassName() { return "AICargoList_IndustryProducing"; }
+
+ /**
+ * @param industry_id The industry to get the list of cargos it produces from.
+ */
+ AICargoList_IndustryProducing(IndustryID industry_id);
+};
+
#endif /* AI_CARGOLIST_HPP */
--- a/src/ai/api/ai_cargolist.hpp.sq Wed Jul 09 13:32:13 2008 +0000
+++ b/src/ai/api/ai_cargolist.hpp.sq Thu Jul 17 23:45:52 2008 +0000
@@ -21,3 +21,41 @@
SQAICargoList.PostRegister(engine);
}
+
+namespace SQConvert {
+ /* Allow AICargoList_IndustryAccepting to be used as Squirrel parameter */
+ template <> AICargoList_IndustryAccepting *GetParam(ForceType<AICargoList_IndustryAccepting *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AICargoList_IndustryAccepting *)instance; }
+ template <> AICargoList_IndustryAccepting &GetParam(ForceType<AICargoList_IndustryAccepting &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AICargoList_IndustryAccepting *)instance; }
+ template <> const AICargoList_IndustryAccepting *GetParam(ForceType<const AICargoList_IndustryAccepting *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AICargoList_IndustryAccepting *)instance; }
+ template <> const AICargoList_IndustryAccepting &GetParam(ForceType<const AICargoList_IndustryAccepting &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AICargoList_IndustryAccepting *)instance; }
+ template <> int Return<AICargoList_IndustryAccepting *>(HSQUIRRELVM vm, AICargoList_IndustryAccepting *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AICargoList_IndustryAccepting", res, NULL, DefSQDestructorCallback<AICargoList_IndustryAccepting>); return 1; }
+}; // namespace SQConvert
+
+void SQAICargoList_IndustryAccepting_Register(Squirrel *engine) {
+ DefSQClass <AICargoList_IndustryAccepting> SQAICargoList_IndustryAccepting("AICargoList_IndustryAccepting");
+ SQAICargoList_IndustryAccepting.PreRegister(engine, "AIAbstractList");
+ SQAICargoList_IndustryAccepting.AddConstructor<void (AICargoList_IndustryAccepting::*)(IndustryID industry_id), 2>(engine, "xi");
+
+ SQAICargoList_IndustryAccepting.DefSQStaticMethod(engine, &AICargoList_IndustryAccepting::GetClassName, "GetClassName", 1, "x");
+
+ SQAICargoList_IndustryAccepting.PostRegister(engine);
+}
+
+namespace SQConvert {
+ /* Allow AICargoList_IndustryProducing to be used as Squirrel parameter */
+ template <> AICargoList_IndustryProducing *GetParam(ForceType<AICargoList_IndustryProducing *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AICargoList_IndustryProducing *)instance; }
+ template <> AICargoList_IndustryProducing &GetParam(ForceType<AICargoList_IndustryProducing &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AICargoList_IndustryProducing *)instance; }
+ template <> const AICargoList_IndustryProducing *GetParam(ForceType<const AICargoList_IndustryProducing *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AICargoList_IndustryProducing *)instance; }
+ template <> const AICargoList_IndustryProducing &GetParam(ForceType<const AICargoList_IndustryProducing &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AICargoList_IndustryProducing *)instance; }
+ template <> int Return<AICargoList_IndustryProducing *>(HSQUIRRELVM vm, AICargoList_IndustryProducing *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AICargoList_IndustryProducing", res, NULL, DefSQDestructorCallback<AICargoList_IndustryProducing>); return 1; }
+}; // namespace SQConvert
+
+void SQAICargoList_IndustryProducing_Register(Squirrel *engine) {
+ DefSQClass <AICargoList_IndustryProducing> SQAICargoList_IndustryProducing("AICargoList_IndustryProducing");
+ SQAICargoList_IndustryProducing.PreRegister(engine, "AIAbstractList");
+ SQAICargoList_IndustryProducing.AddConstructor<void (AICargoList_IndustryProducing::*)(IndustryID industry_id), 2>(engine, "xi");
+
+ SQAICargoList_IndustryProducing.DefSQStaticMethod(engine, &AICargoList_IndustryProducing::GetClassName, "GetClassName", 1, "x");
+
+ SQAICargoList_IndustryProducing.PostRegister(engine);
+}
--- a/src/ai/api/ai_controller.cpp Wed Jul 09 13:32:13 2008 +0000
+++ b/src/ai/api/ai_controller.cpp Thu Jul 17 23:45:52 2008 +0000
@@ -103,6 +103,8 @@
SQAIBridgeList_Length_Register(this->engine);
SQAICargo_Register(this->engine);
SQAICargoList_Register(this->engine);
+ SQAICargoList_IndustryAccepting_Register(this->engine);
+ SQAICargoList_IndustryProducing_Register(this->engine);
SQAICompany_Register(this->engine);
SQAIController_Register(this->engine);
SQAIDate_Register(this->engine);