Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / cargo_type.h
blob83af40a83a06dc2ad095a7981c7bffee639fd5bc
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 */
8 /** @file cargo_type.h Types related to cargoes... */
10 #ifndef CARGO_TYPE_H
11 #define CARGO_TYPE_H
13 #include "core/enum_type.hpp"
15 /**
16 * Cargo slots to indicate a cargo type within a game.
17 * Numbers are re-used between different climates.
18 * @see CargoTypes
20 typedef byte CargoID;
22 /** Available types of cargo */
23 enum CargoType {
24 /* Temperate */
25 CT_PASSENGERS = 0,
26 CT_COAL = 1,
27 CT_MAIL = 2,
28 CT_OIL = 3,
29 CT_LIVESTOCK = 4,
30 CT_GOODS = 5,
31 CT_GRAIN = 6,
32 CT_WOOD = 7,
33 CT_IRON_ORE = 8,
34 CT_STEEL = 9,
35 CT_VALUABLES = 10,
37 /* Arctic */
38 CT_WHEAT = 6,
39 CT_HILLY_UNUSED = 8,
40 CT_PAPER = 9,
41 CT_GOLD = 10,
42 CT_FOOD = 11,
44 /* Tropic */
45 CT_RUBBER = 1,
46 CT_FRUIT = 4,
47 CT_MAIZE = 6,
48 CT_COPPER_ORE = 8,
49 CT_WATER = 9,
50 CT_DIAMONDS = 10,
52 /* Toyland */
53 CT_SUGAR = 1,
54 CT_TOYS = 3,
55 CT_BATTERIES = 4,
56 CT_CANDY = 5,
57 CT_TOFFEE = 6,
58 CT_COLA = 7,
59 CT_COTTON_CANDY = 8,
60 CT_BUBBLES = 9,
61 CT_PLASTIC = 10,
62 CT_FIZZY_DRINKS = 11,
64 NUM_ORIGINAL_CARGO = 12,
65 NUM_CARGO = 64, ///< Maximal number of cargo types in a game.
67 CT_AUTO_REFIT = 0xFD, ///< Automatically choose cargo type when doing auto refitting.
68 CT_NO_REFIT = 0xFE, ///< Do not refit cargo of a vehicle (used in vehicle orders and auto-replace/auto-new).
69 CT_INVALID = 0xFF, ///< Invalid cargo type.
72 /** Test whether cargo type is not CT_INVALID */
73 inline bool IsCargoTypeValid(CargoType t) { return t != CT_INVALID; }
74 /** Test whether cargo type is not CT_INVALID */
75 inline bool IsCargoIDValid(CargoID t) { return t != CT_INVALID; }
77 typedef uint64 CargoTypes;
79 static const CargoTypes ALL_CARGOTYPES = (CargoTypes)UINT64_MAX;
81 /** Class for storing amounts of cargo */
82 struct CargoArray {
83 private:
84 uint amount[NUM_CARGO]; ///< Amount of each type of cargo.
86 public:
87 /** Default constructor. */
88 inline CargoArray()
90 this->Clear();
93 /** Reset all entries. */
94 inline void Clear()
96 memset(this->amount, 0, sizeof(this->amount));
99 /**
100 * Read/write access to an amount of a specific cargo type.
101 * @param cargo Cargo type to access.
103 inline uint &operator[](CargoID cargo)
105 return this->amount[cargo];
109 * Read-only access to an amount of a specific cargo type.
110 * @param cargo Cargo type to access.
112 inline const uint &operator[](CargoID cargo) const
114 return this->amount[cargo];
118 * Get the sum of all cargo amounts.
119 * @return The sum.
121 template <typename T>
122 inline const T GetSum() const
124 T ret = 0;
125 for (size_t i = 0; i < lengthof(this->amount); i++) {
126 ret += this->amount[i];
128 return ret;
132 * Get the amount of cargos that have an amount.
133 * @return The amount.
135 inline byte GetCount() const
137 byte count = 0;
138 for (size_t i = 0; i < lengthof(this->amount); i++) {
139 if (this->amount[i] != 0) count++;
141 return count;
146 /** Types of cargo source and destination */
147 enum SourceType : byte {
148 ST_INDUSTRY, ///< Source/destination is an industry
149 ST_TOWN, ///< Source/destination is a town
150 ST_HEADQUARTERS, ///< Source/destination are company headquarters
153 typedef uint16 SourceID; ///< Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
154 static const SourceID INVALID_SOURCE = 0xFFFF; ///< Invalid/unknown index of source
156 #endif /* CARGO_TYPE_H */