Example #1
0
    private void SplitHorizontalAt(Stack <Region> regionsToSplit, Region beingSplitted, int splitY)
    {
        //Get the number of holes.
        int holes = Settings.HolesInLine(beingSplitted.Width + 1);

        if (holes < 1)
        {
            holes = 1;
        }
        if (holes >= beingSplitted.Width + 1)
        {
            Regions.Add(beingSplitted);
            return;
        }

        //First make the line.
        for (int i = beingSplitted.Left; i <= beingSplitted.Right; ++i)
        {
            Map[i, splitY] = true;
        }
        //Make sure the line isn't blocking a hole at the edge of the region.
        if (beingSplitted.Left > 0 && (!Map[beingSplitted.Left - 1, splitY] || Holes.Contains(new Location(beingSplitted.Left - 1, splitY))))
        {
            Map[beingSplitted.Left, splitY] = false;
            Holes.Add(new Location(beingSplitted.Left, splitY));
        }
        if (beingSplitted.Right < Map.GetLength(1) - 1 && (!Map[beingSplitted.Right + 1, splitY] || Holes.Contains(new Location(beingSplitted.Right + 1, splitY))))
        {
            Map[beingSplitted.Right, splitY] = false;
            Holes.Add(new Location(beingSplitted.Right, splitY));
        }
        //Now make holes.

        //Get the possible cells to put a hole in.
        List <int> cellXs = new List <int>(beingSplitted.Width + 1);

        for (int i = 0; i < beingSplitted.Width + 1; ++i)
        {
            cellXs.Add(i + beingSplitted.Left);
        }

        //Randomly pick from them.
        int randomIndex;

        for (int i = 0; i < holes; ++i)
        {
            randomIndex = MathF.R.Next(0, cellXs.Count);

            Map[cellXs[randomIndex], splitY] = false;
            Holes.Add(new Location(cellXs[randomIndex], splitY));

            cellXs.RemoveAt(randomIndex);
        }

        //Now split the region into two regions (assuming both regions are large enough). Move the regions around a bit to make sure the line we just made is not part of the regions.
        Region one = new Region(beingSplitted.TopLeft, new Location {
            X = beingSplitted.Right, Y = splitY - 1
        });
        Region two = new Region(new Location {
            X = beingSplitted.Left, Y = splitY + 1
        }, beingSplitted.BottomRight);

        if (one.Width >= 0 && one.Height >= 0 && one.Left >= 0 && one.Right < Map.GetLength(0) && one.Top >= 0 && one.Bottom < Map.GetLength(1))
        {
            regionsToSplit.Push(one);
        }
        if (two.Width >= 0 && two.Height >= 0 && two.Left >= 0 && two.Right < Map.GetLength(0) && two.Top >= 0 && two.Bottom < Map.GetLength(1))
        {
            regionsToSplit.Push(two);
        }
    }