mirror of
https://github.com/JGRennison/OpenTTD-patches.git
synced 2024-10-31 15:20:10 +00:00
VarAction2: Cache current town industry counts/distances for var 67/68
This commit is contained in:
parent
48f66306e5
commit
f57088b261
@ -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 */
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user