def connect(a, b): r = rooms[a] s = rooms[b] edge = list(set(Generator.get_outline(r, eight=False)) & set(Generator.get_outline(s, eight=False))) if edge: self.doors.append(random.choice(edge)) self.path[tuple(r)].append(Way(s, self.doors[-1])) return True return False
def outline(self): """ The points making up the shape's boundary @return: Sequence[(int, int)] """ return set(Generator.get_outline(self.points))
def make_buildings(self): rects = [Rect(p[0], p[1], 1, 1) for p in self.seed_points] growing = [[True] * 4 for p in self.seed_points] rects += [r for r in self.seed_rooms] growing += [[False] * 4 for r in self.seed_rooms] rects += [Rect(p[0], p[1], 1, 1) for p in self.points[self.n_seeds:]] growing += [[True] * 4 for p in self.points[self.n_seeds:]] amalgamate_limit = 10 changed = True adjacency = [[] for p in rects] for i in range(len(self.seed_points), self.n_seeds): for j in range(i+1, self.n_seeds): if set(Generator.get_outline(rects[i])) & set(Generator.get_outline(rects[j])): adjacency[i].append(j) # Expand rectangles around the points we picked while changed: changed = False for i, r in enumerate(rects): for d in [UP, DOWN, LEFT, RIGHT]: changed |= self.grow_rect(i, r, growing[i], adjacency, rects, d) self.connect_rooms(rects, adjacency) # Try and amalgamate small rectangles #merges = [None] * len(rects) #for i, r in enumerate(rects): # if r.height == 1 or r.width == 1: # r.height = 0 # r.width = 0 # if r.height*r.width < amalgamate_limit**2: # or random.randrange(6) == -1: # if self.merge_rect(i, r, merges, adjacency[i], rects): # continue #merged = [[] for i in range(len(rects))] #for i, r in enumerate(rects): # if merges[i] == None: # merged[i].append(r) # else: # merged[merges[i]].append(r) #return merged return [[r] for r in rects]
def inner_edges(self): """ The points of the shape's boundary that are not corners @return: Sequence[(int, int)] """ inner_corners = [] points = self.points for x, y in Generator.get_outline(points): if (((x - 1, y) in points and (x + 1, y) in points) or ((x, y - 1) in points and (x, y + 1) in points)): inner_corners.append((x, y)) return set(inner_corners)
def add_gaps(self, other): if isinstance(other, Shape): self.outline_gaps |= self.outline & other.points else: self.outline_gaps |= set(Generator.get_outline(self.points)) & other