예제 #1
0
 def test_not_enough_score_space(self):
     world_map.TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR = 1
     grid = self._generate_grid(1)
     grid = [[MockCell(avatar='avatar')]]
     map = WorldMap(grid)
     map.update(1)
     self.assertEqual(len(list(map.score_cells())), 0)
예제 #2
0
 def test_not_enough_pickup_space(self):
     world_map.TARGET_NUM_PICKUPS_PER_AVATAR = 1
     grid = self._generate_grid(1)
     grid = [[MockCell(avatar='avatar')]]
     map = WorldMap(grid)
     map.reconstruct_interactive_state(1)
     self.assertEqual(len(list(map.pickup_cells())), 0)
예제 #3
0
 def test_pickup_spawn_chance(self):
     world_map.TARGET_NUM_PICKUPS_PER_AVATAR = 5
     world_map.PICKUP_SPAWN_CHANCE = 0
     grid = self._generate_grid()
     map = WorldMap(grid)
     map.update(1)
     self.assertEqual(len(list(map.pickup_cells())), 0)
예제 #4
0
 def test_scores_removed(self):
     world_map.SCORE_DESPAWN_CHANCE = 1
     grid = self._generate_grid()
     grid[0][1].generates_score = True
     map = WorldMap(grid)
     map.reconstruct_interactive_state(1)
     self.assertEqual(len(list(map.score_cells())), 0)
예제 #5
0
 def test_no_score_cells_generated_if_no_suitable_cells(self):
     world_map.TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR = 1
     grid = self._generate_grid(1, 1)
     grid[Location(0, 0)].avatar = 'avatar'
     map = WorldMap(grid)
     map.update(1)
     self.assertEqual(len(list(map.score_cells())), 0)
예제 #6
0
 def test_scores_removed(self):
     self.settings['SCORE_DESPAWN_CHANCE'] = 1
     grid = self._generate_grid()
     grid[Location(0, 1)].generates_score = True
     world_map = WorldMap(grid, self.settings)
     world_map.update(1)
     self.assertEqual(len(list(world_map.score_cells())), 0)
예제 #7
0
 def test_get_valid_cell(self):
     world_map = WorldMap(self._generate_grid(), self.settings)
     for x in (0, 1):
         for y in (0, 1):
             location = Location(x, y)
             self.assertEqual(
                 world_map.get_cell(location).location, location)
예제 #8
0
 def test_not_enough_score_space(self):
     self.settings['TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR'] = 1
     grid = self._generate_grid(1, 1)
     grid[Location(0, 0)].avatar = 'avatar'
     world_map = WorldMap(grid, self.settings)
     world_map.update(1)
     self.assertEqual(len(list(world_map.score_cells())), 0)
예제 #9
0
 def test_pickup_cells(self):
     cells = self._generate_cells()
     cells[0]['pickup'] = {'health_restored': 5}
     cells[8]['pickup'] = {'health_restored': 2}
     map = WorldMap(cells)
     self.assertLocationsEqual(map.pickup_cells(),
                               (Location(-1, -1), Location(1, 1)))
예제 #10
0
 def test_score_cells(self):
     cells = self._generate_cells()
     cells[0]['generates_score'] = True
     cells[5]['generates_score'] = True
     map = WorldMap(cells)
     self.assertLocationsEqual(map.score_cells(),
                               (Location(-1, -1), Location(0, 1)))
예제 #11
0
 def test_pickup_spawn_chance(self):
     self.settings['TARGET_NUM_PICKUPS_PER_AVATAR'] = 5
     self.settings['PICKUP_SPAWN_CHANCE'] = 0
     grid = self._generate_grid()
     world_map = WorldMap(grid, self.settings)
     world_map.update(1)
     self.assertEqual(len(list(world_map.pickup_cells())), 0)
예제 #12
0
 def test_scores_removed(self):
     world_map.SCORE_DESPAWN_CHANCE = 1
     grid = self._generate_grid()
     grid[Location(0, 1)].generates_score = True
     map = WorldMap(grid)
     map.update(1)
     self.assertEqual(len(list(map.score_cells())), 0)
예제 #13
0
 def test_not_enough_pickup_space(self):
     world_map.TARGET_NUM_PICKUPS_PER_AVATAR = 1
     grid = self._generate_grid(1)
     grid = [[MockCell(avatar='avatar')]]
     map = WorldMap(grid)
     map.reconstruct_interactive_state(1)
     self.assertEqual(len(list(map.pickup_cells())), 0)
