forked from beefsack/go-astar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
astar_client.go
96 lines (78 loc) · 2.21 KB
/
astar_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package astar
// astar_clioent.go implements implements Pather for
// the sake of testing. This functionality forms the back end for
// astart_client_test.go, and serves as an example for how to use A* for a graph.
// Nodes are called 'Nodes' and they have X, Y coordinates
// Edges are called 'Edges', they connect Nodes, and they have a cost
//
// The key differences between this example and the Tile world:
// 1) There is no grid. Nodes have arbitrary coordinates.
// 2) Edges are not implied by the grid positions. Instead edges are explicitly
// modelled as 'Edges'.
//
// The key similarities between this example and the Tile world:
// 1) They both use Manhattan distance as their heuristic
// 2) Both implement Pather
// GobotWorld will eventually hold a map of type Node
type GobotWorld struct {
// nodes map[int]*Node // not yet used
}
// Edge type connects two Nodes with a cost
type Edge struct {
from *Node
to *Node
Cost float64
}
// A Node is a place in a grid which implements Grapher.
type Node struct {
// X and Y are the coordinates of the truck.
X, Y int
// array of tubes going to other trucks
outTo []Edge
label string
}
// PathNeighbors returns the neighbors of the Truck
func (t *Node) PathNeighbors() []Pather {
neighbors := []Pather{}
for _, edgeElement := range t.outTo {
neighbors = append(neighbors, Pather(edgeElement.to))
}
return neighbors
}
// PathNeighborCost returns the cost of the edge leading to Node.
func (t *Node) PathNeighborCost(to Pather) float64 {
for _, edgeElement := range (t).outTo {
if Pather((edgeElement.to)) == to {
return edgeElement.Cost
}
}
return 10000000
}
// PathEstimatedCost uses Manhattan distance to estimate orthogonal distance
// between non-adjacent nodes.
func (t *Node) PathEstimatedCost(to Pather) float64 {
toT := to.(*Node)
absX := toT.X - t.X
if absX < 0 {
absX = -absX
}
absY := toT.Y - t.Y
if absY < 0 {
absY = -absY
}
r := float64(absX + absY)
return r
}
// RenderPath renders a path on top of a Goreland world.
func (w GobotWorld) RenderPath(path []Pather) string {
s := ""
for _, p := range path {
pT := p.(*Node)
if pT.label != "END" {
s = pT.label + "->" + s
} else {
s = s + pT.label
}
}
return s
}