From ac5dc8ed228261ce2622269a3a3025a4acac7028 Mon Sep 17 00:00:00 2001 From: SamuXarick <43006711+SamuXarick@users.noreply.github.com> Date: Tue, 12 Nov 2024 22:55:26 +0000 Subject: [PATCH] Codechange: Each town cache the number of each type of industry This allows for a quick FindTownForIndustry without having to iterate over all industries. --- src/cachecheck.cpp | 1 + src/industry.h | 2 ++ src/industry_cmd.cpp | 24 ++++++++++++++++++------ src/saveload/afterload.cpp | 2 ++ src/town.h | 2 ++ src/town_cmd.cpp | 1 + 6 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/cachecheck.cpp b/src/cachecheck.cpp index a68fa7871c..f6296bb14b 100644 --- a/src/cachecheck.cpp +++ b/src/cachecheck.cpp @@ -47,6 +47,7 @@ void CheckCaches() RebuildTownCaches(); RebuildSubsidisedSourceAndDestinationCache(); + RebuildTownIndustryCounts(); uint i = 0; for (Town *t : Town::Iterate()) { diff --git a/src/industry.h b/src/industry.h index cd29dcff7f..d8e9b354a0 100644 --- a/src/industry.h +++ b/src/industry.h @@ -343,4 +343,6 @@ enum IndustryDirectoryInvalidateWindowData { void TrimIndustryAcceptedProduced(Industry *ind); +void RebuildTownIndustryCounts(); + #endif /* INDUSTRY_H */ diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 07e18d6587..0f148e8d81 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -201,6 +201,8 @@ Industry::~Industry() for (Station *st : this->stations_near) { st->RemoveIndustryToDeliver(this); } + + this->town->cache.industry_counts[this->type]--; } /** @@ -1428,17 +1430,15 @@ static CheckNewIndustryProc * const _check_new_industry_procs[CHECK_END] = { * @pre \c *t != nullptr * @post \c *t points to a town on success, and \c nullptr on failure. */ -static CommandCost FindTownForIndustry(TileIndex tile, int type, Town **t) +static CommandCost FindTownForIndustry(TileIndex tile, IndustryType type, Town **t) { *t = ClosestTownFromTile(tile, UINT_MAX); if (_settings_game.economy.multiple_industry_per_town) return CommandCost(); - for (const Industry *i : Industry::Iterate()) { - if (i->type == (uint8_t)type && i->town == *t) { - *t = nullptr; - return_cmd_error(STR_ERROR_ONLY_ONE_ALLOWED_PER_TOWN); - } + if ((*t)->cache.industry_counts[type] != 0) { + *t = nullptr; + return_cmd_error(STR_ERROR_ONLY_ONE_ALLOWED_PER_TOWN); } return CommandCost(); @@ -1812,6 +1812,7 @@ static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type, } i->town = t; + t->cache.industry_counts[i->type]++; i->owner = OWNER_NONE; uint16_t r = Random(); @@ -3235,3 +3236,14 @@ void TrimIndustryAcceptedProduced(Industry *ind) ind->produced.erase(itp.base(), std::end(ind->produced)); ind->produced.shrink_to_fit(); } + +void RebuildTownIndustryCounts() +{ + for (Town *t : Town::Iterate()) { + std::ranges::fill(t->cache.industry_counts, 0); + } + + for (const Industry *ind : Industry::Iterate()) { + ind->town->cache.industry_counts[ind->type]++; + } +} diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 5df9556bba..e37cd5a9f7 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -311,6 +311,8 @@ static void InitializeWindowsAndCaches() /* Rebuild the smallmap list of owners. */ BuildOwnerLegend(); + + RebuildTownIndustryCounts(); } typedef void (CDECL *SignalHandlerPointer)(int); diff --git a/src/town.h b/src/town.h index ce930bb180..3b60d4e28d 100644 --- a/src/town.h +++ b/src/town.h @@ -16,6 +16,7 @@ #include "subsidy_type.h" #include "newgrf_storage.h" #include "cargotype.h" +#include "industry_type.h" template struct BuildingCounts { @@ -46,6 +47,7 @@ struct TownCache { PartOfSubsidy 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 + uint16_t industry_counts[NUM_INDUSTRYTYPES]; ///< The number of each type of industry in the town auto operator<=>(const TownCache &) const = default; }; diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index 1a536ec4b5..512423508f 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -2059,6 +2059,7 @@ static void DoCreateTown(Town *t, TileIndex tile, uint32_t townnameparts, TownSi UpdateTownGrowthRate(t); UpdateTownMaxPass(t); UpdateAirportsNoise(); + std::ranges::fill(t->cache.industry_counts, 0); } /**