OpenTTD Source  12.0-beta2
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_ORIGINAL_CARGO = 12,
65  NUM_CARGO = 64,
66 
67  CT_AUTO_REFIT = 0xFD,
68  CT_NO_REFIT = 0xFE,
69  CT_INVALID = 0xFF,
70 };
71 
73 inline bool IsCargoTypeValid(CargoType t) { return t != CT_INVALID; }
75 inline bool IsCargoIDValid(CargoID t) { return t != CT_INVALID; }
76 
77 typedef uint64 CargoTypes;
78 
79 static const CargoTypes ALL_CARGOTYPES = (CargoTypes)UINT64_MAX;
80 
82 struct CargoArray {
83 private:
84  uint amount[NUM_CARGO];
85 
86 public:
88  inline CargoArray()
89  {
90  this->Clear();
91  }
92 
94  inline void Clear()
95  {
96  memset(this->amount, 0, sizeof(this->amount));
97  }
98 
103  inline uint &operator[](CargoID cargo)
104  {
105  return this->amount[cargo];
106  }
107 
112  inline const uint &operator[](CargoID cargo) const
113  {
114  return this->amount[cargo];
115  }
116 
121  template <typename T>
122  inline const T GetSum() const
123  {
124  T ret = 0;
125  for (size_t i = 0; i < lengthof(this->amount); i++) {
126  ret += this->amount[i];
127  }
128  return ret;
129  }
130 
135  inline byte GetCount() const
136  {
137  byte count = 0;
138  for (size_t i = 0; i < lengthof(this->amount); i++) {
139  if (this->amount[i] != 0) count++;
140  }
141  return count;
142  }
143 };
144 
145 
147 enum SourceType : byte {
151 };
152 
153 typedef uint16 SourceID;
154 static const SourceID INVALID_SOURCE = 0xFFFF;
155 
156 #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:122
CargoArray::amount
uint amount[NUM_CARGO]
Amount of each type of cargo.
Definition: cargo_type.h:84
CargoArray
Class for storing amounts of cargo.
Definition: cargo_type.h:82
CargoArray::GetCount
byte GetCount() const
Get the amount of cargos that have an amount.
Definition: cargo_type.h:135
ST_TOWN
@ ST_TOWN
Source/destination is a town.
Definition: cargo_type.h:149
CargoArray::Clear
void Clear()
Reset all entries.
Definition: cargo_type.h:94
CargoArray::CargoArray
CargoArray()
Default constructor.
Definition: cargo_type.h:88
SourceID
uint16 SourceID
Contains either industry ID, town ID or company ID (or INVALID_SOURCE)
Definition: cargo_type.h:153
IsCargoIDValid
bool IsCargoIDValid(CargoID t)
Test whether cargo type is not CT_INVALID.
Definition: cargo_type.h:75
ST_INDUSTRY
@ ST_INDUSTRY
Source/destination is an industry.
Definition: cargo_type.h:148
ST_HEADQUARTERS
@ ST_HEADQUARTERS
Source/destination are company headquarters.
Definition: cargo_type.h:150
SourceType
SourceType
Types of cargo source and destination.
Definition: cargo_type.h:147
CT_AUTO_REFIT
@ CT_AUTO_REFIT
Automatically choose cargo type when doing auto refitting.
Definition: cargo_type.h:67
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:65
INVALID_SOURCE
static const SourceID INVALID_SOURCE
Invalid/unknown index of source.
Definition: cargo_type.h:154
enum_type.hpp
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
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:112
CargoArray::operator[]
uint & operator[](CargoID cargo)
Read/write access to an amount of a specific cargo type.
Definition: cargo_type.h:103
CT_INVALID
@ CT_INVALID
Invalid cargo type.
Definition: cargo_type.h:69
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:68
IsCargoTypeValid
bool IsCargoTypeValid(CargoType t)
Test whether cargo type is not CT_INVALID.
Definition: cargo_type.h:73