Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 F, G, H As Integer
- Sub Clear()
- NodeX = 0
- NodeY = 0
- ParentX = 0
- ParentY = 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
- ElseIf dx = -1 And dy <> NodeY Then
- G = 14
- Else
- G = 10
- 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(0 To 10) As Node
- Private ClosedList(0 To 50) As Node
- Public Sub Path(StartX As Integer, StartY As Integer, _
- EndX As Integer, EndY As Integer)
- Dim CurrentX, CurrentY As Integer
- Dim TempNode As New Node
- CurrentX = StartX
- CurrentY = StartY
- With TempNode
- .NodeX = StartX
- .NodeY = StartY
- .ParentX = StartX
- .ParentY = StartY
- .GetScores(EndX, EndY)
- End With
- ClosedList(0) = TempNode
- Do While True
- If CurrentX = EndX And CurrentY = EndY Then
- 'debugging : write the values of all nodes in ClosedList
- For intz = 0 To 50
- If Not IsNothing(ClosedList(intz)) Then
- Write((intz + 2) * 2, 2, ClosedList(intz).NodeX, ConsoleColor.Green)
- Write((intz + 3) * 2, 2, ClosedList(intz).NodeY, ConsoleColor.Green)
- Write((intz + 5) * 2, 2, ClosedList(intz).ParentX, ConsoleColor.Green)
- Write((intz + 6) * 2, 2, ClosedList(intz).ParentY, ConsoleColor.Green)
- Write((intz + 8) * 2, 2, ClosedList(intz).F, ConsoleColor.Green)
- End If
- Next
- Else
- AddNeighbors(CurrentX, CurrentY, EndX, EndY)
- 'find the last cell added to the closed list
- For intz = 0 To 50
- If Not IsNothing(ClosedList(intz)) Then
- CurrentX = ClosedList(intz).NodeX
- CurrentY = ClosedList(intz).NodeY
- End If
- Next
- End If
- Loop
- End Sub
- 'this actually does all the work
- 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
- 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(TargetX, TargetY)
- 'add to the next open spot
- For intz = 0 To 10
- If IsNothing(OpenList(intz)) Then
- OpenList(intz) = TempNode
- Console.Title = intz
- End If
- Next
- LowNode.F = 9999
- LowNode.G = 9999
- LowNode.H = 9999
- 'find the lowest
- For intz = 0 To UBound(OpenList)
- If OpenList(intz).F < LowNode.F Then
- LowNode = OpenList(intz)
- End If
- Next
- 'add to the closed list
- For intz = 0 To UBound(ClosedList)
- If IsNothing(ClosedList(intz)) Then
- ClosedList(intz) = LowNode
- End If
- Next
- For intz = 0 To UBound(OpenList)
- If IsNothing(OpenList(intz)) Then
- OpenList(intz).Clear()
- End If
- Next
- End If
- End If
- Next
- Next
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment