From 340189b2b2060d1bc56f1be533b6b5b50567d7f4 Mon Sep 17 00:00:00 2001 From: vietdungdev Date: Mon, 25 May 2026 11:29:08 +0700 Subject: [PATCH] improve 2 --- pkg/nip/rule.go | 55 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/pkg/nip/rule.go b/pkg/nip/rule.go index 79375f2..c0ca8c0 100644 --- a/pkg/nip/rule.go +++ b/pkg/nip/rule.go @@ -572,28 +572,49 @@ func (r Rule) ValidateStats() error { } func evaluateClassSkillsSum(it data.Item) int { - // Check all class skills stats - totalClassSkills := 0 - - for layer := 0; layer <= maxClassSkillsLayer; layer++ { - if itemStat, found := it.FindStat(stat.AddClassSkills, layer); found && itemStat.Value > 0 { - totalClassSkills += itemStat.Value - } - } - - return totalClassSkills + return evaluatePositiveStatSum(it, stat.AddClassSkills, maxClassSkillsLayer) } -func evaluateSkillTabSum(it data.Item) int { - // Check all skill tab stats - totalSkillTabs := 0 - for layer := 0; layer <= maxSkillTabLayer; layer++ { - if itemStat, found := it.FindStat(stat.AddSkillTab, layer); found && itemStat.Value > 0 { - totalSkillTabs += itemStat.Value +func evaluateSkillTabSum(it data.Item) int { + return evaluatePositiveStatSum(it, stat.AddSkillTab, maxSkillTabLayer) +} + +const aggregateLayerFastPathLimit = 64 + +func evaluatePositiveStatSum(it data.Item, statID stat.ID, maxLayer int) int { + // Current aggregate aliases fit in a small stack bitmap; retain the general + // lookup path if additional layers later exceed that bound. + if maxLayer >= aggregateLayerFastPathLimit { + total := 0 + for layer := 0; layer <= maxLayer; layer++ { + if itemStat, found := it.FindStat(statID, layer); found && itemStat.Value > 0 { + total += itemStat.Value + } } + return total } - return totalSkillTabs + var foundLayers [aggregateLayerFastPathLimit]bool + total := 0 + for _, itemStat := range it.Stats { + if itemStat.ID != statID || itemStat.Layer < 0 || itemStat.Layer > maxLayer || foundLayers[itemStat.Layer] { + continue + } + foundLayers[itemStat.Layer] = true + if itemStat.Value > 0 { + total += itemStat.Value + } + } + for _, itemStat := range it.BaseStats { + if itemStat.ID != statID || itemStat.Layer < 0 || itemStat.Layer > maxLayer || foundLayers[itemStat.Layer] { + continue + } + foundLayers[itemStat.Layer] = true + if itemStat.Value > 0 { + total += itemStat.Value + } + } + return total } func maxAliasLayer(statID stat.ID) int {