예제 #1
0
 public LevelLayout Generate()
 {
     #region DEBUG
     float startTime = 0;
     if (BigBoss.Debug.logging(Logs.LevelGenMain))
     {
         BigBoss.Debug.printHeader(Logs.LevelGenMain, "Generating Level: " + Depth);
         startTime = Time.realtimeSinceStartup;
     }
     #endregion
     Layout = new LevelLayout()
     {
         Random = Rand
     };
     Container = new LayoutObjectContainer();
     Log("Mod Rooms", false, GenerateRoomShells, ModRooms);
     Log("Cluster", true, ClusterRooms);
     Log("Place Rooms", true, PlaceRooms);
     Log("Confirm Connection", true, ConfirmConnection);
     Log("Place Stairs", true, PlaceStairs);
     Log("Confirm Edges", true, ConfirmEdges);
     #region DEBUG
     if (BigBoss.Debug.logging())
     {
         BigBoss.Debug.w(Logs.LevelGenMain, "Generate Level took: " + (Time.realtimeSinceStartup - startTime));
         Container.ToLog(Logs.LevelGenMain);
         BigBoss.Debug.printFooter(Logs.LevelGenMain, "Generating Level: " + Depth);
     }
     #endregion
     Layout.Grids.PutAll(Container.GetGrid());
     return(Layout);
 }
예제 #2
0
    protected void ConfirmConnection()
    {
        #region DEBUG
        if (BigBoss.Debug.logging(Logs.LevelGen))
        {
            BigBoss.Debug.printHeader(Logs.LevelGen, "Confirm Connections");
        }
        #endregion
        DrawAction <GenSpace> passTest = Draw.ContainedIn <GenSpace>(Path.PathTypes).Or(Draw.CanDrawDoor());
        var layoutCopy            = Container.GetGrid().Array;
        List <LayoutObject> rooms = new List <LayoutObject>(Layout.Rooms.Cast <LayoutObject>());
        var runningConnected      = Container2D <GenSpace> .CreateArrayFromBounds(layoutCopy);

        // Create initial queue and visited
        var startingRoom = rooms.Take();
        startingRoom.GetConnectedGrid().DrawAll(Draw.AddTo(runningConnected));
        Container2D <bool>          visited;
        Queue <Value2D <GenSpace> > queue;
        ConstructBFS(startingRoom, out queue, out visited);
        visited = visited.Array;
        LayoutObject fail;
        while (!startingRoom.ConnectedTo(rooms, out fail))
        {
            // Find start points
            #region DEBUG
            if (BigBoss.Debug.logging(Logs.LevelGen))
            {
                runningConnected.ToLog(Logs.LevelGen, "Source Setup");
                fail.ToLog(Logs.LevelGen, "Failed to connect to this");
            }
            #endregion
            Value2D <GenSpace> startPoint;
            Value2D <GenSpace> endPoint;
            LayoutObject       hit;
            if (!FindNextPathPoints(layoutCopy, runningConnected, out hit, passTest, queue, visited, out startPoint, out endPoint))
            {
                throw new ArgumentException("Cannot find path to fail room");
            }
            // Connect
            #region DEBUG
            if (BigBoss.Debug.logging(Logs.LevelGen))
            {
                layoutCopy.SetTo(startPoint, GridType.INTERNAL_RESERVED_CUR, Theme);
                layoutCopy.ToLog(Logs.LevelGen, "Largest after putting blocked");
                BigBoss.Debug.w(Logs.LevelGen, "Start Point:" + startPoint);
            }
            #endregion
            var hitConnected = hit.GetConnectedGrid();
            var stack        = layoutCopy.DrawJumpTowardsSearch(
                startPoint.x,
                startPoint.y,
                3,
                5,
                Draw.IsType <GenSpace>(GridType.NULL).And(Draw.Inside <GenSpace>(layoutCopy.Bounding.Expand(5))),
                passTest.And(Draw.ContainedIn(hitConnected)),
                Rand,
                endPoint,
                true);
            var path = new Path(stack);
            if (path.Valid)
            {
                #region DEBUG
                if (BigBoss.Debug.logging(Logs.LevelGen))
                {
                    path.Bake(null).ToLog(Logs.LevelGen, "Connecting Path");
                }
                #endregion
                path.Simplify();
                Point        first = path.FirstEnd;
                Point        second = path.SecondEnd;
                LayoutObject leaf1, leaf2;
                LayoutObject pathObj = path.Bake(Theme);
                Container.ConnectTo(first, pathObj, first, out leaf1, out pathObj);
                Container.ConnectTo(second, pathObj, second, out leaf2, out pathObj);
                if (leaf1[first].Type == GridType.Wall)
                {
                    leaf1.SetTo(first, GridType.Door, Theme);
                }
                if (leaf2[second].Type == GridType.Wall)
                {
                    leaf2.SetTo(second, GridType.Door, Theme);
                }
                foreach (var v in pathObj)
                {
                    layoutCopy[v] = v.val;
                    runningConnected.Put(v);
                    if (!visited[v])
                    {
                        queue.Enqueue(v);
                    }
                    visited[v] = true;
                }
                foreach (var p in path)
                {
                    layoutCopy.DrawAround(p.x, p.y, false, Draw.IsType <GenSpace>(GridType.NULL).IfThen(Draw.SetTo(pathObj, GridType.Floor, Theme).And(Draw.SetTo(GridType.Floor, Theme))));
                    layoutCopy.DrawCorners(p.x, p.y, new DrawAction <GenSpace>((arr, x, y) =>
                    {
                        if (!arr.IsType(x, y, GridType.NULL))
                        {
                            return(false);
                        }
                        return(arr.Cornered(x, y, Draw.IsType <GenSpace>(GridType.Floor)));
                    }).IfThen(Draw.SetTo(pathObj, GridType.Floor, Theme)));
                }
                Container.Objects.Add(pathObj);
                hitConnected.DrawAll(Draw.AddTo(runningConnected));
                #region DEBUG
                if (BigBoss.Debug.logging(Logs.LevelGen))
                {
                    Container.ToLog(Logs.LevelGen, "Final Connection");
                }
                #endregion
            }
            else
            {
                throw new ArgumentException("Cannot create path to hit room");
            }
        }
        #region DEBUG
        if (BigBoss.Debug.logging(Logs.LevelGen))
        {
            BigBoss.Debug.printFooter(Logs.LevelGen, "Confirm Connections");
        }
        #endregion
    }
