From 8d0d45c4313bc05f3345021c94f8d42b3d955b95 Mon Sep 17 00:00:00 2001 From: SamuXarick <43006711+SamuXarick@users.noreply.github.com> Date: Wed, 22 Jun 2022 11:34:25 +0100 Subject: [PATCH] Add: ScriptCargo::GetWeight to get cargo weights --- src/script/api/ai_changelog.hpp | 2 ++ src/script/api/game_changelog.hpp | 2 ++ src/script/api/script_cargo.cpp | 6 ++++++ src/script/api/script_cargo.hpp | 10 ++++++++++ 4 files changed, 20 insertions(+) diff --git a/src/script/api/ai_changelog.hpp b/src/script/api/ai_changelog.hpp index 1e014266a3..6479393760 100644 --- a/src/script/api/ai_changelog.hpp +++ b/src/script/api/ai_changelog.hpp @@ -17,6 +17,8 @@ * * This version is not yet released. The following changes are not set in stone yet. * + * API additions: + * \li AICargo::GetWeight * \li AIIndustryType::ResolveNewGRFID * \li AIObjectType::ResolveNewGRFID * diff --git a/src/script/api/game_changelog.hpp b/src/script/api/game_changelog.hpp index 91dd0d22f5..592550ec1b 100644 --- a/src/script/api/game_changelog.hpp +++ b/src/script/api/game_changelog.hpp @@ -17,6 +17,8 @@ * * This version is not yet released. The following changes are not set in stone yet. * + * API additions: + * \li GSCargo::GetWeight * \li GSIndustryType::ResolveNewGRFID * \li GSObjectType::ResolveNewGRFID * diff --git a/src/script/api/script_cargo.cpp b/src/script/api/script_cargo.cpp index 5e092b52a0..e5a80023f7 100644 --- a/src/script/api/script_cargo.cpp +++ b/src/script/api/script_cargo.cpp @@ -81,3 +81,9 @@ if (!ScriptCargo::IsValidCargo(cargo_type)) return INVALID_DISTRIBUTION_TYPE; return (ScriptCargo::DistributionType)_settings_game.linkgraph.GetDistributionType(cargo_type); } + +/* static */ int64 ScriptCargo::GetWeight(CargoID cargo_type, uint32 amount) +{ + if (!IsValidCargo(cargo_type)) return -1; + return ::CargoSpec::Get(cargo_type)->weight * static_cast(amount) / 16; +} diff --git a/src/script/api/script_cargo.hpp b/src/script/api/script_cargo.hpp index 1bfd8c5531..67a54a3467 100644 --- a/src/script/api/script_cargo.hpp +++ b/src/script/api/script_cargo.hpp @@ -153,6 +153,16 @@ public: * @return The cargo distribution type for the given cargo. */ static DistributionType GetDistributionType(CargoID cargo_type); + + /** + * Get the weight in tonnes for the given amount of + * cargo for the specified type. + * @param cargo_type The cargo to check on. + * @param amount The quantity of cargo. + * @pre ScriptCargo::IsValidCargo(cargo_type). + * @return The weight in tonnes for that quantity of cargo. + */ + static int64 GetWeight(CargoID cargo_type, uint32 amount); }; #endif /* SCRIPT_CARGO_HPP */