Пример #1
0
    def computeFeatures(self, absList, mode=''):
        """ compute classifier features for each mention-mention pair in 
        each abstract in a given list of abstracts. """
        for abs in absList:
            if abs.entities == None:
                abs.entities = Entities(abs)
            if abs.annotatedEntities == None:
                abs.annotatedEntities = Entities(abs)

            # compute idf from current abstract list (ignore current one)
            # TODO: Eventually replace this with idf stats computed from a
            #       a separate corpus
            documentCounts = {}
            for i in range(0, len(absList)):
                abs2 = absList[i]
                if abs2 != abs:
                    for lemma in abs2.getLemmaSet():
                        documentCounts[lemma] = 1 + documentCounts.get(
                            lemma, 0)
            idf = {}
            nAbs = float(len(absList) - 1)
            for lemma, counts in documentCounts.items():
                idf[lemma] = math.log(nAbs / counts)

            idf['unknownToken'] = math.log(nAbs)

            abs.entities.createEntities(self.entityTypes[0], self.sentenceFilter,\
                                        useDetected=self.useDetected)
            abs.annotatedEntities.createTrueEntities(self.entityTypes[0],
                                                     self.sentenceFilter)

            self.computePairFeatures(abs.entities, idf)
            self.computePairFeatures(abs.annotatedEntities, idf)
Пример #2
0
    def build_map(self, level=1):
        self.game_map = GameMap(self.message_log, level)
        self.entities = Entities(self.game_map)
        self.game_map.make_map(self.player, self.entities)
        self.entities.insert_entity(self.player)

        self.fov_recompute = True
        self.fov_map = initialize_fov(self.game_map)
Пример #3
0
 def __init__(self, data):
     self.id_speaker = None
     self.nom = None
     self.prenom = None
     self.description = None
     self.profession = None
     self.statut = None
     Entities.__init__(self, data)
Пример #4
0
    def __init__(self, message: telegram.Message, start=0, temp_name=None):
        self.entities = Entities()
        self.start = start
        self.players = list(
            Player.objects.filter(chat_id=message.chat_id).all())
        for player in self.players:
            if player.user_id == message.from_user.id:
                self.me = Me(temp_name or player.character_name, player.id,
                             player.full_name)
                self.variables = {}
                for variable in player.variable_set.all():
                    self.variables[variable.name.upper()] = variable.value
                break

        self.tags = []
        if message.caption:
            text = message.caption
            entities = message.caption_entities
        else:
            text = message.text
            entities = message.entities
        if not text:
            return
        assert isinstance(text, str)
        last_index = 0

        for entity in entities:
            assert isinstance(entity, telegram.MessageEntity)
            entity_offset = entity.offset
            entity_length = entity.length
            entity_end = entity_offset + entity_length
            if entity.type == entity.MENTION:
                self.push_text(text[last_index:entity_offset])
                mention = text[entity_offset:entity_end]
                self.push_mention(mention)
                last_index = entity_end
            elif entity.type == entity.TEXT_MENTION:
                self.push_text(text[last_index:entity_offset])
                self.push_text_mention(entity.user)
                last_index = entity_end
            elif entity.type == entity.HASHTAG:
                self.push_text(text[last_index:entity_offset])
                self.tags.append(text[entity_offset + 1:entity_end])
                last_index = entity_end
            elif entity.type == entity.BOLD:
                self.push_text(text[last_index:entity_offset])
                self.entities.list.append(Bold(text[entity_offset:entity_end]))
                last_index = entity_end

        self.push_text(text[last_index:])
        if len(self.entities.list) > 0 and isinstance(self.entities.list[0],
                                                      Span):
            v = self.entities.list[0].value
            if start < len(v):
                self.entities.list[0] = Span(v[start:])
            else:
                self.entities.list.pop(0)
Пример #5
0
    def __init__(self):
        self.date_time = time.strftime("%Y.%m.%d-%H.%M.%S")

        self.params = Params()
        self.environment = Environment()

        if self.params.inherit_nn:  # F
            self.inherit_nn = Agent()
            self.inherit_nn.load_network(fname=self.params.inherit_nn)
        else:
            self.inherit_nn = None  # T

        if self.params.general_nn:  # T
            if self.params.inherit_nn:  # F
                self.general_nn = self.inherit_nn
            else:  # T
                self.general_nn = Agent()  # first network

            self.entities = Entities(environment=self.environment,
                                     general_nn=self.general_nn)  # new NN
        else:  # F
            self.general_nn = None
            self.entities = Entities(environment=self.environment,
                                     inherit_nn=self.inherit_nn)

        if self.params.memory_load:
            self.general_nn.load_memory(fname="out/" + self.params.memory_load)
            print("memory replay loaded")

        self.epoch = 1

        if self.params.simulate:
            self.load_scenario()

        if self.params.window_show:
            self.window = Window()
            self.render()

        if self.params.interactive:
            self.to_iterate = 0
            self.running = True
            self.clear()
            self.idle()
        else:
            self.loop()

        # save neural network & experience_replay
        if self.params.save_model and self.params.general_nn:
            self.general_nn.save_network(
                fname=f"out/weights-{self.date_time}.model")
            self.general_nn.save_memory(
                fname=f"out/stack-{self.date_time}.memory")
Пример #6
0
    def init_physicals(self):
        #        self._entities = {}
        self.robot_names = ["dalek", "drWho", "k9", "kachna"]
        self.num_of_robots = len(self.robot_names)

        self.setup_collision_callbacks()

        self.entities = Entities(self.app)

        self.map = Map2D(self)

        self.asteroids = Asteroids(self)
        self.init_robots()
Пример #7
0
 def train(self, absList, modelFilename):
     """ Train a mention clusterer model given a list of abstracts """
     for abstract in absList:
         if abstract.annotatedEntities == None:
             abstract.annotatedEntities = Entities(abstract)
         abstract.annotatedEntities.createTrueEntities(
             self.entityTypes[0], self.sentenceFilter)
Пример #8
0
def handle_roll(message: telegram.Message,
                name: str,
                entities: Entities,
                job_queue: JobQueue,
                chat: Chat,
                hide=False):
    _ = partial(get_by_user, user=message.from_user)
    kind = LogKind.ROLL.value
    result_text = entities.to_html()
    if hide:
        hide_roll = HideRoll(message.chat_id, result_text)
        hide_roll.set()
        keyboard = [[
            InlineKeyboardButton(_(Text.GM_LOOKUP),
                                 callback_data=hide_roll.key())
        ]]

        reply_markup = InlineKeyboardMarkup(keyboard)
        text = '<b>{}</b> {}'.format(name, _(Text.ROLL_HIDE_DICE))
        kind = LogKind.HIDE_DICE.value
    else:
        text = '{} 🎲 {}'.format(name, result_text)
        reply_markup = None
    if not chat.recording:
        text = '[{}] '.format(_(Text.NOT_RECORDING)) + text
    sent = message.chat.send_message(text,
                                     reply_markup=reply_markup,
                                     parse_mode='HTML')
    user = message.from_user
    assert isinstance(user, telegram.User)
    if chat.recording:
        Log.objects.create(
            user_id=user.id,
            message_id=sent.message_id,
            chat=chat,
            content=result_text,
            entities=entities.to_object(),
            user_fullname=user.full_name,
            character_name=name,
            gm=is_gm(message.chat_id, user.id),
            kind=kind,
            created=message.date,
        )
    context = dict(chat_id=message.chat_id,
                   message_id_list=[message.message_id])
    job_queue.run_once(delay_delete_messages, 10, context)
