Example #1
0
    def grow_rect(self, rect, growth, rects, direction):
        """Tries to grow a rectangle in the specified direction

        Returns whether the growth succeeded"""
        changed = False
        if growth[direction]:
            left, top, width, height = rect.x, rect.y, rect.w, rect.h
            if direction == LEFT:
                left -= 1
                width += 1
            elif direction == RIGHT:
                width += 1
            elif direction == DOWN:
                height += 1
            elif direction == UP:
                top -= 1
                height += 1
            new = Rect(left, top, width, height)
            if not (set(util.points_in(new)) - self.space) and len(new.collidelistall(rects)) == 1:
                rect.left = left
                rect.width = width
                rect.top = top
                rect.height = height
                changed = True
                #if rect.width >= 8 and rect.height >= 8 and random.randrange(5) == 0:
                #    growth[direction] = False
            else:
                growth[direction] = False

        return changed
Example #2
0
    def voronoi_ish(self, rects):
        nearest = defaultdict(set)
        for rect in rects:
            p = self.points[0]
            dist = util.manhattan(rect.center, p)
            for q in self.points[1:]:
                d = util.manhattan(rect.center, q)
                if d < dist:
                    dist = d
                    p = q
            nearest[p] |= set(util.points_in(rect))

        for (i, p) in enumerate(self.points):
            for q in self.points[i+1:]:
                self.borders += nearest[p] & nearest[q]
Example #3
0
    def generate(self):
        # Fill with grass
        self.level.draw_square(-self.width/2-self.turret_project-10,
                               -self.turret_project-10,
                               self.width/2-self.turret_project+11,
                               self.height+self.turret_project+10, grass)
        # Draw outer walls
        self.fourwalls(self.width,
                       self.height,
                       self.turretsize,
                       self.wallwidth,
                       self.turret_project,
                       self.gatesize,
                       self.gatehouse_project,
                       4)

        # Keep dimensions
        k_gs = (self.gatesize + 2) % 4 if self.gatesize != 2 else 4
        k_w = (random.randint(int(self.width*0.3), int(self.width*0.5))/2)*2 + k_gs
        k_h = random.randint(int(self.height*0.3), int(self.height*0.5))

        keep_style = random.choice(KEEP_STYLES)
        if keep_style == KEEP_CENTRE:
            self.level.translate(0, (self.height-k_h)/2)
        elif keep_style == KEEP_SIDE:
            side = random.choice((-1, 1))
            self.level.translate((self.width-k_w)/2*side, (self.height-k_h))

        # Draw keep
        self.fourwalls(k_w, k_h,
                       self.turretsize,
                       self.wallwidth+1,
                       self.turret_project,
                       k_gs, 0, 4)

        # Calculate valid space for buildings
        interior = Rect(-self.width/2+self.wallwidth+1, self.wallwidth,
                        self.width-self.wallwidth*2, self.height-self.wallwidth*2+1)
        space = set(util.points_in(interior))

        margin = 6
        if keep_style == KEEP_CENTRE:
            self.level.translate(0, -((self.height-k_h)/2))
            around_keep = Rect(-k_w/2-(self.wallwidth+margin), self.height/2-k_h/2-self.wallwidth-margin,
                               k_w+(self.wallwidth+margin)*2, k_h+(self.wallwidth+margin)*2)
            space -= set(util.points_in(around_keep))
        elif keep_style == KEEP_SIDE:
            self.level.translate(-((self.width-k_w)/2*side), -(self.height-k_h))
            if side == -1:
                left = -self.width/2+self.wallwidth
                width = k_w + margin
            else:
                left = self.width/2-self.wallwidth-k_w-margin
                width = k_w + margin
            around_keep = Rect(left, self.height/2-self.wallwidth-margin,
                               width, self.height)
            space -= set(util.points_in(around_keep))

        #for p in space:
        #    self.level.set_terrain(p, grass)

        # Create buildings
        graph = BuildingGraph(Rect(-self.width/2+self.wallwidth, self.wallwidth,
                                   self.width-2*self.wallwidth, self.height-2*self.wallwidth), space, 20, 1)

        # Draw building walls
        for border in graph.borders:
            self.level.draw_line(border.left, border.top,
                                 border.left, border.bottom, wall)
            self.level.draw_line(border.left, border.bottom,
                                 border.right, border.bottom, wall)
            self.level.draw_line(border.right, border.bottom,
                                 border.right, border.top, wall)
            self.level.draw_line(border.right, border.top,
                                 border.left, border.top, wall)