Exemplo n.º 1
0
    def __init__(self, character_proto: character_pb2.Character):

        self.character_proto = character_proto

        self.name = character_proto.name

        self.base_speed = character_proto.base_speed
        self.base_max_health = character_proto.base_max_health
        self.base_attack = character_proto.base_attack
        self.base_defense = character_proto.base_defense

        self.current_health = character_proto.current_health
        self.experience = character_proto.experience
        self.level = character_proto.level

        self.ticks_since_death = character_proto.ticks_since_death
        self.dead = character_proto.is_dead

        self.position = Position(character_proto.position)
        self.spawn_point = Position(character_proto.spawn_point)

        self.weapon = Weapon(character_proto.weapon)

        self.active_effects = []
        for tup in zip(
                        character_proto.active_effects_temp_status_modifier,
                        character_proto.active_effects_source,
                        character_proto.active_effects_is_player):
            effect = TempStatusModifier(tup[0])
            source = tup[1]
            is_player = tup[2]

            self.active_effects.append((effect, source, is_player))

        self.tagged_players_damage = character_proto.tagged_players_damage
Exemplo n.º 2
0
 def find_position_to_move(self, player: Position,
                           destination: Position) -> Position:
     path = self.api.find_path(player.get_position(), destination)
     pos = None
     if len(path) < player.get_speed():
         pos = path[-1]
     else:
         pos = path[player.get_speed() - 1]
     return pos
Exemplo n.º 3
0
def find_position_to_move(api, player: Player, destination: Position, logger,
                          graph) -> Position:
    #path = api.find_path(player.get_position(), destination)
    path, _ = graph.AStarSearch(player.get_position(), destination)
    pos = None
    logger.info(f"Path: {path}")
    if len(path) <= player.get_speed():
        coordinates = path[-1]
        pos = Position.create(coordinates[0], coordinates[1],
                              player.get_position().board_id)
    else:
        coordinates = path[player.get_speed()]
        pos = Position.create(coordinates[0], coordinates[1],
                              player.get_position().board_id)
    logger.info(f"target destination: {coordinates}")
    return pos
Exemplo n.º 4
0
 def zhou_astar_path_to_move(self, player: Position, destination: Position):
     frontier = []
     path = []
     pp = player.get_position()
     heapq.heapify(frontier)
     start = Node(0, None, (pp.x, pp.y))
     heapq.heappush(frontier, (0, (pp.x, pp.y), start))
     visited = set()
     visited.add((pp.x, pp.y))
     while frontier:
         curr = heapq.heappop(frontier)[2]
         if curr.coord == (destination.x, destination.y):
             while curr:
                 path.append(curr.coord)
                 self.logger.info(str(curr.coord))
                 curr = curr.prev
             path.reverse()
             break
         neighbors = self.getNeighbors(curr.coord[0], curr.coord[1])
         for n in neighbors:
             if n not in visited:
                 dist = abs(destination.y - n[1]) + abs(destination.x -
                                                        n[0])
                 newNode = Node(curr.cost + 1, curr, n)
                 heapq.heappush(
                     frontier,
                     (newNode.cost + dist, newNode.coord, newNode))
                 visited.add(n)
     return path[1]
def make_our_combat_decision(api, my_player, logger, monsters,
                             searching_graph):
    curr_pos = my_player.get_position()
    target_enemy = find_ideal_monster(api, my_player, monsters)
    enemy_pos = target_enemy.get_position()

    if curr_pos.manhattan_distance(
            enemy_pos) <= my_player.get_weapon().get_range():
        return CharacterDecision(decision_type="ATTACK",
                                 action_position=enemy_pos,
                                 action_index=None), target_enemy
    else:
        pos = Position(my_player.get_position())
        pos.y = my_player.get_position().y + my_player.get_speed()
        return CharacterDecision(decision_type="MOVE",
                                 action_position=helpers.find_position_to_move(
                                     api, my_player, enemy_pos, logger,
                                     searching_graph),
                                 action_index=None), target_enemy
