OpenTTD
cargo_type.h
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef CARGO_TYPE_H
13 #define CARGO_TYPE_H
14 
15 #include "core/enum_type.hpp"
16 
22 typedef byte CargoID;
23 
25 enum CargoType {
26  /* Temperate */
27  CT_PASSENGERS = 0,
28  CT_COAL = 1,
29  CT_MAIL = 2,
30  CT_OIL = 3,
31  CT_LIVESTOCK = 4,
32  CT_GOODS = 5,
33  CT_GRAIN = 6,
34  CT_WOOD = 7,
35  CT_IRON_ORE = 8,
36  CT_STEEL = 9,
37  CT_VALUABLES = 10,
38 
39  /* Arctic */
40  CT_WHEAT = 6,
41  CT_HILLY_UNUSED = 8,
42  CT_PAPER = 9,
43  CT_GOLD = 10,
44  CT_FOOD = 11,
45 
46  /* Tropic */
47  CT_RUBBER = 1,
48  CT_FRUIT = 4,
49  CT_MAIZE = 6,
50  CT_COPPER_ORE = 8,
51  CT_WATER = 9,
52  CT_DIAMONDS = 10,
53 
54  /* Toyland */
55  CT_SUGAR = 1,
56  CT_TOYS = 3,
57  CT_BATTERIES = 4,
58  CT_CANDY = 5,
59  CT_TOFFEE = 6,
60  CT_COLA = 7,
61  CT_COTTON_CANDY = 8,
62  CT_BUBBLES = 9,
63  CT_PLASTIC = 10,
64  CT_FIZZY_DRINKS = 11,
65 
66  NUM_CARGO = 64,
67 
68  CT_AUTO_REFIT = 0xFD,
69  CT_NO_REFIT = 0xFE,
70  CT_INVALID = 0xFF,
71 };
72 
74 inline bool IsCargoTypeValid(CargoType t) { return t != CT_INVALID; }
76 inline bool IsCargoIDValid(CargoID t) { return t != CT_INVALID; }
77 
78 typedef uint64 CargoTypes;
79 
80 static const CargoTypes ALL_CARGOTYPES = (CargoTypes)UINT64_MAX;
81 
83 struct CargoArray {
84 private:
85  uint amount[NUM_CARGO];
86 
87 public:
89  inline CargoArray()
90  {
91  this->Clear();
92  }
93 
95  inline void Clear()
96  {
97  memset(this->amount, 0, sizeof(this->amount));
98  }
99 
104  inline uint &operator[](CargoID cargo)
105  {
106  return this->amount[cargo];
107  }
108 
113  inline const uint &operator[](CargoID cargo) const
114  {
115  return this->amount[cargo];
116  }
117 
122  template <typename T>
123  inline const T GetSum() const
124  {
125  T ret = 0;
126  for (size_t i = 0; i < lengthof(this->amount); i++) {
127  ret += this->amount[i];
128  }
129  return ret;
130  }
131 
136  inline byte GetCount() const
137  {
138  byte count = 0;
139  for (size_t i = 0; i < lengthof(this->amount); i++) {
140  if (this->amount[i] != 0) count++;
141  }
142  return count;
143  }
144 };
145 
146 
152 };
154 
155 typedef uint16 SourceID;
156 static const SourceID INVALID_SOURCE = 0xFFFF;
157 
158 #endif /* CARGO_TYPE_H */
uint amount[NUM_CARGO]
Amount of each type of cargo.
Definition: cargo_type.h:85
Source/destination is a town.
Definition: cargo_type.h:150
byte GetCount() const
Get the amount of cargos that have an amount.
Definition: cargo_type.h:136
Maximal number of cargo types in a game.
Definition: cargo_type.h:66
bool IsCargoTypeValid(CargoType t)
Test whether cargo type is not CT_INVALID.
Definition: cargo_type.h:74
Automatically choose cargo type when doing auto refitting.
Definition: cargo_type.h:68
bool IsCargoIDValid(CargoID t)
Test whether cargo type is not CT_INVALID.
Definition: cargo_type.h:76
Type (helpers) for enums.
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
Definition: cargo_type.h:156
Do not refit cargo of a vehicle (used in vehicle orders and auto-replace/auto-new).
Definition: cargo_type.h:69
Invalid cargo type.
Definition: cargo_type.h:70
void Clear()
Reset all entries.
Definition: cargo_type.h:95
CargoType
Available types of cargo.
Definition: cargo_type.h:25
CargoArray()
Default constructor.
Definition: cargo_type.h:89
Source/destination are company headquarters.
Definition: cargo_type.h:151
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:148
Class for storing amounts of cargo.
Definition: cargo_type.h:83
SimpleTinyEnumT< SourceType, byte > SourceTypeByte
The SourceType packed into a byte for savegame purposes.
Definition: cargo_type.h:153
Source/destination is an industry.
Definition: cargo_type.h:149
uint16 SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:155
const uint & operator[](CargoID cargo) const
Read-only access to an amount of a specific cargo type.
Definition: cargo_type.h:113
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
uint & operator[](CargoID cargo)
Read/write access to an amount of a specific cargo type.
Definition: cargo_type.h:104
const T GetSum() const
Get the sum of all cargo amounts.
Definition: cargo_type.h:123