예제 #1
0
    def initMap(self, w, h):
        self.mapdata = []
        self.mapw = w
        self.maph = h
        self.startpoint = [1, h / 2]
        self.endpoint = [w - 2, h / 2]
        self.current_location = AStar.SQ_Location(
            self.startpoint[0], self.startpoint[1]
        )  #Initializes the current location as the starting point
        self.next_location = deepcopy(self.current_location)
        size = w * h
        for i in range(size):
            #Alternates color, to easily visualize the grid
            if i % 2 == 0:
                self.mapdata.append(1)
            else:
                self.mapdata.append(2)

        self.mapdata[(self.startpoint[1] * w) + self.startpoint[0]] = 5
        self.mapdata[(self.endpoint[1] * w) + self.endpoint[0]] = 6
        """Create a copy of the initial configuration of the map """
        self.initial_mapdata = deepcopy(self.mapdata)
        self.maprect = Rect(0, 0, w * 32,
                            h * 32)  #each small cell is 32 pix size.
        """init astar planner """
        """first the map"""
        self.astar = AStar.AStar(
            AStar.SQ_MapHandler(self.mapdata, self.mapw, self.maph))
        """init the start and end positions"""
        start = AStar.SQ_Location(self.startpoint[0], self.startpoint[1])
        end = AStar.SQ_Location(
            self.endpoint[0],
            self.endpoint[1])  #SQ means "square" map location
        self.astar.start(start, end)
예제 #2
0
def createPaths(y, x, dungeon):
    """
    Create paths
    DONE from door to door or
    TODO from path to door or
    from door to path or
    from door to dead end or
    from path to dead end
    """
    #TODO from path to door
    #TODO from door to path
    #TODO from door to dead end
    #TODO from path to dead end
    #TODO make sure every room is accessible!

    mapDataForAstar = []
    doors = []

    for rowId in range(len(dungeon)):
        for charId in range(len(dungeon[rowId])):
            if dungeon[rowId][charId] in "#RFS":  # Check that given spot is free for building
                mapDataForAstar.append(-1)
            elif dungeon[rowId][charId] == "D":  # Check if given spot is door
                if x != charId and y != rowId:
                    doors.append([charId, rowId])  # Add door to door list
                mapDataForAstar.append(1)
            else:
                mapDataForAstar.append(1)

    # ---- Begin path finding -----
    astar = AStar.AStar(AStar.SQ_MapHandler(mapDataForAstar, len(dungeon[0]), len(dungeon)))  # Init A*
    start = AStar.SQ_Location(x, y)  # Give start point
    door = choice(doors)  # Pick door as endpoint
    end = AStar.SQ_Location(door[0], door[1])  # Make the endpoint
    p = astar.findPath(start, end)  # Find the path

    if not p:
        print "No path found!"
        return 0

    else:
        pathlines = []

        for n in p.nodes:
            pathlines.append((n.location.x, n.location.y))  # Add path points to list variable

    for point in pathlines:
        if dungeon[point[1]][point[0]] != "D":  # Make sure that no doors are overwritten
            dungeon[point[1]][point[0]] = "P"  # Write paths to map

    return 1
예제 #3
0
파일: pathf.py 프로젝트: Kriskev/sqlitebot
    def findPath(self):

        astar = AStar.AStar(
            AStar.SQ_MapHandler(self.mapdata, self.mapw, self.maph))
        start = AStar.SQ_Location(self.startpoint[0], self.startpoint[1])
        end = AStar.SQ_Location(self.endpoint[0], self.endpoint[1])

        s = time()
        p = astar.findPath(start, end)
        e = time()

        if not p:
            print "No path found!"
        else:
            #print "Found path in %d moves and %f seconds." % (len(p.nodes),(e-s))
            self.pathlines = []
            self.pathlines.append((start.x * 16 + 8, start.y * 16 + 8))
            for n in p.nodes:
                #self.pathlines.append((n.location.x*16+8,n.location.y*16+8))
                print "%d,%d" % (n.location.x, n.location.y)
예제 #4
0
    def findPath(self, start, end):
        """
		Vrti A* algoritam i pronalazi put kroz labirint.
		Vraca listu cvorova.
		"""

        array = self._mh.getGrayList()
        (width, height) = self._mh.getDimensions()
        mh = AStar.SQ_MapHandler(array, width, height)

        (xs, ys) = (start[0], start[1])
        start = AStar.SQ_Location(xs, ys)
        (xe, ye) = (end[0], end[1])
        end = AStar.SQ_Location(xe, ye)

        astar = AStar.AStar(mh)
        path = astar.findPath(start, end)
        if path == None:  # nema puta
            return None

        nodes = path.getNodes()
        return nodes