Exemplo n.º 1
0
    def test_place_on_random_ground_places_entity_on_random_ground(self):
        width, height = (10, 10)
        map = Map(width, height)
        fg = ForestGenerator(width, height)
        fg.generate_trees(map)

        e = Entity('a', (255, 0, 0))
        map.place_on_random_ground(e)

        self.assertEqual(map.tiles[e.x][e.y].is_walkable, True)
Exemplo n.º 2
0
    def test_generate_trees_fills_holes(self):

        # Generate a bunch of trees with a known seed that generates holes.
        # This is fragile, but there's no other way to test this.
        # This is valuable, because there's a ton of code/complexity behind
        # this (breadth-first search, etc.).
        width, height = (60, 40)
        map = Map(width, height)
        fg = ForestGenerator(width, height)
        pre_fill_num_trees = math.floor(ForestGenerator.TREE_PERCENTAGE *
                                        width * height)

        random.seed(1)
        fg.generate_trees(map)

        actual_num_trees = 0

        for y in range(height):
            for x in range(width):
                if map.tiles[x][y].is_walkable == False:
                    actual_num_trees += 1

        # Strictly more trees because of filled holes
        # With 60x40 and seed=1, fills 6 gaps with trees
        self.assertGreater(actual_num_trees, pre_fill_num_trees)
Exemplo n.º 3
0
    def test_render_recalculates_fov_when_asked(self):
        map_width = 10
        map_height = 10
        map = Map(map_width, map_height)

        # Player is at (0, 0)
        player = Player()

        # Mock the FOV tiles.
        light_radius = 5
        fov_tiles = []

        for i in range(light_radius):
            fov_tiles.append((i, 0))  # horizontal ray
            fov_tiles.append((0, i))  # vertical ray

        tdl_adapter = TdlAdapter("Test Window", map_width, map_height)
        tdl_adapter.calculate_fov = MagicMock(return_value=fov_tiles)

        renderer = MapRenderer(map, player, tdl_adapter)
        self.assertTrue(renderer.recompute_fov)
        renderer.render()  # calls calculate_fov

        self.assertFalse(renderer.recompute_fov)
        tdl_adapter.calculate_fov.reset_mock()  # reset call count to 0
        renderer.render()  # doesn't call calculate_fov
        tdl_adapter.calculate_fov.assert_not_called()

        renderer.recompute_fov = True
        renderer.render()

        tdl_adapter.calculate_fov.assert_called_with(
            player.x, player.y, map.is_walkable, MapRenderer.FOV_ALGORITHM,
            light_radius, MapRenderer.SHOULD_LIGHT_WALLS)
Exemplo n.º 4
0
    def test_render_marks_current_fov_as_explored(self):
        map_width = 10
        map_height = 10
        map = Map(map_width, map_height)

        # Player is at (0, 0)
        player = Player()

        # Mock the FOV tiles.
        light_radius = 5
        fov_tiles = []

        for i in range(light_radius):
            fov_tiles.append((i, 0))  # horizontal ray
            fov_tiles.append((0, i))  # vertical ray

        # Sanity check: it's not explored yet
        for (x, y) in fov_tiles:
            self.assertFalse(map.tiles[x][y].is_explored)

        tdl_adapter = TdlAdapter("Test Window", map_width, map_height)
        tdl_adapter.calculate_fov = MagicMock(return_value=fov_tiles)

        renderer = MapRenderer(map, player, tdl_adapter)
        renderer.render()

        # Just check straight horizontal/vertical, as per our expectation
        for (x, y) in fov_tiles:
            self.assertTrue(map.tiles[x][y].is_explored)
Exemplo n.º 5
0
    def test_is_walkable_returns_true_for_empty_ground_within_bounds(self):
        map = Map(10, 10)
        map.tiles[0][0].is_walkable = False

        self.assertFalse(map.is_walkable(0, 0))
        self.assertTrue(map.is_walkable(5, 5))
        # off-map? not walkable.
        self.assertFalse(map.is_walkable(-1, 6))
        self.assertFalse(map.is_walkable(150, 5))
        self.assertFalse(map.is_walkable(2, -17))
        self.assertFalse(map.is_walkable(3, 77))
Exemplo n.º 6
0
    def main(self):
        config = ConfigWatcher()

        # Hard-coded random seed for easier debugging
        random.seed(config.get("universeSeed"))

        # TODO: shouldn't use this directly here, probably.
        ui_adapter = TdlAdapter('Furusiya', Main.SCREEN_WIDTH,
                                Main.SCREEN_HEIGHT, Main.FPS_LIMIT)

        end_game = False

        map = Map(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT)
        fg = ForestGenerator(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT)
        fg.generate_trees(map)

        player = Player()
        map.place_on_random_ground(player)
        renderer = MapRenderer(map, player, ui_adapter)

        while not end_game:
            renderer.render()
            key = ui_adapter.wait_for_input()
            if (key.key == "ESCAPE" or key.char.lower() == 'q'):
                end_game = True
            elif (key.key == "UP" and map.is_walkable(player.x, player.y - 1)):
                player.y -= 1
                renderer.recompute_fov = True
            elif (key.key == "DOWN"
                  and map.is_walkable(player.x, player.y + 1)):
                renderer.recompute_fov = True
                player.y += 1
            elif (key.key == "LEFT"
                  and map.is_walkable(player.x - 1, player.y)):
                renderer.recompute_fov = True
                player.x -= 1
            elif (key.key == "RIGHT"
                  and map.is_walkable(player.x + 1, player.y)):
                renderer.recompute_fov = True
                player.x += 1

        config.dispose()
Exemplo n.º 7
0
    def test_generate_trees_generates_trees(self):
        width, height = (10, 10)
        map = Map(width, height)
        fg = ForestGenerator(width, height)
        expected_num_trees = math.floor(ForestGenerator.TREE_PERCENTAGE *
                                        width * height)

        fg.generate_trees(map)

        actual_num_trees = 0

        for y in range(height):
            for x in range(width):
                if map.tiles[x][y].is_walkable == False:
                    actual_num_trees += 1

        # might be more trees because of filled gaps between trees
        self.assertGreaterEqual(actual_num_trees, expected_num_trees)
Exemplo n.º 8
0
 def test_is_walkable_returns_false_if_entity_is_there(self):
     map = Map(10, 10)
     e = Entity('@', (0, 0, 0))
     map.place_on_random_ground(e)
     self.assertFalse(map.is_walkable(e.x, e.y))