Advertisement
Guest User

astar2

a guest
Oct 5th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.15 KB | None | 0 0
  1. #Region "Path Finding"
  2.  
  3.     Private Structure Node
  4.  
  5.         Dim NodeX, NodeY As Integer 'the location of this node
  6.         Dim ParentX, ParentY As Integer 'the location of this nodes parent
  7.         Dim ParentG As Integer 'the G score of the parent cell
  8.         Dim F, G, H As Integer
  9.  
  10.         Sub Clear()
  11.             NodeX = 0
  12.             NodeY = 0
  13.             ParentX = 0
  14.             ParentY = 0
  15.             ParentG = 0
  16.             F = 0
  17.             G = 0
  18.             H = 0
  19.         End Sub
  20.  
  21.         Sub GetScores(TargetX As Integer, TargetY As Integer)
  22.  
  23.             Dim dx, dy As Integer
  24.  
  25.             dx = NodeX - ParentX
  26.             dy = NodeY - ParentY
  27.  
  28.             If dx = 1 And dy <> NodeY Then
  29.                 G = 14 + ParentG
  30.             ElseIf dx = -1 And dy <> NodeY Then
  31.                 G = 14 + ParentG
  32.             Else
  33.                 G = 10 + ParentG
  34.             End If
  35.  
  36.             dx = Math.Abs(TargetX - NodeX)
  37.             dy = Math.Abs(TargetY - NodeY)
  38.  
  39.             H = (dx + dy) * 10
  40.  
  41.             F = G + H
  42.  
  43.         End Sub
  44.  
  45.  
  46.     End Structure
  47.  
  48.     Private OpenList As New List(Of Node)
  49.     Private ClosedList As New List(Of Node)
  50.  
  51.     Public Sub Path(StartX As Integer, StartY As Integer, _
  52.                     EndX As Integer, EndY As Integer)
  53.  
  54.         Dim CurrentX, CurrentY, intIndex As Integer
  55.         Dim TempNode, LowNode As New Node
  56.  
  57.         CurrentX = StartX
  58.         CurrentY = StartY
  59.  
  60.         With TempNode
  61.             .NodeX = StartX
  62.             .NodeY = StartY
  63.             .ParentX = StartX
  64.             .ParentY = StartY
  65.             .ParentG = 0
  66.             .GetScores(EndX, EndY)
  67.         End With
  68.  
  69.         ClosedList.Add(TempNode)
  70.  
  71.         intIndex = 0
  72.  
  73.         Do While True
  74.  
  75.             If CurrentX = EndX And CurrentY = EndY Then
  76.  
  77.                 For Each Node In ClosedList
  78.  
  79.                     Write((intIndex + 2) * 2, 2, Node.NodeX, ConsoleColor.Green)
  80.                     Write((intIndex + 3) * 2, 2, Node.NodeY, ConsoleColor.Green)
  81.  
  82.                     Write((intIndex + 5) * 2, 2, Node.ParentX, ConsoleColor.Green)
  83.                     Write((intIndex + 6) * 2, 2, Node.ParentY, ConsoleColor.Green)
  84.  
  85.                     Write((intIndex + 8) * 2, 2, Node.F, ConsoleColor.Green)
  86.  
  87.                     intIndex = intIndex + 1
  88.  
  89.                 Next
  90.  
  91.  
  92.             Else
  93.  
  94.                 'add neighbors to the open list
  95.                 AddNeighbors(CurrentX, CurrentY, EndX, EndY)
  96.  
  97.                 'find the loweset node
  98.                 With LowNode
  99.                     .F = 999999
  100.                 End With
  101.  
  102.                 For Each Node In OpenList
  103.                     If Node.F < LowNode.F Then
  104.                         LowNode = Node
  105.                     End If
  106.                 Next
  107.  
  108.                 CurrentX = LowNode.NodeX
  109.                 CurrentY = LowNode.NodeY
  110.  
  111.                 If Not ClosedList.Contains(LowNode) Then _
  112.                         ClosedList.Add(LowNode)
  113.  
  114.             End If
  115.  
  116.         Loop
  117.         '
  118.     End Sub
  119.  
  120.     'adds the neighbors of a cell to the open list
  121.     Public Sub AddNeighbors(X As Integer, y As Integer, TargetX As Integer, TargetY As Integer)
  122.  
  123.         Dim TempNode As New Node
  124.         Dim LowNode As New Node
  125.         Dim TempG As Integer
  126.  
  127.         TempG = 0
  128.  
  129.         For intx = -1 To 1
  130.             For inty = -1 To 1
  131.  
  132.                 If Not (X + intx) < 0 Then
  133.                     If Not (y + inty) < 0 Then
  134.  
  135.                         'set stats
  136.                         TempNode.NodeX = X + intx
  137.                         TempNode.NodeY = y + inty
  138.                         TempNode.ParentX = X
  139.                         TempNode.ParentY = y
  140.  
  141.                         TempNode.GetScores(X, y)
  142.                         TempG = TempNode.G
  143.                         TempNode.ParentG = TempG
  144.  
  145.                         TempNode.GetScores(TargetX, TargetY)
  146.  
  147.                         'add to the next open list
  148.                         If Not OpenList.Contains(TempNode) Then _
  149.                                 OpenList.Add(TempNode)
  150.  
  151.                     End If
  152.                 End If
  153.  
  154.             Next
  155.         Next
  156.  
  157.  
  158.     End Sub
  159.  
  160. #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement