OpenTTD
cargotype.cpp
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 #include "stdafx.h"
11 #include "cargotype.h"
12 #include "newgrf_cargo.h"
13 #include "string_func.h"
14 #include "strings_func.h"
15 #include <algorithm>
16 
17 #include "table/sprites.h"
18 #include "table/strings.h"
19 #include "table/cargo_const.h"
20 
21 #include "safeguards.h"
22 
24 
29 CargoTypes _cargo_mask;
30 
35 
41 {
42  assert(l < lengthof(_default_climate_cargo));
43 
44  /* Reset and disable all cargo types */
45  memset(CargoSpec::array, 0, sizeof(CargoSpec::array));
46  for (CargoID i = 0; i < lengthof(CargoSpec::array); i++) {
48 
49  /* Set defaults for newer properties, which old GRFs do not know */
50  CargoSpec::Get(i)->multiplier = 0x100;
51  }
52 
53  _cargo_mask = 0;
54 
55  for (CargoID i = 0; i < lengthof(_default_climate_cargo[l]); i++) {
57 
58  /* Bzzt: check if cl is just an index into the cargo table */
59  if (cl < lengthof(_default_cargo)) {
60  /* Copy the indexed cargo */
61  CargoSpec *cargo = CargoSpec::Get(i);
62  *cargo = _default_cargo[cl];
63  if (cargo->bitnum != INVALID_CARGO) SetBit(_cargo_mask, i);
64  continue;
65  }
66 
67  /* Loop through each of the default cargo types to see if
68  * the label matches */
69  for (uint j = 0; j < lengthof(_default_cargo); j++) {
70  if (_default_cargo[j].label == cl) {
72 
73  /* Populate the available cargo mask */
74  SetBit(_cargo_mask, i);
75  break;
76  }
77  }
78  }
79 }
80 
87 {
88  const CargoSpec *cs;
89  FOR_ALL_CARGOSPECS(cs) {
90  if (cs->label == cl) return cs->Index();
91  }
92 
93  /* No matching label was found, so it is invalid */
94  return CT_INVALID;
95 }
96 
97 
104 {
105  if (bitnum == INVALID_CARGO) return CT_INVALID;
106 
107  const CargoSpec *cs;
108  FOR_ALL_CARGOSPECS(cs) {
109  if (cs->bitnum == bitnum) return cs->Index();
110  }
111 
112  /* No matching label was found, so it is invalid */
113  return CT_INVALID;
114 }
115 
121 {
122  SpriteID sprite = this->sprite;
123  if (sprite == 0xFFFF) {
124  /* A value of 0xFFFF indicates we should draw a custom icon */
125  sprite = GetCustomCargoSprite(this);
126  }
127 
128  if (sprite == 0) sprite = SPR_CARGO_GOODS;
129 
130  return sprite;
131 }
132 
133 std::vector<const CargoSpec *> _sorted_cargo_specs;
135 
136 
138 static bool CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b)
139 {
140  static char a_name[64];
141  static char b_name[64];
142 
143  GetString(a_name, a->name, lastof(a_name));
144  GetString(b_name, b->name, lastof(b_name));
145 
146  int res = strnatcmp(a_name, b_name); // Sort by name (natural sorting).
147 
148  /* If the names are equal, sort by cargo bitnum. */
149  return (res != 0) ? res < 0 : (a->bitnum < b->bitnum);
150 }
151 
153 static bool CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * const &b)
154 {
155  int res = (b->classes & CC_PASSENGERS) - (a->classes & CC_PASSENGERS);
156  if (res == 0) {
157  res = (b->classes & CC_MAIL) - (a->classes & CC_MAIL);
158  if (res == 0) {
159  res = (a->classes & CC_SPECIAL) - (b->classes & CC_SPECIAL);
160  if (res == 0) {
161  return CargoSpecNameSorter(a, b);
162  }
163  }
164  }
165 
166  return res < 0;
167 }
168 
171 {
172  _sorted_cargo_specs.clear();
173  const CargoSpec *cargo;
174  /* Add each cargo spec to the list. */
175  FOR_ALL_CARGOSPECS(cargo) {
176  _sorted_cargo_specs.push_back(cargo);
177  }
178 
179  /* Sort cargo specifications by cargo class and name. */
181 
183 
186  if (cargo->classes & CC_SPECIAL) break;
188  SetBit(_standard_cargo_mask, cargo->Index());
189  }
190 }
191 
Functions related to OTTD&#39;s strings.
Special bit used for livery refit tricks instead of normal cargoes.
Definition: cargotype.h:49
SpriteID GetCustomCargoSprite(const CargoSpec *cs)
Get the custom sprite for the given cargo type.
std::vector< const CargoSpec * > _sorted_cargo_specs
Cargo specifications sorted alphabetically by name.
Definition: cargotype.cpp:133
CargoTypes _cargo_mask
Bitmask of cargo types available.
Definition: cargotype.cpp:29
CargoID GetCargoIDByLabel(CargoLabel cl)
Get the cargo ID by cargo label.
Definition: cargotype.cpp:86
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Maximal number of cargo types in a game.
Definition: cargo_type.h:64
#define FOR_ALL_SORTED_CARGOSPECS(var)
Loop header for iterating over cargoes, sorted by name.
Definition: cargotype.h:164
Specification of a cargo type.
Definition: cargotype.h:55
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
uint16 classes
Classes of this cargo type.
Definition: cargotype.h:78
Mail.
Definition: cargotype.h:40
StringID name
Name of this type of cargo.
Definition: cargotype.h:70
uint16 multiplier
Capacity multiplier for vehicles. (8 fractional bits)
Definition: cargotype.h:61
Invalid cargo type.
Definition: cargo_type.h:68
Functions related to low-level strings.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
static bool CargoSpecNameSorter(const CargoSpec *const &a, const CargoSpec *const &b)
Sort cargo specifications by their name.
Definition: cargotype.cpp:138
Table of all default cargo types.
static const CargoLabel _default_climate_cargo[NUM_LANDSCAPE][12]
Table of cargo types available in each climate, by default.
Definition: cargo_const.h:165
CargoLabel label
Unique label of the cargo type.
Definition: cargotype.h:57
static const CargoSpec _default_cargo[]
Cargo types available by default.
Definition: cargo_const.h:14
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
static bool CargoSpecClassSorter(const CargoSpec *const &a, const CargoSpec *const &b)
Sort cargo specifications by their cargo class.
Definition: cargotype.cpp:153
uint8 _sorted_standard_cargo_specs_size
Number of standard cargo specifications stored in the _sorted_cargo_specs array.
Definition: cargotype.cpp:134
CargoID GetCargoIDByBitnum(uint8 bitnum)
Find the CargoID of a &#39;bitnum&#39; value.
Definition: cargotype.cpp:103
SpriteID GetCargoIcon() const
Get sprite for showing cargo of this type.
Definition: cargotype.cpp:120
int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition: string.cpp:578
byte LandscapeID
Landscape type.
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:117
Cargo support for NewGRFs.
void InitializeSortedCargoSpecs()
Initialize the list of sorted cargo specifications.
Definition: cargotype.cpp:170
static const byte INVALID_CARGO
Constant representing invalid cargo.
Definition: cargotype.h:52
uint32 CargoLabel
Globally unique label of a cargo type.
Definition: cargotype.h:21
Types/functions related to cargoes.
CargoID Index() const
Determines index of this cargospec.
Definition: cargotype.h:88
static CargoSpec array[NUM_CARGO]
Array holding all CargoSpecs.
Definition: cargotype.h:126
uint8 bitnum
Cargo bit number, is INVALID_CARGO for a non-used spec.
Definition: cargotype.h:56
Passengers.
Definition: cargotype.h:39
CargoTypes _standard_cargo_mask
Bitmask of real cargo types available.
Definition: cargotype.cpp:34
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
SpriteID sprite
Icon to display this cargo type, may be 0xFFF (which means to resolve an action123 chain)...
Definition: cargotype.h:76
This file contains all sprite-related enums and defines.
void SetupCargoForClimate(LandscapeID l)
Set up the default cargo types for the given landscape type.
Definition: cargotype.cpp:40