mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-10 08:00:05 +00:00
Codechange: Extract code to build cargo acceptance string for re-use.
This commit is contained in:
parent
2ede94bc40
commit
041b9181f9
@ -19,6 +19,8 @@
|
|||||||
#include "table/strings.h"
|
#include "table/strings.h"
|
||||||
#include "table/cargo_const.h"
|
#include "table/cargo_const.h"
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
#include "safeguards.h"
|
#include "safeguards.h"
|
||||||
|
|
||||||
CargoSpec CargoSpec::array[NUM_CARGO];
|
CargoSpec CargoSpec::array[NUM_CARGO];
|
||||||
@ -257,3 +259,39 @@ uint64_t CargoSpec::WeightOfNUnitsInTrain(uint32_t n) const
|
|||||||
if (this->is_freight) n *= _settings_game.vehicle.freight_trains;
|
if (this->is_freight) n *= _settings_game.vehicle.freight_trains;
|
||||||
return this->WeightOfNUnits(n);
|
return this->WeightOfNUnits(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build comma-separated cargo acceptance string.
|
||||||
|
* @param acceptance CargoArray filled with accepted cargo.
|
||||||
|
* @param label Label to prefix cargo acceptance list.
|
||||||
|
* @return String of accepted cargo, or nullopt if no cargo is accepted.
|
||||||
|
*/
|
||||||
|
std::optional<std::string> BuildCargoAcceptanceString(const CargoArray &acceptance, StringID label)
|
||||||
|
{
|
||||||
|
/* Cargo acceptance is displayed in a extra multiline */
|
||||||
|
std::stringstream line;
|
||||||
|
line << GetString(label);
|
||||||
|
|
||||||
|
bool found = false;
|
||||||
|
for (const CargoSpec *cs : _sorted_cargo_specs) {
|
||||||
|
CargoID cid = cs->Index();
|
||||||
|
if (acceptance[cid] > 0) {
|
||||||
|
/* Add a comma between each item. */
|
||||||
|
if (found) line << ", ";
|
||||||
|
found = true;
|
||||||
|
|
||||||
|
/* If the accepted value is less than 8, show it in 1/8:ths */
|
||||||
|
if (acceptance[cid] < 8) {
|
||||||
|
SetDParam(0, acceptance[cid]);
|
||||||
|
SetDParam(1, cs->name);
|
||||||
|
line << GetString(STR_LAND_AREA_INFORMATION_CARGO_EIGHTS);
|
||||||
|
} else {
|
||||||
|
line << GetString(cs->name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (found) return line.str();
|
||||||
|
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
@ -214,6 +214,8 @@ void SetupCargoForClimate(LandscapeID l);
|
|||||||
bool IsDefaultCargo(CargoID cid);
|
bool IsDefaultCargo(CargoID cid);
|
||||||
void BuildCargoLabelMap();
|
void BuildCargoLabelMap();
|
||||||
|
|
||||||
|
std::optional<std::string> BuildCargoAcceptanceString(const CargoArray &acceptance, StringID label);
|
||||||
|
|
||||||
inline CargoID GetCargoIDByLabel(CargoLabel label)
|
inline CargoID GetCargoIDByLabel(CargoLabel label)
|
||||||
{
|
{
|
||||||
auto found = CargoSpec::label_map.find(label);
|
auto found = CargoSpec::label_map.find(label);
|
||||||
|
@ -307,29 +307,9 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Cargo acceptance is displayed in a extra multiline */
|
/* Cargo acceptance is displayed in a extra multiline */
|
||||||
std::stringstream line;
|
auto line = BuildCargoAcceptanceString(acceptance, STR_LAND_AREA_INFORMATION_CARGO_ACCEPTED);
|
||||||
line << GetString(STR_LAND_AREA_INFORMATION_CARGO_ACCEPTED);
|
if (line.has_value()) {
|
||||||
|
this->cargo_acceptance = std::move(*line);
|
||||||
bool found = false;
|
|
||||||
for (const CargoSpec *cs : _sorted_cargo_specs) {
|
|
||||||
CargoID cid = cs->Index();
|
|
||||||
if (acceptance[cid] > 0) {
|
|
||||||
/* Add a comma between each item. */
|
|
||||||
if (found) line << ", ";
|
|
||||||
found = true;
|
|
||||||
|
|
||||||
/* If the accepted value is less than 8, show it in 1/8:ths */
|
|
||||||
if (acceptance[cid] < 8) {
|
|
||||||
SetDParam(0, acceptance[cid]);
|
|
||||||
SetDParam(1, cs->name);
|
|
||||||
line << GetString(STR_LAND_AREA_INFORMATION_CARGO_EIGHTS);
|
|
||||||
} else {
|
|
||||||
line << GetString(cs->name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (found) {
|
|
||||||
this->cargo_acceptance = line.str();
|
|
||||||
} else {
|
} else {
|
||||||
this->cargo_acceptance.clear();
|
this->cargo_acceptance.clear();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user