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