OpenTTD Source 20260621-master-g720d10536d
cargotype.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef CARGOTYPE_H
11#define CARGOTYPE_H
12
13#include "economy_type.h"
14#include "cargo_type.h"
15#include "gfx_type.h"
16#include "newgrf_callbacks.h"
17#include "strings_type.h"
18#include "landscape_type.h"
19#include "core/bitmath_func.hpp"
20
32
34
35
48
50enum class CargoClass : uint8_t {
52 Mail = 1,
53 Express = 2,
55 Bulk = 4,
57 Liquid = 6,
60 Covered = 9,
61 Oversized = 10,
64 Potable = 13,
66 Special = 15,
67};
68
71
72static const uint8_t INVALID_CARGO_BITNUM = 0xFF;
73
74static const uint TOWN_PRODUCTION_DIVISOR = 256;
75
77struct CargoSpec {
80 PixelColour legend_colour;
81 PixelColour rating_colour;
82 uint8_t weight;
83 uint16_t multiplier = 0x100;
86 uint8_t transit_periods[2];
87
91 uint16_t town_production_multiplier = TOWN_PRODUCTION_DIVISOR;
93
99
101
102 const struct GRFFile *grffile;
103 const struct SpriteGroup *group;
104
105 Money current_payment;
106
111 inline CargoType Index() const
112 {
113 return static_cast<CargoType>(this - CargoSpec::array);
114 }
115
121 inline bool IsValid() const
122 {
123 return this->bitnum != INVALID_CARGO_BITNUM;
124 }
125
130 static inline size_t GetArraySize()
131 {
133 }
134
141 static inline CargoSpec *Get(size_t index)
142 {
143 assert(index < lengthof(CargoSpec::array));
144 return &CargoSpec::array[index];
145 }
146
147 SpriteID GetCargoIcon() const;
148
149 inline uint64_t WeightOfNUnits(uint32_t n) const
150 {
151 return n * this->weight / 16u;
152 }
153
154 uint64_t WeightOfNUnitsInTrain(uint32_t n) const;
155
159 struct Iterator {
160 typedef CargoSpec value_type;
161 typedef CargoSpec *pointer;
162 typedef CargoSpec &reference;
163 typedef size_t difference_type;
164 typedef std::forward_iterator_tag iterator_category;
165
166 explicit Iterator(size_t index) : index(index)
167 {
168 this->ValidateIndex();
169 };
170
171 bool operator==(const Iterator &other) const { return this->index == other.index; }
172 CargoSpec * operator*() const { return CargoSpec::Get(this->index); }
173 Iterator & operator++() { this->index++; this->ValidateIndex(); return *this; }
174
175 private:
176 size_t index;
177 void ValidateIndex() { while (this->index < CargoSpec::GetArraySize() && !(CargoSpec::Get(this->index)->IsValid())) this->index++; }
178 };
179
181 struct IterateWrapper {
182 size_t from;
183 IterateWrapper(size_t from = 0) : from(from) {}
184 Iterator begin() { return Iterator(this->from); }
185 Iterator end() { return Iterator(CargoSpec::GetArraySize()); }
186 bool empty() { return this->begin() == this->end(); }
187 };
188
194 static IterateWrapper Iterate(size_t from = 0) { return IterateWrapper(from); }
195
198
199private:
201 static inline std::map<CargoLabel, CargoType> label_map{};
202
203 friend void SetupCargoForClimate(LandscapeType l);
204 friend void BuildCargoLabelMap();
205 friend inline CargoType GetCargoTypeByLabel(CargoLabel ct);
206 friend void FinaliseCargoArray();
207};
208
211
213bool IsDefaultCargo(CargoType cargo_type);
214void BuildCargoLabelMap();
215
216std::optional<std::string> BuildCargoAcceptanceString(const CargoArray &acceptance, StringID label);
217
218inline CargoType GetCargoTypeByLabel(CargoLabel label)
219{
220 auto found = CargoSpec::label_map.find(label);
221 if (found != std::end(CargoSpec::label_map)) return found->second;
222 return INVALID_CARGO;
223}
224
226
228extern std::array<uint8_t, NUM_CARGO> _sorted_cargo_types;
229extern std::vector<const CargoSpec *> _sorted_cargo_specs;
230extern std::span<const CargoSpec *> _sorted_standard_cargo_specs;
231
239{
240 return CargoSpec::Get(cargo)->classes.Any(cc);
241}
242
245 bool operator() (const CargoType &lhs, const CargoType &rhs) const { return _sorted_cargo_types[lhs] < _sorted_cargo_types[rhs]; }
246};
247
248#endif /* CARGOTYPE_H */
Functions related to bit mathematics.
Types related to cargoes...
EnumBitSet< CargoType, uint64_t > CargoTypes
Bitset of CargoType elements.
Definition cargo_type.h:113
static constexpr CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:75
StrongType::Typedef< uint32_t, struct CargoLabelTag, StrongType::Compare > CargoLabel
Globally unique label of a cargo type.
Definition cargo_type.h:17
CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
std::array< uint8_t, NUM_CARGO > _sorted_cargo_types
Sort order of cargoes by cargo type.
std::span< const CargoSpec * > _sorted_standard_cargo_specs
Standard cargo specifications sorted alphabetically by name.
CargoTypes _standard_cargo_mask
Bitmask of real cargo types available.
Definition cargotype.cpp:35
std::vector< const CargoSpec * > _sorted_cargo_specs
Cargo specifications sorted alphabetically by name.
CargoTypes _cargo_mask
Bitmask of cargo types available.
Definition cargotype.cpp:30
std::array< uint8_t, NUM_CARGO > _sorted_cargo_types
Sort order of cargoes by cargo type.
Dimension GetLargestCargoIconSize()
Get dimensions of largest cargo icon.
bool IsDefaultCargo(CargoType cargo_type)
Test if a cargo is a default cargo type.
static const uint8_t INVALID_CARGO_BITNUM
Constant representing invalid cargo.
Definition cargotype.h:72
void BuildCargoLabelMap()
Build cargo label map.
std::optional< std::string > BuildCargoAcceptanceString(const CargoArray &acceptance, StringID label)
Build comma-separated cargo acceptance string.
void SetupCargoForClimate(LandscapeType l)
Set up the default cargo types for the given landscape type.
Definition cargotype.cpp:61
CargoClass
Cargo classes.
Definition cargotype.h:50
@ NotPourable
Not Pourable (open wagon, but not hopper wagon).
Definition cargotype.h:63
@ Powderized
Powderized, moist protected (powder/silo wagon).
Definition cargotype.h:62
@ Hazardous
Hazardous cargo (Nuclear Fuel, Explosives, etc.).
Definition cargotype.h:59
@ NonPotable
Non-potable / non-food / dirty.
Definition cargotype.h:65
@ Bulk
Bulk cargo (Coal, Grain etc., Ores, Fruit).
Definition cargotype.h:55
@ Liquid
Liquids (Oil, Water, Rubber).
Definition cargotype.h:57
@ Potable
Potable / food / clean.
Definition cargotype.h:64
@ Oversized
Oversized (stake/flatbed wagon).
Definition cargotype.h:61
@ Armoured
Armoured cargo (Valuables, Gold, Diamonds).
Definition cargotype.h:54
@ Refrigerated
Refrigerated cargo (Food, Fruit).
Definition cargotype.h:58
@ Express
Express cargo (Goods, Food, Candy, but also possible for passengers).
Definition cargotype.h:53
@ Special
Special bit used for livery refit tricks instead of normal cargoes.
Definition cargotype.h:66
@ PieceGoods
Piece goods (Livestock, Wood, Steel, Paper).
Definition cargotype.h:56
@ Covered
Covered/Sheltered Freight (Transportation in Box Vans, Silo Wagons, etc.).
Definition cargotype.h:60
TownProductionEffect
Town effect when producing cargo.
Definition cargotype.h:36
@ Invalid
Invalid town production effect.
Definition cargotype.h:46
@ End
End marker.
Definition cargotype.h:40
EnumBitSet< CargoClass, uint16_t > CargoClasses
Bitset of CargoClass elements.
Definition cargotype.h:70
void InitializeSortedCargoSpecs()
Initialize the list of sorted cargo specifications.
TownAcceptanceEffect
Town growth effect when delivering cargo.
Definition cargotype.h:22
@ Food
Cargo behaves food/fizzy-drinks-like.
Definition cargotype.h:29
@ Begin
Used for iteration.
Definition cargotype.h:23
@ Water
Cargo behaves water-like.
Definition cargotype.h:28
@ Mail
Cargo behaves mail-like.
Definition cargotype.h:26
@ Passengers
Cargo behaves passenger-like.
Definition cargotype.h:25
@ None
Cargo has no effect.
Definition cargotype.h:24
@ End
End of town effects.
Definition cargotype.h:30
@ Goods
Cargo behaves goods/candy-like.
Definition cargotype.h:27
bool IsCargoInClass(CargoType cargo, CargoClasses cc)
Does cargo c have cargo class cc?
Definition cargotype.h:238
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
Enum-as-bit-set wrapper.
Types related to the economy.
#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
For some enums it is useful to have pre/post increment/decrement operators.
Definition enum_type.hpp:86
EnumClassIndexContainer< std::array< T, to_underlying(N)>, Index > EnumIndexArray
A typedef for EnumClassIndexContainer using std::array as the backing container type.
Types related to the graphics and/or input devices.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
Types related to the landscape.
LandscapeType
Landscape types.
Callbacks that NewGRFs could implement.
EnumBitSet< CargoCallbackMask, uint8_t > CargoCallbackMasks
Bitset of CargoCallbackMask elements.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
Types related to strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Class for storing amounts of cargo.
Definition cargo_type.h:118
Iterable ensemble of all valid CargoSpec.
Definition cargotype.h:181
Iterator to iterate all valid CargoSpec.
Definition cargotype.h:159
Specification of a cargo type.
Definition cargotype.h:77
int32_t initial_payment
Initial payment rate before inflation is applied.
Definition cargotype.h:85
static EnumIndexArray< std::vector< const CargoSpec * >, TownProductionEffect, TownProductionEffect::End > town_production_cargoes
List of cargo specs for each Town Product Effect.
Definition cargotype.h:197
CargoClasses classes
Classes of this cargo type.
Definition cargotype.h:84
uint16_t multiplier
Capacity multiplier for vehicles. (8 fractional bits).
Definition cargotype.h:83
StringID units_volume
Name of a single unit of cargo of this type.
Definition cargotype.h:96
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:141
StringID abbrev
Two letter abbreviation for this cargo type.
Definition cargotype.h:98
uint8_t bitnum
Cargo bit number, is INVALID_CARGO_BITNUM for a non-used spec.
Definition cargotype.h:79
static std::map< CargoLabel, CargoType > label_map
Translation map from CargoLabel to Cargo type.
Definition cargotype.h:201
StringID quantifier
Text for multiple units of cargo of this type.
Definition cargotype.h:97
SpriteID GetCargoIcon() const
Get sprite for showing cargo of this type.
CargoType Index() const
Determines index of this cargospec.
Definition cargotype.h:111
SpriteID sprite
Icon to display this cargo type, may be 0xFFF (which means to resolve an action123 chain).
Definition cargotype.h:100
bool is_freight
Cargo type is considered to be freight (affects train freight multiplier).
Definition cargotype.h:88
uint8_t weight
Weight of a single unit of this cargo type in 1/16 ton (62.5 kg).
Definition cargotype.h:82
CargoLabel label
Unique label of the cargo type.
Definition cargotype.h:78
const struct GRFFile * grffile
NewGRF where group belongs to.
Definition cargotype.h:102
static IterateWrapper Iterate(size_t from=0)
Returns an iterable ensemble of all valid CargoSpec.
Definition cargotype.h:194
static CargoSpec array[NUM_CARGO]
Array holding all CargoSpecs.
Definition cargotype.h:200
friend void BuildCargoLabelMap()
Build cargo label map.
StringID name
Name of this type of cargo.
Definition cargotype.h:94
friend void FinaliseCargoArray()
Check for invalid cargoes.
Definition newgrf.cpp:944
friend void SetupCargoForClimate(LandscapeType l)
Set up the default cargo types for the given landscape type.
Definition cargotype.cpp:61
TownProductionEffect town_production_effect
The effect on town cargo production.
Definition cargotype.h:90
TownAcceptanceEffect town_acceptance_effect
The effect that delivering this cargo type has on towns. Also affects destination of subsidies.
Definition cargotype.h:89
CargoCallbackMasks callback_mask
Bitmask of cargo callbacks that have to be called.
Definition cargotype.h:92
uint16_t town_production_multiplier
Town production multiplier, if commanded by TownProductionEffect.
Definition cargotype.h:91
bool IsValid() const
Tests for validity of this cargospec.
Definition cargotype.h:121
StringID name_single
Name of a single entity of this type of cargo.
Definition cargotype.h:95
static size_t GetArraySize()
Total number of cargospecs, both valid and invalid.
Definition cargotype.h:130
Comparator to sort CargoType by according to desired order.
Definition cargotype.h:244
Dimensions (a width and height) of a rectangle in 2D.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:128
Colour for pixel/line drawing.
Definition gfx_type.h:308
Common wrapper for all the different sprite group types.