Example #1
0
    def create_path(self, dest, gamemap):

        if not gamemap.is_valid_pixel_pos(dest):
            return []

        if not self.can_traverse(dest, gamemap):
            dest = self.__find_traversable_point(dest, gamemap)

        def neighbors(point):
            neighbors = []
            for x in range(-1, 2):
                for y in range(-1, 2):
                    if (x * gamemapping.squ_width, y * gamemapping.squ_width
                        ) != (0, 0) and gamemap.is_valid_pixel_pos(
                            (point[0] + x * gamemapping.squ_width,
                             point[1] + y * gamemapping.squ_width)):
                        neighbors.append(
                            (point[0] + x * gamemapping.squ_width,
                             point[1] + y * gamemapping.squ_width))
            return neighbors

        def heuristic(point, dest):
            return math.sqrt((point[0] - dest[0])**2 + (point[1] - dest[1])**2)

        def collide_squ_width(a, b):
            return a[0] >= b[0] and a[1] >= b[1] and a[
                0] <= b[0] + gamemapping.squ_width and a[
                    1] <= b[1] + gamemapping.squ_width

        came_from = {}
        cost_so_far = {}
        start_point = (int(self.location[0]), int(self.location[1]))
        current_point = start_point

        came_from[start_point] = None
        cost_so_far[start_point] = 0

        frontier = PriorityQueue()
        frontier.put(start_point, 0)

        while not frontier.is_empty():
            # print("path finding...")
            current_point = frontier.pop()

            # print("current point = ", current_point, " dest = ", dest, " ", current_point == dest)

            if collide_squ_width(current_point, dest):
                break

            for neighbor in neighbors(current_point):
                # print("neighbor ", neighbor)
                new_cost = cost_so_far[current_point] + self.get_traverse_cost(
                    neighbor, gamemap)
                if (neighbor not in cost_so_far.keys() or new_cost <
                        cost_so_far[neighbor]) and self.can_traverse(
                            neighbor, gamemap):
                    cost_so_far[neighbor] = new_cost
                    priority = cost_so_far[neighbor] + heuristic(
                        neighbor, dest)
                    frontier.put(neighbor, priority)
                    came_from[neighbor] = current_point

            # rel_point = (gamemap.rect.x + current_point[0], gamemap.rect.y + current_point[1])
            # screen.set_at(rel_point, (0, 0, 0))
            # pygame.display.flip()

        if not collide_squ_width(current_point, dest):
            return []

        path = []
        while current_point != start_point:
            # print("path construction...")
            path.insert(0, current_point)
            current_point = came_from[current_point]

        return path