Advertisement
coder0xff

CodeCombat map scanner

Apr 26th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.25 KB | None | 0 0
  1. #util.py
  2.  
  3. Pi = 3.1415926535
  4.  
  5. def utilHeartbeat():
  6.     self.moveXY(self.pos.x, self.pos.y)
  7.  
  8. #pos.py
  9.  
  10. def pos(x, y):
  11.     return {"x": x, "y": y}
  12.    
  13. def posDistSqr(_from, to):
  14.     return (to.x - _from.x)**2 + (to.y - _from.y)**2
  15.    
  16. def posDist(_from, to):
  17.     return Math.sqrt(distanceSquared(_from, to))
  18.    
  19. def posDir(_from, to):
  20.     dx = to.x - _from.x
  21.     dy = to.y - _from.y
  22.     mag = Math.sqrt(dx**2 + dy**2)
  23.     if mag < 0.001:
  24.         return pos(0, 0)
  25.     return pos(dx/mag, dy/mag)
  26.    
  27. def posSortByDist(_from, _items):
  28.     items = []
  29.     items.extend(_items)
  30.     def selectSecond(pair):
  31.         return pair[1]
  32.     pairs = []
  33.     for item in items:
  34.         pairs.append([item, distanceSquared(_from, item.pos)])
  35.     pairs = sorted(pairs, selectSecond)
  36.     items = []
  37.     for pair in pairs:
  38.         items.append(pair[0])
  39.     return items
  40.  
  41. def posFindNearest(_from, items):
  42.     cur = items[0]
  43.     curDistSqr = posDistSqr(_from, cur.pos)
  44.     for item in items:
  45.         itemDistSqr = posDistSqr(_from, item.pos)
  46.         if itemDistSqr < curDistSqr:
  47.             cur = item
  48.             curDistSqr = itemDistSqr
  49.     return cur
  50.  
  51. #map.py
  52. #needs pos.py
  53. #needs util.py
  54.  
  55. _mapGrid = []
  56. _mapSize = {"width": 0, "height": 0}
  57.  
  58. _mapUnknown = -2
  59. _mapInaccessible = 0
  60. _mapReachable = 1
  61. _mapScanStack = []
  62. #Sample the map at a two meter resolution
  63. _mapResFactor = 2
  64.  
  65. #initialize the map
  66. def _mapInit():
  67.     #begin the bucket fill from the current player location
  68.     intPos=pos(int((self.pos.x) / _mapResFactor + 0.5), int((self.pos.y) / _mapResFactor + 0.5))
  69.     _mapGrow(intPos.x + 1, intPos.y + 1)    
  70.     _mapGrid[intPos.y][intPos.x] = _mapReachable        
  71.     _mapScanStack.append(intPos)
  72.     while len(_mapScanStack) > 0:
  73.         _mapScan()
  74.         utilHeartbeat()
  75.  
  76.     #optimized kernel application
  77.     #to relax the reachable boundary
  78.     repeatCount = Math.ceil(2.0 / _mapResFactor)
  79.     for repeat in range(repeatCount):
  80.         for x in range(_mapSize.width - 1, 0, -1):
  81.             for y in range(_mapSize.height):
  82.                 _mapGrid[y][x] &= _mapGrid[y][x - 1]
  83.     utilHeartbeat()
  84.  
  85.     for repeat in range(repeatCount):
  86.         for x in range(_mapSize.width - 1):
  87.             for y in range(_mapSize.height):
  88.                 _mapGrid[y][x] &= _mapGrid[y][x + 1]
  89.     utilHeartbeat()
  90.    
  91.     for repeat in range(repeatCount):
  92.         for y in range(_mapSize.height - 1, 0, -1):
  93.             for x in range(_mapSize.width):
  94.                 _mapGrid[y][x] &= _mapGrid[y - 1][x]
  95.     utilHeartbeat()
  96.    
  97.     for repeat in range(repeatCount):
  98.         for y in range(_mapSize.height - 1):
  99.             for x in range(_mapSize.width):
  100.                 _mapGrid[y][x] &= _mapGrid[y + 1][x]
  101.     utilHeartbeat()
  102.  
  103. #ensure that _mapGrid is large enough    
  104. def _mapGrow(width, height):
  105.     if width > _mapSize.width:
  106.         for y in range(_mapSize.height):
  107.             _mapGrid[y].extend([_mapUnknown]*(width - _mapSize.width))
  108.     for y in range(_mapSize.height, height):
  109.         _mapGrid.append([_mapUnknown]*width)
  110.     _mapSize.width = width
  111.     _mapSize.height = height    
  112.    
  113. def _mapScan():
  114.     #limit to 300 iterations, so heartbeats can happen
  115.     allowedStepCount = 300
  116.     while len(_mapScanStack) > 0 and allowedStepCount > 0:
  117.         allowedStepCount -= 1
  118.         #perform an iteration of the bucket fill
  119.         current = _mapScanStack.pop()
  120.         _mapGrow(current.x + 2, current.y + 2)
  121.         directions = [pos(-1, 0), pos(0, -1), pos(1, 0), pos(0, 1)]
  122.         for direction in directions:
  123.             xOffset = direction.x
  124.             yOffset = direction.y
  125.             next = pos(current.x + xOffset, current.y + yOffset)
  126.             if _mapGrid[next.y][next.x] != _mapUnknown:
  127.                 continue
  128.             actualCurrent = pos(current.x * _mapResFactor, current.y * _mapResFactor)
  129.             actualNext = pos(next.x * _mapResFactor, next.y * _mapResFactor)
  130.             #use a ray trace to detect reachable grids
  131.             passable = self.isPathClear(actualCurrent, actualNext)
  132.             if passable:
  133.                 _mapGrid[next.y][next.x] = _mapReachable
  134.                 _mapScanStack.append(next)
  135.             else:
  136.                 _mapGrid[next.y][next.x] = _mapInaccessible
  137.  
  138. _mapInit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement