Advertisement
SebastianLague

Alternate room connection algorithm; caching

Mar 30th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. void ConnectClosestRooms(List<Room> allRooms) {
  2.     List<Connection> connections = new List<Connection> ();
  3.     Dictionary<Coord, Connection> connectionCache = new Dictionary<Coord, Connection>();
  4.  
  5.     // lists representing indices of visited/unvisited rooms
  6.     List<int> visitedRooms = new List<int> ();
  7.     List<int> unvisitedRooms = new List<int> ();
  8.  
  9.     // populate lists
  10.     visitedRooms.Add (0);
  11.     for (int i = 1; i < allRooms.Count; i++) {
  12.         unvisitedRooms.Add (i);
  13.     }
  14.  
  15.  
  16.     // search until all rooms have been visited
  17.     while (unvisitedRooms.Count > 0) {
  18.  
  19.         // Find best connection between visited and unvisited rooms
  20.         Connection bestConnection = new Connection ();
  21.         bestConnection.distance = int.MaxValue;
  22.         Connection other = new Connection ();
  23.  
  24.         for (int i = 0; i < visitedRooms.Count; i++) {
  25.             for (int j = 0; j < unvisitedRooms.Count; j++) {
  26.                 int roomA = visitedRooms [i];
  27.                 int roomB = unvisitedRooms [j];
  28.                 Coord key = new Coord (roomA, roomB);
  29.  
  30.                 Connection bestLocalConnection = new Connection ();
  31.                 bestLocalConnection.distance = int.MaxValue;
  32.        
  33.                 if (connectionCache.ContainsKey (key)) {
  34.                     bestLocalConnection = connectionCache [key];
  35.                 }
  36.                 else {
  37.                     // search all tiles to find shortest connection between room A and B
  38.                     for (int tileIndexA = 0; tileIndexA < allRooms [roomA].edgeTiles.Count; tileIndexA++) {
  39.                         for (int tileIndexB = 0; tileIndexB < allRooms [roomB].edgeTiles.Count; tileIndexB++) {
  40.                             int dX = allRooms [roomA].edgeTiles [tileIndexA].tileX - allRooms [roomB].edgeTiles [tileIndexB].tileX;
  41.                             int dY = allRooms [roomA].edgeTiles [tileIndexA].tileY - allRooms [roomB].edgeTiles [tileIndexB].tileY;
  42.                             int distanceBetweenRooms = dX * dX + dY * dY;
  43.  
  44.                             if (distanceBetweenRooms < bestLocalConnection.distance) {
  45.                                 bestLocalConnection = new Connection (roomA, tileIndexA, roomB, tileIndexB, distanceBetweenRooms);
  46.                             }
  47.                         }
  48.                     }
  49.                            
  50.                     connectionCache.Add (key, bestLocalConnection);
  51.                 }
  52.  
  53.                 if (bestLocalConnection.distance < bestConnection.distance) {
  54.                     bestConnection = bestLocalConnection;
  55.                 }
  56.            
  57.             }
  58.         }
  59.         connections.Add (bestConnection);
  60.         visitedRooms.Add (bestConnection.roomIndexB);
  61.         unvisitedRooms.Remove (bestConnection.roomIndexB);
  62.     }
  63.  
  64.  
  65.     // Create passages
  66.     foreach (Connection connection in connections) {
  67.         Coord tileA = allRooms [connection.roomIndexA].edgeTiles [connection.tileA];
  68.         Coord tileB = allRooms [connection.roomIndexB].edgeTiles [connection.tileB];
  69.         CreatePassage (allRooms [connection.roomIndexA], allRooms [connection.roomIndexB], tileA, tileB);
  70.     }
  71.  
  72. }
  73.    
  74. struct Connection {
  75.     public int roomIndexA;
  76.     public int tileA;
  77.     public int roomIndexB;
  78.     public int tileB;
  79.     public int distance;
  80.  
  81.     public Connection (int roomIndexA, int tileA, int roomIndexB, int tileB, int distance)
  82.     {
  83.         this.roomIndexA = roomIndexA;
  84.         this.tileA = tileA;
  85.         this.roomIndexB = roomIndexB;
  86.         this.tileB = tileB;
  87.         this.distance = distance;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement