OpenTTD Source: src/cargotype.h Source File

OpenTTD Source 14.0-beta3

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
21 enum TownAcceptanceEffect : byte {
22 TAE_BEGIN = 0,
23 TAE_NONE = TAE_BEGIN,
24 TAE_PASSENGERS,
25 TAE_MAIL,
26 TAE_GOODS,
27 TAE_WATER,
28 TAE_FOOD,
29 TAE_END,
30 NUM_TAE = TAE_END,
31 };
32
34 enum TownProductionEffect : byte {
35 TPE_NONE,
36 TPE_PASSENGERS,
37 TPE_MAIL,
38 NUM_TPE,
39
44 INVALID_TPE,
45 };
46
48 enum CargoClass {
49 CC_NOAVAILABLE = 0,
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_SPECIAL = 1 << 15,
61 };
62
63 static const byte INVALID_CARGO_BITNUM = 0xFF;
64
65 static const uint TOWN_PRODUCTION_DIVISOR = 256;
66
68 struct CargoSpec {
69 CargoLabel label;
70 uint8_t bitnum{INVALID_CARGO_BITNUM};
71 uint8_t legend_colour;
72 uint8_t rating_colour;
73 uint8_t weight;
74 uint16_t multiplier{0x100};
75 uint16_t classes;
76 int32_t initial_payment;
77 uint8_t transit_periods[2];
78
79 bool is_freight;
80 TownAcceptanceEffect town_acceptance_effect;
81 TownProductionEffect town_production_effect{INVALID_TPE};
82 uint16_t town_production_multiplier{TOWN_PRODUCTION_DIVISOR};
83 uint8_t callback_mask;
84
85 StringID name;
86 StringID name_single;
87 StringID units_volume;
88 StringID quantifier;
89 StringID abbrev;
90
91 SpriteID sprite;
92
93 const struct GRFFile *grffile;
94 const struct SpriteGroup *group;
95
96 Money current_payment;
97
102 inline CargoID Index() const
103 {
104 return this - CargoSpec::array;
105 }
106
112 inline bool IsValid() const
113 {
114 return this->bitnum != INVALID_CARGO_BITNUM;
115 }
116
121 static inline size_t GetArraySize()
122 {
123 return lengthof(CargoSpec::array);
124 }
125
131 static inline CargoSpec *Get(size_t index)
132 {
133 assert(index < lengthof(CargoSpec::array));
134 return &CargoSpec::array[index];
135 }
136
137 SpriteID GetCargoIcon() const;
138
139 inline uint64_t WeightOfNUnits(uint32_t n) const
140 {
141 return n * this->weight / 16u;
142 }
143
144 uint64_t WeightOfNUnitsInTrain(uint32_t n) const;
145
149 struct Iterator {
150 typedef CargoSpec value_type;
151 typedef CargoSpec *pointer;
152 typedef CargoSpec &reference;
153 typedef size_t difference_type;
154 typedef std::forward_iterator_tag iterator_category;
155
156 explicit Iterator(size_t index) : index(index)
157 {
158 this->ValidateIndex();
159 };
160
161 bool operator==(const Iterator &other) const { return this->index == other.index; }
162 bool operator!=(const Iterator &other) const { return !(*this == other); }
163 CargoSpec * operator*() const { return CargoSpec::Get(this->index); }
164 Iterator & operator++() { this->index++; this->ValidateIndex(); return *this; }
165
166 private:
167 size_t index;
168 void ValidateIndex() { while (this->index < CargoSpec::GetArraySize() && !(CargoSpec::Get(this->index)->IsValid())) this->index++; }
169 };
170
171 /*
172 * Iterable ensemble of all valid CargoSpec
173 */
174 struct IterateWrapper {
175 size_t from;
176 IterateWrapper(size_t from = 0) : from(from) {}
177 Iterator begin() { return Iterator(this->from); }
178 Iterator end() { return Iterator(CargoSpec::GetArraySize()); }
179 bool empty() { return this->begin() == this->end(); }
180 };
181
187 static IterateWrapper Iterate(size_t from = 0) { return IterateWrapper(from); }
188
190 static std::array<std::vector<const CargoSpec *>, NUM_TPE> town_production_cargoes;
191
192 private:
193 static CargoSpec array[NUM_CARGO];
194 static inline std::map<CargoLabel, CargoID> label_map{};
195
196 friend void SetupCargoForClimate(LandscapeID l);
197 friend void BuildCargoLabelMap();
198 friend inline CargoID GetCargoIDByLabel(CargoLabel ct);
199 friend void FinaliseCargoArray();
200 };
201
202 extern CargoTypes _cargo_mask;
203 extern CargoTypes _standard_cargo_mask;
204
205 void SetupCargoForClimate(LandscapeID l);
206 bool IsDefaultCargo(CargoID cid);
207 void BuildCargoLabelMap();
208 CargoID GetCargoIDByBitnum(uint8_t bitnum);
209
210 inline CargoID GetCargoIDByLabel(CargoLabel label)
211 {
212 auto found = CargoSpec::label_map.find(label);
213 if (found != std::end(CargoSpec::label_map)) return found->second;
214 return INVALID_CARGO;
215 }
216
217 Dimension GetLargestCargoIconSize();
218
219 void InitializeSortedCargoSpecs();
220 extern std::array<uint8_t, NUM_CARGO> _sorted_cargo_types;
221 extern std::vector<const CargoSpec *> _sorted_cargo_specs;
222 extern std::span<const CargoSpec *> _sorted_standard_cargo_specs;
223
230 inline bool IsCargoInClass(CargoID c, CargoClass cc)
231 {
232 return (CargoSpec::Get(c)->classes & cc) != 0;
233 }
234
235 using SetCargoBitIterator = SetBitIterator<CargoID, CargoTypes>;
236
238 struct CargoIDComparator {
239 bool operator() (const CargoID &lhs, const CargoID &rhs) const { return _sorted_cargo_types[lhs] < _sorted_cargo_types[rhs]; }
240 };
241
242 #endif /* CARGOTYPE_H */
CC_HAZARDOUS
@ CC_HAZARDOUS
Hazardous cargo (Nuclear Fuel, Explosives, etc.)
Definition: cargotype.h:58
CC_COVERED
@ CC_COVERED
Covered/Sheltered Freight (Transportation in Box Vans, Silo Wagons, etc.)
Definition: cargotype.h:59
TPE_NONE
@ TPE_NONE
Town will not produce this cargo type.
Definition: cargotype.h:35
TPE_MAIL
@ TPE_MAIL
Cargo behaves mail-like for production.
Definition: cargotype.h:37
CargoSpec::label
CargoLabel label
Unique label of the cargo type.
Definition: cargotype.h:69
landscape_type.h
TAE_FOOD
@ TAE_FOOD
Cargo behaves food/fizzy-drinks-like.
Definition: cargotype.h:28
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:30
CargoSpec::IterateWrapper
Definition: cargotype.h:174
CargoSpec::Iterator
Iterator to iterate all valid CargoSpec.
Definition: cargotype.h:149
_cargo_mask
CargoTypes _cargo_mask
Bitmask of cargo types available.
Definition: cargotype.cpp:31
CC_NOAVAILABLE
@ CC_NOAVAILABLE
No cargo class has been specified.
Definition: cargotype.h:49
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
CC_EXPRESS
@ CC_EXPRESS
Express cargo (Goods, Food, Candy, but also possible for passengers)
Definition: cargotype.h:52
Pool::PoolItem<&_spritegroup_pool >::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:234
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:131
InitializeSortedCargoSpecs
void InitializeSortedCargoSpecs()
Initialize the list of sorted cargo specifications.
Definition: cargotype.cpp:201
CargoSpec::label_map
static std::map< CargoLabel, CargoID > label_map
Translation map from CargoLabel to Cargo ID.
Definition: cargotype.h:194
CargoSpec::Iterate
static IterateWrapper Iterate(size_t from=0)
Returns an iterable ensemble of all valid CargoSpec.
Definition: cargotype.h:187
CargoSpec
Specification of a cargo type.
Definition: cargotype.h:68
CC_LIQUID
@ CC_LIQUID
Liquids (Oil, Water, Rubber)
Definition: cargotype.h:56
StrongType::Typedef< uint32_t, struct CargoLabelTag, StrongType::Compare >
CargoSpec::GetCargoIcon
SpriteID GetCargoIcon() const
Get sprite for showing cargo of this type.
Definition: cargotype.cpp:154
CC_PASSENGERS
@ CC_PASSENGERS
Passengers.
Definition: cargotype.h:50
CargoSpec::GetArraySize
static size_t GetArraySize()
Total number of cargospecs, both valid and invalid.
Definition: cargotype.h:121
strings_type.h
CargoSpec::initial_payment
int32_t initial_payment
Initial payment rate before inflation is applied.
Definition: cargotype.h:76
CargoSpec::array
static CargoSpec array[NUM_CARGO]
Array holding all CargoSpecs.
Definition: cargotype.h:193
bitmath_func.hpp
CargoSpec::Index
CargoID Index() const
Determines index of this cargospec.
Definition: cargotype.h:102
SetBitIterator
Iterable ensemble of each set bit in a value.
Definition: bitmath_func.hpp:282
CC_SPECIAL
@ CC_SPECIAL
Special bit used for livery refit tricks instead of normal cargoes.
Definition: cargotype.h:60
CargoSpec::weight
uint8_t weight
Weight of a single unit of this cargo type in 1/16 ton (62.5 kg).
Definition: cargotype.h:73
TownAcceptanceEffect
TownAcceptanceEffect
Town growth effect when delivering cargo.
Definition: cargotype.h:21
INVALID_CARGO_BITNUM
static const byte INVALID_CARGO_BITNUM
Constant representing invalid cargo.
Definition: cargotype.h:63
CargoSpec::units_volume
StringID units_volume
Name of a single unit of cargo of this type.
Definition: cargotype.h:87
CargoSpec::IsValid
bool IsValid() const
Tests for validity of this cargospec.
Definition: cargotype.h:112
CargoSpec::SetupCargoForClimate
friend void SetupCargoForClimate(LandscapeID l)
Set up the default cargo types for the given landscape type.
Definition: cargotype.cpp:48
TAE_GOODS
@ TAE_GOODS
Cargo behaves goods/candy-like.
Definition: cargotype.h:26
CC_PIECE_GOODS
@ CC_PIECE_GOODS
Piece goods (Livestock, Wood, Steel, Paper)
Definition: cargotype.h:55
NUM_TAE
@ NUM_TAE
Amount of town effects.
Definition: cargotype.h:30
CargoSpec::town_production_cargoes
static std::array< std::vector< const CargoSpec * >, NUM_TPE > town_production_cargoes
List of cargo specs for each Town Product Effect.
Definition: cargotype.h:190
cargo_type.h
CC_BULK
@ CC_BULK
Bulk cargo (Coal, Grain etc., Ores, Fruit)
Definition: cargotype.h:54
CargoSpec::grffile
const struct GRFFile * grffile
NewGRF where #group belongs to.
Definition: cargotype.h:93
GetLargestCargoIconSize
Dimension GetLargestCargoIconSize()
Get dimensions of largest cargo icon.
Definition: cargotype.cpp:124
CargoSpec::FinaliseCargoArray
friend void FinaliseCargoArray()
Check for invalid cargoes.
Definition: newgrf.cpp:9248
CargoSpec::sprite
SpriteID sprite
Icon to display this cargo type, may be 0xFFF (which means to resolve an action123 chain).
Definition: cargotype.h:91
CargoSpec::callback_mask
uint8_t callback_mask
Bitmask of cargo callbacks that have to be called.
Definition: cargotype.h:83
CargoSpec::BuildCargoLabelMap
friend void BuildCargoLabelMap()
Build cargo label map.
Definition: cargotype.cpp:93
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
CargoSpec::is_freight
bool is_freight
Cargo type is considered to be freight (affects train freight multiplier).
Definition: cargotype.h:79
CargoSpec::bitnum
uint8_t bitnum
Cargo bit number, is INVALID_CARGO_BITNUM for a non-used spec.
Definition: cargotype.h:70
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
CargoClass
CargoClass
Cargo classes.
Definition: cargotype.h:48
BuildCargoLabelMap
void BuildCargoLabelMap()
Build cargo label map.
Definition: cargotype.cpp:93
INVALID_TPE
@ INVALID_TPE
Invalid town production effect.
Definition: cargotype.h:44
CargoSpec::town_production_effect
TownProductionEffect town_production_effect
The effect on town cargo production.
Definition: cargotype.h:81
CC_ARMOURED
@ CC_ARMOURED
Armoured cargo (Valuables, Gold, Diamonds)
Definition: cargotype.h:53
CC_REFRIGERATED
@ CC_REFRIGERATED
Refrigerated cargo (Food, Fruit)
Definition: cargotype.h:57
CargoSpec::quantifier
StringID quantifier
Text for multiple units of cargo of this type.
Definition: cargotype.h:88
TAE_WATER
@ TAE_WATER
Cargo behaves water-like.
Definition: cargotype.h:27
_sorted_standard_cargo_specs
std::span< const CargoSpec * > _sorted_standard_cargo_specs
Standard cargo specifications sorted alphabetically by name.
Definition: cargotype.cpp:169
CargoSpec::name
StringID name
Name of this type of cargo.
Definition: cargotype.h:85
CargoSpec::name_single
StringID name_single
Name of a single entity of this type of cargo.
Definition: cargotype.h:86
CargoSpec::abbrev
StringID abbrev
Two letter abbreviation for this cargo type.
Definition: cargotype.h:89
_sorted_cargo_specs
std::vector< const CargoSpec * > _sorted_cargo_specs
Cargo specifications sorted alphabetically by name.
Definition: cargotype.cpp:168
GetCargoIDByBitnum
CargoID GetCargoIDByBitnum(uint8_t bitnum)
Find the CargoID of a 'bitnum' value.
Definition: cargotype.cpp:138
TownProductionEffect
TownProductionEffect
Town effect when producing cargo.
Definition: cargotype.h:34
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:300
IsDefaultCargo
bool IsDefaultCargo(CargoID cid)
Test if a cargo is a default cargo type.
Definition: cargotype.cpp:111
SetupCargoForClimate
void SetupCargoForClimate(LandscapeID l)
Set up the default cargo types for the given landscape type.
Definition: cargotype.cpp:48
CargoIDComparator
Comparator to sort CargoID by according to desired order.
Definition: cargotype.h:238
OverflowSafeInt< int64_t >
TAE_END
@ TAE_END
End of town effects.
Definition: cargotype.h:29
TAE_NONE
@ TAE_NONE
Cargo has no effect.
Definition: cargotype.h:23
LandscapeID
byte LandscapeID
Landscape type.
Definition: landscape_type.h:13
_sorted_cargo_types
std::array< uint8_t, NUM_CARGO > _sorted_cargo_types
Sort order of cargoes by cargo ID.
Definition: cargotype.cpp:167
TPE_PASSENGERS
@ TPE_PASSENGERS
Cargo behaves passenger-like for production.
Definition: cargotype.h:36
CargoSpec::town_production_multiplier
uint16_t town_production_multiplier
Town production multipler, if commanded by TownProductionEffect.
Definition: cargotype.h:82
NUM_CARGO
static const CargoID NUM_CARGO
Maximum number of cargo types in a game.
Definition: cargo_type.h:74
economy_type.h
TAE_MAIL
@ TAE_MAIL
Cargo behaves mail-like.
Definition: cargotype.h:25
gfx_type.h
IsCargoInClass
bool IsCargoInClass(CargoID c, CargoClass cc)
Does cargo c have cargo class cc?
Definition: cargotype.h:230
CC_MAIL
@ CC_MAIL
Mail.
Definition: cargotype.h:51
SpriteGroup
Definition: newgrf_spritegroup.h:57
CargoSpec::multiplier
uint16_t multiplier
Capacity multiplier for vehicles. (8 fractional bits)
Definition: cargotype.h:74
TAE_PASSENGERS
@ TAE_PASSENGERS
Cargo behaves passenger-like.
Definition: cargotype.h:24
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:107
_standard_cargo_mask
CargoTypes _standard_cargo_mask
Bitmask of real cargo types available.
Definition: cargotype.cpp:36
CargoSpec::classes
uint16_t classes
Classes of this cargo type.
Definition: cargotype.h:75
CargoSpec::town_acceptance_effect
TownAcceptanceEffect town_acceptance_effect
The effect that delivering this cargo type has on towns. Also affects destination of subsidies.
Definition: cargotype.h:80