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