OpenTTD Source 20250312-master-gcdcc6b491d
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 <http://www.gnu.org/licenses/>.
6 */
7
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
33
47
49enum class CargoClass : uint8_t {
50 Passengers = 0,
51 Mail = 1,
52 Express = 2,
53 Armoured = 3,
54 Bulk = 4,
55 PieceGoods = 5,
56 Liquid = 6,
57 Refrigerated = 7,
58 Hazardous = 8,
59 Covered = 9,
60 Oversized = 10,
61 Powderized = 11,
62 NotPourable = 12,
63 Potable = 13,
64 NonPotable = 14,
65 Special = 15,
66};
68
69static const uint8_t INVALID_CARGO_BITNUM = 0xFF;
70
71static const uint TOWN_PRODUCTION_DIVISOR = 256;
72
74struct CargoSpec {
77 uint8_t legend_colour;
78 uint8_t rating_colour;
79 uint8_t weight;
80 uint16_t multiplier = 0x100;
83 uint8_t transit_periods[2];
84
88 uint16_t town_production_multiplier = TOWN_PRODUCTION_DIVISOR;
90
96
98
99 const struct GRFFile *grffile;
100 const struct SpriteGroup *group;
101
102 Money current_payment;
103
108 inline CargoType Index() const
109 {
110 return this - CargoSpec::array;
111 }
112
118 inline bool IsValid() const
119 {
120 return this->bitnum != INVALID_CARGO_BITNUM;
121 }
122
127 static inline size_t GetArraySize()
128 {
130 }
131
137 static inline CargoSpec *Get(size_t index)
138 {
139 assert(index < lengthof(CargoSpec::array));
140 return &CargoSpec::array[index];
141 }
142
143 SpriteID GetCargoIcon() const;
144
145 inline uint64_t WeightOfNUnits(uint32_t n) const
146 {
147 return n * this->weight / 16u;
148 }
149
150 uint64_t WeightOfNUnitsInTrain(uint32_t n) const;
151
155 struct Iterator {
156 typedef CargoSpec value_type;
157 typedef CargoSpec *pointer;
158 typedef CargoSpec &reference;
159 typedef size_t difference_type;
160 typedef std::forward_iterator_tag iterator_category;
161
162 explicit Iterator(size_t index) : index(index)
163 {
164 this->ValidateIndex();
165 };
166
167 bool operator==(const Iterator &other) const { return this->index == other.index; }
168 CargoSpec * operator*() const { return CargoSpec::Get(this->index); }
169 Iterator & operator++() { this->index++; this->ValidateIndex(); return *this; }
170
171 private:
172 size_t index;
173 void ValidateIndex() { while (this->index < CargoSpec::GetArraySize() && !(CargoSpec::Get(this->index)->IsValid())) this->index++; }
174 };
175
176 /*
177 * Iterable ensemble of all valid CargoSpec
178 */
180 size_t from;
181 IterateWrapper(size_t from = 0) : from(from) {}
182 Iterator begin() { return Iterator(this->from); }
183 Iterator end() { return Iterator(CargoSpec::GetArraySize()); }
184 bool empty() { return this->begin() == this->end(); }
185 };
186
192 static IterateWrapper Iterate(size_t from = 0) { return IterateWrapper(from); }
193
195 static std::array<std::vector<const CargoSpec *>, NUM_TPE> town_production_cargoes;
196
197private:
199 static inline std::map<CargoLabel, CargoType> label_map{};
200
201 friend void SetupCargoForClimate(LandscapeType l);
202 friend void BuildCargoLabelMap();
203 friend inline CargoType GetCargoTypeByLabel(CargoLabel ct);
204 friend void FinaliseCargoArray();
205};
206
207extern CargoTypes _cargo_mask;
208extern CargoTypes _standard_cargo_mask;
209
211bool IsDefaultCargo(CargoType cargo_type);
212void BuildCargoLabelMap();
213
214std::optional<std::string> BuildCargoAcceptanceString(const CargoArray &acceptance, StringID label);
215
216inline CargoType GetCargoTypeByLabel(CargoLabel label)
217{
218 auto found = CargoSpec::label_map.find(label);
219 if (found != std::end(CargoSpec::label_map)) return found->second;
220 return INVALID_CARGO;
221}
222
224
226extern std::array<uint8_t, NUM_CARGO> _sorted_cargo_types;
227extern std::vector<const CargoSpec *> _sorted_cargo_specs;
228extern std::span<const CargoSpec *> _sorted_standard_cargo_specs;
229
237{
238 return CargoSpec::Get(c)->classes.Any(cc);
239}
240
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...
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:23
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:75
bool IsCargoInClass(CargoType c, CargoClasses cc)
Does cargo c have cargo class cc?
Definition cargotype.h:236
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.
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:38
static const uint8_t INVALID_CARGO_BITNUM
Constant representing invalid cargo.
Definition cargotype.h:69
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:64
CargoClass
Cargo classes.
Definition cargotype.h:49
@ NotPourable
Not Pourable (open wagon, but not hopper wagon)
@ Powderized
Powderized, moist protected (powder/silo wagon)
@ Hazardous
Hazardous cargo (Nuclear Fuel, Explosives, etc.)
@ NonPotable
Non-potable / non-food / dirty.
@ Bulk
Bulk cargo (Coal, Grain etc., Ores, Fruit)
@ Mail
Mail.
@ Liquid
Liquids (Oil, Water, Rubber)
@ Passengers
Passengers.
@ Potable
Potable / food / clean.
@ Oversized
Oversized (stake/flatbed wagon)
@ Armoured
Armoured cargo (Valuables, Gold, Diamonds)
@ Refrigerated
Refrigerated cargo (Food, Fruit)
@ Express
Express cargo (Goods, Food, Candy, but also possible for passengers)
@ Special
Special bit used for livery refit tricks instead of normal cargoes.
@ PieceGoods
Piece goods (Livestock, Wood, Steel, Paper)
@ Covered
Covered/Sheltered Freight (Transportation in Box Vans, Silo Wagons, etc.)
std::vector< const CargoSpec * > _sorted_cargo_specs
Cargo specifications sorted alphabetically by name.
TownProductionEffect
Town effect when producing cargo.
Definition cargotype.h:35
@ INVALID_TPE
Invalid town production effect.
Definition cargotype.h:45
@ TPE_NONE
Town will not produce this cargo type.
Definition cargotype.h:36
@ TPE_PASSENGERS
Cargo behaves passenger-like for production.
Definition cargotype.h:37
@ TPE_MAIL
Cargo behaves mail-like for production.
Definition cargotype.h:38
void InitializeSortedCargoSpecs()
Initialize the list of sorted cargo specifications.
TownAcceptanceEffect
Town growth effect when delivering cargo.
Definition cargotype.h:22
@ NUM_TAE
Amount of town effects.
Definition cargotype.h:31
@ TAE_END
End of town effects.
Definition cargotype.h:30
@ TAE_GOODS
Cargo behaves goods/candy-like.
Definition cargotype.h:27
@ TAE_NONE
Cargo has no effect.
Definition cargotype.h:24
@ TAE_PASSENGERS
Cargo behaves passenger-like.
Definition cargotype.h:25
@ TAE_MAIL
Cargo behaves mail-like.
Definition cargotype.h:26
@ TAE_FOOD
Cargo behaves food/fizzy-drinks-like.
Definition cargotype.h:29
@ TAE_WATER
Cargo behaves water-like.
Definition cargotype.h:28
CargoTypes _cargo_mask
Bitmask of cargo types available.
Definition cargotype.cpp:33
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
Types related to the economy.
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.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:277
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:113
Iterator to iterate all valid CargoSpec.
Definition cargotype.h:155
Specification of a cargo type.
Definition cargotype.h:74
int32_t initial_payment
Initial payment rate before inflation is applied.
Definition cargotype.h:82
CargoClasses classes
Classes of this cargo type.
Definition cargotype.h:81
uint16_t multiplier
Capacity multiplier for vehicles. (8 fractional bits)
Definition cargotype.h:80
StringID units_volume
Name of a single unit of cargo of this type.
Definition cargotype.h:93
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:137
StringID abbrev
Two letter abbreviation for this cargo type.
Definition cargotype.h:95
uint8_t bitnum
Cargo bit number, is INVALID_CARGO_BITNUM for a non-used spec.
Definition cargotype.h:76
static std::map< CargoLabel, CargoType > label_map
Translation map from CargoLabel to Cargo type.
Definition cargotype.h:199
StringID quantifier
Text for multiple units of cargo of this type.
Definition cargotype.h:94
SpriteID GetCargoIcon() const
Get sprite for showing cargo of this type.
CargoType Index() const
Determines index of this cargospec.
Definition cargotype.h:108
SpriteID sprite
Icon to display this cargo type, may be 0xFFF (which means to resolve an action123 chain).
Definition cargotype.h:97
bool is_freight
Cargo type is considered to be freight (affects train freight multiplier).
Definition cargotype.h:85
uint8_t weight
Weight of a single unit of this cargo type in 1/16 ton (62.5 kg).
Definition cargotype.h:79
CargoLabel label
Unique label of the cargo type.
Definition cargotype.h:75
const struct GRFFile * grffile
NewGRF where group belongs to.
Definition cargotype.h:99
static IterateWrapper Iterate(size_t from=0)
Returns an iterable ensemble of all valid CargoSpec.
Definition cargotype.h:192
static CargoSpec array[NUM_CARGO]
Array holding all CargoSpecs.
Definition cargotype.h:198
friend void BuildCargoLabelMap()
Build cargo label map.
StringID name
Name of this type of cargo.
Definition cargotype.h:91
friend void FinaliseCargoArray()
Check for invalid cargoes.
Definition newgrf.cpp:9560
friend void SetupCargoForClimate(LandscapeType l)
Set up the default cargo types for the given landscape type.
Definition cargotype.cpp:64
TownProductionEffect town_production_effect
The effect on town cargo production.
Definition cargotype.h:87
TownAcceptanceEffect town_acceptance_effect
The effect that delivering this cargo type has on towns. Also affects destination of subsidies.
Definition cargotype.h:86
CargoCallbackMasks callback_mask
Bitmask of cargo callbacks that have to be called.
Definition cargotype.h:89
uint16_t town_production_multiplier
Town production multiplier, if commanded by TownProductionEffect.
Definition cargotype.h:88
static std::array< std::vector< const CargoSpec * >, NUM_TPE > town_production_cargoes
List of cargo specs for each Town Product Effect.
Definition cargotype.h:27
bool IsValid() const
Tests for validity of this cargospec.
Definition cargotype.h:118
StringID name_single
Name of a single entity of this type of cargo.
Definition cargotype.h:92
static size_t GetArraySize()
Total number of cargospecs, both valid and invalid.
Definition cargotype.h:127
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:111
Iterable ensemble of each set bit in a value.