diff options
author | fonsinchen <fonsinchen@openttd.org> | 2014-05-04 13:57:48 +0000 |
---|---|---|
committer | fonsinchen <fonsinchen@openttd.org> | 2014-05-04 13:57:48 +0000 |
commit | bb269661d499352754d254c45bc3ef75a1fac370 (patch) | |
tree | 816020d8d1ed92923dd106b48c3b7f8b4df64e08 /src | |
parent | d2357ec435afc6f09ae7a98a42b389efa62e29f0 (diff) | |
download | openttd-bb269661d499352754d254c45bc3ef75a1fac370.tar.xz |
(svn r26557) -Fix: clean up, test (somewhat), and complete the API for waiting cargo
Diffstat (limited to 'src')
-rw-r--r-- | src/script/api/ai/ai_station.hpp.sq | 1 | ||||
-rw-r--r-- | src/script/api/ai_changelog.hpp | 3 | ||||
-rw-r--r-- | src/script/api/game/game_station.hpp.sq | 1 | ||||
-rw-r--r-- | src/script/api/game_changelog.hpp | 7 | ||||
-rw-r--r-- | src/script/api/script_station.cpp | 56 | ||||
-rw-r--r-- | src/script/api/script_station.hpp | 26 |
6 files changed, 65 insertions, 29 deletions
diff --git a/src/script/api/ai/ai_station.hpp.sq b/src/script/api/ai/ai_station.hpp.sq index 8cb80a4de..6f2b6e741 100644 --- a/src/script/api/ai/ai_station.hpp.sq +++ b/src/script/api/ai/ai_station.hpp.sq @@ -49,6 +49,7 @@ void SQAIStation_Register(Squirrel *engine) SQAIStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaiting, "GetCargoWaiting", 3, ".ii"); 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::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 f54495080..92893c269 100644 --- a/src/script/api/ai_changelog.hpp +++ b/src/script/api/ai_changelog.hpp @@ -19,6 +19,9 @@ * * 1.5.0 is not yet released. The following changes are not set in stone yet. * + * API additions: + * \li AIStation::GetCargoWaitingFromVia + * * \b 1.4.0 * * API additions: diff --git a/src/script/api/game/game_station.hpp.sq b/src/script/api/game/game_station.hpp.sq index ddaa4c575..263ad6ba7 100644 --- a/src/script/api/game/game_station.hpp.sq +++ b/src/script/api/game/game_station.hpp.sq @@ -50,6 +50,7 @@ void SQGSStation_Register(Squirrel *engine) SQGSStation.DefSQStaticMethod(engine, &ScriptStation::GetCargoWaiting, "GetCargoWaiting", 3, ".ii"); 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::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 632296cd9..a6a80f9b3 100644 --- a/src/script/api/game_changelog.hpp +++ b/src/script/api/game_changelog.hpp @@ -19,6 +19,9 @@ * * 1.5.0 is not yet released. The following changes are not set in stone yet. * + * API additions: + * \li GSStation::GetCargoWaitingFromVia + * * \b 1.4.0 * * API additions: @@ -32,8 +35,8 @@ * \li GSGoal::SetProgress * \li GSGoal::SetText * \li GSStation::HasCargoRating - * \li AIStation::GetCargoWaitingFrom - * \li AIStation::GetCargoWaitingVia + * \li GSStation::GetCargoWaitingFrom + * \li GSStation::GetCargoWaitingVia * \li GSStoryPage * \li GSStoryPageList * \li GSStoryPageElementList diff --git a/src/script/api/script_station.cpp b/src/script/api/script_station.cpp index db0865261..d304f7366 100644 --- a/src/script/api/script_station.cpp +++ b/src/script/api/script_station.cpp @@ -39,45 +39,51 @@ return ::GetStationIndex(tile); } -/* static */ int32 ScriptStation::GetCargoWaiting(StationID station_id, CargoID cargo_id) -{ - if (!IsValidStation(station_id)) return -1; - if (!ScriptCargo::IsValidCargo(cargo_id)) return -1; - - return ::Station::Get(station_id)->goods[cargo_id].cargo.TotalCount(); -} - -/* static */ int32 ScriptStation::GetCargoWaitingFrom(StationID station_id, StationID from_station_id, CargoID cargo_id) +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 (!IsValidStation(from_station_id) && from_station_id != STATION_INVALID) 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; const StationCargoList &cargo_list = ::Station::Get(station_id)->goods[cargo_id].cargo; + if (!Tfrom && !Tvia) return cargo_list.TotalCount(); + uint16 cargo_count = 0; - for (StationCargoList::ConstIterator it = cargo_list.Packets()->begin(); it != cargo_list.Packets()->end(); it++) { - CargoPacket *cp = *it; - if (cp->SourceStation() == from_station_id) cargo_count += cp->Count(); + std::pair<StationCargoList::ConstIterator, StationCargoList::ConstIterator> range = Tvia ? + cargo_list.Packets()->equal_range(via_station_id) : + std::make_pair(cargo_list.Packets()->begin(), cargo_list.Packets()->end()); + for (StationCargoList::ConstIterator it = range.first; it != range.second; it++) { + const CargoPacket *cp = *it; + if (!Tfrom || cp->SourceStation() == from_station_id) cargo_count += cp->Count(); } return cargo_count; } -/* static */ int32 ScriptStation::GetCargoWaitingVia(StationID station_id, StationID via_station_id, CargoID cargo_id) +/* static */ int32 ScriptStation::GetCargoWaiting(StationID station_id, CargoID cargo_id) { - if (!IsValidStation(station_id)) return -1; - if (!IsValidStation(via_station_id) && via_station_id != STATION_INVALID) return -1; - if (!ScriptCargo::IsValidCargo(cargo_id)) return -1; + return CountCargoWaiting<false, false>(station_id, STATION_INVALID, STATION_INVALID, cargo_id); +} - const StationCargoList &cargo_list = ::Station::Get(station_id)->goods[cargo_id].cargo; - uint16 cargo_count = 0; - std::pair<StationCargoList::ConstIterator, StationCargoList::ConstIterator> range = cargo_list.Packets()->equal_range(via_station_id); - for (StationCargoList::ConstIterator it = range.first; it != range.second; it++) { - CargoPacket *cp = *it; - cargo_count += cp->Count(); - } +/* static */ int32 ScriptStation::GetCargoWaitingFrom(StationID station_id, + StationID from_station_id, CargoID cargo_id) +{ + return CountCargoWaiting<true, false>(station_id, from_station_id, STATION_INVALID, cargo_id); +} - return cargo_count; +/* static */ int32 ScriptStation::GetCargoWaitingVia(StationID station_id, + StationID via_station_id, CargoID cargo_id) +{ + return CountCargoWaiting<false, true>(station_id, STATION_INVALID, via_station_id, cargo_id); +} + +/* static */ int32 ScriptStation::GetCargoWaitingFromVia(StationID station_id, + StationID from_station_id, StationID via_station_id, CargoID cargo_id) +{ + return CountCargoWaiting<true, true>(station_id, from_station_id, via_station_id, cargo_id); } /* static */ bool ScriptStation::HasCargoRating(StationID station_id, CargoID cargo_id) diff --git a/src/script/api/script_station.hpp b/src/script/api/script_station.hpp index 8108b8370..26fc28042 100644 --- a/src/script/api/script_station.hpp +++ b/src/script/api/script_station.hpp @@ -89,6 +89,7 @@ public: /** * See how much cargo with a specific source station there is waiting on a station. * @param station_id The station to get the cargo-waiting of. + * @param from_station_id The source station of the cargo. Pass STATION_INVALID to get cargo of which the source has been deleted. * @param cargo_id The cargo to get the cargo-waiting of. * @pre IsValidStation(station_id). * @pre IsValidStation(from_station_id) || from_station_id == STATION_INVALID. @@ -100,18 +101,33 @@ public: /** * See how much cargo with a specific via-station there is waiting on a station. - * @param station_id The station to get the cargo-waiting of, or pass STATION_INVALID to get waiting cargo for "via any station". + * @param station_id The station to get the cargo-waiting of. + * @param via_station_id The next station the cargo is going to. Pass STATION_INVALID to get waiting cargo for "via any station". * @param cargo_id The cargo to get the cargo-waiting of. * @pre IsValidStation(station_id). * @pre IsValidStation(via_station_id) || via_station_id == STATION_INVALID. * @pre IsValidCargo(cargo_id). * @return The amount of units waiting at the station with via_station_id as next hop. * @note if ScriptCargo.GetCargoDistributionType(cargo_id) == ScriptCargo.DT_MANUAL, then all waiting cargo will have STATION_INVALID as next hop. - */ static int32 GetCargoWaitingVia(StationID station_id, StationID via_station_id, CargoID cargo_id); /** + * See how much cargo with a specific via-station and source station there is waiting on a station. + * @param station_id The station to get the cargo-waiting of. + * @param from_station_id The source station of the cargo. Pass STATION_INVALID to get cargo of which the source has been deleted. + * @param via_station_id The next station the cargo is going to. Pass STATION_INVALID to get waiting cargo for "via any station". + * @param cargo_id The cargo to get the cargo-waiting of. + * @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 units waiting at the station with from_station_id as source and via_station_id as next hop. + * @note if ScriptCargo.GetCargoDistributionType(cargo_id) == ScriptCargo.DT_MANUAL, then all waiting cargo will have STATION_INVALID as next hop. + */ + static int32 GetCargoWaitingFromVia(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. @@ -226,6 +242,12 @@ public: * @return True if the state could be toggled. */ static bool OpenCloseAirport(StationID station_id); + +private: + template<bool Tfrom, bool Tvia> + static int32 CountCargoWaiting(StationID station_id, StationID from_station_id, + StationID via_station_id, CargoID cargo_id); + }; DECLARE_ENUM_AS_BIT_SET(ScriptStation::StationType) |