Codechange: Factor cargotype weight conversion magic numbers

This commit is contained in:
Henry Wilson 2022-11-02 17:31:10 +00:00 committed by Michael Lutz
parent 019dcb7b7b
commit 89cf0d5da8
6 changed files with 18 additions and 5 deletions

View File

@ -590,7 +590,7 @@ static int DrawRailWagonPurchaseInfo(int left, int right, int y, EngineID engine
/* 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 left, int right, int y, EngineID engine_n
/* 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;

View File

@ -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);
}

View File

@ -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
*/

View File

@ -178,7 +178,7 @@ protected: // These functions should not be called outside acceleration code.
*/
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()) {

View File

@ -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<int64>(amount) / 16;
return ::CargoSpec::Get(cargo_type)->WeightOfNUnits(amount);
}

View File

@ -213,7 +213,7 @@ protected: // These functions should not be called outside acceleration code.
*/
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()) {