OpenTTD Source 20261112-master-gbcbec5e63f
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
31
33
34
47
49enum class CargoClass : uint8_t {
51 Mail = 1,
52 Express = 2,
54 Bulk = 4,
56 Liquid = 6,
59 Covered = 9,
60 Oversized = 10,
63 Potable = 13,
65 Special = 15,
66};
67
70
71static const uint8_t INVALID_CARGO_BITNUM = 0xFF;
72
73static const uint TOWN_PRODUCTION_DIVISOR = 256;
74
76struct CargoSpec {
81 uint8_t weight;
82 uint16_t multiplier = 0x100;
85 uint8_t transit_periods[2];
86
92
98
100
101 const struct GRFFile *grffile;
102 const struct SpriteGroup *group;
103
105
110 inline CargoType Index() const
111 {
112 return static_cast<CargoType>(this - CargoSpec::array);
113 }
114
120 inline bool IsValid() const
121 {
122 return this->bitnum != INVALID_CARGO_BITNUM;
123 }
124
129 static inline size_t GetArraySize()
130 {
132 }
133
140 static inline CargoSpec *Get(size_t index)
141 {
142 assert(index < lengthof(CargoSpec::array));
143 return &CargoSpec::array[index];
144 }
145
146 SpriteID GetCargoIcon() const;
147
153 inline uint64_t WeightOfNUnits(uint32_t n) const
154 {
155 return n * this->weight / 16u;
156 }
157
158 uint64_t WeightOfNUnitsInTrain(uint32_t n) const;
159
163 struct Iterator {
165 using pointer = CargoSpec *;
167 using difference_type = size_t;
168 using iterator_category = std::forward_iterator_tag;
169
174 explicit Iterator(size_t index) : index(index)
175 {
176 this->ValidateIndex();
177 };
178
184 bool operator==(const Iterator &other) const { return this->index == other.index; }
185
190 CargoSpec * operator*() const { return CargoSpec::Get(this->index); }
191
196 Iterator & operator++() { this->index++; this->ValidateIndex(); return *this; }
197
198 private:
199 size_t index;
200
202 void ValidateIndex() { while (this->index < CargoSpec::GetArraySize() && !(CargoSpec::Get(this->index)->IsValid())) this->index++; }
203 };
204
207 size_t from;
208
213 IterateWrapper(size_t from = 0) : from(from) {}
214
219 Iterator begin() { return Iterator(this->from); }
220
226
231 bool empty() { return this->begin() == this->end(); }
232 };
233
239 static IterateWrapper Iterate(size_t from = 0) { return IterateWrapper(from); }
240
243
244private:
246 static inline std::map<CargoLabel, CargoType> label_map{};
247
248 friend void SetupCargoForClimate(LandscapeType l);
249 friend void BuildCargoLabelMap();
251 friend void FinaliseCargoArray();
252};
253
256
258bool IsDefaultCargo(CargoType cargo_type);
259void BuildCargoLabelMap();
260
261std::optional<std::string> BuildCargoAcceptanceString(const CargoArray &acceptance, StringID label);
262
269{
270 auto found = CargoSpec::label_map.find(label);
271 if (found != std::end(CargoSpec::label_map)) return found->second;
272 return INVALID_CARGO;
273}
274
276
278extern std::array<uint8_t, NUM_CARGO> _sorted_cargo_types;
279extern std::vector<const CargoSpec *> _sorted_cargo_specs;
280extern std::span<const CargoSpec *> _sorted_standard_cargo_specs;
281
289{
290 return CargoSpec::Get(cargo)->classes.Any(cc);
291}
292
301 bool operator() (const CargoType &lhs, const CargoType &rhs) const { return _sorted_cargo_types[lhs] < _sorted_cargo_types[rhs]; }
302};
303
304#endif /* CARGOTYPE_H */
Types related to cargoes...
Label< struct CargoLabelTag > CargoLabel
Globally unique label of a cargo type.
Definition cargo_type.h:17
static constexpr CargoType INVALID_CARGO
Invalid marker for cargoes.
Definition cargo_type.h:83
EnumBitSet< CargoType, uint64_t > CargoTypes
Bitset of CargoType elements.
Definition cargo_type.h:115
static constexpr CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:77
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
CargoType GetCargoTypeByLabel(CargoLabel label)
Get cargo assigned to a label.
Definition cargotype.h:268
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 bit number.
Definition cargotype.h:71
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:49
@ NotPourable
Not Pourable (open wagon, but not hopper wagon).
Definition cargotype.h:62
@ Powderized
Powderized, moist protected (powder/silo wagon).
Definition cargotype.h:61
@ Hazardous
Hazardous cargo (Nuclear Fuel, Explosives, etc.).
Definition cargotype.h:58
@ NonPotable
Non-potable / non-food / dirty.
Definition cargotype.h:64
@ Bulk
Bulk cargo (Coal, Grain etc., Ores, Fruit).
Definition cargotype.h:54
@ Liquid
Liquids (Oil, Water, Rubber).
Definition cargotype.h:56
@ Potable
Potable / food / clean.
Definition cargotype.h:63
@ Oversized
Oversized (stake/flatbed wagon).
Definition cargotype.h:60
@ Armoured
Armoured cargo (Valuables, Gold, Diamonds).
Definition cargotype.h:53
@ Refrigerated
Refrigerated cargo (Food, Fruit).
Definition cargotype.h:57
@ Express
Express cargo (Goods, Food, Candy, but also possible for passengers).
Definition cargotype.h:52
@ Special
Special bit used for livery refit tricks instead of normal cargoes.
Definition cargotype.h:65
@ PieceGoods
Piece goods (Livestock, Wood, Steel, Paper).
Definition cargotype.h:55
@ Covered
Covered/Sheltered Freight (Transportation in Box Vans, Silo Wagons, etc.).
Definition cargotype.h:59
static const uint TOWN_PRODUCTION_DIVISOR
By what factor divide town production.
Definition cargotype.h:73
TownProductionEffect
Town effect when producing cargo.
Definition cargotype.h:35
@ Invalid
Invalid town production effect.
Definition cargotype.h:45
@ End
End marker.
Definition cargotype.h:39
EnumBitSet< CargoClass, uint16_t > CargoClasses
Bitset of CargoClass elements.
Definition cargotype.h:69
void InitializeSortedCargoSpecs()
Initialize the list of sorted cargo specifications.
TownAcceptanceEffect
Town growth effect when delivering cargo.
Definition cargotype.h:21
@ Food
Cargo behaves food/fizzy-drinks-like.
Definition cargotype.h:28
@ Begin
Used for iteration.
Definition cargotype.h:22
@ Water
Cargo behaves water-like.
Definition cargotype.h:27
@ Mail
Cargo behaves mail-like.
Definition cargotype.h:25
@ Passengers
Cargo behaves passenger-like.
Definition cargotype.h:24
@ None
Cargo has no effect.
Definition cargotype.h:23
@ End
End of town effects.
Definition cargotype.h:29
@ Goods
Cargo behaves goods/candy-like.
Definition cargotype.h:26
bool IsCargoInClass(CargoType cargo, CargoClasses cc)
Does a given cargo have any cargo class from cc.
Definition cargotype.h:288
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.
@ Invalid
broken savegame (used internally)
Definition saveload.h:438
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:261
Types related to strings.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
Class for storing amounts of cargo.
Definition cargo_type.h:120
Iterable ensemble of all valid CargoSpec.
Definition cargotype.h:206
bool empty()
Check whether this range of valid CargoSpec is empty.
Definition cargotype.h:231
Iterator end()
Get the end iterator for all valid CargoSpec.
Definition cargotype.h:225
Iterator begin()
Get the begin iterator for this range of valid CargoSpec.
Definition cargotype.h:219
IterateWrapper(size_t from=0)
Constructs new iterator wrapper from an stating index.
Definition cargotype.h:213
size_t from
Index from which the iteration started.
Definition cargotype.h:207
Iterator to iterate all valid CargoSpec.
Definition cargotype.h:163
std::forward_iterator_tag iterator_category
The category of the iterator.
Definition cargotype.h:168
CargoSpec * operator*() const
Dereference operator.
Definition cargotype.h:190
Iterator(size_t index)
Constructs new iterator from an index.
Definition cargotype.h:174
void ValidateIndex()
Validate index so it points to a valid cargo spec.
Definition cargotype.h:202
Iterator & operator++()
Increment the iterator and set it to the next position.
Definition cargotype.h:196
size_t index
Index of the CargoSpec that this iterator points to.
Definition cargotype.h:199
bool operator==(const Iterator &other) const
Compare with other iterator.
Definition cargotype.h:184
CargoSpec * pointer
The pointer type to value_type.
Definition cargotype.h:165
CargoSpec value_type
The type pointed by iterator.
Definition cargotype.h:164
CargoSpec & reference
The reference type to value_type.
Definition cargotype.h:166
size_t difference_type
Type used to store difference between two iterators.
Definition cargotype.h:167
Specification of a cargo type.
Definition cargotype.h:76
int32_t initial_payment
Initial payment rate before inflation is applied.
Definition cargotype.h:84
friend CargoType GetCargoTypeByLabel(CargoLabel label)
Get cargo assigned to a label.
Definition cargotype.h:268
static EnumIndexArray< std::vector< const CargoSpec * >, TownProductionEffect, TownProductionEffect::End > town_production_cargoes
List of cargo specs for each Town Product Effect.
Definition cargotype.h:242
CargoClasses classes
Classes of this cargo type.
Definition cargotype.h:83
const struct SpriteGroup * group
Group of sprites used for drawing the cargo.
Definition cargotype.h:102
uint16_t multiplier
Capacity multiplier for vehicles. (8 fractional bits).
Definition cargotype.h:82
StringID units_volume
Name of a single unit of cargo of this type.
Definition cargotype.h:95
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:140
StringID abbrev
Two letter abbreviation for this cargo type.
Definition cargotype.h:97
uint8_t bitnum
Cargo bit number, is INVALID_CARGO_BITNUM for a non-used spec.
Definition cargotype.h:78
static std::map< CargoLabel, CargoType > label_map
Translation map from CargoLabel to Cargo type.
Definition cargotype.h:246
StringID quantifier
Text for multiple units of cargo of this type.
Definition cargotype.h:96
SpriteID GetCargoIcon() const
Get sprite for showing cargo of this type.
CargoType Index() const
Determines index of this cargospec.
Definition cargotype.h:110
SpriteID sprite
Icon to display this cargo type, may be 0xFFF (which means to resolve an action123 chain).
Definition cargotype.h:99
bool is_freight
Cargo type is considered to be freight (affects train freight multiplier).
Definition cargotype.h:87
PixelColour legend_colour
Colour in legends and graphs.
Definition cargotype.h:79
uint8_t weight
Weight of a single unit of this cargo type in 1/16 ton (62.5 kg).
Definition cargotype.h:81
CargoLabel label
Unique label of the cargo type.
Definition cargotype.h:77
const struct GRFFile * grffile
NewGRF where group belongs to.
Definition cargotype.h:101
static IterateWrapper Iterate(size_t from=0)
Returns an iterable ensemble of all valid CargoSpec.
Definition cargotype.h:239
Money current_payment
Payment rate scaled by inflation.
Definition cargotype.h:104
static CargoSpec array[NUM_CARGO]
Array holding all CargoSpecs.
Definition cargotype.h:245
uint64_t WeightOfNUnits(uint32_t n) const
Calculate the weight of n units of cargo.
Definition cargotype.h:153
PixelColour rating_colour
Colour used for box representing the amount of cargo waiting at the station in the station rating vie...
Definition cargotype.h:80
uint64_t WeightOfNUnitsInTrain(uint32_t n) const
Calculate the weight of n units of cargo inside a train wagon.
uint8_t transit_periods[2]
Values used to calculate timefactor for cargo payment alghoritm.
Definition cargotype.h:85
friend void BuildCargoLabelMap()
Build cargo label map.
StringID name
Name of this type of cargo.
Definition cargotype.h:93
friend void FinaliseCargoArray()
Check for invalid cargoes.
Definition newgrf.cpp:938
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:89
TownAcceptanceEffect town_acceptance_effect
The effect that delivering this cargo type has on towns. Also affects destination of subsidies.
Definition cargotype.h:88
CargoCallbackMasks callback_mask
Bitmask of cargo callbacks that have to be called.
Definition cargotype.h:91
uint16_t town_production_multiplier
Town production multiplier, if commanded by TownProductionEffect.
Definition cargotype.h:90
bool IsValid() const
Tests for validity of this cargospec.
Definition cargotype.h:120
StringID name_single
Name of a single entity of this type of cargo.
Definition cargotype.h:94
static size_t GetArraySize()
Total number of cargospecs, both valid and invalid.
Definition cargotype.h:129
Comparator to sort CargoType by according to desired order.
Definition cargotype.h:294
bool operator()(const CargoType &lhs, const CargoType &rhs) const
Call operator that performs the comparison.
Definition cargotype.h:301
Dimensions (a width and height) of a rectangle in 2D.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:126
Colour for pixel/line drawing.
Definition gfx_type.h:307
Common wrapper for all the different sprite group types.