public static EmptyGrid ProcessMap(EmptyGrid map, BinarySpacePartitioningSettings settings)
    {
        // Random Generator
        Random.State initialState = Random.state;
        if (settings.useFixedSeed)
        {
            Random.InitState(settings.seed.GetHashCode());
        }
        else
        {
            Random.InitState(Time.time.ToString().GetHashCode());
        }

        // Set root
        BSPLeaf root = new BSPLeaf(0, 0, map.width - 1, map.height - 1);

        // Space Partition
        root.Split(ref settings);

        // Get terminal leaves
        List <BSPLeaf> list = new List <BSPLeaf>();

        root.GetLeaves(ref list);

        List <Vector2> midpoints = new List <Vector2>();

        // Recursive Division
        if (settings.useOnlyRecursiveDivision)
        {
            // Fill initial
            for (int y = 0; y < map.height; y++)
            {
                for (int x = 0; x < map.width; x++)
                {
                    map.values[x, y] = Cell.CreateCell(CellType.Floor);
                }
            }
            DrawBSPRoom(ref map.values, ref midpoints, ref settings, root);
        }
        else
        {
            // Room placement on map
            foreach (BSPLeaf leaf in list)
            {
                DrawRectangularRoom(ref map.values, ref midpoints, ref settings, leaf);
            }
        }

        if (settings.hasCleanup)
        {
            CleanUpRoomPlacement(ref map.values, ref map.width, ref map.height);
        }

        Random.state = initialState;

        return(map);
    }
Beispiel #2
0
    public void Split(ref BinarySpacePartitioningSettings settings)
    {
        if (!AbleToSplit(ref settings))
        {
            return;
        }

        int whereToSplit;

        if (splitOrientationIsVertical)
        {
            if (settings.canShareSingleWall)
            {
                whereToSplit = Random.Range(0 + settings.minSplitSizeVertical - 1, SizeX - settings.minSplitSizeVertical + 1);
                Left         = new BSPLeaf(bounds.xMin, bounds.zMin, whereToSplit, SizeZ);
                Right        = new BSPLeaf(bounds.xMin + whereToSplit, bounds.zMin, SizeX - whereToSplit, SizeZ);
                if (CanSplit(whereToSplit, true, ref settings))
                {
                    Left.Split(ref settings);
                }
                if (CanSplit(SizeX - whereToSplit, true, ref settings))
                {
                    Right.Split(ref settings);
                }
            }
            else
            {
                whereToSplit = Random.Range(0 + settings.minSplitSizeVertical - 1, SizeX - settings.minSplitSizeVertical + 1);
                Left         = new BSPLeaf(bounds.xMin, bounds.zMin, whereToSplit, SizeZ);
                Right        = new BSPLeaf(bounds.xMin + whereToSplit + 1, bounds.zMin, SizeX - whereToSplit - 1, SizeZ);
                if (CanSplit(whereToSplit, true, ref settings))
                {
                    Left.Split(ref settings);
                }
                if (CanSplit(SizeX - whereToSplit - 1, true, ref settings))
                {
                    Right.Split(ref settings);
                }
            }
        }
        else
        {
            if (settings.canShareSingleWall)
            {
                whereToSplit = Random.Range(0 + settings.minSplitSizeHorizontal - 1, SizeZ - settings.minSplitSizeHorizontal + 1);
                Left         = new BSPLeaf(bounds.xMin, bounds.zMin, SizeX, whereToSplit);
                Right        = new BSPLeaf(bounds.xMin, bounds.zMin + whereToSplit, SizeX, SizeZ - whereToSplit);
                if (CanSplit(whereToSplit, false, ref settings))
                {
                    Left.Split(ref settings);
                }
                if (CanSplit(SizeZ - whereToSplit, false, ref settings))
                {
                    Right.Split(ref settings);
                }
            }
            else
            {
                whereToSplit = Random.Range(0 + settings.minSplitSizeHorizontal - 1, SizeZ - settings.minSplitSizeHorizontal + 1);
                Left         = new BSPLeaf(bounds.xMin, bounds.zMin, SizeX, whereToSplit);
                Right        = new BSPLeaf(bounds.xMin, bounds.zMin + whereToSplit + 1, SizeX, SizeZ - whereToSplit - 1);
                if (CanSplit(whereToSplit, false, ref settings))
                {
                    Left.Split(ref settings);
                }
                if (CanSplit(SizeZ - whereToSplit - 1, false, ref settings))
                {
                    Right.Split(ref settings);
                }
            }
        }
    }