Skip to content

Commit

Permalink
pather fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorgimenez committed May 15, 2024
1 parent dcf657b commit e0840d6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 61 deletions.
9 changes: 8 additions & 1 deletion internal/action/stash.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,14 @@ func (b *Builder) stashInventory(d game.Data, firstRun bool) {
}
for currentTab < 5 {
if b.stashItemAction(i, firstRun) {
r, _ := b.CharacterCfg.Runtime.Rules.EvaluateAll(i)
r, res := b.CharacterCfg.Runtime.Rules.EvaluateAll(i)

if res != nip.RuleResultFullMatch && firstRun {
b.Logger.Debug(
fmt.Sprintf("Item %s [%s] stashed because it was found in the inventory during the first run.", i.Desc().Name, i.Quality.ToString()),
)
break
}

b.Logger.Debug(
fmt.Sprintf("Item %s [%s] stashed", i.Desc().Name, i.Quality.ToString()),
Expand Down
16 changes: 7 additions & 9 deletions internal/action/tp_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import (
"github.com/hectorgimenez/koolo/internal/town"
)

func (b *Builder) ReturnTown() *StepChainAction {
return NewStepChain(func(d game.Data) (steps []step.Step) {
if d.PlayerUnit.Area.IsTown() {
return
}

return []step.Step{
step.OpenPortal(),
step.InteractObject(object.TownPortal, func(d game.Data) bool {
func (b *Builder) ReturnTown() *Chain {
return NewChain(func(d game.Data) []Action {
return []Action{
NewStepChain(func(d game.Data) (steps []step.Step) {
return []step.Step{step.OpenPortal()}
}),
b.InteractObject(object.TownPortal, func(d game.Data) bool {
return d.PlayerUnit.Area.IsTown()
}),
}
Expand Down
39 changes: 17 additions & 22 deletions internal/pather/path_finder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pather

import (
"fmt"
"math"
"math/rand"

Expand All @@ -13,12 +14,11 @@ import (
)

type PathFinder struct {
gr *game.MemoryReader
hid *game.HID
cfg *config.CharacterCfg
worldCache World
worldCachedArea area.ID
worldCachedSeed uint
gr *game.MemoryReader
hid *game.HID
cfg *config.CharacterCfg
worldCache World
worldCacheHash string
}

func NewPathFinder(gr *game.MemoryReader, hid *game.HID, cfg *config.CharacterCfg) *PathFinder {
Expand Down Expand Up @@ -108,36 +108,31 @@ func (pf *PathFinder) GetPath(d game.Data, to data.Position, blacklistedCoords .
collisionGrid[13][210] = false
}

for _, cord := range blacklistedCoords {
collisionGrid[cord[1]][cord[0]] = false
worldCacheHash := fmt.Sprintf("%d-%d-%d-%d", pf.gr.CachedMapSeed, d.PlayerUnit.Area, len(collisionGrid), len(collisionGrid[0]))
if pf.worldCacheHash != worldCacheHash {
pf.worldCache = parseWorld(collisionGrid, d)
pf.worldCacheHash = worldCacheHash
}

// Set Origin and Destination points
pf.worldCache.SetFrom(data.Position{X: fromX, Y: fromY})
pf.worldCache.SetTo(data.Position{X: toX, Y: toY})

// Add some padding to the origin/destination, sometimes when the origin or destination are close to a non-walkable
// area, pather is not able to calculate the path, so we add some padding around origin/dest to avoid this
// If character can not teleport if apply this hacky thing it will try to kill monsters across walls
if d.CanTeleport() {
for i := -3; i < 4; i++ {
for k := -3; k < 4; k++ {
if i == 0 && k == 0 {
continue
}

//collisionGrid[ensureValueInCG(fromY+i, len(collisionGrid))][ensureValueInCG(fromX+k, len(collisionGrid[0]))] = true
collisionGrid[ensureValueInCG(toY+i, len(collisionGrid))][ensureValueInCG(toX+k, len(collisionGrid[0]))] = true
pf.worldCache.SetTile(pf.worldCache.NewTile(KindPlain, ensureValueInCG(toX+k, len(collisionGrid[0])), ensureValueInCG(toY+i, len(collisionGrid))))
}
}
}

if pf.gr.CachedMapSeed != pf.worldCachedSeed || pf.worldCachedArea != d.PlayerUnit.Area || pf.worldCache.World == nil {
pf.worldCache = parseWorld(collisionGrid, d)
pf.worldCachedSeed = pf.gr.CachedMapSeed
pf.worldCachedArea = d.PlayerUnit.Area
for _, cord := range blacklistedCoords {
pf.worldCache.SetTile(pf.worldCache.NewTile(KindBlocker, cord[0], cord[1]))
}

// Set Origin and Destination points
pf.worldCache.SetFrom(data.Position{X: fromX, Y: fromY})
pf.worldCache.SetTo(data.Position{X: toX, Y: toY})

//W.renderPathImg(d, nil, collisionGridOffset)

p, distFloat, found := astar.Path(pf.worldCache.From(), pf.worldCache.To())
Expand Down
16 changes: 8 additions & 8 deletions internal/pather/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,6 @@ func (w *World) renderPathImg(d game.Data, path []astar.Pather, cgOffset data.Po
img.Set(rPosX, rPosY, color.RGBA{204, 204, 0, 255})
}

img.Set(w.From().X, w.From().Y, color.RGBA{
R: 255, G: 0, B: 0, A: 255,
})

img.Set(w.To().X, w.To().Y, color.RGBA{
R: 0, G: 0, B: 255, A: 255,
})

for _, o := range d.Objects {
oPosX, oPosY := relativePosition(d, o.Position, cgOffset)
if o.IsDoor() {
Expand All @@ -132,6 +124,14 @@ func (w *World) renderPathImg(d game.Data, path []astar.Pather, cgOffset data.Po
img.Set(mPosX, mPosY, color.RGBA{255, 0, 255, 255})
}

img.Set(w.From().X, w.From().Y, color.RGBA{
R: 255, G: 0, B: 0, A: 255,
})

img.Set(w.To().X, w.To().Y, color.RGBA{
R: 0, G: 0, B: 255, A: 255,
})

outFile, _ := os.Create("cg.png")
defer outFile.Close()
png.Encode(outFile, img)
Expand Down
32 changes: 11 additions & 21 deletions internal/run/diablo.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package run

import (
"github.com/hectorgimenez/d2go/pkg/data/npc"
"github.com/hectorgimenez/koolo/internal/config"
"github.com/hectorgimenez/koolo/internal/game"
"log/slog"
"slices"
"time"

"github.com/hectorgimenez/d2go/pkg/data/npc"
"github.com/hectorgimenez/koolo/internal/config"
"github.com/hectorgimenez/koolo/internal/game"

"github.com/hectorgimenez/d2go/pkg/data"
"github.com/hectorgimenez/d2go/pkg/data/area"
"github.com/hectorgimenez/d2go/pkg/data/object"
Expand Down Expand Up @@ -160,25 +161,14 @@ func (a Diablo) BuildActions() (actions []action.Action) {
}
return []action.Action{}
}),
action.NewStepChain(func(d game.Data) []step.Step {
a.logger.Debug("Trying to activate seal...", slog.Int("seal", sealNumber+1))
lastInteractionAt := time.Now()
return []step.Step{
step.InteractObject(seal, func(d game.Data) bool {
if obj, found := d.Objects.FindOne(seal); found {
if !obj.Selectable {
a.logger.Debug("Seal activated, waiting for elite group to spawn", slog.Int("seal", sealNumber+1))
}
return !obj.Selectable
}
if time.Since(lastInteractionAt) > time.Second*3 {
lastInteractionAt = time.Now()
a.PathFinder.RandomMovement()
time.Sleep(time.Second)
}
return false
}),
a.builder.InteractObject(seal, func(d game.Data) bool {
if obj, found := d.Objects.FindOne(seal); found {
if !obj.Selectable {
a.logger.Debug("Seal activated, waiting for elite group to spawn", slog.Int("seal", sealNumber+1))
}
return !obj.Selectable
}
return false
}),
)

Expand Down

0 comments on commit e0840d6

Please sign in to comment.