def possible_moves(self, pos: Point) -> List[Point]: """ searches for directions that a corridor can expand used by build_corridors() :param pos: index of tile in grid to find possible moves :type pos: Point :return: list of potential points the path could move :rtype: List[Point] """ # logger.debug(f"inside possible_moves {pos}") available_squares = [] for direction in Direction.cardinal(): # logger.debug(f"direction = {direction}") neighbor = pos + direction # logger.debug(f"neighbor = {neighbor}") if neighbor.x < 1 or self.width - 2 < neighbor.x or neighbor.y < 1 or self.height - 2 < neighbor.y: # logger.debug(f"{neighbor} not in bounds") continue if self.can_carve(pos, direction): # logger.debug(f"can_carve returned True pos={pos}, direction={direction}") available_squares.append(neighbor) # logger.debug(f"available squares:") # for square in available_squares: # logger.debug(f"square={square}") # logger.add("debug.log") return available_squares
def grow_maze(self, start: Point, label: TileType = None): """ :param start: :type start: Point :param label: :type label: map_objects.tile.TileType """ if label is None: label = TileType.CORRIDOR tiles = [] # tiles to check last_direction = Point(0, 0) if not self.can_place(start, last_direction): return region = self.new_region() self.place_tile(start, region=region, label=label) self.corridors.append(start) tiles.append(start) while len(tiles) > 0: tile = tiles[-1] # grab last tile # see which neighboring tiles can be carved open_tiles = [] for d in Direction.cardinal(): if self.can_place(tile, d): open_tiles.append(d) if len(open_tiles) > 0: if (last_direction in open_tiles and randint(1, 101) > self.winding_percent): current_direction = last_direction else: # TODO: refactor for random.choice() current_direction = open_tiles[randint( 0, len(open_tiles) - 1)] self.place_tile(tile + current_direction, region=region, label=label) self.corridors.append(tile + current_direction) tiles.append(tile + current_direction) # * 2) last_direction = current_direction else: tiles.remove(tile) last_direction = None
def find_direct_neighbors(self, point: Point): """ used by possible_moves :param point: :type point: :return: :rtype: """ return self.find_neighbors(point, neighbors=Direction.cardinal())
def grow_maze(self, start: Point, label: TileType = None): """ :param start: :type start: Point :param label: :type label: TileType """ if label is None: label = TileType.CORRIDOR tiles = [] last_direction = Point(0, 0) region = self.new_region() self.carve(start, region, label) tiles.append(start) while len(tiles) > 0: tile = tiles.pop(-1) # grab last tile # see which neighboring tiles can be carved open_tiles = [] for d in Direction.cardinal(): if self.can_carve(tile, d): # print("True") open_tiles.append(d) # else: # print("False") if len(open_tiles) > 0: if (last_direction in open_tiles and randint(1, 101) > self.winding_percent): current_direction = last_direction else: current_direction = open_tiles[randint( 0, len(open_tiles) - 1)] self.carve(tile + current_direction, region, label) self.carve(tile + current_direction * 2, region, label) open_tiles.append(tile + current_direction * 2) last_direction = current_direction else: # end current path last_direction = None
def find_neighbors(self, point: Point, neighbors: Direction = None): """ used by find_direct_neighbors :param point: :type point: Point :param neighbors: direction for neighbors to check, defaults to None :type neighbors: Direction :return yields new point in direction(s) chosen """ if neighbors is None: neighbors = Direction.every() for direction in neighbors: new_point = point + direction if not self.dungeon.in_bounds(new_point): continue yield new_point
def build_corridors(self, start_point: Point = None): cells = [] if start_point is None: start_point = Point( x=randint(1, self.width - 2), y=randint(1, self.height - 2), ) # TODO: refactor can_carve attempts = 0 while not self.can_carve(start_point, Direction.self()): attempts += 1 start_point = Point( x=randint(1, self.width - 2), y=randint(1, self.height - 2), ) # TODO: need to remove this hard stop once everything is combined if attempts > 100: break self.carve(point=start_point, region=self.new_region(), label=TileType.CORRIDOR) # add point to corridor list self.corridors.append(start_point) # add point to open cell list cells.append(start_point) while cells: middle = len(cells) // 2 start_point = cells[-1] possible_moves = self.possible_moves(start_point) if possible_moves: point = choice(possible_moves) self.carve(point=point, region=self.current_region, label=TileType.CORRIDOR) self.corridors.append(point) cells.append(point) else: cells.remove(start_point)
def build_corridors(self, start_point: Point = None): cells = [] if start_point is None: start_point = Point(x=randint(1, self.width - 2), y=randint(1, self.height - 2)) # TODO: refactor can_carve attempts = 0 while not self.can_carve(start_point, Direction.self()): attempts += 1 start_point = Point(x=randint(1, self.width - 2), y=randint(1, self.height - 2)) # TODO: need to remove this hard stop once everything is combined if attempts > 100: break self.carve(pos=start_point, region=self.new_region(), label=TileType.CORRIDOR) # add point to corridor list self.corridors.append(start_point) # add point to open cell list cells.append(start_point) # logger.debug(f"first start_point added to cells: {cells}") attempts = 0 while cells: start_point = cells[-1] possible_moves = self.possible_moves(start_point) if possible_moves: # logger.debug(f"possible_moves is {len(possible_moves)} long") point = choice(possible_moves) # logger.debug(f"chosen point is {point}") self.carve(pos=point, region=self.current_region, label=TileType.CORRIDOR) self.corridors.append(point) cells.append(point) else: cells.remove(start_point)
def possible_moves(self, pos: Point) -> List[Point]: """ searches for directions that a corridor can expand used by build_corridors() :param pos: index of tile in grid to find possible moves :type pos: Point :return: list of potential points the path could move :rtype: List[Point] """ available_squares = [] for direction in Direction.cardinal(): neighbor = pos + direction if (neighbor.x < 1 or self.width - 2 < neighbor.x or neighbor.y < 1 or self.height - 2 < neighbor.y): continue if self.can_carve(pos, direction): available_squares.append(neighbor) return available_squares