OpenTTD
cargotype.cpp
Go to the documentation of this file.
1 /* $Id: cargotype.cpp 26482 2014-04-23 20:13:33Z 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 #include "stdafx.h"
13 #include "cargotype.h"
14 #include "newgrf_cargo.h"
15 #include "string_func.h"
16 #include "strings_func.h"
17 #include "core/sort_func.hpp"
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 uint32 _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 
138 
139 
141 static int CDECL CargoSpecNameSorter(const CargoSpec * const *a, const CargoSpec * const *b)
142 {
143  static char a_name[64];
144  static char b_name[64];
145 
146  GetString(a_name, (*a)->name, lastof(a_name));
147  GetString(b_name, (*b)->name, lastof(b_name));
148 
149  int res = strnatcmp(a_name, b_name); // Sort by name (natural sorting).
150 
151  /* If the names are equal, sort by cargo bitnum. */
152  return (res != 0) ? res : ((*a)->bitnum - (*b)->bitnum);
153 }
154 
156 static int CDECL CargoSpecClassSorter(const CargoSpec * const *a, const CargoSpec * const *b)
157 {
158  int res = ((*b)->classes & CC_PASSENGERS) - ((*a)->classes & CC_PASSENGERS);
159  if (res == 0) {
160  res = ((*b)->classes & CC_MAIL) - ((*a)->classes & CC_MAIL);
161  if (res == 0) {
162  res = ((*a)->classes & CC_SPECIAL) - ((*b)->classes & CC_SPECIAL);
163  if (res == 0) {
164  return CargoSpecNameSorter(a, b);
165  }
166  }
167  }
168 
169  return res;
170 }
171 
174 {
176  const CargoSpec *cargo;
177  /* Add each cargo spec to the list. */
178  FOR_ALL_CARGOSPECS(cargo) {
179  _sorted_cargo_specs[_sorted_cargo_specs_size] = cargo;
181  }
182 
183  /* Sort cargo specifications by cargo class and name. */
184  QSortT(_sorted_cargo_specs, _sorted_cargo_specs_size, &CargoSpecClassSorter);
185 
187 
190  if (cargo->classes & CC_SPECIAL) break;
192  SetBit(_standard_cargo_mask, cargo->Index());
193  }
194 }
195 
Functions related to OTTD&#39;s strings.
Special bit used for livery refit tricks instead of normal cargoes.
Definition: cargotype.h:50
SpriteID GetCustomCargoSprite(const CargoSpec *cs)
Get the custom sprite for the given cargo type.
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.
#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:56
uint8 _sorted_cargo_specs_size
Number of cargo specifications stored at the _sorted_cargo_specs array (including special cargoes)...
Definition: cargotype.cpp:136
#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:79
Mail.
Definition: cargotype.h:41
uint16 multiplier
Capacity multiplier for vehicles. (8 fractional bits)
Definition: cargotype.h:62
Functions related to low-level strings.
static int CDECL CargoSpecNameSorter(const CargoSpec *const *a, const CargoSpec *const *b)
Sort cargo specifications by their name.
Definition: cargotype.cpp:141
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
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:58
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
Invalid cargo type.
Definition: cargo_type.h:70
static int CDECL CargoSpecClassSorter(const CargoSpec *const *a, const CargoSpec *const *b)
Sort cargo specifications by their cargo class.
Definition: cargotype.cpp:156
Maximal number of cargo types in a game.
Definition: cargo_type.h:66
uint8 _sorted_standard_cargo_specs_size
Number of standard cargo specifications stored at the _sorted_cargo_specs array.
Definition: cargotype.cpp:137
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:569
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:118
Cargo support for NewGRFs.
void InitializeSortedCargoSpecs()
Initialize the list of sorted cargo specifications.
Definition: cargotype.cpp:173
Functions related to sorting operations.
static const byte INVALID_CARGO
Constant representing invalid cargo.
Definition: cargotype.h:53
uint32 CargoLabel
Globally unique label of a cargo type.
Definition: cargotype.h:22
uint32 _cargo_mask
Bitmask of cargo types available.
Definition: cargotype.cpp:31
Types/functions related to cargoes.
CargoID Index() const
Determines index of this cargospec.
Definition: cargotype.h:89
uint32 _standard_cargo_mask
Bitmask of real cargo types available.
Definition: cargotype.cpp:36
static CargoSpec array[NUM_CARGO]
Array holding all CargoSpecs.
Definition: cargotype.h:127
uint8 bitnum
Cargo bit number, is INVALID_CARGO for a non-used spec.
Definition: cargotype.h:57
Passengers.
Definition: cargotype.h:40
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
const CargoSpec * _sorted_cargo_specs[NUM_CARGO]
Cargo specifications sorted alphabetically by name.
Definition: cargotype.cpp:135
SpriteID sprite
Icon to display this cargo type, may be 0xFFF (which means to resolve an action123 chain)...
Definition: cargotype.h:77
static void QSortT(T *base, uint num, int(CDECL *comparator)(const T *, const T *), bool desc=false)
Type safe qsort()
Definition: sort_func.hpp:28
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