<commit_before>from heapq import heappush, heappop
from math import sqrt
from operator import itemgetter
import json
import codecs
import time

from yourgame.environ import util


# even-r : 'pointy top'

__all__ = ['HexMapModel',
           'Cell',
           'evenr_to_axial',
           'pixel_to_axial',
           'sprites_to_axial',
           'collide_hex']


def axial_to_pixel(coords, size):
    return size * sqrt(3) * (coords[0] - 0.5 * (coords[1] & 1)), \
           size * 3/2 * coords[1]


def pixel_to_axial(coords, size):
    x, y = coords[:2]
    return (1. / 3. * sqrt(3) * x - 1. / 3. * y) / size, (2. / 3.) * y / size


def axial_to_cube(coords):
    return coords[0], coords[1], -coords[0] - coords[1]


def cube_to_axial(coords):
    return coords[0], coords[1]


def axial_to_oddr(coords):
    # axial => cube
    x = coords[0]
    z = coords[1]
    # y = -x-z

    # cube => odd-r
    q = x + (z - (z & 1)) / 2
    r = z

    return q, r


def axial_to_evenr(coords):
    # axial => cube
    x = coords[0]
    z = coords[1]
    # y = -x-z

    # cube => evenr
    q = x + (z + (z & 1)) / 2
    r = z

    return q, z


def oddr_to_axial(coords):
    # odd-r => cube
    q, r = coords
    x = q - (r - (r & 1)) / 2
    z = r
    # y = -z-x

    # cube => axial
    q = x
    r = z

    return q, r


def evenr_to_axial(coords):
    # even-r => cube
    q, r = coords
    x = q - (r + (int(round(r, 0)) & 1)) / 2
    z = r
    # y = -x-z

    # cube => axial
    q = x
    r = z

    return q, r


def sprites_to_axial(coords):
    x, y = coords[:2]
    x -= y / 2 + 1
    return x, y


def collide_hex(left, right, left_radius=1.0, right_radius=1.0):
    """ Fast approximation of collisions between hex cells in axial space
    """
    dx, dy = dist_axial2(left, right)
    rr = left_radius + right_radius
    return (dx * dx) + (dy * dy) < rr * rr


# returns x and y
def dist_axial2(cell0, cell1):
    q0, r0, z0 = cell0
    q1, r1, z1 = cell1
    return abs(q0 - q1) + abs(r0 - r1), abs(q0 + r0 - q1 - r1) / 2.0


# returns x + y
def dist_axial(cell0, cell1):
    q0, r0, z0 = cell0
    q1, r1, z1 = cell1
    return (abs(q0 - q1) + abs(r0 - r1) +
            abs(q0 + r0 - q1 - r1)) / 2.0


class Cell(object):
    def __init__(self, **kwargs):
        self.kind = kwargs.get("kind", None)
        self.cost = int(kwargs.get("cost", 0))
        self.filename = kwargs.get("filename", None)
        self.raised = kwargs.get("raised", False)
        self.height = float(kwargs.get("height", 0.0))

    def to_json(self):
        return {
            "kind": self.kind,
            "cost": self.cost,
            "filename": self.filename,
            "raised": self.raised,
            "height": self.height
        }


class HexMapModel(object):
    def __init__(self):
        self._data = dict()
        self._width = None
        self._height = None
        self._dirty = False

    def surrounding(self):
        return util.surrounding((0, 0), (self.width-1, self.height-1))

    def neighbors(self, cell):
        return util.neighbors(cell[0], self.surrounding())

    def collidecircle(self, coords, radius):
        """test if circle overlaps level geometry above layer 0 only

        :param coords: axial coords
        :param radius: axial coords
        :return: iterator of coords
        """
        for n in self.neighbors(coords):
            try:
                cell = self._data[coords]
            except KeyError:
                continue

            if cell.height <= 0:
                continue

            if collide_hex(coords, n, radius, .8):
                yield coords

    def _make_file_data(self):
        return {
            "width": self._width,
            "height": self._height,
            "data": {str(key): value.to_json() for key, value in
                     self._data.items()}
        }

    def save_to_disk(self, path):
        with codecs.open(path, "wb", encoding="utf-8") as fob:
            data = self._make_file_data()
            json.dump(data, fob, indent=2)

    def load_from_disk(self, path):
        with codecs.open(path, "rb", encoding="utf-8") as fob:<commit_msg>collidecircle() is no longer a generator<commit_after>from heapq import heappush, heappop
from math import sqrt
from operator import itemgetter
import json
import codecs
import time

