def setUp(self): mock_proto_data1 = MockProtoData(tag=245346374, pos=Mock(x=-5, y=6, z=50), health=35, health_max=45, orders=[ Mock(ability_id=AbilityId.ATTACK, target_unit_tag=1337, progress=1.0) ]) mock_proto_data2 = MockProtoData( tag=245346375, pos=Mock(x=-2, y=7, z=50), orders=[ Mock(ability_id=AbilityId.MOVE, target_world_space_pos=Point2((0, 0)), progress=1.0) ]) mock_proto_data3 = MockProtoData( tag=245346376, pos=Mock(x=7, y=7, z=50), ) self.mock_game_state = MockGameState() self.marine1 = Unit(mock_proto_data1, self.mock_game_state) self.marine2 = Unit(mock_proto_data2, self.mock_game_state) self.marine3 = Unit(mock_proto_data3, self.mock_game_state) self.marines = Units([self.marine1, self.marine2, self.marine3], self.mock_game_state) self.emptyUnitsGroup = Units([], self.mock_game_state)
def _prepare_units(self): # Set of enemy units detected by own sensor tower, as blips have less unit information than normal visible units self.blips: Set[Blip] = set() self.units: Units = Units([], self) self.structures: Units = Units([], self) self.enemy_units: Units = Units([], self) self.enemy_structures: Units = Units([], self) self.mineral_field: Units = Units([], self) self.vespene_geyser: Units = Units([], self) self.resources: Units = Units([], self) self.destructables: Units = Units([], self) self.watchtowers: Units = Units([], self) self.all_units: Units = Units([], self) self.workers: Units = Units([], self) self.townhalls: Units = Units([], self) self.gas_buildings: Units = Units([], self) self.larva: Units = Units([], self) self.techlab_tags: Set[int] = set() self.reactor_tags: Set[int] = set() for unit in self.state.observation_raw.units: if unit.is_blip: self.blips.add(Blip(unit)) else: unit_type: int = unit.unit_type # Convert these units to effects: reaper grenade, parasitic bomb dummy, forcefield unit_obj = Unit(unit, self) self.units.append(unit_obj)
def test_unit_Marine(self): proto_data = Mock(spec=raw_pb.Unit, unit_type=UnitTypeId.MARINE, alliance=Alliance.Self.value, tag=245346374, pos=Mock(x=-5, y=6, z=50), health=35, health_max=45, energy=0, energy_max=0) game_data = Mock( spec=GameData, units={UnitTypeId.MARINE: "data"}, # TODO ) marine = Unit(proto_data, game_data) self.assertEqual(marine.type_id, UnitTypeId.MARINE) self.assertEqual(marine._type_data, "data") self.assertEqual(marine.alliance, Alliance.Self.value) self.assertTrue(marine.is_mine) self.assertEqual(marine.tag, 245346374) self.assertEqual(marine.position, Point2((-5, 6))) self.assertEqual(marine.position3d, Point3((-5, 6, 50))) self.assertEqual(marine.health, 35) self.assertEqual(marine.health_max, 45) self.assertEqual(marine.health_percentage, 35 / 45) self.assertEqual(marine.energy, 0) self.assertEqual(marine.energy_max, 0) self.assertEqual(marine.energy_percentage, 0)
def mock_unit(ai, type_id: UnitTypeId, position: Point2) -> Unit: proto_mock = mock.Mock() proto_mock.tag = randint(0, sys.maxsize) proto_mock.unit_type = type_id.value proto_mock.pos.x = position.x proto_mock.pos.y = position.y proto_mock.orders = [] proto_mock.buff_ids = [] if type_id in mineral_ids: proto_mock.mineral_contents = 1000 else: proto_mock.mineral_contents = 0 if type_id in ALL_GAS: proto_mock.vespene_contents = 1000 proto_mock.assigned_harvesters = 0 proto_mock.ideal_harvesters = 3 else: proto_mock.vespene_contents = 0 unit = Unit(proto_mock, ai) if type_id in {UnitTypeId.NEXUS}: proto_mock.assigned_harvesters = 0 proto_mock.ideal_harvesters = 16 ai.townhalls.append(unit) if type_id in ALL_GAS: ai.gas_buildings.append(unit) if unit.is_structure: proto_mock.health = 400 # Whatever proto_mock.health_max = 400 # Whatever proto_mock.shield = 400 proto_mock.shield_max = 400 ai.structures.append(unit) proto_mock.build_progress = 1 ai.all_own_units.append(unit) proto_mock.alliance = IS_MINE if type_id in {UnitTypeId.PROBE}: proto_mock.health = 20 proto_mock.health_max = 20 proto_mock.shield = 20 proto_mock.shield_max = 20 ai.units.append(unit) ai.workers.append(unit) ai.all_own_units.append(unit) proto_mock.alliance = IS_MINE ai.all_units.append(unit) return unit
def _prepare_units(self): # Set of enemy units detected by own sensor tower, as blips have less unit information than normal visible units self.blips: Set[Blip] = set() self.all_units: Units = Units([], self) self.units: Units = Units([], self) self.workers: Units = Units([], self) self.larva: Units = Units([], self) self.structures: Units = Units([], self) self.townhalls: Units = Units([], self) self.gas_buildings: Units = Units([], self) self.all_own_units: Units = Units([], self) self.enemy_units: Units = Units([], self) self.enemy_structures: Units = Units([], self) self.all_enemy_units: Units = Units([], self) self.resources: Units = Units([], self) self.destructables: Units = Units([], self) self.watchtowers: Units = Units([], self) self.mineral_field: Units = Units([], self) self.vespene_geyser: Units = Units([], self) self.placeholders: Units = Units([], self) self.techlab_tags: Set[int] = set() self.reactor_tags: Set[int] = set() worker_types: Set[UnitTypeId] = { UnitTypeId.DRONE, UnitTypeId.DRONEBURROWED, UnitTypeId.SCV, UnitTypeId.PROBE } index: int = 0 for unit in self.state.observation_raw.units: if unit.is_blip: self.blips.add(Blip(unit)) else: unit_type: int = unit.unit_type # Convert these units to effects: reaper grenade, parasitic bomb dummy, forcefield if unit_type in FakeEffectID: self.state.effects.add(EffectData(unit, fake=True)) continue unit_obj = Unit(unit, self, distance_calculation_index=index, base_build=self.base_build) index += 1 self.all_units.append(unit_obj) if unit.display_type == IS_PLACEHOLDER: self.placeholders.append(unit_obj) continue alliance = unit.alliance # Alliance.Neutral.value = 3 if alliance == 3: # XELNAGATOWER = 149 if unit_type == 149: self.watchtowers.append(unit_obj) # mineral field enums elif unit_type in mineral_ids: self.mineral_field.append(unit_obj) self.resources.append(unit_obj) # geyser enums elif unit_type in geyser_ids: self.vespene_geyser.append(unit_obj) self.resources.append(unit_obj) # all destructable rocks else: self.destructables.append(unit_obj) # Alliance.Self.value = 1 elif alliance == 1: self.all_own_units.append(unit_obj) unit_id: UnitTypeId = unit_obj.type_id if unit_obj.is_structure: self.structures.append(unit_obj) if unit_id in race_townhalls[self.race]: self.townhalls.append(unit_obj) elif unit_id in ALL_GAS or unit_obj.vespene_contents: # TODO: remove "or unit_obj.vespene_contents" when a new linux client newer than version 4.10.0 is released self.gas_buildings.append(unit_obj) elif unit_id in { UnitTypeId.TECHLAB, UnitTypeId.BARRACKSTECHLAB, UnitTypeId.FACTORYTECHLAB, UnitTypeId.STARPORTTECHLAB, }: self.techlab_tags.add(unit_obj.tag) elif unit_id in { UnitTypeId.REACTOR, UnitTypeId.BARRACKSREACTOR, UnitTypeId.FACTORYREACTOR, UnitTypeId.STARPORTREACTOR, }: self.reactor_tags.add(unit_obj.tag) else: self.units.append(unit_obj) if unit_id in worker_types: self.workers.append(unit_obj) elif unit_id == UnitTypeId.LARVA: self.larva.append(unit_obj) # Alliance.Enemy.value = 4 elif alliance == 4: self.all_enemy_units.append(unit_obj) if unit_obj.is_structure: self.enemy_structures.append(unit_obj) else: self.enemy_units.append(unit_obj) # Force distance calculation and caching on all units using scipy pdist or cdist if self.distance_calculation_method == 1: _ = self._pdist elif self.distance_calculation_method in {2, 3}: _ = self._cdist
def __init__(self, response_observation): self.response_observation = response_observation self.actions = response_observation.actions # successful actions since last loop self.action_errors = response_observation.action_errors # error actions since last loop # https://github.com/Blizzard/s2client-proto/blob/51662231c0965eba47d5183ed0a6336d5ae6b640/s2clientprotocol/sc2api.proto#L575 # TODO: implement alerts https://github.com/Blizzard/s2client-proto/blob/51662231c0965eba47d5183ed0a6336d5ae6b640/s2clientprotocol/sc2api.proto#L640 self.observation = response_observation.observation self.observation_raw = self.observation.raw_data self.dead_units: Set[ int] = self.observation_raw.event.dead_units # returns set of tags of units that died self.alerts = self.observation.alerts self.player_result = response_observation.player_result self.chat = response_observation.chat self.common: Common = Common(self.observation.player_common) # Area covered by Pylons and Warpprisms self.psionic_matrix: PsionicMatrix = PsionicMatrix.from_proto( self.observation_raw.player.power_sources) self.game_loop: int = self.observation.game_loop # 22.4 per second on faster game speed # https://github.com/Blizzard/s2client-proto/blob/33f0ecf615aa06ca845ffe4739ef3133f37265a9/s2clientprotocol/score.proto#L31 self.score: ScoreDetails = ScoreDetails(self.observation.score) self.abilities = self.observation.abilities # abilities of selected units self._blipUnits = [] self.own_units: Units = Units([]) self.enemy_units: Units = Units([]) self.mineral_field: Units = Units([]) self.vespene_geyser: Units = Units([]) self.resources: Units = Units([]) self.destructables: Units = Units([]) self.watchtowers: Units = Units([]) self.units: Units = Units([]) for unit in self.observation.raw_data.units: if unit.is_blip: self._blipUnits.append(unit) else: unit_obj = Unit(unit) self.units.append(unit_obj) alliance = unit.alliance # Alliance.Neutral.value = 3 if alliance == 3: unit_type = unit.unit_type # XELNAGATOWER = 149 if unit_type == 149: self.watchtowers.append(unit_obj) # mineral field enums elif unit_type in mineral_ids: self.mineral_field.append(unit_obj) self.resources.append(unit_obj) # geyser enums elif unit_type in geyser_ids: self.vespene_geyser.append(unit_obj) self.resources.append(unit_obj) # all destructable rocks else: self.destructables.append(unit_obj) # Alliance.Self.value = 1 elif alliance == 1: self.own_units.append(unit_obj) # Alliance.Enemy.value = 4 elif alliance == 4: self.enemy_units.append(unit_obj) self.upgrades: Set[UpgradeId] = { UpgradeId(upgrade) for upgrade in self.observation_raw.player.upgrade_ids } # Set of unit tags that died this step self.dead_units: Set[int] = { dead_unit_tag for dead_unit_tag in self.observation_raw.event.dead_units } # Set of enemy units detected by own sensor tower, as blips have less unit information than normal visible units self.blips: Set[Blip] = {Blip(unit) for unit in self._blipUnits} # self.visibility[point]: 0=Hidden, 1=Fogged, 2=Visible self.visibility: PixelMap = PixelMap( self.observation_raw.map_state.visibility, mirrored=True) # HSPARK 수정 시작 # self.creep[point]: 0=No creep, 1=creep # self.creep: PixelMap = PixelMap(self.observation_raw.map_state.creep, mirrored=True) self.creep: PixelMap = PixelMap(self.observation_raw.map_state.creep, mirrored=True, in_bits=True) # HSPARK 수정 끝 # Effects like ravager bile shot, lurker attack, everything in effect_id.py self.effects: Set[EffectData] = { EffectData(effect) for effect in self.observation_raw.effects } """ Usage:
def from_proto(cls, units, bot_object: BotAI): # pylint: disable=E1120 return cls((Unit(raw_unit, bot_object=bot_object) for raw_unit in units))