Skip to content

Commit

Permalink
Purchase missing keys and scrolls as part of the VendorRefill method
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorc committed Jan 15, 2025
1 parent 566c840 commit b278aa9
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 57 deletions.
8 changes: 0 additions & 8 deletions internal/action/town.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ func PreRun(firstRun bool) error {
// Refill pots, sell, buy etc
VendorRefill(false, true)

// Restock keys if the above VendorRefill missed them
RestockKeys()

// Restock tomes if the above VendorRefill missed them
RestockTomes()

// Gamble
Gamble()

Expand Down Expand Up @@ -83,8 +77,6 @@ func InRunReturnTownRoutine() error {
IdentifyAll(false)

VendorRefill(false, true)
RestockKeys()
RestockTomes()
Stash(false)
Gamble()
Stash(false)
Expand Down
97 changes: 71 additions & 26 deletions internal/action/vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,57 @@ import (
"github.com/hectorgimenez/d2go/pkg/data/stat"
)

func openTradeWindow(vendorNPC npc.ID) error {
ctx := context.Get()

err := InteractNPC(vendorNPC)
if err != nil {
return err
}

// Jamella trade button is the first one
if vendorNPC == npc.Jamella {
ctx.HID.KeySequence(win.VK_HOME, win.VK_RETURN)
} else {
ctx.HID.KeySequence(win.VK_HOME, win.VK_DOWN, win.VK_RETURN)
}

SwitchStashTab(4)
ctx.RefreshGameData()

return nil
}

// Act 1 Vendors:
// Potions: Akara
// Keys: Akara
// Scrolls: Akara
// Arrows/Bolts: Charsi

// Act 2 Vendors:
// Potions: Lysander
// Keys: Lysander
// Scrolls: Drognan
// Arrows/Bolts: Fara

// Act 3 Vendors:
// Potions: Ormus
// Keys: Hratli
// Scrolls: Ormus
// Arrows/Bolts: Hratli

// Act 4 Vendors:
// Potions: Jamella
// Keys: Jamella
// Scrolls: Jamella
// Arrows/Bolts: Halbu

// Act 5 Vendors:
// Potions: Malah
// Keys: Malah
// Scrolls: Malah
// Arrows/Bolts: Larzuk

func VendorRefill(forceRefill, sellJunk bool) error {
ctx := context.Get()
ctx.SetLastAction("VendorRefill")
Expand All @@ -32,32 +83,37 @@ func VendorRefill(forceRefill, sellJunk bool) error {
vendorNPC = npc.Lysander
}
}
err := InteractNPC(vendorNPC)

err := openTradeWindow(vendorNPC)
if err != nil {
return err
}

// Jamella trade button is the first one
if vendorNPC == npc.Jamella {
ctx.HID.KeySequence(win.VK_HOME, win.VK_RETURN)
} else {
ctx.HID.KeySequence(win.VK_HOME, win.VK_DOWN, win.VK_RETURN)
}

SwitchStashTab(4)
ctx.RefreshGameData()
town.BuyConsumables(forceRefill)

if sellJunk {
town.SellJunk()
}

// At this point we are guaranteed to have purchased potions, as the selected vendorNPC will always have these.
// Depending on the act, we may still need keys or scrolls.

if town.ShouldBuyTPs() || town.ShouldBuyIDs() {
restockTomes()
}

if ctx.Data.PlayerUnit.Class != data.Assassin {
_, shouldBuyKeys := town.ShouldBuyKeys()
if shouldBuyKeys {
restockKeys()
}
}

return step.CloseAllMenus()
}

func RestockTomes() error {
func restockTomes() error {
ctx := context.Get()
ctx.SetLastAction("RestockTomes")

shouldBuyTPs := town.ShouldBuyTPs()
shouldBuyIDs := town.ShouldBuyIDs()
Expand Down Expand Up @@ -88,16 +144,11 @@ func RestockTomes() error {
return nil
}

err := InteractNPC(vendorNPC)
err := openTradeWindow(vendorNPC)
if err != nil {
return err
}

ctx.HID.KeySequence(win.VK_HOME, win.VK_DOWN, win.VK_RETURN)

SwitchStashTab(4)
ctx.RefreshGameData()

if shouldBuyTPs {
town.BuyTPs()
}
Expand All @@ -109,9 +160,8 @@ func RestockTomes() error {
return nil
}

func RestockKeys() error {
func restockKeys() error {
ctx := context.Get()
ctx.SetLastAction("RestockKeys")

if ctx.Data.PlayerUnit.Class == data.Assassin {
return nil
Expand Down Expand Up @@ -145,16 +195,11 @@ func RestockKeys() error {
return nil
}

err := InteractNPC(vendorNPC)
err := openTradeWindow(vendorNPC)
if err != nil {
return err
}

ctx.HID.KeySequence(win.VK_HOME, win.VK_DOWN, win.VK_RETURN)

SwitchStashTab(4)
ctx.RefreshGameData()

if itm, found := ctx.Data.Inventory.Find(item.Key, item.LocationVendor); found {
ctx.Logger.Debug("Vendor with keys detected, provisioning...")

Expand Down
59 changes: 36 additions & 23 deletions internal/town/shop_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,37 @@ import (
"github.com/hectorgimenez/koolo/internal/ui"
)


func BuyTPs() {
ctx := context.Get()

if _, found := ctx.Data.Inventory.Find(item.TomeOfTownPortal, item.LocationInventory); !found {
ctx.Logger.Info("TP Tome not found, buying one...")
if itm, itmFound := ctx.Data.Inventory.Find(item.TomeOfTownPortal, item.LocationVendor); itmFound {
BuyItem(itm, 1)
}
}
ctx.Logger.Debug("Filling TP Tome...")
if itm, found := ctx.Data.Inventory.Find(item.ScrollOfTownPortal, item.LocationVendor); found {
BuyFullStack(itm)
}
}

func BuyIDs() {
ctx := context.Get()

if _, found := ctx.Data.Inventory.Find(item.TomeOfIdentify, item.LocationInventory); !found {
ctx.Logger.Info("ID Tome not found, buying one...")
if itm, itmFound := ctx.Data.Inventory.Find(item.TomeOfIdentify, item.LocationVendor); itmFound {
BuyItem(itm, 1)
}
}
ctx.Logger.Debug("Filling IDs Tome...")
if itm, found := ctx.Data.Inventory.Find(item.ScrollOfIdentify, item.LocationVendor); found {
BuyFullStack(itm)
}
}

func BuyConsumables(forceRefill bool) {
ctx := context.Get()

Expand All @@ -39,29 +70,11 @@ func BuyConsumables(forceRefill bool) {
}

if ShouldBuyTPs() || forceRefill {
if _, found := ctx.Data.Inventory.Find(item.TomeOfTownPortal, item.LocationInventory); !found {
ctx.Logger.Info("TP Tome not found, buying one...")
if itm, itmFound := ctx.Data.Inventory.Find(item.TomeOfTownPortal, item.LocationVendor); itmFound {
BuyItem(itm, 1)
}
}
ctx.Logger.Debug("Filling TP Tome...")
if itm, found := ctx.Data.Inventory.Find(item.ScrollOfTownPortal, item.LocationVendor); found {
buyFullStack(itm)
}
BuyTPs()
}

if ShouldBuyIDs() || forceRefill {
if _, found := ctx.Data.Inventory.Find(item.TomeOfIdentify, item.LocationInventory); !found {
ctx.Logger.Info("ID Tome not found, buying one...")
if itm, itmFound := ctx.Data.Inventory.Find(item.TomeOfIdentify, item.LocationVendor); itmFound {
BuyItem(itm, 1)
}
}
ctx.Logger.Debug("Filling IDs Tome...")
if itm, found := ctx.Data.Inventory.Find(item.ScrollOfIdentify, item.LocationVendor); found {
buyFullStack(itm)
}
BuyIDs()
}

keyQuantity, shouldBuyKeys := ShouldBuyKeys()
Expand All @@ -71,7 +84,7 @@ func BuyConsumables(forceRefill bool) {

qty, _ := itm.FindStat(stat.Quantity, 0)
if (qty.Value + keyQuantity) <= 12 {
buyFullStack(itm)
BuyFullStack(itm)
}
}
}
Expand Down Expand Up @@ -113,7 +126,7 @@ func ShouldBuyIDs() bool {
func ShouldBuyKeys() (int, bool) {
keys, found := context.Get().Data.Inventory.Find(item.Key, item.LocationInventory)
if !found {
return 12, false
return 12, true
}

qty, found := keys.FindStat(stat.Quantity, 0)
Expand Down Expand Up @@ -154,7 +167,7 @@ func BuyItem(i data.Item, quantity int) {
}
}

func buyFullStack(i data.Item) {
func BuyFullStack(i data.Item) {
screenPos := ui.GetScreenCoordsForItem(i)

context.Get().HID.ClickWithModifier(game.RightButton, screenPos.X, screenPos.Y, game.ShiftKey)
Expand Down

0 comments on commit b278aa9

Please sign in to comment.