예제 #1
0
def test_raw_section_simple():
    region = EmptyRegion(0, 0)

    air = Block('air')
    palette = (air, Block('white_concrete'), Block('light_gray_concrete'),
               Block('gray_concrete'))

    blocks = array.array('B')
    for z in range(16):
        for x in range(16):
            d = x * x + z * z
            if d < 25:
                blocks.append(1)
            elif d < 100:
                blocks.append(2)
            elif d < 225:
                blocks.append(3)
            else:
                blocks.append(0)

    blocks.extend(0 for _ in range(16 * 16 * 16 - len(blocks)))

    assert len(blocks) == 16 * 16 * 16

    section = RawSection(0, blocks, palette)
    region.add_section(section, 0, 0)

    region.save()
예제 #2
0
def test_raw_section():
    region = EmptyRegion(0, 0)

    block = Block('iron_block')
    air = Block('air')
    palette = (air, block)

    def func(x: int, z: int) -> int:
        return math.sin(x * x + z * z) * 0.5 + 0.5

    w = 256
    chunk_w = w // 16
    scale = 0.05
    y_scale = 15

    start = time.time()

    # from 0 to y_scale
    heights = array.array('B')
    for z in range(w):
        for x in range(w):
            u = z - w / 2
            v = x - w / 2
            height = int(func(u * scale, v * scale) * y_scale)
            heights.append(height)

    for chunk_x in range(chunk_w):
        for chunk_z in range(chunk_w):
            blocks = array.array('B')
            for y in range(16):
                for z in range(16):
                    for x in range(16):
                        rx = x + chunk_x * 16
                        rz = z + chunk_z * 16
                        i = rz * w + rx
                        height = heights[i]
                        if y == height:
                            blocks.append(1)
                        else:
                            blocks.append(0)
            region.add_section(RawSection(0, blocks, palette), chunk_x,
                               chunk_z)

    end = time.time()

    LOGGER.info(f'Generating took: {end - start:.3f}s')

    times = []
    n = 3
    for _ in range(n):
        start = time.time()
        region.save()
        end = time.time()
        times.append(end - start)

    LOGGER.info(
        f'Saving (average of {n}) took: {sum(times) / len(times):.3f}s')
예제 #3
0
def test_benchmark():
    region = EmptyRegion(0, 0)

    block = Block('iron_block')

    def func(x: int, z: int) -> int:
        return math.sin(x * x + z * z) * 0.5 + 0.5

    w = 256
    scale = 0.05
    y_scale = 15

    start = time.time()
    for x in range(w):
        for z in range(w):
            u = x - w / 2
            v = z - w / 2
            y = int(func(u * scale, v * scale) * y_scale)
            region.set_block(block, x, y, z)
    end = time.time()

    LOGGER.info(f'Generating took: {end - start:.3f}s')

    times = []
    n = 3
    for _ in range(n):
        start = time.time()
        region.save()
        end = time.time()
        times.append(end - start)

    LOGGER.info(
        f'Saving (average of {n}) took: {sum(times) / len(times):.3f}s')
예제 #4
0
def test_index():
    region = EmptyRegion(0, 0)

    region.set_block(Block('minecraft', 'dirt'), 2, 0, 0)
    region.set_block(Block('minecraft', 'stone'), 3, 0, 0)
    blocks = 'stone,dirt,oak_planks,sand,bedrock'.split(',')
    for i, block in enumerate(blocks):
        region.set_block(Block('minecraft', block), i % 16, 0, i // 16)

    region = Region(region.save())

    for i, block in enumerate(region.get_chunk(0, 0).stream_blocks(index=2)):
        i += 2
        if i < len(blocks):
            assert block.id == blocks[i]
        else:
            assert block.id == 'air'
예제 #5
0
def test_index_5bits():
    region = EmptyRegion(0, 0)

    region.set_block(Block('minecraft', 'dirt'), 2, 0, 0)
    region.set_block(Block('minecraft', 'stone'), 3, 0, 0)
    blocks = 'stone,dirt,oak_planks,sand,bedrock,white_wool,red_wool,green_wool,sponge,awesome,these,dont,need,to,exist,foo,bar'.split(
        ',')
    for i, block in enumerate(blocks):
        region.set_block(Block('minecraft', block), i % 16, 0, i // 16)

    region = Region(region.save())

    for i, block in enumerate(region.get_chunk(0, 0).stream_blocks(index=17)):
        i += 17
        if i < len(blocks):
            assert block.id == blocks[i]
        else:
            assert block.id == 'air'
예제 #6
0
def test_4bits():
    region = EmptyRegion(0, 0)

    region.set_block(Block('minecraft', 'stone'), 0, 0, 0)
    region.set_block(Block('minecraft', 'dirt'), 1, 0, 0)
    region.set_block(Block('minecraft', 'oak_planks'), 2, 0, 0)
    region.set_block(Block('minecraft', 'sand'), 10, 7, 5)
    region.set_block(Block('minecraft', 'white_wool'), 8, 6, 0)
    region.set_block(Block('minecraft', 'bedrock'), 15, 15, 15)

    region = Region(region.save())

    for i, block in enumerate(region.get_chunk(0, 0).stream_blocks()):
        if i == 0:
            assert block.id == 'stone'
        elif i == 1:
            assert block.id == 'dirt'
        elif i == 2:
            assert block.id == 'oak_planks'
        elif i == coord_to_index(10, 7, 5):
            assert block.id == 'sand'
        elif i == coord_to_index(15, 15, 15):
            assert block.id == 'bedrock'
        elif i == coord_to_index(8, 6, 0):
            assert block.id == 'white_wool'
        else:
            assert block.id == 'air'
def test_mixed_chunk_types():
    colors = ['red', 'orange', 'yellow', 'green']

    region = EmptyRegion(0, 0)

    chunk = EmptyChunk(0, 0)
    empty_chunk = EmptyChunk(1, 0)
    for i, color in enumerate(colors):
        chunk.set_block(Block(color), i, 0, 0)
        empty_chunk.set_block(Block(color), i, 0, 0)

    chunk = Chunk(chunk.save())

    region.add_chunk(chunk)
    region.add_chunk(empty_chunk)

    region = Region(region.save())

    for i in range(2):
        chunk = region.get_chunk(i, 0)
        for i, color in enumerate(colors):
            assert chunk.get_block(i, 0, 0).id == color
예제 #8
0
def test_5bits():
    from random import randint
    region = EmptyRegion(0, 0)

    blocks = 'stone,dirt,oak_planks,sand,bedrock,white_wool,red_wool,green_wool,sponge,awesome,these,dont,need,to,exist,foo,bar'.split(
        ',')
    positions = [(randint(0, 15), randint(0, 15), randint(0, 15))
                 for _ in range(len(blocks))]
    for block, pos in zip(blocks, positions):
        region.set_block(Block('minecraft', block), *pos)

    region = Region(region.save())

    for i, block in enumerate(region.get_chunk(0, 0).stream_blocks()):
        if block.id in blocks:
            assert coord_to_index(*positions[blocks.index(block.id)]) == i
        else:
            assert block.id == 'air'