<commit_before>
from .. import arena_game
from .. import dungeon
from .. import event
from .. import gladiator
from .. import calc

import random


class ArenaGameEngine(arena_game.ArenaGameEngine):

    def __init__(self, state=None, *args, **kwargs):
        """
        :param state: {"dungeon": {"size": int},
                       ...}
        """
        super().__init__(state=state, *args, **kwargs)

    def init_new_gladiator_stats(self, gladiators, *args, **kwargs):
        """
        :param gladiators: list of Gladiators
        :return: list of stats to create Gladiator object at start of the game
        """
        stats = super().init_new_gladiator_stats(gladiators, *args, **kwargs)

        if gladiators:
            size = self.get_dungeon_size(gladiators)
        else:
            size = ((0, 3), (0, 3))
        # find free position
        while True:
            pos = (random.randint(size[0][0], size[0][1]),
                   random.randint(size[1][0], size[1][1]))
            if all(pos != g.pos for g in gladiators):
                break
        # positions = ((0, 0), (0, 3), (3, 0), (3, 3))
        # pos = positions[gladiators.index(gladiator)]
        stats["pos"] = pos
        return stats

    def init_new_dungeon_stats(self, gladiators, *args, **kwargs):
        """
        :return: list of stats to create Dungeon object at start of the game
        """
        stats = super().init_new_dungeon_stats(gladiators, *args, **kwargs)
        stats["size"] = self.get_dungeon_size(gladiators)
        return stats

    @staticmethod
    def get_dungeon_size(gladiators):
        positions = [g.pos for g in gladiators]
        pos_x = [p[0] for p in positions]
        pos_y = [p[1] for p in positions]
        size = ((min(pos_x) - 1, max(pos_x) + 1),
                (min(pos_y) - 1, max(pos_y) + 1))
        return size

    @staticmethod
    def within_bounds(pos, size):
        return bool(size[0][0] <= pos[0] <= size[0][1]
                    and size[1][0] <= pos[1] <= size[1][1])

    def get_move_options(self, gladiator_index):
        """
        Used by agent to get available moves
        :param gladiator_index: index in gladiators list
        :return: (dict) [{type: name,
                          targets: [{target: target,
                                     values: []
                                     },
                                    ]
                          },
                         ]
        """
        gladiator = self.gladiators[gladiator_index]
        options = super().get_move_options(gladiator_index=gladiator_index)

        # add options for "move"
        directions = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]
        targets = [d for d in directions
                   if self.within_bounds(calc.add_tuples(gladiator.pos, d),
                                         self.dungeon.size)]
        if targets:
            options_move = {"type": "move",
                            "targets": targets
                            }
            options.append(options_move)

        # add options for "attack"
        # targets are indices of gladiators list
        targets = [self.gladiators.index(g) for g in self.gladiators
                   if (calc.dist(gladiator.pos, g.pos) <= gladiator.range
                       and not g.is_dead()
                       and g is not gladiator)]
        # replace attack option from super()
        index = len(options) - 1
        for option in reversed(options):
            for _, value in option.items():
                if value is "attack":
                    del options[index]
            index -= 1
        if targets:
            options_attack = {"type": "attack",
                              "targets": targets
                              }
            options.append(options_attack)

        return options

    def init_queued_event_stats(self, time, glad_event, move):
        """
        As super(), but adds information about the position at event creation.
        :param time: time the event is created
        :param glad_event: event the gladiator is called
        :param move: move the gladiator wants to queue
        :return: dict of stats for instantiation of event.
        """
        stats = super().init_queued_event_stats(time, glad_event, move)
        glad = self.gladiators[glad_event.owner]
        stats["origin"] = glad.pos
        return stats

    def handle_event(self, event):
        if event.type is "move":
            self.move_move(event)
        else:
            super().handle_event(event=event)

        # self.dungeon.shrink_dungeon(self.state)
        return None

    def move_move(self, event):
        """
        Moves owner of event by target vector (if space is free.)
        :param event:
        :return:
        """
        blocked = False
        player = self.gladiators[event.owner]
        # for glad in self.gladiators:
        #     if glad.pos == calc.add_tuples(player.pos, event.target):
        #         blocked = True
        target_pos = calc.add_tuples(player.pos, event.target)
        if self.dungeon.out_of_bounds(target_pos):
            blocked = True
        if not blocked:
            player.move(event.target)
        return None


class Dungeon(dungeon.Dungeon):
    def __init__(self, size, *args, **kwargs):
        """
        :param size: ((int, int), (int, int))
        """
        super().__init__(*args, **kwargs)
        self.size = size

    def get_init(self, *args, **kwargs):
        init = super().get_init(*args, **kwargs)
        init["size"] = self.size
        return init

    def out_of_bounds(self, position):
        if (self.size[1][0] < position[0] < self.size[0][0]
                or self.size[1][1] < position[1] < self.size[0][1]):
            return True
        return False

    def shrink_dungeon(self, state):
        # shrink dungeon
        positions = [g.pos for g in state["gladiators"]]
        pos_x = [p[0] for p in positions]
        pos_y = [p[1] for p in positions]
        self.size = ((min(pos_x), max(pos_x)), (min(pos_y), max(pos_y)))
        return None


class Event(event.Event):
    def __init__(self, origin=None, *args, **kwargs):
        """
        :param owner: (int) gladiator index
        :param time_stamp: (float)
        :param type: (str) name of type of event
        :param target: (int) Gladiator index OR (int, int) position
        :param origin: (int, int) position
        """
        super().__init__(*args, **kwargs)<commit_msg>fix out of bounds check for position<commit_after>
from .. import arena_game
from .. import dungeon
from .. import event
from .. import gladiator
from .. import calc

