code: private CoordTile getClosestAvailableTile(Pathfindable mover,
                                                int stx,
                                                int sty,
                                                int stw,
                                                int sth,
                                                int dtx,
                                                int dty,
                                                int dtw,
                                                int dth,
                                                int radius)
      {
          int closestX = 0;
          int closestY = 0;
          double dist = Double.MAX_VALUE;
          int size = 1;
          boolean found = false;
          while (!found)
          {
              for (int tx = stx - size; tx <= stx + size; tx++)
              {
                  for (int ty = sty - size; ty <= sty + size; ty++)
                  {
                      if (isAreaAvailable(mover, tx, ty, stw, sth, null))
                      {
                          final double d = UtilMath.getDistance(tx, ty, stw, sth, dtx, dty, dtw, dth);
                          if (d < dist)
                          {
                              dist = d;
                              closestX = tx;
                              closestY = ty;
                              found = true;
                          }
                      }
                  }
              }
              size++;
              if (size >= radius)
              {
                  return null;
              }
          }
          return new CoordTile(closestX, closestY);
      }
docs: Get the closest unused location around the area. The returned tile is not blocking, nor used by an object.
  
  @param mover The object moving on map.
  @param stx The starting horizontal tile index.
  @param sty The starting vertical tile index.
  @param stw The source location width in tile.
  @param sth The source location height in tile.
  @param dtx The ending horizontal tile index.
  @param dty The ending vertical tile index.
  @param dtw The destination location width in tile.
  @param dth The destination location height in tile.
  @param radius The search radius.
  @return The closest tile found.