Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private OpenList As New List(Of Node)
- Private ClosedList As New List(Of Node)
- Private Structure State
- Dim x, y As Integer
- End Structure
- Private Structure Node
- Dim self As State 'the state of this node
- Dim parent As State '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()
- self.x = 0
- self.y = 0
- parent.x = 0
- parent.y = 0
- F = 0
- G = 0
- H = 0
- End Sub
- Sub GetScores(TargetX As Integer, TargetY As Integer)
- Dim dx, dy As Integer
- dx = self.x - parent.x
- dy = self.y - parent.y
- 'check if movement type
- If dx = 1 And dy <> self.y Then
- G = 14 + ParentG
- ElseIf dx = -1 And dy <> self.y Then
- G = 14 + ParentG
- Else
- G = 10 + ParentG
- End If
- 'block-method heuristic (vertical/horizontal movement only)
- dx = Math.Abs(TargetX - self.x)
- dy = Math.Abs(TargetY - self.y)
- H = (dx + dy) * 10
- 'total score
- F = G + H
- End Sub
- End Structure
- Public Sub Path(StartX As Integer, StartY As Integer, _
- EndX As Integer, EndY As Integer)
- Dim CurrentX, CurrentY, intIndex As Integer
- Dim InList As Boolean = False
- Dim TempNode, LowNode As New Node
- 'the first node in the closed list is the starting position
- With TempNode
- .self.x = StartX
- .self.y = StartY
- .parent.x = StartX
- .parent.y = StartY
- .ParentG = 0
- .GetScores(EndX, EndY)
- End With
- ClosedList.Add(TempNode)
- CurrentX = StartX
- CurrentY = StartY
- intIndex = 0
- Do While True
- If CurrentX = EndX And CurrentY = EndY Then
- For Each Node In ClosedList
- 'output the path (backwards i think?)
- Write((intIndex + 2), 2, Node.self.x, ConsoleColor.Green)
- Write((intIndex + 3), 2, Node.self.y, ConsoleColor.Green)
- intIndex = intIndex + 1
- Next
- Console.ReadKey()
- Else
- 'add neighbors to the open list
- AddNeighbors(CurrentX, CurrentY, EndX, EndY)
- 'find the lowest node
- LowNode.F = 999999
- For Each Node In OpenList
- If Node.F < LowNode.F Then
- LowNode = Node
- End If
- Next
- 'remove from open list
- OpenList.Remove(LowNode)
- 'check the closed list for this node
- For Each Node In ClosedList
- If Node.self.x = LowNode.self.x And Node.self.y = LowNode.self.y Then
- InList = True
- End If
- Next
- 'if its not in the list
- If Not InList Then
- 'add it to the list
- ClosedList.Add(LowNode)
- 'set this node as the current node
- CurrentX = LowNode.self.x
- CurrentY = LowNode.self.y
- End If
- 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 InList As Boolean = False
- For intx = -1 To 1
- For inty = -1 To 1
- 'if the node is inside the map boundries
- If Not (X + intx) < 0 Then
- If Not (y + inty) < 0 Then
- 'set positions
- TempNode.self.x = X + intx
- TempNode.self.y = y + inty
- TempNode.parent.x = X
- TempNode.parent.y = y
- 'get the scores of the parent
- TempNode.GetScores(X, y)
- 'assign the parents G score
- TempNode.ParentG = TempNode.G
- 'get this nodes scores
- TempNode.GetScores(TargetX, TargetY)
- 'check the other two lists for this node
- For Each Node In OpenList
- If Node.self.x = TempNode.self.x And Node.self.y = TempNode.self.y Then
- InList = True
- End If
- Next
- 'check the other two lists for this node
- For Each Node In ClosedList
- If Node.self.x = TempNode.self.x And Node.self.y = TempNode.self.y Then
- InList = True
- End If
- Next
- 'add to the next open list if it's not already in either list
- If Not InList Then
- OpenList.Add(TempNode)
- End If
- End If
- End If
- Next
- Next
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment