OpenTTD Source 20241224-master-gf74b0cf984
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
22using CargoID = uint8_t;
23
29/* Temperate */
30static constexpr CargoLabel CT_PASSENGERS = CargoLabel{'PASS'};
31static constexpr CargoLabel CT_COAL = CargoLabel{'COAL'};
32static constexpr CargoLabel CT_MAIL = CargoLabel{'MAIL'};
33static constexpr CargoLabel CT_OIL = CargoLabel{'OIL_'};
34static constexpr CargoLabel CT_LIVESTOCK = CargoLabel{'LVST'};
35static constexpr CargoLabel CT_GOODS = CargoLabel{'GOOD'};
36static constexpr CargoLabel CT_GRAIN = CargoLabel{'GRAI'};
37static constexpr CargoLabel CT_WOOD = CargoLabel{'WOOD'};
38static constexpr CargoLabel CT_IRON_ORE = CargoLabel{'IORE'};
39static constexpr CargoLabel CT_STEEL = CargoLabel{'STEL'};
40static constexpr CargoLabel CT_VALUABLES = CargoLabel{'VALU'};
41
42/* Arctic */
43static constexpr CargoLabel CT_WHEAT = CargoLabel{'WHEA'};
44static constexpr CargoLabel CT_PAPER = CargoLabel{'PAPR'};
45static constexpr CargoLabel CT_GOLD = CargoLabel{'GOLD'};
46static constexpr CargoLabel CT_FOOD = CargoLabel{'FOOD'};
47
48/* Tropic */
49static constexpr CargoLabel CT_RUBBER = CargoLabel{'RUBR'};
50static constexpr CargoLabel CT_FRUIT = CargoLabel{'FRUT'};
51static constexpr CargoLabel CT_MAIZE = CargoLabel{'MAIZ'};
52static constexpr CargoLabel CT_COPPER_ORE = CargoLabel{'CORE'};
53static constexpr CargoLabel CT_WATER = CargoLabel{'WATR'};
54static constexpr CargoLabel CT_DIAMONDS = CargoLabel{'DIAM'};
55
56/* Toyland */
57static constexpr CargoLabel CT_SUGAR = CargoLabel{'SUGR'};
58static constexpr CargoLabel CT_TOYS = CargoLabel{'TOYS'};
59static constexpr CargoLabel CT_BATTERIES = CargoLabel{'BATT'};
60static constexpr CargoLabel CT_CANDY = CargoLabel{'SWET'};
61static constexpr CargoLabel CT_TOFFEE = CargoLabel{'TOFF'};
62static constexpr CargoLabel CT_COLA = CargoLabel{'COLA'};
63static constexpr CargoLabel CT_COTTON_CANDY = CargoLabel{'CTCD'};
64static constexpr CargoLabel CT_BUBBLES = CargoLabel{'BUBL'};
65static constexpr CargoLabel CT_PLASTIC = CargoLabel{'PLST'};
66static constexpr CargoLabel CT_FIZZY_DRINKS = CargoLabel{'FZDR'};
67
69static constexpr CargoLabel CT_NONE = CT_PASSENGERS;
70
71static constexpr CargoLabel CT_INVALID = CargoLabel{UINT32_MAX};
72
73static const CargoID NUM_ORIGINAL_CARGO = 12;
74static 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. */
77static const CargoID CARGO_AUTO_REFIT = 0xFD;
78static const CargoID CARGO_NO_REFIT = 0xFE;
79
80static const CargoID INVALID_CARGO = UINT8_MAX;
81
88
94 static constexpr CargoID CF_ANY = NUM_CARGO;
95 static constexpr CargoID CF_NONE = NUM_CARGO + 1;
96 static constexpr CargoID CF_ENGINES = NUM_CARGO + 2;
97 static constexpr CargoID CF_FREIGHT = NUM_CARGO + 3;
98
99 static constexpr CargoID CF_NO_RATING = NUM_CARGO + 4;
100 static constexpr CargoID CF_SELECT_ALL = NUM_CARGO + 5;
101 static constexpr CargoID CF_EXPAND_LIST = NUM_CARGO + 6;
102};
103
105inline bool IsValidCargoType(CargoLabel t) { return t != CT_INVALID; }
107inline bool IsValidCargoID(CargoID t) { return t != INVALID_CARGO; }
108
109typedef uint64_t CargoTypes;
110
111static const CargoTypes ALL_CARGOTYPES = (CargoTypes)UINT64_MAX;
112
114struct CargoArray : std::array<uint, NUM_CARGO> {
119 template <typename T>
120 inline const T GetSum() const
121 {
122 return std::reduce(this->begin(), this->end(), T{});
123 }
124
129 inline uint GetCount() const
130 {
131 return std::ranges::count_if(*this, [](uint amount) { return amount != 0; });
132 }
133};
134
135
137enum class SourceType : uint8_t {
138 Industry,
139 Town,
141};
142
143typedef uint16_t SourceID;
144static const SourceID INVALID_SOURCE = 0xFFFF;
145
146#endif /* CARGO_TYPE_H */
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
static constexpr CargoLabel CT_INVALID
Invalid cargo type.
Definition cargo_type.h:71
bool IsValidCargoType(CargoLabel t)
Test whether cargo type is not CT_INVALID.
Definition cargo_type.h:105
static constexpr CargoLabel CT_NONE
Dummy label for engines that carry no cargo; they actually carry 0 passengers.
Definition cargo_type.h:69
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
bool IsValidCargoID(CargoID t)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:107
static const CargoID NUM_ORIGINAL_CARGO
Original number of cargo types.
Definition cargo_type.h:73
uint16_t SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition cargo_type.h:143
static constexpr CargoLabel CT_PASSENGERS
Available types of cargo Labels may be re-used between different climates.
Definition cargo_type.h:30
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
Definition cargo_type.h:144
MixedCargoType
Mixed cargo types for definitions with cargo that can vary depending on climate.
Definition cargo_type.h:83
@ MCT_GRAIN_WHEAT_MAIZE
Cargo can be grain, wheat or maize.
Definition cargo_type.h:85
@ MCT_LIVESTOCK_FRUIT
Cargo can be livestock or fruit.
Definition cargo_type.h:84
@ MCT_VALUABLES_GOLD_DIAMONDS
Cargo can be valuables, gold or diamonds.
Definition cargo_type.h:86
static const CargoID NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:74
SourceType
Types of cargo source and destination.
Definition cargo_type.h:137
@ Industry
Source/destination is an industry.
@ Headquarters
Source/destination are company headquarters.
@ Town
Source/destination is a town.
static const CargoID CARGO_AUTO_REFIT
Automatically choose cargo type when doing auto refitting.
Definition cargo_type.h:77
Type (helpers) for enums.
Special cargo filter criteria.
Definition cargo_type.h:93
static constexpr CargoID CF_NONE
Show only items which do not carry cargo (e.g. train engines)
Definition cargo_type.h:95
static constexpr CargoID CF_NO_RATING
Show items with no rating (station list)
Definition cargo_type.h:99
static constexpr CargoID CF_ANY
Show all items independent of carried cargo (i.e. no filtering)
Definition cargo_type.h:94
static constexpr CargoID CF_FREIGHT
Show only vehicles which carry any freight (non-passenger) cargo.
Definition cargo_type.h:97
static constexpr CargoID CF_EXPAND_LIST
Expand list to show all items (station list)
Definition cargo_type.h:101
static constexpr CargoID CF_ENGINES
Show only engines (for rail vehicles only)
Definition cargo_type.h:96
static constexpr CargoID CF_SELECT_ALL
Select all items (station list)
Definition cargo_type.h:100
Type (helpers) for making a strong typedef that is a distinct type.
Class for storing amounts of cargo.
Definition cargo_type.h:114
uint GetCount() const
Get the amount of cargos that have an amount.
Definition cargo_type.h:129
const T GetSum() const
Get the sum of all cargo amounts.
Definition cargo_type.h:120