예제 #14
0
 def test_scores_removed(self):
     world_map.SCORE_DESPAWN_CHANCE = 1
     grid = self._generate_grid()
     grid[0][1].generates_score = True
     map = WorldMap(grid)
     map.reconstruct_interactive_state(1)
     self.assertEqual(len(list(map.score_cells())), 0)
예제 #15
0
 def test_get_y_off_map(self):
     map = WorldMap(self._generate_grid())
     for x in (0, 1):
         with self.assertRaises(ValueError):
             map.get_cell(Location(x, -1))
         with self.assertRaises(ValueError):
             map.get_cell(Location(x, 2))
예제 #16
0
 def test_get_x_off_map(self):
     map = WorldMap(self._generate_grid())
     for y in (0, 1):
         with self.assertRaises(ValueError):
             map.get_cell(Location(-1, y))
         with self.assertRaises(ValueError):
             map.get_cell(Location(2, y))
예제 #17
0
 def test_not_enough_pickup_space(self):
     world_map.TARGET_NUM_PICKUPS_PER_AVATAR = 1
     grid = self._generate_grid(1, 1)
     grid[Location(0, 0)].generates_score = True
     map = WorldMap(grid)
     map.update(1)
     self.assertEqual(len(list(map.pickup_cells())), 0)
예제 #18
0
 def test_interactable_cells(self):
     cells = self._generate_cells()
     cells[0]["interactable"] = {"type": "health"}
     cells[8]["interactable"] = {"type": "damage_boost"}
     map = WorldMap(cells)
     self.assertLocationsEqual(map.interactable_cells(),
                               (Location(-1, -1), Location(1, 1)))
예제 #19
0
 def test_score_cells(self):
     cells = self._generate_cells()
     cells[0]["interactable"] = {"type": "score"}
     cells[8]["interactable"] = {"type": "score"}
     map = WorldMap(cells)
     self.assertLocationsEqual(map.score_cells(),
                               (Location(-1, -1), Location(1, 1)))
예제 #20
0
 def test_pickups_not_added_when_at_target(self):
     world_map.TARGET_NUM_PICKUPS_PER_AVATAR = 1
     grid = self._generate_grid()
     grid[Location(0, 1)].pickup = MockPickup()
     map = WorldMap(grid)
     map.update(1)
     self.assertEqual(len(list(map.pickup_cells())), 1)
     self.assertIn(grid[Location(0, 1)], map.pickup_cells())
예제 #21
0
 def test_pickups_not_added_when_at_target(self):
     world_map.TARGET_NUM_PICKUPS_PER_AVATAR = 1
     grid = self._generate_grid()
     grid[0][1].pickup = True
     map = WorldMap(grid)
     map.reconstruct_interactive_state(1)
     self.assertEqual(len(list(map.pickup_cells())), 1)
     self.assertIn(grid[0][1], map.pickup_cells())
예제 #22
0
 def test_scores_not_added_when_at_target(self):
     world_map.TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR = 1
     grid = self._generate_grid()
     grid[0][1].generates_score = True
     map = WorldMap(grid)
     map.reconstruct_interactive_state(1)
     self.assertEqual(len(list(map.score_cells())), 1)
     self.assertIn(grid[0][1], map.score_cells())
예제 #23
0
 def test_score_despawn_chance(self):
     world_map.TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR = 0
     grid = self._generate_grid()
     grid[0][1].generates_score = True
     map = WorldMap(grid)
     map.reconstruct_interactive_state(1)
     self.assertIn(grid[0][1], map.score_cells())
     self.assertEqual(len(list(map.score_cells())), 1)
예제 #24
0
    def test_grid_expand(self):
        world_map.TARGET_NUM_CELLS_PER_AVATAR = 5
        map = WorldMap(self._generate_grid())
        map.reconstruct_interactive_state(1)
        self.assertGridSize(map, 3)

        map.reconstruct_interactive_state(2)
        self.assertGridSize(map, 4)
예제 #25
0
 def test_interactable_cells(self):
     cells = self._generate_cells()
     cells[0]["interactable"] = {"health_restored": 5}
     cells[8]["interactable"] = {"health_restored": 2}
     map = WorldMap(cells)
     self.assertLocationsEqual(
         map.interactable_cells(), (Location(-1, -1), Location(1, 1))
     )
