1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2025-07-26 11:24:38 -04:00
OpenDiablo2/d2common/d2astar/goreland_example.go
thetogi 7b60882ba3
Replace ebitenUtil with d2DebugUtil and new charmap for debugger. (#577)
* lint updates for /d2astar

* png issue

* debug package

* load bmp properly

* debug draft

* Vector float64 (#565)

* Fixed nil pointer in Copy()

* Position added

Added Floor() and String() methods to Vector.

Also added Position which declares an embedded Vector2 and returns various forms of it.

* d2vector.Vector2 renamed to d2vector.BigFloat

* vector.go renamed to big_float.go

* Float64 stub and more renaming

* Vector value getters

* Separate vector types with initial methods.

* Divide and lint warnings.

* Distance and Length.

* Scale, Abs and Negate.

* CompareFloat64Fuzzy delta direction reversed.

* Refactor vector_test.go.

* Renamed Approx methods.

* Distance and Length.

* Distance and Length.

* Removed BigFloat and Vector, renamed Float64 to Vector, simplified tests.

* Angle, SignedAngle and other small functions.

* Receiver rename.

* SingedAngle and test fixed

* Rotate.

* SetLength.

* Cross.

* NinetyAnti and NinetyClock.

* Lerp and Clamp.

* Reflect and ReflectSurface.

* Cardinal convenience functions.

* Comments.

* Panic on NaN and Inf in Position.

* Lint warnings and comments.

* D2map lint warnings (#566)

* Comments and newlines in engine.go

* Comments and newlines in object.go

* Comments and newlines in animated_entity.go

* Comments and newlines in missile.go

* Comments and newlines in npc.go

* Comments and newlines in player.go

* Removed object.go (incorrectly merged it in during rebase).

* Comments and newlines in renderer.go.

* Comments and newlines in map_entity.go.

* Comments and newlines in walk_mesh.go.

* Comments and newlines in viewport.go and tile_cache.go.

* Comments and newlines in stamp.go and wilderness_tile_types.go.

* Comments and newlines in everything else.

* removing inventory grid test for now because it causes builds to fail on github (#570)

* Refactored d2enum (#567)

* Refactored animation mode enum

* More d2enum changes

* Refactored tile enum

* Refactored weapon class enum

* Refactored more enums

* Refactored item event enum

* Fixed init_functions animation mode

* Removed string2enum from MonsterAnimationMode

* Refactored ItemStatCost description

* Last enum lint errors

* Regenerated monster stringer file

* compressed image

* Replaced restruct with StreamReader (#574)

* Fixed bug in palette.go (#575)

* Javascript console commands (#572)

* Allow the execution of JS from the terminal when hosting a local game or playing a single game

Signed-off-by: William Claude <w.claude@thebeat.co>

* Reorganise imports on edited files

Signed-off-by: William Claude <w.claude@thebeat.co>

* Remove Reset

Signed-off-by: William Claude <w.claude@thebeat.co>

* Add inventory.txt loader, use the records (#573)

* adding resource entry for inventory.txt

* adding loader for inventory.txt

* adding call to inventory.txt loader in d2app

* d2game now uses the inventory.txt records for making the inventory panel

* removed comments

* remove unused files

* update go.mod

* lint updates for /d2astar

* png issue

* update go.mod

Co-authored-by: danhale-git <36298392+danhale-git@users.noreply.github.com>
Co-authored-by: dk <dknuth0101@gmail.com>
Co-authored-by: Intyre <Intyre@gmail.com>
Co-authored-by: William <1004323+aziule@users.noreply.github.com>
2020-07-11 18:12:20 -04:00

105 lines
2.4 KiB
Go

package d2astar
// goreland_example.go implements implements Pather for
// 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
}
const (
_tenFuckingMillion = 10000000
)
// Tube is an edge. They connect Trucks, and have a cost.
type Tube struct {
from *Truck
to *Truck
Cost float64
}
// A Truck is a Truck in a grid which implements Grapher.
type Truck struct {
// X and Y are the coordinates of the truck.
X, Y int
// array of tubes going to other trucks
outTo []Tube
label string
}
// PathNeighbors returns the neighbors of the Truck
func (t *Truck) PathNeighbors() []Pather {
neighbors := []Pather{}
for _, tubeElement := range t.outTo {
neighbors = append(neighbors, Pather(tubeElement.to))
}
return neighbors
}
// PathNeighborCost returns the cost of the tube leading to Truck.
func (t *Truck) PathNeighborCost(to Pather) float64 {
for _, tubeElement := range (t).outTo {
if Pather((tubeElement.to)) == to {
return tubeElement.Cost
}
}
return _tenFuckingMillion
}
// PathEstimatedCost uses Manhattan distance to estimate orthogonal distance
// between non-adjacent nodes.
func (t *Truck) PathEstimatedCost(to Pather) float64 {
toT := to.(*Truck)
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 Goreland) RenderPath(path []Pather) string {
s := ""
for _, p := range path {
pT := p.(*Truck)
s = pT.label + " " + s
}
return s
}