async def figuring_supply(self): for supply_depot in self.structures(UnitTypeId.SUPPLYDEPOT).ready: self.do(supply_depot(AbilityId.MORPH_SUPPLYDEPOT_LOWER)) if self.supply_remaining_intense(): if self.can_afford(UnitTypeId.SUPPLYDEPOT): if self.there_has_valid_supplydepot(): if self.order_execute_num_in_scv('SupplyDepot') < 1: print( 'build a supplydepot near the supplydepot which furthest to the commandcenter' ) await self.building_with_position_related_to_supplydepot( UnitTypeId.SUPPLYDEPOT) elif self.check_if_valid_building_exists( UnitTypeId.COMMANDCENTER): print('build first supplydepot near command center') if self.first_supplydepot_position is not None: output_log('find_position:{0}'.format( self.first_supplydepot_position)) await self.build(UnitTypeId.SUPPLYDEPOT, near=self.first_supplydepot_position, max_distance=1) else: output_log( 'still not found depot position, using a default') await self.build( UnitTypeId.SUPPLYDEPOT, self.structures( UnitTypeId.COMMANDCENTER).ready.first, max_distance=20) else: return
async def record_speicfic_unit_orders(self): for scv_unit in self.units(UnitTypeId.SCV).ready: first_order = get_unit_first_order(scv_unit) if first_order not in ['Gather', 'ReturnCargo']: output_log('new scv order: {0}'.format(first_order)) for marine in self.units(UnitTypeId.MARINE).ready: first_order = get_unit_first_order(marine) output_log('new marine order: {0}'.format(first_order))
async def scout(self): if not self.enemy_structures and not self.enemy_units: if self.check_if_valid_building_exists(UnitTypeId.BARRACKS): if self.order_execute_num_in_scv('move') < 1: scout_scv = self.units(UnitTypeId.SCV).ready.first for position in self.enemy_start_locations: output_log('scout scv move to {0}'.format(position)) self.do(scout_scv.move(position)) time.sleep(0.1)
def get_unit_first_order(self, unit): order_queue = unit.orders try: first_order = re.search('\(name=(.*?)\)', str(order_queue[0].ability)).group(1) except Exception as e: output_log(e) first_order = '' first_order = first_order.lower() return first_order
async def do_upgrade(self): for addon in self.structures(UnitTypeId.BARRACKSTECHLAB).idle: output_log('start check possible available addon') current_upgrades = self.state.upgrades output_log('current upgrades: {0}'.format(current_upgrades)) if self.can_afford(AbilityId.BARRACKSTECHLABRESEARCH_STIMPACK): if UpgradeId.STIMPACK not in current_upgrades: self.do(addon(AbilityId.BARRACKSTECHLABRESEARCH_STIMPACK)) else: output_log('stimpack already upgraded') if self.can_afford(AbilityId.RESEARCH_COMBATSHIELD): if UpgradeId.SHIELDWALL not in current_upgrades: self.do(addon(AbilityId.RESEARCH_COMBATSHIELD)) else: output_log('combatshield already upgraded') else: output_log('there is no available techlab')
async def building_with_position_related_to_supplydepot( self, building_name: UnitTypeId): try: return await self.build( building_name, near=self.structures(UnitTypeId.SUPPLYDEPOT).ready.furthest_to( self.structures(UnitTypeId.COMMANDCENTER).ready.first)) except: try: return await self.build( building_name, near=self.structures( UnitTypeId.SUPPLYDEPOTLOWERED).ready.furthest_to( self.structures( UnitTypeId.COMMANDCENTER).ready.first)) except: output_log('no valid supplydepot')
async def build_gas_station(self): if self.get_building_or_unit_num(UnitTypeId.BARRACKS) < 1: return for commandcenter in self.structures(UnitTypeId.COMMANDCENTER).ready: vespenes = self.vespene_geyser.closer_than(10.0, commandcenter) output_log('there is {0} available vespenes'.format( vespenes.amount)) for vespene in vespenes: if not self.structures(UnitTypeId.REFINERY).closer_than( 1.0, vespene).exists: output_log('there is valid vespene exists') if not self.can_afford(UnitTypeId.REFINERY): break worker = self.select_build_worker(vespene.position) if worker is None: break if not self.structures(UnitTypeId.REFINERY).closer_than( 1.0, vespene).exists: self.do(worker.build(UnitTypeId.REFINERY, vespene))
async def manufacture_battle_unit(self): if self.get_building_or_unit_num(UnitTypeId.FACTORY) <= 1: for barracks in self.structures(UnitTypeId.BARRACKS).ready.idle: if self.get_building_or_unit_num(UnitTypeId.MARINE) < 3: if self.can_afford(UnitTypeId.MARINE): self.do(barracks.train(UnitTypeId.MARINE)) time.sleep(0.1) elif self.check_if_valid_building_exists(UnitTypeId.FACTORY): output_log('current has valid factory') if self.get_building_or_unit_num(UnitTypeId.STARPORT) >= 1: output_log('starport built') if barracks.has_add_on == 1: output_log('there is a barracks had add-on') if self.get_building_or_unit_num( UnitTypeId.MARAUDER) < 3: output_log('now marauder num is less than 3') if self.can_afford(UnitTypeId.MARAUDER): if sys.platform == 'win32': self.do( barracks.train( UnitTypeId.MARAUDER)) # self.do(barracks(AbilityId.BARRACKSTRAIN_MARAUDER)) else: self.do( barracks(AbilityId. BARRACKSTRAIN_MARAUDER)) # self.do(barracks(UnitTypeId.MARAUDER)) time.sleep(0.1) elif self.can_afford(UnitTypeId.MARINE): self.do(barracks.train(UnitTypeId.MARINE)) time.sleep(0.1) else: if self.can_afford(UnitTypeId.MARINE): self.do(barracks.train(UnitTypeId.MARINE)) time.sleep(0.1) target_medivac_num = round( (self.get_building_or_unit_num(UnitTypeId.MARINE) + self.get_building_or_unit_num(UnitTypeId.MARAUDER)) / 5) if self.check_if_valid_building_exists(UnitTypeId.STARPORT): if self.get_building_or_unit_num( UnitTypeId.MEDIVAC) < target_medivac_num: for starport in self.structures( UnitTypeId.STARPORT).ready.idle: if self.can_afford(UnitTypeId.MEDIVAC): self.do(starport.train(UnitTypeId.MEDIVAC)) if self.check_if_valid_building_exists(UnitTypeId.FACTORY): if self.get_building_or_unit_num(UnitTypeId.SIEGETANK) < round( (self.get_building_or_unit_num(UnitTypeId.MARINE) + self.get_building_or_unit_num(UnitTypeId.MARAUDER)) / 8): for factory in self.structures(UnitTypeId.FACTORY).ready.idle: if self.can_afford(UnitTypeId.SIEGETANK): self.do(factory.train(UnitTypeId.SIEGETANK))
async def find_first_building_position(self): if self.first_supplydepot_position is not None: return command_centers = self.structures(UnitTypeId.COMMANDCENTER).ready command_center = command_centers.first near = command_center.position.to2 p = await self.find_placement(UnitTypeId.SUPPLYDEPOT, near.rounded, 100, True, 2) mineral_fields = self.mineral_field.ready if not mineral_fields: output_log('this map has no mineral fields') return closest_dist = mineral_fields.closest_distance_to(p) output_log('closest distance: {0}'.format(closest_dist)) output_log(type(closest_dist)) if closest_dist < 10: output_log('too close to mineral') return if self.first_supplydepot_position is None: output_log('log depot_position') self.first_supplydepot_position = p
def running_siege_tank_strategy(self): for tank in self.units(UnitTypeId.SIEGETANK): if self.get_visible_enemy_battle_unit_or_building(): try: if self.enemy_units.closest_distance_to( tank.position) < 12: self.do(tank(AbilityId.SIEGEMODE_SIEGEMODE)) except Exception as e: output_log('unsiege error, info: {0}'.format(e)) for tank in self.units(UnitTypeId.SIEGETANKSIEGED): if not self.get_visible_enemy_battle_unit_or_building(): self.do(tank(AbilityId.UNSIEGE_UNSIEGE)) else: try: if self.enemy_units.closest_distance_to( tank.position) >= 13: self.do(tank(AbilityId.UNSIEGE_UNSIEGE)) except Exception as e: output_log('unsiege error, info: {0}'.format(e)) all_battle_units = self.get_all_friendly_battle_unit() if all_battle_units: for tank in self.units(UnitTypeId.SIEGETANK): try: self.do( tank.move( self.units.furthest_to( self.structures(UnitTypeId.COMMANDCENTER). ready.first.position))) except Exception as e: output_log('attack movement error: {0}'.format(e))
async def move_and_attack(self): output_log('type of enemy units property: {0}'.format( type(self.enemy_units))) self.running_medivac_strategy() self.running_siege_tank_strategy() current_friendly_battle_unit_num = self.get_friendly_battle_unit( ).amount if current_friendly_battle_unit_num == 0: return if current_friendly_battle_unit_num > 0: current_battlefield_status = self.get_current_battlefield_unit_status( ) # current_battlefield_status:敌我单位数量、战斗力数据 if current_battlefield_status[3] == 0: choice_seed = random.randint(1, 1000) current_total_health = self.get_current_friendly_unit_health() current_enemy_num = self.get_visible_enemy_battle_unit_or_building( ).amount random_seed = random.random() for unit in self.get_friendly_battle_unit(): unit_tag_id = unit.tag if current_enemy_num == self.current_enemy_force_num or current_total_health >= self.current_total_friendly_unit_health: # 没有新的敌人出现,而且没有单位受伤 current_first_order = self.get_unit_first_order(unit) if current_first_order == 'move': try: move_target = self.unit_move_target_position[ unit_tag_id] distance_to_target = move_target.distance_to( unit) if distance_to_target < 6: self.do(unit(AbilityId.STOP)) except Exception as e: output_log( 'distance judge error: {0}'.format(e)) pass if unit.is_attacking is True: if random_seed > 0.01: continue elif unit.is_idle is False: if random_seed > 0.1: continue if choice_seed > 700: choice_num = 0 regroup_point = self.get_army_central_point() self.do(unit.move(regroup_point)) self.unit_move_target_position[ unit_tag_id] = regroup_point elif choice_seed > 400: choice_num = 2 try: move_target_position = self.structures( UnitTypeId.COMMANDCENTER).ready.first self.do(unit.move(move_target_position)) self.unit_move_target_position[ unit_tag_id] = move_target_position except Exception as e: print(e) elif choice_seed > 100: continue else: choice_num = 1 if len(self.enemy_structures) > 0: self.do( unit.attack( random.choice( self.enemy_structures).position)) y = np.zeros(3) y[choice_num] = 1 self.train_data.append([y, current_battlefield_status]) self.current_enemy_force_num = current_enemy_num self.current_total_friendly_unit_health = current_total_health else: attack_choice_seed = random.randint(1, 1000) for unit in self.get_friendly_battle_unit(): random_attack_choice = random.randint(0, 4) if unit.health_percentage > 0.2: if unit.is_attacking: continue nearest_enemy = self.get_nearest_enemy_unit(unit) if nearest_enemy: if unit.name in ('Marine', 'Marauder'): if unit.target_in_range(nearest_enemy, 3): if unit.name == 'Marine': self.do(unit(AbilityId.EFFECT_STIM_MARINE)) elif unit.name == 'Marauder': self.do( unit(AbilityId.EFFECT_STIM_MARAUDER)) else: continue current_around_status = self.get_unit_around_status(unit) if attack_choice_seed > 60: if random_attack_choice == 0: if nearest_enemy is not None: self.do(unit.attack(nearest_enemy)) elif random_attack_choice == 1: target_enemy = self.get_highest_dps_enemy_unit( unit) if target_enemy is not None: self.do(unit.attack(target_enemy)) elif random_attack_choice == 3: evac_postion = self.get_unit_escape_position(unit) self.do(unit.move(evac_postion)) else: pass else: random_attack_choice = 4 commandcenter_position = random.choice( self.structures(UnitTypeId.COMMANDCENTER)).position unit_center_position = self.get_friendly_battle_unit( ).center target_position = Point2( ((commandcenter_position.x + unit_center_position.x) / 2, (commandcenter_position.y + unit_center_position.y) / 2)) self.do(unit.move(target_position)) y = np.zeros(5) y[random_attack_choice] = 1 self.attack_train_data.append([y, current_around_status])
async def development(self): if self.check_army_factory_number() <= 3: if self.can_afford(UnitTypeId.BARRACKS): output_log('can afford barracks now') if self.get_building_or_unit_num( UnitTypeId.COMMANDCENTER) >= 2: if (self.get_building_or_unit_num(UnitTypeId.FACTORY) >= 1 and self.get_building_or_unit_num(UnitTypeId.STARPORT) >= 1) or self.get_building_or_unit_num( UnitTypeId.BARRACKS) < 1: if self.get_building_or_unit_num( UnitTypeId.BARRACKS) < 1: if self.there_has_valid_supplydepot(): await self.building_with_position_related_to_supplydepot( UnitTypeId.BARRACKS) else: if self.there_has_valid_supplydepot(): print('build barracks no.{0}'.format( self.get_building_or_unit_num( UnitTypeId.BARRACKS))) await self.build( UnitTypeId.BARRACKS, near=self.structures( UnitTypeId.BARRACKS).ready.first) elif self.get_building_or_unit_num(UnitTypeId.BARRACKS) < 1: if self.check_if_valid_building_exists( UnitTypeId.SUPPLYDEPOT ) or self.check_if_valid_building_exists( UnitTypeId.SUPPLYDEPOTLOWERED): print('build first barracks') await self.building_with_position_related_to_supplydepot( UnitTypeId.BARRACKS) if self.can_afford(UnitTypeId.FACTORY): if self.get_building_or_unit_num( UnitTypeId.COMMANDCENTER) >= 2: if self.check_if_valid_building_exists( UnitTypeId.BARRACKS): if self.get_building_or_unit_num( UnitTypeId.FACTORY) < 1: if self.get_building_or_unit_num( UnitTypeId.BARRACKS) >= 1: await self.build( UnitTypeId.FACTORY, near=self.structures( UnitTypeId.BARRACKS).furthest_to( self.start_location)) if self.can_afford(UnitTypeId.STARPORT): if self.get_building_or_unit_num( UnitTypeId.COMMANDCENTER) >= 2: if self.check_if_valid_building_exists(UnitTypeId.FACTORY): if self.get_building_or_unit_num( UnitTypeId.FACTORY) >= 1: if self.get_building_or_unit_num( UnitTypeId.STARPORT) < 1: if self.order_execute_num_in_scv( 'Starport') < 1: await self.build( UnitTypeId.STARPORT, near=self.structures( UnitTypeId.FACTORY).ready. furthest_to( self.structures( UnitTypeId.COMMANDCENTER). ready.first)) if (self.get_minerals_nearby_command_center() < self.get_building_or_unit_num(UnitTypeId.COMMANDCENTER) * 12) and self.can_afford(UnitTypeId.COMMANDCENTER): await self.expand_now() if self.can_afford(UnitTypeId.BARRACKSTECHLAB): for barracks in self.structures( UnitTypeId.BARRACKS).ready.idle: try: if self.get_building_or_unit_num( UnitTypeId.BARRACKSTECHLAB ) < 1 and self.check_army_factory_number() < 5: if barracks.add_on_tag == 0: self.do( barracks.build(UnitTypeId.BARRACKSTECHLAB)) elif self.get_building_or_unit_num( UnitTypeId.BARRACKSTECHLAB) < 2: if barracks.add_on_tag == 0: self.do( barracks.build(UnitTypeId.BARRACKSTECHLAB)) except Exception as e: output_log(e) if self.can_afford(UnitTypeId.STARPORTREACTOR): for starport in self.structures( UnitTypeId.STARPORT).ready.idle: try: if self.get_building_or_unit_num( UnitTypeId.STARPORTREACTOR ) < 1 and self.check_army_factory_number() < 5: if starport.add_on_tag == 0: self.do( starport.build(UnitTypeId.STARPORTREACTOR)) elif self.get_building_or_unit_num( UnitTypeId.STARPORTREACTOR) < 2: if starport.add_on_tag == 0: self.do( starport.build(UnitTypeId.STARPORTREACTOR)) except Exception as e: output_log( 'can\'t build starport reactor: {0}'.format(e)) if self.can_afford(UnitTypeId.FACTORYTECHLAB): for starport in self.structures(UnitTypeId.FACTORY).ready.idle: try: if self.get_building_or_unit_num( UnitTypeId.FACTORYTECHLAB) < 1: self.do(starport.build(UnitTypeId.FACTORYTECHLAB)) except Exception as e: output_log( 'can\'t build factory reactor: {0}'.format(e)) else: if self.get_building_or_unit_num( UnitTypeId.BARRACKS) <= self.get_building_or_unit_num( UnitTypeId.COMMANDCENTER) * 3: if self.there_has_valid_supplydepot(): output_log('building more barracks') await self.build(UnitTypeId.BARRACKS, near=self.structures( UnitTypeId.BARRACKS).ready.first)