OpenTTD
cargo_type.h
Go to the documentation of this file.
1 /* $Id: cargo_type.h 23826 2012-01-20 20:18:19Z rubidium $ */
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 CargoTypes {
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 = 32,
67 
68  CT_AUTO_REFIT = 0xFD,
69  CT_NO_REFIT = 0xFE,
70  CT_INVALID = 0xFF,
71 };
72 
74 struct CargoArray {
75 private:
76  uint amount[NUM_CARGO];
77 
78 public:
80  inline CargoArray()
81  {
82  this->Clear();
83  }
84 
86  inline void Clear()
87  {
88  memset(this->amount, 0, sizeof(this->amount));
89  }
90 
95  inline uint &operator[](CargoID cargo)
96  {
97  return this->amount[cargo];
98  }
99 
104  inline const uint &operator[](CargoID cargo) const
105  {
106  return this->amount[cargo];
107  }
108 
113  template <typename T>
114  inline const T GetSum() const
115  {
116  T ret = 0;
117  for (size_t i = 0; i < lengthof(this->amount); i++) {
118  ret += this->amount[i];
119  }
120  return ret;
121  }
122 
127  inline byte GetCount() const
128  {
129  byte count = 0;
130  for (size_t i = 0; i < lengthof(this->amount); i++) {
131  if (this->amount[i] != 0) count++;
132  }
133  return count;
134  }
135 };
136 
137 
143 };
145 
146 typedef uint16 SourceID;
147 static const SourceID INVALID_SOURCE = 0xFFFF;
148 
149 #endif /* CARGO_TYPE_H */
uint amount[NUM_CARGO]
Amount of each type of cargo.
Definition: cargo_type.h:76
Source/destination is a town.
Definition: cargo_type.h:141
Automatically choose cargo type when doing auto refitting.
Definition: cargo_type.h:68
byte GetCount() const
Get the amount of cargos that have an amount.
Definition: cargo_type.h:127
Type (helpers) for enums.
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
Definition: cargo_type.h:147
void Clear()
Reset all entries.
Definition: cargo_type.h:86
Do not refit cargo of a vehicle (used in vehicle orders and auto-replace/auto-new).
Definition: cargo_type.h:69
CargoTypes
Available types of cargo.
Definition: cargo_type.h:25
CargoArray()
Default constructor.
Definition: cargo_type.h:80
Source/destination are company headquarters.
Definition: cargo_type.h:142
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
Invalid cargo type.
Definition: cargo_type.h:70
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:139
Maximal number of cargo types in a game.
Definition: cargo_type.h:66
Class for storing amounts of cargo.
Definition: cargo_type.h:74
SimpleTinyEnumT< SourceType, byte > SourceTypeByte
The SourceType packed into a byte for savegame purposes.
Definition: cargo_type.h:144
Source/destination is an industry.
Definition: cargo_type.h:140
uint16 SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:146
const uint & operator[](CargoID cargo) const
Read-only access to an amount of a specific cargo type.
Definition: cargo_type.h:104
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:95
const T GetSum() const
Get the sum of all cargo amounts.
Definition: cargo_type.h:114