Skip to content

Commit

Permalink
Add Location struct and fixed possible panic
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorgimenez committed May 19, 2024
1 parent 2a5f318 commit e4b4f8e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
31 changes: 17 additions & 14 deletions pkg/data/item/location.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package item

type Location string
type LocationType string

type Location struct {
LocationType
Page int
}

const (
LocationEquipped Location = "equipped"
LocationStash Location = "stash"
LocationSharedStash1 Location = "shared_stash_1"
LocationSharedStash2 Location = "shared_stash_2"
LocationSharedStash3 Location = "shared_stash_3"
LocationBelt Location = "belt"
LocationInventory Location = "inventory"
LocationCube Location = "cube"
LocationVendor Location = "vendor"
LocationGround Location = "ground"
LocationSocket Location = "socket"
LocationUnknown Location = "unknown"
LocationCursor Location = "cursor"
LocationEquipped LocationType = "equipped"
LocationStash LocationType = "stash"
LocationSharedStash LocationType = "shared_stash"
LocationBelt LocationType = "belt"
LocationInventory LocationType = "inventory"
LocationCube LocationType = "cube"
LocationVendor LocationType = "vendor"
LocationGround LocationType = "ground"
LocationSocket LocationType = "socket"
LocationUnknown LocationType = "unknown"
LocationCursor LocationType = "cursor"
)
5 changes: 2 additions & 3 deletions pkg/data/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func (i Inventory) Find(name item.Name, locations ...item.Location) (Item, bool)
return Item{}, false
}

func (i Inventory) ByLocation(locations ...item.Location) []Item {
func (i Inventory) ByLocation(locations ...item.LocationType) []Item {
var items []Item

for _, it := range i.AllItems {
for _, l := range locations {
if it.Location == l {
if it.Location.LocationType == l {
items = append(items, it)
}
}
Expand All @@ -56,7 +56,6 @@ type Item struct {
Quality item.Quality
Position Position
Location item.Location
Page int // Used for shared stash
Ethereal bool
IsHovered bool
BaseStats stat.Stats
Expand Down
19 changes: 14 additions & 5 deletions pkg/memory/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func (gd *GameReader) Inventory(rawPlayerUnits RawPlayerUnits, hover data.HoverD
stashedGold := [4]int{}
stashedGold[0] = mainPlayerStashedGold.Value
for i, puKey := range stashPlayerUnitOrder {
if i > 3 {
break
}
stashGold, _ := stashPlayerUnits[puKey].BaseStats.FindStat(stat.StashGold, 0)
stashedGold[i+1] = stashGold.Value
}
Expand Down Expand Up @@ -93,17 +96,19 @@ func (gd *GameReader) Inventory(rawPlayerUnits RawPlayerUnits, hover data.HoverD
location := item.LocationUnknown
switch itemLoc {
case 0:
// Offline only
if itemOwnerNPC == 2 || itemOwnerNPC == uint(stashPlayerUnits[stashPlayerUnitOrder[0]].UnitID) {
location = item.LocationSharedStash1
location = item.LocationSharedStash
invPage = 1
break
}
if itemOwnerNPC == 3 || itemOwnerNPC == uint(stashPlayerUnits[stashPlayerUnitOrder[1]].UnitID) {
location = item.LocationSharedStash2
location = item.LocationSharedStash
invPage = 2
break
}
if itemOwnerNPC == 4 || itemOwnerNPC == uint(stashPlayerUnits[stashPlayerUnitOrder[2]].UnitID) {
location = item.LocationSharedStash3
location = item.LocationSharedStash
invPage = 3
break
}

Expand All @@ -116,6 +121,7 @@ func (gd *GameReader) Inventory(rawPlayerUnits RawPlayerUnits, hover data.HoverD
}
if data.UnitID(itemOwnerNPC) == mainPlayer.UnitID || itemOwnerNPC == 1 {
location = item.LocationStash
invPage = 0
break
}
case 1:
Expand All @@ -133,7 +139,10 @@ func (gd *GameReader) Inventory(rawPlayerUnits RawPlayerUnits, hover data.HoverD
location = item.LocationCursor
}

itm.Location = location
itm.Location = item.Location{
LocationType: location,
Page: int(invPage),
}

// We don't care about the inventory we don't know where they are, probably previous games or random crap
if location != item.LocationUnknown {
Expand Down

0 comments on commit e4b4f8e

Please sign in to comment.