From b9c44b29be9f4ba8b1a37d24a93c11e6fa98658a Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Wed, 17 Jul 2024 18:16:22 +0100 Subject: [PATCH] Codechange: Pass AirportSpec instead of index to airport resolver object. (#12866) This avoids retrieving AirportSpec again when it is already available. --- src/newgrf_airport.cpp | 16 ++++++++-------- src/newgrf_airport.h | 10 +++++----- src/newgrf_airporttiles.cpp | 2 +- src/table/newgrf_debug_data.h | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/newgrf_airport.cpp b/src/newgrf_airport.cpp index 1471f417e2..0d90c0adad 100644 --- a/src/newgrf_airport.cpp +++ b/src/newgrf_airport.cpp @@ -183,7 +183,7 @@ GrfSpecFeature AirportResolverObject::GetFeature() const uint32_t AirportResolverObject::GetDebugID() const { - return AirportSpec::Get(this->airport_scope.airport_id)->grf_prop.local_id; + return this->airport_scope.spec->grf_prop.local_id; } /* virtual */ uint32_t AirportScopeResolver::GetRandomBits() const @@ -236,22 +236,22 @@ TownScopeResolver *AirportResolverObject::GetTown() * Constructor of the airport resolver. * @param tile %Tile for the callback, only valid for airporttile callbacks. * @param st %Station of the airport for which the callback is run, or \c nullptr for build gui. - * @param airport_id Type of airport for which the callback is run. + * @param spec AirportSpec for which the callback is run. * @param layout Layout of the airport to build. * @param callback Callback ID. * @param param1 First parameter (var 10) of the callback. * @param param2 Second parameter (var 18) of the callback. */ -AirportResolverObject::AirportResolverObject(TileIndex tile, Station *st, uint8_t airport_id, uint8_t layout, +AirportResolverObject::AirportResolverObject(TileIndex tile, Station *st, const AirportSpec *spec, uint8_t layout, CallbackID callback, uint32_t param1, uint32_t param2) - : ResolverObject(AirportSpec::Get(airport_id)->grf_prop.grffile, callback, param1, param2), airport_scope(*this, tile, st, airport_id, layout) + : ResolverObject(spec->grf_prop.grffile, callback, param1, param2), airport_scope(*this, tile, st, spec, layout) { - this->root_spritegroup = AirportSpec::Get(airport_id)->grf_prop.spritegroup[0]; + this->root_spritegroup = spec->grf_prop.spritegroup[0]; } SpriteID GetCustomAirportSprite(const AirportSpec *as, uint8_t layout) { - AirportResolverObject object(INVALID_TILE, nullptr, as->GetIndex(), layout); + AirportResolverObject object(INVALID_TILE, nullptr, as, layout); const SpriteGroup *group = object.Resolve(); if (group == nullptr) return as->preview_sprite; @@ -260,7 +260,7 @@ SpriteID GetCustomAirportSprite(const AirportSpec *as, uint8_t layout) uint16_t GetAirportCallback(CallbackID callback, uint32_t param1, uint32_t param2, Station *st, TileIndex tile) { - AirportResolverObject object(tile, st, st->airport.type, st->airport.layout, callback, param1, param2); + AirportResolverObject object(tile, st, AirportSpec::Get(st->airport.type), st->airport.layout, callback, param1, param2); return object.ResolveCallback(); } @@ -273,7 +273,7 @@ uint16_t GetAirportCallback(CallbackID callback, uint32_t param1, uint32_t param */ StringID GetAirportTextCallback(const AirportSpec *as, uint8_t layout, uint16_t callback) { - AirportResolverObject object(INVALID_TILE, nullptr, as->GetIndex(), layout, (CallbackID)callback); + AirportResolverObject object(INVALID_TILE, nullptr, as, layout, (CallbackID)callback); uint16_t cb_res = object.ResolveCallback(); if (cb_res == CALLBACK_FAILED || cb_res == 0x400) return STR_UNDEFINED; if (cb_res > 0x400) { diff --git a/src/newgrf_airport.h b/src/newgrf_airport.h index 5ac58bc72d..3242e482c5 100644 --- a/src/newgrf_airport.h +++ b/src/newgrf_airport.h @@ -149,7 +149,7 @@ void BindAirportSpecs(); /** Resolver for the airport scope. */ struct AirportScopeResolver : public ScopeResolver { struct Station *st; ///< Station of the airport for which the callback is run, or \c nullptr for build gui. - uint8_t airport_id; ///< Type of airport for which the callback is run. + const AirportSpec *spec; ///< AirportSpec for which the callback is run. uint8_t layout; ///< Layout of the airport to build. TileIndex tile; ///< Tile for the callback, only valid for airporttile callbacks. @@ -158,11 +158,11 @@ struct AirportScopeResolver : public ScopeResolver { * @param ro Surrounding resolver. * @param tile %Tile for the callback, only valid for airporttile callbacks. * @param st %Station of the airport for which the callback is run, or \c nullptr for build gui. - * @param airport_id Type of airport for which the callback is run. + * @param spec AirportSpec for which the callback is run. * @param layout Layout of the airport to build. */ - AirportScopeResolver(ResolverObject &ro, TileIndex tile, Station *st, uint8_t airport_id, uint8_t layout) - : ScopeResolver(ro), st(st), airport_id(airport_id), layout(layout), tile(tile) + AirportScopeResolver(ResolverObject &ro, TileIndex tile, Station *st, const AirportSpec *spec, uint8_t layout) + : ScopeResolver(ro), st(st), spec(spec), layout(layout), tile(tile) { } @@ -177,7 +177,7 @@ struct AirportResolverObject : public ResolverObject { AirportScopeResolver airport_scope; std::optional town_scope = std::nullopt; ///< The town scope resolver (created on the first call). - AirportResolverObject(TileIndex tile, Station *st, uint8_t airport_id, uint8_t layout, + AirportResolverObject(TileIndex tile, Station *st, const AirportSpec *spec, uint8_t layout, CallbackID callback = CBID_NO_CALLBACK, uint32_t callback_param1 = 0, uint32_t callback_param2 = 0); TownScopeResolver *GetTown(); diff --git a/src/newgrf_airporttiles.cpp b/src/newgrf_airporttiles.cpp index a4adf748a8..8cd6123182 100644 --- a/src/newgrf_airporttiles.cpp +++ b/src/newgrf_airporttiles.cpp @@ -216,7 +216,7 @@ AirportTileResolverObject::AirportTileResolverObject(const AirportTileSpec *ats, CallbackID callback, uint32_t callback_param1, uint32_t callback_param2) : ResolverObject(ats->grf_prop.grffile, callback, callback_param1, callback_param2), tiles_scope(*this, ats, tile, st), - airport_scope(*this, tile, st, st != nullptr ? st->airport.type : (uint8_t)AT_DUMMY, st != nullptr ? st->airport.layout : 0) + airport_scope(*this, tile, st, st != nullptr ? AirportSpec::Get(st->airport.type) : nullptr, st != nullptr ? st->airport.layout : 0) { this->root_spritegroup = ats->grf_prop.spritegroup[0]; } diff --git a/src/table/newgrf_debug_data.h b/src/table/newgrf_debug_data.h index 1be431a939..e134d182e2 100644 --- a/src/table/newgrf_debug_data.h +++ b/src/table/newgrf_debug_data.h @@ -548,7 +548,7 @@ class NIHAirport : public NIHelper { uint Resolve(uint index, uint var, uint param, bool &avail) const override { Station *st = Station::Get(index); - AirportResolverObject ro(st->airport.tile, st, st->airport.type, st->airport.layout); + AirportResolverObject ro(st->airport.tile, st, AirportSpec::Get(st->airport.type), st->airport.layout); return ro.GetScope(VSG_SCOPE_SELF)->GetVariable(var, param, avail); }