def __init__(self, knowledge: Knowledge, close_enemies: Units, unit: Unit, our_units: Units, our_median: Point2): self.ai = knowledge.ai self.our_units = our_units self.our_median = our_median self.close_enemies = close_enemies self.my_height = self.ai.get_terrain_height(unit) self.enemy_power = ExtendedPower(knowledge.unit_values) self.our_power = ExtendedPower(knowledge.unit_values) for unit in our_units: # type: Unit self.our_power.add_unit(unit) self.worker_only = False if self.close_enemies.exists: self.enemy_center = close_enemies.center # Can be empty! self.powered_enemies = close_enemies.filter(lambda x: knowledge.unit_values.power(x) > 0.1) if self.powered_enemies.exists: self.closest = unit.position.closest(self.powered_enemies) self.worker_only = True for enemy in self.powered_enemies: # type: Unit if not knowledge.unit_values.is_worker(enemy): self.worker_only = False self.enemy_power.add_unit(enemy) else: self.closest = unit.position.closest(self.close_enemies) self.enemy_center_height = self.ai.get_terrain_height(self.enemy_center) self.closest_height = self.ai.get_terrain_height(self.closest) else: self.powered_enemies = Units([], self.ai) # empty list
def _defenders_from(self, task: UnitTask, current_power: ExtendedPower, position: Point2, power: ExtendedPower, units: Units): """ Get defenders from a task. """ if current_power.is_enough_for(power): return exclude_types = [] exclude_types.append(UnitTypeId.OVERSEER) exclude_types.extend(self.knowledge.unit_values.worker_types) exclude_types.extend(self.peace_unit_types) role_units = self.roles[task.value].units\ .exclude_type(exclude_types) unit: Unit for unit in role_units.sorted_by_distance_to(position): enough_air_power = current_power.air_power >= power.air_presence * 1.1 enough_ground_power = current_power.ground_power >= power.ground_presence * 1.1 if not self.unit_values.can_shoot_air(unit) and not enough_air_power and enough_ground_power: # Don't pull any more units that can't actually shoot the targets continue if not self.unit_values.can_shoot_ground(unit, self.knowledge) and enough_air_power and not enough_ground_power: # Don't pull any more units that can't actually shoot the targets continue current_power.add_unit(unit) units.append(unit) if current_power.is_enough_for(power): return return
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()
class CombatUnits(): def __init__(self, units: Units, knowledge: 'Knowledge'): self.knowledge = knowledge self.unit_values = knowledge.unit_values self.units = units self.center = sc2math.unit_geometric_median(units) self.ground_units = self.units.not_flying if self.ground_units: self.center = self.ground_units.closest_to((self.center)).position self.power = ExtendedPower(self.unit_values) self.power.add_units(self.units) self.debug_index = 0 self._total_distance: Optional[float] = None self._area_by_circles: float = 0 def is_too_spread_out(self) -> bool: if self._total_distance is None: self._total_distance = 0 self._area_by_circles = 5 for unit in self.units: d = unit.distance_to(self.center) self._total_distance += d self._area_by_circles += unit.radius ** 2 # self.knowledge.print(f"spread: {self._total_distance} d to {self._total_radius} r") return (self._total_distance / len(self.units)) ** 2 > self._area_by_circles * 2 def is_in_combat(self, closest_enemies: 'CombatUnits') -> bool: if closest_enemies is None: return False distance = self.center.distance_to_point2(closest_enemies.center) if distance > 17: return False if distance < 10 \ or self.knowledge.unit_cache.enemy_in_range(self.center, 10).exclude_type(self.unit_values.combat_ignore): return True engaged_power = 0 total_power = 0 for unit in self.units: # type: Unit power = self.unit_values.power(unit) total_power += power for enemy_near in closest_enemies.units: d = enemy_near.distance_to(unit) if d < self.unit_values.real_range(unit, enemy_near, self.knowledge): engaged_power += power break return engaged_power > total_power * 0.15
def __init__(self, units: Units, knowledge: 'Knowledge'): self.knowledge = knowledge self.unit_values = knowledge.unit_values self.units = units self.center = sc2math.unit_geometric_median(units) self.ground_units = self.units.not_flying if self.ground_units: self.center = self.ground_units.closest_to((self.center)).position self.power = ExtendedPower(self.unit_values) self.power.add_units(self.units) self.debug_index = 0 self._total_distance: Optional[float] = None self._area_by_circles: float = 0
def enemy_total_power(self) -> ExtendedPower: """Returns the total power of all enemy units we currently know about. Assumes they are all in full health. Ignores workers and overlords.""" total_power = ExtendedPower(self.unit_value) for type_id in self._known_enemy_units_dict: if self.unit_value.is_worker(type_id): continue if type_id == UnitTypeId.OVERLORD: continue count_for_unit_type = self.unit_count(type_id) total_power.add_unit(type_id, count_for_unit_type) return total_power
def calc_needs_evacuation(self): """ Checks if the zone needs evacuation for the workers mining there. This is a method because it is quite CPU heavy. """ enemies: Units = self.cache.enemy_in_range(self.mineral_line_center, 10) power = ExtendedPower(self.unit_values) power.add_units(enemies) if power.ground_power > 3 and enemies.exclude_type( self.unit_values.worker_types): self.needs_evacuation = True else: self.needs_evacuation = False
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)
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
def get_defenders(self, power: ExtendedPower, position: Point2 ) -> Units: units = Units([], self.ai) current_power = ExtendedPower(self.unit_values) self._defenders_from(UnitTask.Idle, current_power, position, power, units) self._defenders_from(UnitTask.Moving, current_power, position, power, units) self._defenders_from(UnitTask.Fighting, current_power, position, power, units) self._defenders_from(UnitTask.Attacking, current_power, position, power, units) return units
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
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
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 _should_attack(self, power: ExtendedPower) -> bool: if self.attack_on_advantage: if ((self.game_analyzer.our_army_predict in at_least_clear_advantage and self.game_analyzer.our_income_advantage in at_least_small_disadvantage) or (self.game_analyzer.our_army_predict in at_least_small_advantage and self.game_analyzer.our_income_advantage in at_least_clear_disadvantage)): # Our army is bigger but economy is weaker, attack! return True if ((self.game_analyzer.our_army_predict in at_least_small_disadvantage and self.game_analyzer.our_income_advantage in at_least_clear_advantage) or (self.game_analyzer.our_army_predict in at_least_clear_disadvantage and self.game_analyzer.our_income_advantage in at_least_small_advantage)): # Our army is smaller but economy is better, focus on defence! return False enemy_total_power: ExtendedPower = self.knowledge.enemy_units_manager.enemy_total_power enemy_total_power.multiply(ENEMY_TOTAL_POWER_MULTIPLIER) multiplier = ENEMY_TOTAL_POWER_MULTIPLIER zone_count = 0 for zone in self.knowledge.expansion_zones: # type: Zone if zone.is_enemys: zone_count += 1 enemy_main: Zone = self.knowledge.expansion_zones[-1] enemy_natural: Zone = self.knowledge.expansion_zones[-2] if zone_count == 1 and enemy_main.is_enemys: # We should seriously consider whether we want to crash and burn against a one base defense enemy_total_power.add_units(enemy_main.enemy_static_defenses) #multiplier *= 2 elif zone_count == 2 and enemy_natural.is_enemys: enemy_total_power.add_units(enemy_natural.enemy_static_defenses) # if (self.knowledge.enemy_race == Race.Terran # and self.knowledge.enemy_units_manager.unit_count(UnitTypeId.SIEGETANK) > 1): # multiplier = 1.6 enemy_total_power.power = max(self.start_attack_power, enemy_total_power.power) if power.is_enough_for(enemy_total_power, 1 / multiplier): self.print(f"Power {power.power:.2f} is larger than required attack power {enemy_total_power.power:.2f} -> attack!") return True if self.ai.supply_used > 190: self.print(f"Supply is {self.ai.supply_used} -> attack!") return True return False
def enemy_static_power(self) -> ExtendedPower: """Returns power of enemy static defenses.""" power = ExtendedPower(self.unit_values) power.add_units(self.enemy_static_defenses) return power
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)
def solve_combat(self, goal: CombatGoal, command: CombatAction, enemies: EnemyData) -> List[CombatAction]: oracle = goal.unit if not oracle.has_buff(BuffId.ORACLEWEAPON): goal.ready_to_shoot = False air_shooter_enemies = Units([], self.ai) enemy: Unit power = ExtendedPower(self.unit_values) for enemy in enemies.close_enemies: if self.unit_values.air_range(enemy) < enemy.distance_to(oracle) + 1: air_shooter_enemies.append(enemy) power.add_unit(enemy) if self.unit_values.is_static_air_defense(enemy): power.add(5) # can't beat turrets with oracle enemy_center = enemies.close_enemies.center for air_shooter in air_shooter_enemies: # type: Unit if air_shooter.is_light and not air_shooter.is_flying: power.add_units(air_shooter_enemies) else: power.add_units(air_shooter_enemies * 2) time = self.knowledge.ai.time if goal.move_type == MoveType.PanicRetreat and oracle.has_buff(BuffId.ORACLEWEAPON): return self.disable_beam(oracle) possible_targets = enemies.close_enemies.filter(lambda u: not u.is_flying and not u.is_structure and u.is_light) if possible_targets.exists: if oracle.energy > 50 and possible_targets.closest_distance_to(oracle) < 5 and not oracle.has_buff(BuffId.ORACLEWEAPON): return self.enable_beam(oracle) if power.air_power > 0 and power.air_power <= 3: target = air_shooter_enemies.closest_to(oracle) if target.is_light or target.health_percentage < 0.5: if not oracle.has_buff(BuffId.ORACLEWEAPON): return self.enable_beam(oracle) # Kill the target return [CombatAction(oracle, target, True)] #target_pos = self.knowledge.pathing_manager.find_weak_influence_air(goal.target, 7) #move_step = self.knowledge.pathing_manager.find_influence_air_path(oracle.position, target_pos) return [CombatAction(oracle, target.position, True)] elif goal.ready_to_shoot and possible_targets: return [CombatAction(oracle, possible_targets.closest_to(oracle), True)] elif power.air_power > 12: # Panic retreat to whatever direction if goal.move_type in offensive: new_target: Point2 = self.knowledge.pathing_manager.find_weak_influence_air(goal.target, 7) step = self.knowledge.pathing_manager.find_influence_air_path(oracle.position, new_target) # backstep: Point2 = self.knowledge.pathing_manager.find_weak_influence_air(oracle.position, 7) move_action = CombatAction(oracle, step, False) else: backstep = self.knowledge.pathing_manager.find_influence_air_path(oracle.position, goal.target) move_action = CombatAction(oracle, backstep, False) # Todo disable beam? return [move_action] elif power.air_power > 3: # Try kiting while killing the target target = self.knowledge.pathing_manager.find_weak_influence_air(goal.target, 7) backstep = self.knowledge.pathing_manager.find_influence_air_path(oracle.position, target) if goal.move_type in offensive: move_action = CombatAction(oracle, backstep, False) else: move_action = CombatAction(oracle, backstep, False) if oracle.has_buff(BuffId.ORACLEWEAPON): if possible_targets: closest = possible_targets.closest_to(oracle) if closest.distance_to(oracle) < 5: return [CombatAction(oracle, closest, True)] return [CombatAction(oracle, command.target, True), move_action] else: return [move_action] if possible_targets.exists: return [CombatAction(oracle, command.target, True)] else: return [CombatAction(oracle, command.target, False)]
async def execute(self) -> bool: unit: Unit all_defenders = self.knowledge.roles.all_from_task(UnitTask.Defending) for i in range(0, len(self.knowledge.expansion_zones)): zone: 'Zone' = self.knowledge.expansion_zones[i] zone_tags = self.defender_tags[i] zone_defenders_all = all_defenders.tags_in(zone_tags) zone_worker_defenders = zone_defenders_all(self.worker_type) zone_defenders = zone_defenders_all.exclude_type(self.worker_type) enemies = zone.known_enemy_units # Let's loop zone starting from our main, which is the one we want to defend the most # Check that zone is either in our control or is our start location that has no Nexus if zone_defenders.exists or zone.is_ours or zone == self.knowledge.own_main_zone: if not self.defense_required(enemies): # Delay before removing defenses in case we just lost visibility of the enemies if zone.last_scouted_center == self.knowledge.ai.time \ or self.zone_seen_enemy[i] + PlanZoneDefense.ZONE_CLEAR_TIMEOUT < self.ai.time: self.knowledge.roles.clear_tasks(zone_defenders_all) zone_defenders.clear() zone_tags.clear() continue # Zone is well under control. else: self.zone_seen_enemy[i] = self.ai.time if enemies.exists: # enemy_center = zone.assaulting_enemies.center enemy_center = enemies.closest_to( zone.center_location).position elif zone.assaulting_enemies: enemy_center = zone.assaulting_enemies.closest_to( zone.center_location).position else: enemy_center = zone.gather_point defense_required = ExtendedPower(self.unit_values) defense_required.add_power(zone.assaulting_enemy_power) defense_required.multiply(1.5) defenders = ExtendedPower(self.unit_values) for unit in zone_defenders: self.combat.add_unit(unit) defenders.add_unit(unit) # Add units to defenders that are being warped in. for unit in self.knowledge.roles.units( UnitTask.Idle).not_ready: if unit.distance_to(zone.center_location) < zone.radius: # unit is idle in the zone, add to defenders self.combat.add_unit(unit) self.knowledge.roles.set_task(UnitTask.Defending, unit) zone_tags.append(unit.tag) if not defenders.is_enough_for(defense_required): defense_required.substract_power(defenders) for unit in self.knowledge.roles.get_defenders( defense_required, zone.center_location): if unit.distance_to( zone.center_location) < zone.radius: # Only count units that are close as defenders defenders.add_unit(unit) self.knowledge.roles.set_task(UnitTask.Defending, unit) self.combat.add_unit(unit) zone_tags.append(unit.tag) if len(enemies) > 1 or (len(enemies) == 1 and enemies[0].type_id not in self.unit_values.worker_types): # Pull workers to defend only and only if the enemy isn't one worker scout if defenders.is_enough_for(defense_required): # Workers should return to mining. for unit in zone_worker_defenders: zone.go_mine(unit) if unit.tag in zone_tags: # Just in case, should be in zone tags always. zone_tags.remove(unit.tag) # Zone is well under control without worker defense. else: await self.worker_defence(defenders.power, defense_required, enemy_center, zone, zone_tags, zone_worker_defenders) self.combat.execute(enemy_center, MoveType.SearchAndDestroy) return True # never block
def enemy_static_air_power(self) -> ExtendedPower: """Returns power of enemy static ground defenses on the zone.""" power = ExtendedPower(self.unit_values) for air_def in self.enemy_static_air_defenses: power.add_unit(air_def) return power
def enemy_static_ground_power(self) -> ExtendedPower: """Returns power of enemy static ground defenses.""" power = ExtendedPower(self.unit_values) for ground_def in self.enemy_static_ground_defenses: power.add_unit(ground_def) return power
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)
def __init__(self, center_location, is_start_location, knowledge): self.center_location: Point2 = center_location self.is_start_location: bool = is_start_location self.knowledge = knowledge self.ai: sc2.BotAI = knowledge.ai self.cache: 'UnitCacheManager' = knowledge.unit_cache self.unit_values: UnitValue = knowledge.unit_values self.needs_evacuation = False self._is_enemys = False self.zone_index: int = 0 self.paths: Dict[int, Path] = dict( ) # paths to other expansions as it is dictated in the .expansion_zones # Game time seconds when we have last had visibility on this zone. self.last_scouted_center: float = -1 self.last_scouted_mineral_line: float = -1 # Timing on when there could be enemy workers here self.could_have_enemy_workers_in = 0 # All mineral fields on the zone self._original_mineral_fields: Units = self.ai.expansion_locations.get( self.center_location, Units([], self.ai)) self.mineral_fields: Units = Units( self._original_mineral_fields.copy(), self.ai) self.last_minerals: int = 10000000 # Arbitrary value just to ensure a lower value will get updated. # Game time seconds when scout has last circled around the center location of this zone. # All vespene geysers on the zone self.gas_buildings: Units = None self.scout_last_circled: Optional[int] = None self.our_townhall: Optional[Unit] = None self.enemy_townhall: Optional[Unit] = None self.known_enemy_units: Units = Units([], self.ai) self.our_units: Units = Units([], self.ai) self.our_workers: Units = Units([], self.ai) self.enemy_workers: Units = Units([], self.ai) self.known_enemy_power: ExtendedPower = ExtendedPower(self.unit_values) self.our_power: ExtendedPower = ExtendedPower(self.unit_values) # Assaulting enemies can be further away, but zone defense should prepare for at least that amount of defense self.assaulting_enemies: Units = Units([], self.ai) self.assaulting_enemy_power: ExtendedPower = ExtendedPower( self.unit_values) # 3 positions behind minerals self.behind_mineral_positions: List[ Point2] = self._init_behind_mineral_positions() self._count_minerals() self._minerals_counter = IntervalFunc(knowledge.ai, self._count_minerals, 0.5) self.gather_point = self.center_location.towards( self.ai.game_info.map_center, 5) self.height = self.ai.get_terrain_height(center_location) # This is ExtendedRamp! self.ramp = self._find_ramp(self.ai) self.radius = Zone.ZONE_RADIUS if self.ramp is not None: self.gather_point = self.ramp.top_center.towards( self.center_location, 4)
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 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
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)
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()