OpenTTD Source: src/cargotype.h Source File

OpenTTD Source 15.0-RC2

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
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
22 enum TownAcceptanceEffect : uint8_t {
23 TAE_BEGIN = 0,
24 TAE_NONE = TAE_BEGIN,
25 TAE_PASSENGERS,
26 TAE_MAIL,
27 TAE_GOODS,
28 TAE_WATER,
29 TAE_FOOD,
30 TAE_END,
31 NUM_TAE = TAE_END,
32};
33
35 enum TownProductionEffect : uint8_t {
36 TPE_NONE,
37 TPE_PASSENGERS,
38 TPE_MAIL,
39 NUM_TPE,
40
45 INVALID_TPE,
46};
47
49 enum 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};
67using CargoClasses = EnumBitSet<CargoClass, uint16_t>;
68
69 static const uint8_t INVALID_CARGO_BITNUM = 0xFF;
70
71static const uint TOWN_PRODUCTION_DIVISOR = 256;
72
74 struct CargoSpec {
75 CargoLabel label;
76 uint8_t bitnum = INVALID_CARGO_BITNUM;
77 PixelColour legend_colour;
78 PixelColour rating_colour;
79 uint8_t weight;
80 uint16_t multiplier = 0x100;
81 CargoClasses classes;
82 int32_t initial_payment;
83 uint8_t transit_periods[2];
84
85 bool is_freight;
86 TownAcceptanceEffect town_acceptance_effect;
87 TownProductionEffect town_production_effect = INVALID_TPE;
88 uint16_t town_production_multiplier = TOWN_PRODUCTION_DIVISOR;
89 CargoCallbackMasks callback_mask;
90
91 StringID name;
92 StringID name_single;
93 StringID units_volume;
94 StringID quantifier;
95 StringID abbrev;
96
97 SpriteID sprite;
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 {
129 return lengthof(CargoSpec::array);
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 */
179 struct IterateWrapper {
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:
198 static CargoSpec array[NUM_CARGO];
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
210void SetupCargoForClimate(LandscapeType l);
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
223 Dimension GetLargestCargoIconSize();
224
225void InitializeSortedCargoSpecs();
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
236 inline bool IsCargoInClass(CargoType cargo, CargoClasses cc)
237{
238 return CargoSpec::Get(cargo)->classes.Any(cc);
239}
240
241using SetCargoBitIterator = SetBitIterator<CargoType, CargoTypes>;
242
244 struct CargoTypeComparator {
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 */
bitmath_func.hpp
Functions related to bit mathematics.
cargo_type.h
Types related to cargoes...
CargoType
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
NUM_CARGO
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:73
_sorted_cargo_types
std::array< uint8_t, NUM_CARGO > _sorted_cargo_types
Sort order of cargoes by cargo type.
Definition cargotype.cpp:189
GetLargestCargoIconSize
Dimension GetLargestCargoIconSize()
Get dimensions of largest cargo icon.
Definition cargotype.cpp:163
IsDefaultCargo
bool IsDefaultCargo(CargoType cargo_type)
Test if a cargo is a default cargo type.
Definition cargotype.cpp:150
_sorted_standard_cargo_specs
std::span< const CargoSpec * > _sorted_standard_cargo_specs
Standard cargo specifications sorted alphabetically by name.
Definition cargotype.cpp:191
_standard_cargo_mask
CargoTypes _standard_cargo_mask
Bitmask of real cargo types available.
Definition cargotype.cpp:36
INVALID_CARGO_BITNUM
static const uint8_t INVALID_CARGO_BITNUM
Constant representing invalid cargo.
Definition cargotype.h:69
BuildCargoLabelMap
void BuildCargoLabelMap()
Build cargo label map.
Definition cargotype.cpp:132
BuildCargoAcceptanceString
std::optional< std::string > BuildCargoAcceptanceString(const CargoArray &acceptance, StringID label)
Build comma-separated cargo acceptance string.
Definition cargotype.cpp:267
SetupCargoForClimate
void SetupCargoForClimate(LandscapeType l)
Set up the default cargo types for the given landscape type.
Definition cargotype.cpp:62
CargoClass
CargoClass
Cargo classes.
Definition cargotype.h:49
CargoClass::NotPourable
@ NotPourable
Not Pourable (open wagon, but not hopper wagon)
CargoClass::Powderized
@ Powderized
Powderized, moist protected (powder/silo wagon)
CargoClass::Hazardous
@ Hazardous
Hazardous cargo (Nuclear Fuel, Explosives, etc.)
CargoClass::NonPotable
@ NonPotable
Non-potable / non-food / dirty.
CargoClass::Bulk
@ Bulk
Bulk cargo (Coal, Grain etc., Ores, Fruit)
CargoClass::Mail
@ Mail
Mail.
CargoClass::Liquid
@ Liquid
Liquids (Oil, Water, Rubber)
CargoClass::Passengers
@ Passengers
Passengers.
CargoClass::Potable
@ Potable
Potable / food / clean.
CargoClass::Oversized
@ Oversized
Oversized (stake/flatbed wagon)
CargoClass::Armoured
@ Armoured
Armoured cargo (Valuables, Gold, Diamonds)
CargoClass::Refrigerated
@ Refrigerated
Refrigerated cargo (Food, Fruit)
CargoClass::Express
@ Express
Express cargo (Goods, Food, Candy, but also possible for passengers)
CargoClass::Special
@ Special
Special bit used for livery refit tricks instead of normal cargoes.
CargoClass::PieceGoods
@ PieceGoods
Piece goods (Livestock, Wood, Steel, Paper)
CargoClass::Covered
@ Covered
Covered/Sheltered Freight (Transportation in Box Vans, Silo Wagons, etc.)
_sorted_cargo_specs
std::vector< const CargoSpec * > _sorted_cargo_specs
Cargo specifications sorted alphabetically by name.
Definition cargotype.cpp:190
TownProductionEffect
TownProductionEffect
Town effect when producing cargo.
Definition cargotype.h:35
INVALID_TPE
@ INVALID_TPE
Invalid town production effect.
Definition cargotype.h:45
TPE_NONE
@ TPE_NONE
Town will not produce this cargo type.
Definition cargotype.h:36
TPE_PASSENGERS
@ TPE_PASSENGERS
Cargo behaves passenger-like for production.
Definition cargotype.h:37
TPE_MAIL
@ TPE_MAIL
Cargo behaves mail-like for production.
Definition cargotype.h:38
InitializeSortedCargoSpecs
void InitializeSortedCargoSpecs()
Initialize the list of sorted cargo specifications.
Definition cargotype.cpp:223
TownAcceptanceEffect
TownAcceptanceEffect
Town growth effect when delivering cargo.
Definition cargotype.h:22
NUM_TAE
@ NUM_TAE
Amount of town effects.
Definition cargotype.h:31
TAE_END
@ TAE_END
End of town effects.
Definition cargotype.h:30
TAE_GOODS
@ TAE_GOODS
Cargo behaves goods/candy-like.
Definition cargotype.h:27
TAE_NONE
@ TAE_NONE
Cargo has no effect.
Definition cargotype.h:24
TAE_PASSENGERS
@ TAE_PASSENGERS
Cargo behaves passenger-like.
Definition cargotype.h:25
TAE_MAIL
@ TAE_MAIL
Cargo behaves mail-like.
Definition cargotype.h:26
TAE_FOOD
@ TAE_FOOD
Cargo behaves food/fizzy-drinks-like.
Definition cargotype.h:29
TAE_WATER
@ TAE_WATER
Cargo behaves water-like.
Definition cargotype.h:28
IsCargoInClass
bool IsCargoInClass(CargoType cargo, CargoClasses cc)
Does cargo c have cargo class cc?
Definition cargotype.h:236
_cargo_mask
CargoTypes _cargo_mask
Bitmask of cargo types available.
Definition cargotype.cpp:31
BaseBitSet::Any
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
Definition base_bitset_type.hpp:170
EnumBitSet< CargoClass, uint16_t >
OverflowSafeInt< int64_t >
economy_type.h
Types related to the economy.
gfx_type.h
Types related to the graphics and/or input devices.
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
landscape_type.h
Types related to the landscape.
LandscapeType
LandscapeType
Landscape types.
Definition landscape_type.h:16
newgrf_callbacks.h
Callbacks that NewGRFs could implement.
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
strings_type.h
Types related to strings.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
CargoArray
Class for storing amounts of cargo.
Definition cargo_type.h:111
CargoSpec::IterateWrapper
Definition cargotype.h:179
CargoSpec::Iterator
Iterator to iterate all valid CargoSpec.
Definition cargotype.h:155
CargoSpec
Specification of a cargo type.
Definition cargotype.h:74
CargoSpec::initial_payment
int32_t initial_payment
Initial payment rate before inflation is applied.
Definition cargotype.h:82
CargoSpec::classes
CargoClasses classes
Classes of this cargo type.
Definition cargotype.h:81
CargoSpec::multiplier
uint16_t multiplier
Capacity multiplier for vehicles. (8 fractional bits)
Definition cargotype.h:80
CargoSpec::units_volume
StringID units_volume
Name of a single unit of cargo of this type.
Definition cargotype.h:93
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:137
CargoSpec::abbrev
StringID abbrev
Two letter abbreviation for this cargo type.
Definition cargotype.h:95
CargoSpec::bitnum
uint8_t bitnum
Cargo bit number, is INVALID_CARGO_BITNUM for a non-used spec.
Definition cargotype.h:76
CargoSpec::label_map
static std::map< CargoLabel, CargoType > label_map
Translation map from CargoLabel to Cargo type.
Definition cargotype.h:199
CargoSpec::quantifier
StringID quantifier
Text for multiple units of cargo of this type.
Definition cargotype.h:94
CargoSpec::GetCargoIcon
SpriteID GetCargoIcon() const
Get sprite for showing cargo of this type.
Definition cargotype.cpp:176
CargoSpec::Index
CargoType Index() const
Determines index of this cargospec.
Definition cargotype.h:108
CargoSpec::sprite
SpriteID sprite
Icon to display this cargo type, may be 0xFFF (which means to resolve an action123 chain).
Definition cargotype.h:97
CargoSpec::is_freight
bool is_freight
Cargo type is considered to be freight (affects train freight multiplier).
Definition cargotype.h:85
CargoSpec::weight
uint8_t weight
Weight of a single unit of this cargo type in 1/16 ton (62.5 kg).
Definition cargotype.h:79
CargoSpec::label
CargoLabel label
Unique label of the cargo type.
Definition cargotype.h:75
CargoSpec::grffile
const struct GRFFile * grffile
NewGRF where group belongs to.
Definition cargotype.h:99
CargoSpec::Iterate
static IterateWrapper Iterate(size_t from=0)
Returns an iterable ensemble of all valid CargoSpec.
Definition cargotype.h:192
CargoSpec::array
static CargoSpec array[NUM_CARGO]
Array holding all CargoSpecs.
Definition cargotype.h:198
CargoSpec::BuildCargoLabelMap
friend void BuildCargoLabelMap()
Build cargo label map.
Definition cargotype.cpp:132
CargoSpec::name
StringID name
Name of this type of cargo.
Definition cargotype.h:91
CargoSpec::FinaliseCargoArray
friend void FinaliseCargoArray()
Check for invalid cargoes.
Definition newgrf.cpp:937
CargoSpec::SetupCargoForClimate
friend void SetupCargoForClimate(LandscapeType l)
Set up the default cargo types for the given landscape type.
Definition cargotype.cpp:62
CargoSpec::town_production_effect
TownProductionEffect town_production_effect
The effect on town cargo production.
Definition cargotype.h:87
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:86
CargoSpec::callback_mask
CargoCallbackMasks callback_mask
Bitmask of cargo callbacks that have to be called.
Definition cargotype.h:89
CargoSpec::town_production_multiplier
uint16_t town_production_multiplier
Town production multiplier, if commanded by TownProductionEffect.
Definition cargotype.h:88
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:25
CargoSpec::IsValid
bool IsValid() const
Tests for validity of this cargospec.
Definition cargotype.h:118
CargoSpec::name_single
StringID name_single
Name of a single entity of this type of cargo.
Definition cargotype.h:92
CargoSpec::GetArraySize
static size_t GetArraySize()
Total number of cargospecs, both valid and invalid.
Definition cargotype.h:127
CargoTypeComparator
Comparator to sort CargoType by according to desired order.
Definition cargotype.h:244
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition geometry_type.hpp:56
GRFFile
Dynamic data of a loaded NewGRF.
Definition newgrf.h:114
PixelColour
Colour for pixel/line drawing.
Definition gfx_type.h:405
SetBitIterator
Iterable ensemble of each set bit in a value.
Definition bitmath_func.hpp:277
SpriteGroup
Definition newgrf_spritegroup.h:47
StrongType::Typedef< uint32_t, struct CargoLabelTag, StrongType::Compare >