Пример #9
0
    def __init__(self, money, health, morale):
        # Initialize renderer first because it starts SDL
        self.renderer = Renderer()

        self.textures = Textures()
        self.textures.load(self.renderer.sdl_renderer)

        self.user_interface = UserInterface(self.textures)
        self.controller = Controller()
        self.entities = Entities()

        self.entities.init_player(0, 0, self.textures.get(TextureType.PLAYER),
                                  money, health, morale)

        world_creator = WorldCreator(2)
        self.entities.map_rectangle = world_creator.create(
            self.entities, self.textures)
Пример #10
0
 def save_post(self, author, content, date, parent):
     try:
         with pny.db_session:
             Entities.Post(content=content,
                           topic=parent.topic_id,
                           author='' if author is None else str(author),
                           date=date)
     except BaseException as e:
         self.logger.error("Save post: " + str(e))
         self.logger.error("Content: " + content + " parent_id: " +
                           str(parent.topic_id))
Пример #11
0
 def save_category(self, title, link, parent, forum):
     try:
         with pny.db_session:
             return Entities.Category(title=title,
                                      link=link,
                                      forum=forum.forum_id,
                                      parent_category=None if parent is None
                                      else parent.category_id)
     except KeyError as e:
         self.logger.error(str(e))
         self.logger.error("For element: " + str(title))
Пример #12
0
 def save_topic(self, author, date, link, parent, title):
     try:
         with pny.db_session:
             return Entities.Topic(
                 title=title,
                 link=link,
                 author='' if author is None else str(author),
                 date=date,
                 category=parent.category_id)
     except BaseException as e:
         self.logger.error(str(e))
         self.logger.error("Title: " + title + " link: " + link +
                           " author: " + author)
Пример #13
0
    def clusterMentions(self, abstract):
        """ cluster mentions that refer to same entity in a given abstract. 
        create list of entities using hierarchical aglomerative clustering 
        """
        # cluster DETECTED mentions
        if abstract.entities == None:
            abstract.entities = Entities(abstract)
        abstract.entities.createEntities(self.entityTypes[0], self.sentenceFilter,\
                                         useDetected=True, useIds=False, mergeExactOnly=True)
        if abstract.annotatedEntities == None:
            abstract.annotatedEntities = Entities(abstract)
        abstract.annotatedEntities.createTrueEntities(self.entityTypes[0],
                                                      self.sentenceFilter)

        # discard groups that are likely to be false positives
        #    if self.entityTypes[0] == 'group':
        #      gList = self.filterGroupClusters(abstract.entities.lists[self.entityTypes[0]])
        #      abstract.entities.lists[self.entityTypes[0]] = gList

        # assign ids to each of the mention clusters
        currentId = 0
        for mTemplate in abstract.entities.lists[self.entityTypes[0]]:
            mTemplate.setId(str(currentId))
            currentId += 1
Пример #14
0
def handle_loop_roll(message: telegram.Message, command: str, name: str,
                     text: str, chat: Chat, **__):
    """
    Tales from the Loop
    """
    def _(t: Text):
        return get_by_user(t, user=message.from_user)

    hide = command[-1] == 'h'
    text = text.strip()
    roll_match = LOOP_ROLL_REGEX.match(text)

    if not roll_match:
        return error_message(message, _(Text.LOOP_SYNTAX_ERROR))
    number = int(roll_match.group(1))
    if number == 0:
        return error_message(message, _(Text.LOOP_ZERO_DICE))
    result_list = [secrets.randbelow(6) + 1 for _i in range(number)]
    description = text[roll_match.end():]
    entities = Entities([LoopResult(result_list), Span(description)])
    handle_roll(message, name, entities, chat, hide)
Пример #15
0
def handle_normal_roll(message: telegram.Message, command: str, name: str,
                       start: int, chat: Chat, **_):
    rpg_message = RpgMessage(message, start)
    hide = command[-1] == 'h'
    entities = rpg_message.entities.list
    roll_counter = 0
    next_entities = []
    try:
        for entity in entities:
            if isinstance(entity, Span):
                result_entities = dice.roll_entities(entity.value,
                                                     chat.default_dice_face)
                local_roll_counter = 0
                for result_entity in result_entities:
                    if isinstance(result_entity, RollResult):
                        local_roll_counter += 1
                if local_roll_counter > 0:
                    next_entities.extend(result_entities)
                    roll_counter += local_roll_counter
                else:
                    next_entities.append(entity)
            else:
                next_entities.append(entity)
        if roll_counter == 0:
            default_roll_entities = dice.roll_entities('1d',
                                                       chat.default_dice_face)
            default_roll_entities.extend(next_entities)
            next_entities = default_roll_entities
    except dice.RollError as e:
        error_text = Text.ERROR
        if len(e.args) > 0:
            error_kind = e.args[0]
            try:
                error_text = Text[error_kind.value]
            except KeyError:
                pass
        return error_message(message, get_by_user(error_text,
                                                  message.from_user))
    handle_roll(message, name, Entities(next_entities), chat, hide)
Пример #16
0
class Game:
    # Parameters: starting values for money, health, and morale
    def __init__(self, money, health, morale):
        # Initialize renderer first because it starts SDL
        self.renderer = Renderer()

        self.textures = Textures()
        self.textures.load(self.renderer.sdl_renderer)

        self.user_interface = UserInterface(self.textures)
        self.controller = Controller()
        self.entities = Entities()

        self.entities.init_player(0, 0, self.textures.get(TextureType.PLAYER),
                                  money, health, morale)

        world_creator = WorldCreator(2)
        self.entities.map_rectangle = world_creator.create(
            self.entities, self.textures)

    def run(self):
        running = True

        # For average FPS calculation
        frames = 0
        last_frame = sdl2.SDL_GetTicks()

        # Display splash screen
        while sdl2.SDL_GetTicks() < last_frame\
        + Renderer.splash_screen_display_time:
            self.renderer.render_splash_screen(self.textures)

        # Game loop:
        while running:
            # 1. Handle input from the user interface
            screen_dimensions = [
                self.renderer.screen_width, self.renderer.screen_height
            ]
            running = self.user_interface.handle_input(self.controller,
                                                       screen_dimensions)

            # 2. Update entities from the controller
            self.controller.update_entities(self.entities)
            self.controller.generate_NPCs(self.entities, self.textures)
            # if not self.controller.check_player_meters(self.entities):
            if self.controller.current_health <= 0 or self.controller.current_morale <= 0:
                # Display splash screen
                last_frame = sdl2.SDL_GetTicks()
                while sdl2.SDL_GetTicks() < last_frame\
                + Renderer.splash_screen_display_time:
                    self.renderer.render_lose_screen(self.textures)
                running = False

            # 3. Update screen from the renderer
            self.renderer.render(self.entities, self.textures,
                                 self.user_interface, screen_dimensions)

            # For debugging:
            # Average FPS for performance profiling, prints every 5 seconds
            frames += 1
            if sdl2.SDL_GetTicks() - last_frame > 5000:
                print('Average FPS: ' + str(frames / 5.0))
                frames = 0
                last_frame = sdl2.SDL_GetTicks()

        self.close()

    def get_renderer(self):
        """Returns the game renderer that was instantiated by the game class
		so that it can be used in other components that need to borrow from it.
		"""

        return self.renderer

    def get_textures(self):
        """Returns the game textures that were instantiated by the game class
		so that they can be used by other components.
		"""

        return self.textures

    # Closes the game renderer
    def close(self):
        self.textures.unload()
        self.renderer.close()
