예제 #1
0
class HeatArea:
    def __init__(self, ai: sc2.BotAI, knowledge: 'Knowledge', x: int, y: int, x2: int, y2: int):
        self.ai = ai
        self.knowledge = knowledge
        self.cache: UnitCacheManager = self.knowledge.unit_cache
        self.bottom_left_x = x
        self.bottom_left_y = y
        self.top_right_x = x2
        self.top_right_y = y2
        self.center = Point2(((x + x2) / 2.0, (y + y2) / 2.0))
        self._x, self._y = self.center.rounded
        self.zone: Optional['Zone'] = None
        self.heat: float = 0
        self.stealth_heat: float = 0
        self.last_enemy_power = ExtendedPower(knowledge.unit_values)

        d2 = 15
        for zone in knowledge.expansion_zones:
            if zone.center_location.distance_to(self.center) < d2:
                if ai.get_terrain_height(zone.center_location) == ai.get_terrain_height(self.center):
                    # if zone == self.knowledge.own_main_zone:
                    #     print("MAIN ZONE")
                    self.zone = zone

    def update(self,  time_change: float):
        if self.stealth_heat > 0:
            self.stealth_heat = min(2, max(0, (self.stealth_heat - time_change) * (1 - time_change * 0.5)))

        if self.heat > 0:
            if self.is_visible():
                self.heat = max(0, (self.heat - time_change * 0.02) * (1 - time_change * 0.5))
            else:
                self.heat = max(0, (self.heat - time_change * 0.01) * (1 - time_change*0.25))

        self.heat += self.last_enemy_power.power * time_change
        self.last_enemy_power.clear()

    def is_visible(self) -> bool:
        # TODO: Check all corners?
        return self.ai.state.visibility.data_numpy[self._y, self._x] == 2
