Update: Translations from eints
[openttd-github.git] / src / cargotype.cpp
blob870369dbbd9438609e39d8f51f1fe4600a041f43
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 * Set up the default cargo types for the given landscape type.
46 * @param l Landscape
48 void SetupCargoForClimate(LandscapeID l)
50 assert(l < lengthof(_default_climate_cargo));
52 _cargo_mask = 0;
53 _default_cargo_labels.clear();
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]) {
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();
75 if (insert->IsValid()) {
76 SetBit(_cargo_mask, insert->Index());
77 _default_cargo_labels.push_back(insert->label);
79 ++insert;
82 /* Reset and disable remaining cargo types. */
83 std::fill(insert, std::end(CargoSpec::array), CargoSpec{});
85 BuildCargoLabelMap();
88 /**
89 * Build cargo label map.
90 * This is called multiple times during NewGRF initialization as cargos are defined, so that TranslateRefitMask() and
91 * GetCargoTranslation(), also used during initialization, get the correct information.
93 void BuildCargoLabelMap()
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;
102 CargoSpec::label_map.emplace(cs.label, cs.Index());
107 * Test if a cargo is a default cargo type.
108 * @param cid Cargo ID.
109 * @returns true iff the cargo type is a default cargo type.
111 bool IsDefaultCargo(CargoID cid)
113 auto cs = CargoSpec::Get(cid);
114 if (!cs->IsValid()) return false;
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; });
121 * Get dimensions of largest cargo icon.
122 * @return Dimensions of largest cargo icon.
124 Dimension GetLargestCargoIconSize()
126 Dimension size = {0, 0};
127 for (const CargoSpec *cs : _sorted_cargo_specs) {
128 size = maxdim(size, GetSpriteSize(cs->GetCargoIcon()));
130 return size;
134 * Find the CargoID of a 'bitnum' value.
135 * @param bitnum 'bitnum' to find.
136 * @return First CargoID with the given bitnum, or #INVALID_CARGO if not found or if the provided \a bitnum is invalid.
138 CargoID GetCargoIDByBitnum(uint8_t bitnum)
140 if (bitnum == INVALID_CARGO_BITNUM) return INVALID_CARGO;
142 for (const CargoSpec *cs : CargoSpec::Iterate()) {
143 if (cs->bitnum == bitnum) return cs->Index();
146 /* No matching label was found, so it is invalid */
147 return INVALID_CARGO;
151 * Get sprite for showing cargo of this type.
152 * @return Sprite number to use.
154 SpriteID CargoSpec::GetCargoIcon() const
156 SpriteID sprite = this->sprite;
157 if (sprite == 0xFFFF) {
158 /* A value of 0xFFFF indicates we should draw a custom icon */
159 sprite = GetCustomCargoSprite(this);
162 if (sprite == 0) sprite = SPR_CARGO_GOODS;
164 return sprite;
167 std::array<uint8_t, NUM_CARGO> _sorted_cargo_types; ///< Sort order of cargoes by cargo ID.
168 std::vector<const CargoSpec *> _sorted_cargo_specs; ///< Cargo specifications sorted alphabetically by name.
169 std::span<const CargoSpec *> _sorted_standard_cargo_specs; ///< Standard cargo specifications sorted alphabetically by name.
171 /** Sort cargo specifications by their name. */
172 static bool CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b)
174 std::string a_name = GetString(a->name);
175 std::string b_name = GetString(b->name);
177 int res = StrNaturalCompare(a_name, b_name); // Sort by name (natural sorting).
179 /* If the names are equal, sort by cargo bitnum. */
180 return (res != 0) ? res < 0 : (a->bitnum < b->bitnum);
183 /** Sort cargo specifications by their cargo class. */
184 static bool CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * const &b)
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);
197 return res < 0;
200 /** Initialize the list of sorted cargo specifications. */
201 void InitializeSortedCargoSpecs()
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);
210 /* Sort cargo specifications by cargo class and name. */
211 std::sort(_sorted_cargo_specs.begin(), _sorted_cargo_specs.end(), &CargoSpecClassSorter);
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));
218 /* Count the number of standard cargos and fill the mask. */
219 _standard_cargo_mask = 0;
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());
229 /* _sorted_standard_cargo_specs is a subset of _sorted_cargo_specs. */
230 _sorted_standard_cargo_specs = { _sorted_cargo_specs.data(), nb_standard_cargo };
233 uint64_t CargoSpec::WeightOfNUnitsInTrain(uint32_t n) const
235 if (this->is_freight) n *= _settings_game.vehicle.freight_trains;
236 return this->WeightOfNUnits(n);