(svn r9202) [NoAI] -Add: some initial functions for cargo and industries.
--- a/bin/ai/SQNoAI/main.nut Thu Mar 15 13:36:45 2007 +0000
+++ b/bin/ai/SQNoAI/main.nut Thu Mar 15 13:50:58 2007 +0000
@@ -1,13 +1,17 @@
/* Extend our own AI from AIController so we can access basic things. */
class SQNoAI extends AIController {
- base = null;
- company = null;
- map = null;
- town = null;
+ base = null;
+ cargo = null;
+ company = null;
+ industry = null;
+ map = null;
+ town = null;
constructor() {
this.base = AIBase();
+ this.cargo = AICargo();
this.company = AICompany();
+ this.industry = AIIndustry();
this.map = AIMap();
this.town = AITown();
}
@@ -19,11 +23,31 @@
function SQNoAI::GameLoop()
{
if (this.GetTick() == 1) {
- this.company.SetCompanyName("SQNoAI");
+ if (!this/company.SetCompanyName("NoAI")) {
+ this.company.SetCompanyName("NoAI " + this->base.Random());
+ }
print("Map size: " + this.map.GetMapSizeX() + " by " +
this.map.GetMapSizeY() + ", " + this.map.GetMapSize() + " tiles");
}
+ if (this.GetTick() < 10) {
+ local cargo_label = this.cargo.GetCargoLabel(this.GetTick());
+ print(cargo_label + " is " + (this.cargo.IsFreight(this.GetTick()) ? "" : "not ") +
+ "freight and the income is " + this.cargo.GetCargoIncome(20, 10, this.GetTick()) +
+ " per 20 tiles in 10 days");
+ }
+
+ if (this.GetTick() < this.industry.GetMaxIndustryID()) {
+ if (this.industry.IsValidIndustry(this.GetTick())) {
+ local industry_name = this.industry.GetName(this.GetTick());
+ local t = this.industry.GetLocation(this.GetTick());
+
+ print("Industry " + industry_name + " [" + this->GetTick() + " of " +
+ this.industry.GetIndustryCount() + "] at (" + this.map.GetTileX(t) +
+ ", " + this.map.GetTileY(t));
+ }
+ }
+
if (this.GetTick() % 10 == 0) {
local company_name = this.company.GetCompanyName();
print(company_name + " has loaned " + this.company.GetLoanAmount());
@@ -36,9 +60,7 @@
if (this.GetTick() % 13 == 0) {
local town_id = this.base.RandomRange(this.town.GetMaxTownID());
- if (!this.town.IsValidTown(town_id)) {
- print("He, an invalid town ID... could happen :(");
- } else {
+ if (this.town.IsValidTown(town_id)) {
local town_name = this.town.GetName(town_id);
local t = this.town.GetLocation(town_id);
--- a/projects/openttd.vcproj Thu Mar 15 13:36:45 2007 +0000
+++ b/projects/openttd.vcproj Thu Mar 15 13:50:58 2007 +0000
@@ -901,12 +901,18 @@
RelativePath=".\..\src\ai\core\ai_base.hpp">
</File>
<File
+ RelativePath=".\..\src\ai\core\ai_cargo.hpp">
+ </File>
+ <File
RelativePath=".\..\src\ai\core\ai_company.hpp">
</File>
<File
RelativePath=".\..\src\ai\core\ai_controller.hpp">
</File>
<File
+ RelativePath=".\..\src\ai\core\ai_industry.hpp">
+ </File>
+ <File
RelativePath=".\..\src\ai\core\ai_map.hpp">
</File>
<File
@@ -926,12 +932,18 @@
RelativePath=".\..\src\ai\core\base\random.cpp">
</File>
<File
+ RelativePath=".\..\src\ai\core\cargo\query.cpp">
+ </File>
+ <File
RelativePath=".\..\src\ai\core\company\name.cpp">
</File>
<File
RelativePath=".\..\src\ai\core\company\money.cpp">
</File>
<File
+ RelativePath=".\..\src\ai\core\industry\query.cpp">
+ </File>
+ <File
RelativePath=".\..\src\ai\core\map\conversion.cpp">
</File>
<File
--- a/projects/openttd_vs80.vcproj Thu Mar 15 13:36:45 2007 +0000
+++ b/projects/openttd_vs80.vcproj Thu Mar 15 13:50:58 2007 +0000
@@ -1424,6 +1424,10 @@
>
</File>
<File
+ RelativePath=".\..\src\ai\core\ai_cargo.hpp"
+ >
+ </File>
+ <File
RelativePath=".\..\src\ai\core\ai_company.hpp"
>
</File>
@@ -1432,6 +1436,10 @@
>
</File>
<File
+ RelativePath=".\..\src\ai\core\ai_industry.hpp"
+ >
+ </File>
+ <File
RelativePath=".\..\src\ai\core\ai_map.hpp"
>
</File>
@@ -1456,6 +1464,10 @@
>
</File>
<File
+ RelativePath=".\..\src\ai\core\cargo\query.cpp"
+ >
+ </File>
+ <File
RelativePath=".\..\src\ai\core\company\name.cpp"
>
</File>
@@ -1464,6 +1476,10 @@
>
</File>
<File
+ RelativePath=".\..\src\ai\core\industry\query.cpp"
+ >
+ </File>
+ <File
RelativePath=".\..\src\ai\core\map\conversion.cpp"
>
</File>
--- a/source.list Thu Mar 15 13:36:45 2007 +0000
+++ b/source.list Thu Mar 15 13:50:58 2007 +0000
@@ -270,8 +270,10 @@
# AI Headers
ai/core/ai.h
ai/core/ai_base.hpp
+ai/core/ai_cargo.hpp
ai/core/ai_company.hpp
ai/core/ai_controller.hpp
+ai/core/ai_industry.hpp
ai/core/ai_map.hpp
ai/core/ai_object.hpp
ai/core/ai_town.hpp
@@ -279,8 +281,10 @@
# AI Core
ai/core/ai.cpp
ai/core/base/random.cpp
+ai/core/cargo/query.cpp
ai/core/company/name.cpp
ai/core/company/money.cpp
+ai/core/industry/query.cpp
ai/core/map/conversion.cpp
ai/core/map/query.cpp
ai/core/object/commands.cpp
--- a/src/ai/NoAI/NoAI.cpp Thu Mar 15 13:36:45 2007 +0000
+++ b/src/ai/NoAI/NoAI.cpp Thu Mar 15 13:50:58 2007 +0000
@@ -9,11 +9,34 @@
/* virtual */ void NoAI::GameLoop()
{
if (this->GetTick() == 1) {
- this->company.SetCompanyName("NoAI");
+ if (!this->company.SetCompanyName("NoAI")) {
+ char name[32];
+ snprintf(name, lengthof(name), "NoAI %d", this->base.Random());
+ this->company.SetCompanyName(name);
+ }
printf("Map size: %d by %d, %d tiles\n", this->map.GetMapSizeX(),
this->map.GetMapSizeY(), this->map.GetMapSize());
}
+ if (this->GetTick() < 10) {
+ char *cargo_label = this->cargo.GetCargoLabel(this->GetTick());
+ printf("%s is %sfreight and the income is %d per 20 tiles in 10 days\n",
+ cargo_label, this->cargo.IsFreight(this->GetTick()) ? "" : "not ",
+ this->cargo.GetCargoIncome(20, 10, this->GetTick()));
+ free(cargo_label);
+ }
+
+ if (this->GetTick() < this->industry.GetMaxIndustryID()) {
+ if (this->industry.IsValidIndustry(this->GetTick())) {
+ char *industry_name = this->industry.GetName(this->GetTick());
+ TileIndex t = this->industry.GetLocation(this->GetTick());
+
+ printf("Industry %s [%d of %d] at (%d, %d)\n",
+ industry_name, this->GetTick(), this->industry.GetIndustryCount(),
+ this->map.GetTileX(t), this->map.GetTileY(t));
+ }
+ }
+
if (this->GetTick() % 10 == 0) {
char *company_name = this->company.GetCompanyName();
printf("%s has loaned %d\n", company_name, this->company.GetLoanAmount());
@@ -27,9 +50,7 @@
if (this->GetTick() % 13 == 0) {
TownID town_id = this->base.RandomRange(this->town.GetMaxTownID());
- if (!this->town.IsValidTown(town_id)) {
- printf("He, an invalid town ID... could happen :(\n");
- } else {
+ if (this->town.IsValidTown(town_id)) {
char *town_name = this->town.GetName(town_id);
TileIndex t = this->town.GetLocation(town_id);
--- a/src/ai/NoAI/NoAI.hpp Thu Mar 15 13:36:45 2007 +0000
+++ b/src/ai/NoAI/NoAI.hpp Thu Mar 15 13:50:58 2007 +0000
@@ -8,14 +8,18 @@
#include "../core/ai_factory.hpp"
#include "../core/ai_controller.hpp"
#include "../core/ai_base.hpp"
+#include "../core/ai_cargo.hpp"
#include "../core/ai_company.hpp"
+#include "../core/ai_industry.hpp"
#include "../core/ai_map.hpp"
#include "../core/ai_town.hpp"
class NoAI: public AIController {
private:
AIBase base;
+ AICargo cargo;
AICompany company;
+ AIIndustry industry;
AIMap map;
AITown town;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/core/ai_cargo.hpp Thu Mar 15 13:50:58 2007 +0000
@@ -0,0 +1,50 @@
+/* $Id$ */
+
+/** @file ai_cargo.hpp Everything to query about cargos */
+
+#ifndef AI_CARGO_HPP
+#define AI_CARGO_HPP
+
+#include "ai_object.hpp"
+
+class AICargo : public AIObject {
+public:
+ /**
+ * Gets the string representation of the cargo label
+ * @param cargo_type to get the string representation of
+ * @return the cargo label
+ * @note the returned cargo label must be freed
+ */
+ char *GetCargoLabel(CargoID cargo_type);
+
+ /**
+ * Checks whether the give cargo is a freight or not
+ * @param cargo_type is this cargo freight or not?
+ * @return true if and only if the cargo is freight
+ */
+ bool IsFreight(CargoID cargo_type);
+
+ /**
+ * Get the income for transporting a piece of cargo over the
+ * given distance within the specified time.
+ * @param distance the distance the cargo travels from begin to end
+ * @param days_in_transit amount of (game) days the cargo is in transit
+ * @param cargo_type the cargo to transport
+ * @return the amount of money that would be made by this trip
+ */
+ int32 GetCargoIncome(uint32 distance, uint32 days_in_transit, CargoID cargo_type);
+};
+
+#ifdef SQUIRREL_CLASS
+void SQAICargoRegister(SquirrelEngine *engine) {
+ DefSQClass <AICargo> SQAICargo("AICargo");
+ SQAICargo.PreRegister(engine);
+ SQAICargo.AddConstructor(engine);
+ SQAICargo.DefSQFunction(engine, &AICargo::GetCargoLabel, "GetCargoLabel");
+ SQAICargo.DefSQFunction(engine, &AICargo::IsFreight, "IsFreight");
+ SQAICargo.DefSQFunction(engine, &AICargo::GetCargoIncome, "GetCargoIncome");
+ SQAICargo.PostRegister(engine);
+}
+#endif /* SQUIRREL_CLASS */
+
+#endif /* AI_CARGO_HPP */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/core/ai_industry.hpp Thu Mar 15 13:50:58 2007 +0000
@@ -0,0 +1,66 @@
+/* $Id$ */
+
+/** @file ai_industry.hpp Everything to query about industries */
+
+#ifndef AI_INDUSTRY_HPP
+#define AI_INDUSTRY_HPP
+
+#include "ai_object.hpp"
+
+class AIIndustry : public AIObject {
+public:
+ /**
+ * Gets the maximum industry index; there are no valid industries with a higher index
+ * @return the maximum town index
+ * @post return >= 0
+ */
+ IndustryID GetMaxIndustryID();
+
+ /**
+ * Gets the number of industries
+ * @return the number of industries
+ * @post return >= 0
+ */
+ int32 GetIndustryCount();
+
+ /**
+ * Checks whether the given industry index is valid
+ * @param industry_id the index to check
+ * @return true if and only if the industry is valid
+ */
+ bool IsValidIndustry(IndustryID industry_id);
+
+ /**
+ * Get the name of the industry
+ * @param industry_id the industry to get the name of
+ * @pre this->IsValidIndustry(industry_id)
+ * @return the name of the industry
+ * @note the returned name must be freed
+ */
+ char *GetName(IndustryID industry_id);
+
+ /**
+ * Gets the location of the industry
+ * @param industry_id the location of the industry
+ * @pre this->IsValidIndustry(industry_id)
+ * @return the location of the industry
+ * @post return >= 0
+ */
+ TileIndex GetLocation(IndustryID industry_id);
+};
+
+#ifdef SQUIRREL_CLASS
+void SQAIIndustryRegister(SquirrelEngine *engine) {
+ DefSQClass <AIIndustry> SQAIIndustry("AIIndustry");
+ SQAIIndustry.PreRegister(engine);
+ SQAIIndustry.AddConstructor(engine);
+ SQAIIndustry.DefSQFunction(engine, &AIIndustry::GetMaxIndustryID, "GetMaxIndustryID");
+ SQAIIndustry.DefSQFunction(engine, &AIIndustry::GetIndustryCount, "GetIndustryCount");
+ SQAIIndustry.DefSQFunction(engine, &AIIndustry::IsValidIndustry, "IsValidIndustry");
+ SQAIIndustry.DefSQFunction(engine, &AIIndustry::GetName, "GetName");
+ SQAIIndustry.DefSQFunction(engine, &AIIndustry::GetLocation, "GetLocation");
+ SQAIIndustry.PostRegister(engine);
+}
+#endif /* SQUIRREL_CLASS */
+
+#endif /* AI_INDUSTRY_HPP */
--- a/src/ai/core/ai_town.hpp Thu Mar 15 13:36:45 2007 +0000
+++ b/src/ai/core/ai_town.hpp Thu Mar 15 13:50:58 2007 +0000
@@ -42,17 +42,17 @@
/**
* Gets the number of inhabitants in the town
* @param town_id the town to get the name of
- * @pre this->IsValidTown(town)
+ * @pre this->IsValidTown(town_id)
* @return the number of inhabitants
* @post return >= 0
*/
int32 GetPopulation(TownID town_id);
/**
- * Gets the location of inhabitants in the town
+ * Gets the location of the town
* @param town_id the location of the town
- * @pre this->IsValidTown(town)
- * @return the number of inhabitants
+ * @pre this->IsValidTown(town_id)
+ * @return the location of the town
* @post return >= 0
*/
TileIndex GetLocation(TownID town_id);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/core/cargo/query.cpp Thu Mar 15 13:50:58 2007 +0000
@@ -0,0 +1,34 @@
+/* $Id$ */
+
+/** @file query.cpp handles the query-related of the AICargo class */
+
+#include "../ai_cargo.hpp"
+#include "../../../cargotype.h"
+#include "../../../player.h" // For economy.h
+#include "../../../economy.h"
+
+char *AICargo::GetCargoLabel(CargoID cargo_type)
+{
+ const CargoSpec *cargo = GetCargo(cargo_type);
+ if (cargo == NULL) return NULL;
+
+ /* cargo->label is a uint32 packing a 4 character non-terminated string,
+ * like "PASS", "COAL", "OIL_". New ones can be defined by NewGRFs */
+ char *cargo_label = MallocT<char>(sizeof(cargo->label) + 1);
+ for (uint i = 0; i < sizeof(cargo->label); i++) {
+ cargo_label[i] = GB(cargo->label, (sizeof(cargo->label) - i - 1) * 8, 8);
+ }
+ cargo_label[sizeof(cargo->label)] = '\0';
+ return cargo_label;
+}
+
+bool AICargo::IsFreight(CargoID cargo_type)
+{
+ const CargoSpec *cargo = GetCargo(cargo_type);
+ return cargo != NULL && cargo->is_freight;
+}
+
+int32 AICargo::GetCargoIncome(uint32 distance, uint32 days_in_transit, CargoID cargo_type)
+{
+ return GetTransportedGoodsIncome(1, distance, days_in_transit, cargo_type);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ai/core/industry/query.cpp Thu Mar 15 13:50:58 2007 +0000
@@ -0,0 +1,41 @@
+/* $Id$ */
+
+/** @file query.cpp handles the query-related of the AIIndustry class */
+
+#include "../ai_industry.hpp"
+#include "../../../industry.h"
+#include "../../../strings.h"
+#include "../../../variables.h" /* For SetDParam */
+#include "table/strings.h"
+
+IndustryID AIIndustry::GetMaxIndustryID()
+{
+ return ::GetMaxIndustryIndex();
+}
+
+int32 AIIndustry::GetIndustryCount()
+{
+ return ::GetNumIndustries();
+}
+
+bool AIIndustry::IsValidIndustry(IndustryID industry_id)
+{
+ return ::IsValidIndustryID(industry_id);
+}
+
+char *AIIndustry::GetName(IndustryID industry_id)
+{
+ static const int len = 64;
+ char *industry_name = MallocT<char>(len);
+
+ SetDParam(0, industry_id);
+ GetString(industry_name, STR_INDUSTRY, &industry_name[len - 1]);
+
+ return industry_name;
+}
+
+TileIndex AIIndustry::GetLocation(IndustryID industry_id)
+{
+ const Industry *i = ::GetIndustry(industry_id);
+ return i->xy;
+}
--- a/src/ai/core/town/query.cpp Thu Mar 15 13:36:45 2007 +0000
+++ b/src/ai/core/town/query.cpp Thu Mar 15 13:50:58 2007 +0000
@@ -6,6 +6,7 @@
#include "../../../town.h"
#include "../../../strings.h"
#include "../../../variables.h" /* For SetDParam */
+#include "table/strings.h"
TownID AITown::GetMaxTownID()
{
@@ -27,9 +28,8 @@
static const int len = 64;
char *town_name = MallocT<char>(len);
- const Town *t = ::GetTown(town_id);
- SetDParam(0, t->townnameparts);
- GetString(town_name, t->townnametype, &town_name[len - 1]);
+ SetDParam(0, town_id);
+ GetString(town_name, STR_TOWN, &town_name[len - 1]);
return town_name;
}