From 547f9ab3821300b0330d70b20c3ec3407d545d8b Mon Sep 17 00:00:00 2001 From: kts of kettek Date: Sat, 3 Feb 2024 02:00:04 -0800 Subject: [PATCH] Add non-diagonal movement --- sauce/astar.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/sauce/astar.go b/sauce/astar.go index 5e6755c..fdb9208 100644 --- a/sauce/astar.go +++ b/sauce/astar.go @@ -49,7 +49,8 @@ func FindPath(w *World, fromX, fromY int, toX, toY int) ([]Node, bool) { // Get neighbors. var neighbors []*Node - for x := -1; x <= 1; x++ { + // diagonal + /*for x := -1; x <= 1; x++ { for y := -1; y <= 1; y++ { if x == 0 && y == 0 { continue @@ -60,6 +61,28 @@ func FindPath(w *World, fromX, fromY int, toX, toY int) ([]Node, bool) { } neighbors = append(neighbors, &Node{Coord: coord, parent: current, g: math.MaxUint16, f: math.MaxUint16}) } + }*/ + // non-diagonal + for x := -1; x <= 1; x++ { + if x == 0 { + continue + } + coord := Coord{current.X + int16(x), current.Y} + if cell := w.At(int(coord.X), int(coord.Y)); cell == nil || cell.Hueristic == 255 { + continue + } + neighbors = append(neighbors, &Node{Coord: coord, parent: current, g: math.MaxUint16, f: math.MaxUint16}) + + } + for y := -1; y <= 1; y++ { + if y == 0 { + continue + } + coord := Coord{current.X, current.Y + int16(y)} + if cell := w.At(int(coord.X), int(coord.Y)); cell == nil || cell.Hueristic == 255 { + continue + } + neighbors = append(neighbors, &Node{Coord: coord, parent: current, g: math.MaxUint16, f: math.MaxUint16}) } // Iterate over neighbors.