예제 #2
0
class GroupCombatManager(ManagerBase):
    def __init__(self):
        super().__init__()

    async def start(self, knowledge: 'Knowledge'):
        await super().start(knowledge)
        self.cache: UnitCacheManager = self.knowledge.unit_cache
        self.pather: PathingManager = self.knowledge.pathing_manager
        self.tags: List[int] = []

        self.unit_micros: Dict[UnitTypeId, MicroStep] = dict()
        self.all_enemy_power = ExtendedPower(self.unit_values)

        # Micro controllers / handlers
        self.unit_micros[UnitTypeId.DRONE] = MicroWorkers(knowledge)
        self.unit_micros[UnitTypeId.PROBE] = MicroWorkers(knowledge)
        self.unit_micros[UnitTypeId.SCV] = MicroWorkers(knowledge)

        # Protoss
        self.unit_micros[UnitTypeId.ARCHON] = NoMicro(knowledge)
        self.unit_micros[UnitTypeId.ADEPT] = MicroAdepts(knowledge)
        self.unit_micros[UnitTypeId.CARRIER] = MicroCarriers(knowledge)
        self.unit_micros[UnitTypeId.COLOSSUS] = MicroColossi(knowledge)
        self.unit_micros[UnitTypeId.DARKTEMPLAR] = MicroZerglings(knowledge)
        self.unit_micros[UnitTypeId.DISRUPTOR] = MicroDisruptor(knowledge)
        self.unit_micros[UnitTypeId.DISRUPTORPHASED] = MicroPurificationNova(
            knowledge)
        self.unit_micros[UnitTypeId.HIGHTEMPLAR] = MicroHighTemplars(knowledge)
        self.unit_micros[UnitTypeId.OBSERVER] = MicroObservers(knowledge)
        self.unit_micros[UnitTypeId.ORACLE] = MicroOracles(knowledge)
        self.unit_micros[UnitTypeId.PHOENIX] = MicroPhoenixes(knowledge)
        self.unit_micros[UnitTypeId.SENTRY] = MicroSentries(knowledge)
        self.unit_micros[UnitTypeId.STALKER] = MicroStalkers(knowledge)
        self.unit_micros[UnitTypeId.WARPPRISM] = MicroWarpPrism(knowledge)
        self.unit_micros[UnitTypeId.VOIDRAY] = MicroVoidrays(knowledge)
        self.unit_micros[UnitTypeId.ZEALOT] = MicroZealots(knowledge)

        # Zerg
        self.unit_micros[UnitTypeId.ZERGLING] = MicroZerglings(knowledge)
        self.unit_micros[UnitTypeId.ULTRALISK] = NoMicro(knowledge)
        self.unit_micros[UnitTypeId.OVERSEER] = MicroOverseers(knowledge)
        self.unit_micros[UnitTypeId.QUEEN] = MicroQueens(knowledge)

        # Terran
        self.unit_micros[UnitTypeId.HELLIONTANK] = NoMicro(knowledge)
        self.unit_micros[UnitTypeId.SIEGETANK] = MicroTanks(knowledge)
        self.unit_micros[UnitTypeId.VIKINGFIGHTER] = MicroVikings(knowledge)
        self.unit_micros[UnitTypeId.MARINE] = MicroBio(knowledge)
        self.unit_micros[UnitTypeId.MARAUDER] = MicroBio(knowledge)

        self.generic_micro = GenericMicro(knowledge)
        self.regroup_threshold = 0.75

    async def update(self):
        self.enemy_groups: List[CombatUnits] = self.group_enemy_units()
        self.all_enemy_power.clear()

        for group in self.enemy_groups:  # type: CombatUnits
            self.all_enemy_power.add_units(group.units)

    async def post_update(self):
        pass

    @property
    def debug(self):
        return self._debug and self.knowledge.debug

    def add_unit(self, unit: Unit):
        if unit.type_id in ignored:  # Just no
            return

        self.tags.append(unit.tag)

    def add_units(self, units: Units):
        for unit in units:
            self.add_unit(unit)

    def get_all_units(self) -> Units:
        units = Units([], self.ai)
        for tag in self.tags:
            unit = self.cache.by_tag(tag)
            if unit:
                units.append(unit)
        return units

    def execute(self, target: Point2, move_type=MoveType.Assault):
        our_units = self.get_all_units()
        if len(our_units) < 1:
            return

        self.own_groups: List[CombatUnits] = self.group_own_units(
            our_units, 12)

        total_power = ExtendedPower(self.unit_values)

        for group in self.own_groups:
            total_power.add_power(group.power)

        if self.debug:
            fn = lambda group: group.center.distance_to(self.ai.start_location)
            sorted_list = sorted(self.own_groups, key=fn)
            for i in range(0, len(sorted_list)):
                sorted_list[i].debug_index = i

        for group in self.own_groups:
            center = group.center
            closest_enemies = self.closest_group(center, self.enemy_groups)

            if closest_enemies is None:
                if move_type == MoveType.PanicRetreat:
                    self.move_to(group, target, move_type)
                else:
                    self.attack_to(group, target, move_type)
            else:
                power = group.power
                enemy_power = ExtendedPower(closest_enemies)
                enemy_center = closest_enemies.center

                is_in_combat = group.is_in_combat(closest_enemies)
                # pseudocode for attack

                if move_type == MoveType.DefensiveRetreat or move_type == MoveType.PanicRetreat:
                    self.move_to(group, target, move_type)
                    break

                if power.power > self.regroup_threshold * total_power.power:
                    # Most of the army is here
                    if (group.is_too_spread_out() and not is_in_combat):
                        self.regroup(group, group.center)
                    else:
                        self.attack_to(group, target, move_type)

                elif is_in_combat:
                    if not power.is_enough_for(enemy_power, 0.75):
                        # Regroup if possible
                        own_closest_group = self.closest_group(
                            center, self.own_groups)

                        if own_closest_group:
                            self.move_to(group, own_closest_group.center,
                                         MoveType.ReGroup)
                        else:
                            # fight to bitter end
                            self.attack_to(group, closest_enemies.center,
                                           move_type)
                    else:
                        self.attack_to(group, closest_enemies.center,
                                       move_type)
                else:
                    if group.power.is_enough_for(self.all_enemy_power, 0.85):
                        # We have enough units here to crush everything the enemy has
                        self.attack_to(group, closest_enemies.center,
                                       move_type)
                    else:
                        # Regroup if possible
                        if move_type == MoveType.Assault:
                            # Group towards attack target
                            own_closest_group = self.closest_group(
                                target, self.own_groups)
                        else:
                            # Group up with closest group
                            own_closest_group = self.closest_group(
                                center, self.own_groups)

                        if own_closest_group:
                            self.move_to(group, own_closest_group.center,
                                         MoveType.ReGroup)
                        else:
                            # fight to bitter end
                            self.attack_to(group, closest_enemies.center,
                                           move_type)

                # if move_type == MoveType.SearchAndDestroy:
                #     enemy_closest_group = self.closest_group(center, self.enemy_groups)
                #     if enemy_closest_group:
                #         self.attack_to(actions, group, closest_enemies.center, move_type)
                #     else:
                #         self.attack_to(actions, group, target, move_type)
                # elif power.is_enough_for(enemy_power, 0.75):
                #     if move_type == MoveType.PanicRetreat:
                #         self.move_to(actions, group, target, move_type)
                #     else:
                #         self.attack_to(actions, group, closest_enemies.center, move_type)
                #
                # elif distance > 12:
                #     own_closest_group = self.closest_group(center, self.own_groups)
                #
                #     if own_closest_group:
                #         self.move_to(actions, group, own_closest_group.center, MoveType.ReGroup)
                #
                #     elif move_type == MoveType.PanicRetreat:
                #         self.move_to(actions, group, target, move_type)
                #     else:
                #         self.attack_to(actions, group, enemy_center, move_type)
                #
                # elif enemy_power.is_enough_for(power, 0.75):
                #     own_closest_group = self.closest_group(center, self.own_groups)
                #
                #     if own_closest_group:
                #         self.move_to(actions, group, own_closest_group.center, MoveType.ReGroup)
                #
                # elif move_type == MoveType.PanicRetreat:
                #     self.move_to(actions, group, target, move_type)
                # else:
                #     self.attack_to(actions, group, target, move_type)

        self.tags.clear()

    def regroup(self, group: CombatUnits, target: Union[Unit, Point2]):
        if isinstance(target, Unit):
            target = self.pather.find_path(group.center, target.position, 1)
        else:
            target = self.pather.find_path(group.center, target, 3)
        self.move_to(group, target, MoveType.Push)

    def move_to(self, group: CombatUnits, target, move_type: MoveType):
        self.action_to(group, target, move_type, False)

    def attack_to(self, group: CombatUnits, target, move_type: MoveType):
        self.action_to(group, target, move_type, True)

    def action_to(self, group: CombatUnits, target, move_type: MoveType,
                  is_attack: bool):
        if isinstance(target, Point2) and group.ground_units:
            if move_type in {MoveType.DefensiveRetreat, MoveType.PanicRetreat}:
                target = self.pather.find_influence_ground_path(
                    group.center, target, 14)
            else:
                target = self.pather.find_path(group.center, target, 14)

        own_unit_cache: Dict[UnitTypeId, Units] = {}

        for unit in group.units:
            units = own_unit_cache.get(unit.type_id, Units([], self.ai))
            if units.amount == 0:
                real_type = self.unit_values.real_type(unit.type_id)
                own_unit_cache[real_type] = units

            units.append(unit)

        for type_id, type_units in own_unit_cache.items():
            micro: MicroStep = self.unit_micros.get(type_id,
                                                    self.generic_micro)
            micro.init_group(group, type_units, self.enemy_groups, move_type)
            group_action = micro.group_solve_combat(type_units,
                                                    Action(target, is_attack))

            for unit in type_units:
                action = micro.unit_solve_combat(unit, group_action)
                order = action.to_commmand(unit)
                if order:
                    self.ai.do(order)

                if self.debug:
                    if action.debug_comment:
                        status = action.debug_comment
                    elif action.ability:
                        status = action.ability.name
                    elif action.is_attack:
                        status = "Attack"
                    else:
                        status = "Move"
                    if action.target is not None:
                        if isinstance(action.target, Unit):
                            status += f": {action.target.type_id.name}"
                        else:
                            status += f": {action.target}"

                    status += f" G: {group.debug_index}"
                    status += f"\n{move_type.name}"

                    pos3d: Point3 = unit.position3d
                    pos3d = Point3((pos3d.x, pos3d.y, pos3d.z + 2))
                    self.ai._client.debug_text_world(status, pos3d, size=10)

    def closest_group(
            self, start: Point2,
            combat_groups: List[CombatUnits]) -> Optional[CombatUnits]:
        group = None
        best_distance = 50  # doesn't find enemy groups closer than this

        for combat_group in combat_groups:
            center = combat_group.center

            if center == start:
                continue  # it's the same group!

            distance = start.distance_to(center)
            if distance < best_distance:
                best_distance = distance
                group = combat_group

        return group

    def group_own_units(self,
                        our_units: Units,
                        lookup_distance: float = 7) -> List[CombatUnits]:
        groups: List[Units] = []
        assigned: Dict[int, int] = dict()

        for unit in our_units:
            if unit.tag in assigned:
                continue

            units = Units([unit], self.ai)
            index = len(groups)

            assigned[unit.tag] = index

            groups.append(units)
            self.include_own_units(unit, units, lookup_distance, index,
                                   assigned)

        return [CombatUnits(u, self.knowledge) for u in groups]

    def include_own_units(self, unit: Unit, units: Units,
                          lookup_distance: float, index: int,
                          assigned: Dict[int, int]):
        units_close_by = self.cache.own_in_range(unit.position,
                                                 lookup_distance)

        for unit_close in units_close_by:
            if unit_close.tag in assigned or unit_close.tag not in self.tags:
                continue

            assigned[unit_close.tag] = index
            units.append(unit_close)
            self.include_own_units(unit_close, units, lookup_distance, index,
                                   assigned)

    def group_enemy_units(self) -> List[CombatUnits]:
        groups: List[Units] = []
        assigned: Dict[int, int] = dict()
        lookup_distance = 7

        for unit in self.knowledge.known_enemy_units_mobile:
            if unit.tag in assigned or unit.type_id in self.unit_values.combat_ignore or not unit.can_be_attacked:
                continue

            units = Units([unit], self.ai)
            index = len(groups)

            assigned[unit.tag] = index

            groups.append(units)
            self.include_enemy_units(unit, units, lookup_distance, index,
                                     assigned)

        return [CombatUnits(u, self.knowledge) for u in groups]

    def include_enemy_units(self, unit: Unit, units: Units,
                            lookup_distance: float, index: int,
                            assigned: Dict[int, int]):
        units_close_by = self.cache.enemy_in_range(unit.position,
                                                   lookup_distance)

        for unit_close in units_close_by:
            if unit_close.tag in assigned or unit_close.tag not in self.tags or not unit.can_be_attacked:
                continue

            assigned[unit_close.tag] = index
            units.append(unit_close)
            self.include_enemy_units(unit_close, units, lookup_distance, index,
                                     assigned)
