<commit_before>from roglick.lib import libtcod
from roglick.dungeon.utils import PerlinNoise2D, smootherstep, smoothstep, lerp
from roglick.utils import clamp
from roglick.engine import random

width=180
height=100
font='data/fonts/arial10x10.png'

libtcod.console_set_custom_font(font.encode('UTF-8'), libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(width, height, "Perlin Noise Test".encode('UTF-8'), False)


cells = []
for x in range(width):
    cells.append([(0,width*width+height*height) for y in range(height)])

points = 200
for pi in range(points):
    px = random.get_int(0, width-1)
    py = random.get_int(0, height-1)

    cell, dist = cells[px][py]
    cells[px][py] = (pi, 0)

    for d in range(1, max(width,height)):
        changed = False
        for x in range(px-d, px+d+1):
            if x < 0 or x >= width:
                continue
            dist2 = (px - x)**2 + (d)**2
            if py-d >= 0 and dist2 < cells[x][py-d][1]:
                cells[x][py-d] = (pi, dist2)
                changed = True
            if py+d < height and dist2 < cells[x][py+d][1]:
                cells[x][py+d] = (pi, dist2)
                changed = True

        for y in range(py-d, py+d+1):
            if y < 0 or y >= height:
                continue
            dist2 = (py - y)**2 + (d)**2
            if px-d >= 0 and dist2 < cells[px-d][y][1]:
                cells[px-d][y] = (pi, dist2)
                changed = True
            if px+d < width and dist2 < cells[px+d][y][1]:
                cells[px+d][y] = (pi, dist2)
                changed = True
        if not changed:
            break

for x in range(width):
    for y in range(height):
        if cells[x][y][1] == 0:
            c = b'#'
        else:
            c = b' '
        color = int(255 * cells[x][y][0]/points)
        color = libtcod.Color(color,color,color)

        libtcod.console_put_char_ex(0,
                x, y,
                c, libtcod.light_green, color)

libtcod.console_flush()
while True:
    key = libtcod.console_wait_for_keypress(True)
    if key.vk == libtcod.KEY_SPACE:
        break

<commit_msg>Refactor code, add Lloyd algorithm<commit_after>import time


from roglick.lib import libtcod
from roglick.dungeon.utils import PerlinNoise2D, smootherstep, smoothstep, lerp
from roglick.utils import clamp
from roglick.engine import random

width=180
height=100
font='data/fonts/arial10x10.png'

libtcod.console_set_custom_font(font.encode('UTF-8'), libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(width, height, "Perlin Noise Test".encode('UTF-8'), False)


def init_cells():
    cells = []
    for x in range(width):
        cells.append([(None,width*width+height*height) for y in range(height)])

    return cells


def add_voronoi_cell(cells, cell_id, x, y):
    px,py = x,y
    cells[px][py] = (cell_id, 0)

    for d in range(1, max(width,height)):
        changed = False
        for x in range(px-d, px+d+1):
            if x < 0 or x >= width:
                continue
            dist2 = (px - x)**2 + (d)**2
            if py-d >= 0 and dist2 < cells[x][py-d][1]:
                cells[x][py-d] = (cell_id, dist2)
                changed = True
            if py+d < height and dist2 < cells[x][py+d][1]:
                cells[x][py+d] = (cell_id, dist2)
                changed = True

        for y in range(py-d, py+d+1):
            if y < 0 or y >= height:
                continue
            dist2 = (py - y)**2 + (d)**2
            if px-d >= 0 and dist2 < cells[px-d][y][1]:
                cells[px-d][y] = (cell_id, dist2)
                changed = True
            if px+d < width and dist2 < cells[px+d][y][1]:
                cells[px+d][y] = (cell_id, dist2)
                changed = True
        if not changed:
            break


cells = init_cells()
points = 200
centers = []
for pi in range(points):
    px = random.get_int(0, width-1)
    py = random.get_int(0, height-1)
    centers.append((px,py))

    add_voronoi_cell(cells, pi, px, py)

for i in range(5):
    for x in range(width):
        for y in range(height):
            if cells[x][y][1] == 0:
                c = b'#'
            else:
                c = b' '
            color = int(255 * cells[x][y][0]/points)
            color = libtcod.Color(color,color,color)

            libtcod.console_put_char_ex(0,
                    x, y,
                    c, libtcod.light_green, color)

    libtcod.console_flush()
    libtcod.sys_save_screenshot()

    time.sleep(0.5)

    weights = [[0,0,0] for i in range(points)]
    for x in range(width):
        for y in range(height):
            cell = cells[x][y][0]
            weights[cell][0] += x
            weights[cell][1] += y
            weights[cell][2] += 1

    for i in range(points):
        x_avg = weights[i][0] / weights[i][2]
        y_avg = weights[i][1] / weights[i][2]
        centers[i] = (int(x_avg),int(y_avg))

    cells = init_cells()
    for i in range(points):
        add_voronoi_cell(cells, i, centers[i][0], centers[i][1])


#while True:
#    key = libtcod.console_wait_for_keypress(True)
#    if key.vk == libtcod.KEY_SPACE:
#        break


{'fraction_non_alphanumeric': 0.1389263036367135, 'fraction_numerical': 0.0238599191841447, 'mean_word_length': 2.0098436595251883, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6757574', 'n_tokens_mistral': 1918, 'n_tokens_neox': 1816, 'n_words': 436}