OpenTTD Source: src/cargo_type.h Source File

OpenTTD Source 1.11.0-RC1

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"
14
20 typedef byte CargoID;
21
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,
36
37 /* Arctic */
38 CT_WHEAT = 6,
39 CT_HILLY_UNUSED = 8,
40 CT_PAPER = 9,
41 CT_GOLD = 10,
42 CT_FOOD = 11,
43
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,
51
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,
63
64 NUM_CARGO = 64,
65
66 CT_AUTO_REFIT = 0xFD,
67 CT_NO_REFIT = 0xFE,
68 CT_INVALID = 0xFF,
69 };
70
72 inline bool IsCargoTypeValid(CargoType t) { return t != CT_INVALID; }
74 inline bool IsCargoIDValid(CargoID t) { return t != CT_INVALID; }
75
76 typedef uint64 CargoTypes;
77
78 static const CargoTypes ALL_CARGOTYPES = (CargoTypes)UINT64_MAX;
79
81 struct CargoArray {
82 private:
83 uint amount[NUM_CARGO];
84
85 public:
87 inline CargoArray()
88 {
89 this->Clear();
90 }
91
93 inline void Clear()
94 {
95 memset(this->amount, 0, sizeof(this->amount));
96 }
97
102 inline uint &operator[](CargoID cargo)
103 {
104 return this->amount[cargo];
105 }
106
111 inline const uint &operator[](CargoID cargo) const
112 {
113 return this->amount[cargo];
114 }
115
120 template <typename T>
121 inline const T GetSum() const
122 {
123 T ret = 0;
124 for (size_t i = 0; i < lengthof(this->amount); i++) {
125 ret += this->amount[i];
126 }
127 return ret;
128 }
129
134 inline byte GetCount() const
135 {
136 byte count = 0;
137 for (size_t i = 0; i < lengthof(this->amount); i++) {
138 if (this->amount[i] != 0) count++;
139 }
140 return count;
141 }
142 };
143
144
146 enum SourceType : byte {
147 ST_INDUSTRY,
148 ST_TOWN,
149 ST_HEADQUARTERS,
150 };
151
152 typedef uint16 SourceID;
153 static const SourceID INVALID_SOURCE = 0xFFFF;
154
155 #endif /* CARGO_TYPE_H */
CargoType
CargoType
Available types of cargo.
Definition: cargo_type.h:23
CargoArray::GetSum
const T GetSum() const
Get the sum of all cargo amounts.
Definition: cargo_type.h:121
CargoArray::amount
uint amount[NUM_CARGO]
Amount of each type of cargo.
Definition: cargo_type.h:83
CargoArray
Class for storing amounts of cargo.
Definition: cargo_type.h:81
CargoArray::GetCount
byte GetCount() const
Get the amount of cargos that have an amount.
Definition: cargo_type.h:134
ST_TOWN
@ ST_TOWN
Source/destination is a town.
Definition: cargo_type.h:148
CargoArray::Clear
void Clear()
Reset all entries.
Definition: cargo_type.h:93
CargoArray::CargoArray
CargoArray()
Default constructor.
Definition: cargo_type.h:87
SourceID
uint16 SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:152
IsCargoIDValid
bool IsCargoIDValid(CargoID t)
Test whether cargo type is not CT_INVALID.
Definition: cargo_type.h:74
ST_INDUSTRY
@ ST_INDUSTRY
Source/destination is an industry.
Definition: cargo_type.h:147
ST_HEADQUARTERS
@ ST_HEADQUARTERS
Source/destination are company headquarters.
Definition: cargo_type.h:149
SourceType
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:146
CT_AUTO_REFIT
@ CT_AUTO_REFIT
Automatically choose cargo type when doing auto refitting.
Definition: cargo_type.h:66
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:64
INVALID_SOURCE
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
Definition: cargo_type.h:153
enum_type.hpp
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
CargoArray::operator[]
const uint & operator[](CargoID cargo) const
Read-only access to an amount of a specific cargo type.
Definition: cargo_type.h:111
CargoArray::operator[]
uint & operator[](CargoID cargo)
Read/write access to an amount of a specific cargo type.
Definition: cargo_type.h:102
CT_INVALID
@ CT_INVALID
Invalid cargo type.
Definition: cargo_type.h:68
CT_NO_REFIT
@ CT_NO_REFIT
Do not refit cargo of a vehicle (used in vehicle orders and auto-replace/auto-new).
Definition: cargo_type.h:67
IsCargoTypeValid
bool IsCargoTypeValid(CargoType t)
Test whether cargo type is not CT_INVALID.
Definition: cargo_type.h:72