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)
def setUp(self): """ Sets up the JSON of the world state generated by the service file for testing. """ self.avatar_manager = self.DummyAvatarManager() CELLS = [ [ { "interactable": MockPickup("b"), "avatar": self.avatar_manager.avatars[0], }, {}, {}, ], [{}, { "habitable": False }, { "interactable": MockPickup("a") }], ] grid = { Location(x, y - 1): MockCell(Location(x, y - 1), **CELLS[x][y]) for y in range(3) for x in range(2) } grid[Location(0, 1)].interactable = ScoreLocation(grid[Location(0, 1)]) test_game_state = GameState(WorldMap(grid, {}), self.avatar_manager) self.world_state_json = test_game_state.serialize()
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))
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)
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)
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)
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)
def setup_world(self): avatar_manager = SimpleAvatarManager() CELLS = [ [ { 'pickup': MockPickup('b'), 'avatar': avatar_manager.avatars[0] }, {}, { 'generates_score': True }, ], [ {}, { 'habitable': False }, { 'pickup': MockPickup('a') }, ], ] grid = { Location(x, y - 1): MockCell(Location(x, y - 1), **CELLS[x][y]) for y in xrange(3) for x in xrange(2) } state_provider.set_world(GameState(WorldMap(grid, {}), avatar_manager)) return service.get_world_state()
def setUp(self): """ Sets up the JSON of the world state generated by the service file for testing. """ self.avatar_manager = self.DummyAvatarManager() CELLS = [ [ { 'pickup': MockPickup('b'), 'avatar': self.avatar_manager.avatars[0] }, {}, { 'generates_score': True }, ], [ {}, { 'habitable': False }, { 'pickup': MockPickup('a') }, ], ] grid = { Location(x, y - 1): MockCell(Location(x, y - 1), **CELLS[x][y]) for y in range(3) for x in range(2) } test_game_state = GameState(WorldMap(grid, {}), self.avatar_manager) self.world_state_json = test_game_state.serialise()
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)))
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)))
def apply_fog_of_war(world_map, avatar_wrapper): """ Takes a world state and an avatar and returns a personalised view of the world. :param world_map: the state of the game :param avatar_wrapper: the application's view of the avatar :return: a world state tailored to the given avatar """ location = avatar_wrapper.location no_fog_distance = avatar_wrapper.fog_of_war_modifier + world_map.get_no_fog_distance( ) partial_fog_distance = avatar_wrapper.fog_of_war_modifier + world_map.get_partial_fog_distance( ) lower_x = max(location.x - partial_fog_distance, 0) lower_y = max(location.y - partial_fog_distance, 0) upper_x = min(location.x + partial_fog_distance, world_map.num_cols - 1) upper_y = min(location.y + partial_fog_distance, world_map.num_rows - 1) x_range = upper_x - lower_x y_range = upper_y - lower_y grid = [[None for y in range(y_range + 1)] for x in range(x_range + 1)] for x in range(x_range + 1): for y in range(y_range + 1): cell_location = Location(x + lower_x, y + lower_y) if world_map.is_on_map(cell_location): x_dist = abs(cell_location.x - location.x) y_dist = abs(cell_location.y - location.y) if should_partially_fog(no_fog_distance, partial_fog_distance, x_dist, y_dist): grid[x][y] = partially_fog_cell( world_map.get_cell(cell_location)) else: grid[x][y] = world_map.get_cell(cell_location) return WorldMap(grid)
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)))
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)))
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)
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))
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)
def test_grid_expand(self): self.construct_simulation_runner([], []) settings = SETTINGS.copy() settings["TARGET_NUM_CELLS_PER_AVATAR"] = 5 self.simulation_runner.game_state.world_map = WorldMap( self._generate_grid(), settings) self.simulation_runner.update(1, self.simulation_runner.game_state) print(self.simulation_runner.game_state.world_map) assert self.simulation_runner.game_state.world_map.is_on_map( Location(-1, -1)) assert self.simulation_runner.game_state.world_map.is_on_map( Location(-1, 2)) assert self.simulation_runner.game_state.world_map.is_on_map( Location(2, 2)) assert self.simulation_runner.game_state.world_map.is_on_map( Location(2, -1)) self.assertGridSize(self.simulation_runner.game_state.world_map, 4) self.simulation_runner.update(4, self.simulation_runner.game_state) self.assertGridSize(self.simulation_runner.game_state.world_map, 6) assert self.simulation_runner.game_state.world_map.is_on_map( Location(0, 3)) assert self.simulation_runner.game_state.world_map.is_on_map( Location(3, 0)) assert self.simulation_runner.game_state.world_map.is_on_map( Location(-2, 0)) assert self.simulation_runner.game_state.world_map.is_on_map( Location(0, -2))
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)
def apply_fog_of_war(world_map, avatar_wrapper): """ Takes a world state and an avatar and returns a personalised view of the world. :param world_map: the state of the game :param avatar_wrapper: the application's view of the avatar :return: a world state tailored to the given avatar """ location = avatar_wrapper.location no_fog_distance = avatar_wrapper.fog_of_war_modifier + world_map.get_no_fog_distance( ) partial_fog_distance = avatar_wrapper.fog_of_war_modifier + world_map.get_partial_fog_distance( ) lower_x = max(location.x - partial_fog_distance, world_map.min_x()) lower_y = max(location.y - partial_fog_distance, world_map.min_y()) upper_x = min(location.x + partial_fog_distance, world_map.max_x()) upper_y = min(location.y + partial_fog_distance, world_map.max_y()) grid = {} for x in range(lower_x, upper_x + 1): for y in range(lower_y, upper_y + 1): cell_location = Location(x, y) if world_map.is_on_map(cell_location): x_dist = abs(cell_location.x - location.x) y_dist = abs(cell_location.y - location.y) if should_partially_fog(no_fog_distance, partial_fog_distance, x_dist, y_dist): grid[location] = partially_fog_cell( world_map.get_cell(cell_location)) else: grid[location] = world_map.get_cell(cell_location) return WorldMap(grid, world_map.settings)
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)
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)
def test_scores_applied(self): grid = self._generate_grid() avatar = DummyAvatar() grid[Location(1, 1)].generates_score = True grid[Location(1, 1)].avatar = avatar WorldMap(grid).update(1) self.assertEqual(avatar.score, 1)
def test_grid_doesnt_expand(self): self.construct_simulation_runner([], []) settings = SETTINGS.copy() settings["TARGET_NUM_CELLS_PER_AVATAR"] = 4 self.simulation_runner.game_state.world_map = WorldMap( self._generate_grid(), settings) self.simulation_runner.update(1, self.simulation_runner.game_state) self.assertGridSize(self.simulation_runner.game_state.world_map, 2)
def test_iter(self): grid = [ [MockCell(Location(-1, -1), name='A'), MockCell(Location(-1, 0), name='B'), MockCell(Location(-1, 1), name='C')], [MockCell(Location(0, -1), name='D'), MockCell(Location(0, 0), name='E'), MockCell(Location(0, 1), name='F')], [MockCell(Location(1, -1), name='E'), MockCell(Location(1, 0), name='G'), MockCell(Location(1, 1), name='H')], ] map = WorldMap(self._grid_from_list(grid)) self.assertEqual([list(column) for column in map], grid)
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())
def test_pickups_applied(self): grid = self._generate_grid() pickup = MockPickup() avatar = DummyAvatar() grid[Location(1, 1)].pickup = pickup grid[Location(1, 1)].avatar = avatar WorldMap(grid).update(1) self.assertEqual(pickup.applied_to, avatar)
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())
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)
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)