예제 #3
0
    protected void ClusterAround(LayoutObjectContainer cluster, LayoutObject obj)
    {
        #region Debug
        if (BigBoss.Debug.logging(Logs.LevelGen))
        {
            BigBoss.Debug.printHeader("Cluster Around");
        }
        #endregion
        obj.ShiftOutside(cluster, new Point(1, 0), null, false, false);
        obj.Shift(-1, 0); // Shift to overlapping slightly
        MultiMap <bool> visited = new MultiMap <bool>();
        visited[0, 0] = true;
        ProbabilityList <ClusterInfo> shiftOptions = new ProbabilityList <ClusterInfo>();
        Queue <Point> shiftQueue = new Queue <Point>();
        shiftQueue.Enqueue(new Point());
        Container2D <GenSpace> clusterGrid = cluster.GetGrid();
        Container2D <GenSpace> objGrid     = obj.GetGrid();
        #region Debug
        if (BigBoss.Debug.logging(Logs.LevelGen))
        {
            var tmp = new MultiMap <GenSpace>();
            tmp.PutAll(obj.GetGrid());
            tmp.PutAll(cluster.GetGrid());
            tmp.ToLog(Logs.LevelGen, "Starting placement");
        }
        #endregion
        while (shiftQueue.Count > 0)
        {
            Point curShift = shiftQueue.Dequeue();
            #region Debug
            if (BigBoss.Debug.Flag(DebugManager.DebugFlag.FineSteps) && BigBoss.Debug.logging(Logs.LevelGen))
            {
                var tmpMap = new MultiMap <GenSpace>();
                tmpMap.PutAll(clusterGrid);
                tmpMap.PutAll(objGrid, curShift);
                tmpMap.ToLog(Logs.LevelGen, "Analyzing at shift " + curShift);
            }
            #endregion
            // Test if pass
            List <Point> intersectPoints = new List <Point>();
            if (objGrid.DrawAll((arr, x, y) =>
            {
                if (GridTypeEnum.EdgeType(arr[x, y].GetGridType()))
                {
                    GridType clusterType = clusterGrid[x + curShift.x, y + curShift.y].GetGridType();
                    if (clusterType == GridType.NULL)
                    {
                        return(true);
                    }
                    intersectPoints.Add(new Point(x, y));
                    return(GridTypeEnum.EdgeType(clusterType));
                }
                else
                {
                    return(!clusterGrid.Contains(x + curShift.x, y + curShift.y));
                }
            }) &&
                intersectPoints.Count > 0)
            { // Passed test
                // queue surrounding points
                visited.DrawAround(curShift.x, curShift.y, true, Draw.Not(Draw.EqualTo(true)).IfThen(Draw.AddTo <bool>(shiftQueue).And(Draw.SetTo(true))));

                #region Debug
                if (BigBoss.Debug.Flag(DebugManager.DebugFlag.FineSteps) && BigBoss.Debug.logging(Logs.LevelGen))
                {
                    BigBoss.Debug.w(Logs.LevelGen, "passed with " + intersectPoints.Count);
                }
                #endregion
                shiftOptions.Add(new ClusterInfo()
                {
                    Shift = curShift, Intersects = intersectPoints
                }, Math.Pow(intersectPoints.Count, 3));
            }
        }
        #region Debug
        if (BigBoss.Debug.logging(Logs.LevelGen))
        {
            shiftOptions.ToLog(Logs.LevelGen, "Shift options");
        }
        #endregion
        List <Point> clusterDoorOptions = new List <Point>();
        ClusterInfo  info;
        var          placed = new List <Value2D <GenSpace> >(0);
        while (shiftOptions.Take(Rand, out info))
        {
            clusterGrid.DrawPoints(info.Intersects, Draw.CanDrawDoor().IfThen(Draw.AddTo <GenSpace>(clusterDoorOptions)).Shift(info.Shift));
            #region Debug
            if (BigBoss.Debug.logging(Logs.LevelGen))
            {
                BigBoss.Debug.w(Logs.LevelGen, "selected " + info.Shift);
                var tmpMap = new MultiMap <GenSpace>();
                clusterGrid.DrawAll(Draw.CopyTo(tmpMap));
                objGrid.DrawAll(Draw.CopyTo(tmpMap, info.Shift));
                tmpMap.DrawPoints(info.Intersects, Draw.SetTo(GridType.INTERNAL_RESERVED_CUR, Theme).Shift(info.Shift));
                tmpMap.ToLog(Logs.LevelGen, "Intersect Points");
                tmpMap = new MultiMap <GenSpace>();
                clusterGrid.DrawAll(Draw.CopyTo(tmpMap));
                objGrid.DrawAll(Draw.CopyTo(tmpMap, info.Shift));
                tmpMap.DrawPoints(clusterDoorOptions, Draw.SetTo(GridType.Door, Theme));
                tmpMap.ToLog(Logs.LevelGen, "Cluster door options");
            }
            #endregion
            if (clusterDoorOptions.Count > 0)
            { // Cluster side has door options
                obj.Shift(info.Shift.x, info.Shift.y);
                placed = obj.PlaceSomeDoors(clusterDoorOptions, Theme, Rand);
                if (placed.Count != 0)
                { // Placed a door
                    foreach (Point p in placed)
                    {
                        LayoutObject clusterObj;
                        cluster.GetObjAt(p, out clusterObj);
                        obj.Connect(clusterObj);
                    }
                    break;
                }
                else
                {
                    #region Debug
                    if (BigBoss.Debug.logging(Logs.LevelGen))
                    {
                        BigBoss.Debug.w(Logs.LevelGen, "selected point failed to match " + info.Shift + ". Backing up");
                    }
                    #endregion
                    obj.Shift(-info.Shift.x, -info.Shift.y);
                }
            }
        }
        if (placed.Count == 0)
        {
            throw new ArgumentException("Could not cluster rooms");
        }
        #region Debug
        if (BigBoss.Debug.logging(Logs.LevelGen))
        {
            var tmpMap = new MultiMap <GenSpace>();
            tmpMap.PutAll(clusterGrid);
            tmpMap.PutAll(obj.GetGrid());
            tmpMap.ToLog(Logs.LevelGen, "Final setup " + info.Shift);
            BigBoss.Debug.printFooter("Cluster Around");
        }
        #endregion
    }