SebastianLague

Alternate room connection algorithm; no caching

Mar 30th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.20 KB | None | 0 0
  1. void ConnectClosestRooms(List<Room> allRooms) {
  2.  
  3.     List<Connection> connections = new List<Connection> ();
  4.     List<int> visitedRooms = new List<int> ();
  5.     List<int> unvisitedRooms = new List<int> ();
  6.  
  7.     visitedRooms.Add (0);
  8.     for (int i = 1; i < allRooms.Count; i++) {
  9.         unvisitedRooms.Add (i);
  10.     }
  11.  
  12.     // search until all rooms have been visited
  13.     while (unvisitedRooms.Count > 0) {
  14.         // Find best connection between visited and unvisited rooms
  15.         Connection bestConnection = new Connection ();
  16.         bestConnection.distance = int.MaxValue;
  17.  
  18.         for (int i = 0; i < visitedRooms.Count; i++) {
  19.             for (int j = 0; j < unvisitedRooms.Count; j++) {
  20.                 int roomA = visitedRooms [i];
  21.                 int roomB = unvisitedRooms [j];
  22.  
  23.                 for (int tileIndexA = 0; tileIndexA < allRooms [roomA].edgeTiles.Count; tileIndexA++) {
  24.                     for (int tileIndexB = 0; tileIndexB < allRooms [roomB].edgeTiles.Count; tileIndexB++) {
  25.                         int dX = allRooms [roomA].edgeTiles [tileIndexA].tileX - allRooms [roomB].edgeTiles [tileIndexB].tileX;
  26.                         int dY = allRooms [roomA].edgeTiles [tileIndexA].tileY - allRooms [roomB].edgeTiles [tileIndexB].tileY;
  27.                         int distanceBetweenRooms = dX * dX + dY * dY;
  28.  
  29.                         if (distanceBetweenRooms < bestConnection.distance) {
  30.                             bestConnection = new Connection (roomA, tileIndexA, roomB, tileIndexB, distanceBetweenRooms);
  31.                         }
  32.                     }
  33.                 }
  34.             }
  35.         }
  36.         connections.Add (bestConnection);
  37.         visitedRooms.Add (bestConnection.roomIndexB);
  38.         unvisitedRooms.Remove (bestConnection.roomIndexB);
  39.     }
  40.  
  41.     // Create passages
  42.     foreach (Connection connection in connections) {
  43.         Coord tileA = allRooms [connection.roomIndexA].edgeTiles [connection.tileA];
  44.         Coord tileB = allRooms [connection.roomIndexB].edgeTiles [connection.tileB];
  45.         CreatePassage (allRooms [connection.roomIndexA], allRooms [connection.roomIndexB], tileA, tileB);
  46.     }
  47. }
  48.  
  49.    
  50. struct Connection {
  51.     public int roomIndexA;
  52.     public int tileA;
  53.     public int roomIndexB;
  54.     public int tileB;
  55.     public int distance;
  56.  
  57.     public Connection (int roomIndexA, int tileA, int roomIndexB, int tileB, int distance)
  58.     {
  59.         this.roomIndexA = roomIndexA;
  60.         this.tileA = tileA;
  61.         this.roomIndexB = roomIndexB;
  62.         this.tileB = tileB;
  63.         this.distance = distance;
  64.     }
  65. }
Add Comment
Please, Sign In to add comment