@@ -41,35 +41,33 @@ void SetupCargoForClimate(LandscapeID l)
41
41
{
42
42
assert (l < lengthof (_default_climate_cargo));
43
43
44
- /* Reset and disable all cargo types */
45
- std::fill (std::begin (CargoSpec::array), std::end (CargoSpec::array), CargoSpec{});
46
-
47
44
_cargo_mask = 0 ;
48
45
49
- for (CargoID i = 0 ; i < lengthof (_default_climate_cargo[l]); i++) {
50
- CargoLabel cl = _default_climate_cargo[l][i];
46
+ /* Copy from default cargo by label or index. */
47
+ auto insert = std::begin (CargoSpec::array);
48
+ for (const CargoLabel &cl : _default_climate_cargo[l]) {
51
49
52
- /* Bzzt: check if cl is just an index into the cargo table */
50
+ /* Check if value is an index into the cargo table */
53
51
if (cl < lengthof (_default_cargo)) {
54
- /* Copy the indexed cargo */
55
- CargoSpec *cargo = CargoSpec::Get (i);
56
- *cargo = _default_cargo[cl];
57
- if (cargo->bitnum != INVALID_CARGO_BITNUM) SetBit (_cargo_mask, i);
58
- continue ;
59
- }
60
-
61
- /* Loop through each of the default cargo types to see if
62
- * the label matches */
63
- for (uint j = 0 ; j < lengthof (_default_cargo); j++) {
64
- if (_default_cargo[j].label == cl) {
65
- *CargoSpec::Get (i) = _default_cargo[j];
66
-
67
- /* Populate the available cargo mask */
68
- SetBit (_cargo_mask, i);
69
- break ;
52
+ /* Copy the default cargo by index. */
53
+ *insert = _default_cargo[cl];
54
+ } else {
55
+ /* Search for label in default cargo types and copy if found. */
56
+ auto found = std::find_if (std::begin (_default_cargo), std::end (_default_cargo), [&cl](const CargoSpec &cs) { return cs.label == cl; });
57
+ if (found != std::end (_default_cargo)) {
58
+ *insert = *found;
59
+ } else {
60
+ /* Index or label is invalid, this should not happen. */
61
+ NOT_REACHED ();
70
62
}
71
63
}
64
+
65
+ if (insert->IsValid ()) SetBit (_cargo_mask, insert->Index ());
66
+ ++insert;
72
67
}
68
+
69
+ /* Reset and disable remaining cargo types. */
70
+ std::fill (insert, std::end (CargoSpec::array), CargoSpec{});
73
71
}
74
72
75
73
/* *
0 commit comments