VarAction2: Cache current town industry counts/distances for var 67/68

This commit is contained in:
Jonathan G Rennison 2023-12-23 20:59:12 +00:00
parent 48f66306e5
commit f57088b261
2 changed files with 23 additions and 0 deletions

View File

@ -145,6 +145,23 @@ uint32 IndustriesScopeResolver::GetCountAndDistanceOfClosestInstance(byte param_
* In either case, just do the regular var67 */
if (mask & 0xFFFF) closest_dist = this->GetClosestIndustry(ind_index);
if (mask & 0xFF0000) count = ClampTo<byte>(Industry::GetIndustryTypeCount(ind_index));
} else if (layout_filter == 0 && town_filter) {
/* Count only those which match the same industry type and town */
std::unique_ptr<IndustryLocationDistanceAndCountCache> &cache = this->town_location_distance_cache;
if (cache == nullptr) {
cache = std::make_unique<IndustryLocationDistanceAndCountCache>();
MemSetT(cache->distances, 0xFF, NUM_INDUSTRYTYPES);
MemSetT(cache->counts, 0, NUM_INDUSTRYTYPES);
for (const Industry *i : Industry::Iterate()) {
if (i == this->industry || i->type >= NUM_INDUSTRYTYPES || i->town != this->industry->town) continue;
uint dist = DistanceManhattan(this->tile, i->location.tile);
if (dist < (uint)cache->distances[i->type]) cache->distances[i->type] = (uint16)dist;
cache->counts[i->type] = ClampTo<uint8>(cache->counts[i->type] + 1);
}
}
closest_dist = cache->distances[ind_index];
count = cache->counts[ind_index];
} else {
/* Count only those who match the same industry type and layout filter
* Unfortunately, we have to do it manually */

View File

@ -16,6 +16,11 @@ struct IndustryLocationDistanceCache {
uint16 distances[NUM_INDUSTRYTYPES];
};
struct IndustryLocationDistanceAndCountCache {
uint16 distances[NUM_INDUSTRYTYPES];
uint8 counts[NUM_INDUSTRYTYPES];
};
/** Resolver for industry scopes. */
struct IndustriesScopeResolver : public ScopeResolver {
TileIndex tile; ///< Tile owned by the industry.
@ -24,6 +29,7 @@ struct IndustriesScopeResolver : public ScopeResolver {
IndustryType type; ///< Type of the industry.
mutable std::unique_ptr<IndustryLocationDistanceCache> location_distance_cache;
mutable std::unique_ptr<IndustryLocationDistanceAndCountCache> town_location_distance_cache;
/**
* Scope resolver for industries.