Advertisement
Guest User

Untitled

a guest
Apr 20th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. import numpy
  2.  
  3.  
  4. ## Generate a "heat map" that represents how far away each cell in the provided
  5. # map is from any of the goal cells. The heat map can be used to allow entities
  6. # to move towards those goal cells by simply examining their neighbors and
  7. # moving to the one with the shortest remaining distance.
  8. # \param gridMap A Numpy array where 0 represents "open" and any
  9. #         other value represents "closed"
  10. # \param goals A list (set, etc.) of (x, y) tuples representing the goal
  11. #        tiles.
  12. # \param maxDepth How far out from the goals to search; any locations further
  13. #        out will have a value of -1 in the result.
  14. def getHeatMap(gridMap, goals, maxDepth = 10**10):
  15.     # A Numpy array of integers where each value is
  16.     # the distance the corresponding cell in gridMap is from any goal tile.
  17.     # Start with all non-goal tiles at -1 and all goal tiles at 0.
  18.     heatMap = numpy.ones(gridMap.shape, dtype = numpy.int32) * -1
  19.     for x, y in goals:
  20.         heatMap[(x, y)] = 0
  21.  
  22.     xVals, yVals = numpy.where(gridMap != 0)
  23.     # Maps cells we've seen to their costs. Add unreachable cells here so we
  24.     # never try to reach them.
  25.     cellToCost = dict([((xVals[i], yVals[i]), -1) for i in xrange(len(xVals))])
  26.     # Queue of cells waiting to be scanned.
  27.     cellQueue = []
  28.     # Max values to feed into xrange when getting neighbors.
  29.     maxX = gridMap.shape[0]
  30.     maxY = gridMap.shape[1]
  31.     for x, y in goals:
  32.         cellToCost[(x, y)] = 0
  33.         for xi in xrange(max(0, x - 1), min(maxX, x + 2)):
  34.             for yi in xrange(max(0, y - 1), min(maxY, y + 2)):
  35.                 if (xi, yi) not in cellToCost and gridMap[xi, yi] == 0:
  36.                     cellToCost[(xi, yi)] = 1
  37.                     heatMap[(xi, yi)] = 1
  38.                     cellQueue.append((xi, yi))
  39.  
  40.     # Pop a cell, examine its neighbors, and add any not-yet-processed
  41.     # neighbors to the queue. This is a simple breadth-first search.
  42.     while cellQueue:
  43.         x, y = cellQueue.pop(0)
  44.         cost = cellToCost[(x, y)]
  45.         # Find all neighbors for whom we have a new route.
  46.         # We could use a nested loop here but it's about 10% slower.
  47.         for dx, dy in [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1),
  48.                 (1, 0), (1, 1)]:
  49.             neighbor = (x + dx, y + dy)
  50.             if (neighbor[0] >= 0 and neighbor[0] < maxX and
  51.                     neighbor[1] >= 0 and neighbor[1] < maxY):
  52.                 if neighbor not in cellToCost:
  53.                     cellToCost[neighbor] = cost + 1
  54.                     heatMap[neighbor] = cost + 1
  55.                     if cost + 1 < maxDepth:
  56.                         cellQueue.append(neighbor)
  57.     return heatMap
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement