Ejemplo n.º 1
0
        bool GenerateBranchInternal(Room prev, int depth, int maxDepth, bool doBranch)
        {
            if (depth >= maxDepth)
            {
                return(false);
            }

            var connPtNum = GetMaxConnectionPoints(prev);
            var seq       = Enumerable.Range(0, connPtNum).ToList();

            rand.Shuffle(seq);

            if (doBranch)
            {
                var numBranch = prev.NumBranches.Random(rand);
                numBranch -= prev.Edges.Count;
                for (int i = 0; i < numBranch; i++)
                {
                    var rm = template.CreateNormal(depth, prev);

                    Link?link = null;
                    foreach (var connPt in seq)
                    {
                        if ((link = PlaceRoom(prev, rm, connPt)) != null)
                        {
                            seq.Remove(connPt);
                            break;
                        }
                    }

                    if (link == null)
                    {
                        return(false);
                    }

                    Edge.Link(prev, rm, link.Value);
                    if (!GenerateBranchInternal(rm, depth + 1, maxDepth, false))
                    {
                        collision.Remove(rm);
                        Edge.UnLink(prev, rm);
                        return(false);
                    }
                    rm.Depth = depth;
                    rooms.Add(rm);
                }
            }
            else
            {
                while (prev.Edges.Count < prev.NumBranches.Begin)
                {
                    var rm = template.CreateNormal(depth, prev);

                    Link?link = null;
                    foreach (var connPt in seq)
                    {
                        if ((link = PlaceRoom(prev, rm, connPt)) != null)
                        {
                            seq.Remove(connPt);
                            break;
                        }
                    }

                    if (link == null)
                    {
                        return(false);
                    }

                    Edge.Link(prev, rm, link.Value);
                    if (!GenerateBranchInternal(rm, depth + 1, maxDepth, false))
                    {
                        collision.Remove(rm);
                        Edge.UnLink(prev, rm);
                        return(false);
                    }
                    rm.Depth = depth;
                    rooms.Add(rm);
                }
            }
            return(true);
        }