# Path: compositecore.py
# class Leaf(Component):
#     """
#     Abstract leaf class of composite design pattern.
# 
#     Component classes of leaf type should inherit from this class.
#     """
# 
#     def __init__(self):
#         super(Leaf, self).__init__()
# 
#     def get_relative_of_type(self, component_type):
#         return self.parent.get_child_or_ancestor(component_type)
# 
#     def has_relative_of_type(self, component_type):
#         return self.parent.has_child_or_ancestor(component_type)


import geometry
import rng
import turn
from compositecore import Leaf
            self._calculate_seen_entities()
        return self._seen_entities_cache

    def _calculate_seen_entities(self):
        if not self.parent.dungeon_level.value is None:
            seen_entities = []
            for entity in self.parent.dungeon_level.value.entities:
                if self.parent.dungeon_mask.can_see_point(entity.position.value):
                    seen_entities.append(entity)
            self._seen_entities_cache = [entity for entity in seen_entities if not entity is self.parent]
            self._seen_entities_cache_timestamp = turn.current_turn

    def get_seen_entities_closest_first(self):
        """
        Gets all seen entities sorted on distance from self not including self.
        """
        return sorted(self.get_seen_entities(), key=lambda entity:
                      (geometry.chess_distance(self.parent.position.value, entity.position.value),
                       -entity.health.hp.ratio_of_full(), hash(entity)))

    def get_closest_seen_entity(self):
        """
        Gets the closest of all seen entities not including self.
        """
        closest_seen_entities = self.get_seen_entities_closest_first()
        if len(closest_seen_entities) < 1:
            return None
        return closest_seen_entities[0]  # The first is oneself.



# Path: compositecore.py
# class Leaf(Component):
#     """
#     Abstract leaf class of composite design pattern.
# 
#     Component classes of leaf type should inherit from this class.
#     """
# 
#     def __init__(self):
#         super(Leaf, self).__init__()
# 
#     def get_relative_of_type(self, component_type):
#         return self.parent.get_child_or_ancestor(component_type)
# 
#     def has_relative_of_type(self, component_type):
#         return self.parent.has_child_or_ancestor(component_type)


# Path: vision.py
import geometry
import rng
import turn
from compositecore import Leaf

            self._calculate_seen_entities()
        return self._seen_entities_cache

    def _calculate_seen_entities(self):
        if not self.parent.dungeon_level.value is None:
            seen_entities = []
            for entity in self.parent.dungeon_level.value.entities:
                if self.parent.dungeon_mask.can_see_point(entity.position.value):
                    seen_entities.append(entity)
            self._seen_entities_cache = [entity for entity in seen_entities if not entity is self.parent]
            self._seen_entities_cache_timestamp = turn.current_turn

    def get_seen_entities_closest_first(self):
        """
        Gets all seen entities sorted on distance from self not including self.
        """
        return sorted(self.get_seen_entities(), key=lambda entity:
                      (geometry.chess_distance(self.parent.position.value, entity.position.value),
                       -entity.health.hp.ratio_of_full(), hash(entity)))

    def get_closest_seen_entity(self):
        """
        Gets the closest of all seen entities not including self.
        """
        closest_seen_entities = self.get_seen_entities_closest_first()
        if len(closest_seen_entities) < 1:
            return None
        return closest_seen_entities[0]  # The first is oneself.



class AwarenessChecker(Leaf):