Advertisement
Guest User

Getting closer.

a guest
Oct 6th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.42 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.  
  78.                 For Each Node In ClosedList
  79.  
  80.                     Write((intIndex + 2), 2, Node.NodeX, ConsoleColor.Green)
  81.                     Write((intIndex + 3), 2, Node.NodeY, ConsoleColor.Green)
  82.  
  83.                     Write((intIndex + 5), 2, Node.ParentX, ConsoleColor.Green)
  84.                     Write((intIndex + 6), 2, Node.ParentY, ConsoleColor.Green)
  85.  
  86.                     Write((intIndex + 8), 2, Node.F, ConsoleColor.Green)
  87.  
  88.                     intIndex = intIndex + 1
  89.  
  90.                     Console.ReadKey(True)
  91.                     Console.Clear()
  92.  
  93.                 Next
  94.  
  95.  
  96.             Else
  97.  
  98.                 'add neighbors to the open list
  99.                 AddNeighbors(CurrentX, CurrentY, EndX, EndY)
  100.  
  101.                 'find the loweset node
  102.                 With LowNode
  103.                     .F = 999999
  104.                 End With
  105.  
  106.                 For Each Node In OpenList
  107.                     If Node.F < LowNode.F Then
  108.                         LowNode = Node
  109.                     End If
  110.                 Next
  111.  
  112.                 'remove from open list
  113.                 OpenList.Remove(LowNode)
  114.  
  115.                 CurrentX = LowNode.NodeX
  116.                 CurrentY = LowNode.NodeY
  117.  
  118.                 'add to closed list
  119.                 If Not ClosedList.Contains(LowNode) Then _
  120.                         ClosedList.Add(LowNode)
  121.  
  122.             End If
  123.  
  124.         Loop
  125.  
  126.     End Sub
  127.  
  128.     'adds the neighbors of a cell to the open list
  129.     Public Sub AddNeighbors(X As Integer, y As Integer, TargetX As Integer, TargetY As Integer)
  130.  
  131.         Dim TempNode As New Node
  132.         Dim LowNode As New Node
  133.         Dim TempG As Integer
  134.  
  135.         TempG = 0
  136.  
  137.         For intx = -1 To 1
  138.             For inty = -1 To 1
  139.  
  140.                 If Not (X + intx) < 0 Then
  141.                     If Not (y + inty) < 0 Then
  142.  
  143.                         'set stats
  144.                         TempNode.NodeX = X + intx
  145.                         TempNode.NodeY = y + inty
  146.                         TempNode.ParentX = X
  147.                         TempNode.ParentY = y
  148.  
  149.                         TempNode.GetScores(X, y)
  150.                         TempG = TempNode.G
  151.                         TempNode.ParentG = TempG
  152.  
  153.                         TempNode.GetScores(TargetX, TargetY)
  154.  
  155.                         'add to the next open list if it's not already in either list
  156.                         If Not OpenList.Contains(TempNode) _
  157.                             And Not ClosedList.Contains(TempNode) Then _
  158.                                 OpenList.Add(TempNode)
  159.  
  160.                     End If
  161.                     End If
  162.  
  163.             Next
  164.         Next
  165.  
  166.  
  167.     End Sub
  168.  
  169. #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement