Beispiel #1
0
    public RoomComposite CreateDungeon(RoomFlow flow)
    {
        //Debug.Log("Creating dungeon!");
        ClearDungeon();
        int seed = Random.Range(0, int.MaxValue);

        //seed = 32024;
        //Random.InitState(seed);
        Debug.Log("Random seed: " + seed);

        //Debug.Log(flow.name);
        currentFlow = flow;
        currentFlow.Init();
        List <List <FlowNode> > cycles = FindCycles(currentFlow);

        cycles.Sort((a, b) => b.Count.CompareTo(a.Count)); // sort in decending order

        for (int numTries = 0; numTries < 100; numTries++)
        {
            if (TryGenerating(cycles))
            {
                Debug.Log("Created dungeon in " + numTries + " tries!");
                //dungeonComposite.PrintGrid();
                roomPlacer.PlaceComposite(dungeonComposite, 0, 0);
                decorationPlacer.PlaceDecorations();

                return(dungeonComposite);
            }
        }
        // if failed, dungeonComposite will still be null
        return(null);
    }
Beispiel #2
0
    public RoomComposite CreateDungeon()
    {
        RoomFlow flow = currentFlow;

        if (randomFlow || flows == null)
        {
            flow = flows[Random.Range(0, flows.Length)];
        }
        return(CreateDungeon(flow));
    }
Beispiel #3
0
    private List <List <FlowNode> > FindCycles(RoomFlow flow)
    {
        List <List <FlowNode> > cycles = new List <List <FlowNode> >();

        Dictionary <FlowNode, FlowNode> child2parent = new Dictionary <FlowNode, FlowNode>();
        HashSet <FlowNode> explored = new HashSet <FlowNode>();
        Stack <FlowNode>   frontier = new Stack <FlowNode>();

        frontier.Push(flow.verticies[0]);

        // DFS
        while (frontier.Count > 0)
        {
            FlowNode current = frontier.Pop();
            if (explored.Contains(current))
            {
                continue;
            }
            explored.Add(current);

            foreach (FlowNode adj in current.neighbors)
            {
                if (explored.Contains(adj))
                {
                    if (child2parent[current] == adj)
                    {
                        continue;
                    }
                    // create a cycle
                    // ** need to update if graph has one directional edges **
                    List <FlowNode> newCycle = new List <FlowNode>();
                    newCycle.Add(current);
                    FlowNode n = current;
                    while (n != adj)
                    {
                        n = child2parent[n];
                        if (current == null)
                        {
                            Debug.LogWarning("Null found while looking for cycles!");
                        }
                        newCycle.Add(n);
                    }
                    cycles.Add(newCycle);
                }
                else
                {
                    child2parent[adj] = current;
                    frontier.Push(adj);
                }
            }
        }

        return(cycles);
    }