Update: Translations from eints
[openttd-github.git] / src / cargotype.cpp
blobf91a0006cb0dd57c7c66074025a7582d03222023
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/geometry_func.hpp"
13 #include "newgrf_cargo.h"
14 #include "string_func.h"
15 #include "strings_func.h"
16 #include "settings_type.h"
18 #include "table/sprites.h"
19 #include "table/strings.h"
20 #include "table/cargo_const.h"
22 #include "safeguards.h"
24 CargoSpec CargoSpec::array[NUM_CARGO];
25 std::array<std::vector<const CargoSpec *>, NUM_TPE> CargoSpec::town_production_cargoes{};
27 /**
28 * Bitmask of cargo types available. This includes phony cargoes like regearing cargoes.
29 * Initialized during a call to #SetupCargoForClimate.
31 CargoTypes _cargo_mask;
33 /**
34 * Bitmask of real cargo types available. Phony cargoes like regearing cargoes are excluded.
36 CargoTypes _standard_cargo_mask;
38 /**
39 * List of default cargo labels, used when setting up cargo types for default vehicles.
40 * This is done by label so that a cargo label can be redefined in a different slot.
42 static std::vector<CargoLabel> _default_cargo_labels;
44 /**
45 * Default cargo translation for upto version 7 NewGRFs.
46 * This maps the original 12 cargo slots to their original label. If a climate dependent cargo is not present it will
47 * map to CT_INVALID. For default cargoes this ends up as a 1:1 mapping via climate slot -> label -> cargo ID.
49 static std::array<CargoLabel, 12> _v7_cargo_labels;
51 /**
52 * Default cargo translation for version 8+ NewGRFs.
53 * This maps the 32 "bitnum" cargo slots to their original label. If a bitnum is not present it will
54 * map to CT_INVALID.
56 static std::array<CargoLabel, 32> _v8_cargo_labels;
58 /**
59 * Set up the default cargo types for the given landscape type.
60 * @param l Landscape
62 void SetupCargoForClimate(LandscapeID l)
64 assert(l < lengthof(_default_climate_cargo));
66 _cargo_mask = 0;
67 _default_cargo_labels.clear();
68 _v7_cargo_labels.fill(CT_INVALID);
69 _v8_cargo_labels.fill(CT_INVALID);
71 /* Copy from default cargo by label or index. */
72 auto insert = std::begin(CargoSpec::array);
73 for (const auto &cl : _default_climate_cargo[l]) {
75 /* Check if value is an index into the cargo table */
76 if (std::holds_alternative<int>(cl)) {
77 /* Copy the default cargo by index. */
78 *insert = _default_cargo[std::get<int>(cl)];
79 } else {
80 /* Search for label in default cargo types and copy if found. */
81 CargoLabel label = std::get<CargoLabel>(cl);
82 auto found = std::find_if(std::begin(_default_cargo), std::end(_default_cargo), [&label](const CargoSpec &cs) { return cs.label == label; });
83 if (found != std::end(_default_cargo)) {
84 *insert = *found;
85 } else {
86 /* Index or label is invalid, this should not happen. */
87 NOT_REACHED();
91 if (insert->IsValid()) {
92 SetBit(_cargo_mask, insert->Index());
93 _default_cargo_labels.push_back(insert->label);
94 _v7_cargo_labels[insert->Index()] = insert->label;
95 _v8_cargo_labels[insert->bitnum] = insert->label;
97 ++insert;
100 /* Reset and disable remaining cargo types. */
101 std::fill(insert, std::end(CargoSpec::array), CargoSpec{});
103 BuildCargoLabelMap();
107 * Get default cargo translation table for a NewGRF, used if the NewGRF does not provide its own.
108 * @param grf_version GRF version of translation table.
109 * @return Default translation table for GRF version.
111 std::span<const CargoLabel> GetDefaultCargoTranslationTable(uint8_t grf_version)
113 if (grf_version < 8) return _v7_cargo_labels;
114 return _v8_cargo_labels;
118 * Build cargo label map.
119 * This is called multiple times during NewGRF initialization as cargos are defined, so that TranslateRefitMask() and
120 * GetCargoTranslation(), also used during initialization, get the correct information.
122 void BuildCargoLabelMap()
124 CargoSpec::label_map.clear();
125 for (const CargoSpec &cs : CargoSpec::array) {
126 /* During initialization, CargoSpec can be marked valid before the label has been set. */
127 if (!cs.IsValid() || cs.label == CargoLabel{0} || cs.label == CT_INVALID) continue;
128 /* Label already exists, don't addd again. */
129 if (CargoSpec::label_map.count(cs.label) != 0) continue;
131 CargoSpec::label_map.emplace(cs.label, cs.Index());
136 * Test if a cargo is a default cargo type.
137 * @param cid Cargo ID.
138 * @returns true iff the cargo type is a default cargo type.
140 bool IsDefaultCargo(CargoID cid)
142 auto cs = CargoSpec::Get(cid);
143 if (!cs->IsValid()) return false;
145 CargoLabel label = cs->label;
146 return std::any_of(std::begin(_default_cargo_labels), std::end(_default_cargo_labels), [&label](const CargoLabel &cl) { return cl == label; });
150 * Get dimensions of largest cargo icon.
151 * @return Dimensions of largest cargo icon.
153 Dimension GetLargestCargoIconSize()
155 Dimension size = {0, 0};
156 for (const CargoSpec *cs : _sorted_cargo_specs) {
157 size = maxdim(size, GetSpriteSize(cs->GetCargoIcon()));
159 return size;
163 * Get sprite for showing cargo of this type.
164 * @return Sprite number to use.
166 SpriteID CargoSpec::GetCargoIcon() const
168 SpriteID sprite = this->sprite;
169 if (sprite == 0xFFFF) {
170 /* A value of 0xFFFF indicates we should draw a custom icon */
171 sprite = GetCustomCargoSprite(this);
174 if (sprite == 0) sprite = SPR_CARGO_GOODS;
176 return sprite;
179 std::array<uint8_t, NUM_CARGO> _sorted_cargo_types; ///< Sort order of cargoes by cargo ID.
180 std::vector<const CargoSpec *> _sorted_cargo_specs; ///< Cargo specifications sorted alphabetically by name.
181 std::span<const CargoSpec *> _sorted_standard_cargo_specs; ///< Standard cargo specifications sorted alphabetically by name.
183 /** Sort cargo specifications by their name. */
184 static bool CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b)
186 std::string a_name = GetString(a->name);
187 std::string b_name = GetString(b->name);
189 int res = StrNaturalCompare(a_name, b_name); // Sort by name (natural sorting).
191 /* If the names are equal, sort by cargo bitnum. */
192 return (res != 0) ? res < 0 : (a->bitnum < b->bitnum);
195 /** Sort cargo specifications by their cargo class. */
196 static bool CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * const &b)
198 int res = (b->classes & CC_PASSENGERS) - (a->classes & CC_PASSENGERS);
199 if (res == 0) {
200 res = (b->classes & CC_MAIL) - (a->classes & CC_MAIL);
201 if (res == 0) {
202 res = (a->classes & CC_SPECIAL) - (b->classes & CC_SPECIAL);
203 if (res == 0) {
204 return CargoSpecNameSorter(a, b);
209 return res < 0;
212 /** Initialize the list of sorted cargo specifications. */
213 void InitializeSortedCargoSpecs()
215 for (auto &tpc : CargoSpec::town_production_cargoes) tpc.clear();
216 _sorted_cargo_specs.clear();
217 /* Add each cargo spec to the list, and determine the largest cargo icon size. */
218 for (const CargoSpec *cargo : CargoSpec::Iterate()) {
219 _sorted_cargo_specs.push_back(cargo);
222 /* Sort cargo specifications by cargo class and name. */
223 std::sort(_sorted_cargo_specs.begin(), _sorted_cargo_specs.end(), &CargoSpecClassSorter);
225 /* Populate */
226 for (auto it = std::begin(_sorted_cargo_specs); it != std::end(_sorted_cargo_specs); ++it) {
227 _sorted_cargo_types[(*it)->Index()] = static_cast<uint8_t>(it - std::begin(_sorted_cargo_specs));
230 /* Count the number of standard cargos and fill the mask. */
231 _standard_cargo_mask = 0;
232 uint8_t nb_standard_cargo = 0;
233 for (const auto &cargo : _sorted_cargo_specs) {
234 assert(cargo->town_production_effect != INVALID_TPE);
235 CargoSpec::town_production_cargoes[cargo->town_production_effect].push_back(cargo);
236 if (cargo->classes & CC_SPECIAL) break;
237 nb_standard_cargo++;
238 SetBit(_standard_cargo_mask, cargo->Index());
241 /* _sorted_standard_cargo_specs is a subset of _sorted_cargo_specs. */
242 _sorted_standard_cargo_specs = { _sorted_cargo_specs.data(), nb_standard_cargo };
245 uint64_t CargoSpec::WeightOfNUnitsInTrain(uint32_t n) const
247 if (this->is_freight) n *= _settings_game.vehicle.freight_trains;
248 return this->WeightOfNUnits(n);