OpenTTD Source 20250428-master-g68ba2735ea
cargotype.cpp File Reference

Implementation of cargoes. More...

#include "stdafx.h"
#include "cargotype.h"
#include "core/geometry_func.hpp"
#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"

Go to the source code of this file.

Functions

void SetupCargoForClimate (LandscapeType l)
 Set up the default cargo types for the given landscape type.
 
std::span< const CargoLabelGetClimateDependentCargoTranslationTable ()
 Get default climate-dependent cargo translation table for a NewGRF, used if the NewGRF does not provide its own.
 
std::span< const CargoLabelGetClimateIndependentCargoTranslationTable ()
 Get default climate-independent cargo translation table for a NewGRF, used if the NewGRF does not provide its own.
 
void BuildCargoLabelMap ()
 Build cargo label map.
 
bool IsDefaultCargo (CargoType cargo_type)
 Test if a cargo is a default cargo type.
 
Dimension GetLargestCargoIconSize ()
 Get dimensions of largest cargo icon.
 
static bool CargoSpecNameSorter (const CargoSpec *const &a, const CargoSpec *const &b)
 Sort cargo specifications by their name.
 
static bool CargoSpecClassSorter (const CargoSpec *const &a, const CargoSpec *const &b)
 Sort cargo specifications by their cargo class.
 
void InitializeSortedCargoSpecs ()
 Initialize the list of sorted cargo specifications.
 
std::optional< std::string > BuildCargoAcceptanceString (const CargoArray &acceptance, StringID label)
 Build comma-separated cargo acceptance string.
 

Variables

CargoTypes _cargo_mask
 Bitmask of cargo types available.
 
CargoTypes _standard_cargo_mask
 Bitmask of real cargo types available.
 
static std::vector< CargoLabel_default_cargo_labels
 List of default cargo labels, used when setting up cargo types for default vehicles.
 
static std::array< CargoLabel, 12 > _climate_dependent_cargo_labels
 Default cargo translation for up to version 7 NewGRFs.
 
static std::array< CargoLabel, 32 > _climate_independent_cargo_labels
 Default cargo translation for version 8+ NewGRFs.
 
std::array< uint8_t, NUM_CARGO_sorted_cargo_types
 Sort order of cargoes by cargo type.
 
std::vector< const CargoSpec * > _sorted_cargo_specs
 Cargo specifications sorted alphabetically by name.
 
std::span< const CargoSpec * > _sorted_standard_cargo_specs
 Standard cargo specifications sorted alphabetically by name.
 

Detailed Description

Implementation of cargoes.

Definition in file cargotype.cpp.

Function Documentation

◆ BuildCargoAcceptanceString()

std::optional< std::string > BuildCargoAcceptanceString ( const CargoArray acceptance,
StringID  label 
)

Build comma-separated cargo acceptance string.

Parameters
acceptanceCargoArray filled with accepted cargo.
labelLabel to prefix cargo acceptance list.
Returns
String of accepted cargo, or nullopt if no cargo is accepted.

Definition at line 267 of file cargotype.cpp.

References _sorted_cargo_specs, GetListSeparator(), and GetString().

Referenced by BuildHouseWindow::GetHouseInformation(), and LandInfoWindow::OnInit().

◆ BuildCargoLabelMap()

void BuildCargoLabelMap ( )

Build cargo label map.

This is called multiple times during NewGRF initialization as cargos are defined, so that TranslateRefitMask() and GetCargoTranslation(), also used during initialization, get the correct information.

Definition at line 132 of file cargotype.cpp.

Referenced by CargoReserveInfo().

◆ CargoSpecClassSorter()

static bool CargoSpecClassSorter ( const CargoSpec *const &  a,
const CargoSpec *const &  b 
)
static

Sort cargo specifications by their cargo class.

Definition at line 206 of file cargotype.cpp.

References CargoSpecNameSorter(), CargoSpec::classes, Mail, Passengers, Special, and BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test().

Referenced by InitializeSortedCargoSpecs().

◆ CargoSpecNameSorter()

static bool CargoSpecNameSorter ( const CargoSpec *const &  a,
const CargoSpec *const &  b 
)
static

Sort cargo specifications by their name.

Definition at line 194 of file cargotype.cpp.

References GetString(), CargoSpec::name, and StrNaturalCompare().

Referenced by CargoSpecClassSorter().

◆ GetClimateDependentCargoTranslationTable()

std::span< const CargoLabel > GetClimateDependentCargoTranslationTable ( )

