def execute(self, observations: Observations) -> actions.FunctionCall: if not self.building_built: if self.build_action_id() in observations.available_actions(): unit_y, unit_x = self.base_location.locate_command_center( observations.screen()) if unit_x.any(): target = self.base_location.transform_distance( int(unit_x.mean()), self.x_from_base, int(unit_y.mean()), self.y_from_base) self.building_built = True return self.build_action(target) elif not self.select_scv_order.done(observations): return self.select_scv_order.execute(observations) elif not self.building_rallied: if not self.building_selected: unit_type = observations.screen().unit_type() unit_y, unit_x = (unit_type == self.building_type()).nonzero() if unit_y.any(): target = [int(unit_x.mean()), int(unit_y.mean())] self.building_selected = True return self.actions.select_point(target) else: self.building_rallied = True if self.base_location.command_center_is_top_left(): target = [29, 21] else: target = [29, 46] return self.actions.rally_units_minimap(target) return self.actions.no_op()
def execute(self, observations: Observations) -> actions.FunctionCall: if not self.builder_scv_selected: self.builder_scv_selected = True print("select builder") return SelectSCV(self.base_location).execute(observations) # https://itnext.io/how-to-locate-and-select-units-in-pysc2-2bb1c81f2ad3 elif not self.refinery_building and self.action_ids.build_refinery( ) in observations.available_actions(): unit_type = observations.screen().unit_type() vespene_y, vespene_x = (unit_type == self.unit_type_ids. neutral_vespene_geyser()).nonzero() vespene_geyser_count = int(math.ceil(len(vespene_y) / 97)) units = [] for i in range(0, len(vespene_y)): units.append((vespene_x[i], vespene_y[i])) kmeans = KMeans(vespene_geyser_count) kmeans.fit(units) vespene1_x = int(kmeans.cluster_centers_[0][0]) vespene1_y = int(kmeans.cluster_centers_[0][1]) self.refinery_target = [vespene1_x, vespene1_y] self.refinery_building = True print("refinery building") return self.actions.build_refinery(self.refinery_target) elif self.refinery_building and not self.refinery_selected: unit_type = observations.screen().unit_type() refinery_y, refinery_x = ( unit_type == self.unit_type_ids.terran_refinery()).nonzero() if refinery_y.any(): self.refinery_selected = True print("refinery selected") return self.actions.select_point(self.refinery_target) elif self.refinery_selected and not self.first_collector_scv_selected: if observations.single_select().is_built(): self.first_collector_scv_selected = True print("select first collector") return SelectSCV(self.base_location).execute(observations) elif self.first_collector_scv_selected and not self.first_collector_scv_sent: self.first_collector_scv_sent = True print("sent first collector") return self.actions.harvest_gather(self.refinery_target) elif self.first_collector_scv_sent and not self.second_collector_scv_selected: self.second_collector_scv_selected = True print("select second collector") return SelectSCV(self.base_location).execute(observations) elif self.second_collector_scv_selected and not self.second_collector_scv_sent: self.second_collector_scv_sent = True print("sent second collector") return self.actions.harvest_gather(self.refinery_target) return self.actions.no_op()
def barracks_count(self, observations: Observations) -> int: unit_type = observations.screen().unit_type() unit_type_ids = UnitTypeIds() barracks_y, barracks_x = ( unit_type == unit_type_ids.terran_barracks()).nonzero() barracks_count = int(round(len(barracks_y) / 137)) return barracks_count
def supply_depots_count(self, observations: Observations) -> int: unit_type = observations.screen().unit_type() unit_type_ids = UnitTypeIds() depot_y, depot_x = ( unit_type == unit_type_ids.terran_supply_depot()).nonzero() supply_depot_count = int(round(len(depot_y) / 69)) return supply_depot_count
def build_state(self, location: Location, observations: Observations) -> []: unit_type = observations.screen().unit_type() unit_type_ids = UnitTypeIds() cc_y, cc_x = (unit_type == unit_type_ids.terran_command_center()).nonzero() cc_count = 1 if cc_y.any() else 0 depot_y, depot_x = (unit_type == unit_type_ids.terran_supply_depot()).nonzero() supply_depot_count = int(round(len(depot_y) / 69)) barracks_y, barracks_x = (unit_type == unit_type_ids.terran_barracks()).nonzero() barracks_count = int(round(len(barracks_y) / 137)) current_state = np.zeros(8) current_state[0] = cc_count current_state[1] = supply_depot_count current_state[2] = barracks_count current_state[3] = observations.player().food_army() hot_squares = np.zeros(4) enemy_y, enemy_x = (observations.minimap().player_relative() == _PLAYER_HOSTILE).nonzero() for i in range(0, len(enemy_y)): y = int(math.ceil((enemy_y[i] + 1) / 32)) x = int(math.ceil((enemy_x[i] + 1) / 32)) hot_squares[((y - 1) * 2) + (x - 1)] = 1 if not location.command_center_is_top_left(): hot_squares = hot_squares[::-1] for i in range(0, 4): current_state[i + 4] = hot_squares[i] return current_state
def command_center_count(self, observations: Observations) -> int: unit_type = observations.screen().unit_type() unit_type_ids = UnitTypeIds() cc_y, cc_x = ( unit_type == unit_type_ids.terran_command_center()).nonzero() cc_count = 1 if cc_y.any() else 0 return cc_count
def factories_count(self, observations: Observations) -> int: unit_type = observations.screen().unit_type() unit_type_ids = UnitTypeIds() factories_y, factories_x = ( unit_type == unit_type_ids.terran_factory()).nonzero() factories_count = int(round(len(factories_y) / 137)) return factories_count
def refineries_count(self, observations: Observations) -> int: unit_type = observations.screen().unit_type() unit_type_ids = UnitTypeIds() unit_y, unit_x = ( unit_type == unit_type_ids.terran_refinery()).nonzero() units_count = int(round(len(unit_y) / 97)) return units_count
def execute(self, observations: Observations) -> actions.FunctionCall: if not self.scv_selected: unit_type = observations.screen().unit_type() unit_y, unit_x = ( unit_type == self.unit_type_ids.terran_scv()).nonzero() rand_unit_index = random.randint(0, len(unit_y) - 1) target = [unit_x[rand_unit_index], unit_y[rand_unit_index]] self.scv_selected = True return self.actions.select_point(target) elif not self.scv_moved and self.action_ids.move_minimap( ) in observations.available_actions(): unit_y, unit_x = self.other_bases_minimap_locations.pop(0) target = [unit_x, unit_y] self.scv_moved = True return self.actions.move_minimap(target) elif self.scv_moved and observations.player().idle_worker_count( ) > 0 and len(self.other_bases_minimap_locations) > 0: self.scv_moved = False return self.actions.select_idle_worker() elif self.infinite_scouting and len( self.other_bases_minimap_locations) == 0: self.other_bases_minimap_locations = self.base_location.other_unknown_bases_locations_on_minimap( ) elif not self.scv_back_to_base and len( self.other_bases_minimap_locations) == 0: unit_y, unit_x = self.base_location.base_location_on_minimap() target = [unit_x, unit_y] self.scv_back_to_base = True return self.actions.move_minimap(target) return self.actions.no_op()
def reactor_barracks_count(self, observations: Observations) -> int: unit_type = observations.screen().unit_type() unit_type_ids = UnitTypeIds() reactor_y, reactor_x = ( unit_type == unit_type_ids.terran_barracks_reactor()).nonzero() if reactor_y.any(): return 1 return 0
def __init__(self, first_observations: Observations): player_y, player_x = (first_observations.minimap().player_relative() == _PLAYER_SELF).nonzero() self.base_top_left = player_y.mean() <= 31 unit_type = first_observations.screen().unit_type() self.unit_type_ids = UnitTypeIds() self.cc_y, self.cc_x = ( unit_type == self.unit_type_ids.terran_command_center()).nonzero()
def executable(self, observations: Observations) -> bool: unit_type = observations.screen().unit_type() unit_y, unit_x = (unit_type == self.unit_type_ids. terran_barracks_techlab()).nonzero() if unit_y.any(): return True else: return False
def techlab_barracks_count(self, observations: Observations) -> int: unit_type = observations.screen().unit_type() unit_type_ids = UnitTypeIds() techlabs_y, techlabs_x = ( unit_type == unit_type_ids.terran_barracks_techlab()).nonzero() if techlabs_y.any(): return 1 return 0
def execute(self, observations: Observations) -> actions.FunctionCall: if not self.barracks_selected: unit_type = observations.screen().unit_type() unit_y, unit_x = ( unit_type == self.unit_type_ids.terran_barracks()).nonzero() target = [int(unit_x.mean()), int(unit_y.mean())] self.barracks_selected = True return self.actions.select_point(target) elif self.action_ids.build_techlab_barracks( ) in observations.available_actions(): self.tech_lab_built = True unit_type = observations.screen().unit_type() unit_y, unit_x = ( unit_type == self.unit_type_ids.terran_barracks()).nonzero() target = [int(unit_x.mean()), int(unit_y.mean())] return self.actions.build_techlab_barracks(target) return self.actions.no_op()
def detect_race(self, observations: Observations): enemy_y, enemy_x = (observations.screen().player_relative() == _PLAYER_ENEMY).nonzero() if enemy_y.any(): unit_type = observations.screen().unit_type() for unit_id in AllUnitTypeIdsPerRace().zerg(): unit_y, unit_x = (unit_type == unit_id).nonzero() if unit_y.any(): self.enemy_race = RaceNames().zerg() unit_type = observations.screen().unit_type() for unit_id in AllUnitTypeIdsPerRace().protoss(): unit_y, unit_x = (unit_type == unit_id).nonzero() if unit_y.any(): self.enemy_race = RaceNames().protoss() if not self.race_detected(): self.enemy_race = RaceNames().terran()
def _select_command_center( self, observations: Observations) -> actions.FunctionCall: unit_type = observations.screen().unit_type() cc_y, cc_x = ( unit_type == self.unit_type_ids.terran_command_center()).nonzero() if cc_y.any(): target = [round(cc_x.mean()), round(cc_y.mean())] return self.actions.select_point(target) return self.actions.no_op()
def select_barracks(self, observations: Observations) -> actions.FunctionCall: unit_type = observations.screen().unit_type() barracks_y, barracks_x = ( unit_type == self.unit_type_ids.terran_barracks()).nonzero() if barracks_y.any(): i = random.randint(0, len(barracks_y) - 1) target = [barracks_x[i], barracks_y[i]] return self.actions.select_point_all(target) return self.actions.no_op()
def order(self, observations: Observations, step_index: int) -> Order: if not self.build_orders.finished(observations): if not self.base_location.command_center_is_visible( observations.screen()): current_order = CenterCameraOnCommandCenter(self.base_location) else: current_order = self.build_orders.current(observations) return current_order else: return NoOrder()
def execute(self, observations: Observations) -> actions.FunctionCall: if not self.select_scv_order.done(observations): return self.select_scv_order.execute(observations) elif self.action_ids.move_screen() in observations.available_actions(): unit_type = observations.screen().unit_type() unit_y, unit_x = ( unit_type == self.unit_type_ids.terran_refinery()).nonzero() if unit_y.any(): target = [int(unit_x.mean()), int(unit_y.mean())] self.scv_sent_to_refinery = True return self.actions.move_screen(target) return self.actions.no_op()
def send_scv_to_mineral( self, observations: Observations) -> actions.FunctionCall: if self.action_ids.harvest_gather() in observations.available_actions( ): unit_type = observations.screen().unit_type() unit_y, unit_x = (unit_type == self.unit_type_ids. neutral_mineral_field()).nonzero() if unit_y.any(): i = random.randint(0, len(unit_y) - 1) m_x = unit_x[i] m_y = unit_y[i] target = [int(m_x), int(m_y)] return self.actions.harvest_gather(target) return self.actions.no_op()
def execute(self, observations: Observations) -> actions.FunctionCall: if not self.scv_selected: if observations.player().idle_worker_count() > 0: self.scv_selected = True return self.actions.select_idle_worker() else: unit_type = observations.screen().unit_type() unit_y, unit_x = ( unit_type == self.unit_type_ids.terran_scv()).nonzero() rand_unit_index = random.randint(0, len(unit_y) - 1) target = [unit_x[rand_unit_index], unit_y[rand_unit_index]] self.scv_selected = True return self.actions.select_point(target) return self.actions.no_op()
def execute(self, observations: Observations) -> actions.FunctionCall: if not self.supply_depot_built: if not self.select_scv_order.done(observations): return self.select_scv_order.execute(observations) elif self.action_ids.build_supply_depot( ) in observations.available_actions(): cc_y, cc_x = self.base_location.locate_command_center( observations.screen()) target = self.base_location.transform_distance( int(cc_x.mean()), self.x_from_base, int(cc_y.mean()), self.y_from_base) self.supply_depot_built = True return self.actions.build_supply_depot(target) return self.actions.no_op()
def _select_tech_barrack( self, observations: Observations) -> actions.FunctionCall: techlab_barrack_count = BuildingCounter().techlab_barracks_count( observations) if techlab_barrack_count >= 1: self.step = self.step + 1 unit_type = observations.screen().unit_type() center_y, center_x = (unit_type == self.unit_type_ids. terran_barracks_techlab()).nonzero() center_x = round(center_x.mean()) center_y = round(center_y.mean()) target = [center_x, center_y] return self.actions.select_point(target) return self.actions.no_op()
def execute(self, observations: Observations) -> actions.FunctionCall: if not self.command_center_selected: unit_type = observations.screen().unit_type() center_y, center_x = (unit_type == self.unit_type_ids. terran_command_center()).nonzero() center_x = round(center_x.mean()) center_y = round(center_y.mean()) target = [center_x, center_y] self.command_center_selected = True return self.actions.select_point(target) elif self.command_center_selected and self.action_ids.morph_orbital_command( ) in observations.available_actions(): self.orbital_command_built = True return self.actions.morph_orbital_command() return self.actions.no_op()
def execute(self, observations: Observations) -> actions.FunctionCall: if self.done(observations): self._reset() elif self._train_action_id() in observations.available_actions(): self.already_trained = self.already_trained + 1 return self._train_action() elif not self.barracks_selected: unit_type = observations.screen().unit_type() unit_y, unit_x = ( unit_type == self.unit_type_ids.terran_barracks()).nonzero() if unit_y.any(): target = [int(unit_x.mean()), int(unit_y.mean())] self.barracks_selected = True return self.actions.select_point(target) return self.actions.no_op()
def _see_enemy_on_screen(self, observations: Observations) -> bool: enemy_y, enemy_x = (observations.screen().player_relative() == _PLAYER_ENEMY).nonzero() return enemy_y.any()