Changeset - r26524:5061ea344fd8
[Not reviewed]
master
0 6 0
Henry Wilson - 2 years 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
 
@@ -587,13 +587,13 @@ static int DrawRailWagonPurchaseInfo(int
 
	}
 
	y += FONT_HEIGHT_NORMAL;
 

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

	
 
	/* Wagon speed limit, displayed if above zero */
 
	if (_settings_game.vehicle.wagon_speed_limits) {
 
@@ -681,13 +681,13 @@ static int DrawRoadVehPurchaseInfo(int l
 
		}
 
		y += FONT_HEIGHT_NORMAL;
 

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

	
 
		/* Max speed - Engine power */
 
		SetDParam(0, e->GetDisplayMaxSpeed());
src/cargotype.cpp
Show inline comments
 
@@ -9,12 +9,13 @@
 

	
 
#include "stdafx.h"
 
#include "cargotype.h"
 
#include "newgrf_cargo.h"
 
#include "string_func.h"
 
#include "strings_func.h"
 
#include "settings_type.h"
 

	
 
#include "table/sprites.h"
 
#include "table/strings.h"
 
#include "table/cargo_const.h"
 

	
 
#include "safeguards.h"
 
@@ -206,6 +207,11 @@ void InitializeSortedCargoSpecs()
 
	}
 

	
 
	/* _sorted_standard_cargo_specs is a subset of _sorted_cargo_specs. */
 
	_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
 
@@ -120,12 +120,19 @@ struct CargoSpec {
 
		assert(index < lengthof(CargoSpec::array));
 
		return &CargoSpec::array[index];
 
	}
 

	
 
	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
 
	 */
 
	struct Iterator {
 
		typedef CargoSpec value_type;
 
		typedef CargoSpec *pointer;
src/roadveh.h
Show inline comments
 
@@ -175,13 +175,13 @@ protected: // These functions should not
 
	/**
 
	 * Allows to know the weight value that this vehicle will use.
 
	 * @return Weight value from the engine in tonnes.
 
	 */
 
	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()) {
 
			/* Road vehicle weight is in units of 1/4 t. */
 
			weight += GetVehicleProperty(this, PROP_ROADVEH_WEIGHT, RoadVehInfo(this->engine_type)->weight) / 4;
 
		}
src/script/api/script_cargo.cpp
Show inline comments
 
@@ -82,8 +82,8 @@
 
	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<int64>(amount) / 16;
 
	return ::CargoSpec::Get(cargo_type)->WeightOfNUnits(amount);
 
}
src/train.h
Show inline comments
 
@@ -210,13 +210,13 @@ protected: // These functions should not
 
	/**
 
	 * Allows to know the weight value that this vehicle will use.
 
	 * @return Weight value from the engine in tonnes.
 
	 */
 
	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()) {
 
			weight += GetVehicleProperty(this, PROP_TRAIN_WEIGHT, RailVehInfo(this->engine_type)->weight);
 
		}
 

	
0 comments (0 inline, 0 general)