Пример #17
0
 def test_build_vector(self):
   docs = Entities.build_vector('rkprtr6jlhf2jv')
   self.assertEqual(len(docs), 10)
Пример #18
0
def handle_coc_roll(message: telegram.Message, command: str, name: str,
                    text: str, chat: Chat, **__):
    """
    Call of Cthulhu
    """
    def _(t: Text):
        return get_by_user(t, user=message.from_user)

    def roll() -> int:
        return secrets.randbelow(100) + 1

    hide = command[-1] == 'h'
    text = text.strip()
    numbers = re.findall(r'\d{1,2}', text)

    # have not modifier
    rolled_list = [roll()]
    rolled = rolled_list[0]
    modifier_name = None

    # have not target value
    if len(numbers) == 0:
        handle_roll(message, name,
                    Entities([Span(text),
                              RollResult(str(rolled), rolled)]), chat, hide)
        return

    skill_number = int(numbers[0])
    # have modifier
    modifier_matched = re.search('[-+]', command)
    if modifier_matched:
        modifier = modifier_matched.group(0)
        extra = 1
        if len(numbers) > 1:
            extra = int(numbers[0])
            skill_number = int(numbers[1])
        for _i in range(extra):
            rolled_list.append(roll())
        if modifier == '+':
            rolled = min(rolled_list)
            modifier_name = _(Text.COC_BONUS_DIE)
        elif modifier == '-':
            rolled = max(rolled_list)
            modifier_name = _(Text.COC_PENALTY_DIE)

    half_skill_number = skill_number // 2
    skill_number_divide_5 = skill_number // 5

    if rolled == 1:
        level = _(Text.COC_CRITICAL)
    elif rolled <= skill_number_divide_5:
        level = _(Text.COC_EXTREME_SUCCESS)
    elif rolled <= half_skill_number:
        level = _(Text.COC_HARD_SUCCESS)
    elif rolled <= skill_number:
        level = _(Text.COC_REGULAR_SUCCESS)
    elif rolled == 100:
        level = _(Text.COC_FUMBLE)
    elif rolled >= 95 and skill_number < 50:
        level = _(Text.COC_FUMBLE)
    else:
        level = _(Text.COC_FAIL)

    entities = [
        Span(text),
        Span(' → '),
        CocResult(rolled, level, modifier_name, rolled_list)
    ]
    handle_roll(message, name, Entities(entities), chat, hide)
Пример #19
0
import pyrr
from OpenGL.GL import *

from core import Audio, GUI, DisplayManager, RenderEngine
from core.Camera import Camera
from entities import Entities
from islands import IslandGenerator

window = DisplayManager.Window()

cam = Camera(window)

masterRenderer = RenderEngine.MasterRenderer()

entities = []
ellipsoid = Entities.Ellipsoid()
entities.append(ellipsoid)

tree = Entities.Tree()
entities.append(tree)
tree.position = pyrr.Vector3([0, 0, -10])

island = IslandGenerator.generateMeshForIsland(0, 0)
print(island)

# LIGHTING
lights = []
light1 = RenderEngine.Light(pyrr.Vector3([0, 5, -5]), [1, 1, 1],
                            [0.5, 0.5, 0.5])
lights.append(light1)
Пример #20
0
def predict_sequence_ner(NER_pipeline, sequence):
    result = NER_pipeline(sequence)
    entities = Entities(result)
    return entities
Пример #21
0
from merge import Merge
from writer import Write


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="prepare_for_neuronet")
    parser.add_argument('--input', help='Input directory of bunches (annotation files)')
    args = parser.parse_args()

    input_dir = args.input

    input_dir, output_dir = Utils.init_paths_neuroner(input_dir)
    annotators = ['eugenia', 'victoria', 'isabel', 'carmen']

    variable_dict, variable_hash_dict, section_dict = Entities.get_final_annotators_entities(
                                                                                    input_dir,
                                                                                    output_dir,
                                                                                    t_number=False)

    merged_variables, _ = Merge.merge_entities(variable_dict)
    merged_sections, _ = Merge.merge_entities(section_dict)
    merged_variables_hash = Merge.merge_hash(variable_hash_dict)

    section_variable = Merge.merge_variables_sections(merged_variables, merged_sections)

    Write.accepted_variables_neuroner(section_variable, merged_variables_hash, output_dir)

    print("Done")



Пример #22
0
 def entities(self, text):
     logging.debug('entities: text - ' + text)
     e = Entities(enNlpModel, text)
     res = json.dumps(e.to_json(), sort_keys=True)
     logging.debug('entities: result - ' + res)
     return res
Пример #23
0
 def save_forum(self, link):
     with pny.db_session:
         forum = Entities.Forum(link=link)
         return forum
Пример #24
0
 def __init__(self, data):
     self.id_conference = None
     self.titre = None
     self.resume = None
     self.date = None
     Entities.__init__(self, data)
Пример #25
0
class RpgMessage:
    me = None
    variables = {}
    segments: List[Entity]
    entities: Entities

    def __init__(self, message: telegram.Message, start=0, temp_name=None):
        self.entities = Entities()
        self.start = start
        self.players = list(
            Player.objects.filter(chat_id=message.chat_id).all())
        for player in self.players:
            if player.user_id == message.from_user.id:
                self.me = Me(temp_name or player.character_name, player.id,
                             player.full_name)
                self.variables = {}
                for variable in player.variable_set.all():
                    self.variables[variable.name.upper()] = variable.value
                break

        self.tags = []
        if message.caption:
            text = message.caption
            entities = message.caption_entities
        else:
            text = message.text
            entities = message.entities
        if not text:
            return
        assert isinstance(text, str)
        last_index = 0

        for entity in entities:
            assert isinstance(entity, telegram.MessageEntity)
            entity_offset = entity.offset
            entity_length = entity.length
            entity_end = entity_offset + entity_length
            if entity.type == entity.MENTION:
                self.push_text(text[last_index:entity_offset])
                mention = text[entity_offset:entity_end]
                self.push_mention(mention)
                last_index = entity_end
            elif entity.type == entity.TEXT_MENTION:
                self.push_text(text[last_index:entity_offset])
                self.push_text_mention(entity.user)
                last_index = entity_end
            elif entity.type == entity.HASHTAG:
                self.push_text(text[last_index:entity_offset])
                self.tags.append(text[entity_offset + 1:entity_end])
                last_index = entity_end
            elif entity.type == entity.BOLD:
                self.push_text(text[last_index:entity_offset])
                self.entities.list.append(Bold(text[entity_offset:entity_end]))
                last_index = entity_end

        self.push_text(text[last_index:])
        if len(self.entities.list) > 0 and isinstance(self.entities.list[0],
                                                      Span):
            v = self.entities.list[0].value
            if start < len(v):
                self.entities.list[0] = Span(v[start:])
            else:
                self.entities.list.pop(0)

    def replace_variable(self, matched):
        return self.variables.get(matched.group(1).upper(), matched.group(0))

    def resolve_variable(self, text: str):
        counter = 16
        text = VARIABLE_REGEX.sub(self.replace_variable, text, count=counter)
        extra_resolve_level = 3
        for _ in range(extra_resolve_level):
            if len(text) > 256:
                break
            text = VARIABLE_REGEX.sub(self.replace_variable,
                                      text,
                                      count=counter)
        return text

    def push_text(self, text: str):
        def push(x: str):
            if not x:
                return
            resolved = self.resolve_variable(x)
            self.entities.list.append(Span(resolved))

        last_index = 0
        for match in ME_REGEX.finditer(text):
            push(text[last_index:match.start()])
            self.entities.list.append(self.me)
            last_index = match.end()
        push(text[last_index:])

    def push_mention(self, mention: str):
        username = mention[1:]  # skip @
        for player in self.players:
            if player.username == username:
                character = Character(player.character_name, player.id,
                                      player.full_name)
                return self.entities.list.append(character)
        return self.entities.list.append(Span(mention))

    def push_text_mention(self, user):
        for player in self.players:
            if player.user_id == user.id:
                character = Character(player.character_name, player.id,
                                      player.full_name)
                self.entities.list.append(character)
                return

    def has_me(self) -> bool:
        for segment in self.entities.list:
            if isinstance(segment, Me):
                return True
        return False

    def is_empty(self) -> bool:
        return len(self.entities.list) == 0

    def telegram_html_text(self) -> str:
        text = self.entities.telegram_html()
        if self.tags:
            tags = ' '.join(['#{}'.format(tag) for tag in self.tags])
            return '{} {}'.format(text, tags)
        else:
            return text
