From 1a53b484224982b124b8afa981ca98171b29b050 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Mon, 10 Mar 2025 18:59:21 +0000 Subject: [PATCH] Codechange: Use EnumBitSet for PartsOfSubsidy. (#13791) --- src/industry.h | 2 +- src/subsidy.cpp | 26 +++++++++++++------------- src/subsidy_type.h | 10 +++++----- src/town.h | 2 +- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/industry.h b/src/industry.h index b8e99436d9..d7683d0c41 100644 --- a/src/industry.h +++ b/src/industry.h @@ -103,7 +103,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> { uint8_t was_cargo_delivered = 0; ///< flag that indicate this has been the closest industry chosen for cargo delivery by a station. see DeliverGoodsToIndustry IndustryControlFlags ctlflags{}; ///< flags overriding standard behaviours - PartOfSubsidy part_of_subsidy{}; ///< NOSAVE: is this industry a source/destination of a subsidy? + PartsOfSubsidy part_of_subsidy{}; ///< NOSAVE: is this industry a source/destination of a subsidy? StationList stations_near{}; ///< NOSAVE: List of nearby stations. mutable std::string cached_name{}; ///< NOSAVE: Cache of the resolved name of the industry diff --git a/src/subsidy.cpp b/src/subsidy.cpp index d3acb11910..537063dbb6 100644 --- a/src/subsidy.cpp +++ b/src/subsidy.cpp @@ -93,8 +93,8 @@ void Subsidy::AwardTo(CompanyID company) static inline void SetPartOfSubsidyFlag(Source source, PartOfSubsidy flag) { switch (source.type) { - case SourceType::Industry: Industry::Get(source.ToIndustryID())->part_of_subsidy |= flag; return; - case SourceType::Town: Town::Get(source.ToTownID())->cache.part_of_subsidy |= flag; return; + case SourceType::Industry: Industry::Get(source.ToIndustryID())->part_of_subsidy.Set(flag); return; + case SourceType::Town: Town::Get(source.ToTownID())->cache.part_of_subsidy.Set(flag); return; default: NOT_REACHED(); } } @@ -102,13 +102,13 @@ static inline void SetPartOfSubsidyFlag(Source source, PartOfSubsidy flag) /** Perform a full rebuild of the subsidies cache. */ void RebuildSubsidisedSourceAndDestinationCache() { - for (Town *t : Town::Iterate()) t->cache.part_of_subsidy = POS_NONE; + for (Town *t : Town::Iterate()) t->cache.part_of_subsidy = {}; - for (Industry *i : Industry::Iterate()) i->part_of_subsidy = POS_NONE; + for (Industry *i : Industry::Iterate()) i->part_of_subsidy = {}; for (const Subsidy *s : Subsidy::Iterate()) { - SetPartOfSubsidyFlag(s->src, POS_SRC); - SetPartOfSubsidyFlag(s->dst, POS_DST); + SetPartOfSubsidyFlag(s->src, PartOfSubsidy::Source); + SetPartOfSubsidyFlag(s->dst, PartOfSubsidy::Destination); } } @@ -177,8 +177,8 @@ void CreateSubsidy(CargoType cargo_type, Source src, Source dst) const CargoSpec *cs = CargoSpec::Get(s->cargo_type); EncodedString headline = GetEncodedString(STR_NEWS_SERVICE_SUBSIDY_OFFERED, cs->name, s->src.GetFormat(), s->src.id, s->dst.GetFormat(), s->dst.id, _settings_game.difficulty.subsidy_duration); AddNewsItem(std::move(headline), NewsType::Subsidies, NewsStyle::Normal, {}, s->src.GetNewsReference(), s->dst.GetNewsReference()); - SetPartOfSubsidyFlag(s->src, POS_SRC); - SetPartOfSubsidyFlag(s->dst, POS_DST); + SetPartOfSubsidyFlag(s->src, PartOfSubsidy::Source); + SetPartOfSubsidyFlag(s->dst, PartOfSubsidy::Destination); AI::BroadcastNewEvent(new ScriptEventSubsidyOffer(s->index)); Game::NewEvent(new ScriptEventSubsidyOffer(s->index)); @@ -510,10 +510,10 @@ bool CheckSubsidised(CargoType cargo_type, CompanyID company, Source src, const if (!src.IsValid()) return false; switch (src.type) { case SourceType::Industry: - if (!(Industry::Get(src.ToIndustryID())->part_of_subsidy & POS_SRC)) return false; + if (!Industry::Get(src.ToIndustryID())->part_of_subsidy.Test(PartOfSubsidy::Source)) return false; break; case SourceType::Town: - if (!(Town::Get(src.ToTownID())->cache.part_of_subsidy & POS_SRC)) return false; + if (!Town::Get(src.ToTownID())->cache.part_of_subsidy.Test(PartOfSubsidy::Source)) return false; break; default: return false; } @@ -532,7 +532,7 @@ bool CheckSubsidised(CargoType cargo_type, CompanyID company, Source src, const for (TileIndex tile = it; tile != INVALID_TILE; tile = ++it) { if (!IsTileType(tile, MP_HOUSE)) continue; const Town *t = Town::GetByTile(tile); - if (t->cache.part_of_subsidy & POS_DST) include(towns_near, t); + if (t->cache.part_of_subsidy.Test(PartOfSubsidy::Destination)) include(towns_near, t); } break; } @@ -548,7 +548,7 @@ bool CheckSubsidised(CargoType cargo_type, CompanyID company, Source src, const case SourceType::Industry: for (const auto &i : st->industries_near) { if (s->dst.ToIndustryID() == i.industry->index) { - assert(i.industry->part_of_subsidy & POS_DST); + assert(i.industry->part_of_subsidy.Test(PartOfSubsidy::Destination)); subsidised = true; if (!s->IsAwarded()) s->AwardTo(company); } @@ -557,7 +557,7 @@ bool CheckSubsidised(CargoType cargo_type, CompanyID company, Source src, const case SourceType::Town: for (const Town *tp : towns_near) { if (s->dst.ToTownID() == tp->index) { - assert(tp->cache.part_of_subsidy & POS_DST); + assert(tp->cache.part_of_subsidy.Test(PartOfSubsidy::Destination)); subsidised = true; if (!s->IsAwarded()) s->AwardTo(company); } diff --git a/src/subsidy_type.h b/src/subsidy_type.h index 1d4dd17d19..de03cc9ba1 100644 --- a/src/subsidy_type.h +++ b/src/subsidy_type.h @@ -13,12 +13,12 @@ #include "core/enum_type.hpp" /** What part of a subsidy is something? */ -enum PartOfSubsidy : uint8_t { - POS_NONE = 0, ///< nothing - POS_SRC = 1 << 0, ///< bit 0 set -> town/industry is source of subsidised path - POS_DST = 1 << 1, ///< bit 1 set -> town/industry is destination of subsidised path +enum class PartOfSubsidy : uint8_t { + Source, ///< town/industry is source of subsidised path + Destination, ///< town/industry is destination of subsidised path }; -DECLARE_ENUM_AS_BIT_SET(PartOfSubsidy) + +using PartsOfSubsidy = EnumBitSet; using SubsidyID = PoolID; ///< ID of a subsidy struct Subsidy; diff --git a/src/town.h b/src/town.h index 517c6b1b24..1c6e134db6 100644 --- a/src/town.h +++ b/src/town.h @@ -41,7 +41,7 @@ struct TownCache { uint32_t num_houses = 0; ///< Amount of houses uint32_t population = 0; ///< Current population of people TrackedViewportSign sign{}; ///< Location of name sign, UpdateVirtCoord updates this - PartOfSubsidy part_of_subsidy{}; ///< Is this town a source/destination of a subsidy? + PartsOfSubsidy part_of_subsidy{}; ///< Is this town a source/destination of a subsidy? std::array squared_town_zone_radius{}; ///< UpdateTownRadius updates this given the house count BuildingCounts building_counts{}; ///< The number of each type of building in the town