예제 #26
0
 def test_scores_not_added_when_at_target(self):
     world_map.TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR = 1
     grid = self._generate_grid()
     grid[0][1].generates_score = True
     map = WorldMap(grid)
     map.reconstruct_interactive_state(1)
     self.assertEqual(len(list(map.score_cells())), 1)
     self.assertIn(grid[0][1], map.score_cells())
예제 #27
0
 def test_pickups_not_added_when_at_target(self):
     world_map.TARGET_NUM_PICKUPS_PER_AVATAR = 1
     grid = self._generate_grid()
     grid[0][1].pickup = True
     map = WorldMap(grid)
     map.reconstruct_interactive_state(1)
     self.assertEqual(len(list(map.pickup_cells())), 1)
     self.assertIn(grid[0][1], map.pickup_cells())
예제 #28
0
 def test_all_cells(self):
     world_map = WorldMap(self._generate_grid(), self.settings)
     cell_names = [c.name for c in world_map.all_cells()]
     self.assertIn("A", cell_names)
     self.assertIn("B", cell_names)
     self.assertIn("C", cell_names)
     self.assertIn("D", cell_names)
     self.assertEqual(len(cell_names), 4)
예제 #29
0
 def test_score_despawn_chance(self):
     world_map.TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR = 0
     grid = self._generate_grid()
     grid[0][1].generates_score = True
     map = WorldMap(grid)
     map.reconstruct_interactive_state(1)
     self.assertIn(grid[0][1], map.score_cells())
     self.assertEqual(len(list(map.score_cells())), 1)
예제 #30
0
    def test_grid_expand(self):
        world_map.TARGET_NUM_CELLS_PER_AVATAR = 5
        map = WorldMap(self._generate_grid())
        map.update(1)
        self.assertGridSize(map, 3)

        map.update(2)
        self.assertGridSize(map, 4)
예제 #31
0
    def test_location_on_map(self):
        world_map = WorldMap(self._generate_grid(), self.settings)
        for x in (0, 1):
            for y in (0, 1):
                self.assertTrue(world_map.is_on_map(Location(x, y)))

        self.assertFalse(world_map.is_on_map(Location(2, 2)))
        self.assertFalse(world_map.is_on_map(Location(-1, 1)))
예제 #32
0
 def test_score_despawn_chance(self):
     world_map.TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR = 0
     grid = self._generate_grid()
     grid[Location(0, 1)].generates_score = True
     map = WorldMap(grid)
     map.update(1)
     self.assertIn(grid[Location(0, 1)], map.score_cells())
     self.assertEqual(len(list(map.score_cells())), 1)
예제 #33
0
 def test_all_cells(self):
     map = WorldMap(self._generate_grid())
     cell_names = [c.name for c in map.all_cells()]
     self.assertIn('A', cell_names)
     self.assertIn('B', cell_names)
     self.assertIn('C', cell_names)
     self.assertIn('D', cell_names)
     self.assertEqual(len(cell_names), 4)
예제 #34
0
 def test_scores_not_added_when_at_target(self):
     world_map.TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR = 1
     grid = self._generate_grid()
     grid[Location(0, 1)].generates_score = True
     map = WorldMap(grid)
     map.update(1)
     self.assertEqual(len(list(map.score_cells())), 1)
     self.assertIn(grid[Location(0, 1)], map.score_cells())
예제 #35
0
 def test_pickup_cells(self):
     pickup_cell1 = MockCell(pickup=MockPickup())
     pickup_cell2 = MockCell(pickup=MockPickup())
     no_pickup_cell = MockCell()
     grid = self._grid_from_list([[pickup_cell1, no_pickup_cell], [no_pickup_cell, pickup_cell2]])
     map = WorldMap(grid)
     cells = list(map.pickup_cells())
     self.assertIn(pickup_cell1, cells)
     self.assertIn(pickup_cell2, cells)
     self.assertEqual(len(cells), 2, "Non-pickup cells present")
예제 #36
0
 def test_score_cells(self):
     score_cell1 = MockCell(generates_score=True)
     score_cell2 = MockCell(generates_score=True)
     no_score_cell = MockCell()
     grid = self._grid_from_list([[score_cell1, no_score_cell], [no_score_cell, score_cell2]])
     map = WorldMap(grid)
     cells = list(map.score_cells())
     self.assertIn(score_cell1, cells)
     self.assertIn(score_cell2, cells)
     self.assertEqual(len(cells), 2, "Non-scoring cells present")
