OpenTTD Source: src/cargotype.cpp Source File

OpenTTD Source 12.0-beta1

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
22 CargoSpec CargoSpec::array[NUM_CARGO];
23
28 CargoTypes _cargo_mask;
29
33 CargoTypes _standard_cargo_mask;
34
39 void SetupCargoForClimate(LandscapeID l)
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) = {};
46 CargoSpec::Get(i)->bitnum = INVALID_CARGO;
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++) {
55 CargoLabel cl = _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) {
70 *CargoSpec::Get(i) = _default_cargo[j];
71
72 /* Populate the available cargo mask */
73 SetBit(_cargo_mask, i);
74 break;
75 }
76 }
77 }
78 }
79
86 CargoID GetDefaultCargoID(LandscapeID l, CargoType ct)
87 {
88 assert(l < lengthof(_default_climate_cargo));
89
90 if (ct == CT_INVALID) return CT_INVALID;
91
92 assert(ct < lengthof(_default_climate_cargo[0]));
93 CargoLabel cl = _default_climate_cargo[l][ct];
94 /* Bzzt: check if cl is just an index into the cargo table */
95 if (cl < lengthof(_default_cargo)) {
96 cl = _default_cargo[cl].label;
97 }
98
99 return GetCargoIDByLabel(cl);
100 }
101
107 CargoID GetCargoIDByLabel(CargoLabel cl)
108 {
109 for (const CargoSpec *cs : CargoSpec::Iterate()) {
110 if (cs->label == cl) return cs->Index();
111 }
112
113 /* No matching label was found, so it is invalid */
114 return CT_INVALID;
115 }
116
117
123 CargoID GetCargoIDByBitnum(uint8 bitnum)
124 {
125 if (bitnum == INVALID_CARGO) return CT_INVALID;
126
127 for (const CargoSpec *cs : CargoSpec::Iterate()) {
128 if (cs->bitnum == bitnum) return cs->Index();
129 }
130
131 /* No matching label was found, so it is invalid */
132 return CT_INVALID;
133 }
134
139 SpriteID CargoSpec::GetCargoIcon() const
140 {
141 SpriteID sprite = this->sprite;
142 if (sprite == 0xFFFF) {
143 /* A value of 0xFFFF indicates we should draw a custom icon */
144 sprite = GetCustomCargoSprite(this);
145 }
146
147 if (sprite == 0) sprite = SPR_CARGO_GOODS;
148
149 return sprite;
150 }
151
152 std::vector<const CargoSpec *> _sorted_cargo_specs;
153 span<const CargoSpec *> _sorted_standard_cargo_specs;
154
156 static bool CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b)
157 {
158 static char a_name[64];
159 static char b_name[64];
160
161 GetString(a_name, a->name, lastof(a_name));
162 GetString(b_name, b->name, lastof(b_name));
163
164 int res = strnatcmp(a_name, b_name); // Sort by name (natural sorting).
165
166 /* If the names are equal, sort by cargo bitnum. */
167 return (res != 0) ? res < 0 : (a->bitnum < b->bitnum);
168 }
169
171 static bool CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * const &b)
172 {
173 int res = (b->classes & CC_PASSENGERS) - (a->classes & CC_PASSENGERS);
174 if (res == 0) {
175 res = (b->classes & CC_MAIL) - (a->classes & CC_MAIL);
176 if (res == 0) {
177 res = (a->classes & CC_SPECIAL) - (b->classes & CC_SPECIAL);
178 if (res == 0) {
179 return CargoSpecNameSorter(a, b);
180 }
181 }
182 }
183
184 return res < 0;
185 }
186
188 void InitializeSortedCargoSpecs()
189 {
190 _sorted_cargo_specs.clear();
191 /* Add each cargo spec to the list. */
192 for (const CargoSpec *cargo : CargoSpec::Iterate()) {
193 _sorted_cargo_specs.push_back(cargo);
194 }
195
196 /* Sort cargo specifications by cargo class and name. */
197 std::sort(_sorted_cargo_specs.begin(), _sorted_cargo_specs.end(), &CargoSpecClassSorter);
198
199 /* Count the number of standard cargos and fill the mask. */
200 _standard_cargo_mask = 0;
201 uint8 nb_standard_cargo = 0;
202 for (const auto &cargo : _sorted_cargo_specs) {
203 if (cargo->classes & CC_SPECIAL) break;
204 nb_standard_cargo++;
205 SetBit(_standard_cargo_mask, cargo->Index());
206 }
207
208 /* _sorted_standard_cargo_specs is a subset of _sorted_cargo_specs. */
209 _sorted_standard_cargo_specs = { _sorted_cargo_specs.data(), nb_standard_cargo };
210 }
211
cargo_const.h
INVALID_CARGO
static const byte INVALID_CARGO
Constant representing invalid cargo.
Definition: cargotype.h:54
CargoType
CargoType
Available types of cargo.
Definition: cargo_type.h:23
_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:59
_sorted_cargo_specs
std::vector< const CargoSpec * > _sorted_cargo_specs
Cargo specifications sorted alphabetically by name.
Definition: cargotype.cpp:152
_default_climate_cargo
static const CargoLabel _default_climate_cargo[NUM_LANDSCAPE][NUM_ORIGINAL_CARGO]
Table of cargo types available in each climate, by default.
Definition: cargo_const.h:99
GetCargoIDByBitnum
CargoID GetCargoIDByBitnum(uint8 bitnum)
Find the CargoID of a 'bitnum' value.
Definition: cargotype.cpp:123
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:119
SetupCargoForClimate
void SetupCargoForClimate(LandscapeID l)
Set up the default cargo types for the given landscape type.
Definition: cargotype.cpp:39
CargoSpec::Iterate
static IterateWrapper Iterate(size_t from=0)
Returns an iterable ensemble of all valid CargoSpec.
Definition: cargotype.h:168
CargoSpec
Specification of a cargo type.
Definition: cargotype.h:57
CargoSpec::GetCargoIcon
SpriteID GetCargoIcon() const
Get sprite for showing cargo of this type.
Definition: cargotype.cpp:139
CC_PASSENGERS
@ CC_PASSENGERS
Passengers.
Definition: cargotype.h:41
CargoLabel
uint32 CargoLabel
Globally unique label of a cargo type.
Definition: cargotype.h:23
CargoSpecNameSorter
static bool CargoSpecNameSorter(const CargoSpec *const &a, const CargoSpec *const &b)
Sort cargo specifications by their name.
Definition: cargotype.cpp:156
CargoSpec::bitnum
uint8 bitnum
Cargo bit number, is INVALID_CARGO for a non-used spec.
Definition: cargotype.h:58
CargoSpec::array
static CargoSpec array[NUM_CARGO]
Array holding all CargoSpecs.
Definition: cargotype.h:171
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
CC_SPECIAL
@ CC_SPECIAL
Special bit used for livery refit tricks instead of normal cargoes.
Definition: cargotype.h:51
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
_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:78
safeguards.h
sprites.h
stdafx.h
CargoSpecClassSorter
static bool CargoSpecClassSorter(const CargoSpec *const &a, const CargoSpec *const &b)
Sort cargo specifications by their cargo class.
Definition: cargotype.cpp:171
string_func.h
GetCustomCargoSprite
SpriteID GetCustomCargoSprite(const CargoSpec *cs)
Get the custom sprite for the given cargo type.
Definition: newgrf_cargo.cpp:54
strings_func.h
_sorted_standard_cargo_specs
span< const CargoSpec * > _sorted_standard_cargo_specs
Standard cargo specifications sorted alphabetically by name.
Definition: cargotype.cpp:153
GetDefaultCargoID
CargoID GetDefaultCargoID(LandscapeID l, CargoType ct)
Get the cargo ID of a default cargo, if present.
Definition: cargotype.cpp:86
InitializeSortedCargoSpecs
void InitializeSortedCargoSpecs()
Initialize the list of sorted cargo specifications.
Definition: cargotype.cpp:188
CargoSpec::classes
uint16 classes
Classes of this cargo type.
Definition: cargotype.h:80
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:65
cargotype.h
CargoSpec::name
StringID name
Name of this type of cargo.
Definition: cargotype.h:72
CargoSpec::multiplier
uint16 multiplier
Capacity multiplier for vehicles. (8 fractional bits)
Definition: cargotype.h:63
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:378
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:718
LandscapeID
byte LandscapeID
Landscape type.
Definition: landscape_type.h:13
CT_INVALID
@ CT_INVALID
Invalid cargo type.
Definition: cargo_type.h:69
CC_MAIL
@ CC_MAIL
Mail.
Definition: cargotype.h:42
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:394
GetCargoIDByLabel
CargoID GetCargoIDByLabel(CargoLabel cl)
Get the cargo ID by cargo label.
Definition: cargotype.cpp:107
newgrf_cargo.h
_default_cargo
static const CargoSpec _default_cargo[]
Cargo types available by default.
Definition: cargo_const.h:52