Пример #1
0
    def _run(self, region):

        # TODO: Randomize split amount
        # TODO: Set limits. This is done with the node recursion so far.
        # TODO: Support recursion terminator that still allows the region to
        # be split in the other direction.

        #print '*' * 35
        #print region, 'width:', region.width, 'height:', region.height

        leaves = []
        axis = self.split_dir.run()
        if axis == Axis.X:
            #print 'width:', region.width
            #print 1, region.width - 1
            split = region.x1 + random.randint(1, region.width - 1)
            #print 'split:', split
            leaves.extend([
                Region(region.x1, region.y1, split, region.y2),
                Region(split, region.y1, region.x2, region.y2),
            ])
        else:
            #print 'height:', region.height
            #print 1, region.height - 1
            split = region.y1 + random.randint(1, region.height - 1)
            #print 'split:', split
            leaves.extend([
                Region(region.x1, region.y1, region.x2, split),
                Region(region.x1, split, region.x2, region.y2),
            ])

        #for leave in leaves:
        #    print '->', leave, 'width:', leave.width, 'height:', leave.height

        return leaves
Пример #2
0
 def _run(self, region):
     regions = []
     width = self.width.run()
     num_rows = region.width / width
     for i in range(num_rows):
         regions.append(
             Region(region.x1 + (i * width), region.y1,
                    region.x1 + ((i + 1) * width), region.y2))
     return regions
Пример #3
0
    def _run(self, p_region):
        regions = []
        for i in xrange(self.max_iters):

            # Randomise some dimensions.
            #w = random.randint(self.min_size, self.max_size)
            #h = random.randint(self.min_size, self.max_size)
            w = self.width_sampler.run()
            h = self.height_sampler.run()

            # Randomise some coords. Weight them towards the center if
            # specified.
            x, y = 0, 0
            rx = p_region.width - w
            ry = p_region.height - h

            if self.center_weight is not None:
                xWeights = {
                    i: center_weight(i / (float(rx) - 1) * 180.0)
                    for i in range(rx)
                }
                yWeights = {
                    i: center_weight(i / (float(ry) - 1) * 180.0)
                    for i in range(ry)
                }
                x = utils.get_weighted_choice(xWeights.items())
                y = utils.get_weighted_choice(yWeights.items())
            else:
                x = random.randint(0, rx)
                y = random.randint(0, ry)

            # Check for overlap with previous rooms.
            region = Region(x, y, x + w, y + h)
            if self.intersect:
                regions.append(region)
            else:
                for other_region in regions:
                    if region.intersects(other_region):
                        break
                else:
                    regions.append(region)

        return regions
Пример #4
0
 def _run(self, region):
     regions = []
     height = self.height.run()
     num_cols = region.height / height
     for i in range(num_cols):
         regions.append(Region(
             region.x1, 
             region.y1 + (i * height),
             region.x2, 
             region.y1 + ((i + 1) * height)
         ))
     return regions
Пример #5
0
    def _run(self, region):
        regions = []

        num_cols = self.num_cols.run()
        num_rows = self.num_rows.run()

        x = region.width / float(num_cols)
        y = region.height / float(num_rows)

        for i in range(num_cols):
            x_offset = region.x1 + i * x
            for j in range(num_rows):
                y_offset = region.y1 + j * y
                regions.append(Region(x_offset, y_offset, x_offset + x, y_offset + y))

        return regions
Пример #6
0
 def _get_padding_region(self, region):
     x1 = y1 = x2 = y2 = 0
     if self.padding is not None:
         x1 = y1 = x2 = y2 = self.padding.run()
     if self.padding_x1 is not None:
         x1 = self.padding_x1.run()
     if self.padding_y1 is not None:
         y1 = self.padding_y1.run()
     if self.padding_x2 is not None:
         x2 = self.padding_x2.run()
     if self.padding_y2 is not None:
         y2 = self.padding_y2.run()
     return Region(
         region.x1 + x1,
         region.y1 + y1,
         region.x2 - x2,
         region.y2 - y2,
     )
