diff --git a/pkg/data/stat/stats.go b/pkg/data/stat/stats.go index 54e6cda..ee4cd29 100644 --- a/pkg/data/stat/stats.go +++ b/pkg/data/stat/stats.go @@ -28,6 +28,17 @@ func (i Stats) FindStat(id ID, layer int) (Data, bool) { return Data{}, false } + +func (i Stats) FindStatIndex(id ID, layer int) (int, bool) { + for idx, s := range i { + if s.ID == id && s.Layer == layer { + return idx, true + } + } + + return 0, false +} + func (s ID) String() string { return StringStats[s] } diff --git a/pkg/memory/item.go b/pkg/memory/item.go index dd11422..a6e1cd3 100644 --- a/pkg/memory/item.go +++ b/pkg/memory/item.go @@ -493,8 +493,13 @@ func (gd *GameReader) Inventory(rawPlayerUnits RawPlayerUnits, hover data.HoverD } } - // Build final inventory - inventory.AllItems = make([]data.Item, len(allItems)) + type itemDistance struct { + item *data.Item + distance int + } + + // Finalize items and retain the distance with the item that owns it. + sortedItems := make([]itemDistance, len(allItems)) for i, itm := range allItems { baseDesc := itm.Desc() maxReq := calculateItemLevelReq(itm, baseDesc) @@ -529,20 +534,19 @@ func (gd *GameReader) Inventory(rawPlayerUnits RawPlayerUnits, hover data.HoverD break } } - inventory.AllItems[i] = *itm + sortedItems[i] = itemDistance{ + item: itm, + distance: utils.DistanceFromPoint(mainPlayer.Position, itm.Position), + } } - // Sort items by distance if needed, using pre-calculated distances - if len(inventory.AllItems) > 0 { - // Pre-calculate distances to avoid recalculating in sort comparisons - distances := make([]int, len(inventory.AllItems)) - for i, invItem := range inventory.AllItems { - distances[i] = utils.DistanceFromPoint(mainPlayer.Position, invItem.Position) - } + sort.SliceStable(sortedItems, func(i, j int) bool { + return sortedItems[i].distance < sortedItems[j].distance + }) - sort.SliceStable(inventory.AllItems, func(i, j int) bool { - return distances[i] < distances[j] - }) + inventory.AllItems = make([]data.Item, len(sortedItems)) + for i, itm := range sortedItems { + inventory.AllItems[i] = *itm.item } inventory.Belt = belt @@ -616,8 +620,8 @@ func (gd *GameReader) getItemStats(statsListExPtr uintptr) (stat.Stats, stat.Sta for _, mStat := range modifierBaseStats { // Update fullStats - if existingStat, found := fullStats.FindStat(mStat.ID, mStat.Layer); found { - existingStat.Value = mStat.Value + if idx, found := fullStats.FindStatIndex(mStat.ID, mStat.Layer); found { + fullStats[idx].Value = mStat.Value } else { fullStats = append(fullStats, mStat) }