OpenTTD Source 20241224-master-gf74b0cf984
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 "strings_type.h"
17#include "landscape_type.h"
18#include "core/bitmath_func.hpp"
19
32
46
48enum CargoClass : uint16_t {
50 CC_PASSENGERS = 1 << 0,
51 CC_MAIL = 1 << 1,
52 CC_EXPRESS = 1 << 2,
53 CC_ARMOURED = 1 << 3,
54 CC_BULK = 1 << 4,
55 CC_PIECE_GOODS = 1 << 5,
56 CC_LIQUID = 1 << 6,
57 CC_REFRIGERATED = 1 << 7,
58 CC_HAZARDOUS = 1 << 8,
59 CC_COVERED = 1 << 9,
60 CC_OVERSIZED = 1 << 10,
61 CC_POWDERIZED = 1 << 11,
62 CC_NOT_POURABLE = 1 << 12,
63 CC_POTABLE = 1 << 13,
64 CC_NON_POTABLE = 1 << 14,
65 CC_SPECIAL = 1 << 15,
66};
67
69using CargoClasses = uint16_t;
70
71static const uint8_t INVALID_CARGO_BITNUM = 0xFF;
72
73static const uint TOWN_PRODUCTION_DIVISOR = 256;
74
76struct CargoSpec {
79 uint8_t legend_colour;
80 uint8_t rating_colour;
81 uint8_t weight;
82 uint16_t multiplier = 0x100;
85 uint8_t transit_periods[2];
86
90 uint16_t town_production_multiplier = TOWN_PRODUCTION_DIVISOR;
91 uint8_t callback_mask;
92
98
100
101 const struct GRFFile *grffile;
102 const struct SpriteGroup *group;
103
104 Money current_payment;
105
110 inline CargoID Index() const
111 {
112 return 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
139 static inline CargoSpec *Get(size_t index)
140 {
141 assert(index < lengthof(CargoSpec::array));
142 return &CargoSpec::array[index];
143 }
144
145 SpriteID GetCargoIcon() const;
146
147 inline uint64_t WeightOfNUnits(uint32_t n) const
148 {
149 return n * this->weight / 16u;
150 }
151
152 uint64_t WeightOfNUnitsInTrain(uint32_t n) const;
153
157 struct Iterator {
158 typedef CargoSpec value_type;
159 typedef CargoSpec *pointer;
160 typedef CargoSpec &reference;
161 typedef size_t difference_type;
162 typedef std::forward_iterator_tag iterator_category;
163
164 explicit Iterator(size_t index) : index(index)
165 {
166 this->ValidateIndex();
167 };
168
169 bool operator==(const Iterator &other) const { return this->index == other.index; }
170 bool operator!=(const Iterator &other) const { return !(*this == other); }
171 CargoSpec * operator*() const { return CargoSpec::Get(this->index); }
172 Iterator & operator++() { this->index++; this->ValidateIndex(); return *this; }
173
174 private:
175 size_t index;
176 void ValidateIndex() { while (this->index < CargoSpec::GetArraySize() && !(CargoSpec::Get(this->index)->IsValid())) this->index++; }
177 };
178
179 /*
180 * Iterable ensemble of all valid CargoSpec
181 */
183 size_t from;
184 IterateWrapper(size_t from = 0) : from(from) {}
185 Iterator begin() { return Iterator(this->from); }
186 Iterator end() { return Iterator(CargoSpec::GetArraySize()); }
187 bool empty() { return this->begin() == this->end(); }
188 };
189
195 static IterateWrapper Iterate(size_t from = 0) { return IterateWrapper(from); }
196
198 static std::array<std::vector<const CargoSpec *>, NUM_TPE> town_production_cargoes;
199
200private:
202 static inline std::map<CargoLabel, CargoID> label_map{};
203
204 friend void SetupCargoForClimate(LandscapeID l);
205 friend void BuildCargoLabelMap();
206 friend inline CargoID GetCargoIDByLabel(CargoLabel ct);
207 friend void FinaliseCargoArray();
208};
209
210extern CargoTypes _cargo_mask;
211extern CargoTypes _standard_cargo_mask;
212
214bool IsDefaultCargo(CargoID cid);
215void BuildCargoLabelMap();
216
217std::optional<std::string> BuildCargoAcceptanceString(const CargoArray &acceptance, StringID label);
218
219inline CargoID GetCargoIDByLabel(CargoLabel label)
220{
221 auto found = CargoSpec::label_map.find(label);
222 if (found != std::end(CargoSpec::label_map)) return found->second;
223 return INVALID_CARGO;
224}
225
227
229extern std::array<uint8_t, NUM_CARGO> _sorted_cargo_types;
230extern std::vector<const CargoSpec *> _sorted_cargo_specs;
231extern std::span<const CargoSpec *> _sorted_standard_cargo_specs;
232
240{
241 return (CargoSpec::Get(c)->classes & cc) != 0;
242}
243
245
248 bool operator() (const CargoID &lhs, const CargoID &rhs) const { return _sorted_cargo_types[lhs] < _sorted_cargo_types[rhs]; }
249};
250
251#endif /* CARGOTYPE_H */
Functions related to bit mathematics.
Types related to cargoes...
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
static const CargoID NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:74
bool IsDefaultCargo(CargoID cid)
Test if a cargo is a default cargo type.
std::array< uint8_t, NUM_CARGO > _sorted_cargo_types
Sort order of cargoes by cargo ID.
Dimension GetLargestCargoIconSize()
Get dimensions of largest cargo icon.
bool IsCargoInClass(CargoID c, CargoClass cc)
Does cargo c have cargo class cc?
Definition cargotype.h:239
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:71
void BuildCargoLabelMap()
Build cargo label map.
CargoClass
Cargo classes.
Definition cargotype.h:48
@ CC_MAIL
Mail.
Definition cargotype.h:51
@ CC_REFRIGERATED
Refrigerated cargo (Food, Fruit)
Definition cargotype.h:57
@ CC_ARMOURED
Armoured cargo (Valuables, Gold, Diamonds)
Definition cargotype.h:53
@ CC_BULK
Bulk cargo (Coal, Grain etc., Ores, Fruit)
Definition cargotype.h:54
@ CC_NOAVAILABLE
No cargo class has been specified.
Definition cargotype.h:49
@ CC_EXPRESS
Express cargo (Goods, Food, Candy, but also possible for passengers)
Definition cargotype.h:52
@ CC_POWDERIZED
Powderized, moist protected (powder/silo wagon)
Definition cargotype.h:61
@ CC_COVERED
Covered/Sheltered Freight (Transportation in Box Vans, Silo Wagons, etc.)
Definition cargotype.h:59
@ CC_LIQUID
Liquids (Oil, Water, Rubber)
Definition cargotype.h:56
@ CC_NON_POTABLE
Non-potable / non-food / dirty.
Definition cargotype.h:64
@ CC_PIECE_GOODS
Piece goods (Livestock, Wood, Steel, Paper)
Definition cargotype.h:55
@ CC_PASSENGERS
Passengers.
Definition cargotype.h:50
@ CC_HAZARDOUS
Hazardous cargo (Nuclear Fuel, Explosives, etc.)
Definition cargotype.h:58
@ CC_OVERSIZED
Oversized (stake/flatbed wagon)
Definition cargotype.h:60
@ CC_POTABLE
Potable / food / clean.
Definition cargotype.h:63
@ CC_NOT_POURABLE
Not Pourable (open wagon, but not hopper wagon)
Definition cargotype.h:62
@ CC_SPECIAL
Special bit used for livery refit tricks instead of normal cargoes.
Definition cargotype.h:65
std::optional< std::string > BuildCargoAcceptanceString(const CargoArray &acceptance, StringID label)
Build comma-separated cargo acceptance string.
std::vector< const CargoSpec * > _sorted_cargo_specs
Cargo specifications sorted alphabetically by name.
TownProductionEffect
Town effect when producing cargo.
Definition cargotype.h:34
@ INVALID_TPE
Invalid town production effect.
Definition cargotype.h:44
@ TPE_NONE
Town will not produce this cargo type.
Definition cargotype.h:35
@ TPE_PASSENGERS
Cargo behaves passenger-like for production.
Definition cargotype.h:36
@ TPE_MAIL
Cargo behaves mail-like for production.
Definition cargotype.h:37
void InitializeSortedCargoSpecs()
Initialize the list of sorted cargo specifications.
void SetupCargoForClimate(LandscapeID l)
Set up the default cargo types for the given landscape type.
Definition cargotype.cpp:64
uint16_t CargoClasses
Bitmask of cargo classes.
Definition cargotype.h:69
TownAcceptanceEffect
Town growth effect when delivering cargo.
Definition cargotype.h:21
@ NUM_TAE
Amount of town effects.
Definition cargotype.h:30
@ TAE_END
End of town effects.
Definition cargotype.h:29
@ TAE_GOODS
Cargo behaves goods/candy-like.
Definition cargotype.h:26
@ TAE_NONE
Cargo has no effect.
Definition cargotype.h:23
@ TAE_PASSENGERS
Cargo behaves passenger-like.
Definition cargotype.h:24
@ TAE_MAIL
Cargo behaves mail-like.
Definition cargotype.h:25
@ TAE_FOOD
Cargo behaves food/fizzy-drinks-like.
Definition cargotype.h:28
@ TAE_WATER
Cargo behaves water-like.
Definition cargotype.h:27
CargoTypes _cargo_mask
Bitmask of cargo types available.
Definition cargotype.cpp:33
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:18
Types related to the landscape.
uint8_t LandscapeID
Landscape type.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:280
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:114
Comparator to sort CargoID by according to desired order.
Definition cargotype.h:247
Iterator to iterate all valid CargoSpec.
Definition cargotype.h:157
Specification of a cargo type.
Definition cargotype.h:76
int32_t initial_payment
Initial payment rate before inflation is applied.
Definition cargotype.h:84
CargoClasses classes
Classes of this cargo type.
Definition cargotype.h:83
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 ID.
Definition cargotype.h:139
CargoID Index() const
Determines index of this cargospec.
Definition cargotype.h:110
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
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.
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
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:195
static CargoSpec array[NUM_CARGO]
Array holding all CargoSpecs.
Definition cargotype.h:201
friend void BuildCargoLabelMap()
Build cargo label map.
uint8_t callback_mask
Bitmask of cargo callbacks that have to be called.
Definition cargotype.h:91
StringID name
Name of this type of cargo.
Definition cargotype.h:93
friend void FinaliseCargoArray()
Check for invalid cargoes.
Definition newgrf.cpp:9324
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
static std::map< CargoLabel, CargoID > label_map
Translation map from CargoLabel to Cargo ID.
Definition cargotype.h:202
friend void SetupCargoForClimate(LandscapeID l)
Set up the default cargo types for the given landscape type.
Definition cargotype.cpp:64
uint16_t town_production_multiplier
Town production multipler, if commanded by TownProductionEffect.
Definition cargotype.h:90
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: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
Dimensions (a width and height) of a rectangle in 2D.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:108
Iterable ensemble of each set bit in a value.