Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Region "Path Finding"
- Private Structure Node
- Dim NodeX, NodeY As Integer 'the location of this node
- Dim ParentX, ParentY As Integer 'the location of this nodes parent
- Dim ParentG As Integer 'the G score of the parent cell
- Dim F, G, H As Integer
- Sub Clear()
- NodeX = 0
- NodeY = 0
- ParentX = 0
- ParentY = 0
- ParentG = 0
- F = 0
- G = 0
- H = 0
- End Sub
- Sub GetScores(TargetX As Integer, TargetY As Integer)
- Dim dx, dy As Integer
- dx = NodeX - ParentX
- dy = NodeY - ParentY
- If dx = 1 And dy <> NodeY Then
- G = 14 + ParentG
- ElseIf dx = -1 And dy <> NodeY Then
- G = 14 + ParentG
- Else
- G = 10 + ParentG
- End If
- dx = Math.Abs(TargetX - NodeX)
- dy = Math.Abs(TargetY - NodeY)
- H = (dx + dy) * 10
- F = G + H
- End Sub
- End Structure
- Private OpenList As New List(Of Node)
- Private ClosedList As New List(Of Node)
- Public Sub Path(StartX As Integer, StartY As Integer, _
- EndX As Integer, EndY As Integer)
- Dim CurrentX, CurrentY, intIndex As Integer
- Dim TempNode, LowNode As New Node
- CurrentX = StartX
- CurrentY = StartY
- With TempNode
- .NodeX = StartX
- .NodeY = StartY
- .ParentX = StartX
- .ParentY = StartY
- .ParentG = 0
- .GetScores(EndX, EndY)
- End With
- ClosedList.Add(TempNode)
- intIndex = 0
- Do While True
- If CurrentX = EndX And CurrentY = EndY Then
- For Each Node In ClosedList
- Write((intIndex + 2), 2, Node.NodeX, ConsoleColor.Green)
- Write((intIndex + 3), 2, Node.NodeY, ConsoleColor.Green)
- Write((intIndex + 5), 2, Node.ParentX, ConsoleColor.Green)
- Write((intIndex + 6), 2, Node.ParentY, ConsoleColor.Green)
- Write((intIndex + 8), 2, Node.F, ConsoleColor.Green)
- intIndex = intIndex + 1
- Console.ReadKey(True)
- Console.Clear()
- Next
- Else
- 'add neighbors to the open list
- AddNeighbors(CurrentX, CurrentY, EndX, EndY)
- 'find the loweset node
- With LowNode
- .F = 999999
- End With
- For Each Node In OpenList
- If Node.F < LowNode.F Then
- LowNode = Node
- End If
- Next
- 'remove from open list
- OpenList.Remove(LowNode)
- CurrentX = LowNode.NodeX
- CurrentY = LowNode.NodeY
- 'add to closed list
- If Not ClosedList.Contains(LowNode) Then _
- ClosedList.Add(LowNode)
- End If
- Loop
- End Sub
- 'adds the neighbors of a cell to the open list
- Public Sub AddNeighbors(X As Integer, y As Integer, TargetX As Integer, TargetY As Integer)
- Dim TempNode As New Node
- Dim LowNode As New Node
- Dim TempG As Integer
- TempG = 0
- For intx = -1 To 1
- For inty = -1 To 1
- If Not (X + intx) < 0 Then
- If Not (y + inty) < 0 Then
- 'set stats
- TempNode.NodeX = X + intx
- TempNode.NodeY = y + inty
- TempNode.ParentX = X
- TempNode.ParentY = y
- TempNode.GetScores(X, y)
- TempG = TempNode.G
- TempNode.ParentG = TempG
- TempNode.GetScores(TargetX, TargetY)
- 'add to the next open list if it's not already in either list
- If Not OpenList.Contains(TempNode) _
- And Not ClosedList.Contains(TempNode) Then _
- OpenList.Add(TempNode)
- End If
- End If
- Next
- Next
- End Sub
- #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement