mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-09-05 19:14:14 -04:00
lint updates for /d2astar (#507)
This commit is contained in:
parent
0f9e846834
commit
5b26624cb8
@ -4,28 +4,29 @@ package d2astar
|
||||
// the sake of testing. This functionality forms the back end for
|
||||
// goreland_test.go, and serves as an example for how to use A* for a graph.
|
||||
|
||||
|
||||
// The Magical World of Goreland, is where Ted Stevens and Al Gore are from.
|
||||
//
|
||||
//
|
||||
// It is composed of Big Trucks, and a Series of Tubes!
|
||||
//
|
||||
//
|
||||
// Ok, it is basically just a Graph.
|
||||
// Nodes are called "Trucks" and they have X, Y coordinates
|
||||
// Edges are called "Tubes", they connect Trucks, and they have a cost
|
||||
//
|
||||
//
|
||||
// The key differences between this example and the Tile world:
|
||||
// 1) There is no grid. Trucks have arbitrary coordinates.
|
||||
// 2) Edges are not implied by the grid positions. Instead edges are explicitly
|
||||
// modelled as Tubes.
|
||||
//
|
||||
//
|
||||
// The key similarities between this example and the Tile world:
|
||||
// 1) They both use Manhattan distance as their heuristic
|
||||
// 2) Both implement Pather
|
||||
|
||||
//Goreland represents a world of trucks and tubes.
|
||||
type Goreland struct {
|
||||
// trucks map[int]*Truck // not needed really
|
||||
}
|
||||
|
||||
//Tube is an edge. They connect Trucks, and have a cost.
|
||||
type Tube struct {
|
||||
from *Truck
|
||||
to *Truck
|
||||
@ -39,7 +40,7 @@ type Truck struct {
|
||||
X, Y int
|
||||
|
||||
// array of tubes going to other trucks
|
||||
out_to []Tube
|
||||
outTo []Tube
|
||||
|
||||
label string
|
||||
}
|
||||
@ -49,8 +50,8 @@ func (t *Truck) PathNeighbors() []Pather {
|
||||
|
||||
neighbors := []Pather{}
|
||||
|
||||
for _, tube_element := range t.out_to {
|
||||
neighbors = append(neighbors, Pather(tube_element.to))
|
||||
for _, tubeElement := range t.outTo {
|
||||
neighbors = append(neighbors, Pather(tubeElement.to))
|
||||
}
|
||||
return neighbors
|
||||
}
|
||||
@ -58,9 +59,9 @@ func (t *Truck) PathNeighbors() []Pather {
|
||||
// PathNeighborCost returns the cost of the tube leading to Truck.
|
||||
func (t *Truck) PathNeighborCost(to Pather) float64 {
|
||||
|
||||
for _, tube_element := range (t).out_to {
|
||||
if Pather((tube_element.to)) == to {
|
||||
return tube_element.Cost
|
||||
for _, tubeElement := range (t).outTo {
|
||||
if Pather((tubeElement.to)) == to {
|
||||
return tubeElement.Cost
|
||||
}
|
||||
}
|
||||
return 10000000
|
||||
|
@ -19,51 +19,51 @@ func AddTube(t1, t2 *Truck, cost float64) *Tube {
|
||||
tube1.from = t1
|
||||
tube1.to = t2
|
||||
|
||||
t1.out_to = append(t1.out_to, *tube1)
|
||||
t2.out_to = append(t2.out_to, *tube1)
|
||||
t1.outTo = append(t1.outTo, *tube1)
|
||||
t2.outTo = append(t2.outTo, *tube1)
|
||||
|
||||
return tube1
|
||||
}
|
||||
|
||||
// Consider a world with Nodes (Trucks) and Edges (Tubes), Edges each having a cost
|
||||
//
|
||||
//
|
||||
// E
|
||||
// /|
|
||||
// / |
|
||||
// S--M
|
||||
//
|
||||
//
|
||||
// S=Start at (0,0)
|
||||
// E=End at (1,1)
|
||||
// M=Middle at (0,1)
|
||||
//
|
||||
//
|
||||
// S-M and M-E are clean clear tubes. cost: 1
|
||||
//
|
||||
//
|
||||
// S-E is either:
|
||||
//
|
||||
//
|
||||
// 1) TestGraphPath_ShortDiagonal : diagonal is a nice clean clear Tube , cost: 1.9
|
||||
// Solver should traverse the bridge.
|
||||
// Expect solution: Start, End Total cost: 1.9
|
||||
//
|
||||
//
|
||||
// 1) TestGraphPath_LongDiagonal : diagonal is a Tube plugged full of
|
||||
// "enormous amounts of material"!, cost: 10000.
|
||||
// Solver should avoid the plugged tube.
|
||||
// Expect solution Start,Middle,End Total cost: 2.0
|
||||
|
||||
func createGorelandGraphPath_Diagonal(t *testing.T, diagonal_cost float64, expectedDist float64) {
|
||||
func createGorelandGraphPathDiagonal(t *testing.T, diagonalCost float64, expectedDist float64) {
|
||||
|
||||
world := new(Goreland)
|
||||
|
||||
tr_start := AddTruck(0, 0, "Start")
|
||||
tr_mid := AddTruck(0, 1, "Middle")
|
||||
tr_end := AddTruck(1, 1, "End")
|
||||
trStart := AddTruck(0, 0, "Start")
|
||||
trMid := AddTruck(0, 1, "Middle")
|
||||
trEnd := AddTruck(1, 1, "End")
|
||||
|
||||
AddTube(tr_start, tr_end, diagonal_cost)
|
||||
AddTube(tr_start, tr_mid, 1)
|
||||
AddTube(tr_mid, tr_end, 1)
|
||||
AddTube(trStart, trEnd, diagonalCost)
|
||||
AddTube(trStart, trMid, 1)
|
||||
AddTube(trMid, trEnd, 1)
|
||||
|
||||
t.Logf("Goreland. Diagonal cost: %v\n\n", diagonal_cost)
|
||||
t.Logf("Goreland. Diagonal cost: %v\n\n", diagonalCost)
|
||||
|
||||
p, dist, found := Path(tr_start, tr_end, math.MaxFloat64)
|
||||
p, dist, found := Path(trStart, trEnd, math.MaxFloat64)
|
||||
|
||||
if !found {
|
||||
t.Log("Could not find a path")
|
||||
@ -79,8 +79,8 @@ func createGorelandGraphPath_Diagonal(t *testing.T, diagonal_cost float64, expec
|
||||
}
|
||||
|
||||
func TestGraphPaths_ShortDiagonal(t *testing.T) {
|
||||
createGorelandGraphPath_Diagonal(t, 1.9, 1.9)
|
||||
createGorelandGraphPathDiagonal(t, 1.9, 1.9)
|
||||
}
|
||||
func TestGraphPaths_LongDiagonal(t *testing.T) {
|
||||
createGorelandGraphPath_Diagonal(t, 10000, 2.0)
|
||||
createGorelandGraphPathDiagonal(t, 10000, 2.0)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user