improve item.go 2
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user