Ejemplo n.º 1
0
    def test_factory_dist(self):
        fact = functools.partial(Factory, 0, 0, 0, 0)

        a = fact(position=(0, 0))
        b = fact(position=(0, 1))
        self.assertEqual(1, factory_dist(a, b))
        self.assertEqual(0, factory_dist(b, b))

        a = fact(position=(-1, -1))
        b = fact(position=(1, 1))
        self.assertEqual(2, factory_dist(a, b))

        a = fact(position=(0, 0))
        b = fact(position=(0, 0.5))
        self.assertEqual(0, factory_dist(a, b))
Ejemplo n.º 2
0
    def test_units_move(self):
        unit_types = [Bomb, Troop]

        for unit_type in unit_types:
            source_fac = Factory(0, 1, 0, 0, (15, -10))
            dest_fac = Factory(1, 1, 0, 0, (-5, 20))

            unit = unit_type(10, source_fac, dest_fac)

            np.testing.assert_allclose(
                unit.get_position(),
                source_fac.position
            )
            self.assertTrue(unit.active)

            total_dist = factory_dist(source_fac, dest_fac)

            for i in range(1, total_dist):
                unit.move()
                self.assertEqual(i, unit.travelled)
                self.assertTrue(unit.active)
                # TODO: test position

            unit.move()
            self.assertFalse(unit.active)
            np.testing.assert_allclose(
                unit.get_position(),
                dest_fac.position
            )
Ejemplo n.º 3
0
 def all_links_ok(factories):
     """
     Returns true if all distances are between min_dist and max_dist.
     TODO: This is a very lazy implementation that should be improved.
     """
     for a, b in itertools.combinations(factories, 2):
         if not self.min_dist < factory_dist(a, b) < self.max_dist:
             return False
     return True
Ejemplo n.º 4
0
 def link_factories(self):
     """
     Create a list of factory-factory distances, mainly for exporting to
     JSON
     """
     self.links = []
     for a, b in itertools.combinations(self.factories, 2):
         dist = factory_dist(a, b)
         self.links.append((a.id, b.id, dist))
         self.links.append((b.id, a.id, dist))
Ejemplo n.º 5
0
    def __init__(self, strength, source, destination):
        self.strength = strength

        self.active = True

        # N.B. implement a jsonize that just outputs source, dest IDs
        self.source = source
        self.destination = destination

        self.team = self.source.team

        self.distance = factory_dist(source, destination)
        self.travelled = 0
Ejemplo n.º 6
0
    def test_troop_resolve(self):
        source_fac = Factory(0, -1, 0, 0, (15, -10))
        dest_fac = Factory(1, 1, 0, 0, (-5, 20))

        unit = Troop(10, source_fac, dest_fac)

        unit.travelled = factory_dist(source_fac, dest_fac)-1

        self.assertTrue(unit.active)

        unit.move()
        self.assertFalse(unit.active)

        self.assertEqual(dest_fac.occupying_troops, {-1: 10, 1: 0})
Ejemplo n.º 7
0
    def test_bomb_resolve(self):
        source_fac = Factory(0, 1, 0, 0, (15, -10))
        dest_fac = Factory(1, 1, 0, 0, (-5, 20))

        unit = Bomb(None, source_fac, dest_fac)

        unit.travelled = factory_dist(source_fac, dest_fac)-1

        self.assertTrue(unit.active)

        unit.move()
        self.assertFalse(unit.active)
        self.assertEqual(source_fac.bombs_arriving, 0)
        self.assertEqual(dest_fac.bombs_arriving, 1)