OpenTTD Source  13.2.1
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 #include "core/span_type.hpp"
20 #include <vector>
21 
23 typedef uint32 CargoLabel;
24 
26 enum TownEffect : byte {
27  TE_BEGIN = 0,
28  TE_NONE = TE_BEGIN,
36 };
37 
39 enum CargoClass {
41  CC_PASSENGERS = 1 << 0,
42  CC_MAIL = 1 << 1,
43  CC_EXPRESS = 1 << 2,
44  CC_ARMOURED = 1 << 3,
45  CC_BULK = 1 << 4,
46  CC_PIECE_GOODS = 1 << 5,
47  CC_LIQUID = 1 << 6,
48  CC_REFRIGERATED = 1 << 7,
49  CC_HAZARDOUS = 1 << 8,
50  CC_COVERED = 1 << 9,
51  CC_SPECIAL = 1 << 15,
52 };
53 
54 static const byte INVALID_CARGO = 0xFF;
55 
57 struct CargoSpec {
58  uint8 bitnum;
60  uint8 legend_colour;
61  uint8 rating_colour;
62  uint8 weight;
63  uint16 multiplier;
65  uint8 transit_days[2];
66 
67  bool is_freight;
69  uint8 callback_mask;
70 
76 
78 
79  uint16 classes;
80  const struct GRFFile *grffile;
81  const struct SpriteGroup *group;
82 
83  Money current_payment;
84 
89  inline CargoID Index() const
90  {
91  return this - CargoSpec::array;
92  }
93 
99  inline bool IsValid() const
100  {
101  return this->bitnum != INVALID_CARGO;
102  }
103 
108  static inline size_t GetArraySize()
109  {
110  return lengthof(CargoSpec::array);
111  }
112 
118  static inline CargoSpec *Get(size_t index)
119  {
120  assert(index < lengthof(CargoSpec::array));
121  return &CargoSpec::array[index];
122  }
123 
124  SpriteID GetCargoIcon() const;
125 
126  inline uint64 WeightOfNUnits(uint32 n) const
127  {
128  return n * this->weight / 16u;
129  }
130 
131  uint64 WeightOfNUnitsInTrain(uint32 n) const;
132 
136  struct Iterator {
137  typedef CargoSpec value_type;
138  typedef CargoSpec *pointer;
139  typedef CargoSpec &reference;
140  typedef size_t difference_type;
141  typedef std::forward_iterator_tag iterator_category;
142 
143  explicit Iterator(size_t index) : index(index)
144  {
145  this->ValidateIndex();
146  };
147 
148  bool operator==(const Iterator &other) const { return this->index == other.index; }
149  bool operator!=(const Iterator &other) const { return !(*this == other); }
150  CargoSpec * operator*() const { return CargoSpec::Get(this->index); }
151  Iterator & operator++() { this->index++; this->ValidateIndex(); return *this; }
152 
153  private:
154  size_t index;
155  void ValidateIndex() { while (this->index < CargoSpec::GetArraySize() && !(CargoSpec::Get(this->index)->IsValid())) this->index++; }
156  };
157 
158  /*
159  * Iterable ensemble of all valid CargoSpec
160  */
161  struct IterateWrapper {
162  size_t from;
163  IterateWrapper(size_t from = 0) : from(from) {}
164  Iterator begin() { return Iterator(this->from); }
165  Iterator end() { return Iterator(CargoSpec::GetArraySize()); }
166  bool empty() { return this->begin() == this->end(); }
167  };
168 
174  static IterateWrapper Iterate(size_t from = 0) { return IterateWrapper(from); }
175 
176 private:
178 
179  friend void SetupCargoForClimate(LandscapeID l);
180 };
181 
182 extern CargoTypes _cargo_mask;
183 extern CargoTypes _standard_cargo_mask;
184 
187 CargoID GetCargoIDByBitnum(uint8 bitnum);
189 
191 extern std::vector<const CargoSpec *> _sorted_cargo_specs;
193 
200 static inline bool IsCargoInClass(CargoID c, CargoClass cc)
201 {
202  return (CargoSpec::Get(c)->classes & cc) != 0;
203 }
204 
206 
207 #endif /* CARGOTYPE_H */
INVALID_CARGO
static const byte INVALID_CARGO
Constant representing invalid cargo.
Definition: cargotype.h:54
CargoType
CargoType
Available types of cargo.
Definition: cargo_type.h:23
GetDefaultCargoID
CargoID GetDefaultCargoID(LandscapeID l, CargoType ct)
Get the cargo ID of a default cargo, if present.
Definition: cargotype.cpp:87
CC_HAZARDOUS
@ CC_HAZARDOUS
Hazardous cargo (Nuclear Fuel, Explosives, etc.)
Definition: cargotype.h:49
CargoSpec::callback_mask
uint8 callback_mask
Bitmask of cargo callbacks that have to be called.
Definition: cargotype.h:69
CC_COVERED
@ CC_COVERED
Covered/Sheltered Freight (Transportation in Box Vans, Silo Wagons, etc.)
Definition: cargotype.h:50
CargoSpec::label
CargoLabel label
Unique label of the cargo type.
Definition: cargotype.h:59
landscape_type.h
GetCargoIDByBitnum
CargoID GetCargoIDByBitnum(uint8 bitnum)
Find the CargoID of a 'bitnum' value.
Definition: cargotype.cpp:124
CargoSpec::IterateWrapper
Definition: cargotype.h:161
TE_GOODS
@ TE_GOODS
Cargo behaves goods/candy-like.
Definition: cargotype.h:31
CargoSpec::Iterator
Iterator to iterate all valid CargoSpec.
Definition: cargotype.h:136
CargoSpec::initial_payment
int32 initial_payment
Initial payment rate before inflation is applied.
Definition: cargotype.h:64
CargoSpec::town_effect
TownEffect town_effect
The effect that delivering this cargo type has on towns. Also affects destination of subsidies.
Definition: cargotype.h:68
_cargo_mask
CargoTypes _cargo_mask
Bitmask of cargo types available.
Definition: cargotype.cpp:29
CC_NOAVAILABLE
@ CC_NOAVAILABLE
No cargo class has been specified.
Definition: cargotype.h:40
CC_EXPRESS
@ CC_EXPRESS
Express cargo (Goods, Food, Candy, but also possible for passengers)
Definition: cargotype.h:43
Pool::PoolItem<&_spritegroup_pool >::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:118
InitializeSortedCargoSpecs
void InitializeSortedCargoSpecs()
Initialize the list of sorted cargo specifications.
Definition: cargotype.cpp:189
CargoSpec::Iterate
static IterateWrapper Iterate(size_t from=0)
Returns an iterable ensemble of all valid CargoSpec.
Definition: cargotype.h:174
CargoSpec
Specification of a cargo type.
Definition: cargotype.h:57
CC_LIQUID
@ CC_LIQUID
Liquids (Oil, Water, Rubber)
Definition: cargotype.h:47
CargoSpec::GetCargoIcon
SpriteID GetCargoIcon() const
Get sprite for showing cargo of this type.
Definition: cargotype.cpp:140
CC_PASSENGERS
@ CC_PASSENGERS
Passengers.
Definition: cargotype.h:41
CargoSpec::GetArraySize
static size_t GetArraySize()
Total number of cargospecs, both valid and invalid.
Definition: cargotype.h:108
strings_type.h
CargoLabel
uint32 CargoLabel
Globally unique label of a cargo type.
Definition: cargotype.h:23
CargoSpec::bitnum
uint8 bitnum
Cargo bit number, is INVALID_CARGO for a non-used spec.
Definition: cargotype.h:58
TownEffect
TownEffect
Town growth effect when delivering cargo.
Definition: cargotype.h:26
CargoSpec::array
static CargoSpec array[NUM_CARGO]
Array holding all CargoSpecs.
Definition: cargotype.h:177
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
bitmath_func.hpp
GetCargoIDByLabel
CargoID GetCargoIDByLabel(CargoLabel cl)
Get the cargo ID by cargo label.
Definition: cargotype.cpp:108
CargoSpec::Index
CargoID Index() const
Determines index of this cargospec.
Definition: cargotype.h:89
_sorted_standard_cargo_specs
span< const CargoSpec * > _sorted_standard_cargo_specs
Standard cargo specifications sorted alphabetically by name.
Definition: cargotype.cpp:154
TE_NONE
@ TE_NONE
Cargo has no effect.
Definition: cargotype.h:28
SetBitIterator
Iterable ensemble of each set bit in a value.
Definition: bitmath_func.hpp:329
CC_SPECIAL
@ CC_SPECIAL
Special bit used for livery refit tricks instead of normal cargoes.
Definition: cargotype.h:51
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
CargoSpec::units_volume
StringID units_volume
Name of a single unit of cargo of this type.
Definition: cargotype.h:73
CargoSpec::IsValid
bool IsValid() const
Tests for validity of this cargospec.
Definition: cargotype.h:99
CargoSpec::SetupCargoForClimate
friend void SetupCargoForClimate(LandscapeID l)
Set up the default cargo types for the given landscape type.
Definition: cargotype.cpp:40
CC_PIECE_GOODS
@ CC_PIECE_GOODS
Piece goods (Livestock, Wood, Steel, Paper)
Definition: cargotype.h:46
cargo_type.h
CC_BULK
@ CC_BULK
Bulk cargo (Coal, Grain etc., Ores, Fruit)
Definition: cargotype.h:45
CargoSpec::grffile
const struct GRFFile * grffile
NewGRF where #group belongs to.
Definition: cargotype.h:80
CargoSpec::sprite
SpriteID sprite
Icon to display this cargo type, may be 0xFFF (which means to resolve an action123 chain).
Definition: cargotype.h:77
span_type.hpp
CargoSpec::weight
uint8 weight
Weight of a single unit of this cargo type in 1/16 ton (62.5 kg).
Definition: cargotype.h:62
CargoSpec::is_freight
bool is_freight
Cargo type is considered to be freight (affects train freight multiplier).
Definition: cargotype.h:67
CargoClass
CargoClass
Cargo classes.
Definition: cargotype.h:39
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
CC_ARMOURED
@ CC_ARMOURED
Armoured cargo (Valuables, Gold, Diamonds)
Definition: cargotype.h:44
CC_REFRIGERATED
@ CC_REFRIGERATED
Refrigerated cargo (Food, Fruit)
Definition: cargotype.h:48
CargoSpec::quantifier
StringID quantifier
Text for multiple units of cargo of this type.
Definition: cargotype.h:74
CargoSpec::classes
uint16 classes
Classes of this cargo type.
Definition: cargotype.h:79
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:65
CargoSpec::name
StringID name
Name of this type of cargo.
Definition: cargotype.h:71
TE_MAIL
@ TE_MAIL
Cargo behaves mail-like.
Definition: cargotype.h:30
CargoSpec::name_single
StringID name_single
Name of a single entity of this type of cargo.
Definition: cargotype.h:72
CargoSpec::abbrev
StringID abbrev
Two letter abbreviation for this cargo type.
Definition: cargotype.h:75
_sorted_cargo_specs
std::vector< const CargoSpec * > _sorted_cargo_specs
Cargo specifications sorted alphabetically by name.
Definition: cargotype.cpp:153
CargoSpec::multiplier
uint16 multiplier
Capacity multiplier for vehicles. (8 fractional bits)
Definition: cargotype.h:63
TE_WATER
@ TE_WATER
Cargo behaves water-like.
Definition: cargotype.h:32
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:386
SetupCargoForClimate
void SetupCargoForClimate(LandscapeID l)
Set up the default cargo types for the given landscape type.
Definition: cargotype.cpp:40
NUM_TE
@ NUM_TE
Amount of town effects.
Definition: cargotype.h:35
OverflowSafeInt< int64 >
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
LandscapeID
byte LandscapeID
Landscape type.
Definition: landscape_type.h:13
TE_FOOD
@ TE_FOOD
Cargo behaves food/fizzy-drinks-like.
Definition: cargotype.h:33
IsCargoInClass
static bool IsCargoInClass(CargoID c, CargoClass cc)
Does cargo c have cargo class cc?
Definition: cargotype.h:200
economy_type.h
TE_END
@ TE_END
End of town effects.
Definition: cargotype.h:34
gfx_type.h
CC_MAIL
@ CC_MAIL
Mail.
Definition: cargotype.h:42
SpriteGroup
Definition: newgrf_spritegroup.h:57
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:106
TE_PASSENGERS
@ TE_PASSENGERS
Cargo behaves passenger-like.
Definition: cargotype.h:29
_standard_cargo_mask
CargoTypes _standard_cargo_mask
Bitmask of real cargo types available.
Definition: cargotype.cpp:34