예제 #3
0
class MicroStep(ABC):
    def __init__(self, knowledge):
        self.knowledge: 'Knowledge' = knowledge
        self.ai: sc2.BotAI = knowledge.ai
        self.unit_values: UnitValue = knowledge.unit_values
        self.cd_manager: CooldownManager = knowledge.cooldown_manager
        self.pather: PathingManager = knowledge.pathing_manager
        self.cache: UnitCacheManager = knowledge.unit_cache
        self.delay_to_shoot = self.ai._client.game_step + 1.5
        self.enemy_groups: List[CombatUnits] = []
        self.ready_to_attack_ratio: float = 0.0
        self.center: Point2 = Point2((0, 0))
        self.group: CombatUnits
        self.engage_ratio = 0
        self.can_engage_ratio = 0
        self.closest_group: CombatUnits
        self.engaged: Dict[int, List[int]] = dict()
        self.engaged_power = ExtendedPower(knowledge.unit_values)
        self.our_power = ExtendedPower(knowledge.unit_values)
        self.closest_units: Dict[int, Optional[Unit]] = dict()
        self.move_type = MoveType.Assault

    def init_group(self, group: CombatUnits, units: Units, enemy_groups: List[CombatUnits], move_type: MoveType):
        self.group = group
        self.move_type = move_type
        ready_to_attack = 0

        self.our_power = group.power
        self.closest_units.clear()
        self.engaged_power.clear()

        self.closest_group = None
        self.closest_group_distance = 1000000
        for enemy_group in enemy_groups:
            d = enemy_group.center.distance_to(group.center)
            if d < self.closest_group_distance:
                self.closest_group_distance = d
                self.closest_group = enemy_group

        self.enemy_groups = enemy_groups
        self.center = units.center
        self.enemies_near_by: Units = self.knowledge.unit_cache.enemy_in_range(self.center, 15 + len(group.units) * 0.1)

        self.engaged_power.add_units(self.enemies_near_by)

        engage_count = 0
        can_engage_count = 0
        for unit in units:
            closest_distance = 1000
            if self.ready_to_shoot(unit):
                ready_to_attack += 1

            engage_added = False
            can_engage_added = False
            for enemy_near in self.enemies_near_by:  # type: Unit
                d = enemy_near.distance_to(unit)
                if d < closest_distance:
                    self.closest_units[unit.tag] = enemy_near
                    closest_distance = d

                if not engage_added and d < self.unit_values.real_range(enemy_near, unit, self.knowledge):
                    engage_count += 1
                    engage_added = True

                if not can_engage_added and d < self.unit_values.real_range(unit, enemy_near, self.knowledge):
                    can_engage_count += 1
                    can_engage_added = True

        self.ready_to_attack_ratio = ready_to_attack / len(units)
        self.engage_ratio = engage_count / len(units)
        self.can_engage_ratio = can_engage_count / len(units)

    def ready_to_shoot(self, unit: Unit) -> bool:
        if unit.type_id == UnitTypeId.CYCLONE:
            # if knowledge.cooldown_manager.is_ready(self.unit.tag, AbilityId.LOCKON_LOCKON):
            #     self.ready_to_shoot = True
            #     return
            if self.cd_manager.is_ready(unit.tag, AbilityId.CANCEL_LOCKON):
                return False


        if unit.type_id == UnitTypeId.DISRUPTOR:
            return self.cd_manager.is_ready(unit.tag, AbilityId.EFFECT_PURIFICATIONNOVA)

        if unit.type_id == UnitTypeId.ORACLE:
            tick = self.ai.state.game_loop % 16
            return tick < 8

        if unit.type_id == UnitTypeId.CARRIER:
            tick = self.ai.state.game_loop % 32
            return tick < 8

        return unit.weapon_cooldown <= self.delay_to_shoot

    @abstractmethod
    def group_solve_combat(self, units: Units, current_command: Action) -> Action:
        pass

    @abstractmethod
    def unit_solve_combat(self, unit: Unit, current_command: Action) -> Action:
        pass


    def focus_fire(self, unit: Unit, current_command: Action, prio: Optional[Dict[UnitTypeId, int]]) -> Action:
        shoot_air = self.unit_values.can_shoot_air(unit)
        shoot_ground = self.unit_values.can_shoot_ground(unit, self.knowledge)

        air_range = self.unit_values.air_range(unit)
        ground_range = self.unit_values.ground_range(unit, self.knowledge)
        lookup = min(air_range + 3, ground_range + 3)
        enemies = self.cache.enemy_in_range(unit.position, lookup)

        last_target = self.last_targeted(unit)

        if not enemies:
            # No enemies to shoot at
            return current_command

        value_func: Callable[[Unit],  float] = None
        if prio:
            value_func = lambda u: 1 if u.type_id in changelings else prio.get(u.type_id, -1) \
                  * (1 - u.shield_health_percentage)
        else:
            value_func = lambda u: 1 if u.type_id in changelings else 2 \
                  * self.unit_values.power_by_type(u.type_id, 1 - u.shield_health_percentage)


        best_target: Optional[Unit] = None
        best_score: float = 0
        for enemy in enemies:  # type: Unit
            if not enemy.is_target:
                continue

            if not shoot_air and enemy.is_flying:
                continue

            if not shoot_ground and not enemy.is_flying:
                continue

            pos: Point2 = enemy.position
            score = value_func(enemy) + (1 - pos.distance_to(unit) / lookup)
            if enemy.tag == last_target:
                score += 3

            if score > best_score:
                best_target = enemy
                best_score = score

        if best_target:
            return Action(best_target, True)

        return current_command

    def melee_focus_fire(self, unit: Unit, current_command: Action) -> Action:
        ground_range = self.unit_values.ground_range(unit, self.knowledge)
        lookup = ground_range + 3
        enemies = self.cache.enemy_in_range(unit.position, lookup)

        last_target = self.last_targeted(unit)

        if not enemies:
            # No enemies to shoot at
            return current_command

        def melee_value(u: Unit):
            val = 1 - u.shield_health_percentage
            range = self.unit_values.real_range(unit, u, self.knowledge)
            if unit.distance_to(u) < range:
                val += 1
            return val

        value_func = melee_value
        close_enemies = self.cache.enemy_in_range(unit.position, lookup)

        best_target: Optional[Unit] = None
        best_score: float = 0

        for enemy in close_enemies:  # type: Unit
            if enemy.is_flying:
                continue

            pos: Point2 = enemy.position
            score = value_func(enemy) + (1 - pos.distance_to(unit) / lookup)
            if enemy.tag == last_target:
                score += 3

            if score > best_score:
                best_target = enemy
                best_score = score

        if best_target:
            return Action(best_target, True)

        return current_command

    def last_targeted(self, unit: Unit) -> Optional[int]:
        if unit.orders:
            # action: UnitCommand
            # current_action: UnitOrder
            current_action = unit.orders[0]
            # targeting unit
            if isinstance(current_action.target, int):
                # tag found
                return current_action.target
        return None

    def is_locked_on(self, unit: Unit) -> bool:
        if unit.has_buff(BuffId.LOCKON):
            return True
        return False
