Advertisement
Guest User

Untitled

a guest
Nov 1st, 2013
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. from itertools import product
  2. from math import sqrt
  3.  
  4. class Pathfinding:
  5.     def __init__(self, width, height, impassable):
  6.  
  7.         nodes = [[Gridnode(x, y) for y in range(height)] for x in xrange(width)]
  8.         graph = {}
  9.  
  10.         for x, y in product(range(width), range(height)):
  11.             node = nodes[x][y]
  12.             graph[node] = []
  13.  
  14.             for i, j in product([-1, 0, 1], [-1, 0, 1]):
  15.                 if not (0 <= x + i < width):
  16.                     continue
  17.                 if not (0 <= y + j < height):
  18.                     continue
  19.                 if (x + i, y + j) in impassable:
  20.                     continue
  21.                 graph[nodes[x][y]].append(nodes[x+i][y+j])
  22.  
  23.         self.graph, self.nodes = graph, nodes
  24.  
  25.     def heuristic(self, node, goal):
  26.         D = 10
  27.         dx = abs(node.x - goal.x)
  28.         dy = abs(node.y - goal.y)
  29.         return D * max(dx, dy)
  30.  
  31.     def move_cost(self, current, node):
  32.        cross = abs(current.x - node.x) == 1 and abs(current.y - node.y) == 1
  33.        return 19 if cross else 10
  34.  
  35.     def search(self, start, end):
  36.         sx, sy = start[0], start[1]
  37.         ex, ey = end[0], end[1]
  38.  
  39.         start = self.nodes[sx][sy]
  40.         end = self.nodes[ex][ey]
  41.  
  42.         openset = set()
  43.         closedset = set()
  44.         current = start
  45.         openset.add(current)
  46.         start.parent = None
  47.  
  48.         while openset:
  49.             current = min(openset, key=lambda o:o.g + o.h)
  50.  
  51.             if current == end:
  52.                 path = []
  53.  
  54.                 while current.parent:
  55.                     path.append(current)
  56.                     current = current.parent
  57.                 path.append(current)
  58.                 return path[::-1]
  59.  
  60.             openset.remove(current)
  61.             closedset.add(current)
  62.  
  63.             for node in self.graph[current]:
  64.                 if node in closedset:
  65.                     continue
  66.                 if node in openset:
  67.                     new_g = current.g + self.move_cost(current, node)
  68.                     if node.g > new_g:
  69.                         node.g = new_g
  70.                         node.parent = current
  71.                 else:
  72.                     node.g = current.g + self.move_cost(current, node)
  73.                     node.h = self.heuristic(node, end)
  74.                     node.parent = current
  75.                     openset.add(node)
  76.        
  77.         return None
  78.  
  79.  
  80. class Gridnode:
  81.     def __init__(self, x, y):
  82.         self.x, self.y = x, y
  83.         self.g = 0
  84.         self.h = 0
  85.         self.parent = None
  86.  
  87. def main():
  88.     from sandbox import SandboxMapMenu
  89.     from cocos.director import director
  90.     from cocos.scene import Scene
  91.  
  92.     director.init(3*96, 2*96, vsync=False, do_not_scale=True)
  93.     director.run(Scene(SandboxMapMenu()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement