OpenTTD Source: src/cargotype.cpp Source File

OpenTTD Source 14.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 "core/geometry_func.hpp"
13 #include "newgrf_cargo.h"
14 #include "string_func.h"
15 #include "strings_func.h"
16 #include "settings_type.h"
17
18 #include "table/sprites.h"
19 #include "table/strings.h"
20 #include "table/cargo_const.h"
21
22 #include "safeguards.h"
23
24 CargoSpec CargoSpec::array[NUM_CARGO];
25 std::array<std::vector<const CargoSpec *>, NUM_TPE> CargoSpec::town_production_cargoes{};
26
31 CargoTypes _cargo_mask;
32
36 CargoTypes _standard_cargo_mask;
37
42 void SetupCargoForClimate(LandscapeID l)
43 {
44 assert(l < lengthof(_default_climate_cargo));
45
46 _cargo_mask = 0;
47
48 /* Copy from default cargo by label or index. */
49 auto insert = std::begin(CargoSpec::array);
50 for (const CargoLabel &cl : _default_climate_cargo[l]) {
51
52 /* Check if value is an index into the cargo table */
53 if (cl < lengthof(_default_cargo)) {
54 /* Copy the default cargo by index. */
55 *insert = _default_cargo[cl];
56 } else {
57 /* Search for label in default cargo types and copy if found. */
58 auto found = std::find_if(std::begin(_default_cargo), std::end(_default_cargo), [&cl](const CargoSpec &cs) { return cs.label == cl; });
59 if (found != std::end(_default_cargo)) {
60 *insert = *found;
61 } else {
62 /* Index or label is invalid, this should not happen. */
63 NOT_REACHED();
64 }
65 }
66
67 if (insert->IsValid()) SetBit(_cargo_mask, insert->Index());
68 ++insert;
69 }
70
71 /* Reset and disable remaining cargo types. */
72 std::fill(insert, std::end(CargoSpec::array), CargoSpec{});
73 }
74
79 Dimension GetLargestCargoIconSize()
80 {
81 Dimension size = {0, 0};
82 for (const CargoSpec *cs : _sorted_cargo_specs) {
83 size = maxdim(size, GetSpriteSize(cs->GetCargoIcon()));
84 }
85 return size;
86 }
87
94 CargoID GetDefaultCargoID(LandscapeID l, CargoType ct)
95 {
96 assert(l < lengthof(_default_climate_cargo));
97
98 if (!IsValidCargoType(ct)) return INVALID_CARGO;
99
100 assert(ct < lengthof(_default_climate_cargo[0]));
101 CargoLabel cl = _default_climate_cargo[l][ct];
102 /* Bzzt: check if cl is just an index into the cargo table */
103 if (cl < lengthof(_default_cargo)) {
104 cl = _default_cargo[cl].label;
105 }
106
107 return GetCargoIDByLabel(cl);
108 }
109
115 CargoID GetCargoIDByLabel(CargoLabel cl)
116 {
117 for (const CargoSpec *cs : CargoSpec::Iterate()) {
118 if (cs->label == cl) return cs->Index();
119 }
120
121 /* No matching label was found, so it is invalid */
122 return INVALID_CARGO;
123 }
124
125
131 CargoID GetCargoIDByBitnum(uint8_t bitnum)
132 {
133 if (bitnum == INVALID_CARGO_BITNUM) return INVALID_CARGO;
134
135 for (const CargoSpec *cs : CargoSpec::Iterate()) {
136 if (cs->bitnum == bitnum) return cs->Index();
137 }
138
139 /* No matching label was found, so it is invalid */
140 return INVALID_CARGO;
141 }
142
147 SpriteID CargoSpec::GetCargoIcon() const
148 {
149 SpriteID sprite = this->sprite;
150 if (sprite == 0xFFFF) {
151 /* A value of 0xFFFF indicates we should draw a custom icon */
152 sprite = GetCustomCargoSprite(this);
153 }
154
155 if (sprite == 0) sprite = SPR_CARGO_GOODS;
156
157 return sprite;
158 }
159
160 std::array<uint8_t, NUM_CARGO> _sorted_cargo_types;
161 std::vector<const CargoSpec *> _sorted_cargo_specs;
162 std::span<const CargoSpec *> _sorted_standard_cargo_specs;
163
165 static bool CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b)
166 {
167 std::string a_name = GetString(a->name);
168 std::string b_name = GetString(b->name);
169
170 int res = StrNaturalCompare(a_name, b_name); // Sort by name (natural sorting).
171
172 /* If the names are equal, sort by cargo bitnum. */
173 return (res != 0) ? res < 0 : (a->bitnum < b->bitnum);
174 }
175
177 static bool CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * const &b)
178 {
179 int res = (b->classes & CC_PASSENGERS) - (a->classes & CC_PASSENGERS);
180 if (res == 0) {
181 res = (b->classes & CC_MAIL) - (a->classes & CC_MAIL);
182 if (res == 0) {
183 res = (a->classes & CC_SPECIAL) - (b->classes & CC_SPECIAL);
184 if (res == 0) {
185 return CargoSpecNameSorter(a, b);
186 }
187 }
188 }
189
190 return res < 0;
191 }
192
194 void InitializeSortedCargoSpecs()
195 {
196 for (auto &tpc : CargoSpec::town_production_cargoes) tpc.clear();
197 _sorted_cargo_specs.clear();
198 /* Add each cargo spec to the list, and determine the largest cargo icon size. */
199 for (const CargoSpec *cargo : CargoSpec::Iterate()) {
200 _sorted_cargo_specs.push_back(cargo);
201 }
202
203 /* Sort cargo specifications by cargo class and name. */
204 std::sort(_sorted_cargo_specs.begin(), _sorted_cargo_specs.end(), &CargoSpecClassSorter);
205
206 /* Populate */
207 for (auto it = std::begin(_sorted_cargo_specs); it != std::end(_sorted_cargo_specs); ++it) {
208 _sorted_cargo_types[(*it)->Index()] = static_cast<uint8_t>(it - std::begin(_sorted_cargo_specs));
209 }
210
211 /* Count the number of standard cargos and fill the mask. */
212 _standard_cargo_mask = 0;
213 uint8_t nb_standard_cargo = 0;
214 for (const auto &cargo : _sorted_cargo_specs) {
215 assert(cargo->town_production_effect != INVALID_TPE);
216 CargoSpec::town_production_cargoes[cargo->town_production_effect].push_back(cargo);
217 if (cargo->classes & CC_SPECIAL) break;
218 nb_standard_cargo++;
219 SetBit(_standard_cargo_mask, cargo->Index());
220 }
221
222 /* _sorted_standard_cargo_specs is a subset of _sorted_cargo_specs. */
223 _sorted_standard_cargo_specs = { _sorted_cargo_specs.data(), nb_standard_cargo };
224 }
225
226 uint64_t CargoSpec::WeightOfNUnitsInTrain(uint32_t n) const
227 {
228 if (this->is_freight) n *= _settings_game.vehicle.freight_trains;
229 return this->WeightOfNUnits(n);
230 }
cargo_const.h
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:36
IsValidCargoType
bool IsValidCargoType(CargoType t)
Test whether cargo type is not CT_INVALID.
Definition: cargo_type.h:88
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
CargoSpec::label
CargoLabel label
Unique label of the cargo type.
Definition: cargotype.h:72
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:30
_sorted_cargo_types
std::array< uint8_t, NUM_CARGO > _sorted_cargo_types
Sort order of cargoes by cargo ID.
Definition: cargotype.cpp:160
_sorted_cargo_specs
std::vector< const CargoSpec * > _sorted_cargo_specs
Cargo specifications sorted alphabetically by name.
Definition: cargotype.cpp:161
_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_t bitnum)
Find the CargoID of a 'bitnum' value.
Definition: cargotype.cpp:131
maxdim
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Definition: geometry_func.cpp:22
StrNaturalCompare
int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition: string.cpp:585
SetupCargoForClimate
void SetupCargoForClimate(LandscapeID l)
Set up the default cargo types for the given landscape type.
Definition: cargotype.cpp:42
CargoSpec::Iterate
static IterateWrapper Iterate(size_t from=0)
Returns an iterable ensemble of all valid CargoSpec.
Definition: cargotype.h:190
CargoSpec
Specification of a cargo type.
Definition: cargotype.h:71
CargoSpec::GetCargoIcon
SpriteID GetCargoIcon() const
Get sprite for showing cargo of this type.
Definition: cargotype.cpp:147
CC_PASSENGERS
@ CC_PASSENGERS
Passengers.
Definition: cargotype.h:53
CargoSpecNameSorter
static bool CargoSpecNameSorter(const CargoSpec *const &a, const CargoSpec *const &b)
Sort cargo specifications by their name.
Definition: cargotype.cpp:165
CargoSpec::array
static CargoSpec array[NUM_CARGO]
Array holding all CargoSpecs.
Definition: cargotype.h:196
_sorted_standard_cargo_specs
std::span< const CargoSpec * > _sorted_standard_cargo_specs
Standard cargo specifications sorted alphabetically by name.
Definition: cargotype.cpp:162
VehicleSettings::freight_trains
uint8_t freight_trains
value to multiply the weight of cargo by
Definition: settings_type.h:529
CC_SPECIAL
@ CC_SPECIAL
Special bit used for livery refit tricks instead of normal cargoes.
Definition: cargotype.h:63
INVALID_CARGO_BITNUM
static const byte INVALID_CARGO_BITNUM
Constant representing invalid cargo.
Definition: cargotype.h:66
CargoSpec::town_production_cargoes
static std::array< std::vector< const CargoSpec * >, NUM_TPE > town_production_cargoes
List of cargo specs for each Town Product Effect.
Definition: cargotype.h:193
_cargo_mask
CargoTypes _cargo_mask
Bitmask of cargo types available.
Definition: cargotype.cpp:31
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:55
CargoSpec::sprite
SpriteID sprite
Icon to display this cargo type, may be 0xFFF (which means to resolve an action123 chain).
Definition: cargotype.h:94
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:82
CargoSpec::bitnum
uint8_t bitnum
Cargo bit number, is INVALID_CARGO_BITNUM for a non-used spec.
Definition: cargotype.h:73
stdafx.h
CargoLabel
uint32_t CargoLabel
Globally unique label of a cargo type.
Definition: cargotype.h:21
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
GetSpriteSize
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition: gfx.cpp:941
CargoSpecClassSorter
static bool CargoSpecClassSorter(const CargoSpec *const &a, const CargoSpec *const &b)
Sort cargo specifications by their cargo class.
Definition: cargotype.cpp:177
string_func.h
GetCustomCargoSprite
SpriteID GetCustomCargoSprite(const CargoSpec *cs)
Get the custom sprite for the given cargo type.
Definition: newgrf_cargo.cpp:54
INVALID_TPE
@ INVALID_TPE
Invalid town production effect.
Definition: cargotype.h:47
strings_func.h
geometry_func.hpp
GetDefaultCargoID
CargoID GetDefaultCargoID(LandscapeID l, CargoType ct)
Get the cargo ID of a default cargo, if present.
Definition: cargotype.cpp:94
InitializeSortedCargoSpecs
void InitializeSortedCargoSpecs()
Initialize the list of sorted cargo specifications.
Definition: cargotype.cpp:194
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition: strings.cpp:327
cargotype.h
CargoSpec::name
StringID name
Name of this type of cargo.
Definition: cargotype.h:88
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:300
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
GameSettings::vehicle
VehicleSettings vehicle
options for vehicles
Definition: settings_type.h:627
LandscapeID
byte LandscapeID
Landscape type.
Definition: landscape_type.h:13
NUM_CARGO
static const CargoID NUM_CARGO
Maximum number of cargo types in a game.
Definition: cargo_type.h:68
CC_MAIL
@ CC_MAIL
Mail.
Definition: cargotype.h:54
GetCargoIDByLabel
CargoID GetCargoIDByLabel(CargoLabel cl)
Get the cargo ID by cargo label.
Definition: cargotype.cpp:115
newgrf_cargo.h
GetLargestCargoIconSize
Dimension GetLargestCargoIconSize()
Get dimensions of largest cargo icon.
Definition: cargotype.cpp:79
_default_cargo
static const CargoSpec _default_cargo[]
Cargo types available by default.
Definition: cargo_const.h:52
CargoSpec::classes
uint16_t classes
Classes of this cargo type.
Definition: cargotype.h:78