From f9d0877f36deaf0bf54be05781d3bb68246aab34 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Fri, 16 May 2025 18:23:41 +0100 Subject: [PATCH] Codechange: Improve performance of exclusive preview engine test. Check group statistics to test if a company has built an exclusive preview engine. This improves performance by avoiding iterating the vehicle pool. --- src/engine.cpp | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index 627c7b9db4..a63b5a1954 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -1085,22 +1085,13 @@ static void NewVehicleAvailable(Engine *e) * prevent that company from getting future intro periods for a while. */ if (e->flags.Test(EngineFlag::ExclusivePreview)) { for (Company *c : Company::Iterate()) { - uint block_preview = c->block_preview; - if (!e->company_avail.Test(c->index)) continue; - /* 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) { - /* The user did prove me wrong, so restore old value */ - c->block_preview = block_preview; - break; - } - } + /* Check the company's 'ALL_GROUP' group statistics. This only includes countable vehicles, which is fine + * as those are the only engines that can be given exclusive previews. */ + if (GetGroupNumEngines(c->index, ALL_GROUP, e->index) == 0) { + /* The company did not build this engine during preview. */ + c->block_preview = 20; } } }