2019-01-15 20:49:01 +00:00
|
|
|
package soko
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PathFinder struct {
|
2019-01-16 06:33:04 +00:00
|
|
|
state *State
|
|
|
|
moves []Moves
|
2019-01-15 20:49:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 06:33:04 +00:00
|
|
|
func NewPathFinder(s *State) PathFinder {
|
|
|
|
return PathFinder{s, nil}
|
2019-01-15 20:49:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 06:33:04 +00:00
|
|
|
func NewPathFinderMoves(s *State, moves []Moves) PathFinder {
|
|
|
|
return PathFinder{s, moves}
|
2019-01-15 20:49:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 06:33:04 +00:00
|
|
|
func (p PathFinder) Find(target int) []int {
|
2019-01-15 20:49:01 +00:00
|
|
|
source := p.state.Player.Pos
|
2019-01-16 06:33:04 +00:00
|
|
|
level := p.state.Level
|
|
|
|
size := level.Size()
|
|
|
|
distances := NewInts(size)
|
|
|
|
distances[source] = 0
|
|
|
|
moves := NewInts(size)
|
|
|
|
moves[source] = source
|
|
|
|
state := p.newPathFinderState(source)
|
|
|
|
|
2019-01-15 20:49:01 +00:00
|
|
|
heuristic := func(i int) int {
|
2019-01-16 06:33:04 +00:00
|
|
|
idx := state.frontier[i]
|
|
|
|
return distances[idx] + p.state.IdxToPos[idx].DistInt(p.state.IdxToPos[target])
|
2019-01-15 20:49:01 +00:00
|
|
|
}
|
|
|
|
for {
|
2019-01-16 06:33:04 +00:00
|
|
|
curr := state.frontier[0]
|
2019-01-15 20:49:01 +00:00
|
|
|
if curr == target {
|
|
|
|
break
|
|
|
|
}
|
2019-01-16 06:33:04 +00:00
|
|
|
state.frontier = state.frontier[1:]
|
2019-01-15 20:49:01 +00:00
|
|
|
|
2019-01-16 06:33:04 +00:00
|
|
|
state.findBetterNeighbours(distances, curr, func(nextIdx, newDistance int) {
|
|
|
|
moves[nextIdx] = curr
|
2019-01-15 20:49:01 +00:00
|
|
|
})
|
|
|
|
|
2019-01-16 06:33:04 +00:00
|
|
|
if len(state.frontier) == 0 {
|
2019-01-15 20:49:01 +00:00
|
|
|
return nil // no path
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply heuristic to frontier (favor points closer to target)
|
2019-01-16 06:33:04 +00:00
|
|
|
sort.Slice(state.frontier, func(i, j int) bool { return heuristic(i) < heuristic(j) })
|
2019-01-15 20:49:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// build reverse path
|
|
|
|
curr := target
|
2019-01-16 06:33:04 +00:00
|
|
|
path := []int{curr}
|
2019-01-15 20:49:01 +00:00
|
|
|
for {
|
|
|
|
curr = moves[curr]
|
|
|
|
if curr == source {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
path = append(path, curr)
|
|
|
|
}
|
|
|
|
// reverse path
|
|
|
|
n := len(path)
|
|
|
|
for i := 0; i < n/2; i++ {
|
|
|
|
path[i], path[n-i-1] = path[n-i-1], path[i]
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2019-01-16 06:33:04 +00:00
|
|
|
func (p PathFinder) FindDistances() Ints {
|
2019-01-15 20:49:01 +00:00
|
|
|
source := p.state.Player.Pos
|
2019-01-16 06:33:04 +00:00
|
|
|
level := p.state.Level
|
|
|
|
size := level.Size()
|
|
|
|
distances := NewInts(size)
|
|
|
|
distances[source] = 0
|
|
|
|
state := p.newPathFinderState(source)
|
|
|
|
|
2019-01-15 20:49:01 +00:00
|
|
|
for {
|
2019-01-16 06:33:04 +00:00
|
|
|
curr := state.frontier[0]
|
|
|
|
state.frontier = state.frontier[1:]
|
2019-01-15 20:49:01 +00:00
|
|
|
|
2019-01-16 06:33:04 +00:00
|
|
|
state.findBetterNeighbours(distances, curr, nil)
|
2019-01-15 20:49:01 +00:00
|
|
|
|
2019-01-16 06:33:04 +00:00
|
|
|
if len(state.frontier) == 0 {
|
2019-01-15 20:49:01 +00:00
|
|
|
return distances
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-16 06:33:04 +00:00
|
|
|
|
|
|
|
func (p PathFinder) newPathFinderState(source int) *pathFinderState {
|
|
|
|
return &pathFinderState{p.state, append(make([]int, 0, p.state.Level.Size()), source), p.moves}
|
|
|
|
}
|
|
|
|
|
|
|
|
type pathFinderState struct {
|
|
|
|
state *State
|
|
|
|
frontier []int
|
|
|
|
moves []Moves
|
|
|
|
}
|
|
|
|
|
|
|
|
type betterNeighbourFn func(int, int)
|
|
|
|
|
|
|
|
func (s *pathFinderState) findBetterNeighbours(distances []int, curr int, better betterNeighbourFn) {
|
|
|
|
currDistance := distances[curr]
|
|
|
|
newDistance := currDistance + 1
|
|
|
|
|
|
|
|
var moves []int
|
|
|
|
if s.moves == nil {
|
|
|
|
for _, dir := range Directions {
|
|
|
|
nextIdx := s.state.Level.MoveIdx(curr, dir)
|
|
|
|
if nextIdx == -1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
moves = append(moves, nextIdx)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
moves = s.moves[curr].Valid
|
|
|
|
}
|
|
|
|
for _, nextIdx := range moves {
|
|
|
|
if distance := distances[nextIdx]; distance != -1 && distance <= newDistance { // skip when shorter path exists
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !s.state.Walkable[nextIdx] || s.state.Bricks[nextIdx] != -1 { // filter neighbours
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
distances[nextIdx] = newDistance
|
|
|
|
s.frontier = append(s.frontier, nextIdx)
|
|
|
|
if better == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
better(nextIdx, newDistance)
|
|
|
|
}
|
|
|
|
}
|