예제 #4
0
class GameAnalyzer(ManagerBase):
    def __init__(self):
        super().__init__()
        self._enemy_air_percentage = 0
        self._our_income_advantage = 0
        self._our_predicted_army_advantage = 0
        self._our_predicted_tech_advantage = 0
        self.enemy_gas_income = 0
        self.enemy_mineral_income = 0
        self.our_zones = 0
        self.enemy_zones = 0
        self.our_power: ExtendedPower = None
        self.enemy_power: ExtendedPower = None
        self.enemy_predict_power: ExtendedPower = None
        self.predicted_defeat_time = 0.0
        self.minerals_left: List[int] = []
        self.vespene_left: List[int] = []
        self.resource_updater: IntervalFunc = None

        self._last_income: Advantage = Advantage.Even
        self._last_army: Advantage = Advantage.Even
        self._last_predict: Advantage = Advantage.Even

    async def start(self, knowledge: 'Knowledge'):
        await super().start(knowledge)
        self.enemy_predicter: EnemyArmyPredicter = knowledge.enemy_army_predicter
        self.our_power = ExtendedPower(self.unit_values)
        self.enemy_power: ExtendedPower = ExtendedPower(self.unit_values)
        self.enemy_predict_power: ExtendedPower = ExtendedPower(
            self.unit_values)
        self.resource_updater = IntervalFunc(self.ai,
                                             self.save_resources_status, 1)
        self.resource_updater.execute()

    def save_resources_status(self):
        self.minerals_left.append(self.ai.minerals)
        self.vespene_left.append(self.ai.vespene)

    async def update(self):
        self.resource_updater.execute()

        self.our_power.clear()
        self.our_zones = 0
        self.enemy_zones = 0
        our_income = self.knowledge.income_calculator.mineral_income + self.knowledge.income_calculator.gas_income

        if self.knowledge.my_worker_type is None:
            # random and we haven't seen enemy race yat
            enemy_workers = 12
        else:
            enemy_workers = self.knowledge.enemy_units_manager.enemy_worker_count
            if not self.knowledge.enemy_main_zone.is_scouted_at_least_once:
                enemy_workers += 12

        mineral_fields = 0
        for zone in self.knowledge.zone_manager.expansion_zones:  # type: Zone
            if zone.is_enemys:
                self.enemy_zones += 1
                mineral_fields += len(zone.mineral_fields)
            if zone.is_ours:
                self.our_zones += 1

        built_vespene = len(self.cache.enemy(self.unit_values.gas_miners))
        self._enemy_gas_income = min(enemy_workers,
                                     built_vespene * 3) * GAS_MINE_RATE
        workers_on_minerals = min(mineral_fields * 2,
                                  enemy_workers - built_vespene * 3)
        workers_on_minerals = max(0, workers_on_minerals)
        self.enemy_mineral_income = workers_on_minerals

        enemy_income = self.enemy_mineral_income + self._enemy_gas_income
        self._our_income_advantage = our_income - enemy_income
        self.our_power.add_units(
            self.ai.units.filter(lambda u: u.is_ready and u.type_id != self.
                                 knowledge.my_worker_type))

        self.enemy_predict_power = self.enemy_predicter.predicted_enemy_power
        self.enemy_power = self.enemy_predicter.enemy_power

        self._enemy_air_percentage = 0
        if self.enemy_predict_power.air_presence > 0:
            self._enemy_air_percentage = self.enemy_predict_power.air_power / self.enemy_predict_power.power

        being_defeated = self.predicting_defeat
        if being_defeated and self.predicted_defeat_time == 0.0:
            self.predicted_defeat_time = self.ai.time
        elif not being_defeated and self.predicted_defeat_time != 0.0:
            self.predicted_defeat_time = 0

        income = self._calc_our_income_advantage()
        army = self._calc_our_army_advantage()
        predict = self._calc_our_army_predict()

        if self._last_income != income:
            self.print(f'Income advantage is now {income.name}')

        if self._last_army != army:
            self.print(f'Known army advantage is now {army.name}')

        if self._last_predict != predict:
            self.print(f'Predicted army advantage is now {predict.name}')

        self._last_income = income
        self._last_army = army
        self._last_predict = predict

    async def post_update(self):
        if self.debug:
            msg = f"Our income: {self.knowledge.income_calculator.mineral_income} / {round(self.knowledge.income_calculator.gas_income)}"
            msg += f"\nEnemy income: {self.enemy_mineral_income} / {round(self.enemy_gas_income)}"
            msg += f"\nResources: {round(self._our_income_advantage)}+{self.our_zones - self.enemy_zones}" \
                f" ({self.our_income_advantage.name})"
            msg += f"\nArmy: {round(self.our_power.power)} vs" \
                f" {round(self.enemy_power.power)} ({self.our_army_advantage.name})"
            msg += f"\nArmy predict: {round(self.our_power.power)} vs" \
                f" {round(self.enemy_predict_power.power)} ({self.our_army_predict.name})"
            msg += f"\nEnemy air: {self.enemy_air.name}"
            self.client.debug_text_2d(msg, Point2((0.4, 0.15)), None, 14)

    @property
    def our_income_advantage(self) -> Advantage:
        return self._last_income

    def _calc_our_income_advantage(self) -> Advantage:
        number = self._our_income_advantage + (self.our_zones -
                                               self.enemy_zones) * 10

        if number > 40:
            return Advantage.OverwhelmingAdvantage
        if number < -40:
            return Advantage.OverwhelmingDisadvantage

        if number > 20:
            return Advantage.ClearAdvantage
        if number < -20:
            return Advantage.ClearDisadvantage

        if number > 10:
            return Advantage.SmallAdvantage
        if number < -10:
            return Advantage.SmallDisadvantage

        if number > 5:
            return Advantage.SlightAdvantage
        if number < -5:
            return Advantage.SlightDisadvantage

        return Advantage.Even

    @property
    def army_at_least_clear_disadvantage(self) -> bool:
        return self.our_army_predict in at_least_clear_disadvantage

    @property
    def army_at_least_small_disadvantage(self) -> bool:
        return self.our_army_predict in at_least_small_disadvantage

    @property
    def army_at_least_clear_advantage(self) -> bool:
        return self.our_army_predict in at_least_clear_advantage

    @property
    def army_at_least_small_advantage(self) -> bool:
        return self.our_army_predict in at_least_small_advantage

    @property
    def army_at_least_advantage(self) -> bool:
        return self.our_army_predict in at_least_advantage

    @property
    def army_can_survive(self) -> bool:
        return self.our_army_predict not in at_least_small_disadvantage

    @property
    def predicting_victory(self) -> bool:
        return (self.our_army_predict == Advantage.OverwhelmingAdvantage and
                self.our_income_advantage == Advantage.OverwhelmingAdvantage)

    @property
    def bean_predicting_defeat_for(self) -> float:
        if self.predicted_defeat_time == 0:
            return 0
        return self.ai.time - self.predicted_defeat_time

    @property
    def predicting_defeat(self) -> bool:
        return (self.our_army_predict == Advantage.OverwhelmingDisadvantage
                and (self.ai.supply_workers < 5 or self.our_income_advantage
                     == Advantage.OverwhelmingDisadvantage))

    @property
    def our_army_predict(self) -> Advantage:
        return self._last_predict

    def _calc_our_army_predict(self) -> Advantage:
        if self.our_power.is_enough_for(self.enemy_predict_power,
                                        our_percentage=1 / 1.1):
            if self.our_power.power > 20 and self.our_power.is_enough_for(
                    self.enemy_predict_power, our_percentage=1 / 3):
                return Advantage.OverwhelmingAdvantage
            if self.our_power.power > 10 and self.our_power.is_enough_for(
                    self.enemy_predict_power, our_percentage=1 / 2):
                return Advantage.ClearAdvantage
            if self.our_power.power > 5 and self.our_power.is_enough_for(
                    self.enemy_predict_power, our_percentage=1 / 1.4):
                return Advantage.SmallAdvantage
            return Advantage.SlightAdvantage

        if self.enemy_predict_power.is_enough_for(self.our_power,
                                                  our_percentage=1 / 1.1):
            if self.enemy_predict_power.power > 20 and self.enemy_predict_power.is_enough_for(
                    self.our_power, our_percentage=1 / 3):
                return Advantage.OverwhelmingDisadvantage
            if self.enemy_predict_power.power > 10 and self.enemy_predict_power.is_enough_for(
                    self.our_power, our_percentage=1 / 2):
                return Advantage.ClearDisadvantage
            if self.enemy_predict_power.power > 5 and self.enemy_predict_power.is_enough_for(
                    self.our_power, our_percentage=1 / 1.4):
                return Advantage.SmallDisadvantage
            return Advantage.SlightDisadvantage
        return Advantage.Even

    @property
    def our_army_advantage(self) -> Advantage:
        return self._last_army

    def _calc_our_army_advantage(self) -> Advantage:
        if self.our_power.is_enough_for(self.enemy_power,
                                        our_percentage=1 / 1.1):
            if self.our_power.power > 20 and self.our_power.is_enough_for(
                    self.enemy_power, our_percentage=1 / 3):
                return Advantage.OverwhelmingAdvantage
            if self.our_power.power > 10 and self.our_power.is_enough_for(
                    self.enemy_power, our_percentage=1 / 2):
                return Advantage.ClearAdvantage
            if self.our_power.power > 5 and self.our_power.is_enough_for(
                    self.enemy_power, our_percentage=1 / 1.4):
                return Advantage.SmallAdvantage
            return Advantage.SlightAdvantage

        if self.enemy_power.is_enough_for(self.our_power,
                                          our_percentage=1 / 1.1):
            if self.enemy_power.power > 20 and self.enemy_power.is_enough_for(
                    self.our_power, our_percentage=1 / 3):
                return Advantage.OverwhelmingDisadvantage
            if self.enemy_power.power > 10 and self.enemy_power.is_enough_for(
                    self.our_power, our_percentage=1 / 2):
                return Advantage.ClearDisadvantage
            if self.enemy_power.power > 5 and self.enemy_power.is_enough_for(
                    self.our_power, our_percentage=1 / 1.4):
                return Advantage.SmallDisadvantage
            return Advantage.SlightDisadvantage
        return Advantage.Even

    @property
    def enemy_air(self) -> AirArmy:
        if self._enemy_air_percentage > 0.90:
            return AirArmy.AllAir
        if self._enemy_air_percentage > 0.65:
            return AirArmy.AlmostAllAir
        if self._enemy_air_percentage > 0.35:
            return AirArmy.Mixed
        if self._enemy_air_percentage > 0:
            return AirArmy.SomeAir
        return AirArmy.NoAir

    async def on_end(self, game_result: Result):
        own_types: List[UnitTypeId] = []
        own_types_left: Dict[UnitTypeId, int] = {}
        enemy_types: List[UnitTypeId] = []
        enemy_types_left: Dict[UnitTypeId, int] = {}

        lost_data = self.knowledge.lost_units_manager.get_own_enemy_lost_units(
        )
        own_lost: Dict[UnitTypeId, List[Unit]] = lost_data[0]
        enemy_lost: Dict[UnitTypeId, List[Unit]] = lost_data[1]

        for unit_type, units in self.cache.own_unit_cache.items(
        ):  # type: (UnitTypeId, Units)
            type_id = self.unit_values.real_type(unit_type)
            if type_id not in own_types:
                own_types.append(type_id)
            val = own_types_left.get(type_id, 0)
            own_types_left[type_id] = val + units.amount

        for unit_count in self.knowledge.enemy_units_manager.enemy_composition:  # type: UnitCount
            unit_type = unit_count.enemy_type
            if unit_type not in enemy_types:
                enemy_types.append(unit_type)
            val = enemy_types_left.get(unit_type, 0)
            enemy_types_left[unit_type] = val + unit_count.count

        for unit_type, units in own_lost.items(
        ):  # type: (UnitTypeId, List[Unit])
            if unit_type not in own_types:
                own_types.append(unit_type)

        for unit_type, units in enemy_lost.items(
        ):  # type: (UnitTypeId, List[Unit])
            if unit_type not in enemy_types:
                enemy_types.append(unit_type)

        self.print_end("Own units:")
        self._print_by_type(own_types, own_lost, own_types_left)
        self.print_end("Enemy units:")
        self._print_by_type(enemy_types, enemy_lost, enemy_types_left)

        maxed_minerals = max(self.minerals_left)
        avg_minerals = sum(self.minerals_left) / len(self.minerals_left)

        maxed_gas = max(self.vespene_left)
        avg_gas = sum(self.vespene_left) / len(self.vespene_left)
        self.print_end(
            f'Minerals max {maxed_minerals} Average {round(avg_minerals)}')
        self.print_end(f'Vespene max {maxed_gas} Average {round(avg_gas)}')

    def _print_by_type(self, types: List[UnitTypeId],
                       lost_units: Dict[UnitTypeId, List[Unit]],
                       left_units: Dict[UnitTypeId, int]):
        def get_counts(unit_type: UnitTypeId) -> tuple:
            dead = len(lost_units.get(unit_type, []))
            alive = left_units.get(unit_type, 0)
            total = dead + alive
            return total, alive, dead

        # Sort types by total count
        types = sorted(types, key=lambda t: get_counts(t)[0], reverse=True)

        for unit_type in types:
            counts = get_counts(unit_type)
            self.print_end(f"{str(unit_type.name).ljust(17)} "
                           f"total: {str(counts[0]).rjust(3)} "
                           f"alive: {str(counts[1]).rjust(3)} "
                           f"dead: {str(counts[2]).rjust(3)} ")

    def print_end(self, msg: str):
        self.knowledge.print(msg, "GameAnalyzerEnd", stats=False)
예제 #5
0
    async def update_influence(self):
        power = ExtendedPower(self.unit_values)
        self.path_finder_terrain.reset()  # Reset
        self.path_finder_ground.reset()  # Reset

        positions = []
        for mf in self.ai.mineral_field:  # type: Unit
            # In 4.8.5+ minerals are no linger visible in pathing grid
            positions.append(mf.position)

        # for mf in self.ai.mineral_walls:  # type: Unit
        #     # In 4.8.5+ minerals are no linger visible in pathing grid
        #     positions.append(mf.position)

        self.path_finder_terrain.create_block(positions, (2, 1))
        self.path_finder_ground.create_block(positions, (2, 1))

        self.set_rocks(self.path_finder_terrain)
        self.set_rocks(self.path_finder_ground)

        for building in self.ai.structures + self.knowledge.known_enemy_structures:  # type: Unit
            if building.type_id in buildings_2x2:
                self.path_finder_ground.create_block(building.position, (2, 2))
            elif building.type_id in buildings_3x3:
                self.path_finder_ground.create_block(building.position, (3, 3))
            elif building.type_id in buildings_5x5:
                self.path_finder_ground.create_block(building.position, (5, 3))
                self.path_finder_ground.create_block(building.position, (3, 5))

        self.set_rocks(self.path_finder_ground)

        self.path_finder_ground.normalize_influence(20)
        self.path_finder_air.normalize_influence(20)

        for enemy_type in self.cache.enemy_unit_cache:  # type: UnitTypeId
            enemies: Units = self.cache.enemy_unit_cache.get(
                enemy_type, Units([], self.ai))
            if len(enemies) == 0:
                continue

            example_enemy: Unit = enemies[0]
            power.clear()
            power.add_unit(enemy_type, 100)

            if self.unit_values.can_shoot_air(example_enemy):
                positions: List[Point2] = map(
                    lambda u: u.position,
                    enemies)  # need to be specified in both places
                s_range = self.unit_values.air_range(example_enemy)

                if example_enemy.type_id == UnitTypeId.CYCLONE:
                    s_range = 7

                self.path_finder_air.add_influence(positions, power.air_power,
                                                   s_range + 3)

            if self.unit_values.can_shoot_ground(example_enemy,
                                                 self.knowledge):
                positions = map(lambda u: u.position,
                                enemies)  # need to be specified in both places
                s_range = self.unit_values.ground_range(
                    example_enemy, self.knowledge)
                if example_enemy.type_id == UnitTypeId.CYCLONE:
                    s_range = 7

                if s_range < 2:
                    self.path_finder_ground.add_influence_walk(
                        positions, power.ground_power, 7)
                elif s_range < 5:
                    self.path_finder_ground.add_influence_walk(
                        positions, power.ground_power, 7)
                else:
                    self.path_finder_ground.add_influence(
                        positions, power.ground_power, s_range + 3)

        # influence, radius, points, can it hit air?
        effect_dict: Dict[EffectId, Tuple[float, float, List[Point2],
                                          bool]] = dict()
        for effect in self.ai.state.effects:
            values: Tuple[float, float, List[Point2], bool] = None

            if effect.id == EffectId.RAVAGERCORROSIVEBILECP:
                values = effect_dict.get(effect.id, (1000, 2.5, [], True))
                values[2].append(Point2.center(effect.positions))
            elif effect.id == EffectId.BLINDINGCLOUDCP:
                values = effect_dict.get(effect.id, (400, 3.5, [], False))
                values[2].append(Point2.center(effect.positions))
            elif effect.id == EffectId.NUKEPERSISTENT:
                values = effect_dict.get(effect.id, (900, 9, [], True))
                values[2].append(Point2.center(effect.positions))
            elif effect.id == EffectId.PSISTORMPERSISTENT:
                values = effect_dict.get(effect.id, (300, 3.5, [], True))
                values[2].append(Point2.center(effect.positions))
            elif effect.id == EffectId.LIBERATORTARGETMORPHDELAYPERSISTENT:
                values = effect_dict.get(effect.id, (200, 6, [], False))
                values[2].append(Point2.center(effect.positions))
            elif effect.id == EffectId.LIBERATORTARGETMORPHPERSISTENT:
                values = effect_dict.get(effect.id, (300, 6, [], False))
                values[2].append(Point2.center(effect.positions))
            elif effect.id == EffectId.LURKERMP:
                # Each lurker spine deals splash damage to a radius of 0.5
                values = effect_dict.get(effect.id, (1000, 1, [], False))
                values[2].extend(effect.positions)

            if values is not None and effect.id not in effect_dict:
                effect_dict[effect.id] = values

        for effects in effect_dict.values():
            if effects[3]:
                self.path_finder_air.add_influence(effects[2], effects[0],
                                                   effects[1])
            self.path_finder_ground.add_influence(effects[2], effects[0],
                                                  effects[1])
