// Sparsify the map by removing dead-end cells.
    private void Sparsify(VirtualMap map, VirtualMap vmapBelow)
    {
        // Compute the number of cells to remove as a percentage of the total number of cells in the map
        int noOfDeadEndCellsToRemove = (int)Math.Floor((decimal)sparsenessModifier / 100 * (map.ActualWidth * map.ActualHeight));

        if (verbose)
        {
            Console.WriteLine("Sparsify: removing  " + sparsenessModifier + "% i.e. " + noOfDeadEndCellsToRemove + " out of " + map.ActualWidth * map.ActualHeight + " cells");
        }

        int noOfRemovedCells = 0;
        IEnumerable <CellLocation> deads;

        while (noOfRemovedCells < noOfDeadEndCellsToRemove)
        {
            // We sweep and remove all current dead ends
            deads = map.DeadEndCellLocations;

            int currentlyRemovedCells = 0;
            foreach (CellLocation location in deads)
            {
                if (vmapBelow != null && location == vmapBelow.end)
                {
                    continue;                                                 // For multi-storey to work correctly, we do not remove the cell above the below's end
                }
                //				Console.WriteLine("Dead at " + starting_location);
                //				Console.WriteLine(map.CalculateDeadEndCorridorDirection(starting_location));
                map.CreateWall(location, map.CalculateDeadEndCorridorDirection(location));
                currentlyRemovedCells++;
                if (++noOfRemovedCells == noOfDeadEndCellsToRemove)
                {
                    break;
                }
            }
            if (currentlyRemovedCells == 0)
            {
                //				Console.WriteLine("We have no more dead ends!");
                break;  // No more dead endss
            }
            //			Console.WriteLine("We removed a total of " + noOfRemovedCells + " cells");
        }
    }
    // Open dead ends by linking them to rooms
    private void OpenDeadEnds(VirtualMap map)
    {
        //		Console.WriteLine("DEAD END MOD: " + openDeadEndModifier);
        if (openDeadEndModifier == 0)
        {
            return;
        }

        IEnumerable <CellLocation> deads = map.DeadEndCellLocations;

        foreach (CellLocation deadEnd in deads)
        {
            if (DungeonGenerator.Random.Instance.Next(1, 99) < openDeadEndModifier)
            {
                CellLocation currentLocation = deadEnd;
                //				int count=0;
                do
                {
                    // Initialize the direction picker not to select the dead-end corridor direction
                    DirectionPicker directionPicker = new DirectionPicker(map, currentLocation, map.CalculateDeadEndCorridorDirection(currentLocation));
                    //                    Debug.Log("We have a dead and " + directionPicker);
                    VirtualMap.DirectionType direction = directionPicker.GetNextDirection(map, currentLocation);
                    //					Debug.Log("We choose dir " + direction);
                    if (direction == VirtualMap.DirectionType.None)
                    {
                        throw new InvalidOperationException("Could not remove the dead end!");
                    }
                    //						Debug.Log("Cannot go that way!");
                    else
                    {
                        // Create a corridor in the selected direction
                        currentLocation = map.CreateCorridor(currentLocation, direction);
                    }
                    //					count++;
                } while (map.IsDeadEnd(currentLocation) && currentLocation != deadEnd); // Stop when you intersect an existing corridor, or when you end back to the starting cell (that means we could not remove the dead end, happens with really small maps
                //				Debug.Log("Dead end removed");
            }
        }
    }