def loot_items(api, my_player, logger, board, item_tiles):
    #### grab one based on closeness ####
    current_tile_items = board.get_tile_at(my_player.get_position()).items
    if current_tile_items is not None and len(current_tile_items) > 0:
        return CharacterDecision(decision_type="PICKUP",
                                 action_position=None,
                                 action_index=0)

    item_tiles.sort(key=lambda x: Position(x[1], x[2],
                                           my_player.get_position().board_id).
                    manhattan_distance(my_player.get_position()))
    return CharacterDecision(decision_type="MOVE",
                             action_position=item_tiles[0],
                             action_index=0)
Exemplo n.º 7
0
    def get_path(self, start: Position, end: Position, avoid_hashes):
        deltas = [(0, 1), (-1, 0), (0, -1), (1, 0)]
        # deltas = bfs_deltas[1024]
        lowest = start.manhattan_distance(end)
        path = [start]

        queue = deque([start])
        visited = {
        }  # map node to its next node, so start maps to next_node, 2nd to last node maps to end
        start_hash = self.hash_pos(start)
        visited[start_hash] = None
        while (len(queue) > 0):
            pos = queue.popleft()
            # reach end, return next node
            curr_hash = self.hash_pos(pos)
            if self.equal_pos(pos, end):
                # backtrack
                bt_hash = self.hash_pos(pos)
                bt_path = deque([])
                while bt_hash in visited:
                    prev_node_hash = visited[bt_hash]
                    next_node = self.read_pos_hash(bt_hash)
                    bt_path.appendleft(next_node)
                    if prev_node_hash == self.hash_pos(start):
                        self.logger.info(
                            "Bfs path start {} next {} end {}".format(
                                self.get_position_str(start),
                                self.get_position_str(next_node),
                                self.get_position_str(end)))
                        return bt_path
                    bt_hash = prev_node_hash
                break
            for delta in deltas:
                dx = delta[0]
                dy = delta[1]
                check_pos: Position = self.create_pos(pos.x + dx, pos.y + dy)
                tile: Tile = self.player_board.get_tile_at(check_pos)
                if tile.type == "BLANK":
                    check_hash = self.hash_pos(check_pos)
                    if check_hash not in visited and check_hash not in avoid_hashes:
                        queue.append(check_pos)
                        visited[check_hash] = curr_hash
                else:
                    pass
                # dist = check_pos.manhattan_distance(end)
                # if dist < lowest:
                #     path[0] = check_pos
                #     lowest = dist
        self.logger.info("exhausted bfs...")
        return path
