# HG changeset patch # User Henry Wilson # Date 2022-11-02 17:31:10 # Node ID 5061ea344fd8f128af60125626edcec0040a26d3 # Parent f2007b1bb171d1700053a3bd8d8d098464f25006 Codechange: Factor cargotype weight conversion magic numbers diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -590,7 +590,7 @@ static int DrawRailWagonPurchaseInfo(int /* Wagon weight - (including cargo) */ uint weight = e->GetDisplayWeight(); SetDParam(0, weight); - uint cargo_weight = (e->CanCarryCargo() ? CargoSpec::Get(te.cargo)->weight * te.capacity / 16 : 0); + uint cargo_weight = (e->CanCarryCargo() ? CargoSpec::Get(te.cargo)->WeightOfNUnitsInTrain(te.capacity) : 0); SetDParam(1, cargo_weight + weight); DrawString(left, right, y, STR_PURCHASE_INFO_WEIGHT_CWEIGHT); y += FONT_HEIGHT_NORMAL; @@ -684,7 +684,7 @@ static int DrawRoadVehPurchaseInfo(int l /* Road vehicle weight - (including cargo) */ int16 weight = e->GetDisplayWeight(); SetDParam(0, weight); - uint cargo_weight = (e->CanCarryCargo() ? CargoSpec::Get(te.cargo)->weight * te.capacity / 16 : 0); + uint cargo_weight = (e->CanCarryCargo() ? CargoSpec::Get(te.cargo)->WeightOfNUnits(te.capacity) : 0); SetDParam(1, cargo_weight + weight); DrawString(left, right, y, STR_PURCHASE_INFO_WEIGHT_CWEIGHT); y += FONT_HEIGHT_NORMAL; diff --git a/src/cargotype.cpp b/src/cargotype.cpp --- a/src/cargotype.cpp +++ b/src/cargotype.cpp @@ -12,6 +12,7 @@ #include "newgrf_cargo.h" #include "string_func.h" #include "strings_func.h" +#include "settings_type.h" #include "table/sprites.h" #include "table/strings.h" @@ -209,3 +210,8 @@ void InitializeSortedCargoSpecs() _sorted_standard_cargo_specs = { _sorted_cargo_specs.data(), nb_standard_cargo }; } +uint64 CargoSpec::WeightOfNUnitsInTrain(uint32 n) const +{ + if (this->is_freight) n *= _settings_game.vehicle.freight_trains; + return this->WeightOfNUnits(n); +} diff --git a/src/cargotype.h b/src/cargotype.h --- a/src/cargotype.h +++ b/src/cargotype.h @@ -123,6 +123,13 @@ struct CargoSpec { SpriteID GetCargoIcon() const; + inline uint64 WeightOfNUnits(uint32 n) const + { + return n * this->weight / 16u; + } + + uint64 WeightOfNUnitsInTrain(uint32 n) const; + /** * Iterator to iterate all valid CargoSpec */ diff --git a/src/roadveh.h b/src/roadveh.h --- a/src/roadveh.h +++ b/src/roadveh.h @@ -178,7 +178,7 @@ protected: // These functions should not */ inline uint16 GetWeight() const { - uint16 weight = (CargoSpec::Get(this->cargo_type)->weight * this->cargo.StoredCount()) / 16; + uint16 weight = CargoSpec::Get(this->cargo_type)->WeightOfNUnits(this->cargo.StoredCount()); /* Vehicle weight is not added for articulated parts. */ if (!this->IsArticulatedPart()) { diff --git a/src/script/api/script_cargo.cpp b/src/script/api/script_cargo.cpp --- a/src/script/api/script_cargo.cpp +++ b/src/script/api/script_cargo.cpp @@ -85,5 +85,5 @@ /* 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; + return ::CargoSpec::Get(cargo_type)->WeightOfNUnits(amount); } diff --git a/src/train.h b/src/train.h --- a/src/train.h +++ b/src/train.h @@ -213,7 +213,7 @@ protected: // These functions should not */ inline uint16 GetWeight() const { - uint16 weight = (CargoSpec::Get(this->cargo_type)->weight * this->cargo.StoredCount() * FreightWagonMult(this->cargo_type)) / 16; + uint16 weight = CargoSpec::Get(this->cargo_type)->WeightOfNUnitsInTrain(this->cargo.StoredCount()); /* Vehicle weight is not added for articulated parts. */ if (!this->IsArticulatedPart()) {