OpenTTD Source 20241224-master-gf74b0cf984
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 <sstream>
#include "safeguards.h"

Go to the source code of this file.

Functions

void SetupCargoForClimate (LandscapeID 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 (CargoID cid)
 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 upto 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 ID.
 
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 269 of file cargotype.cpp.

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

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 134 of file cargotype.cpp.

Referenced by CargoChangeInfo().

◆ CargoSpecClassSorter()

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

Sort cargo specifications by their cargo class.

Definition at line 208 of file cargotype.cpp.

References CargoSpecNameSorter(), CC_MAIL, CC_PASSENGERS, CC_SPECIAL, and CargoSpec::classes.

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 196 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 115 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 124 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 165 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 ( CargoID  cid)

Test if a cargo is a default cargo type.

Parameters
cidCargo ID.
Returns
true iff the cargo type is a default cargo type.

Definition at line 152 of file cargotype.cpp.

References _default_cargo_labels, and CargoSpec::Get().

Referenced by CalculateRefitMasks().

◆ SetupCargoForClimate()

void SetupCargoForClimate ( LandscapeID  l)

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

Parameters
lLandscape

Definition at line 64 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 33 of file cargotype.cpp.

Referenced by CalculateRefitMasks(), CargoChangeInfo(), CompanyStationsWindow::OnDropdownSelect(), CompanyStationsWindow::SetStringParameters(), and ShowRefitOptionsList().

◆ _climate_dependent_cargo_labels

std::array<CargoLabel, 12> _climate_dependent_cargo_labels
static

Default cargo translation for upto 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 ID.

Definition at line 51 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 58 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 44 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 ID.

Definition at line 191 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 38 of file cargotype.cpp.

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