Skip to content

Commit f742f51

Browse files
committed
attempt to fix the random movement when character stuck
1 parent 42d6e7b commit f742f51

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

internal/action/step/move_to.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ func (m *MoveToStep) Run(d game.Data, container container.Container) error {
111111
if m.path == nil || !m.cachePath(d) || stuck {
112112
if stuck {
113113
if len(m.path.AstarPather) == 0 {
114-
container.PathFinder.RandomMovement()
114+
randomPosX, randomPosY := pather.FindFirstWalkable(d.PlayerUnit.Position, d.AreaOrigin, d.CollisionGrid, 15)
115+
screenX, screenY := container.PathFinder.GameCoordsToScreenCords(d.PlayerUnit.Position.X, d.PlayerUnit.Position.Y, randomPosX, randomPosY)
116+
container.PathFinder.MoveCharacter(d, screenX, screenY)
115117
m.lastRun = time.Now()
116118

117119
return nil

internal/pather/path_finder.go

+2-11
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,6 @@ func (pf *PathFinder) GetClosestWalkablePath(d game.Data, dest data.Position, bl
189189
}
190190

191191
func (pf *PathFinder) MoveThroughPath(d game.Data, p *Pather, distance int) {
192-
//if len(p.AstarPather) == 0 {
193-
// if teleport {
194-
// hid.Click(hid.RightButton)
195-
// } else {
196-
// hid.PressKey(config.Config.Bindings.ForceMove)
197-
// }
198-
// return
199-
//}
200-
201192
moveTo := p.AstarPather[0].(*Tile)
202193
if distance > 0 && len(p.AstarPather) > distance {
203194
moveTo = p.AstarPather[len(p.AstarPather)-distance].(*Tile)
@@ -210,11 +201,11 @@ func (pf *PathFinder) MoveThroughPath(d game.Data, p *Pather, distance int) {
210201
}
211202

212203
if distance > 0 {
213-
pf.moveCharacter(d, screenX, screenY)
204+
pf.MoveCharacter(d, screenX, screenY)
214205
}
215206
}
216207

217-
func (pf *PathFinder) moveCharacter(d game.Data, x, y int) {
208+
func (pf *PathFinder) MoveCharacter(d game.Data, x, y int) {
218209
if d.CanTeleport() {
219210
pf.hid.Click(game.RightButton, x, y)
220211
} else {

internal/pather/path_finding_tools.go

+25
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,28 @@ func IsWalkable(pos data.Position, areaOriginPos data.Position, collisionGrid []
7777

7878
return collisionGrid[indexY][indexX]
7979
}
80+
81+
// FindFirstWalkable finds the first walkable position from a given position and radius
82+
func FindFirstWalkable(from data.Position, areaOriginPos data.Position, grid [][]bool, radius int) (int, int) {
83+
startX := from.X - areaOriginPos.X
84+
startY := from.Y - areaOriginPos.Y
85+
86+
for r := radius; r >= 0; r-- {
87+
for dx := -r; dx <= r; dx++ {
88+
dy := int(math.Sqrt(float64(r*r - dx*dx)))
89+
positions := [][2]int{
90+
{startX + dx, startY + dy},
91+
{startX + dx, startY - dy},
92+
{startX - dx, startY + dy},
93+
{startX - dx, startY - dy},
94+
}
95+
for _, pos := range positions {
96+
newX, newY := pos[0]+areaOriginPos.X, pos[1]+areaOriginPos.Y
97+
if pos[0] >= 0 && pos[0] < len(grid) && pos[1] >= 0 && pos[1] < len(grid[0]) && IsWalkable(data.Position{newX, newY}, areaOriginPos, grid) {
98+
return newX, newY
99+
}
100+
}
101+
}
102+
}
103+
return -1, -1
104+
}

0 commit comments

Comments
 (0)