Пример #26
0
class Engine():
    def __init__(self):
        # Set up the game window
        #libtcod.console_set_custom_font('spritesheet.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
        libtcod.console_set_custom_font(
            'Winterwing_Curses.png',
            libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_ASCII_INROW)
        libtcod.console_init_root(game_constants.screen_width,
                                  game_constants.screen_height,
                                  'Ascetic of the Cabal',
                                  True,
                                  libtcod.RENDERER_SDL2,
                                  vsync=True)

        # Establish the primary console as well as the detail panel
        self.con = libtcod.console.Console(game_constants.screen_width,
                                           game_constants.screen_height)
        self.panel = libtcod.console.Console(game_constants.screen_width,
                                             game_constants.panel_height)

        # Create references for the player input
        self.key = libtcod.Key()
        self.mouse = libtcod.Mouse()

        self.initialize_game()

    # Initializes a lot of run-specific items. Kept outside of init because it has to be re-run on a restart
    def initialize_game(self):
        # Create and initialize the Message Log
        self.message_log = MessageLog()

        #Initialize the player
        self.player = self.initialize_player()

        # Create a game map and fill it with enemies
        self.build_map()

        self.player_target = None

        # Establish the Game State
        self.game_map.compute_dijkstra_map([self.player], 'player', True)
        self.game_map.compute_dijkstra_map(
            self.entities.get_sublist(lambda x: x.name != "Ascetic"),
            "enemies")
        self.game_state = GameStates.PLAYERS_TURN
        self.previous_game_state = GameStates.PLAYERS_TURN
        self.game_running = True

    # Creates the player object, with all associated defaults
    # This can and probably should be moved to another file
    def initialize_player(self):
        player_components = {
            "Fighter":
            Fighter(hp=300, defense=2, power=5, factions=[Factions.PLAYER]),
            "Inventory":
            Inventory(26),
            "Devotee":
            Devotee(100),
            "StatusContainer":
            StatusContainer()
        }

        player = Entity(int(game_constants.screen_width / 2),
                        int(game_constants.screen_height / 2),
                        '@',
                        libtcod.white,
                        "Ascetic",
                        True,
                        RenderOrder.ACTOR,
                        message_log=self.message_log,
                        state=AIStates.INANIMATE,
                        components=player_components)

        player.get_component("Inventory").equip_item(
            generate_starting_pistol(self.message_log))
        return player

    # Generates a game map and initializes the FOV map of it
    def build_map(self, level=1):
        self.game_map = GameMap(self.message_log, level)
        self.entities = Entities(self.game_map)
        self.game_map.make_map(self.player, self.entities)
        self.entities.insert_entity(self.player)

        self.fov_recompute = True
        self.fov_map = initialize_fov(self.game_map)

    # Literally 0 recollection what this does
    def grade_map_down(self):
        self.game_map.grade_down()

    def cull_dead(self):
        # Finds every entity in the game world with 0 or less health and kills them
        # Returns true if it kills the player, otherwise false
        player_killed = False
        dead_entities = self.entities.get_sublist(
            lambda x: x.has_component("Fighter") and not x.get_component(
                "Fighter").isAlive() and x.char != '%')

        if dead_entities:
            for dead_entity in dead_entities:
                drop = dead_entity.get_component("Fighter").die()
                if drop:
                    self.entities.insert_entity(drop)
                if dead_entity == self.player:
                    player_killed = True
        return player_killed

    def send_invalid_action_message(self):
        self.message_log.add_message(Message("Can't do that here"),
                                     libtcod.red)

    # Initializes the game's start menu, and captures any player input on that menu and passes it to the appropriate handler
    def start_screen(self):
        show_main_menu = True

        while show_main_menu:
            libtcod.sys_check_for_event(
                libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, self.key,
                self.mouse)
            main_menu(self.con, game_constants.main_menu_background_image)

            libtcod.console_flush()

            action = handle_keys(self.key, GameStates.MAIN_MENU)
            game_type = action.get('game_start')
            exit_game = action.get('action') == 'exit'

            if exit_game:
                return False
            elif game_type == 'from_scratch':
                return True
            elif game_type == 'from_save':
                self.player, self.entities, self.game_map, self.message_log, self.game_state = load_game(
                )
                self.fov_map = initialize_fov(self.game_map)
                self.entities.set_log_all(self.message_log)
                return True

    def main(self):
        # Game Loop
        while self.game_running:
            # Check input streams for an event
            libtcod.sys_check_for_event(
                libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, self.key,
                self.mouse)

            # If we need to recompute fov, do so
            if self.fov_recompute:
                recompute_fov(self.fov_map, self.player.x, self.player.y)

            # Render the game world according to current FOV, mark FOV recompute as complete, and flush to console
            render_all(self.con, self.panel, self.entities, self.player,
                       self.game_map, self.fov_map, self.fov_recompute,
                       self.message_log, self.mouse, self.game_state,
                       self.player_target)
            self.fov_recompute = False
            libtcod.console_flush()

            # Interpret the input into a game action
            input = handle_keys(self.key, self.game_state)
            action = input.get('action')

            inventory_item = input.get(
                'inventory_item') if 'inventory_item' in input else None
            dialogue_option = input.get(
                'dialogue_option') if 'dialogue_option' in input else None
            shop_option = input.get(
                'shop_option') if 'shop_option' in input else None
            unequip_item = input.get('slot') if 'slot' in input else None

            # If players turned and it's their turn to move
            if action == 'move' and self.game_state == GameStates.PLAYERS_TURN:
                # Calculate where they should move
                dx, dy = input.get('move')
                destination_x = self.player.x + dx
                destination_y = self.player.y + dy

                # TODO: This is where you hid the noclip check. Fix this for release
                #if not self.game_map.is_blocked(destination_x, destination_y):
                if True:
                    # If they're not about to walk into a wall, check for enemies at the destination
                    potential_collision_list = self.entities.get_sublist(
                        lambda ent: ent.x == destination_x and ent.y ==
                        destination_y and ent.blocks)
                    target = potential_collision_list[
                        0] if potential_collision_list else None
                    if target and target.state == AIStates.HOSTILE:
                        # If there are enemies, attack them
                        self.player.get_component("Fighter").attack(target)
                        self.game_state = GameStates.ENEMY_TURN
                    elif target and target.state == AIStates.FRIENDLY:
                        self.previous_game_state = self.game_state
                        self.player_target = target
                        self.game_state = GameStates.DIALOGUE
                    else:
                        # If there are not enemies, move and mark FOV for recomputation
                        self.player.move(dx, dy, self.game_map)
                        self.fov_recompute = True
                        self.game_map.compute_dijkstra_map([self.player],
                                                           'player', True)
                        self.game_state = GameStates.ENEMY_TURN

            # If the player grabs something, check if there is an object at their feet, and either have them pick it up (if it's an Item) or add it to their wallet (if it's money)
            elif action == 'grab' and self.game_state == GameStates.PLAYERS_TURN:
                for item in self.entities.get_sublist(
                        lambda entity: (entity.has_component("Item") or entity.
                                        has_component("Money")) and entity.x ==
                        self.player.x and entity.y == self.player.y):
                    if item.has_component("Money"):
                        self.player.get_component("Fighter").pick_up_money(
                            item)
                    else:
                        self.player.get_component("Inventory").add_item(item)
                    self.entities.remove_entity(item)
                    self.game_state = GameStates.ENEMY_TURN

            # Open up the inventory menu
            elif action == 'inventory' and inventory_item is None:
                self.previous_game_state = self.game_state
                self.game_state = GameStates.INVENTORY_OPEN

            # Open up the equipped menu
            elif action == 'equipped':
                self.previous_game_state = self.game_state
                self.game_state = GameStates.EQUIPPED_OPEN

            elif action == 'unequip' and self.game_state == GameStates.EQUIPPED_OPEN:
                self.player.get_component("Inventory").unequip_slot(
                    unequip_item)

            # if the player has selected an inventory item to use, get the item object, and equip it if it's vgear, or use it if it's a consumable (like a potion)
            elif inventory_item is not None and self.previous_game_state != GameStates.PLAYER_DEAD and inventory_item < len(
                    self.player.get_component("Inventory").items):
                item_entity = self.player.get_component(
                    "Inventory").items[inventory_item]
                if ItemType(item_entity.get_component(
                        "Item").item_type) != ItemType.NONE:
                    self.player.get_component("Inventory").equip_item(
                        item_entity)
                else:
                    print("In the else")
                    if item_entity.get_component("Item").use(self.player):
                        self.player.get_component("Inventory").remove_item(
                            item_entity)

            # if the player is in dialogue, provide the dialogue option to the target's Character object
            elif dialogue_option is not None:
                dialogue_response = self.player_target.get_component(
                    "Character").talk(dialogue_option)
                if dialogue_response.shop:
                    self.game_state = GameStates.SHOPPING

            # if the player attempts to go down some stairs, make sure they're on stairs, then build a new map and clear the console
            elif action == 'go_down' and self.game_state == GameStates.PLAYERS_TURN:
                stairs_candidates = self.entities.get_sublist(
                    lambda entity: entity.x == self.player.x and entity.y ==
                    self.player.y and entity.has_component("Stairs"))
                if stairs_candidates:
                    self.build_map(
                        stairs_candidates[0].get_component("Stairs").floor)
                    libtcod.console_clear(self.con)

            # Save the game
            elif action == 'save':
                save_game(self.player, self.entities, self.game_map,
                          self.message_log, self.game_state)

            # if the player draws their gun, change to a player shoot state and await gunfire
            elif self.game_state == GameStates.PLAYERS_TURN and action == 'gun':
                if (self.player.get_component("Inventory").slot_filled(
                        "RANGED")):
                    self.previous_game_state = self.game_state
                    self.game_state = GameStates.PLAYER_SHOOT
                    self.message_log.add_message(
                        Message(
                            "Taking aim. Click on your target, or e to holster"
                        ))
                else:
                    self.message_log.add_message(
                        Message("No ranged weapon equipped!"))

            # if the player already has their gun drawn and presses the draw button, holster it instead
            elif self.game_state == GameStates.PLAYER_SHOOT and action == 'holster':
                self.game_state = self.previous_game_state
                self.message_log.add_message(Message("Holstered your weapon"))

            # if the player has their gun drawn and clicks on a target, check if there is line of sight
            # and if so, shoot the target. This sets the AI to hostile if it isn't already (this should be handled by Fighter)
            elif self.game_state == GameStates.PLAYER_SHOOT and self.mouse.lbutton_pressed:
                target = get_shoot_target(self.mouse, self.entities,
                                          self.fov_map)
                if (target):
                    line_of_sight = draw_line((self.player.x, self.player.y),
                                              (target.x, target.y))
                    if not [
                            space for space in line_of_sight
                            if self.game_map.is_blocked(space[0], space[1])
                    ]:
                        self.player.get_component("Fighter").ranged_attack(
                            target)
                        target.state = AIStates.HOSTILE
                        self.game_state = GameStates.ENEMY_TURN
                    else:
                        self.message_log.add_message(
                            Message("You don't have a clear line of sight!"))

            # if the player right clicks something, get open up the inspect menu for that target
            elif self.mouse.rbutton_pressed and self.game_state != GameStates.INSPECT_OPEN:
                target = get_shoot_target(self.mouse, self.entities,
                                          self.fov_map, False)
                if (target):
                    self.player_target = target
                    self.previous_game_state = self.game_state
                    self.game_state = GameStates.INSPECT_OPEN

            # If the player is buying something, they make the purchase
            elif action == 'buy' and shop_option is not None:
                target.get_component("Shop").purchase(shop_option, self.player)

            elif action == 'status':
                self.previous_game_state = self.game_state
                self.game_state = GameStates.STATUS

            # Exit the game
            if action == 'exit' and (self.game_state in [
                    GameStates.INVENTORY_OPEN, GameStates.DIALOGUE,
                    GameStates.EQUIPPED_OPEN, GameStates.SHOPPING,
                    GameStates.INSPECT_OPEN, GameStates.STATUS
            ]):
                self.game_state = self.previous_game_state
            elif action == 'exit':
                return True

            # Set the game to fullscreen
            if action == 'fullscreen':
                libtcod.console_set_fullscreen(
                    not libtcod.console_is_fullscreen())

            # cull_dead returns true if the player is dead, so this conditional calls it to cull the dead, and then
            # checks if the game is over
            if self.cull_dead():
                self.game_state = GameStates.PLAYER_DEAD

            # when it's the AI's turn, find every entity that has AI and move it (if it's hostile)
            if self.game_state == GameStates.ENEMY_TURN:
                for entity in self.entities.get_entity_set():
                    if entity.has_component(
                            "AI") and entity.state == AIStates.HOSTILE:
                        entity.get_component("AI").take_turn(
                            self.player, self.fov_map, self.game_map,
                            self.entities)
                        if self.cull_dead():
                            self.game_state = GameStates.PLAYER_DEAD
                    if entity.has_component("StatusContainer"):
                        entity.get_component("StatusContainer").tick_clocks()
                        for status in entity.get_component(
                                "StatusContainer").get_statuses():
                            status_mapping[status](entity, self.entities,
                                                   self.game_map)

                if self.game_state != GameStates.PLAYER_DEAD:
                    self.player.get_component("StatusContainer").tick_clocks()
                    for status in self.player.get_component(
                            "StatusContainer").get_statuses():
                        status_mapping[status](self.player, self.entities,
                                               self.game_map)
                    self.game_map.compute_dijkstra_map(
                        self.entities.get_sublist(
                            lambda x: x.name != "Ascetic"), "enemies")
                    self.game_state = GameStates.PLAYERS_TURN

            # TODO: need a check somewhere around here to tick condition clocks, and then to apply conditions

            if action == 'restart':
                libtcod.console_clear(self.con)
                self.initialize_game()
