Ejemplo n.º 1
0
    def grow_room(self, room, growing, max_size, pad_v=0, pad_h=0, space=None):
        """Tries to grow a room in the specified direction

        Returns whether the growth succeeded"""
        space = space if space is not None else self.interior_space
        for d, grow in enumerate(growing):
            if not grow:
                continue
            if (((d == LEFT or d == RIGHT) and room.w > max_size) or
                    ((d == UP or d == DOWN) and room.h > max_size)):
                growing[d] = False
                continue
            left, top, width, height = room.x, room.y, room.w, room.h
            if d == LEFT:
                left -= 1
                width += 1
                if room.w <= 1:
                    collision = None
                else:
                    collision = Rect(room.x - pad_h, room.y + 1 - pad_v,
                                     1 + pad_h, max(1, room.h + 2 * pad_v - 2))
            elif d == RIGHT:
                width += 1
                if room.w <= 1:
                    collision = None
                else:
                    collision = Rect(room.right - 1 - pad_h, room.y + 1,
                                     1 + pad_h, max(1, room.h + 2 * pad_v - 2))
            elif d == DOWN:
                height += 1
                if room.h <= 1:
                    collision = None
                else:
                    collision = Rect(room.x + 1 - pad_h, room.bottom - 1,
                                     max(1, room.w - 2 + 2 * pad_h), 1 + pad_v)
            elif d == UP:
                top -= 1
                height += 1
                if room.h <= 1:
                    collision = None
                else:
                    collision = Rect(room.x + 1 - pad_h, room.y - pad_v,
                                     max(1, room.w - 2 + 2 * pad_h), 1 + pad_v)
            if collision is not None:
                building_collisions = collision.collidelistall([r.rect for r in self.shapes if isinstance(r, Room)])
            else:
                building_collisions = []
            if not (set(Generator.get_rect(collision)) - space) and len(building_collisions) == 0:
                room.left = left
                room.width = width
                room.top = top
                room.height = height
            else:
                print room.rect, collision, d, building_collisions, (set(Generator.get_rect(collision)) - space)
                growing[d] = False
Ejemplo n.º 2
0
    def grow_rect(self, i, rect, growth, adjacency, rects, direction):
        """Tries to grow a rectangle in the specified direction

        Returns whether the growth succeeded"""
        if rect.w > 100 or rect.h > 100:
            growth[direction] = False
            return False
        if growth[direction]:
            left, top, width, height = rect.x, rect.y, rect.w, rect.h
            if direction == LEFT:
                left -= 1
                width += 1
                if height > 1:
                    collision = Rect(rect.x, rect.y+1, 1, rect.h-2)
                else:
                    collision = Rect(rect.x, rect.y, 1, 1)
            elif direction == RIGHT:
                width += 1
                if height > 1:
                    collision = Rect(rect.right-1, rect.y+1, 1, rect.h-2)
                else:
                    collision = Rect(rect.right-1, rect.y, 1, 1)
            elif direction == DOWN:
                height += 1
                if width > 1:
                    collision = Rect(rect.x+1, rect.bottom-1, rect.w-2, 1)
                else:
                    collision = Rect(rect.x, rect.bottom-1, 1, 1)
            elif direction == UP:
                top -= 1
                height += 1
                if width > 1:
                    collision = Rect(rect.x+1, rect.y, rect.w-2, 1)
                else:
                    collision = Rect(rect.x, rect.y, 1, 1)
            building_collisions = collision.collidelistall(rects)
            try:
                building_collisions.remove(i)
            except ValueError:
                pass
            if not (set(Generator.get_rect(collision)) - self.space) and len(building_collisions) == 0:
                rect.left = left
                rect.width = width
                rect.top = top
                rect.height = height
                #if rect.width >= 8 and rect.height >= 8 and random.randrange(5) == 0:
                #    growth[direction] = False
                return True
            else:
                growth[direction] = False
                if building_collisions:
                    care_about = [j for j in building_collisions if j < len(self.points)]
                    # If we collided with a building, make a note.
                    adjacency[i] += care_about
                    for j in care_about:
                        adjacency[j].append(i)

        return False
Ejemplo n.º 3
0
 def points(self):
     return set(Generator.get_rect(self.rect))