Guest User

asdasf

a guest
Oct 8th, 2012
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.53 KB | None | 0 0
  1.     Private OpenList As New List(Of Node)
  2.     Private ClosedList As New List(Of Node)
  3.  
  4.     Private Structure State
  5.         Dim x, y As Integer
  6.     End Structure
  7.  
  8.     Private Structure Node
  9.  
  10.         Dim self As State 'the state of this node
  11.         Dim parent As State  'the location of this nodes parent
  12.         Dim ParentG As Integer 'the G score of the parent cell
  13.         Dim F, G, H As Integer
  14.  
  15.         Sub Clear()
  16.             self.x = 0
  17.             self.y = 0
  18.             parent.x = 0
  19.             parent.y = 0
  20.             F = 0
  21.             G = 0
  22.             H = 0
  23.         End Sub
  24.  
  25.         Sub GetScores(TargetX As Integer, TargetY As Integer)
  26.  
  27.             Dim dx, dy As Integer
  28.  
  29.             dx = self.x - parent.x
  30.             dy = self.y - parent.y
  31.  
  32.             'check if movement type
  33.             If dx = 1 And dy <> self.y Then
  34.                 G = 14 + ParentG
  35.             ElseIf dx = -1 And dy <> self.y Then
  36.                 G = 14 + ParentG
  37.             Else
  38.                 G = 10 + ParentG
  39.             End If
  40.  
  41.             'block-method heuristic (vertical/horizontal movement only)
  42.             dx = Math.Abs(TargetX - self.x)
  43.             dy = Math.Abs(TargetY - self.y)
  44.  
  45.             H = (dx + dy) * 10
  46.  
  47.             'total score
  48.             F = G + H
  49.  
  50.         End Sub
  51.  
  52.     End Structure
  53.  
  54.     Public Sub Path(StartX As Integer, StartY As Integer, _
  55.                     EndX As Integer, EndY As Integer)
  56.  
  57.         Dim CurrentX, CurrentY, intIndex As Integer
  58.         Dim InList As Boolean = False
  59.         Dim TempNode, LowNode As New Node
  60.  
  61.         'the first node in the closed list is the starting position
  62.         With TempNode
  63.             .self.x = StartX
  64.             .self.y = StartY
  65.             .parent.x = StartX
  66.             .parent.y = StartY
  67.             .ParentG = 0
  68.             .GetScores(EndX, EndY)
  69.         End With
  70.  
  71.         ClosedList.Add(TempNode)
  72.  
  73.         CurrentX = StartX
  74.         CurrentY = StartY
  75.  
  76.         intIndex = 0
  77.  
  78.         Do While True
  79.  
  80.             If CurrentX = EndX And CurrentY = EndY Then
  81.  
  82.                 For Each Node In ClosedList
  83.  
  84.                     'output the path (backwards i think?)
  85.                     Write((intIndex + 2), 2, Node.self.x, ConsoleColor.Green)
  86.                     Write((intIndex + 3), 2, Node.self.y, ConsoleColor.Green)
  87.  
  88.                     intIndex = intIndex + 1
  89.  
  90.                 Next
  91.  
  92.                 Console.ReadKey()
  93.  
  94.             Else
  95.  
  96.                 'add neighbors to the open list
  97.                 AddNeighbors(CurrentX, CurrentY, EndX, EndY)
  98.  
  99.                 'find the lowest node
  100.                 LowNode.F = 999999
  101.  
  102.                 For Each Node In OpenList
  103.                     If Node.F < LowNode.F Then
  104.                         LowNode = Node
  105.                     End If
  106.                 Next
  107.  
  108.                 'remove from open list
  109.                 OpenList.Remove(LowNode)
  110.  
  111.                 'check the closed list for this node
  112.                 For Each Node In ClosedList
  113.                     If Node.self.x = LowNode.self.x And Node.self.y = LowNode.self.y Then
  114.                         InList = True
  115.                     End If
  116.                 Next
  117.  
  118.                 'if its not in the list
  119.                 If Not InList Then
  120.  
  121.                     'add it to the list
  122.                     ClosedList.Add(LowNode)
  123.  
  124.                     'set this node as the current node
  125.                     CurrentX = LowNode.self.x
  126.                     CurrentY = LowNode.self.y
  127.  
  128.                 End If
  129.  
  130.             End If
  131.  
  132.         Loop
  133.  
  134.     End Sub
  135.  
  136.     'adds the neighbors of a cell to the open list
  137.     Public Sub AddNeighbors(X As Integer, y As Integer, TargetX As Integer, TargetY As Integer)
  138.  
  139.         Dim TempNode As New Node
  140.         Dim LowNode As New Node
  141.         Dim InList As Boolean = False
  142.  
  143.         For intx = -1 To 1
  144.             For inty = -1 To 1
  145.  
  146.                 'if the node is inside the map boundries
  147.                 If Not (X + intx) < 0 Then
  148.                     If Not (y + inty) < 0 Then
  149.  
  150.                         'set positions
  151.                         TempNode.self.x = X + intx
  152.                         TempNode.self.y = y + inty
  153.                         TempNode.parent.x = X
  154.                         TempNode.parent.y = y
  155.  
  156.                         'get the scores of the parent
  157.                         TempNode.GetScores(X, y)
  158.  
  159.                         'assign the parents G score
  160.                         TempNode.ParentG = TempNode.G
  161.  
  162.                         'get this nodes scores
  163.                         TempNode.GetScores(TargetX, TargetY)
  164.  
  165.                         'check the other two lists for this node
  166.                         For Each Node In OpenList
  167.                             If Node.self.x = TempNode.self.x And Node.self.y = TempNode.self.y Then
  168.                                 InList = True
  169.                             End If
  170.                         Next
  171.  
  172.                         'check the other two lists for this node
  173.                         For Each Node In ClosedList
  174.                             If Node.self.x = TempNode.self.x And Node.self.y = TempNode.self.y Then
  175.                                 InList = True
  176.                             End If
  177.                         Next
  178.  
  179.                         'add to the next open list if it's not already in either list
  180.                         If Not InList Then
  181.                             OpenList.Add(TempNode)
  182.                         End If
  183.  
  184.                     End If
  185.                 End If
  186.  
  187.             Next
  188.         Next
  189.  
  190.  
  191.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment