Skip to content

Commit

Permalink
Merge branch 'main' into Clear_level
Browse files Browse the repository at this point in the history
  • Loading branch information
elobo91 authored Jan 7, 2025
2 parents f4cb1ed + cee154d commit 5b388bb
Show file tree
Hide file tree
Showing 18 changed files with 284 additions and 166 deletions.
9 changes: 5 additions & 4 deletions config/template/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ commandLineArgs: '' # Command line arguments for D2
killD2OnStop: true # Terminate D2 process on bot stop
classicMode: false # Set to true to use legacy graphics
closeMiniPanel: false # Set to true to close the mini panel at start of game in legacy graphics
hidePortraits: true # Set to true to hide mercenary and other players portraits (avatar)
enableCubeRecipes: true # Enable cubing of flawlesses and tokens

health: # Healing configuration, all values in %
Expand Down Expand Up @@ -63,7 +64,7 @@ game:
focusOnElitePacks: false
spider_cavern:
openChests: true
focusOnElitePacks: false
focusOnElitePacks: false
pit:
# default - Outer Cloister -> Monastery Gates -> Tamoe Highland
moveThroughBlackMarsh: false # Use Black Marsh -> Tamoe Highland route
Expand Down Expand Up @@ -187,6 +188,6 @@ gambling:
items: [ coronet, amulet, ring ] # Items to gamble, same value as [name] in pickit files.

backtotown:
noHpPotions: true
noMpPotions: false
mercDied: true
noHpPotions: true
noMpPotions: false
mercDied: true
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/bwmarrin/discordgo v0.28.1
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/gorilla/websocket v1.5.3
github.com/hectorgimenez/d2go v0.0.0-20241216152730-04f45d306978
github.com/hectorgimenez/d2go v0.0.0-20250106015039-8bce23b03973
github.com/inkeliz/gowebview v1.0.1
github.com/lxn/win v0.0.0-20210218163916-a377121e959e
github.com/otiai10/copy v1.14.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aN
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hectorgimenez/d2go v0.0.0-20241216152730-04f45d306978 h1:nfC7EZCCDEXNisTgE8LuCPRRxBbtnhP3hX1nxmZaS3s=
github.com/hectorgimenez/d2go v0.0.0-20241216152730-04f45d306978/go.mod h1:EOVayMaK8D13wsZiZ6n8AK3+Qflm1wHZsCqnzlVIci0=
github.com/hectorgimenez/d2go v0.0.0-20241220085033-444f4b0e9b01 h1:2sXMzraTLYD5E6eY9sHA6OkDfYrqu3l2sbgAXENk7cg=
github.com/hectorgimenez/d2go v0.0.0-20241220085033-444f4b0e9b01/go.mod h1:EOVayMaK8D13wsZiZ6n8AK3+Qflm1wHZsCqnzlVIci0=
github.com/hectorgimenez/d2go v0.0.0-20241230114702-591fbf16055e h1:gal4qtJv+5ue0NB+PxIN4E8bx3f2p3w7aSRUCkq/D+s=
github.com/hectorgimenez/d2go v0.0.0-20241230114702-591fbf16055e/go.mod h1:EOVayMaK8D13wsZiZ6n8AK3+Qflm1wHZsCqnzlVIci0=
github.com/hectorgimenez/d2go v0.0.0-20250106015039-8bce23b03973 h1:dRqPFWAf0yb1T2d/NQLOLTJkega8E8MZlVUuMKOrPkA=
github.com/hectorgimenez/d2go v0.0.0-20250106015039-8bce23b03973/go.mod h1:EOVayMaK8D13wsZiZ6n8AK3+Qflm1wHZsCqnzlVIci0=
github.com/inkeliz/gowebview v1.0.1 h1:4gpLE2qt4kV3DB+xHkHKUeLLiGPN5Xw3or9A3hVqYyA=
github.com/inkeliz/gowebview v1.0.1/go.mod h1:4SNjXp/fogE11MwvJD67kMBmSObY2BBqinEgH8+8eM8=
github.com/inkeliz/w32 v1.0.2 h1:Es8Bmw9ApOY0PVRpGs7wsqIKdK5C3xBkP5TOATfVmtU=
Expand Down
35 changes: 27 additions & 8 deletions internal/action/clear_area.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,37 @@ func ClearAreaAroundPosition(pos data.Position, radius int, filter data.MonsterF
ctx := context.Get()
ctx.SetLastAction("ClearAreaAroundPosition")

return ctx.Char.KillMonsterSequence(func(d game.Data) (data.UnitID, bool) {
for _, m := range d.Monsters.Enemies(filter) {
distanceToTarget := pather.DistanceFromPoint(pos, m.Position)
if ctx.Data.AreaData.IsWalkable(m.Position) && distanceToTarget <= radius {
return m.UnitID, true
for {
ctx.PauseIfNotPriority()

// Check for closest monster within radius - monsters are already sorted by distance
err := ctx.Char.KillMonsterSequence(func(d game.Data) (data.UnitID, bool) {
for _, m := range d.Monsters.Enemies(filter) {
dist := pather.DistanceFromPoint(pos, m.Position)
if ctx.Data.AreaData.IsWalkable(m.Position) && dist <= radius {
return m.UnitID, true
}
}
return 0, false
}, nil)

if err != nil {
return err
}

return 0, false
}, nil)
// If no monsters found within radius, we're done
found := false
for _, m := range ctx.Data.Monsters.Enemies(filter) {
if pather.DistanceFromPoint(pos, m.Position) <= radius {
found = true
break
}
}
if !found {
return nil
}
}
}

func ClearThroughPath(pos data.Position, radius int, filter data.MonsterFilter) error {
ctx := context.Get()

Expand Down
48 changes: 28 additions & 20 deletions internal/action/item_pickup.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func ItemPickup(maxDistance int) error {
continue
}

// Clear enemy monsters near the item
ClearAreaAroundPosition(itemToPickup.Position, 3, data.MonsterAnyFilter())
// First clear the area around item
ClearAreaAroundPosition(itemToPickup.Position, 4, data.MonsterAnyFilter())

ctx.Logger.Debug(fmt.Sprintf(
"Item Detected: %s [%d] at X:%d Y:%d",
Expand All @@ -77,30 +77,38 @@ func ItemPickup(maxDistance int) error {
itemToPickup.Position.Y,
))

err := MoveToCoords(itemToPickup.Position)
if err != nil {
ctx.Logger.Warn("Failed moving closer to item, trying to pickup anyway")
}
// Try moving closer if no line of sight
if !ctx.PathFinder.LineOfSight(ctx.Data.PlayerUnit.Position, itemToPickup.Position) {
ctx.Logger.Debug("No line of sight to item, moving closer",
slog.String("item", itemToPickup.Desc().Name))

err = step.PickupItem(itemToPickup)
if err == nil {
continue // Item picked up successfully, move to next item
if err := step.MoveTo(itemToPickup.Position, step.WithDistanceToFinish(1)); err != nil {
continue
}
}

if errors.Is(err, step.ErrItemTooFar) {
ctx.Logger.Debug("Item is too far away, retrying...")
continue
// Now try normal pickup sequence
if err := step.MoveTo(itemToPickup.Position, step.WithDistanceToFinish(3)); err == nil {
if err := step.PickupItem(itemToPickup); err != nil {
if errors.Is(err, step.ErrItemTooFar) {
// If too far, try with distance 2
ctx.Logger.Debug("Item is too far away, retrying...")
if err := step.MoveTo(itemToPickup.Position, step.WithDistanceToFinish(2)); err == nil {
if pickErr := step.PickupItem(itemToPickup); pickErr != nil {
ctx.Logger.Warn(
"Failed to pickup item blacklisting it",
slog.String("itemName", itemToPickup.Desc().Name),
slog.Int("unitID", int(itemToPickup.UnitID)),
)
ctx.CurrentGame.BlacklistedItems = append(ctx.CurrentGame.BlacklistedItems, itemToPickup)
}
}
}
}
}

// If it's any other error, blacklist the item
ctx.CurrentGame.BlacklistedItems = append(ctx.CurrentGame.BlacklistedItems, itemToPickup)
ctx.Logger.Warn(
"Failed picking up item, blacklisting it",
slog.String("itemName", itemToPickup.Desc().Name),
slog.Int("unitID", int(itemToPickup.UnitID)),
)
}
}

func GetItemsToPickup(maxDistance int) []data.Item {
ctx := context.Get()
ctx.SetLastAction("GetItemsToPickup")
Expand Down
28 changes: 26 additions & 2 deletions internal/action/stash.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"slices"
"strconv"
"strings"

"github.com/hectorgimenez/d2go/pkg/data"
"github.com/hectorgimenez/d2go/pkg/data/area"
Expand Down Expand Up @@ -281,14 +282,37 @@ func stashItemAction(i data.Item, rule string, ruleFile string, skipLogging bool
}
}

// Don't log items that we already have in inventory during first run
if !skipLogging {
// Don't log items that we already have in inventory during first run or that we don't want to notify about (gems, low runes .. etc)
if !skipLogging && shouldNotifyAboutStashing(i) && ruleFile != "" {
event.Send(event.ItemStashed(event.WithScreenshot(ctx.Name, fmt.Sprintf("Item %s [%d] stashed", i.Name, i.Quality), screenshot), data.Drop{Item: i, Rule: rule, RuleFile: ruleFile}))
}

return true
}

func shouldNotifyAboutStashing(i data.Item) bool {
ctx := context.Get()

ctx.Logger.Debug(fmt.Sprintf("Checking if we should notify about stashing %s %v", i.Name, i.Desc()))
// Don't notify about gems
if strings.Contains(i.Desc().Type, "gem") {
return false
}

// Skip low runes (below lem)
lowRunes := []string{"elrune", "eldrune", "tirrune", "nefrune", "ethrune", "ithrune", "talrune", "ralrune", "ortrune", "thulrune", "amnrune", "solrune", "shaelrune", "dolrune", "helrune", "iorune", "lumrune", "korune", "falrune"}
if i.Desc().Type == item.TypeRune {
itemName := strings.ToLower(string(i.Name))
for _, runeName := range lowRunes {
if itemName == runeName {
return false
}
}
}

return true
}

func clickStashGoldBtn() {
ctx := context.Get()
ctx.SetLastStep("clickStashGoldBtn")
Expand Down
5 changes: 3 additions & 2 deletions internal/action/step/interact_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package step

import (
"fmt"
"time"

"github.com/hectorgimenez/d2go/pkg/data"
"github.com/hectorgimenez/d2go/pkg/data/area"
"github.com/hectorgimenez/d2go/pkg/data/mode"
Expand All @@ -11,7 +13,6 @@ import (
"github.com/hectorgimenez/koolo/internal/town"
"github.com/hectorgimenez/koolo/internal/ui"
"github.com/hectorgimenez/koolo/internal/utils"
"time"
)

const (
Expand Down Expand Up @@ -74,7 +75,7 @@ func InteractObject(obj data.Object, isCompletedFn func() bool) error {
ctx.PauseIfNotPriority()

if interactionAttempts >= maxInteractionAttempts || mouseOverAttempts >= 20 {
return fmt.Errorf("failed interacting with object")
return fmt.Errorf("[%s] failed interacting with object [%v] in Area: [%s]", ctx.Name, obj.Name, ctx.Data.PlayerUnit.Area.Area().Name)
}

ctx.RefreshGameData()
Expand Down
33 changes: 29 additions & 4 deletions internal/action/step/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,32 @@ import (

const DistanceToFinishMoving = 4

func MoveTo(dest data.Position) error {
type MoveOpts struct {
distanceOverride *int
}

type MoveOption func(*MoveOpts)

// WithDistanceToFinish overrides the default DistanceToFinishMoving
func WithDistanceToFinish(distance int) MoveOption {
return func(opts *MoveOpts) {
opts.distanceOverride = &distance
}
}

func MoveTo(dest data.Position, options ...MoveOption) error {
// Initialize options
opts := &MoveOpts{}

// Apply any provided options
for _, o := range options {
o(opts)
}

minDistanceToFinishMoving := DistanceToFinishMoving
if opts.distanceOverride != nil {
minDistanceToFinishMoving = *opts.distanceOverride
}

ctx := context.Get()
ctx.SetLastStep("MoveTo")
Expand Down Expand Up @@ -44,7 +68,6 @@ func MoveTo(dest data.Position) error {
previousDistance := 0

for {
ctx.RefreshGameData()

// Pause the execution if the priority is not the same as the execution priority
ctx.PauseIfNotPriority()
Expand Down Expand Up @@ -112,8 +135,10 @@ func MoveTo(dest data.Position) error {
}

// This is a workaround to avoid the character to get stuck in the same position when the hitbox of the destination is too big
if distance < 20 && math.Abs(float64(previousDistance-distance)) < 4 {
minDistanceToFinishMoving += 4
if distance < 20 && math.Abs(float64(previousDistance-distance)) < DistanceToFinishMoving {
minDistanceToFinishMoving += DistanceToFinishMoving
} else if opts.distanceOverride != nil {
minDistanceToFinishMoving = *opts.distanceOverride
} else {
minDistanceToFinishMoving = DistanceToFinishMoving
}
Expand Down
Loading

0 comments on commit 5b388bb

Please sign in to comment.