Пример #27
0
class Controler():
    """
    Controler class
    """
    def __init__(self):
        self.date_time = time.strftime("%Y.%m.%d-%H.%M.%S")

        self.params = Params()
        self.environment = Environment()

        if self.params.inherit_nn:  # F
            self.inherit_nn = Agent()
            self.inherit_nn.load_network(fname=self.params.inherit_nn)
        else:
            self.inherit_nn = None  # T

        if self.params.general_nn:  # T
            if self.params.inherit_nn:  # F
                self.general_nn = self.inherit_nn
            else:  # T
                self.general_nn = Agent()  # first network

            self.entities = Entities(environment=self.environment,
                                     general_nn=self.general_nn)  # new NN
        else:  # F
            self.general_nn = None
            self.entities = Entities(environment=self.environment,
                                     inherit_nn=self.inherit_nn)

        if self.params.memory_load:
            self.general_nn.load_memory(fname="out/" + self.params.memory_load)
            print("memory replay loaded")

        self.epoch = 1

        if self.params.simulate:
            self.load_scenario()

        if self.params.window_show:
            self.window = Window()
            self.render()

        if self.params.interactive:
            self.to_iterate = 0
            self.running = True
            self.clear()
            self.idle()
        else:
            self.loop()

        # save neural network & experience_replay
        if self.params.save_model and self.params.general_nn:
            self.general_nn.save_network(
                fname=f"out/weights-{self.date_time}.model")
            self.general_nn.save_memory(
                fname=f"out/stack-{self.date_time}.memory")

    def load_scenario(self):
        """
        Simulate a scenario
        """
        self.entities.clear()

        # 3 creatures fixed positions in random order creation

        rnd = numpy.random.permutation(3)
        pos_x = numpy.array([3, 2, 3])
        pos_y = numpy.array([3, 3, 2])
        strength = numpy.array([0.9, 0.5, 0.1])

        for i in rnd.argsort():
            self.entities.spawn_creature(pos_x=pos_x[i],
                                         pos_y=pos_y[i],
                                         strength=strength[i])

    def clear(self):
        """
        The clear code for terminal
        """
        print("\033c")

    def render(self):
        """
        Render
        """
        self.window.render(self.environment.grass, self.entities.creatures,
                           self.epoch)

    def refresh(self, sleep=0):
        """
        Update screen
        """
        if self.window.handle_event():
            self.render()
        time.sleep(sleep)

    def next_epoch(self):
        """
        Next Iteration
        """
        if self.params.simulate:
            self.load_scenario()

        self.environment.grow_grass(reset=self.params.simulate)
        self.environment.epoch = self.epoch

        # move creatures
        self.entities.iterate()

        while len(self.entities.creatures) < self.params.min_n_creatures:
            self.entities.spawn_creature()  ## verify that general_nn is acting

        if self.params.window_show:
            self.render()

        self.epoch += 1

    def console(self):
        """
        –
        """
        self.clear()
        print(f'Epoch: {self.epoch}')
        print(f'Creatures: {len(self.entities.creatures)}')
        print("Type 'help' for a list of commands.")

    def help(self):
        """
        –
        """
        self.clear()
        print('iter -i [indefinately] -c [counter] -s [stop]')
        print('exit')
        print('')
        print('Type enter to return to idle.')

    def iterate(self, args):
        """
        –
        """
        if len(args) == 1:
            self.clear()
            print('Please enter appropriate parameters')
        if args[1] == '-i':
            self.to_iterate = -1
        elif args[1] == '-c':
            if len(args) == 3:
                self.to_iterate = int(args[2])
                print(f'Added {args[2]} iteratios to queue. ')
                self.console()
        elif args[1] == '-s':
            self.to_iterate = 0
            self.console()

    def idle(self):
        """
        Updates window for input while not iterating
        """
        self.console()

        while self.running:
            if self.params.window_show and not self.entities.random_policy:
                self.refresh(sleep=0.1)  # wait until initial random end

            if select.select([
                    sys.stdin,
            ], [], [], 0.0)[0]:
                args = input().split()
                if len(args) == 0:
                    self.console()
                else:
                    if args[0] == 'help':
                        self.help()
                    elif args[0] == 'iter':
                        self.iterate(args)
                    elif args[0] == 'exit':
                        self.clear()
                        self.running = False
                    else:
                        self.clear()
                        print('Command not understood.')

                termios.tcflush(sys.stdin, termios.TCIOFLUSH)

            if self.to_iterate == -1:
                self.next_epoch()
                if self.params.verbose:
                    print(f"epoch: {self.epoch}")
            elif self.to_iterate > 0:
                self.next_epoch()
                self.to_iterate -= 1

            if self.params.window_show:
                if self.window.quit_command:
                    self.to_iterate = 0
                    self.running = False
                    self.window.close_window()

    def loop(self):
        """
        Loops epochs.
        """
        if self.params.verbose:
            print(f"Running for {self.params.max_epochs} epochs")
        for _ in range(self.params.max_epochs):
            if self.params.window_show:
                self.refresh(sleep=.1)
                if self.window.quit_command:
                    self.window.close_window()
                    break

            self.next_epoch()
            if self.params.verbose and self.epoch % 1000 == 0:
                print(f"Epoch: {self.epoch}")
