public int addRoom(DungeonDir dir, Random rand) {
    if (!canGenerateRoom(dir)) return 0;

    int prevX = x, prevY = y;

    if (move(dir, 2)) {
      addRoomAt(x, y);
      elementList.getAt(x, y).connect(dir.reversed());
      DungeonElement prevElement = elementList.getAt(prevX, prevY);
      if (prevElement != null) prevElement.connect(dir);

      RoomCombo shape = DungeonElementType.RoomCombo.random(rand);

      if (shape != RoomCombo.SINGLE) {
        List<byte[]> rooms = new ArrayList<>(4);
        byte origX = x, origY = y;

        switch (shape) {
          case LONG:
            switch (rand.nextInt(3)) {
              case 0:
                dir = dir.rotatedLeft();
                break;
              case 1:
                dir = dir.rotatedRight();
                break;
              default:
            }

            move(dir, 1);
            if (canGenerateRoom(dir)) {
              move(dir, 2);
              rooms.add(new byte[] {x, y});
            }

            break;

          case TURN:
          case BLOCK:
            boolean turnWay = rand.nextBoolean();

            for (int a = 0; a < (shape == RoomCombo.TURN ? 2 : 3); a++) {
              move(dir, 1);
              if (!canGenerateRoom(dir)) {
                rooms.clear();
                break;
              }

              move(dir, 2);
              rooms.add(new byte[] {x, y});
              dir = turnWay ? dir.rotatedLeft() : dir.rotatedRight();
            }

            break;

          default:
        }

        for (byte[] loc : rooms) addRoomAt(loc[0], loc[1]);

        rooms.add(new byte[] {origX, origY});

        if (rooms.size() > 1) {
          int xx, yy;
          boolean found;

          for (byte[] loc : rooms) {
            for (DungeonDir testDir : DungeonDir.values) {
              xx = loc[0] + testDir.addX * 3;
              yy = loc[1] + testDir.addY * 3;

              if (elementList.getTypeAt(xx, yy) == DungeonElementType.ROOM) {
                found = false;

                for (byte[] testLoc : rooms) {
                  if (testLoc[0] == xx && testLoc[1] == yy) {
                    found = true;
                    break;
                  }
                }

                if (found) elementList.getAt(loc[0], loc[1]).connect(testDir);
              }
            }
          }
        }

        byte[] moveTo = rooms.get(rand.nextInt(rooms.size()));
        moveToElement(elementList.getAt(moveTo[0], moveTo[1]));

        return rooms.size();
      }

      return 1;
    } else return 0;
  }