From 5c89dff677d9fa180626136a12caa8a354a18a94 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 23 Oct 2025 20:59:58 +0100 Subject: [PATCH] Codechange: Iterate road/tram masks instead of checking each type. (#14716) --- src/company_base.h | 6 ++++-- src/company_cmd.cpp | 20 ++++---------------- src/road.h | 14 -------------- src/road_type.h | 16 ++++++++++++++++ src/town_cmd.cpp | 12 +++--------- 5 files changed, 27 insertions(+), 41 deletions(-) diff --git a/src/company_base.h b/src/company_base.h index cdaca96dc4..a1409c84ed 100644 --- a/src/company_base.h +++ b/src/company_base.h @@ -45,8 +45,10 @@ struct CompanyInfrastructure { return std::accumulate(std::begin(this->rail), std::end(this->rail), 0U); } - uint32_t GetRoadTotal() const; - uint32_t GetTramTotal() const; + uint32_t GetRoadTramTotal(RoadTramType rtt) const; + + inline uint32_t GetRoadTotal() const { return GetRoadTramTotal(RTT_ROAD); } + inline uint32_t GetTramTotal() const { return GetRoadTramTotal(RTT_TRAM); } }; class FreeUnitIDGenerator { diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index ef62b91538..577f54b4b4 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -1291,26 +1291,14 @@ int CompanyServiceInterval(const Company *c, VehicleType type) /** * Get total sum of all owned road bits. + * @param rtt RoadTramType to get total for. * @return Combined total road road bits. */ -uint32_t CompanyInfrastructure::GetRoadTotal() const +uint32_t CompanyInfrastructure::GetRoadTramTotal(RoadTramType rtt) const { uint32_t total = 0; - for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) { - if (RoadTypeIsRoad(rt)) total += this->road[rt]; - } - return total; -} - -/** - * Get total sum of all owned tram bits. - * @return Combined total of tram road bits. - */ -uint32_t CompanyInfrastructure::GetTramTotal() const -{ - uint32_t total = 0; - for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) { - if (RoadTypeIsTram(rt)) total += this->road[rt]; + for (RoadType rt : GetMaskForRoadTramType(rtt)) { + total += this->road[rt]; } return total; } diff --git a/src/road.h b/src/road.h index b67df0e70a..4918d7cc80 100644 --- a/src/road.h +++ b/src/road.h @@ -21,20 +21,6 @@ #include "newgrf_badge_type.h" #include "economy_func.h" - -enum RoadTramType : bool { - RTT_ROAD, - RTT_TRAM, -}; - -enum RoadTramTypes : uint8_t { - RTTB_ROAD = 1 << RTT_ROAD, - RTTB_TRAM = 1 << RTT_TRAM, -}; -DECLARE_ENUM_AS_BIT_SET(RoadTramTypes) - -static const RoadTramType _roadtramtypes[] = { RTT_ROAD, RTT_TRAM }; - /** Roadtype flag bit numbers. */ enum class RoadTypeFlag : uint8_t { Catenary = 0, ///< Bit number for adding catenary diff --git a/src/road_type.h b/src/road_type.h index 07a6deb69e..99b5f8a40b 100644 --- a/src/road_type.h +++ b/src/road_type.h @@ -31,6 +31,22 @@ DECLARE_INCREMENT_DECREMENT_OPERATORS(RoadType) using RoadTypes = EnumBitSet; +/** + * The different types of road type. + */ +enum RoadTramType : bool { + RTT_ROAD, ///< Road road type. + RTT_TRAM, ///< Tram road type. +}; + +enum RoadTramTypes : uint8_t { + RTTB_ROAD = 1 << RTT_ROAD, ///< Road road type bit. + RTTB_TRAM = 1 << RTT_TRAM, ///< Tram road type bit. +}; +DECLARE_ENUM_AS_BIT_SET(RoadTramTypes) + +static const RoadTramType _roadtramtypes[] = { RTT_ROAD, RTT_TRAM }; + /** * Enumeration for the road parts on a tile. * diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index dea412c698..b3c95ba053 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -965,14 +965,9 @@ RoadType GetTownRoadType() const RoadTypeInfo *best = nullptr; const uint16_t assume_max_speed = 50; - for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) { - if (RoadTypeIsTram(rt)) continue; - + for (RoadType rt : GetMaskForRoadTramType(RTT_ROAD)) { const RoadTypeInfo *rti = GetRoadTypeInfo(rt); - /* Unused road type. */ - if (rti->label == 0) continue; - /* Can town build this road. */ if (!rti->flags.Test(RoadTypeFlag::TownBuild)) continue; @@ -997,10 +992,9 @@ RoadType GetTownRoadType() static TimerGameCalendar::Date GetTownRoadTypeFirstIntroductionDate() { const RoadTypeInfo *best = nullptr; - for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) { - if (RoadTypeIsTram(rt)) continue; + for (RoadType rt : GetMaskForRoadTramType(RTT_ROAD)) { const RoadTypeInfo *rti = GetRoadTypeInfo(rt); - if (rti->label == 0) continue; // Unused road type. + if (!rti->flags.Test(RoadTypeFlag::TownBuild)) continue; // Town can't build this road type. if (best != nullptr && rti->introduction_date >= best->introduction_date) continue;