OpenTTD Source  20241108-master-g80f628063a
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 (LandscapeID l)
 Set up the default cargo types for the given landscape type. More...
 
std::span< const CargoLabelGetDefaultCargoTranslationTable (uint8_t grf_version)
 Get default cargo translation table for a NewGRF, used if the NewGRF does not provide its own. More...
 
void BuildCargoLabelMap ()
 Build cargo label map. More...
 
bool IsDefaultCargo (CargoID cid)
 Test if a cargo is a default cargo type. More...
 
Dimension GetLargestCargoIconSize ()
 Get dimensions of largest cargo icon. More...
 
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.
 

Variables

CargoTypes _cargo_mask
 Bitmask of cargo types available. More...
 
CargoTypes _standard_cargo_mask
 Bitmask of real cargo types available. More...
 
static std::vector< CargoLabel_default_cargo_labels
 List of default cargo labels, used when setting up cargo types for default vehicles. More...
 
static std::array< CargoLabel, 12 > _v7_cargo_labels
 Default cargo translation for upto version 7 NewGRFs. More...
 
static std::array< CargoLabel, 32 > _v8_cargo_labels
 Default cargo translation for version 8+ NewGRFs. More...
 
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

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

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

◆ GetDefaultCargoTranslationTable()

std::span<const CargoLabel> GetDefaultCargoTranslationTable ( uint8_t  grf_version)

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

Parameters
grf_versionGRF version of translation table.
Returns
Default translation table for GRF version.

Definition at line 111 of file cargotype.cpp.

References _v7_cargo_labels, and _v8_cargo_labels.

Referenced by GetCargoTranslation().

◆ GetLargestCargoIconSize()

Dimension GetLargestCargoIconSize ( )

Get dimensions of largest cargo icon.

Returns
Dimensions of largest cargo icon.

Definition at line 153 of file cargotype.cpp.

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

Referenced by IndustryViewWindow::OnInit(), and SubsidyListWindow::OnInit().

◆ 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 140 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 62 of file cargotype.cpp.

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.

◆ _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().

◆ _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 CargoAndEngineFilter(), InitializeSortedCargoSpecs(), and BuildVehicleWindow::SetCargoFilterArray().

◆ _v7_cargo_labels

std::array<CargoLabel, 12> _v7_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 49 of file cargotype.cpp.

Referenced by GetDefaultCargoTranslationTable().

◆ _v8_cargo_labels

std::array<CargoLabel, 32> _v8_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 GetDefaultCargoTranslationTable().