Codechange: Speed up industry generation using industry-type checks. (#13094)

Store a list of industries per industry type. This allows industry generation checks which only consider a specific industry type to check a reduced set of industries, leading to a potential performance increase.

This also removes the need to track industry type counts as well.
This commit is contained in:
Peter Nelson
2024-11-22 23:17:24 +00:00
committed by GitHub
parent a951896364
commit 16038879e4
5 changed files with 39 additions and 77 deletions

View File

@@ -92,10 +92,11 @@ uint32_t GetIndustryIDAtOffset(TileIndex tile, const Industry *i, uint32_t cur_g
static uint32_t GetClosestIndustry(TileIndex tile, IndustryType type, const Industry *current)
{
uint32_t best_dist = UINT32_MAX;
for (const Industry *i : Industry::Iterate()) {
if (i->type != type || i == current) continue;
best_dist = std::min(best_dist, DistanceManhattan(tile, i->location.tile));
for (const IndustryID &industry : Industry::industries[type]) {
if (industry == current->index) continue;
best_dist = std::min(best_dist, DistanceManhattan(tile, Industry::Get(industry)->location.tile));
}
return best_dist;
@@ -145,8 +146,11 @@ static uint32_t GetCountAndDistanceOfClosestInstance(uint8_t param_setID, uint8_
} else {
/* Count only those who match the same industry type and layout filter
* Unfortunately, we have to do it manually */
for (const Industry *i : Industry::Iterate()) {
if (i->type == ind_index && i != current && (i->selected_layout == layout_filter || layout_filter == 0) && (!town_filter || i->town == current->town)) {
for (const IndustryID &industry : Industry::industries[ind_index]) {
if (industry == current->index) continue;
const Industry *i = Industry::Get(industry);
if ((layout_filter == 0 || i->selected_layout == layout_filter) && (!town_filter || i->town == current->town)) {
closest_dist = std::min(closest_dist, DistanceManhattan(current->location.tile, i->location.tile));
count++;
}
@@ -278,9 +282,12 @@ static uint32_t GetCountAndDistanceOfClosestInstance(uint8_t param_setID, uint8_
}
/* Distance of nearest industry of given type */
case 0x64:
case 0x64: {
if (this->tile == INVALID_TILE) break;
return GetClosestIndustry(this->tile, MapNewGRFIndustryType(parameter, indspec->grf_prop.grffile->grfid), this->industry);
IndustryType type = MapNewGRFIndustryType(parameter, indspec->grf_prop.grffile->grfid);
if (type >= NUM_INDUSTRYTYPES) return UINT32_MAX;
return GetClosestIndustry(this->tile, type, this->industry);
}
/* Get town zone and Manhattan distance of closest town */
case 0x65: {
if (this->tile == INVALID_TILE) break;