diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 0a2805295c..94270717c5 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -5604,6 +5604,46 @@ static const SpriteGroup *CreateGroupFromGroupID(byte feature, byte setid, byte return new ResultSpriteGroup(spriteset_start, num_sprites); } +static void ProcessDeterministicSpriteGroupRanges(const std::vector &ranges, std::vector &ranges_out, const SpriteGroup *default_group) +{ + /* Sort ranges ascending. When ranges overlap, this may required clamping or splitting them */ + std::vector bounds; + for (uint i = 0; i < ranges.size(); i++) { + bounds.push_back(ranges[i].low); + if (ranges[i].high != UINT32_MAX) bounds.push_back(ranges[i].high + 1); + } + std::sort(bounds.begin(), bounds.end()); + bounds.erase(std::unique(bounds.begin(), bounds.end()), bounds.end()); + + std::vector target; + for (uint j = 0; j < bounds.size(); ++j) { + uint32 v = bounds[j]; + const SpriteGroup *t = default_group; + for (uint i = 0; i < ranges.size(); i++) { + if (ranges[i].low <= v && v <= ranges[i].high) { + t = ranges[i].group; + break; + } + } + target.push_back(t); + } + assert(target.size() == bounds.size()); + + for (uint j = 0; j < bounds.size(); ) { + if (target[j] != default_group) { + DeterministicSpriteGroupRange &r = ranges_out.emplace_back(); + r.group = target[j]; + r.low = bounds[j]; + while (j < bounds.size() && target[j] == r.group) { + j++; + } + r.high = j < bounds.size() ? bounds[j] - 1 : UINT32_MAX; + } else { + j++; + } + } +} + /* Action 0x02 */ static void NewSpriteGroup(ByteReader *buf) { diff --git a/src/newgrf_internal.h b/src/newgrf_internal.h index 242c171e43..8e704fb361 100644 --- a/src/newgrf_internal.h +++ b/src/newgrf_internal.h @@ -249,8 +249,7 @@ struct VarAction2OptimiseState { const SpriteGroup *PruneTargetSpriteGroup(const SpriteGroup *result); void OptimiseVarAction2Adjust(VarAction2OptimiseState &state, const GrfSpecFeature feature, const byte varsize, DeterministicSpriteGroup *group, DeterministicSpriteGroupAdjust &adjust); -void ProcessDeterministicSpriteGroupRanges(const std::vector &ranges, std::vector &ranges_out, const SpriteGroup *default_group); void OptimiseVarAction2DeterministicSpriteGroup(VarAction2OptimiseState &state, const GrfSpecFeature feature, const byte varsize, DeterministicSpriteGroup *group); void HandleVarAction2OptimisationPasses(); -#endif /* NEWGRF_INTERNAL_H */ \ No newline at end of file +#endif /* NEWGRF_INTERNAL_H */ diff --git a/src/newgrf_optimiser.cpp b/src/newgrf_optimiser.cpp index c82d928642..283c5142af 100644 --- a/src/newgrf_optimiser.cpp +++ b/src/newgrf_optimiser.cpp @@ -2698,46 +2698,6 @@ void HandleVarAction2OptimisationPasses() } } -void ProcessDeterministicSpriteGroupRanges(const std::vector &ranges, std::vector &ranges_out, const SpriteGroup *default_group) -{ - /* Sort ranges ascending. When ranges overlap, this may required clamping or splitting them */ - std::vector bounds; - for (uint i = 0; i < ranges.size(); i++) { - bounds.push_back(ranges[i].low); - if (ranges[i].high != UINT32_MAX) bounds.push_back(ranges[i].high + 1); - } - std::sort(bounds.begin(), bounds.end()); - bounds.erase(std::unique(bounds.begin(), bounds.end()), bounds.end()); - - std::vector target; - for (uint j = 0; j < bounds.size(); ++j) { - uint32 v = bounds[j]; - const SpriteGroup *t = default_group; - for (uint i = 0; i < ranges.size(); i++) { - if (ranges[i].low <= v && v <= ranges[i].high) { - t = ranges[i].group; - break; - } - } - target.push_back(t); - } - assert(target.size() == bounds.size()); - - for (uint j = 0; j < bounds.size(); ) { - if (target[j] != default_group) { - DeterministicSpriteGroupRange &r = ranges_out.emplace_back(); - r.group = target[j]; - r.low = bounds[j]; - while (j < bounds.size() && target[j] == r.group) { - j++; - } - r.high = j < bounds.size() ? bounds[j] - 1 : UINT32_MAX; - } else { - j++; - } - } -} - const SpriteGroup *PruneTargetSpriteGroup(const SpriteGroup *result) { if (HasGrfOptimiserFlag(NGOF_NO_OPT_VARACT2) || HasGrfOptimiserFlag(NGOF_NO_OPT_VARACT2_GROUP_PRUNE)) return result;