astar_node.bmx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ' This is an implementation of an AStar Algorithm
  2. Strict
  3. Import "priority_queue.bmx"
  4. 'Private
  5. ' Each AStar node has a list of neighbours that it connects to
  6. ' This is the AStarNeighbourInfo type. AStarNeighbourInfo
  7. ' holds both a reference to the neighbouring nodes, but also information
  8. ' about the cost to get to that node
  9. Type AStarNeighbourInfo
  10. Method getNode:AStarNode()
  11. Return _neighbour
  12. End Method
  13. Method edgeCost:Float()
  14. Return _edgeCost
  15. End Method
  16. Method free()
  17. _neighbour = Null
  18. End Method
  19. ' private
  20. Field _neighbour:AStarNode
  21. Field _edgeCost:Float ' Cost to get to this neighbour
  22. EndType
  23. Public
  24. ' These are the nodes in the AStar graph
  25. Type AStarNode Extends PriorityQueueNode
  26. ' public
  27. ' Call this to add a fully constructed neighbour node to this node
  28. Method addNeighbour(neighbourNode:AStarNode, edgeCost:Float)
  29. Local neighbourInfo:AStarNeighbourInfo = New AStarNeighbourInfo
  30. neighbourInfo._neighbour = neighbourNode
  31. neighbourInfo._edgeCost = edgeCost
  32. _neighbours.AddLast(neighbourInfo)
  33. End Method
  34. Method getNeighbours:TList()
  35. Return _neighbours
  36. End Method
  37. Method setGoalDistance(goalDistance:Float)
  38. _goalDistance = goalDistance
  39. End Method
  40. Method goalDistance:Float()
  41. Return _goalDistance
  42. End Method
  43. Method setCostToGetHere(cost:Float)
  44. _costFromStart = cost
  45. End Method
  46. Method costToGetHere:Float()
  47. Return _costFromStart
  48. End Method
  49. ' Yes using PTR here is what we want
  50. Method setParent(parent:AStarNode)
  51. _parentNode = parent
  52. End Method
  53. Method getParent:AStarNode()
  54. Return _parentNode
  55. End Method
  56. ' Sets whether the node is in the open list or not
  57. Method setInOpen(inOpen:Int)
  58. _inOpen = inOpen
  59. End Method
  60. Method inOpen()
  61. Return _inOpen
  62. End Method
  63. ' Sets whether the node is in the closed list or not
  64. Method setInClosed(inClosed:Int)
  65. _inClosed = inClosed
  66. End Method
  67. Method inClosed()
  68. Return _inClosed
  69. End Method
  70. ' Initialise new nodes to default values
  71. Method New()
  72. _costFromStart = 0
  73. _parentNode = Null
  74. _neighbours = New TList
  75. _inOpen = False
  76. _inClosed = False
  77. _goalDistance = 0
  78. End Method
  79. ' private
  80. Field _costFromStart:Float ' We keep track of cost to get to this node
  81. Field _goalDistance:Float ' Goal to distance
  82. Field _parentNode:AStarNode ' Used to trace back from the end to the start of the finished path
  83. Field _neighbours:TList ' The neighbours of this node
  84. Field _inOpen:Int ' Keep track of what list the node is in
  85. Field _inClosed:Int
  86. ' TODO:
  87. ' These are required in order to calculate distance
  88. ' What would be better is to provide something like an AStarPos object
  89. ' with a method to get distance between them that way you could provide a more
  90. ' generic mechanism for distanc calculation
  91. Field _x:Int
  92. Field _y:Int
  93. End Type