summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfonsinchen <fonsinchen@openttd.org>2014-09-21 16:20:14 +0000
committerfonsinchen <fonsinchen@openttd.org>2014-09-21 16:20:14 +0000
commit73d273ffd63d809234c6fa5744d13e520e3e480b (patch)
treeec3b95b87fe627a3120fb0479a67b3a661d9587d
parent9fdb88962fed69472a88861cf08d9559e8dff2a4 (diff)
downloadopenttd-73d273ffd63d809234c6fa5744d13e520e3e480b.tar.xz
(svn r26892) -Feature: Script API for retrieving planned flow
-rw-r--r--bin/ai/regression/regression.nut19
-rw-r--r--bin/ai/regression/regression.txt38
-rw-r--r--src/script/api/ai/ai_station.hpp.sq4
-rw-r--r--src/script/api/ai_changelog.hpp4
-rw-r--r--src/script/api/game/game_station.hpp.sq4
-rw-r--r--src/script/api/game_changelog.hpp4
-rw-r--r--src/script/api/script_station.cpp60
-rw-r--r--src/script/api/script_station.hpp60
8 files changed, 189 insertions, 4 deletions
diff --git a/bin/ai/regression/regression.nut b/bin/ai/regression/regression.nut
index 72a0c5a97..d856c3307 100644
--- a/bin/ai/regression/regression.nut
+++ b/bin/ai/regression/regression.nut
@@ -1269,6 +1269,25 @@ function Regression::Station()
}
}
+ print("");
+ print("--CargoPlanned--");
+ for (local cargo = 0; cargo <= 1000; cargo += 1000) {
+ for (local station0 = 0; station0 <= 1000; station0 += 1000) {
+ print(" GetCargoPlanned(" + station0 + ", " + cargo + "): " +
+ AIStation.GetCargoPlanned(station0, cargo));
+ for (local station1 = 0; station1 <= 1000; station1 += 1000) {
+ print(" GetCargoPlannedFrom(" + station0 + ", " + station1 + ", " + cargo + "): " +
+ AIStation.GetCargoPlannedFrom(station0, station1, cargo));
+ print(" GetCargoPlannedVia(" + station0 + ", " + station1 + ", " + cargo + "): " +
+ AIStation.GetCargoPlannedFrom(station0, station1, cargo));
+ for (local station2 = 0; station2 <= 1000; station2 += 1000) {
+ print(" GetCargoPlannedFromVia(" + station0 + ", " + station1 + ", " + station2 + ", " + cargo + "): " +
+ AIStation.GetCargoPlannedFromVia(station0, station1, station2, cargo));
+ }
+ }
+ }
+ }
+
local list = AIStationList(AIStation.STATION_BUS_STOP + AIStation.STATION_TRUCK_STOP);
print("");
diff --git a/bin/ai/regression/regression.txt b/bin/ai/regression/regression.txt
index e6f30d220..5150c5c28 100644
--- a/bin/ai/regression/regression.txt
+++ b/bin/ai/regression/regression.txt
@@ -7539,6 +7539,44 @@ ERROR: IsEnd() is invalid as Begin() is never called
GetCargoWaitingFromVia(1000, 1000, 0, 1000): -1
GetCargoWaitingFromVia(1000, 1000, 1000, 1000): -1
+--CargoPlanned--
+ GetCargoPlanned(0, 0): 0
+ GetCargoPlannedFrom(0, 0, 0): 0
+ GetCargoPlannedVia(0, 0, 0): 0
+ GetCargoPlannedFromVia(0, 0, 0, 0): 0
+ GetCargoPlannedFromVia(0, 0, 1000, 0): -1
+ GetCargoPlannedFrom(0, 1000, 0): -1
+ GetCargoPlannedVia(0, 1000, 0): -1
+ GetCargoPlannedFromVia(0, 1000, 0, 0): -1
+ GetCargoPlannedFromVia(0, 1000, 1000, 0): -1
+ GetCargoPlanned(1000, 0): -1
+ GetCargoPlannedFrom(1000, 0, 0): -1
+ GetCargoPlannedVia(1000, 0, 0): -1
+ GetCargoPlannedFromVia(1000, 0, 0, 0): -1
+ GetCargoPlannedFromVia(1000, 0, 1000, 0): -1
+ GetCargoPlannedFrom(1000, 1000, 0): -1
+ GetCargoPlannedVia(1000, 1000, 0): -1
+ GetCargoPlannedFromVia(1000, 1000, 0, 0): -1
+ GetCargoPlannedFromVia(1000, 1000, 1000, 0): -1
+ GetCargoPlanned(0, 1000): -1
+ GetCargoPlannedFrom(0, 0, 1000): -1
+ GetCargoPlannedVia(0, 0, 1000): -1
+ GetCargoPlannedFromVia(0, 0, 0, 1000): -1
+ GetCargoPlannedFromVia(0, 0, 1000, 1000): -1
+ GetCargoPlannedFrom(0, 1000, 1000): -1
+ GetCargoPlannedVia(0, 1000, 1000): -1
+ GetCargoPlannedFromVia(0, 1000, 0, 1000): -1
+ GetCargoPlannedFromVia(0, 1000, 1000, 1000): -1
+ GetCargoPlanned(1000, 1000): -1
+ GetCargoPlannedFrom(1000, 0, 1000): -1
+ GetCargoPlannedVia(1000, 0, 1000): -1
+ GetCargoPlannedFromVia(1000, 0, 0, 1000): -1
+ GetCargoPlannedFromVia(1000, 0, 1000, 1000): -1
+ GetCargoPlannedFrom(1000, 1000, 1000): -1
+ GetCargoPlannedVia(1000, 1000, 1000): -1
+ GetCargoPlannedFromVia(1000, 1000, 0, 1000): -1
+ GetCargoPlannedFromVia(1000, 1000, 1000, 1000): -1
+
--StationList--
Count(): 2
Location ListDump:
diff --git a/src/script/api/ai/ai_station.hpp.sq b/src/script/api/ai/ai_station.hpp.sq
index 6f2b6e741..af7012d35 100644
--- a/src/script/api/ai/ai_station.hpp.sq
+++ b/src/script/api/ai/ai_station.hpp.sq
@@ -50,6 +50,10 @@ void SQAIStation_Register(Squirrel *engine)
SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaitingFrom, "GetCargoWaitingFrom", 4, ".iii");
SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaitingVia, "GetCargoWaitingVia", 4, ".iii");
SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaitingFromVia, "GetCargoWaitingFromVia", 5, ".iiii");
+ SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoPlanned, "GetCargoPlanned", 3, ".ii");
+ SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoPlannedFrom, "GetCargoPlannedFrom", 4, ".iii");
+ SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoPlannedVia, "GetCargoPlannedVia", 4, ".iii");
+ SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoPlannedFromVia, "GetCargoPlannedFromVia", 5, ".iiii");
SQAIStation.DefSQStaticMethod(engine, &ScriptStation::HasCargoRating, "HasCargoRating", 3, ".ii");
SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoRating, "GetCargoRating", 3, ".ii");
SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCoverageRadius, "GetCoverageRadius", 2, ".i");
diff --git a/src/script/api/ai_changelog.hpp b/src/script/api/ai_changelog.hpp
index 869bf0efe..54d69776e 100644
--- a/src/script/api/ai_changelog.hpp
+++ b/src/script/api/ai_changelog.hpp
@@ -20,6 +20,10 @@
* 1.5.0 is not yet released. The following changes are not set in stone yet.
*
* API additions:
+ * \li AIStation::GetCargoPlanned
+ * \li AIStation::GetCargoPlannedFrom
+ * \li AIStation::GetCargoPlannedFromVia
+ * \li AIStation::GetCargoPlannedVia
* \li AIStation::GetCargoWaitingFromVia
*
* \b 1.4.2
diff --git a/src/script/api/game/game_station.hpp.sq b/src/script/api/game/game_station.hpp.sq
index 263ad6ba7..e3a7425ca 100644
--- a/src/script/api/game/game_station.hpp.sq
+++ b/src/script/api/game/game_station.hpp.sq
@@ -51,6 +51,10 @@ void SQGSStation_Register(Squirrel *engine)
SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaitingFrom, "GetCargoWaitingFrom", 4, ".iii");
SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaitingVia, "GetCargoWaitingVia", 4, ".iii");
SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaitingFromVia, "GetCargoWaitingFromVia", 5, ".iiii");
+ SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoPlanned, "GetCargoPlanned", 3, ".ii");
+ SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoPlannedFrom, "GetCargoPlannedFrom", 4, ".iii");
+ SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoPlannedVia, "GetCargoPlannedVia", 4, ".iii");
+ SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoPlannedFromVia, "GetCargoPlannedFromVia", 5, ".iiii");
SQGSStation.DefSQStaticMethod(engine, &ScriptStation::HasCargoRating, "HasCargoRating", 3, ".ii");
SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoRating, "GetCargoRating", 3, ".ii");
SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCoverageRadius, "GetCoverageRadius", 2, ".i");
diff --git a/src/script/api/game_changelog.hpp b/src/script/api/game_changelog.hpp
index eeae33296..8ce162e0a 100644
--- a/src/script/api/game_changelog.hpp
+++ b/src/script/api/game_changelog.hpp
@@ -20,6 +20,10 @@
* 1.5.0 is not yet released. The following changes are not set in stone yet.
*
* API additions:
+ * \li GSStation::GetCargoPlanned
+ * \li GSStation::GetCargoPlannedFrom
+ * \li GSStation::GetCargoPlannedFromVia
+ * \li GSStation::GetCargoPlannedVia
* \li GSStation::GetCargoWaitingFromVia
*
* \b 1.4.2
diff --git a/src/script/api/script_station.cpp b/src/script/api/script_station.cpp
index c03f851a3..197e97c74 100644
--- a/src/script/api/script_station.cpp
+++ b/src/script/api/script_station.cpp
@@ -40,13 +40,24 @@
}
template<bool Tfrom, bool Tvia>
+/* static */ bool ScriptStation::IsCargoRequestValid(StationID station_id,
+ StationID from_station_id, StationID via_station_id, CargoID cargo_id)
+{
+ if (!IsValidStation(station_id)) return false;
+ if (Tfrom && !IsValidStation(from_station_id) && from_station_id != STATION_INVALID) return false;
+ if (Tvia && !IsValidStation(via_station_id) && via_station_id != STATION_INVALID) return false;
+ if (!ScriptCargo::IsValidCargo(cargo_id)) return false;
+ return true;
+}
+
+template<bool Tfrom, bool Tvia>
/* static */ int32 ScriptStation::CountCargoWaiting(StationID station_id,
StationID from_station_id, StationID via_station_id, CargoID cargo_id)
{
- if (!IsValidStation(station_id)) return -1;
- if (Tfrom && !IsValidStation(from_station_id) && from_station_id != STATION_INVALID) return -1;
- if (Tvia && !IsValidStation(via_station_id) && via_station_id != STATION_INVALID) return -1;
- if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
+ if (!ScriptStation::IsCargoRequestValid<Tfrom, Tvia>(station_id, from_station_id,
+ via_station_id, cargo_id)) {
+ return -1;
+ }
const StationCargoList &cargo_list = ::Station::Get(station_id)->goods[cargo_id].cargo;
if (!Tfrom && !Tvia) return cargo_list.TotalCount();
@@ -87,6 +98,47 @@ template<bool Tfrom, bool Tvia>
return CountCargoWaiting<true, true>(station_id, from_station_id, via_station_id, cargo_id);
}
+template<bool Tfrom, bool Tvia>
+/* static */ int32 ScriptStation::CountCargoPlanned(StationID station_id,
+ StationID from_station_id, StationID via_station_id, CargoID cargo_id)
+{
+ if (!ScriptStation::IsCargoRequestValid<Tfrom, Tvia>(station_id, from_station_id,
+ via_station_id, cargo_id)) {
+ return -1;
+ }
+
+ const FlowStatMap &flows = ::Station::Get(station_id)->goods[cargo_id].flows;
+ if (Tfrom) {
+ return Tvia ? flows.GetFlowFromVia(from_station_id, via_station_id) :
+ flows.GetFlowFrom(from_station_id);
+ } else {
+ return Tvia ? flows.GetFlowVia(via_station_id) : flows.GetFlow();
+ }
+}
+
+/* static */ int32 ScriptStation::GetCargoPlanned(StationID station_id, CargoID cargo_id)
+{
+ return CountCargoPlanned<false, false>(station_id, STATION_INVALID, STATION_INVALID, cargo_id);
+}
+
+/* static */ int32 ScriptStation::GetCargoPlannedFrom(StationID station_id,
+ StationID from_station_id, CargoID cargo_id)
+{
+ return CountCargoPlanned<true, false>(station_id, from_station_id, STATION_INVALID, cargo_id);
+}
+
+/* static */ int32 ScriptStation::GetCargoPlannedVia(StationID station_id,
+ StationID via_station_id, CargoID cargo_id)
+{
+ return CountCargoPlanned<false, true>(station_id, STATION_INVALID, via_station_id, cargo_id);
+}
+
+/* static */ int32 ScriptStation::GetCargoPlannedFromVia(StationID station_id,
+ StationID from_station_id, StationID via_station_id, CargoID cargo_id)
+{
+ return CountCargoPlanned<true, true>(station_id, from_station_id, via_station_id, cargo_id);
+}
+
/* static */ bool ScriptStation::HasCargoRating(StationID station_id, CargoID cargo_id)
{
if (!IsValidStation(station_id)) return false;
diff --git a/src/script/api/script_station.hpp b/src/script/api/script_station.hpp
index 26fc28042..60ee1f02e 100644
--- a/src/script/api/script_station.hpp
+++ b/src/script/api/script_station.hpp
@@ -128,6 +128,58 @@ public:
static int32 GetCargoWaitingFromVia(StationID station_id, StationID from_station_id, StationID via_station_id, CargoID cargo_id);
/**
+ * See how much cargo was planned to pass (including production and consumption) this station per month.
+ * @param station_id The station to get the planned flow for.
+ * @param cargo_id The cargo type to get the planned flow for.
+ * @pre IsValidStation(station_id).
+ * @pre IsValidCargo(cargo_id).
+ * @return The amount of cargo units planned to pass the station per month.
+ */
+ static int32 GetCargoPlanned(StationID station_id, CargoID cargo_id);
+
+ /**
+ * See how much cargo from the specified origin was planned to pass (including production and consumption) this station per month.
+ * @param station_id The station to get the planned flow for.
+ * @param from_station_id The station the cargo originates at.
+ * @param cargo_id The cargo type to get the planned flow for.
+ * @pre IsValidStation(station_id).
+ * @pre IsValidStation(from_station_id) || from_station_id == STATION_INVALID.
+ * @pre IsValidCargo(cargo_id).
+ * @return The amount of cargo units from the specified origin planned to pass the station per month.
+ */
+ static int32 GetCargoPlannedFrom(StationID station_id, StationID from_station_id, CargoID cargo_id);
+
+ /**
+ * See how much cargo was planned to pass (including production and consumption) this station per month, heading for the specified next hop.
+ * @param station_id The station to get the planned flow for.
+ * @param via_station_id The next station the cargo will go on to.
+ * @param cargo_id The cargo type to get the planned flow for.
+ * @pre IsValidStation(station_id).
+ * @pre IsValidStation(via_station_id) || via_station_id == STATION_INVALID.
+ * @pre IsValidCargo(cargo_id).
+ * @return The amount of cargo units planned to pass the station per month, going via the specified next hop.
+ * @note Cargo planned to go "via" the same station that's being queried is actually planned to be consumed there.
+ */
+ static int32 GetCargoPlannedVia(StationID station_id, StationID via_station_id, CargoID cargo_id);
+
+ /**
+ * See how much cargo from the specified origin was planned to pass this station per month,
+ * heading for the specified next hop.
+ * @param station_id The station to get the planned flow for.
+ * @param from_station_id The station the cargo originates at.
+ * @param via_station_id The next station the cargo will go on to.
+ * @param cargo_id The cargo type to get the planned flow for.
+ * @pre IsValidStation(station_id).
+ * @pre IsValidStation(from_station_id) || from_station_id == STATION_INVALID.
+ * @pre IsValidStation(via_station_id) || via_station_id == STATION_INVALID.
+ * @pre IsValidCargo(cargo_id).
+ * @return The amount of cargo units from the specified origin planned to pass the station per month, going via the specified next hop.
+ * @note Cargo planned to go "via" the same station that's being queried is actually planned to be consumed there.
+ * @note Cargo planned to pass "from" the same station that's being queried is actually produced there.
+ */
+ static int32 GetCargoPlannedFromVia(StationID station_id, StationID from_station_id, StationID via_station_id, CargoID cargo_id);
+
+ /**
* Check whether the given cargo at the given station a rating.
* @param station_id The station to get the cargo-rating state of.
* @param cargo_id The cargo to get the cargo-rating state of.
@@ -245,9 +297,17 @@ public:
private:
template<bool Tfrom, bool Tvia>
+ static bool IsCargoRequestValid(StationID station_id, StationID from_station_id,
+ StationID via_station_id, CargoID cargo_id);
+
+ template<bool Tfrom, bool Tvia>
static int32 CountCargoWaiting(StationID station_id, StationID from_station_id,
StationID via_station_id, CargoID cargo_id);
+ template<bool Tfrom, bool Tvia>
+ static int32 CountCargoPlanned(StationID station_id, StationID from_station_id,
+ StationID via_station_id, CargoID cargo_id);
+
};
DECLARE_ENUM_AS_BIT_SET(ScriptStation::StationType)