summaryrefslogtreecommitdiff
path: root/src/ai/api
diff options
context:
space:
mode:
authortruebrain <truebrain@openttd.org>2011-11-23 16:05:19 +0000
committertruebrain <truebrain@openttd.org>2011-11-23 16:05:19 +0000
commit229e572663158d8fd37cabf3c2a3cb06811ff0a1 (patch)
treee27f01fdf1de01262be29bb795b0be8d54b04f5e /src/ai/api
parent4e09cde6495f35a6d56cacc0dbf636df7bc94dbd (diff)
downloadopenttd-229e572663158d8fd37cabf3c2a3cb06811ff0a1.tar.xz
(svn r23298) -Add: track statistics of all incoming and outgoing goods. Incoming based on TownEffect, outgoing based on CargoType (based on patch by Terkhen)
Diffstat (limited to 'src/ai/api')
-rw-r--r--src/ai/api/ai_cargo.cpp5
-rw-r--r--src/ai/api/ai_cargo.hpp7
-rw-r--r--src/ai/api/ai_cargo.hpp.sq13
-rw-r--r--src/ai/api/ai_changelog.hpp14
-rw-r--r--src/ai/api/ai_town.cpp29
-rw-r--r--src/ai/api/ai_town.hpp23
-rw-r--r--src/ai/api/ai_town.hpp.sq3
7 files changed, 64 insertions, 30 deletions
diff --git a/src/ai/api/ai_cargo.cpp b/src/ai/api/ai_cargo.cpp
index 1ff308c68..5c38c7107 100644
--- a/src/ai/api/ai_cargo.cpp
+++ b/src/ai/api/ai_cargo.cpp
@@ -20,6 +20,11 @@
return (cargo_type < NUM_CARGO && ::CargoSpec::Get(cargo_type)->IsValid());
}
+/* static */ bool AICargo::IsValidTownEffect(TownEffect towneffect_type)
+{
+ return (towneffect_type >= TE_BEGIN && towneffect_type < TE_END);
+}
+
/* static */ char *AICargo::GetCargoLabel(CargoID cargo_type)
{
if (!IsValidCargo(cargo_type)) return NULL;
diff --git a/src/ai/api/ai_cargo.hpp b/src/ai/api/ai_cargo.hpp
index ed658c0a4..0d6a86451 100644
--- a/src/ai/api/ai_cargo.hpp
+++ b/src/ai/api/ai_cargo.hpp
@@ -63,6 +63,13 @@ public:
static bool IsValidCargo(CargoID cargo_type);
/**
+ * Checks whether the given town effect type is valid.
+ * @param towneffect_type The town effect to check.
+ * @return True if and only if the town effect type is valid.
+ */
+ static bool IsValidTownEffect(TownEffect towneffect_type);
+
+ /**
* Gets the string representation of the cargo label.
* @param cargo_type The cargo to get the string representation of.
* @pre AICargo::IsValidCargo(cargo_type).
diff --git a/src/ai/api/ai_cargo.hpp.sq b/src/ai/api/ai_cargo.hpp.sq
index d94d9c1f0..a39e3b114 100644
--- a/src/ai/api/ai_cargo.hpp.sq
+++ b/src/ai/api/ai_cargo.hpp.sq
@@ -55,12 +55,13 @@ void SQAICargo_Register(Squirrel *engine)
SQAICargo.DefSQConst(engine, AICargo::CT_AUTO_REFIT, "CT_AUTO_REFIT");
SQAICargo.DefSQConst(engine, AICargo::CT_NO_REFIT, "CT_NO_REFIT");
- SQAICargo.DefSQStaticMethod(engine, &AICargo::IsValidCargo, "IsValidCargo", 2, ".i");
- SQAICargo.DefSQStaticMethod(engine, &AICargo::GetCargoLabel, "GetCargoLabel", 2, ".i");
- SQAICargo.DefSQStaticMethod(engine, &AICargo::IsFreight, "IsFreight", 2, ".i");
- SQAICargo.DefSQStaticMethod(engine, &AICargo::HasCargoClass, "HasCargoClass", 3, ".ii");
- SQAICargo.DefSQStaticMethod(engine, &AICargo::GetTownEffect, "GetTownEffect", 2, ".i");
- SQAICargo.DefSQStaticMethod(engine, &AICargo::GetCargoIncome, "GetCargoIncome", 4, ".iii");
+ SQAICargo.DefSQStaticMethod(engine, &AICargo::IsValidCargo, "IsValidCargo", 2, ".i");
+ SQAICargo.DefSQStaticMethod(engine, &AICargo::IsValidTownEffect, "IsValidTownEffect", 2, ".i");
+ SQAICargo.DefSQStaticMethod(engine, &AICargo::GetCargoLabel, "GetCargoLabel", 2, ".i");
+ SQAICargo.DefSQStaticMethod(engine, &AICargo::IsFreight, "IsFreight", 2, ".i");
+ SQAICargo.DefSQStaticMethod(engine, &AICargo::HasCargoClass, "HasCargoClass", 3, ".ii");
+ SQAICargo.DefSQStaticMethod(engine, &AICargo::GetTownEffect, "GetTownEffect", 2, ".i");
+ SQAICargo.DefSQStaticMethod(engine, &AICargo::GetCargoIncome, "GetCargoIncome", 4, ".iii");
SQAICargo.PostRegister(engine);
}
diff --git a/src/ai/api/ai_changelog.hpp b/src/ai/api/ai_changelog.hpp
index 30f72ac4f..86db27d97 100644
--- a/src/ai/api/ai_changelog.hpp
+++ b/src/ai/api/ai_changelog.hpp
@@ -23,6 +23,7 @@
*
* \li AICargo::CT_AUTO_REFIT
* \li AICargo::CT_NO_REFIT
+ * \li AICargo::IsValidTownEffect
* \li AICargoList_StationAccepting
* \li AICompany::GetQuarterlyIncome
* \li AICompany::GetQuarterlyExpenses
@@ -34,12 +35,25 @@
* \li AIOrder::GetOrderRefit
* \li AIOrder::IsRefitOrder
* \li AIOrder::SetOrderRefit
+ * \li AITown::GetLastMonthReceived
* \li AITown::GetTownAuthority
* \li AIVehicle::ERR_VEHICLE_TOO_LONG in case vehicle length limit is reached
*
+ * API renames:
+ * \li AITown::GetLastMonthTransported to AITown::GetLastMonthSupplied to better
+ * reflect what it does
+ *
* API removals:
* \li AICompany::GetCompanyValue, use AICompany::GetQuarterlyCompanyValue instead.
*
+ * Other changes:
+ * \li AITown::GetLastMonthProduction no longer has prerequisites based on town
+ * effects.
+ * \li AITown::GetLastMonthTransported no longer has prerequisites based on
+ * town effects.
+ * \li AITown::GetLastMonthTransportedPercentage no longer has prerequisites
+ * based on town effects.
+ *
* \b 1.1.2
*
* No changes
diff --git a/src/ai/api/ai_town.cpp b/src/ai/api/ai_town.cpp
index e78d7dfc7..c1efe5ec7 100644
--- a/src/ai/api/ai_town.cpp
+++ b/src/ai/api/ai_town.cpp
@@ -70,25 +70,17 @@
const Town *t = ::Town::Get(town_id);
- switch (AICargo::GetTownEffect(cargo_id)) {
- case AICargo::TE_PASSENGERS: return t->pass.old_max;
- case AICargo::TE_MAIL: return t->mail.old_max;
- default: return -1;
- }
+ return t->supplied[cargo_id].old_max;
}
-/* static */ int32 AITown::GetLastMonthTransported(TownID town_id, CargoID cargo_id)
+/* static */ int32 AITown::GetLastMonthSupplied(TownID town_id, CargoID cargo_id)
{
if (!IsValidTown(town_id)) return -1;
if (!AICargo::IsValidCargo(cargo_id)) return -1;
const Town *t = ::Town::Get(town_id);
- switch (AICargo::GetTownEffect(cargo_id)) {
- case AICargo::TE_PASSENGERS: return t->pass.old_act;
- case AICargo::TE_MAIL: return t->mail.old_act;
- default: return -1;
- }
+ return t->supplied[cargo_id].old_act;
}
/* static */ int32 AITown::GetLastMonthTransportedPercentage(TownID town_id, CargoID cargo_id)
@@ -97,12 +89,17 @@
if (!AICargo::IsValidCargo(cargo_id)) return -1;
const Town *t = ::Town::Get(town_id);
+ return ::ToPercent8(t->GetPercentTransported(cargo_id));
+}
- switch (AICargo::GetTownEffect(cargo_id)) {
- case AICargo::TE_PASSENGERS: return ::ToPercent8(t->GetPercentPassTransported());
- case AICargo::TE_MAIL: return ::ToPercent8(t->GetPercentMailTransported());
- default: return -1;
- }
+/* static */ int32 AITown::GetLastMonthReceived(TownID town_id, AICargo::TownEffect towneffect_id)
+{
+ if (!IsValidTown(town_id)) return -1;
+ if (!AICargo::IsValidTownEffect(towneffect_id)) return -1;
+
+ const Town *t = ::Town::Get(town_id);
+
+ return t->received[towneffect_id].old_act;
}
/* static */ int32 AITown::GetDistanceManhattanToTile(TownID town_id, TileIndex tile)
diff --git a/src/ai/api/ai_town.hpp b/src/ai/api/ai_town.hpp
index e73abb9cd..9ad4b24c1 100644
--- a/src/ai/api/ai_town.hpp
+++ b/src/ai/api/ai_town.hpp
@@ -12,6 +12,7 @@
#ifndef AI_TOWN_HPP
#define AI_TOWN_HPP
+#include "ai_cargo.hpp"
#include "ai_company.hpp"
/**
@@ -154,23 +155,21 @@ public:
* @param cargo_id The index of the cargo.
* @pre IsValidTown(town_id).
* @pre AICargo::IsValidCargo(cargo_id).
- * @pre AICargo::GetTownEffect(cargo_id) == TE_PASSENGERS || AICargo::GetTownEffect(cargo_id) == TE_MAIL.
* @return The last month's production of the given cargo for this town.
* @post Return value is always non-negative.
*/
static int32 GetLastMonthProduction(TownID town_id, CargoID cargo_id);
/**
- * Get the total amount of cargo transported from a town last month.
- * @param town_id The index of the industry.
+ * Get the total amount of cargo supplied from a town last month.
+ * @param town_id The index of the town.
* @param cargo_id The index of the cargo.
* @pre IsValidTown(town_id).
* @pre AICargo::IsValidCargo(cargo_id).
- * @pre AICargo::GetTownEffect(cargo_id) == TE_PASSENGERS || AICargo::GetTownEffect(cargo_id) == TE_MAIL.
- * @return The amount of given cargo transported from this town last month.
+ * @return The amount of cargo supplied for transport from this town last month.
* @post Return value is always non-negative.
*/
- static int32 GetLastMonthTransported(TownID town_id, CargoID cargo_id);
+ static int32 GetLastMonthSupplied(TownID town_id, CargoID cargo_id);
/**
* Get the percentage of transported production of the given cargo at a town.
@@ -178,13 +177,23 @@ public:
* @param cargo_id The index of the cargo.
* @pre IsValidTown(town_id).
* @pre AICargo::IsValidCargo(cargo_id).
- * @pre AICargo::GetTownEffect(cargo_id) == TE_PASSENGERS || AICargo::GetTownEffect(cargo_id) == TE_MAIL.
* @return The percentage of given cargo transported from this town last month.
* @post Return value is always non-negative.
*/
static int32 GetLastMonthTransportedPercentage(TownID town_id, CargoID cargo_id);
/**
+ * Get the total amount of cargo effects received by a town last month.
+ * @param town_id The index of the town.
+ * @param towneffect_id The index of the cargo.
+ * @pre IsValidTown(town_id).
+ * @pre AICargo::IsValidTownEffect(cargo_id).
+ * @return The amount of cargo received by this town last month for this cargo effect.
+ * @post Return value is always non-negative.
+ */
+ static int32 GetLastMonthReceived(TownID town_id, AICargo::TownEffect towneffect_id);
+
+ /**
* Get the manhattan distance from the tile to the AITown::GetLocation()
* of the town.
* @param town_id The town to get the distance to.
diff --git a/src/ai/api/ai_town.hpp.sq b/src/ai/api/ai_town.hpp.sq
index 315aba2a4..7bc1a3a91 100644
--- a/src/ai/api/ai_town.hpp.sq
+++ b/src/ai/api/ai_town.hpp.sq
@@ -67,8 +67,9 @@ void SQAITown_Register(Squirrel *engine)
SQAITown.DefSQStaticMethod(engine, &AITown::GetHouseCount, "GetHouseCount", 2, ".i");
SQAITown.DefSQStaticMethod(engine, &AITown::GetLocation, "GetLocation", 2, ".i");
SQAITown.DefSQStaticMethod(engine, &AITown::GetLastMonthProduction, "GetLastMonthProduction", 3, ".ii");
- SQAITown.DefSQStaticMethod(engine, &AITown::GetLastMonthTransported, "GetLastMonthTransported", 3, ".ii");
+ SQAITown.DefSQStaticMethod(engine, &AITown::GetLastMonthSupplied, "GetLastMonthSupplied", 3, ".ii");
SQAITown.DefSQStaticMethod(engine, &AITown::GetLastMonthTransportedPercentage, "GetLastMonthTransportedPercentage", 3, ".ii");
+ SQAITown.DefSQStaticMethod(engine, &AITown::GetLastMonthReceived, "GetLastMonthReceived", 3, ".ii");
SQAITown.DefSQStaticMethod(engine, &AITown::GetDistanceManhattanToTile, "GetDistanceManhattanToTile", 3, ".ii");
SQAITown.DefSQStaticMethod(engine, &AITown::GetDistanceSquareToTile, "GetDistanceSquareToTile", 3, ".ii");
SQAITown.DefSQStaticMethod(engine, &AITown::IsWithinTownInfluence, "IsWithinTownInfluence", 3, ".ii");