public override void Generate()
    {
        dungeonPerformance = new StreamWriter("dungeon.csv");
        dungeonPerformance.WriteLine("generation,non-linearity");
        int       generation = 0, currentNonlinearity = int.MaxValue;
        int       bestAttempt = 0;
        MZDungeon currentBest = null;

        while (currentNonlinearity > 12 && generation < maxGenerations)
        {
            generation++;
            //Debug.Log("Generation: " + generation);
            base.Generate();
            //Debug.Log("Method base.Generate() has finished running, now measuring non-linearity.");
            int nonlinearity = MeasureNonlinearity();
            Debug.Log("Dungeon generation " + generation + " nonlinearity: " + nonlinearity);
            if (nonlinearity < currentNonlinearity)
            {
                currentNonlinearity = nonlinearity;
                bestAttempt         = generation;
                currentBest         = dungeon;
            }
            dungeonPerformance.WriteLine(generation + "," + currentNonlinearity);
        }
        dungeonPerformance.WriteLine(bestAttempt + "," + currentNonlinearity);
        dungeonPerformance.Close();
        Debug.Assert(currentBest != null);
        Debug.Log("Chose " + bestAttempt + " nonlinearity: " + currentNonlinearity);

        dungeon = currentBest;
    }
 public AStar(AStarHelper aStarHelper, int start, int goal, MZDungeon dungeon)
 {
     this.aStarHelper = aStarHelper;
     this.start       = start;
     this.goal        = goal;
     this.dungeon     = dungeon;
     this.current     = start;
     openSet          = new Dictionary <int, float>();
     closedSet        = new List <int>();
 }
    public void Generate()
    {
        int attempt = 0;

        while (true)
        {
            try {
                KeyLevelRoomMapping levels;
                int roomsPerLock;
                if (constraints.GetMaxKeys() > 0)
                {
                    roomsPerLock = constraints.GetMaxRooms() /
                                   constraints.GetMaxKeys();
                }
                else
                {
                    roomsPerLock = constraints.GetMaxRooms();
                }

                bool keepTrying = true;
                levels = null;
                while (keepTrying)
                {
                    dungeon = new MZDungeon();

                    // Maps keyLevel -> MZRooms that were created when lockCount had that
                    // value
                    levels = new KeyLevelRoomMapping(constraints.GetMaxKeys());

                    // Create the entrance to the dungeon:
                    InitEntranceRoom(levels);

                    try {
                        // Fill the dungeon with rooms:
                        PlaceRooms(levels, roomsPerLock);
                        keepTrying = false;
                    } catch (OutOfRoomsException e) {
                        // We can run out of rooms where certain links have
                        // predetermined locks. Example: if a river bisects the
                        // map, the keyLevel for rooms in the river > 0 because
                        // crossing water requires a key. If there are not
                        // enough rooms before the river to build up to the
                        // key for the river, we've run out of rooms.
                        if (debug)
                        {
                            Debug.Log("Ran out of rooms. roomsPerLock was " + roomsPerLock);
                        }
                        roomsPerLock = roomsPerLock * constraints.GetMaxKeys() /
                                       (constraints.GetMaxKeys() + 1);
                        if (debug)
                        {
                            Debug.Log("roomsPerLock is now " + roomsPerLock);
                        }

                        if (roomsPerLock == 0)
                        {
                            throw new MZGenerationFailureException(
                                      "Failed to place rooms. Have you forgotten to disable boss-locking?");
                            // If the boss room is locked, the final key is used
                            // only for the boss room. So if the final key is
                            // also used to cross the river, rooms cannot be
                            // placed.
                        }
                    }
                }

                // Place the boss and goal rooms:
                PlaceBossGoalRooms(levels);

                // Place switches and the locks that require it:
                PlaceSwitches();

                ComputeIntensity(levels);

                // Place the keys within the dungeon:
                PlaceKeys(levels);

                if (levels.KeyCount() - 1 != constraints.GetMaxKeys())
                {
                    throw new RetryException();
                }

                // Make the dungeon less tree-like:
                Graphify();

                CheckAcceptable();
                return;
            } catch (RetryException e) {
                if (++attempt > maxRetries)
                {
                    throw new MZGenerationFailureException("MZDungeon generator failed", e);
                }
                if (debug)
                {
                    Debug.Log("Retrying dungeon generation...");
                }
            }
        }
    }
    private List <int> astar(int start, int goal, int keyLevel, MZDungeon dungeon)
    {
        AStar <int> astar = new AStar <int>(new AStarHelper(dungeon, keyLevel), start, goal, dungeon);

        return(astar.Solve());
    }
 public AStarHelper(MZDungeon _dungeon, int _keyLevel)
 {
     this.dungeon  = _dungeon;
     this.keyLevel = _keyLevel;
 }