From 89c69f991ce49bed83b6725e710aca63e1459bf1 Mon Sep 17 00:00:00 2001 From: SamuXarick <43006711+SamuXarick@users.noreply.github.com> Date: Fri, 29 Dec 2023 11:19:17 +0000 Subject: [PATCH] Change: Iterate group vehicle lists for GetPreviewCompany and NewVehicleAvailable As a compromise, the result of this change differs from the original. Since the list only contains primary vehicles, free wagons aren't taken into consideration. --- src/engine.cpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index 4f9b3b5da2..09f6da61bc 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -885,13 +885,18 @@ static CompanyID GetPreviewCompany(Engine *e) c->old_economy[0].performance_history > best_hist) { /* Check whether the company uses similar vehicles */ - for (const Vehicle *v : Vehicle::Iterate()) { - if (v->owner != c->index || v->type != e->type) continue; - if (!v->GetEngine()->CanCarryCargo() || !HasBit(cargomask, v->cargo_type)) continue; + bool match = false; + const VehicleList &vehicle_list = c->group_all[e->type].vehicle_list; + for (const Vehicle *v : vehicle_list) { + for (const Vehicle *u = v; u != nullptr; u = u->Next()) { + if (!u->GetEngine()->CanCarryCargo() || !HasBit(cargomask, u->cargo_type)) continue; - best_hist = c->old_economy[0].performance_history; - best_company = c->index; - break; + best_hist = c->old_economy[0].performance_history; + best_company = c->index; + match = true; + break; + } + if (match) break; } } } @@ -1052,15 +1057,20 @@ static void NewVehicleAvailable(Engine *e) /* We assume the user did NOT build it.. prove me wrong ;) */ c->block_preview = 20; - for (const Vehicle *v : Vehicle::Iterate()) { - if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_SHIP || - (v->type == VEH_AIRCRAFT && Aircraft::From(v)->IsNormalAircraft())) { - if (v->owner == c->index && v->engine_type == index) { + bool match = false; + const VehicleList &vehicle_list = c->group_all[e->type].vehicle_list; + for (const Vehicle *v : vehicle_list) { + for (const Vehicle *u = v; u != nullptr; u = u->Next()) { + if (u->type == VEH_AIRCRAFT && !Aircraft::From(u)->IsNormalAircraft()) continue; + + if (u->engine_type == index) { /* The user did prove me wrong, so restore old value */ c->block_preview = block_preview; + match = true; break; } } + if (match) break; } } }