OpenTTD Source: src/cargo_type.h Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef CARGO_TYPE_H
11#define CARGO_TYPE_H
12
13#include "core/strong_typedef_type.hpp"
14
16 using CargoLabel = StrongType::Typedef<uint32_t, struct CargoLabelTag, StrongType::Compare>;
17
21 using CargoType = uint8_t;
22
28/* Temperate */
29 static constexpr CargoLabel CT_PASSENGERS{'PASS'};
30static constexpr CargoLabel CT_COAL{'COAL'};
31static constexpr CargoLabel CT_MAIL{'MAIL'};
32static constexpr CargoLabel CT_OIL{'OIL_'};
33static constexpr CargoLabel CT_LIVESTOCK{'LVST'};
34static constexpr CargoLabel CT_GOODS{'GOOD'};
35static constexpr CargoLabel CT_GRAIN{'GRAI'};
36static constexpr CargoLabel CT_WOOD{'WOOD'};
37static constexpr CargoLabel CT_IRON_ORE{'IORE'};
38static constexpr CargoLabel CT_STEEL{'STEL'};
39static constexpr CargoLabel CT_VALUABLES{'VALU'};
40
41/* Arctic */
42static constexpr CargoLabel CT_WHEAT{'WHEA'};
43static constexpr CargoLabel CT_PAPER{'PAPR'};
44static constexpr CargoLabel CT_GOLD{'GOLD'};
45static constexpr CargoLabel CT_FOOD{'FOOD'};
46
47/* Tropic */
48static constexpr CargoLabel CT_RUBBER{'RUBR'};
49static constexpr CargoLabel CT_FRUIT{'FRUT'};
50static constexpr CargoLabel CT_MAIZE{'MAIZ'};
51static constexpr CargoLabel CT_COPPER_ORE{'CORE'};
52static constexpr CargoLabel CT_WATER{'WATR'};
53static constexpr CargoLabel CT_DIAMONDS{'DIAM'};
54
55/* Toyland */
56static constexpr CargoLabel CT_SUGAR{'SUGR'};
57static constexpr CargoLabel CT_TOYS{'TOYS'};
58static constexpr CargoLabel CT_BATTERIES{'BATT'};
59static constexpr CargoLabel CT_CANDY{'SWET'};
60static constexpr CargoLabel CT_TOFFEE{'TOFF'};
61static constexpr CargoLabel CT_COLA{'COLA'};
62static constexpr CargoLabel CT_COTTON_CANDY{'CTCD'};
63static constexpr CargoLabel CT_BUBBLES{'BUBL'};
64static constexpr CargoLabel CT_PLASTIC{'PLST'};
65static constexpr CargoLabel CT_FIZZY_DRINKS{'FZDR'};
66
68 static constexpr CargoLabel CT_NONE = CT_PASSENGERS;
69
70 static constexpr CargoLabel CT_INVALID{UINT32_MAX};
71
72 static const CargoType NUM_ORIGINAL_CARGO = 12;
73 static const CargoType NUM_CARGO = 64;
74
75/* CARGO_AUTO_REFIT and CARGO_NO_REFIT are stored in save-games for refit-orders, so should not be changed. */
76 static const CargoType CARGO_AUTO_REFIT = 0xFD;
77 static const CargoType CARGO_NO_REFIT = 0xFE;
78
79static const CargoType INVALID_CARGO = UINT8_MAX;
80
82 enum MixedCargoType : uint8_t {
83 MCT_LIVESTOCK_FRUIT,
84 MCT_GRAIN_WHEAT_MAIZE,
85 MCT_VALUABLES_GOLD_DIAMONDS,
86};
87
92 namespace CargoFilterCriteria {
93 static constexpr CargoType CF_ANY = NUM_CARGO;
94 static constexpr CargoType CF_NONE = NUM_CARGO + 1;
95 static constexpr CargoType CF_ENGINES = NUM_CARGO + 2;
96 static constexpr CargoType CF_FREIGHT = NUM_CARGO + 3;
97
98 static constexpr CargoType CF_NO_RATING = NUM_CARGO + 4;
99 static constexpr CargoType CF_SELECT_ALL = NUM_CARGO + 5;
100 static constexpr CargoType CF_EXPAND_LIST = NUM_CARGO + 6;
101};
102
104 inline bool IsValidCargoType(CargoType cargo) { return cargo != INVALID_CARGO; }
105
106typedef uint64_t CargoTypes;
107
108static const CargoTypes ALL_CARGOTYPES = (CargoTypes)UINT64_MAX;
109
111 struct CargoArray : std::array<uint, NUM_CARGO> {
116 template <typename T>
117 inline const T GetSum() const
118 {
119 return std::reduce(this->begin(), this->end(), T{});
120 }
121
126 inline uint GetCount() const
127 {
128 return std::ranges::count_if(*this, [](uint amount) { return amount != 0; });
129 }
130};
131
132#endif /* CARGO_TYPE_H */
CT_INVALID
static constexpr CargoLabel CT_INVALID
Invalid cargo type.
Definition cargo_type.h:70
CargoType
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
IsValidCargoType
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:104
CT_NONE
static constexpr CargoLabel CT_NONE
Dummy label for engines that carry no cargo; they actually carry 0 passengers.
Definition cargo_type.h:68
NUM_ORIGINAL_CARGO
static const CargoType NUM_ORIGINAL_CARGO
Original number of cargo types.
Definition cargo_type.h:72
CARGO_AUTO_REFIT
static const CargoType CARGO_AUTO_REFIT
Automatically choose cargo type when doing auto refitting.
Definition cargo_type.h:76
MixedCargoType
MixedCargoType
Mixed cargo types for definitions with cargo that can vary depending on climate.
Definition cargo_type.h:82
MCT_GRAIN_WHEAT_MAIZE
@ MCT_GRAIN_WHEAT_MAIZE
Cargo can be grain, wheat or maize.
Definition cargo_type.h:84
MCT_LIVESTOCK_FRUIT
@ MCT_LIVESTOCK_FRUIT
Cargo can be livestock or fruit.
Definition cargo_type.h:83
MCT_VALUABLES_GOLD_DIAMONDS
@ MCT_VALUABLES_GOLD_DIAMONDS
Cargo can be valuables, gold or diamonds.
Definition cargo_type.h:85
CT_PASSENGERS
static constexpr CargoLabel CT_PASSENGERS
Available types of cargo Labels may be re-used between different climates.
Definition cargo_type.h:29
NUM_CARGO
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:73
CARGO_NO_REFIT
static const CargoType CARGO_NO_REFIT
Do not refit cargo of a vehicle (used in vehicle orders and auto-replace/auto-renew).
Definition cargo_type.h:77
CargoFilterCriteria
Special cargo filter criteria.
Definition cargo_type.h:92
CargoFilterCriteria::CF_FREIGHT
static constexpr CargoType CF_FREIGHT
Show only vehicles which carry any freight (non-passenger) cargo.
Definition cargo_type.h:96
CargoFilterCriteria::CF_NONE
static constexpr CargoType CF_NONE
Show only items which do not carry cargo (e.g. train engines)
Definition cargo_type.h:94
CargoFilterCriteria::CF_ENGINES
static constexpr CargoType CF_ENGINES
Show only engines (for rail vehicles only)
Definition cargo_type.h:95
CargoFilterCriteria::CF_EXPAND_LIST
static constexpr CargoType CF_EXPAND_LIST
Expand list to show all items (station list)
Definition cargo_type.h:100
CargoFilterCriteria::CF_ANY
static constexpr CargoType CF_ANY
Show all items independent of carried cargo (i.e. no filtering)
Definition cargo_type.h:93
CargoFilterCriteria::CF_NO_RATING
static constexpr CargoType CF_NO_RATING
Show items with no rating (station list)
Definition cargo_type.h:98
CargoFilterCriteria::CF_SELECT_ALL
static constexpr CargoType CF_SELECT_ALL
Select all items (station list)
Definition cargo_type.h:99
strong_typedef_type.hpp
Type (helpers) for making a strong typedef that is a distinct type.
CargoArray
Class for storing amounts of cargo.
Definition cargo_type.h:111
CargoArray::GetCount
uint GetCount() const
Get the amount of cargos that have an amount.
Definition cargo_type.h:126
CargoArray::GetSum
const T GetSum() const
Get the sum of all cargo amounts.
Definition cargo_type.h:117
StrongType::Typedef< uint32_t, struct CargoLabelTag, StrongType::Compare >