OpenTTD Source: src/cargotype.cpp Source File

OpenTTD Source 13.1

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 "settings_type.h"
16
17 #include "table/sprites.h"
18 #include "table/strings.h"
19 #include "table/cargo_const.h"
20
21 #include "safeguards.h"
22
23 CargoSpec CargoSpec::array[NUM_CARGO];
24
29 CargoTypes _cargo_mask;
30
34 CargoTypes _standard_cargo_mask;
35
40 void SetupCargoForClimate(LandscapeID l)
41 {
42 assert(l < lengthof(_default_climate_cargo));
43
44 /* Reset and disable all cargo types */
45 for (CargoID i = 0; i < lengthof(CargoSpec::array); i++) {
46 *CargoSpec::Get(i) = {};
47 CargoSpec::Get(i)->bitnum = INVALID_CARGO;
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++) {
56 CargoLabel cl = _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) {
71 *CargoSpec::Get(i) = _default_cargo[j];
72
73 /* Populate the available cargo mask */
74 SetBit(_cargo_mask, i);
75 break;
76 }
77 }
78 }
79 }
80
87 CargoID GetDefaultCargoID(LandscapeID l, CargoType ct)
88 {
89 assert(l < lengthof(_default_climate_cargo));
90
91 if (ct == CT_INVALID) return CT_INVALID;
92
93 assert(ct < lengthof(_default_climate_cargo[0]));
94 CargoLabel cl = _default_climate_cargo[l][ct];
95 /* Bzzt: check if cl is just an index into the cargo table */
96 if (cl < lengthof(_default_cargo)) {
97 cl = _default_cargo[cl].label;
98 }
99
100 return GetCargoIDByLabel(cl);
101 }
102
108 CargoID GetCargoIDByLabel(CargoLabel cl)
109 {
110 for (const CargoSpec *cs : CargoSpec::Iterate()) {
111 if (cs->label == cl) return cs->Index();
112 }
113
114 /* No matching label was found, so it is invalid */
115 return CT_INVALID;
116 }
117
118
124 CargoID GetCargoIDByBitnum(uint8 bitnum)
125 {
126 if (bitnum == INVALID_CARGO) return CT_INVALID;
127
128 for (const CargoSpec *cs : CargoSpec::Iterate()) {
129 if (cs->bitnum == bitnum) return cs->Index();
130 }
131
132 /* No matching label was found, so it is invalid */
133 return CT_INVALID;
134 }
135
140 SpriteID CargoSpec::GetCargoIcon() const
141 {
142 SpriteID sprite = this->sprite;
143 if (sprite == 0xFFFF) {
144 /* A value of 0xFFFF indicates we should draw a custom icon */
145 sprite = GetCustomCargoSprite(this);
146 }
147
148 if (sprite == 0) sprite = SPR_CARGO_GOODS;
149
150 return sprite;
151 }
152
153 std::vector<const CargoSpec *> _sorted_cargo_specs;
154 span<const CargoSpec *> _sorted_standard_cargo_specs;
155
157 static bool CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b)
158 {
159 static char a_name[64];
160 static char b_name[64];
161
162 GetString(a_name, a->name, lastof(a_name));
163 GetString(b_name, b->name, lastof(b_name));
164
165 int res = strnatcmp(a_name, b_name); // Sort by name (natural sorting).
166
167 /* If the names are equal, sort by cargo bitnum. */
168 return (res != 0) ? res < 0 : (a->bitnum < b->bitnum);
169 }
170
172 static bool CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * const &b)
173 {
174 int res = (b->classes & CC_PASSENGERS) - (a->classes & CC_PASSENGERS);
175 if (res == 0) {
176 res = (b->classes & CC_MAIL) - (a->classes & CC_MAIL);
177 if (res == 0) {
178 res = (a->classes & CC_SPECIAL) - (b->classes & CC_SPECIAL);
179 if (res == 0) {
180 return CargoSpecNameSorter(a, b);
181 }
182 }
183 }
184
185 return res < 0;
186 }
187
189 void InitializeSortedCargoSpecs()
190 {
191 _sorted_cargo_specs.clear();
192 /* Add each cargo spec to the list. */
193 for (const CargoSpec *cargo : CargoSpec::Iterate()) {
194 _sorted_cargo_specs.push_back(cargo);
195 }
196
197 /* Sort cargo specifications by cargo class and name. */
198 std::sort(_sorted_cargo_specs.begin(), _sorted_cargo_specs.end(), &CargoSpecClassSorter);
199
200 /* Count the number of standard cargos and fill the mask. */
201 _standard_cargo_mask = 0;
202 uint8 nb_standard_cargo = 0;
203 for (const auto &cargo : _sorted_cargo_specs) {
204 if (cargo->classes & CC_SPECIAL) break;
205 nb_standard_cargo++;
206 SetBit(_standard_cargo_mask, cargo->Index());
207 }
208
209 /* _sorted_standard_cargo_specs is a subset of _sorted_cargo_specs. */
210 _sorted_standard_cargo_specs = { _sorted_cargo_specs.data(), nb_standard_cargo };
211 }
212
213 uint64 CargoSpec::WeightOfNUnitsInTrain(uint32 n) const
214 {
215 if (this->is_freight) n *= _settings_game.vehicle.freight_trains;
216 return this->WeightOfNUnits(n);
217 }
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:34
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:153
_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:124
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:118
SetupCargoForClimate
void SetupCargoForClimate(LandscapeID l)
Set up the default cargo types for the given landscape type.
Definition: cargotype.cpp:40
CargoSpec::Iterate
static IterateWrapper Iterate(size_t from=0)
Returns an iterable ensemble of all valid CargoSpec.
Definition: cargotype.h:174
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:140
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:157
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:177
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:29
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:54
CargoSpec::sprite
SpriteID sprite
Icon to display this cargo type, may be 0xFFF (which means to resolve an action123 chain).
Definition: cargotype.h:77
safeguards.h
settings_type.h
sprites.h
CargoSpec::is_freight
bool is_freight
Cargo type is considered to be freight (affects train freight multiplier).
Definition: cargotype.h:67
stdafx.h
CargoSpecClassSorter
static bool CargoSpecClassSorter(const CargoSpec *const &a, const CargoSpec *const &b)
Sort cargo specifications by their cargo class.
Definition: cargotype.cpp:172
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:154
GetDefaultCargoID
CargoID GetDefaultCargoID(LandscapeID l, CargoType ct)
Get the cargo ID of a default cargo, if present.
Definition: cargotype.cpp:87
InitializeSortedCargoSpecs
void InitializeSortedCargoSpecs()
Initialize the list of sorted cargo specifications.
Definition: cargotype.cpp:189
CargoSpec::classes
uint16 classes
Classes of this cargo type.
Definition: cargotype.h:79
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:71
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:386
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:737
GameSettings::vehicle
VehicleSettings vehicle
options for vehicles
Definition: settings_type.h:595
LandscapeID
byte LandscapeID
Landscape type.
Definition: landscape_type.h:13
CT_INVALID
@ CT_INVALID
Invalid cargo type.
Definition: cargo_type.h:69
VehicleSettings::freight_trains
uint8 freight_trains
value to multiply the weight of cargo by
Definition: settings_type.h:500
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:402
GetCargoIDByLabel
CargoID GetCargoIDByLabel(CargoLabel cl)
Get the cargo ID by cargo label.
Definition: cargotype.cpp:108
newgrf_cargo.h
_default_cargo
static const CargoSpec _default_cargo[]
Cargo types available by default.
Definition: cargo_const.h:52