Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- self.path = []
- closedSet = set ()
- openSet = set ()
- openSet.add (self.pathStart)
- cameFrom = {}
- gScore = {}
- hScore = {}
- fScore = {}
- cameFrom [self.pathStart] = None
- gScore [self.pathStart] = 0
- hScore [self.pathStart] = heuristic (self.pathStart, self.pathEnd)
- fScore [self.pathStart] = gScore [self.pathStart] + hScore [self.pathStart]
- while len (openSet) > 0:
- # Find the item in the open set with the lowest f score
- minFNode = None
- for x in openSet:
- if not minFNode or fScore [x] < fScore [minFNode]:
- minFNode = x
- # If we got to the end, then reconstruct our path and exit
- if minFNode == self.pathEnd:
- curNode = self.pathEnd
- while cameFrom [curNode]:
- self.path.append (cameFrom [curNode])
- curNode = cameFrom [curNode]
- return True
- openSet.remove (minFNode)
- closedSet.add (minFNode)
- neighbors = []
- if minFNode [0] > 0:
- # Left
- neighbors.append ((minFNode [0] - 1, minFNode [1]))
- if minFNode [0] < GRID_WIDTH - 1:
- # Right
- neighbors.append ((minFNode [0] + 1, minFNode [1]))
- if minFNode [1] > 0:
- # Up
- neighbors.append ((minFNode [0], minFNode [1] - 1))
- if minFNode [1] < GRID_HEIGHT - 1:
- # Down
- neighbors.append ((minFNode [0], minFNode [1] + 1))
- for x in neighbors:
- if not self.terrainGrid.passable [x [1]][x [0]]:
- continue
- if x in closedSet:
- continue
- # The 1 is distance to neighbor, I'm not using diagonals yet
- tentGScore = gScore [minFNode] + 1
- tentIsBetter = False
- if not x in openSet:
- openSet.add (x)
- tentIsBetter = True
- elif tentGScore < gScore [x]:
- tentIsBetter = True
- if tentIsBetter:
- cameFrom [x] = minFNode
- gScore [x] = tentGScore
- hScore [x] = heuristic (x, self.pathEnd)
- fScore [x] = gScore [x] + hScore [x]
- return False
Add Comment
Please, Sign In to add comment