From 429a6f58e763f83c94ea57b48c77bc18ac9b7013 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Fri, 20 Oct 2023 18:40:26 +0100 Subject: [PATCH] Codechange: Use max_element to find best cargo (sub-)type in NewGRF var 42. (#11370) This replaces two manual loops to find the best cargo (sub-)type. --- src/newgrf_engine.cpp | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 603e839929..27321ec496 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -435,19 +435,11 @@ static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *objec case 0x42: { // Consist cargo information if (!HasBit(v->grf_cache.cache_valid, NCVV_CONSIST_CARGO_INFORMATION)) { - const Vehicle *u; + std::array common_cargoes{}; byte cargo_classes = 0; - uint8_t common_cargoes[NUM_CARGO]; - uint8_t common_subtypes[256]; byte user_def_data = 0; - CargoID common_cargo_type = CT_INVALID; - uint8_t common_subtype = 0xFF; // Return 0xFF if nothing is carried - /* Reset our arrays */ - memset(common_cargoes, 0, sizeof(common_cargoes)); - memset(common_subtypes, 0, sizeof(common_subtypes)); - - for (u = v; u != nullptr; u = u->Next()) { + for (const Vehicle *u = v; u != nullptr; u = u->Next()) { if (v->type == VEH_TRAIN) user_def_data |= Train::From(u)->tcache.user_def_data; /* Skip empty engines */ @@ -458,16 +450,13 @@ static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *objec } /* Pick the most common cargo type */ - uint common_cargo_best_amount = 0; - for (CargoID cargo = 0; cargo < NUM_CARGO; cargo++) { - if (common_cargoes[cargo] > common_cargo_best_amount) { - common_cargo_best_amount = common_cargoes[cargo]; - common_cargo_type = cargo; - } - } + auto cargo_it = std::max_element(std::begin(common_cargoes), std::end(common_cargoes)); + /* Return CT_INVALID if nothing is carried */ + CargoID common_cargo_type = (*cargo_it == 0) ? (CargoID)CT_INVALID : static_cast(std::distance(std::begin(common_cargoes), cargo_it)); /* Count subcargo types of common_cargo_type */ - for (u = v; u != nullptr; u = u->Next()) { + std::array common_subtypes{}; + for (const Vehicle *u = v; u != nullptr; u = u->Next()) { /* Skip empty engines and engines not carrying common_cargo_type */ if (u->cargo_type != common_cargo_type || !u->GetEngine()->CanCarryCargo()) continue; @@ -475,13 +464,9 @@ static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *objec } /* Pick the most common subcargo type*/ - uint common_subtype_best_amount = 0; - for (uint i = 0; i < lengthof(common_subtypes); i++) { - if (common_subtypes[i] > common_subtype_best_amount) { - common_subtype_best_amount = common_subtypes[i]; - common_subtype = i; - } - } + auto subtype_it = std::max_element(std::begin(common_subtypes), std::end(common_subtypes)); + /* Return UINT8_MAX if nothing is carried */ + uint8_t common_subtype = (*subtype_it == 0) ? UINT8_MAX : static_cast(std::distance(std::begin(common_subtypes), subtype_it)); /* Note: We have to store the untranslated cargotype in the cache as the cache can be read by different NewGRFs, * which will need different translations */