Changeset - r26524:5061ea344fd8
[Not reviewed]
master
0 6 0
Henry Wilson - 22 months ago 2022-11-02 17:31:10
henry@henryandlizzy.uk
Codechange: Factor cargotype weight conversion magic numbers
6 files changed with 18 insertions and 5 deletions:
0 comments (0 inline, 0 general)
src/build_vehicle_gui.cpp
Show inline comments
 
@@ -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;
src/cargotype.cpp
Show inline comments
 
@@ -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);
 
}
src/cargotype.h
Show inline comments
 
@@ -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
 
	 */
src/roadveh.h
Show inline comments
 
@@ -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()) {
src/script/api/script_cargo.cpp
Show inline comments
 
@@ -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);
 
}
src/train.h
Show inline comments
 
@@ -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()) {
0 comments (0 inline, 0 general)