コード例 #1
0
    async def pylonLocations(self):
        #find positions around the nexus for pylons, cannons and shields.
        #front = self.unit.position + Point2((cos(self.unit.facing), sin(self.unit.facing))) * 2

        #find the edge of the minerals around us.
        if self.game.mineral_field.closer_than(15, self.unit):

            mins = self.game.mineral_field.closer_than(15, self.unit)
            vasp = self.game.vespene_geyser.closer_than(15, self.unit)
            mf = Units((mins + vasp), self.game)
            f_distance = 0
            mineral_1 = None
            mineral_2 = None
            if mf:
                for mineral in mf:
                    #loop other minerals and find the 2 minerals that are furthest apart.
                    for n_mineral in mf:
                        #make sure it's not the same mineral.
                        if mineral.position == n_mineral.position:
                            continue
                        #get the distance between the 2.
                        t_dist = mineral.position3d.distance_to(
                            n_mineral.position3d)
                        if t_dist > f_distance:
                            mineral_1 = mineral
                            mineral_2 = n_mineral
                            f_distance = t_dist

            self.mineral1 = mineral_1
            self.mineral2 = mineral_2

            nf = [mineral_1, mineral_2]
            if len(nf) == 0:
                return
            center_pos = Point2((sum([item.position.x for item in nf]) / len(nf), \
              sum([item.position.y for item in nf]) / len(nf)))
            nexdis = self.unit.distance_to(center_pos)
            fmost = mf.furthest_to(center_pos)

            self.p1 = self.unit.position.towards(center_pos, -5.5)
            self.p2 = self.unit.position.towards(center_pos, 7)
            self.p3 = fmost.position.towards(self.p1, (3))
            fclose = mf.furthest_to(self.p3)
            self.p4 = fclose.position.towards(self.p1, (3))

            self.p5 = self.unit.position.towards(
                self.midpoint(self.p1, self.p4), 9)
            self.p6 = self.unit.position.towards(
                self.midpoint(self.p1, self.p3), 9)
コード例 #2
0
 def _get_builder(self, location=None):
     ws = self.bot.workers.gathering
     if ws:  # if workers found
         not_scouts = Units([w for w in ws if self.scouting_worker is None or w.tag != self.scouting_worker.tag], self.bot.game_data())
         if not_scouts.amount > 0:
             if location is None:
                 return not_scouts.furthest_to(not_scouts.center)
             else:
                 return not_scouts.closest_to(location)
     return None
コード例 #3
0
class TestUnits(unittest.TestCase):

    # @classmethod
    # def setUpClass(cls):
    #     pass

    # @classmethod
    # def tearDownClass(cls):
    #     pass

    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 tearDown(self):
        # unnecessary here
        del self.marine1
        del self.marine2
        del self.marine3
        del self.marines
        del self.mock_game_state

    def test_amount(self):
        self.assertEqual(self.marines.amount, 3)
        self.assertEqual(self.emptyUnitsGroup.amount, 0)

    def test_empty(self):
        self.assertFalse(self.marines.empty)
        self.assertTrue(self.emptyUnitsGroup.empty)

    def test_exists(self):
        self.assertTrue(self.marines.exists)
        self.assertFalse(self.emptyUnitsGroup.exists)

    def test_find_by_tag(self):
        self.assertEqual(self.marines.find_by_tag(245346374), self.marine1)
        self.assertIsNone(self.marines.find_by_tag(245346))

    def test_first(self):
        self.assertEqual(self.marines.first, self.marine1)

    def test_random(self):
        self.assertTrue(
            self.marines.random in [self.marine1, self.marine2, self.marine3])

    def test_closest_distance_to(self):
        self.assertEqual(self.marines.closest_distance_to(Point2((10, 10))),
                         (3**2 + 3**2)**0.5)

    def test_closest_to(self):
        self.assertEqual(self.marines.closest_to(Point2((10, 10))),
                         self.marine3)

    def test_furthest_to(self):
        self.assertEqual(self.marines.furthest_to(Point2((10, 10))),
                         self.marine1)

    def test_closer_than(self):
        self.assertEqual(self.marines.closer_than(20, Point2((10, 10))),
                         self.marines)
        self.assertEqual(self.marines.closer_than(6, Point2((10, 10))),
                         Units([self.marine3], self.mock_game_state))
        self.assertEqual(self.marines.closer_than(2, Point2((10, 10))),
                         self.emptyUnitsGroup)

    def test_tags_in(self):
        self.assertEqual(
            self.marines.tags_in({245346374, 245346375}),
            Units([self.marine1, self.marine2], self.mock_game_state))
        self.assertEqual(self.marines.tags_in({}), self.emptyUnitsGroup)

    def test_tags_not_in(self):
        self.assertEqual(self.marines.tags_not_in({}), self.marines)
        self.assertEqual(
            self.marines.tags_not_in({245346374}),
            Units([self.marine2, self.marine3], self.mock_game_state))

    def test_of_type(self):
        self.assertEqual(self.marines.of_type(UnitTypeId.MARINE), self.marines)
        self.assertEqual(self.marines.of_type([UnitTypeId.MARINE]),
                         self.marines)

    def test_exclude_type(self):
        self.assertEqual(self.marines.exclude_type([UnitTypeId.MARINE]),
                         self.emptyUnitsGroup)

    def test_tags(self):
        self.assertSetEqual(self.marines.tags, {u.tag for u in self.marines})

    def test_noqueue(self):
        self.assertEqual(self.marines.noqueue,
                         Units([self.marine3], self.mock_game_state))

    def test_idle(self):
        self.assertEqual(self.marines.idle,
                         Units([self.marine3], self.mock_game_state))

    def test_owned(self):
        self.assertEqual(self.marines.owned, self.marines)

    def test_enemy(self):
        self.assertEqual(self.marines.enemy, self.emptyUnitsGroup)