Skip to content

Commit

Permalink
attempt to fix the random movement when character stuck
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorgimenez committed May 23, 2024
1 parent 42d6e7b commit f742f51
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
4 changes: 3 additions & 1 deletion internal/action/step/move_to.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ func (m *MoveToStep) Run(d game.Data, container container.Container) error {
if m.path == nil || !m.cachePath(d) || stuck {
if stuck {
if len(m.path.AstarPather) == 0 {
container.PathFinder.RandomMovement()
randomPosX, randomPosY := pather.FindFirstWalkable(d.PlayerUnit.Position, d.AreaOrigin, d.CollisionGrid, 15)
screenX, screenY := container.PathFinder.GameCoordsToScreenCords(d.PlayerUnit.Position.X, d.PlayerUnit.Position.Y, randomPosX, randomPosY)
container.PathFinder.MoveCharacter(d, screenX, screenY)
m.lastRun = time.Now()

return nil
Expand Down
13 changes: 2 additions & 11 deletions internal/pather/path_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,6 @@ func (pf *PathFinder) GetClosestWalkablePath(d game.Data, dest data.Position, bl
}

func (pf *PathFinder) MoveThroughPath(d game.Data, p *Pather, distance int) {
//if len(p.AstarPather) == 0 {
// if teleport {
// hid.Click(hid.RightButton)
// } else {
// hid.PressKey(config.Config.Bindings.ForceMove)
// }
// return
//}

moveTo := p.AstarPather[0].(*Tile)
if distance > 0 && len(p.AstarPather) > distance {
moveTo = p.AstarPather[len(p.AstarPather)-distance].(*Tile)
Expand All @@ -210,11 +201,11 @@ func (pf *PathFinder) MoveThroughPath(d game.Data, p *Pather, distance int) {
}

if distance > 0 {
pf.moveCharacter(d, screenX, screenY)
pf.MoveCharacter(d, screenX, screenY)
}
}

func (pf *PathFinder) moveCharacter(d game.Data, x, y int) {
func (pf *PathFinder) MoveCharacter(d game.Data, x, y int) {
if d.CanTeleport() {
pf.hid.Click(game.RightButton, x, y)
} else {
Expand Down
25 changes: 25 additions & 0 deletions internal/pather/path_finding_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,28 @@ func IsWalkable(pos data.Position, areaOriginPos data.Position, collisionGrid []

return collisionGrid[indexY][indexX]
}

// FindFirstWalkable finds the first walkable position from a given position and radius
func FindFirstWalkable(from data.Position, areaOriginPos data.Position, grid [][]bool, radius int) (int, int) {
startX := from.X - areaOriginPos.X
startY := from.Y - areaOriginPos.Y

for r := radius; r >= 0; r-- {
for dx := -r; dx <= r; dx++ {
dy := int(math.Sqrt(float64(r*r - dx*dx)))
positions := [][2]int{
{startX + dx, startY + dy},
{startX + dx, startY - dy},
{startX - dx, startY + dy},
{startX - dx, startY - dy},
}
for _, pos := range positions {
newX, newY := pos[0]+areaOriginPos.X, pos[1]+areaOriginPos.Y
if pos[0] >= 0 && pos[0] < len(grid) && pos[1] >= 0 && pos[1] < len(grid[0]) && IsWalkable(data.Position{newX, newY}, areaOriginPos, grid) {
return newX, newY
}
}
}
}
return -1, -1
}

0 comments on commit f742f51

Please sign in to comment.