class EnemyArmyPredicter(ManagerBase):
    def __init__(self):
        super().__init__()

    async def start(self, knowledge: 'Knowledge'):
        await super().start(knowledge)
        self.enemy_units_manager: EnemyUnitsManager = self.knowledge.enemy_units_manager

        self.lost_units_manager: LostUnitsManager = knowledge.lost_units_manager
        self.unit_values: 'UnitValue' = knowledge.unit_values

        self.updater = IntervalFuncAsync(self.ai, self._real_update, INTERVAL)

        self.enemy_base_value_minerals = 400 + 12 * 50 + 50
        self.enemy_known_worker_count = 12

        self.mineral_dict: Dict['Zone', int] = {}

        # Last time minerals were updated
        self.mineral_updated_dict: Dict['Zone', float] = {}
        self.gas_dict: Dict[Point2, int] = {}

        for zone in knowledge.expansion_zones:
            minerals = 0
            if zone.last_minerals is not None:
                minerals = zone.last_minerals

            self.mineral_dict[zone] = minerals

        for geyser in self.ai.vespene_geyser:  # type: Unit
            self.gas_dict[geyser.position] = 2250

        self.enemy_mined_minerals = 0
        self.enemy_mined_minerals_prediction = 0
        self.enemy_mined_gas = 0

        self.enemy_army_known_minerals = 0
        self.enemy_army_known_gas = 0

        self.own_army_value_minerals = 0
        self.own_army_value_gas = 0

        self.predicted_enemy_free_minerals = 0
        self.predicted_enemy_free_gas = 0

        self.predicted_enemy_army_minerals = 0
        self.predicted_enemy_army_gas = 0

        self.predicted_enemy_composition: List[UnitCount] = []

        self.enemy_power = ExtendedPower(self.unit_values)
        self.predicted_enemy_power = ExtendedPower(self.unit_values)

    @property
    def own_value(self):
        """ Our exact army value that we know of """
        return self.own_army_value_minerals + self.own_army_value_gas

    @property
    def enemy_value(self):
        """ Best estimation on how big value enemy army has """
        return self.predicted_enemy_army_minerals + self.predicted_enemy_army_gas

    async def update(self):
        await self.updater.execute()

    async def _real_update(self):
        await self.update_own_army_value()

        self.enemy_power.clear()
        self.predicted_enemy_power.clear()

        self.predicted_enemy_composition.clear()
        gas_miners = self.knowledge.known_enemy_structures.of_type([
            UnitTypeId.ASSIMILATOR, UnitTypeId.EXTRACTOR, UnitTypeId.REFINERY
        ])

        minerals_used: int = 0
        gas_used: int = 0
        enemy_composition = self.enemy_units_manager.enemy_composition
        self.enemy_known_worker_count = 0

        self.enemy_army_known_minerals = 0
        self.enemy_army_known_gas = 0

        self.predicted_enemy_free_minerals = 0
        self.predicted_enemy_free_gas = 0

        self.predicted_enemy_army_minerals = 0
        self.predicted_enemy_army_gas = 0

        for unit_count in enemy_composition:
            if unit_count.count > 0:
                # TODO: Overlords!
                if self.unit_values.is_worker(unit_count.enemy_type):
                    self.enemy_known_worker_count += unit_count.count

                mineral_value = self.unit_values.minerals(
                    unit_count.enemy_type) * unit_count.count
                gas_value = self.unit_values.gas(
                    unit_count.enemy_type) * unit_count.count
                minerals_used += mineral_value
                gas_used += gas_value

                if not self.unit_values.is_worker(unit_count.enemy_type) \
                        and self.unit_values.power_by_type(unit_count.enemy_type) > 0.25:
                    self.enemy_power.add_unit(unit_count.enemy_type,
                                              unit_count.count)

                    self.predicted_enemy_composition.append(unit_count)
                    # Save values as to what we know to be true
                    self.enemy_army_known_minerals += mineral_value
                    self.enemy_army_known_gas += gas_value

        mined_minerals: int = 0
        mined_minerals_predict: float = 0
        worker_count_per_base = 12  # TODO: Just random guess

        for zone in self.knowledge.enemy_expansion_zones:
            current_minerals = zone.last_minerals
            if current_minerals is None:
                current_minerals = 0

            last_minerals = self.mineral_dict.get(zone, 0)

            if last_minerals > current_minerals:
                self.mineral_dict[zone] = current_minerals
                self.mineral_updated_dict[zone] = self.ai.time

                if zone.is_enemys:
                    mined_minerals += last_minerals - current_minerals
            elif zone.is_enemys:
                prediction = last_minerals - (
                    self.ai.time - self.mineral_updated_dict.get(zone, 0)
                ) * MINERAL_MINING_SPEED * worker_count_per_base
                prediction = max(0.0, prediction)
                mined_minerals_predict += last_minerals - prediction

        self.enemy_mined_minerals += mined_minerals
        self.enemy_mined_minerals_prediction = round(
            self.enemy_mined_minerals + mined_minerals_predict)

        if gas_miners.exists:

            for miner in gas_miners:  # type: Unit
                last_gas = self.gas_dict.get(miner.position, 2250)
                if miner.is_visible:
                    gas = miner.vespene_contents
                else:
                    gas = max(0.0, last_gas - 169.61 / 60 * INTERVAL)

                self.gas_dict[miner.position] = gas
                self.enemy_mined_gas += last_gas - gas

        lost_tuple: tuple = self.lost_units_manager.calculate_enemy_lost_resources(
        )
        minerals_used += lost_tuple[0]
        gas_used += lost_tuple[1]

        self.predicted_enemy_free_minerals = round(
            self.enemy_base_value_minerals +
            self.enemy_mined_minerals_prediction - minerals_used)

        self.predicted_enemy_free_gas = round(self.enemy_mined_gas - gas_used)

        if self.predicted_enemy_free_minerals < 0:
            # Possibly hidden base or more workers than we think?
            self.print(
                f"Predicting negative free minerals for enemy: {self.predicted_enemy_free_minerals}"
            )

        await self.predict_enemy_composition()

        for unit_count in self.predicted_enemy_composition:
            self.predicted_enemy_power.add_unit(unit_count.enemy_type,
                                                unit_count.count)
            mineral_value = self.unit_values.minerals(
                unit_count.enemy_type) * unit_count.count
            gas_value = self.unit_values.minerals(
                unit_count.enemy_type) * unit_count.count
            self.predicted_enemy_army_minerals += mineral_value
            self.predicted_enemy_army_gas += gas_value

    async def update_own_army_value(self):
        self.own_army_value_minerals = 0
        self.own_army_value_gas = 0

        for unit in self.ai.units:
            if not self.unit_values.is_worker(unit.type_id):
                self.own_army_value_minerals += self.unit_values.minerals(
                    unit.type_id)
                self.own_army_value_gas += self.unit_values.gas(unit.type_id)

    async def predict_enemy_composition(self):
        if self.knowledge.enemy_race == Race.Random:
            return  # let's wait until we know the actual race.

        guesser = CompositionGuesser(self.knowledge)
        guesser.left_minerals = self.predicted_enemy_free_minerals
        guesser.left_gas = self.predicted_enemy_free_gas

        additional_guess: List[UnitCount] = guesser.predict_enemy_composition()
        for unit_count in additional_guess:
            existing = self.find(self.predicted_enemy_composition,
                                 unit_count.enemy_type)
            if existing is None:
                self.predicted_enemy_composition.append(unit_count)
            else:
                existing.count += unit_count.count

    def find(self, lst: List[UnitCount], enemy_type) -> Optional[UnitCount]:
        for unit_count in lst:
            if unit_count.enemy_type == enemy_type:
                return unit_count
        return None

    async def post_update(self):
        await self.debug_message()

    async def debug_message(self):
        if self.knowledge.my_race == Race.Protoss:
            # my_comp = self.enemy_build.gate_type_values(self.predicted_enemy_composition)
            # my_comp.extend(self.enemy_build.robo_type_values(self.predicted_enemy_composition))
            # my_comp.extend(self.enemy_build.star_type_values(self.predicted_enemy_composition))

            enemy_comp = sorted(self.predicted_enemy_composition,
                                key=lambda uc: uc.count,
                                reverse=True)
            # my_comp = sorted(my_comp, key=lambda c: c.count, reverse=True)

            if self.debug:
                client: Client = self.ai._client
                msg = f"Us vs them: {self.own_value} / {self.enemy_value}\n"
                msg += f"Known enemy army (M/G): {self.enemy_army_known_minerals} / {self.enemy_army_known_gas}\n"
                msg += f"Enemy predicted money (M/G): {self.predicted_enemy_free_minerals} / {self.predicted_enemy_free_gas}\n"

                msg += f"\nComposition:\n"

                for unit_count in enemy_comp:
                    msg += f" {unit_count.to_short_string()}"

                # msg += f"\nCounter:\n"

                # for unit_count in my_comp:
                #     if unit_count.count > 0:
                #         msg += f" {unit_count.to_string()}\n"

                client.debug_text_2d(msg, Point2((0.1, 0.1)), None, 16)