🌐 Galician Localization January 2025
[openttd-jgr.git] / src / cargotype.cpp
blob87f1eeb50528682dfebe6be861c0a576957954e6
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 */
8 /** @file cargotype.cpp Implementation of cargoes. */
10 #include "stdafx.h"
11 #include "cargotype.h"
12 #include "core/format.hpp"
13 #include "core/geometry_func.hpp"
14 #include "gfx_func.h"
15 #include "newgrf_cargo.h"
16 #include "string_func.h"
17 #include "strings_func.h"
18 #include "settings_type.h"
19 #include "3rdparty/cpp-btree/btree_map.h"
21 #include "table/sprites.h"
22 #include "table/strings.h"
23 #include "table/cargo_const.h"
25 #include <sstream>
27 #include "safeguards.h"
29 CargoSpec CargoSpec::array[NUM_CARGO];
30 std::array<std::vector<CargoID>, NUM_TPE> CargoSpec::town_production_cargoes{};
31 std::array<CargoTypes, NUM_TPE> CargoSpec::town_production_cargo_mask{};
33 /**
34 * Bitmask of cargo types available. This includes phony cargoes like regearing cargoes.
35 * Initialized during a call to #SetupCargoForClimate.
37 CargoTypes _cargo_mask;
39 /**
40 * Bitmask of real cargo types available. Phony cargoes like regearing cargoes are excluded.
42 CargoTypes _standard_cargo_mask;
44 /**
45 * List of default cargo labels, used when setting up cargo types for default vehicles.
46 * This is done by label so that a cargo label can be redefined in a different slot.
48 static std::vector<CargoLabel> _default_cargo_labels;
50 static btree::btree_map<CargoLabel, CargoID> _cargo_label_map; ///< Translation map from CargoLabel to Cargo ID.
51 CargoID _cargo_id_passengers = INVALID_CARGO;
52 CargoID _cargo_id_mail = INVALID_CARGO;
54 /**
55 * Default cargo translation for upto version 7 NewGRFs.
56 * This maps the original 12 cargo slots to their original label. If a climate dependent cargo is not present it will
57 * map to CT_INVALID. For default cargoes this ends up as a 1:1 mapping via climate slot -> label -> cargo ID.
59 static std::array<CargoLabel, 12> _climate_dependent_cargo_labels;
61 /**
62 * Default cargo translation for version 8+ NewGRFs.
63 * This maps the 32 "bitnum" cargo slots to their original label. If a bitnum is not present it will
64 * map to CT_INVALID.
66 static std::array<CargoLabel, 32> _climate_independent_cargo_labels;
68 /**
69 * Set up the default cargo types for the given landscape type.
70 * @param l Landscape
72 void SetupCargoForClimate(LandscapeID l)
74 assert(l < lengthof(_default_climate_cargo));
76 _cargo_mask = 0;
77 _default_cargo_labels.clear();
78 _climate_dependent_cargo_labels.fill(CT_INVALID);
79 _climate_independent_cargo_labels.fill(CT_INVALID);
81 /* Copy from default cargo by label or index. */
82 auto insert = std::begin(CargoSpec::array);
83 for (const auto &cl : _default_climate_cargo[l]) {
85 struct visitor {
86 const CargoSpec &operator()(const int &index)
88 /* Copy the default cargo by index. */
89 return _default_cargo[index];
91 const CargoSpec &operator()(const CargoLabel &label)
93 /* Search for label in default cargo types and copy if found. */
94 auto found = std::ranges::find(_default_cargo, label, &CargoSpec::label);
95 if (found != std::end(_default_cargo)) return *found;
97 /* Index or label is invalid, this should not happen. */
98 NOT_REACHED();
102 *insert = std::visit(visitor{}, cl);
104 if (insert->IsValid()) {
105 SetBit(_cargo_mask, insert->Index());
106 _default_cargo_labels.push_back(insert->label);
107 _climate_dependent_cargo_labels[insert->Index()] = insert->label;
108 _climate_independent_cargo_labels[insert->bitnum] = insert->label;
110 ++insert;
113 /* Reset and disable remaining cargo types. */
114 std::fill(insert, std::end(CargoSpec::array), CargoSpec{});
116 BuildCargoLabelMap();
120 * Get default climate-dependent cargo translation table for a NewGRF, used if the NewGRF does not provide its own.
121 * @return Default translation table for GRF version.
123 std::span<const CargoLabel> GetClimateDependentCargoTranslationTable()
125 return _climate_dependent_cargo_labels;
129 * Get default climate-independent cargo translation table for a NewGRF, used if the NewGRF does not provide its own.
130 * @return Default translation table for GRF version.
132 std::span<const CargoLabel> GetClimateIndependentCargoTranslationTable()
134 return _climate_independent_cargo_labels;
138 * Build cargo label map.
139 * This is called multiple times during NewGRF initialization as cargos are defined, so that TranslateRefitMask() and
140 * GetCargoTranslation(), also used during initialization, get the correct information.
142 void BuildCargoLabelMap()
144 _cargo_label_map.clear();
145 for (const CargoSpec &cs : CargoSpec::array) {
146 /* During initialization, CargoSpec can be marked valid before the label has been set. */
147 if (!cs.IsValid() || cs.label == CargoLabel{} || cs.label == CT_INVALID) continue;
148 /* Label already exists, don't add again. */
149 if (_cargo_label_map.count(cs.label) != 0) continue;
151 _cargo_label_map.insert(std::make_pair(cs.label, cs.Index()));
153 _cargo_id_passengers = GetCargoIDByLabelUsingMap(CT_PASSENGERS);
154 _cargo_id_mail = GetCargoIDByLabelUsingMap(CT_MAIL);
158 * Test if a cargo is a default cargo type.
159 * @param cid Cargo ID.
160 * @returns true iff the cargo type is a default cargo type.
162 bool IsDefaultCargo(CargoID cid)
164 auto cs = CargoSpec::Get(cid);
165 if (!cs->IsValid()) return false;
167 CargoLabel label = cs->label;
168 return std::any_of(std::begin(_default_cargo_labels), std::end(_default_cargo_labels), [&label](const CargoLabel &cl) { return cl == label; });
172 * Get dimensions of largest cargo icon.
173 * @return Dimensions of largest cargo icon.
175 Dimension GetLargestCargoIconSize()
177 Dimension size = {0, 0};
178 for (const CargoSpec *cs : _sorted_cargo_specs) {
179 size = maxdim(size, GetSpriteSize(cs->GetCargoIcon()));
181 return size;
184 CargoID GetCargoIDByLabelUsingMap(CargoLabel label)
186 auto found = _cargo_label_map.find(label);
187 if (found != _cargo_label_map.end()) return found->second;
188 return INVALID_CARGO;
192 * Get sprite for showing cargo of this type.
193 * @return Sprite number to use.
195 SpriteID CargoSpec::GetCargoIcon() const
197 SpriteID sprite = this->sprite;
198 if (sprite == 0xFFFF) {
199 /* A value of 0xFFFF indicates we should draw a custom icon */
200 sprite = GetCustomCargoSprite(this);
203 if (sprite == 0) sprite = SPR_CARGO_GOODS;
205 return sprite;
208 std::array<uint8_t, NUM_CARGO> _sorted_cargo_types; ///< Sort order of cargoes by cargo ID.
209 std::vector<const CargoSpec *> _sorted_cargo_specs; ///< Cargo specifications sorted alphabetically by name.
210 std::span<const CargoSpec *> _sorted_standard_cargo_specs; ///< Standard cargo specifications sorted alphabetically by name.
212 /** Sort cargo specifications by their name. */
213 static bool CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b)
215 format_buffer a_name;
216 format_buffer b_name;
217 AppendStringInPlace(a_name, a->name);
218 AppendStringInPlace(b_name, b->name);
220 int res = StrNaturalCompare(a_name, b_name); // Sort by name (natural sorting).
222 /* If the names are equal, sort by cargo bitnum. */
223 return (res != 0) ? res < 0 : (a->bitnum < b->bitnum);
226 /** Sort cargo specifications by their cargo class. */
227 static bool CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * const &b)
229 int res = (b->classes & CC_PASSENGERS) - (a->classes & CC_PASSENGERS);
230 if (res == 0) {
231 res = (b->classes & CC_MAIL) - (a->classes & CC_MAIL);
232 if (res == 0) {
233 res = (a->classes & CC_SPECIAL) - (b->classes & CC_SPECIAL);
234 if (res == 0) {
235 return CargoSpecNameSorter(a, b);
240 return res < 0;
243 /** Initialize the list of sorted cargo specifications. */
244 void InitializeSortedCargoSpecs()
246 for (auto &tpc : CargoSpec::town_production_cargoes) tpc.clear();
247 for (auto &tpc : CargoSpec::town_production_cargo_mask) tpc = 0;
248 _sorted_cargo_specs.clear();
249 /* Add each cargo spec to the list, and determine the largest cargo icon size. */
250 for (const CargoSpec *cargo : CargoSpec::Iterate()) {
251 _sorted_cargo_specs.push_back(cargo);
254 /* Sort cargo specifications by cargo class and name. */
255 std::sort(_sorted_cargo_specs.begin(), _sorted_cargo_specs.end(), &CargoSpecClassSorter);
257 /* Populate */
258 for (auto it = std::begin(_sorted_cargo_specs); it != std::end(_sorted_cargo_specs); ++it) {
259 _sorted_cargo_types[(*it)->Index()] = static_cast<uint8_t>(it - std::begin(_sorted_cargo_specs));
262 /* Count the number of standard cargos and fill the mask. */
263 _standard_cargo_mask = 0;
264 uint8_t nb_standard_cargo = 0;
265 for (const auto &cargo : _sorted_cargo_specs) {
266 assert(cargo->town_production_effect != INVALID_TPE);
267 CargoSpec::town_production_cargoes[cargo->town_production_effect].push_back(cargo->Index());
268 SetBit(CargoSpec::town_production_cargo_mask[cargo->town_production_effect], cargo->Index());
269 if (cargo->classes & CC_SPECIAL) break;
270 nb_standard_cargo++;
271 SetBit(_standard_cargo_mask, cargo->Index());
274 /* _sorted_standard_cargo_specs is a subset of _sorted_cargo_specs. */
275 _sorted_standard_cargo_specs = { _sorted_cargo_specs.data(), nb_standard_cargo };
278 uint64_t CargoSpec::WeightOfNUnitsInTrain(uint32_t n) const
280 if (this->is_freight) n *= _settings_game.vehicle.freight_trains;
281 return this->WeightOfNUnits(n);
285 * Build comma-separated cargo acceptance string.
286 * @param acceptance CargoArray filled with accepted cargo.
287 * @param label Label to prefix cargo acceptance list.
288 * @return String of accepted cargo, or nullopt if no cargo is accepted.
290 std::optional<std::string> BuildCargoAcceptanceString(const CargoArray &acceptance, StringID label)
292 std::string_view list_separator = GetListSeparator();
294 /* Cargo acceptance is displayed in a extra multiline */
295 format_buffer line;
296 AppendStringInPlace(line, label);
298 bool found = false;
299 for (const CargoSpec *cs : _sorted_cargo_specs) {
300 CargoID cid = cs->Index();
301 if (acceptance[cid] > 0) {
302 /* Add a comma between each item. */
303 if (found) line.append(list_separator);
304 found = true;
306 /* If the accepted value is less than 8, show it in 1/8:ths */
307 if (acceptance[cid] < 8) {
308 SetDParam(0, acceptance[cid]);
309 SetDParam(1, cs->name);
310 AppendStringInPlace(line, STR_LAND_AREA_INFORMATION_CARGO_EIGHTS);
311 } else {
312 AppendStringInPlace(line, cs->name);
317 if (found) return line.to_string();
319 return std::nullopt;