예제 #37
0
    def test_get_all_pickup_locations(self):
        pickup_cell = MockCell()
        pickup_cell.interactable = HealthPickup(pickup_cell)
        grid = self._grid_from_list([[pickup_cell, MockCell()],
                                     [MockCell(), MockCell()]])
        world_map = WorldMap(grid, self.settings)
        pickup_list = list(world_map.pickup_cells())

        self.assertEqual(len(pickup_list), 1)
        self.assertTrue(isinstance(pickup_list[0].interactable, ALL_PICKUPS))
예제 #38
0
    def test_get_all_score_locations(self):
        score_cell = MockCell()
        score_cell.interactable = ScoreLocation(score_cell)
        grid = self._grid_from_list([[score_cell, MockCell()],
                                     [MockCell(), MockCell()]])
        world_map = WorldMap(grid, self.settings)
        score_list = list(world_map.score_cells())

        self.assertEqual(len(score_list), 1)
        self.assertTrue(isinstance(score_list[0].interactable, ScoreLocation))
예제 #39
0
 def test_pickup_cells(self):
     pickup_cell1 = MockCell(pickup='pickup1')
     pickup_cell2 = MockCell(pickup='pickup')
     no_pickup_cell = MockCell()
     grid = [[pickup_cell1, no_pickup_cell], [no_pickup_cell, pickup_cell2]]
     map = WorldMap(grid)
     cells = list(map.pickup_cells())
     self.assertIn(pickup_cell1, cells)
     self.assertIn(pickup_cell2, cells)
     self.assertEqual(len(cells), 2, "Non-pickup cells present")
예제 #40
0
 def test_pickup_cells(self):
     pickup_cell1 = MockCell(pickup='pickup1')
     pickup_cell2 = MockCell(pickup='pickup')
     no_pickup_cell = MockCell()
     grid = [[pickup_cell1, no_pickup_cell], [no_pickup_cell, pickup_cell2]]
     map = WorldMap(grid)
     cells = list(map.pickup_cells())
     self.assertIn(pickup_cell1, cells)
     self.assertIn(pickup_cell2, cells)
     self.assertEqual(len(cells), 2, "Non-pickup cells present")
예제 #41
0
 def test_interactable_cells(self):
     pickup_cell1 = MockCell(interactable=MockPickup())
     pickup_cell2 = MockCell(interactable=MockPickup())
     no_pickup_cell = MockCell()
     grid = self._grid_from_list([[pickup_cell1, no_pickup_cell],
                                  [no_pickup_cell, pickup_cell2]])
     world_map = WorldMap(grid, self.settings)
     cells = list(world_map.interactable_cells())
     self.assertIn(pickup_cell1, cells)
     self.assertIn(pickup_cell2, cells)
     self.assertEqual(len(cells), 2, "Non-pickup cells present")
예제 #42
0
 def test_score_cells(self):
     score_cell1 = MockCell()
     score_cell1.interactable = ScoreLocation(score_cell1)
     score_cell2 = MockCell()
     score_cell2.interactable = ScoreLocation(score_cell2)
     no_score_cell = MockCell()
     grid = self._grid_from_list([[score_cell1, no_score_cell],
                                  [no_score_cell, score_cell2]])
     world_map = WorldMap(grid, self.settings)
     cells = list(world_map.score_cells())
     self.assertIn(score_cell1, cells)
     self.assertIn(score_cell2, cells)
     self.assertEqual(len(cells), 2, "Non-scoring cells present")
예제 #43
0
    def test_scores_added(self):
        world_map.TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR = 1
        map = WorldMap(self._generate_grid())
        map.update(1)
        self.assertEqual(len(list(map.score_cells())), 1)

        map.update(2)
        self.assertEqual(len(list(map.score_cells())), 2)
예제 #44
0
 def test_potential_spawns(self):
     spawnable1 = MockCell()
     spawnable2 = MockCell()
     score_cell = MockCell(generates_score=True)
     unhabitable = MockCell(habitable=False)
     filled = MockCell(avatar='avatar')
     grid = self._grid_from_list([[spawnable1, score_cell, unhabitable], [unhabitable, spawnable2, filled]])
     map = WorldMap(grid)
     cells = list(map.potential_spawn_locations())
     self.assertIn(spawnable1, cells)
     self.assertIn(spawnable2, cells)
     self.assertNotIn(score_cell, cells, "Score cells should not be spawns")
     self.assertNotIn(unhabitable, cells, "Unhabitable cells should not be spawns")
     self.assertNotIn(filled, cells, "Cells with avatars should not be spawns")
     self.assertEqual(len(cells), 2)
예제 #45
0
    def test_out_of_bounds_random_edge(self):
        map = WorldMap.generate_empty_map(3, 4, {})
        with self.assertRaisesRegexp(ValueError, 'Beyond range'):
            get_random_edge_index(map, rng=ConstantRng(-1))

        with self.assertRaisesRegexp(ValueError, 'Beyond range'):
            get_random_edge_index(map, rng=ConstantRng(6))
예제 #46
0
    def test_pickups_added(self):
        world_map.TARGET_NUM_PICKUPS_PER_AVATAR = 1
        world_map.PICKUP_SPAWN_CHANCE = 1
        map = WorldMap(self._generate_grid())
        map.update(1)
        self.assertEqual(len(list(map.pickup_cells())), 1)

        map.update(2)
        self.assertEqual(len(list(map.pickup_cells())), 2)
예제 #47
0
 def test_get_random_edge_index_can_give_all_possible(self):
     map = WorldMap.generate_empty_map(3, 4, {})
     get_random_edge_index(map, rng=ConstantRng(1))
     expected = frozenset((
         (0,  1), (1, 1),
         (-1, 0), (2, 0),
         (0, -1), (1, -1),
     ))
     actual = frozenset(get_random_edge_index(map, rng=ConstantRng(i))
                        for i in xrange(6))
     self.assertEqual(expected, actual)
예제 #48
0
def generate_map(height, width, obstacle_ratio=0.1):
    world_map = WorldMap.generate_empty_map(height, width)

    # We designate one non-corner edge cell as empty, to ensure that the map can be expanded
    always_empty_edge_x, always_empty_edge_y = get_random_edge_index(world_map)
    always_empty_location = Location(always_empty_edge_x, always_empty_edge_y)

    for cell in shuffled(world_map.all_cells()):
        if cell.location != always_empty_location and random.random() < obstacle_ratio:
            cell.habitable = False
            # So long as all habitable neighbours can still reach each other,
            # then the map cannot get bisected
            if not _all_habitable_neighbours_can_reach_each_other(cell, world_map):
                cell.habitable = True

    return world_map
예제 #49
0
    def get_map(self):
        height = self.settings['START_HEIGHT']
        width = self.settings['START_WIDTH']
        world_map = WorldMap.generate_empty_map(height, width, self.settings)

        # We designate one non-corner edge cell as empty, to ensure that the map can be expanded
        always_empty_edge_x, always_empty_edge_y = get_random_edge_index(world_map)
        always_empty_location = Location(always_empty_edge_x, always_empty_edge_y)

        for cell in shuffled(world_map.all_cells()):
            if cell.location != always_empty_location and random.random() < self.settings['OBSTACLE_RATIO']:
                cell.habitable = False
                # So long as all habitable neighbours can still reach each other,
                # then the map cannot get bisected
                if not _all_habitable_neighbours_can_reach_each_other(cell, world_map):
                    cell.habitable = True

        return world_map
예제 #50
0
    def test_get_random_edge_index(self):
        map = WorldMap.generate_empty_map(3, 4, {})
        self.assertEqual(
            (0, -1), get_random_edge_index(map, rng=ConstantRng(0)))
        self.assertEqual(
            (1, -1), get_random_edge_index(map, rng=ConstantRng(1)))
        self.assertEqual(
            (0, 1), get_random_edge_index(map, rng=ConstantRng(2)))
        self.assertEqual(
            (1, 1), get_random_edge_index(map, rng=ConstantRng(3)))
        self.assertEqual(
            (-1, 0), get_random_edge_index(map, rng=ConstantRng(4)))
        self.assertEqual(
            (2, 0), get_random_edge_index(map, rng=ConstantRng(5)))

        # Verify no out of bounds
        with self.assertRaisesRegexp(ValueError, 'Beyond range'):
            get_random_edge_index(map, rng=ConstantRng(-1))

        with self.assertRaisesRegexp(ValueError, 'Beyond range'):
            get_random_edge_index(map, rng=ConstantRng(6))
예제 #51
0
 def test_retrieve_negative(self):
     map = WorldMap(self._generate_grid(3, 3))
     self.assertTrue(map.is_on_map(Location(-1, -1)))