OpenTTD Source  14.0-beta2
cargo_type.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 CARGO_TYPE_H
11 #define CARGO_TYPE_H
12 
13 #include "core/enum_type.hpp"
15 
18 
22 using CargoID = byte;
23 
29 /* Temperate */
30 static constexpr CargoLabel CT_PASSENGERS = CargoLabel{'PASS'};
31 static constexpr CargoLabel CT_COAL = CargoLabel{'COAL'};
32 static constexpr CargoLabel CT_MAIL = CargoLabel{'MAIL'};
33 static constexpr CargoLabel CT_OIL = CargoLabel{'OIL_'};
34 static constexpr CargoLabel CT_LIVESTOCK = CargoLabel{'LVST'};
35 static constexpr CargoLabel CT_GOODS = CargoLabel{'GOOD'};
36 static constexpr CargoLabel CT_GRAIN = CargoLabel{'GRAI'};
37 static constexpr CargoLabel CT_WOOD = CargoLabel{'WOOD'};
38 static constexpr CargoLabel CT_IRON_ORE = CargoLabel{'IORE'};
39 static constexpr CargoLabel CT_STEEL = CargoLabel{'STEL'};
40 static constexpr CargoLabel CT_VALUABLES = CargoLabel{'VALU'};
41 
42 /* Arctic */
43 static constexpr CargoLabel CT_WHEAT = CargoLabel{'WHEA'};
44 static constexpr CargoLabel CT_PAPER = CargoLabel{'PAPR'};
45 static constexpr CargoLabel CT_GOLD = CargoLabel{'GOLD'};
46 static constexpr CargoLabel CT_FOOD = CargoLabel{'FOOD'};
47 
48 /* Tropic */
49 static constexpr CargoLabel CT_RUBBER = CargoLabel{'RUBR'};
50 static constexpr CargoLabel CT_FRUIT = CargoLabel{'FRUI'};
51 static constexpr CargoLabel CT_MAIZE = CargoLabel{'MAIZ'};
52 static constexpr CargoLabel CT_COPPER_ORE = CargoLabel{'CORE'};
53 static constexpr CargoLabel CT_WATER = CargoLabel{'WATR'};
54 static constexpr CargoLabel CT_DIAMONDS = CargoLabel{'DIAM'};
55 
56 /* Toyland */
57 static constexpr CargoLabel CT_SUGAR = CargoLabel{'SUGR'};
58 static constexpr CargoLabel CT_TOYS = CargoLabel{'TOYS'};
59 static constexpr CargoLabel CT_BATTERIES = CargoLabel{'BATT'};
60 static constexpr CargoLabel CT_CANDY = CargoLabel{'SWET'};
61 static constexpr CargoLabel CT_TOFFEE = CargoLabel{'TOFF'};
62 static constexpr CargoLabel CT_COLA = CargoLabel{'COLA'};
63 static constexpr CargoLabel CT_COTTON_CANDY = CargoLabel{'CTCD'};
64 static constexpr CargoLabel CT_BUBBLES = CargoLabel{'BUBL'};
65 static constexpr CargoLabel CT_PLASTIC = CargoLabel{'PLST'};
66 static constexpr CargoLabel CT_FIZZY_DRINKS = CargoLabel{'FZDR'};
67 
69 static constexpr CargoLabel CT_NONE = CT_PASSENGERS;
70 
71 static constexpr CargoLabel CT_INVALID = CargoLabel{UINT32_MAX};
72 
73 static const CargoID NUM_ORIGINAL_CARGO = 12;
74 static const CargoID NUM_CARGO = 64;
75 
76 /* CARGO_AUTO_REFIT and CARGO_NO_REFIT are stored in save-games for refit-orders, so should not be changed. */
77 static const CargoID CARGO_AUTO_REFIT = 0xFD;
78 static const CargoID CARGO_NO_REFIT = 0xFE;
79 
80 static const CargoID INVALID_CARGO = UINT8_MAX;
81 
87  static constexpr CargoID CF_ANY = NUM_CARGO;
88  static constexpr CargoID CF_NONE = NUM_CARGO + 1;
89  static constexpr CargoID CF_ENGINES = NUM_CARGO + 2;
90  static constexpr CargoID CF_FREIGHT = NUM_CARGO + 3;
91 };
92 
94 inline bool IsValidCargoType(CargoLabel t) { return t != CT_INVALID; }
96 inline bool IsValidCargoID(CargoID t) { return t != INVALID_CARGO; }
97 
98 typedef uint64_t CargoTypes;
99 
100 static const CargoTypes ALL_CARGOTYPES = (CargoTypes)UINT64_MAX;
101 
103 struct CargoArray : std::array<uint, NUM_CARGO> {
108  template <typename T>
109  inline const T GetSum() const
110  {
111  return std::reduce(this->begin(), this->end(), T{});
112  }
113 
118  inline uint GetCount() const
119  {
120  return std::count_if(this->begin(), this->end(), [](uint amount) { return amount != 0; });
121  }
122 };
123 
124 
126 enum class SourceType : byte {
127  Industry,
128  Town,
129  Headquarters,
130 };
131 
132 typedef uint16_t SourceID;
133 static const SourceID INVALID_SOURCE = 0xFFFF;
134 
135 #endif /* CARGO_TYPE_H */
CargoArray::GetSum
const T GetSum() const
Get the sum of all cargo amounts.
Definition: cargo_type.h:109
CARGO_NO_REFIT
static const CargoID CARGO_NO_REFIT
Do not refit cargo of a vehicle (used in vehicle orders and auto-replace/auto-renew).
Definition: cargo_type.h:78
CargoArray::GetCount
uint GetCount() const
Get the amount of cargos that have an amount.
Definition: cargo_type.h:118
CargoFilterCriteria::CF_ENGINES
static constexpr CargoID CF_ENGINES
Show only engines (for rail vehicles only)
Definition: cargo_type.h:89
IsValidCargoType
bool IsValidCargoType(CargoLabel t)
Test whether cargo type is not CT_INVALID.
Definition: cargo_type.h:94
CargoArray
Class for storing amounts of cargo.
Definition: cargo_type.h:103
StrongType::Typedef< uint32_t, struct CargoLabelTag, StrongType::Compare >
strong_typedef_type.hpp
CT_NONE
static constexpr CargoLabel CT_NONE
Dummy label for engines that carry no cargo; they actually carry 0 passengers.
Definition: cargo_type.h:69
CargoFilterCriteria::CF_NONE
static constexpr CargoID CF_NONE
Show only items which do not carry cargo (e.g. train engines)
Definition: cargo_type.h:88
SourceID
uint16_t SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:132
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
SourceType::Industry
@ Industry
Source/destination is an industry.
SourceType::Town
@ Town
Source/destination is a town.
CARGO_AUTO_REFIT
static const CargoID CARGO_AUTO_REFIT
Automatically choose cargo type when doing auto refitting.
Definition: cargo_type.h:77
CT_INVALID
static constexpr CargoLabel CT_INVALID
Invalid cargo type.
Definition: cargo_type.h:71
SourceType
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:126
CT_PASSENGERS
static constexpr CargoLabel CT_PASSENGERS
Available types of cargo Labels may be re-used between different climates.
Definition: cargo_type.h:30
INVALID_SOURCE
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
Definition: cargo_type.h:133
SourceType::Headquarters
@ Headquarters
Source/destination are company headquarters.
enum_type.hpp
IsValidCargoID
bool IsValidCargoID(CargoID t)
Test whether cargo type is not INVALID_CARGO.
Definition: cargo_type.h:96
NUM_ORIGINAL_CARGO
static const CargoID NUM_ORIGINAL_CARGO
Original number of cargo types.
Definition: cargo_type.h:73
NUM_CARGO
static const CargoID NUM_CARGO
Maximum number of cargo types in a game.
Definition: cargo_type.h:74
CargoFilterCriteria::CF_FREIGHT
static constexpr CargoID CF_FREIGHT
Show only vehicles which carry any freight (non-passenger) cargo.
Definition: cargo_type.h:90
CargoFilterCriteria
Special cargo filter criteria.
Definition: cargo_type.h:86
CargoFilterCriteria::CF_ANY
static constexpr CargoID CF_ANY
Show all items independent of carried cargo (i.e. no filtering)
Definition: cargo_type.h:87