1
0
Fork 0

Codechange: Use range-for and iterator to populate default cargo table.

pull/11314/head
Peter Nelson 2023-09-17 17:53:13 +01:00 committed by PeterN
parent 280dce9543
commit fd2dd4397f
1 changed files with 20 additions and 22 deletions

View File

@ -41,35 +41,33 @@ void SetupCargoForClimate(LandscapeID l)
{ {
assert(l < lengthof(_default_climate_cargo)); assert(l < lengthof(_default_climate_cargo));
/* Reset and disable all cargo types */
std::fill(std::begin(CargoSpec::array), std::end(CargoSpec::array), CargoSpec{});
_cargo_mask = 0; _cargo_mask = 0;
for (CargoID i = 0; i < lengthof(_default_climate_cargo[l]); i++) { /* Copy from default cargo by label or index. */
CargoLabel cl = _default_climate_cargo[l][i]; auto insert = std::begin(CargoSpec::array);
for (const CargoLabel &cl : _default_climate_cargo[l]) {
/* Bzzt: check if cl is just an index into the cargo table */ /* Check if value is an index into the cargo table */
if (cl < lengthof(_default_cargo)) { if (cl < lengthof(_default_cargo)) {
/* Copy the indexed cargo */ /* Copy the default cargo by index. */
CargoSpec *cargo = CargoSpec::Get(i); *insert = _default_cargo[cl];
*cargo = _default_cargo[cl]; } else {
if (cargo->bitnum != INVALID_CARGO_BITNUM) SetBit(_cargo_mask, i); /* Search for label in default cargo types and copy if found. */
continue; auto found = std::find_if(std::begin(_default_cargo), std::end(_default_cargo), [&cl](const CargoSpec &cs) { return cs.label == cl; });
if (found != std::end(_default_cargo)) {
*insert = *found;
} else {
/* Index or label is invalid, this should not happen. */
NOT_REACHED();
}
} }
/* Loop through each of the default cargo types to see if if (insert->IsValid()) SetBit(_cargo_mask, insert->Index());
* the label matches */ ++insert;
for (uint j = 0; j < lengthof(_default_cargo); j++) { }
if (_default_cargo[j].label == cl) {
*CargoSpec::Get(i) = _default_cargo[j];
/* Populate the available cargo mask */ /* Reset and disable remaining cargo types. */
SetBit(_cargo_mask, i); std::fill(insert, std::end(CargoSpec::array), CargoSpec{});
break;
}
}
}
} }
/** /**