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); }
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 }
protected void ClusterRooms() { #region DEBUG if (BigBoss.Debug.logging(Logs.LevelGen)) { BigBoss.Debug.printHeader(Logs.LevelGen, "Cluster Rooms"); } #endregion List <LayoutObject> ret = new List <LayoutObject>(); int numClusters = Rand.Next(maxRoomClusters - minRoomClusters) + minRoomClusters; // Num clusters cannot be more than half num rooms if (numClusters > Objects.Count / 2) { numClusters = Objects.Count / 2; } List <LayoutObjectContainer> clusters = new List <LayoutObjectContainer>(); for (int i = 0; i < numClusters; i++) { clusters.Add(new LayoutObjectContainer()); } #region DEBUG if (BigBoss.Debug.logging(Logs.LevelGen)) { BigBoss.Debug.w(Logs.LevelGen, "Number of clusters: " + numClusters); } #endregion // Add two rooms to each foreach (LayoutObjectContainer cluster in clusters) { LayoutObject obj1 = (LayoutObject)Objects.Take(); LayoutObject obj2 = (LayoutObject)Objects.Take(); cluster.Objects.Add(obj1); ClusterAround(cluster, obj2); cluster.Objects.Add(obj2); #region DEBUG if (BigBoss.Debug.logging(Logs.LevelGen)) { cluster.ToLog(Logs.LevelGen); } #endregion } #region DEBUG if (BigBoss.Debug.logging(Logs.LevelGen)) { BigBoss.Debug.w(Logs.LevelGen, "Rooms left: " + Objects.Count); } #endregion // For remaining rooms, put into random clusters foreach (LayoutObject r in new List <ILayoutObject>(Objects)) { if (Rand.Percent(clusterProbability)) { LayoutObjectContainer cluster = clusters.Random(Rand); ClusterAround(cluster, r); cluster.Objects.Add(r); Objects.Remove(r); #region DEBUG if (BigBoss.Debug.logging(Logs.LevelGen)) { cluster.ToLog(Logs.LevelGen); } #endregion } } // Add Clusters to rooms list foreach (ILayoutObject cluster in clusters) { Objects.Add(cluster); } #region DEBUG if (BigBoss.Debug.logging(Logs.LevelGen)) { foreach (LayoutObjectContainer cluster in clusters) { cluster.ToLog(Logs.LevelGen); } BigBoss.Debug.printFooter(Logs.LevelGen, "Cluster Rooms"); } #endregion }