Exemplo n.º 8
0
    def __init__(self, proto_board: board_pb2.Board):

        self.proto_board = proto_board

        self.width = proto_board.width
        self.height = proto_board.height
        self.grid = []

        for x in range(self.width):
            row = []
            for y in range(self.height):
                row.append(proto_board.grid[x * self.height + y])
            self.grid.append(row)

        self.portals = []
        for i in range(len(proto_board.portals)):
            self.portals.append(Position(proto_board.portals[i]))
    def make_decision(self, player_name: str,
                      game_state: GameState) -> CharacterDecision:
        """
        Parameters:
        player_name (string): The name of your player
        game_state (GameState): The current game state
        """
        ######################## Initialize ########################
        self.api = API(game_state, player_name)
        self.my_player = game_state.get_all_players()[player_name]
        self.current_board = game_state.get_board(
            self.my_player.get_position().board_id)
        self.curr_pos = self.my_player.get_position()
        self.monsters_on_board = {
            name: monster
            for name, monster in game_state.get_all_monsters().items()
            if monster.get_position().board_id == self.curr_pos.board_id
        }
        target_monster = None
        self.searching_graph = search.Graph(
            self.current_board,
            self.my_player.get_position().board_id)
        self.logger.info("In make_decision")

        # self.logger.info(helpers.TELL_ME_ME(self.my_player))

        ######################## TAKE TURN HERE ########################
        # self.logger.info(f"All monsters: {game_state.get_all_monsters()}")
        #decision = CharacterDecision(
        #        decision_type="MOVE",
        #        action_position=Position(self.curr_pos.x+2, self.curr_pos.y, "tb_tbdt_dt"),
        #        action_index=None)

        # Combine nearby items with inventory items
        available_items_tiles = helpers.non_api_find_items(
            self.my_player, self.current_board, self.my_player.get_speed(),
            self.logger)
        available_items = self.my_player.inventory + [
            tup[0] for tup in available_items_tiles
        ]
        self.logger.info(f"My Player: {self.my_player.__dict__}")
        self.logger.info(f"Current position: {self.curr_pos.__dict__}")
        self.logger.info(f"Available items around: {available_items_tiles}")
        self.logger.info(f"Our inventory: {self.my_player.inventory}")
        self.logger.info(
            f"Our weapon actual: {self.my_player.get_weapon().__dict__}")
        self.logger.info(
            f"Our weapon: {self.my_player.get_weapon().stats.__dict__}")
        self.logger.info(
            f"Our clothes: {self.my_player.get_clothes().stats.__dict__}")
        self.logger.info(
            f"Our shoes: {self.my_player.get_shoes().stats.__dict__}")
        self.logger.info(f"Our hat: {self.my_player.get_hat().stats.__dict__}")
        self.logger.info(
            f"Our accessory: {self.my_player.get_accessory().stats.__dict__}")

        # best item to equip here!
        item_index = helpers.should_we_equip(self.my_player, available_items,
                                             self.logger)
        self.logger.info(f"Item index: {item_index}")
        # Find non-consumable items
        droppable_items = [
            (index, item)
            for index, item in enumerate(self.my_player.inventory)
            if type(item) in [Weapon, Clothes, Shoes, Hat, Accessory]
        ]
        nearby_monsters = helpers.monsters_in_range(
            self.my_player, list(self.monsters_on_board.values()))

        if item_index != -1 and item_index >= len(self.my_player.inventory):
            target_item, x, y = available_items_tiles[
                item_index - len(self.my_player.inventory)]
            if x != self.curr_pos.x or y != self.curr_pos.y:
                pos = Position.create(x, y, self.curr_pos.board_id)
                decision = decision_maker.head_to(pos)
            else:
                if len(self.my_player.inventory) >= 16:
                    self.logger.warning(
                        "==================INVENTORY FULL??? pOG================"
                    )
                else:
                    decision = decision_maker.pickup(self.my_player,
                                                     target_item,
                                                     self.current_board)
        elif item_index != -1:
            decision = decision_maker.equip_given_item(item_index)
        elif nearby_monsters:
            decision, target_monster = decision_maker.make_our_combat_decision(
                self.api, self.my_player, self.logger, self.monsters_on_board,
                self.searching_graph)
        elif droppable_items:
            index, item = droppable_items[0]
            decision = decision_maker.drop_item(index)
        #elif available_items_tiles:
        #    decision = decision_maker.loot_items(self.api, self.my_player, self.logger, self.current_board, available_items_tiles)
        else:
            decision, target_monster = decision_maker.make_our_combat_decision(
                self.api, self.my_player, self.logger, self.monsters_on_board,
                self.searching_graph)

        #decision = decision_maker.make_our_weapon_decision(self.api, self.my_player, self.logger)
        #decision = decision_maker.head_to_portal_decision(self.api, self.my_player, self.logger)
        # decision_maker.head_to_portal_decision(self.api, self.my_player, self.logger)
        self.logger.info(f"We are doing {decision.__dict__}")
        try:
            self.logger.info(
                f"Position is: {decision.action_position.__dict__}")
        except:
            pass
        self.logger.info(
            f"Player experience: {self.my_player.get_total_experience()}")
        #try:
        # self.logger.info(f"====near target monsters=== {[mon.__dict__ for mon in list(self.monsters_on_board.values()) if abs(self.my_player.get_level() - mon.get_level()) < 4][:9]}")
        #except:
        #    self.logger.info(f"======================log failed")
        ######################## Logging ########################
        self.memory.set_value("last_decision", decision)
        self.memory.set_value("last_target_monster", target_monster)
        self.logger.info(
            f"{target_monster.__dict__ if target_monster else 'None'}")
        self.memory.set_value("last_current_health",
                              self.my_player.current_health)

        ######################## END TURN ########################
        return decision
        """
Exemplo n.º 10
0
    def make_decision(self, player_name: str,
                      game_state: GameState) -> CharacterDecision:
        """
        Parameters:
        player_name (string): The name of your player
        game_state (GameState): The current game state
        """
        self.logger.info(
            "==========================NEW TURN==========================")
        self.api = API(game_state, player_name)
        self.character = game_state.get_character(player_name)
        self.my_player = game_state.get_all_players()[player_name]
        #self.pvpboard = game_state.get_pvp_board()
        self.board = game_state.get_board(self.board_id)
        self.curr_pos = self.my_player.get_position()
        self.monsters = game_state.get_monsters_on_board(self.board_id)

        self.obstacles = self.get_obstacles(game_state)
        self.bad_monster_squares = self.get_monsters(game_state, self.board_id)
        # cycle through items
        items = self.my_player.get_inventory()
        self.logger.info("items: {}".format(items))
        cur_weapon = self.my_player.get_weapon
        self.logger.info('performing inventory check')
        try:
            if self.character.clothes is not None:
                self.print_stats(self.character.clothes)
        except:
            self.logger.info("no clothes to print")
            pass
        try:
            if self.character.hat is not None:
                self.print_stats(self.character.hat)
        except:
            self.logger.info("no hat to print")
            pass
        try:
            if self.character.weapon is not None:
                self.print_stats(self.character.weapon)
        except:
            self.logger.info("no weapon to print")
            pass
        try:
            if self.character.shoes is not None:
                self.print_stats(self.character.shoes)
        except:
            self.logger.info("no shoes to print")
            pass
        try:
            if self.character.accessory is not None:
                self.print_stats(self.character.accessory)
        except:
            self.logger.info("no accessory to print")
            pass
        for i, item in reversed(list(enumerate(items))):
            # self.logger.info('exp change: {}, {}'.format(item.get_flat_experience_change(), item.get_percent_experience_change()))
            # self.logger.info('atk change: {}, {}'.format(item.get_flat_attack_change(), item.get_percent_attack_change()))
            # if item.get_flat_attack_change() > cur_weapon.get_flat_attack_change():
            #     self.logger.info('equiping item')
            #     return CharacterDecision(
            #         decision_type="EQUIP",
            #         action_position=None,
            #         action_index=i
            #     )
            try:
                self.logger.info("grading index {} in the inventory".format(i))
                # self.logger.info(type(item))

                item_type = item.__class__.__name__
                # self.logger.info(item_type)

                if "Consumable" in item_type:
                    #idk do we equip the consumable before we fite the guy
                    #but also if its a health potion do it now
                    self.logger.info(
                        'index {} is a consumable, eating!'.format(i))
                    #actually drop consumables f**k em (no wee eat them right there)
                    return CharacterDecision(decision_type="EQUIP",
                                             action_position=None,
                                             action_index=i)
                    # continue
                self.logger.info(self.print_stats(item))
                stat_mod = item.get_stats()
                new_stats = 0
                try:
                    new_stats += stat_mod.get_flat_speed_change() * 0
                except:
                    new_stats += 0
                try:
                    new_stats += stat_mod.get_percent_speed_change() * 0
                except:
                    new_stats += 0
                try:
                    new_stats += stat_mod.get_flat_health_change() * .1
                except:
                    new_stats += 0
                try:
                    new_stats += stat_mod.get_percent_health_change() * 30
                except:
                    new_stats += 0
                try:
                    new_stats += stat_mod.get_flat_experience_change() * 10
                except:
                    new_stats += 0
                try:
                    new_stats += stat_mod.get_percent_experience_change() * 200
                except:
                    new_stats += 0
                try:
                    new_stats += stat_mod.get_flat_attack_change() * 10
                except:
                    new_stats += 0
                try:
                    new_stats += stat_mod.get_percent_attack_change() * 70
                except:
                    new_stats += 0
                try:
                    new_stats += stat_mod.get_flat_defense_change() * 2
                except:
                    new_stats += 0
                try:
                    new_stats += stat_mod.get_percent_defense_change() * 30
                except:
                    new_stats += 0
                try:
                    new_stats += stat_mod.get_flat_regen_per_turn() * 0
                except:
                    new_stats += 0
                self.logger.info("got stats for index {}".format(i))
                # new_stats = stat_mods.get_flat_speed_change() + stat_mods.get_percent_speed_change() + stat_mods.get_flat_health_change() + stat_mods.get_percent_health_change() + stat_mods.get_flat_defense_change() + stat_mods.get_flat_attack_change() + stat_mods.get_percent_attack_change()
                self.logger.info("stat check for index {} is {}".format(
                    i, new_stats))
                for typ in ["Clothes", "Hat", "Shoes", "Weapon", "Accessory"]:

                    if typ in item_type:
                        self.logger.info('index {} is a {}'.format(i, typ))
                        current_stats = self.stats[typ]
                        self.logger.info(
                            'old stats: {} , new stats: {}'.format(
                                current_stats, new_stats))
                        if new_stats > current_stats:
                            self.logger.info("equipping")
                            self.stats[typ] = new_stats
                            return CharacterDecision(decision_type="EQUIP",
                                                     action_position=None,
                                                     action_index=i)
                        else:
                            self.logger.info(
                                "this {} sucks, dropping it".format(typ))
                            return CharacterDecision(decision_type="DROP",
                                                     action_position=None,
                                                     action_index=i)
            except Exception as e:

                self.logger.error(e)
                return CharacterDecision(decision_type="DROP",
                                         action_position=None,
                                         action_index=0)

        # item pick up
        tile_items = self.board.get_tile_at(self.curr_pos).items
        if len(tile_items) > 0:
            self.memory.set_value("last_action", "PICKUP")
            self.logger.info("picking up item: {}".format(tile_items))
            try:
                for i in range(len(tile_items)):
                    self.logger.info("grading new item index {}".format(i))
                    if "Consumable" in tile_items[i].__class__.__name__:
                        return CharacterDecision(decision_type="PICKUP",
                                                 action_position=self.curr_pos,
                                                 action_index=i)
                    stat_mods = tile_items[i].get_stats()
                    stat_sum = stat_mods.get_flat_speed_change(
                    ) * 0 + stat_mods.get_percent_speed_change(
                    ) * 0 + stat_mods.get_flat_health_change(
                    ) * .1 + stat_mods.get_percent_health_change(
                    ) * 30 + stat_mods.get_flat_defense_change(
                    ) * 2 + stat_mods.get_flat_attack_change(
                    ) * 10 + stat_mods.get_percent_attack_change(
                    ) * 70 + stat_mods.get_percent_defense_change(
                    ) * 30 + stat_mods.get_flat_regen_per_turn(
                    ) * 0 + stat_mods.get_flat_experience_change(
                    ) * 10 + stat_mods.get_percent_experience_change() * 200
                    self.logger.info("new item stat: " + str(stat_sum))
                    self.logger.info(
                        "curr stat item: " +
                        str(self.stats[tile_items[i].__class__.__name__]))
                    if stat_sum > self.stats[tile_items[i].__class__.__name__]:
                        self.logger.info(
                            "picking up item at index {}".format(i))
                        return CharacterDecision(decision_type="PICKUP",
                                                 action_position=self.curr_pos,
                                                 action_index=i)
                    else:
                        self.logger.info(
                            "skipping index {}, shitty item".format(i))
            except Exception as e:
                self.logger.error(e)
                self.logger.info("picking up item at index 0")
                return CharacterDecision(decision_type="PICKUP",
                                         action_position=self.curr_pos,
                                         action_index=i)
        for d in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            target_pos = Position.create(self.curr_pos.x + d[0],
                                         self.curr_pos.y + d[1],
                                         self.curr_pos.get_board_id())
            tile_items = self.board.get_tile_at(target_pos).items
            if len(tile_items) > 0:
                for i in range(len(tile_items)):
                    self.logger.info("grading new item index {}".format(i))
                    if "Consumable" in tile_items[i].__class__.__name__:
                        self.memory.set_value("last_action", "MOVE")
                        self.logger.info("moving to item")
                        return CharacterDecision(decision_type="MOVE",
                                                 action_position=target_pos,
                                                 action_index=0)
                    stat_mods = tile_items[i].get_stats()
                    stat_sum = stat_mods.get_flat_speed_change(
                    ) * 0 + stat_mods.get_percent_speed_change(
                    ) * 0 + stat_mods.get_flat_health_change(
                    ) * .1 + stat_mods.get_percent_health_change(
                    ) * 30 + stat_mods.get_flat_defense_change(
                    ) * 2 + stat_mods.get_flat_attack_change(
                    ) * 10 + stat_mods.get_percent_attack_change(
                    ) * 70 + stat_mods.get_percent_defense_change(
                    ) * 30 + stat_mods.get_flat_regen_per_turn(
                    ) * 0 + stat_mods.get_flat_experience_change(
                    ) * 10 + stat_mods.get_percent_experience_change() * 200
                    self.logger.info("new item stat: " + str(stat_sum))
                    self.logger.info(
                        "curr stat item: " +
                        str(self.stats[tile_items[i].__class__.__name__]))
                    if stat_sum > self.stats[tile_items[i].__class__.__name__]:
                        self.memory.set_value("last_action", "MOVE")
                        self.logger.info("moving to item")
                        return CharacterDecision(decision_type="MOVE",
                                                 action_position=target_pos,
                                                 action_index=0)
                    else:
                        self.logger.info(
                            "skipping index {}, shitty item".format(i))

        ## Choose weakest monster
        weakestMonster = self.findWeakest(self.monsters, self.curr_pos)
        weapon = self.my_player.get_weapon()
        ## Check if weakest monster is in attack range
        if self.curr_pos.manhattan_distance(
                weakestMonster.position) <= weapon.get_range():
            self.logger.info("Attacking monster: " +
                             str(weakestMonster.get_name()) + " with health " +
                             str(weakestMonster.get_current_health()) + "/" +
                             str(weakestMonster.get_max_health()))
            return CharacterDecision(
                decision_type="ATTACK",
                action_position=weakestMonster.get_position(),
                action_index=0)
        ## Move to weakest monster!
        self.logger.info("Chosen weakest monster: " +
                         str(weakestMonster.get_name()) + " || location: (" +
                         str(weakestMonster.get_position().x) + "," +
                         str(weakestMonster.get_position().y) + ")")

        positionToMove = self.zhou_astar_path_to_move(
            self.my_player, weakestMonster.get_position())
        # hard code walk back
        if positionToMove[0] >= 6:
            positionToMove[0] = 4
        positionObjectToMove = self.curr_pos
        newPos = positionObjectToMove.create(positionToMove[0],
                                             positionToMove[1], self.board_id)
        self.logger.info("Location to move now: (" + str(newPos.x) + ", " +
                         str(newPos.y) + ")")
        return CharacterDecision(decision_type="MOVE",
                                 action_position=newPos,
                                 action_index=0)
Exemplo n.º 11
0
 def heuristic(self, start, goal):
     start_position = Position.create(start[0], start[1], self.board_id)
     end_position = Position.create(goal[0], goal[1], self.board_id)
     return start_position.manhattan_distance(end_position)
Exemplo n.º 12
0
 def equal_pos(self, pos1: Position, pos2: Position):
     return pos1.x == pos2.x and pos1.y == pos2.y and pos1.get_board_id(
     ) == pos2.get_board_id()
Exemplo n.º 13
0
 def get_position_str(self, pos: Position):
     return str(pos.get_board_id()) + " | " + str(pos.get_x()) + ", " + str(
         pos.get_y())