Guest User

Untitled

a guest
Nov 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. self.path = []
  2.  
  3. closedSet = set ()
  4. openSet = set ()
  5.  
  6. openSet.add (self.pathStart)
  7.  
  8. cameFrom = {}
  9. gScore = {}
  10. hScore = {}
  11. fScore = {}
  12.  
  13. cameFrom [self.pathStart] = None
  14. gScore [self.pathStart] = 0
  15. hScore [self.pathStart] = heuristic (self.pathStart, self.pathEnd)
  16. fScore [self.pathStart] = gScore [self.pathStart] + hScore [self.pathStart]
  17.  
  18. while len (openSet) > 0:
  19. # Find the item in the open set with the lowest f score
  20. minFNode = None
  21. for x in openSet:
  22. if not minFNode or fScore [x] < fScore [minFNode]:
  23. minFNode = x
  24.  
  25. # If we got to the end, then reconstruct our path and exit
  26. if minFNode == self.pathEnd:
  27. curNode = self.pathEnd
  28. while cameFrom [curNode]:
  29. self.path.append (cameFrom [curNode])
  30. curNode = cameFrom [curNode]
  31. return True
  32.  
  33. openSet.remove (minFNode)
  34. closedSet.add (minFNode)
  35.  
  36. neighbors = []
  37.  
  38. if minFNode [0] > 0:
  39. # Left
  40. neighbors.append ((minFNode [0] - 1, minFNode [1]))
  41. if minFNode [0] < GRID_WIDTH - 1:
  42. # Right
  43. neighbors.append ((minFNode [0] + 1, minFNode [1]))
  44. if minFNode [1] > 0:
  45. # Up
  46. neighbors.append ((minFNode [0], minFNode [1] - 1))
  47. if minFNode [1] < GRID_HEIGHT - 1:
  48. # Down
  49. neighbors.append ((minFNode [0], minFNode [1] + 1))
  50.  
  51. for x in neighbors:
  52. if not self.terrainGrid.passable [x [1]][x [0]]:
  53. continue
  54.  
  55. if x in closedSet:
  56. continue
  57.  
  58. # The 1 is distance to neighbor, I'm not using diagonals yet
  59. tentGScore = gScore [minFNode] + 1
  60. tentIsBetter = False
  61.  
  62. if not x in openSet:
  63. openSet.add (x)
  64. tentIsBetter = True
  65. elif tentGScore < gScore [x]:
  66. tentIsBetter = True
  67.  
  68. if tentIsBetter:
  69. cameFrom [x] = minFNode
  70. gScore [x] = tentGScore
  71. hScore [x] = heuristic (x, self.pathEnd)
  72. fScore [x] = gScore [x] + hScore [x]
  73. return False
Add Comment
Please, Sign In to add comment