Exemplo n.º 1
0
 private void ChooseStartAndEndRooms()
 {
     _startRoom = _rooms[Utility.random.Next(_rooms.Count)];
     _endRoom   = _rooms[Utility.random.Next(_rooms.Count)];
     if (_startRoom.Equals(_endRoom))
     {
         ChooseStartAndEndRooms();
     }
     if (GetAdjacentRooms(_startRoom).Contains(_endRoom))
     {
         ChooseStartAndEndRooms();
     }
 }
Exemplo n.º 2
0
        private List <NewLevelGen_Room> GetLeastWeightPathBetweenRooms(NewLevelGen_Room source, NewLevelGen_Room target)
        {
            //Djikstra
            Dictionary <NewLevelGen_Room, int> distancesFromSource = new Dictionary <NewLevelGen_Room, int>();                              //List of distance from the source to room X
            Dictionary <NewLevelGen_Room, NewLevelGen_Room> previousNodeFromSource = new Dictionary <NewLevelGen_Room, NewLevelGen_Room>(); //List of previous node in optimal path from source
            List <NewLevelGen_Room> unoptimizedRooms = new List <NewLevelGen_Room>(_rooms);

            foreach (NewLevelGen_Room room in _rooms)
            {
                distancesFromSource.Add(room, int.MaxValue);
                previousNodeFromSource.Add(room, null);
            }

            distancesFromSource[source] = 0; //Distance from source to itself is 0

            while (unoptimizedRooms.Count > 0)
            {
                //Get unoptimizedRooms entry with shortest distance in distancesFromSource
                int shortest = int.MaxValue;
                NewLevelGen_Room nextRoom = unoptimizedRooms[0];
                foreach (NewLevelGen_Room room in unoptimizedRooms)
                {
                    if (distancesFromSource[room] < shortest)
                    {
                        shortest = distancesFromSource[room];
                        nextRoom = room;
                    }
                }
                unoptimizedRooms.Remove(nextRoom);
                if (shortest == int.MaxValue) //If we haven't found a linked node
                {
                    return(null);             //No path exists!
                }

                foreach (Tuple <NewLevelGen_Room, NewLevelGen_Room, int> adj in GetAdjacenciesForRoom(nextRoom))
                {
                    NewLevelGen_Room neighbor = adj.Item1;
                    if (neighbor.Equals(nextRoom))
                    {
                        neighbor = adj.Item2;
                    }

                    int newDistance = distancesFromSource[nextRoom] + adj.Item3;
                    if (newDistance < distancesFromSource[neighbor])
                    {
                        distancesFromSource[neighbor]    = newDistance;
                        previousNodeFromSource[neighbor] = nextRoom;
                    }
                }
            }

            List <NewLevelGen_Room> roomPath            = new List <NewLevelGen_Room>();
            NewLevelGen_Room        nextRoomInFinalPath = target;

            while (previousNodeFromSource[nextRoomInFinalPath] != null)
            {
                roomPath.Add(nextRoomInFinalPath);
                nextRoomInFinalPath = previousNodeFromSource[nextRoomInFinalPath];
            }
            roomPath.Add(source);
            roomPath.Reverse();
            return(roomPath);
        }