public static void GenerateNewTerrain(
        Terrain terrain,
        Color A, Color B,
        int smoothing, float slopeMultiplier,
        float scale_1, float height_1,
        float scale_2, float height_2,
        float scale_3, float height_3,
        float scale_4, float height_4)
    {
        //We want to actually generate the terrain now
        TerrainData terrainData = terrain.terrainData;

        int xRes = 513; int yRes = 513;

        float[,] heightmap = new float[xRes, yRes];

        x_offset = Random.Range(0, 10000);
        y_offset = Random.Range(0, 10000);

        for (int y_pos = 0; y_pos < yRes; y_pos++)
        {
            for (int x_pos = 0; x_pos < xRes; x_pos++)
            {
                heightmap[x_pos, y_pos]  = Mathf.PerlinNoise(x_pos / scale_1 + x_offset, y_pos / scale_1 + y_offset) * height_1;
                heightmap[x_pos, y_pos] += Mathf.PerlinNoise(x_pos / scale_2 + y_offset, y_pos / scale_2 + x_offset) * height_2;
                heightmap[x_pos, y_pos] -= Mathf.PerlinNoise(x_pos / scale_3 + x_offset, y_pos / scale_3 + y_offset) * height_3;
                heightmap[x_pos, y_pos] += Mathf.PerlinNoise(x_pos / scale_4 + x_offset, y_pos / scale_4 + y_offset) * height_4;
            }
        }

        terrainData.SetHeights(0, 0, heightmap);

        //Colour in the terrain based on slope
        Texture2D tex2D = new Texture2D(513, 513);

        for (int y = 0; y < terrainData.alphamapResolution; y++)
        {
            for (int x = 0; x < terrainData.alphamapResolution; x++)
            {
                tex2D.SetPixel(y, x, SlopeToColour(A, B, GetAverageSlopeAtPosition(heightmap, x, y, smoothing) * slopeMultiplier));
            }
        }

        //This is to convert the Texture2D that any sane person would use into the bizarre TerrainLayer array that Unity3D terrain textures use
        System.IO.File.WriteAllBytes("Assets/graphics/Terrain Texture.jpg", tex2D.EncodeToJPG());

        TerrainManager.SetTexture(terrainData, tex2D);

        //Now generate the terrain map
        System.IO.File.WriteAllBytes("Assets/graphics/Terrain Map.jpg", TerrainManager.GenerateTerrainMap(heightmap).EncodeToJPG());
    }