Пример #7
0
def on_draw():
    window.clear()

    grid = 40
    pRegion = Region(0, 0, WIDTH / grid, HEIGHT / grid)
    sineWeight = SineWeight(1, invert=True)
    regions = random_regions(pRegion,
                             5,
                             20,
                             30,
                             intersect=True,
                             center_weight=sineWeight)

    for region in regions:
        #colour = pglib.utils.get_random_colour(a=0.05)
        #rectArgs = [region.x1 * grid, region.y1 * grid, region.x2 * grid, region.y2 * grid]
        #drawFns.append(partial(rectangle, *rectArgs, fill=colour, stroke=(1, 0, 0, 0.75)))
        Rect(
            Point2d(region.x1 * grid_spacing, region.y1 * grid_spacing),
            Point2d(region.x2 * grid_spacing, region.y2 * grid_spacing),
            colour=None,
            line_colour=utils.get_random_colour(1),
            line_width=4,
        ).draw()
Пример #8
0
import logging

from pglib.draw.pyglet.appbase import AppBase
from pglib.generators.bspregions import BspRegions
from pglib.node import Node
from pglib.region import Region
from pglib.samplers.range import Range

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)

MIN_LEAF_SIZE = 6
MAX_LEAF_SIZE = 20
GRID_SPACING = 10
WIDTH = 64
HEIGHT = 48
WINDOW_WIDTH = WIDTH * GRID_SPACING
WINDOW_HEIGHT = HEIGHT * GRID_SPACING

# TODO: Port remaining bspregion stuff to bsp
# Create tree.
root = Node('root',
            BspRegions(Range(MIN_LEAF_SIZE, MAX_LEAF_SIZE), padding=Range(5)))

# Add some input data.
root.add_input(Region(0, 0, WIDTH, HEIGHT))

# Create test app and run.
app = AppBase(root, GRID_SPACING, WINDOW_WIDTH, WINDOW_HEIGHT)
app.run()
Пример #9
0
from pglib.region import Region
from pglib.samplers.choice import Choice
from pglib.samplers.constant import Constant
from pglib.samplers.range import Range

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)

GRID_SPACING = 10
WIDTH = 100
HEIGHT = 100
WINDOW_WIDTH = WIDTH * GRID_SPACING
WINDOW_HEIGHT = HEIGHT * GRID_SPACING

# Create tree.
block = Node('block', Grid(Range(2, 4), Range(2, 4)))
footpath = Node('footpath', RegionBase(padding=Constant(1)))
building = Node(
    'footpath',
    Bsp(Choice([Axis.X, Axis.Y])),
    recurse_while_fn=lambda i, r: r.data.width > 5 and r.data.height > 5)

block.add_child(footpath)
footpath.add_child(building)

# Add some input data.
block.add_input(Region(0, 0, WIDTH, HEIGHT))

# Create test app and run.
app = AppBase(block, GRID_SPACING, WINDOW_WIDTH, WINDOW_HEIGHT)
app.run()
Пример #10
0
from pglib.region import Region
from pglib.samplers.constant import Constant


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)


GRID_SPACING = 10
WIDTH = 30
HEIGHT = 20
WINDOW_WIDTH = WIDTH * GRID_SPACING
WINDOW_HEIGHT = HEIGHT * GRID_SPACING


# Create tree.
root = Node('root', Row(Constant(2), padding=Constant(1)))
node_a = Node('every_second', Column(Constant(6)))
node_b = Node('every_other', Column(Constant(6), padding_x1=Constant(3)))

root.add_child(node_a, lambda data: data[slice(0, len(data), 2)])
root.add_child(node_b, lambda data: data[slice(1, len(data), 2)])
node_a.add_child(Node('bottom', Image('pillar_bottom')))
node_b.add_child(Node('top', Image('pillar_top')))

# Add some input data.
root.add_input(Region(5, 0, WIDTH - 5, HEIGHT))

# Create test app and run.
app = AppBase(root, GRID_SPACING, WINDOW_WIDTH, WINDOW_HEIGHT)
app.run()