OpenTTD Source  20240917-master-g9ab0a47812
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 
25 std::array<std::vector<const CargoSpec *>, NUM_TPE> CargoSpec::town_production_cargoes{};
26 
31 CargoTypes _cargo_mask;
32 
37 
42 static std::vector<CargoLabel> _default_cargo_labels;
43 
49 {
50  assert(l < lengthof(_default_climate_cargo));
51 
52  _cargo_mask = 0;
53  _default_cargo_labels.clear();
54 
55  /* Copy from default cargo by label or index. */
56  auto insert = std::begin(CargoSpec::array);
57  for (const auto &cl : _default_climate_cargo[l]) {
58 
59  /* Check if value is an index into the cargo table */
60  if (std::holds_alternative<int>(cl)) {
61  /* Copy the default cargo by index. */
62  *insert = _default_cargo[std::get<int>(cl)];
63  } else {
64  /* Search for label in default cargo types and copy if found. */
65  CargoLabel label = std::get<CargoLabel>(cl);
66  auto found = std::find_if(std::begin(_default_cargo), std::end(_default_cargo), [&label](const CargoSpec &cs) { return cs.label == label; });
67  if (found != std::end(_default_cargo)) {
68  *insert = *found;
69  } else {
70  /* Index or label is invalid, this should not happen. */
71  NOT_REACHED();
72  }
73  }
74 
75  if (insert->IsValid()) {
76  SetBit(_cargo_mask, insert->Index());
77  _default_cargo_labels.push_back(insert->label);
78  }
79  ++insert;
80  }
81 
82  /* Reset and disable remaining cargo types. */
83  std::fill(insert, std::end(CargoSpec::array), CargoSpec{});
84 
86 }
87 
94 {
95  CargoSpec::label_map.clear();
96  for (const CargoSpec &cs : CargoSpec::array) {
97  /* During initialization, CargoSpec can be marked valid before the label has been set. */
98  if (!cs.IsValid() || cs.label == CargoLabel{0} || cs.label == CT_INVALID) continue;
99  /* Label already exists, don't addd again. */
100  if (CargoSpec::label_map.count(cs.label) != 0) continue;
101 
102  CargoSpec::label_map.emplace(cs.label, cs.Index());
103  }
104 }
105 
112 {
113  auto cs = CargoSpec::Get(cid);
114  if (!cs->IsValid()) return false;
115 
116  CargoLabel label = cs->label;
117  return std::any_of(std::begin(_default_cargo_labels), std::end(_default_cargo_labels), [&label](const CargoLabel &cl) { return cl == label; });
118 }
119 
125 {
126  Dimension size = {0, 0};
127  for (const CargoSpec *cs : _sorted_cargo_specs) {
128  size = maxdim(size, GetSpriteSize(cs->GetCargoIcon()));
129  }
130  return size;
131 }
132 
139 {
140  if (bitnum == INVALID_CARGO_BITNUM) return INVALID_CARGO;
141 
142  for (const CargoSpec *cs : CargoSpec::Iterate()) {
143  if (cs->bitnum == bitnum) return cs->Index();
144  }
145 
146  /* No matching label was found, so it is invalid */
147  return INVALID_CARGO;
148 }
149 
155 {
156  SpriteID sprite = this->sprite;
157  if (sprite == 0xFFFF) {
158  /* A value of 0xFFFF indicates we should draw a custom icon */
160  }
161 
162  if (sprite == 0) sprite = SPR_CARGO_GOODS;
163 
164  return sprite;
165 }
166 
167 std::array<uint8_t, NUM_CARGO> _sorted_cargo_types;
168 std::vector<const CargoSpec *> _sorted_cargo_specs;
169 std::span<const CargoSpec *> _sorted_standard_cargo_specs;
170 
172 static bool CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b)
173 {
174  std::string a_name = GetString(a->name);
175  std::string b_name = GetString(b->name);
176 
177  int res = StrNaturalCompare(a_name, b_name); // Sort by name (natural sorting).
178 
179  /* If the names are equal, sort by cargo bitnum. */
180  return (res != 0) ? res < 0 : (a->bitnum < b->bitnum);
181 }
182 
184 static bool CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * const &b)
185 {
186  int res = (b->classes & CC_PASSENGERS) - (a->classes & CC_PASSENGERS);
187  if (res == 0) {
188  res = (b->classes & CC_MAIL) - (a->classes & CC_MAIL);
189  if (res == 0) {
190  res = (a->classes & CC_SPECIAL) - (b->classes & CC_SPECIAL);
191  if (res == 0) {
192  return CargoSpecNameSorter(a, b);
193  }
194  }
195  }
196 
197  return res < 0;
198 }
199 
202 {
203  for (auto &tpc : CargoSpec::town_production_cargoes) tpc.clear();
204  _sorted_cargo_specs.clear();
205  /* Add each cargo spec to the list, and determine the largest cargo icon size. */
206  for (const CargoSpec *cargo : CargoSpec::Iterate()) {
207  _sorted_cargo_specs.push_back(cargo);
208  }
209 
210  /* Sort cargo specifications by cargo class and name. */
212 
213  /* Populate */
214  for (auto it = std::begin(_sorted_cargo_specs); it != std::end(_sorted_cargo_specs); ++it) {
215  _sorted_cargo_types[(*it)->Index()] = static_cast<uint8_t>(it - std::begin(_sorted_cargo_specs));
216  }
217 
218  /* Count the number of standard cargos and fill the mask. */
220  uint8_t nb_standard_cargo = 0;
221  for (const auto &cargo : _sorted_cargo_specs) {
222  assert(cargo->town_production_effect != INVALID_TPE);
223  CargoSpec::town_production_cargoes[cargo->town_production_effect].push_back(cargo);
224  if (cargo->classes & CC_SPECIAL) break;
225  nb_standard_cargo++;
226  SetBit(_standard_cargo_mask, cargo->Index());
227  }
228 
229  /* _sorted_standard_cargo_specs is a subset of _sorted_cargo_specs. */
230  _sorted_standard_cargo_specs = { _sorted_cargo_specs.data(), nb_standard_cargo };
231 }
232 
233 uint64_t CargoSpec::WeightOfNUnitsInTrain(uint32_t n) const
234 {
236  return this->WeightOfNUnits(n);
237 }
cargo_const.h
CC_MAIL
@ CC_MAIL
Mail.
Definition: cargotype.h:51
_standard_cargo_mask
CargoTypes _standard_cargo_mask
Bitmask of real cargo types available.
Definition: cargotype.cpp:36
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
CC_PASSENGERS
@ CC_PASSENGERS
Passengers.
Definition: cargotype.h:50
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:167
_sorted_cargo_specs
std::vector< const CargoSpec * > _sorted_cargo_specs
Cargo specifications sorted alphabetically by name.
Definition: cargotype.cpp:168
BuildCargoLabelMap
void BuildCargoLabelMap()
Build cargo label map.
Definition: cargotype.cpp:93
GetCargoIDByBitnum
CargoID GetCargoIDByBitnum(uint8_t bitnum)
Find the CargoID of a 'bitnum' value.
Definition: cargotype.cpp:138
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:134
maxdim
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Definition: geometry_func.cpp:22
CargoSpec::label_map
static std::map< CargoLabel, CargoID > label_map
Translation map from CargoLabel to Cargo ID.
Definition: cargotype.h:197
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:566
IsDefaultCargo
bool IsDefaultCargo(CargoID cid)
Test if a cargo is a default cargo type.
Definition: cargotype.cpp:111
SetupCargoForClimate
void SetupCargoForClimate(LandscapeID l)
Set up the default cargo types for the given landscape type.
Definition: cargotype.cpp:48
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
StrongType::Typedef< uint32_t, struct CargoLabelTag, StrongType::Compare >
CargoSpec::GetCargoIcon
SpriteID GetCargoIcon() const
Get sprite for showing cargo of this type.
Definition: cargotype.cpp:154
CargoSpecNameSorter
static bool CargoSpecNameSorter(const CargoSpec *const &a, const CargoSpec *const &b)
Sort cargo specifications by their name.
Definition: cargotype.cpp:172
CargoSpec::array
static CargoSpec array[NUM_CARGO]
Array holding all CargoSpecs.
Definition: cargotype.h:196
CC_SPECIAL
@ CC_SPECIAL
Special bit used for livery refit tricks instead of normal cargoes.
Definition: cargotype.h:60
_sorted_standard_cargo_specs
std::span< const CargoSpec * > _sorted_standard_cargo_specs
Standard cargo specifications sorted alphabetically by name.
Definition: cargotype.cpp:169
CargoSpec::classes
CargoClasses classes
Classes of this cargo type.
Definition: cargotype.h:78
_default_climate_cargo
static const std::variant< CargoLabel, int > _default_climate_cargo[NUM_LANDSCAPE][NUM_ORIGINAL_CARGO]
Table of cargo types available in each climate, by default.
Definition: cargo_const.h:99
VehicleSettings::freight_trains
uint8_t freight_trains
value to multiply the weight of cargo by
Definition: settings_type.h:504
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
_default_cargo_labels
static std::vector< CargoLabel > _default_cargo_labels
List of default cargo labels, used when setting up cargo types for default vehicles.
Definition: cargotype.cpp:42
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:57
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
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition: stdafx.h:280
settings_type.h
sprites.h
INVALID_TPE
@ INVALID_TPE
Invalid town production effect.
Definition: cargotype.h:44
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
LandscapeID
uint8_t LandscapeID
Landscape type.
Definition: landscape_type.h:13
stdafx.h
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:18
GetSpriteSize
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition: gfx.cpp:922
CargoSpecClassSorter
static bool CargoSpecClassSorter(const CargoSpec *const &a, const CargoSpec *const &b)
Sort cargo specifications by their cargo class.
Definition: cargotype.cpp:184
string_func.h
INVALID_CARGO_BITNUM
static const uint8_t INVALID_CARGO_BITNUM
Constant representing invalid cargo.
Definition: cargotype.h:66
CT_INVALID
static constexpr CargoLabel CT_INVALID
Invalid cargo type.
Definition: cargo_type.h:71
GetCustomCargoSprite
SpriteID GetCustomCargoSprite(const CargoSpec *cs)
Get the custom sprite for the given cargo type.
Definition: newgrf_cargo.cpp:54
strings_func.h
geometry_func.hpp
InitializeSortedCargoSpecs
void InitializeSortedCargoSpecs()
Initialize the list of sorted cargo specifications.
Definition: cargotype.cpp:201
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:319
cargotype.h
CargoID
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
CargoSpec::name
StringID name
Name of this type of cargo.
Definition: cargotype.h:88
GameSettings::vehicle
VehicleSettings vehicle
options for vehicles
Definition: settings_type.h:602
NUM_CARGO
static const CargoID NUM_CARGO
Maximum number of cargo types in a game.
Definition: cargo_type.h:74
newgrf_cargo.h
GetLargestCargoIconSize
Dimension GetLargestCargoIconSize()
Get dimensions of largest cargo icon.
Definition: cargotype.cpp:124
_default_cargo
static const CargoSpec _default_cargo[]
Cargo types available by default.
Definition: cargo_const.h:52