예제 #1
0
    public static bool ConnectTo(this ILayoutObject obj1, Point pt1, ILayoutObject obj2, Point pt2, out LayoutObject retObj1, out LayoutObject retObj2)
    {
        if (obj1 is LayoutObjectContainer)
        {
            if (!((LayoutObjectContainer)obj1).GetObjAt(pt1, out retObj1))
            {
                retObj2 = null;
                return(false);
            }
        }
        else
        {
            retObj1 = (LayoutObject)obj1;
        }

        if (obj2 is LayoutObjectContainer)
        {
            if (!((LayoutObjectContainer)obj2).GetObjAt(pt2, out retObj2))
            {
                return(false);
            }
        }
        else
        {
            retObj2 = (LayoutObject)obj2;
        }
        retObj1.Connect(retObj2);
        retObj2.Connect(retObj1);
        return(true);
    }
예제 #2
0
    public bool FindAndConnect(LayoutObject obj1, Point connectPt)
    {
        Point        pt = new Value2D <GridType>(connectPt.x + obj1.ShiftP.x, connectPt.y + obj1.ShiftP.y);
        LayoutObject obj;

        if (GetObjAt(pt, out obj))
        {
            obj1.Connect(obj);
            return(true);
        }
        return(false);
    }
예제 #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
    }