Get default climate-dependent cargo translation table for a NewGRF, used if the NewGRF does not provide its own.

Returns
Default translation table for GRF version.

Definition at line 113 of file cargotype.cpp.

References _climate_dependent_cargo_labels.

Referenced by GetCargoTranslation(), and GetCargoTranslationTable().

◆ GetClimateIndependentCargoTranslationTable()

std::span< const CargoLabel > GetClimateIndependentCargoTranslationTable ( )

Get default climate-independent cargo translation table for a NewGRF, used if the NewGRF does not provide its own.

Returns
Default translation table for GRF version.

Definition at line 122 of file cargotype.cpp.

References _climate_independent_cargo_labels.

Referenced by GetCargoTranslation(), and GetCargoTranslationTable().

◆ GetLargestCargoIconSize()

Dimension GetLargestCargoIconSize ( )

Get dimensions of largest cargo icon.

Returns
Dimensions of largest cargo icon.

Definition at line 163 of file cargotype.cpp.

References _sorted_cargo_specs, GetSpriteSize(), and maxdim().

Referenced by BaseVehicleListWindow::BuildCargoDropDownList(), DrawAircraftImage(), IndustryCargoesWindow::OnClick(), IndustryViewWindow::OnInit(), and SubsidyListWindow::OnInit().

◆ InitializeSortedCargoSpecs()

void InitializeSortedCargoSpecs ( )

◆ IsDefaultCargo()

bool IsDefaultCargo ( CargoType  cargo_type)

Test if a cargo is a default cargo type.

Parameters
cargo_typeCargo type.
Returns
true iff the cargo type is a default cargo type.

Definition at line 150 of file cargotype.cpp.

References _default_cargo_labels, and CargoSpec::Get().

Referenced by CalculateRefitMasks().

◆ SetupCargoForClimate()

void SetupCargoForClimate ( LandscapeType  l)

Set up the default cargo types for the given landscape type.

Parameters
lLandscape

Definition at line 62 of file cargotype.cpp.

Referenced by ResetNewGRFData().

Variable Documentation

◆ _cargo_mask

CargoTypes _cargo_mask

Bitmask of cargo types available.

This includes phony cargoes like regearing cargoes. Initialized during a call to SetupCargoForClimate.

Definition at line 31 of file cargotype.cpp.

Referenced by CalculateRefitMasks(), CargoReserveInfo(), CompanyStationsWindow::GetWidgetString(), CompanyStationsWindow::OnDropdownSelect(), and ShowRefitOptionsList().

◆ _climate_dependent_cargo_labels

std::array<CargoLabel, 12> _climate_dependent_cargo_labels
static

Default cargo translation for up to version 7 NewGRFs.

This maps the original 12 cargo slots to their original label. If a climate dependent cargo is not present it will map to CT_INVALID. For default cargoes this ends up as a 1:1 mapping via climate slot -> label -> cargo type.

Definition at line 49 of file cargotype.cpp.

Referenced by GetClimateDependentCargoTranslationTable().

◆ _climate_independent_cargo_labels

std::array<CargoLabel, 32> _climate_independent_cargo_labels
static

Default cargo translation for version 8+ NewGRFs.

This maps the 32 "bitnum" cargo slots to their original label. If a bitnum is not present it will map to CT_INVALID.

Definition at line 56 of file cargotype.cpp.

Referenced by GetClimateIndependentCargoTranslationTable().

◆ _default_cargo_labels

std::vector<CargoLabel> _default_cargo_labels
static

List of default cargo labels, used when setting up cargo types for default vehicles.

This is done by label so that a cargo label can be redefined in a different slot.

Definition at line 42 of file cargotype.cpp.

Referenced by IsDefaultCargo().

◆ _sorted_cargo_specs

◆ _sorted_cargo_types

std::array<uint8_t, NUM_CARGO> _sorted_cargo_types

Sort order of cargoes by cargo type.

Definition at line 189 of file cargotype.cpp.

Referenced by InitializeSortedCargoSpecs().

◆ _sorted_standard_cargo_specs

◆ _standard_cargo_mask

CargoTypes _standard_cargo_mask

Bitmask of real cargo types available.

Phony cargoes like regearing cargoes are excluded.

Definition at line 36 of file cargotype.cpp.

Referenced by CalculateRefitMasks(), CargoAndEngineFilter(), InitializeSortedCargoSpecs(), and BuildVehicleWindow::SetCargoFilterArray().