VarAction2: Cache current town industry counts/distances for var 67/68
This commit is contained in:
@@ -145,6 +145,23 @@ uint32 IndustriesScopeResolver::GetCountAndDistanceOfClosestInstance(byte param_
|
|||||||
* In either case, just do the regular var67 */
|
* In either case, just do the regular var67 */
|
||||||
if (mask & 0xFFFF) closest_dist = this->GetClosestIndustry(ind_index);
|
if (mask & 0xFFFF) closest_dist = this->GetClosestIndustry(ind_index);
|
||||||
if (mask & 0xFF0000) count = ClampTo<byte>(Industry::GetIndustryTypeCount(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 {
|
} else {
|
||||||
/* Count only those who match the same industry type and layout filter
|
/* Count only those who match the same industry type and layout filter
|
||||||
* Unfortunately, we have to do it manually */
|
* Unfortunately, we have to do it manually */
|
||||||
|
@@ -16,6 +16,11 @@ struct IndustryLocationDistanceCache {
|
|||||||
uint16 distances[NUM_INDUSTRYTYPES];
|
uint16 distances[NUM_INDUSTRYTYPES];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct IndustryLocationDistanceAndCountCache {
|
||||||
|
uint16 distances[NUM_INDUSTRYTYPES];
|
||||||
|
uint8 counts[NUM_INDUSTRYTYPES];
|
||||||
|
};
|
||||||
|
|
||||||
/** Resolver for industry scopes. */
|
/** Resolver for industry scopes. */
|
||||||
struct IndustriesScopeResolver : public ScopeResolver {
|
struct IndustriesScopeResolver : public ScopeResolver {
|
||||||
TileIndex tile; ///< Tile owned by the industry.
|
TileIndex tile; ///< Tile owned by the industry.
|
||||||
@@ -24,6 +29,7 @@ struct IndustriesScopeResolver : public ScopeResolver {
|
|||||||
IndustryType type; ///< Type of the industry.
|
IndustryType type; ///< Type of the industry.
|
||||||
|
|
||||||
mutable std::unique_ptr<IndustryLocationDistanceCache> location_distance_cache;
|
mutable std::unique_ptr<IndustryLocationDistanceCache> location_distance_cache;
|
||||||
|
mutable std::unique_ptr<IndustryLocationDistanceAndCountCache> town_location_distance_cache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scope resolver for industries.
|
* Scope resolver for industries.
|
||||||
|
Reference in New Issue
Block a user