Пример #28
0
class TestGame(Widget):
    def init_collision_ids(self):
        self.ultrasound_count = 10

        # self.collide_control = CollideControl(self.ultrasound_count)
        # self.collision_ids = self.collide_control.collision_ids
        self.collision_ids = {
            "wall": 1,
            "obstacle_rect": 2,
            "obstacle": 3,
            "asteroid": 5,
            "ultrasound_detectable": 0,
            "ultrasound": [50 + i for i in range(self.ultrasound_count)],
            "robot": 100,  # let it be free after robot for num of robot create
            "candy": 42,
        }

        detected_names = ["wall", "obstacle", "obstacle_rect", "robot"]

        self.collision_ids["ultrasound_detectable"] = list(
            {self.collision_ids[name]
             for name in detected_names})
        print("ultrasound_detectable")
        print(self.collision_ids["ultrasound_detectable"])

        # ignore touch of user
        self.ignore_groups = []
        self.ignore_groups.extend(self.collision_ids["ultrasound"])
        # [ self.ignore_groups.append(self.collision_ids[key]) for key in ['robot']]

    def __init__(self, **kwargs):
        self.init_collision_ids()
        super(TestGame, self).__init__(**kwargs)

        self.gameworld.init_gameworld(
            [
                "cymunk_physics",
                "poly_renderer",
                "rotate_poly_renderer",
                "rotate_renderer",
                #'steering_system'
                "rotate",
                "position",
                "cymunk_touch",
            ],
            callback=self.init_game,
        )

    def info(self, text):
        self.app.info_text += "\n" + str(text)

    def init_game(self):
        # called automatically? probably
        self.pp = pprint.PrettyPrinter(indent=4)
        self.pprint = self.pp.pprint

        self.field_size = 800, 600
        self.to_draw_obstacles = 0

        self.robot = None
        self.robots = None

        self.setup_states()
        self.set_state()
        self.init_loaders()
        print("init_physicals")
        self.init_physicals()
        # self.init_space_constraints()

        self.init_properties_updater()

        self.init_control_logic()

    def init_control_logic(self):
        self.init_chase_candy_updater()

    def init_loaders(self):
        self.fl = FileLoader(self)

    def init_physicals(self):
        #        self._entities = {}
        self.robot_names = ["dalek", "drWho", "k9", "kachna"]
        self.num_of_robots = len(self.robot_names)

        self.setup_collision_callbacks()

        self.entities = Entities(self.app)

        self.map = Map2D(self)

        self.asteroids = Asteroids(self)
        self.init_robots()

    def init_robots(self):
        self.robots = [
            self.get_robot(name, i) for i, name in enumerate(self.robot_names)
        ]

        self.candy = Candy(self)

    def unused_load_robot_svg(self, robot):
        self.fl.load_svg(robot.path, self.gameworld)

    def add_robot(self):
        i = len(self.robots)
        name = f"robot_{i}"
        robot = self.get_robot(name, i)
        self.robots.append(robot)

    def get_robot(self, name, i):
        drive = "mecanum"
        us_count = 3
        return Robot(
            root=self,
            drive=drive,
            robot_name=name,
            us_id_offset=i * us_count,
            robot_number=i,
        )

    def toggle_robot_control(self, state):
        self.robot_controlled = state

        if not state:
            return

        for r in self.robots:
            r.add_state("INIT")
            r.reset_ultrasounds()

    def init_chase_candy_updater(self):
        for r in self.robots:
            r.chase_candy(self.candy)
        self.robot_controlled = False
        Clock.schedule_once(self.chase_candy_update)

    def chase_candy_update(self, dt):
        if self.robot_controlled:
            for r in self.robots:
                r.goto_target()
        Clock.schedule_once(self.chase_candy_update, 0.05)

    def draw_asteroids(self):
        self.asteroids.draw_asteroids()

    def setup_collision_callbacks(self):
        """Setup the correct collisions for the cymunk physics system manager.

        use the physics_system.add_collision_handler
        to define between which collision_ids the collision should happen and between which not

        Following handler functions are passed
        - begin_func - called once on collision begin
        - separate_func - called once on collision end
        """

        physics_system = self.gameworld.system_manager["cymunk_physics"]

        def ignore_collision(na, nb):
            """Returns false to indicate ignoring the collision."""
            return False

        # collide_remove_first

        # add robots
        us_detectable = self.collision_ids["ultrasound_detectable"]
        rob_collision_ids = [
            self.collision_ids["robot"] + ct
            for ct in range(self.num_of_robots)
        ]
        us_detectable.extend(rob_collision_ids)

        self.begin_ultrasound_callback = {}

        # ignore_collision of ultrasound triangle with 0-1024 collision_ids
        # to enable the triangles to clip through other objects
        # ! this should be done on robot / on ultrasound creation
        for us_id in self.collision_ids["ultrasound"]:
            for index_id in range(1024):
                physics_system.add_collision_handler(
                    index_id,
                    us_id,
                    begin_func=ignore_collision,
                    separate_func=ignore_collision,
                )

        # add ultrasound triangles object detection via collision
        # ! this should be done on robot / on ultrasound creation
        for us_id in self.collision_ids["ultrasound"]:
            for detectable in us_detectable:
                print("us_id", us_id)
                physics_system.add_collision_handler(
                    detectable,
                    us_id,
                    begin_func=self.return_begin_ultrasound_callback(
                        us_id, True),
                    separate_func=self.return_begin_ultrasound_callback(
                        us_id, False),
                )

        for r_ct in rob_collision_ids:
            from pudb.remote import set_trace

            set_trace(term_size=(238, 54), host="0.0.0.0", port=6900)  # noqa
            physics_system.add_collision_handler(
                self.collision_ids["candy"],
                r_ct,
                begin_func=self.begin_candy_callback,
                separate_func=self.begin_candy_callback,
            )

    def candy_caught(self, robot_ent_id):
        print("candy eaten! by robot:", robot_ent_id)
        self.candy.reset_position()
        self.to_draw_obstacles = 2

    def begin_candy_callback(self, space, arbiter):
        # self.r
        robot_ent_id = arbiter.shapes[1].body.data
        # us[us_id] = rob
        self.candy_caught(robot_ent_id)
        return False

    def get_robot_from_us_id(self, us_id):
        for r in self.robots:
            if r.is_this_us_mine(us_id):
                return r
        return None

    def get_robot_from_ent_id(self, robot_id):
        for r in self.robots:
            if r.ent == robot_id:
                return r
        return None

    def return_begin_ultrasound_callback(self, us_id, state):
        # this adds the segmentation fault on exit - but currently I am not able to simulate ultrasounds any other way than
        # returning
        def begin_ultrasound_callback(self, space, arbiter):
            # ent0_id = arbiter.shapes[0].body.data #detectable_object
            # ent1_id = arbiter.shapes[1].body.data #robot
            space.enable_contact_graph = True
            # print(space.bodies)

            ent0 = arbiter.shapes[0]
            e_id = ent0.body.data
            # a = ent0.body
            # print(len(arbiter.shapes))
            con = arbiter.contacts
            # print(a)
            # print(dir(a))
            # print(a.contact)
            rob_ent = arbiter.shapes[1].body.data

            if con is not None:
                r = self.get_robot_from_ent_id(rob_ent)

                # r = self.get_robot_from_us_id(us_id)
                r.ultrasound_detection(us_id, ent0, state)
                ent = self.gameworld.entities[e_id]
                cat = [
                    cat for cat, id_list in self.entities.items()
                    if e_id in id_list
                ]
                # print('detect', cat, e_id)
            return False

        ind = 2 * us_id + int(state)
        self.begin_ultrasound_callback[ind] = types.MethodType(
            begin_ultrasound_callback, self)
        return self.begin_ultrasound_callback[ind]
        # return begin_ultrasound_callback

    def add_entity(self, ent, category):
        # add to entity counter
        print("added entity", category)
        if category not in self.entities.keys():
            self.entities[category] = []
        self.entities.add_item(category, ent)

    def set_robots_rand(self):
        for r in self.robots:
            r.set_random_position()

    def kick_robots(self):
        for r in self.robots:
            self.kick_robot(r)

    def kick_robot(self, r):
        rob_ent = r.ent
        print(rob_ent)

        rob_body = self.gameworld.entities[rob_ent].cymunk_physics.body

        im = (10000, 10000)
        seq = [-1, 1]
        imp = (choice(seq) * randint(*im), choice(seq) * randint(*im))
        rob_body.apply_impulse(imp)
        print("impulse", imp)

    def init_entity(
        self,
        component_dict,
        component_order,
        category="default_category",
        object_info=None,
    ):
        if object_info is not None:
            category = object_info.get("category", category)
        else:
            object_info = {}

        ent = self.gameworld.init_entity(component_dict, component_order)

        # add to counter
        self.add_entity(ent, category)

        object_info.update({"ent": ent})
        entity_info = object_info

        # print('@'*42)
        # self.pprint(entity_info)
        # print(Robot.cats, category in Robot.cats)

        # add to specific subobjects
        # if self.robot is not None:
        #   self.robot.add_entity(entity_info)
        #      if category == 'robot':
        #         print('added robot')

        return ent

    def destroy_all_entities(self):
        self.destroy_entities()

    def destroy_entities(self, cat_list=None, skip_cat_list=None):
        for ent_cat, ent_list in self.entities.items():
            delete = False
            if cat_list is None and skip_cat_list is None:
                delete = True
            else:
                if cat_list is None:
                    if ent_cat not in skip_cat_list:
                        delete = True
                else:
                    if ent_cat in cat_list:
                        delete = True

            if delete:
                prinf("Clearing entities of " + ent_cat)
                for ent in ent_list:
                    self.destroy_created_entity(ent, 0)
                self.entities[ent_cat].clear()
        for r in self.robots:
            r.reset_ultrasounds()

    def destroy_created_entity(self, ent_id, dt):
        self.gameworld.remove_entity(ent_id)

    # def draw_some_stuff(self):
    #   self.load_svg('objects.svg', self.gameworld)
    # self.load_svg('map.svg', self.gameworld)
    #    self.map.draw_stuff()
    #       self.load_svg('map.svg', self.gameworld)

    def draw_obstacles(self):
        self.map.draw_obstacles(5)

    def draw_rect_obstacles(self):
        self.map.draw_rect_obstacles(5)

    def update(self, dt):
        self.gameworld.update(dt)

    def setup_states(self):
        self.gameworld.add_state(
            state_name="main",
            systems_added=["poly_renderer"],
            systems_removed=[],
            systems_paused=[],
            systems_unpaused=["poly_renderer"],
            screenmanager_screen="main",
        )

    def set_state(self):
        self.gameworld.state = "main"

    def init_properties_updater(self):
        Clock.schedule_once(self.update_properties)

    def update_properties(self, dt):
        self.app.ultrasound_status = "\n".join(
            [r.ultrasound_status() for r in self.robots])
        self.app.robot_states = "\n\n".join(
            [str(r.states) for r in self.robots])
        #       self.app.robot_score =
        #   self.r.reset_ultrasounds()
        if self.to_draw_obstacles > 0:
            self.map.draw_obstacles(self.to_draw_obstacles)
            self.to_draw_obstacles = 0
        Clock.schedule_once(self.update_properties, 0.05)