from yourgame.environ import util


# even-r : 'pointy top'

__all__ = ['HexMapModel',
           'Cell',
           'evenr_to_axial',
           'pixel_to_axial',
           'sprites_to_axial',
           'collide_hex']


def axial_to_pixel(coords, size):
    return size * sqrt(3) * (coords[0] - 0.5 * (coords[1] & 1)), \
           size * 3/2 * coords[1]


def pixel_to_axial(coords, size):
    x, y = coords[:2]
    return (1. / 3. * sqrt(3) * x - 1. / 3. * y) / size, (2. / 3.) * y / size


def axial_to_cube(coords):
    return coords[0], coords[1], -coords[0] - coords[1]


def cube_to_axial(coords):
    return coords[0], coords[1]


def axial_to_oddr(coords):
    # axial => cube
    x = coords[0]
    z = coords[1]
    # y = -x-z

    # cube => odd-r
    q = x + (z - (z & 1)) / 2
    r = z

    return q, r


def axial_to_evenr(coords):
    # axial => cube
    x = coords[0]
    z = coords[1]
    # y = -x-z

    # cube => evenr
    q = x + (z + (z & 1)) / 2
    r = z

    return q, z


def oddr_to_axial(coords):
    # odd-r => cube
    q, r = coords
    x = q - (r - (r & 1)) / 2
    z = r
    # y = -z-x

    # cube => axial
    q = x
    r = z

    return q, r


def evenr_to_axial(coords):
    # even-r => cube
    q, r = coords
    x = q - (r + (int(round(r, 0)) & 1)) / 2
    z = r
    # y = -x-z

    # cube => axial
    q = x
    r = z

    return q, r


def sprites_to_axial(coords):
    x, y = coords[:2]
    x -= y / 2 + 1
    return x, y


def collide_hex(left, right, left_radius=1.0, right_radius=1.0):
    """ Fast approximation of collisions between hex cells in axial space
    """
    dx, dy = dist_axial2(left, right)
    rr = left_radius + right_radius
    return (dx * dx) + (dy * dy) < rr * rr


# returns x and y
def dist_axial2(cell0, cell1):
    q0, r0, z0 = cell0
    q1, r1, z1 = cell1
    return abs(q0 - q1) + abs(r0 - r1), abs(q0 + r0 - q1 - r1) / 2.0


# returns x + y
def dist_axial(cell0, cell1):
    q0, r0, z0 = cell0
    q1, r1, z1 = cell1
    return (abs(q0 - q1) + abs(r0 - r1) +
            abs(q0 + r0 - q1 - r1)) / 2.0


class Cell(object):
    def __init__(self, **kwargs):
        self.kind = kwargs.get("kind", None)
        self.cost = int(kwargs.get("cost", 0))
        self.filename = kwargs.get("filename", None)
        self.raised = kwargs.get("raised", False)
        self.height = float(kwargs.get("height", 0.0))

    def to_json(self):
        return {
            "kind": self.kind,
            "cost": self.cost,
            "filename": self.filename,
            "raised": self.raised,
            "height": self.height
        }


class HexMapModel(object):
    def __init__(self):
        self._data = dict()
        self._width = None
        self._height = None
        self._dirty = False

    def surrounding(self, coord):
        return util.surrounding_clip(coord,
                                     (0, 0), (self.width-1, self.height-1))

    def collidecircle(self, coords, radius):
        """test if circle overlaps level geometry above layer 0 only

        :param coords: axial coords
        :param radius: axial coords
        :return: iterator of coords
        """
        retval = list()
        for n in self.surrounding(coords):
            try:
                cell = self._data[coords]
            except KeyError:
                continue

            if cell.height <= 0:
                continue

            if collide_hex(coords, n, radius, .8):
                retval.append(coords)
        return retval

    def _make_file_data(self):
        return {
            "width": self._width,
            "height": self._height,
            "data": {str(key): value.to_json() for key, value in
                     self._data.items()}
        }

    def save_to_disk(self, path):
        with codecs.open(path, "wb", encoding="utf-8") as fob:
            data = self._make_file_data()
            json.dump(data, fob, indent=2)

    def load_from_disk(self, path):
        with codecs.open(path, "rb", encoding="utf-8") as fob:
{'fraction_non_alphanumeric': 0.1373908884371637, 'fraction_numerical': 0.023556140141097692, 'mean_word_length': 1.8362156663275686, 'pattern_counts': {'":': 16, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2811209', 'n_tokens_mistral': 3191, 'n_tokens_neox': 2989, 'n_words': 821}