improve item.go 2

This commit is contained in:
vietdungdev
2026-05-25 12:07:17 +07:00
parent 15f204e2ff
commit 63e001982c
2 changed files with 30 additions and 15 deletions

View File

@@ -28,6 +28,17 @@ func (i Stats) FindStat(id ID, layer int) (Data, bool) {
return Data{}, false 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 { func (s ID) String() string {
return StringStats[s] return StringStats[s]
} }

View File

@@ -493,8 +493,13 @@ func (gd *GameReader) Inventory(rawPlayerUnits RawPlayerUnits, hover data.HoverD
} }
} }
// Build final inventory type itemDistance struct {
inventory.AllItems = make([]data.Item, len(allItems)) 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 { for i, itm := range allItems {
baseDesc := itm.Desc() baseDesc := itm.Desc()
maxReq := calculateItemLevelReq(itm, baseDesc) maxReq := calculateItemLevelReq(itm, baseDesc)
@@ -529,20 +534,19 @@ func (gd *GameReader) Inventory(rawPlayerUnits RawPlayerUnits, hover data.HoverD
break 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 sort.SliceStable(sortedItems, func(i, j int) bool {
if len(inventory.AllItems) > 0 { return sortedItems[i].distance < sortedItems[j].distance
// 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(inventory.AllItems, func(i, j int) bool { inventory.AllItems = make([]data.Item, len(sortedItems))
return distances[i] < distances[j] for i, itm := range sortedItems {
}) inventory.AllItems[i] = *itm.item
} }
inventory.Belt = belt inventory.Belt = belt
@@ -616,8 +620,8 @@ func (gd *GameReader) getItemStats(statsListExPtr uintptr) (stat.Stats, stat.Sta
for _, mStat := range modifierBaseStats { for _, mStat := range modifierBaseStats {
// Update fullStats // Update fullStats
if existingStat, found := fullStats.FindStat(mStat.ID, mStat.Layer); found { if idx, found := fullStats.FindStatIndex(mStat.ID, mStat.Layer); found {
existingStat.Value = mStat.Value fullStats[idx].Value = mStat.Value
} else { } else {
fullStats = append(fullStats, mStat) fullStats = append(fullStats, mStat)
} }