Пример #29
0
from merge import Merge
from writer import Write


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="re_annotation")
    parser.add_argument('--bunch', help='Which set is going to compare')
    args = parser.parse_args()

    bunch = args.bunch

    input_dir, output_dir = Utils.init_paths()
    annotators = Utils.annators_name(input_dir)

    variable_dict, variable_hash_dict, section_dict= Entities.get_annotators_entities(bunch,
                                                                                    annotators,
                                                                                    input_dir,
                                                                                    t_number=False)

    merged_variables, owner_file = Merge.merge_entities(variable_dict)
    merged_variables = Entities.sorted_entities(merged_variables)

    merged_sections, _ = Merge.merge_entities(section_dict)
    merged_variables_hash = Merge.merge_hash(variable_hash_dict)

    ctakes_dir = input_dir.replace("input", "ctakes_output")
    ctakes_variables, ctakes_variables_hash, ctakes_sections = Entities.get_ctakes_entities(bunch,
                                                                                            ctakes_dir,
                                                                                            t_number=False)

    merged_variables, merged_variables_hash, merged_sections = Merge.merge_ctakes_annotators(merged_variables,
                                                                            merged_variables_hash,
Пример #30
0
 def __init__(self, world):
     self.world = world
     self.entities = Entities(self)
     self.grid = Grid(self)
     self.sign_waypoints = SignWayPoints(self)