import random


class ArenaGameEngine(arena_game.ArenaGameEngine):

    def __init__(self, state=None, *args, **kwargs):
        """
        :param state: {"dungeon": {"size": int},
                       ...}
        """
        super().__init__(state=state, *args, **kwargs)

    def init_new_gladiator_stats(self, gladiators, *args, **kwargs):
        """
        :param gladiators: list of Gladiators
        :return: list of stats to create Gladiator object at start of the game
        """
        stats = super().init_new_gladiator_stats(gladiators, *args, **kwargs)

        if gladiators:
            size = self.get_dungeon_size(gladiators)
        else:
            size = ((0, 3), (0, 3))
        # find free position
        while True:
            pos = (random.randint(size[0][0], size[0][1]),
                   random.randint(size[1][0], size[1][1]))
            if all(pos != g.pos for g in gladiators):
                break
        # positions = ((0, 0), (0, 3), (3, 0), (3, 3))
        # pos = positions[gladiators.index(gladiator)]
        stats["pos"] = pos
        return stats

    def init_new_dungeon_stats(self, gladiators, *args, **kwargs):
        """
        :return: list of stats to create Dungeon object at start of the game
        """
        stats = super().init_new_dungeon_stats(gladiators, *args, **kwargs)
        # stats["size"] = self.get_dungeon_size(gladiators)
        stats["size"] = ((0, 3), (0, 3))
        return stats

    @staticmethod
    def get_dungeon_size(gladiators):
        positions = [g.pos for g in gladiators]
        pos_x = [p[0] for p in positions]
        pos_y = [p[1] for p in positions]
        size = ((min(pos_x) - 1, max(pos_x) + 1),
                (min(pos_y) - 1, max(pos_y) + 1))
        return size

    @staticmethod
    def within_bounds(pos, size):
        return bool(size[0][0] <= pos[0] <= size[0][1]
                    and size[1][0] <= pos[1] <= size[1][1])

    def get_move_options(self, gladiator_index):
        """
        Used by agent to get available moves
        :param gladiator_index: index in gladiators list
        :return: (dict) [{type: name,
                          targets: [{target: target,
                                     values: []
                                     },
                                    ]
                          },
                         ]
        """
        gladiator = self.gladiators[gladiator_index]
        options = super().get_move_options(gladiator_index=gladiator_index)

        # add options for "move"
        directions = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]
        targets = [d for d in directions
                   if self.within_bounds(calc.add_tuples(gladiator.pos, d),
                                         self.dungeon.size)]
        if targets:
            options_move = {"type": "move",
                            "targets": targets
                            }
            options.append(options_move)

        # add options for "attack"
        # targets are indices of gladiators list
        targets = [self.gladiators.index(g) for g in self.gladiators
                   if (calc.dist(gladiator.pos, g.pos) <= gladiator.range
                       and not g.is_dead()
                       and g is not gladiator)]
        # replace attack option from super()
        index = len(options) - 1
        for option in reversed(options):
            for _, value in option.items():
                if value is "attack":
                    del options[index]
            index -= 1
        if targets:
            options_attack = {"type": "attack",
                              "targets": targets
                              }
            options.append(options_attack)

        return options

    def init_queued_event_stats(self, time, glad_event, move):
        """
        As super(), but adds information about the position at event creation.
        :param time: time the event is created
        :param glad_event: event the gladiator is called
        :param move: move the gladiator wants to queue
        :return: dict of stats for instantiation of event.
        """
        stats = super().init_queued_event_stats(time, glad_event, move)
        glad = self.gladiators[glad_event.owner]
        stats["origin"] = glad.pos
        return stats

    def handle_event(self, event):
        if event.type is "move":
            self.move_move(event)
        else:
            super().handle_event(event=event)

        # self.dungeon.shrink_dungeon(self.state)
        return None

    def move_move(self, event):
        """
        Moves owner of event by target vector (if space is free.)
        :param event:
        :return:
        """
        blocked = False
        player = self.gladiators[event.owner]
        # for glad in self.gladiators:
        #     if glad.pos == calc.add_tuples(player.pos, event.target):
        #         blocked = True
        target_pos = calc.add_tuples(player.pos, event.target)
        if not self.within_bounds(target_pos):
            blocked = True
        if not blocked:
            player.move(event.target)
        return None


class Dungeon(dungeon.Dungeon):
    def __init__(self, size, *args, **kwargs):
        """
        :param size: ((int, int), (int, int))
        """
        super().__init__(*args, **kwargs)
        self.size = size

    def get_init(self, *args, **kwargs):
        init = super().get_init(*args, **kwargs)
        init["size"] = self.size
        return init

    def shrink_dungeon(self, state):
        # shrink dungeon
        positions = [g.pos for g in state["gladiators"]]
        pos_x = [p[0] for p in positions]
        pos_y = [p[1] for p in positions]
        self.size = ((min(pos_x), max(pos_x)), (min(pos_y), max(pos_y)))
        return None


class Event(event.Event):
    def __init__(self, origin=None, *args, **kwargs):
        """
        :param owner: (int) gladiator index
        :param time_stamp: (float)
        :param type: (str) name of type of event
        :param target: (int) Gladiator index OR (int, int) position
        :param origin: (int, int) position
        """
        super().__init__(*args, **kwargs)
{'fraction_non_alphanumeric': 0.11954678587945121, 'fraction_numerical': 0.009711731154616926, 'mean_word_length': 1.7606382978723405, 'pattern_counts': {'":': 16, '<': 17, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5757305', 'n_tokens_mistral': 3910, 'n_tokens_neox': 3768, 'n_words': 1050}