/**
   * Only looks at the adjacent tiles.
   *
   * @param v
   * @param grid
   * @return
   */
  public vector getClosestAdjWalkableTile(vector v) {

    boolean[][] gridTiles = grid.getGridTiles();
    final float gridSize = grid.getGridSize();

    int x = (int) (v.x / gridSize);
    int y = (int) (v.y / gridSize);

    if (x < 1) x = 1;
    else if (x > gridTiles.length - 2) x = gridTiles.length - 2;

    if (y < 1) y = 1;
    else if (y > gridTiles[0].length - 2) y = gridTiles[0].length - 2;

    //		int minDx = x < 1 ? 0 : -1;
    //		int minDy = y < 1 ? 0 : -1;
    //
    //		int maxDx = x >= gridTiles   .length-2 ? 1 : 2;
    //		int maxDy = y >= gridTiles[0].length-2 ? 1 : 2;

    for (int i = -1; i < 2; ++i)
      for (int j = -1; j < 2; ++j)
        if (gridTiles[x + i][y + j])
          if (i == j && i == 0) return v;
          else
            return new vector(
                ((x + i) * gridSize) + gridSize / 2, ((y + j) * gridSize) + gridSize / 2);

    return v;
  }
  public static boolean isWalkable(vector nearDp, Grid grid) {

    int i = (int) (nearDp.x / grid.getGridSize());
    int j = (int) (nearDp.y / grid.getGridSize());

    boolean[][] gridTiles = grid.getGridTiles();

    if (i >= gridTiles.length || i < 0) return false;
    if (j >= gridTiles[0].length || j < 0) return false;

    return gridTiles[i][j];
  }
  public ArrayList<vector> getAllWalkableLocNextToThis(RectF area) {

    boolean[][] gridTiles = grid.getGridTiles();
    final float gridSize = grid.getGridSize();
    final float halfGridSize = gridSize / 2;

    ArrayList<vector> locs = new ArrayList<vector>();

    int leftI = (int) ((area.left - 2) / gridSize);
    int topJ = (int) ((area.top - 2) / gridSize);

    int rightI = (int) ((area.right + 2) / gridSize);
    int bottomJ = (int) ((area.bottom + 2) / gridSize);

    int i_iters = rightI - leftI;
    int j_iters = bottomJ - topJ;

    leftI = leftI < 0 ? 0 : leftI;
    topJ = topJ < 0 ? 0 : topJ;

    rightI = rightI >= gridTiles.length ? gridTiles.length - 1 : rightI;
    bottomJ = bottomJ >= gridTiles[0].length ? gridTiles[0].length - 1 : bottomJ;

    for (int i = 0; i < i_iters; ++i) {
      int tempI = leftI + i;
      if (gridTiles[tempI][topJ])
        locs.add(new vector(tempI * gridSize + halfGridSize, topJ * gridSize + halfGridSize));
      if (gridTiles[tempI][bottomJ])
        locs.add(new vector(tempI * gridSize + halfGridSize, bottomJ * gridSize + halfGridSize));
    }

    for (int j = 0; j < j_iters; ++j) {
      int tempJ = topJ + j;
      if (gridTiles[rightI][tempJ])
        locs.add(new vector(rightI * gridSize + halfGridSize, tempJ * gridSize + halfGridSize));
      if (gridTiles[leftI][tempJ])
        locs.add(new vector(leftI * gridSize + halfGridSize, tempJ * gridSize + halfGridSize));
    }

    return locs;
  }
 public float getGridSize() {
   return grid.getGridSize();
 }
  public vector getWalkableLocNextToThis(vector comingFrom, RectF area) {
    boolean[][] gridTiles = grid.getGridTiles();
    final float gridSize = grid.getGridSize();

    //		int comingFromI = (int)(comingFrom.x/gridSize);
    //		int comingFromJ = (int)(comingFrom.y/gridSize);
    //
    //		int numTilesToSearchX = (int)(area.width()/gridSize) + 1;
    //		int numTilesToSearchY = (int)(area.height()/gridSize) + 1;
    float extraX = gridSize * 0.8f;
    float extraY = gridSize * 0.8f;

    int leftI = (int) ((area.left - 2) / gridSize);
    int topJ = (int) ((area.top - 2) / gridSize);

    int rightI = (int) ((area.right + 2) / gridSize);
    int bottomJ = (int) ((area.bottom + 2) / gridSize);

    leftI = leftI < 0 ? 0 : leftI;
    topJ = topJ < 0 ? 0 : topJ;

    rightI = rightI >= gridTiles.length ? gridTiles.length - 1 : rightI;
    bottomJ = bottomJ >= gridTiles[0].length ? gridTiles[0].length - 1 : bottomJ;

    int iInc, startI;
    if (comingFrom.x > area.centerX()) {
      startI = rightI;
      iInc = -1;
    } else {
      startI = leftI;
      iInc = 1;
    }

    int jInc, startJ;
    if (comingFrom.y > area.centerY()) {
      startJ = bottomJ;
      jInc = -1;
    } else {
      startJ = topJ;
      jInc = 1;
    }

    int i_iters = rightI - leftI;

    if (startJ > -1 && startJ < gridTiles[0].length)
      for (int i = 0; i < i_iters; ++i) {
        int tempI = startI + iInc * i;
        if (tempI > -1 && tempI < gridTiles.length)
          if (gridTiles[tempI][startJ]) {
            vector v = new vector(tempI * gridSize, startJ * gridSize);
            v.x += v.x < area.centerX() ? extraX : 0;
            v.y += v.y < area.centerY() ? extraY : 0;
            return v;
          }
      }

    int j_iters = bottomJ - topJ;

    if (startI > -1 && startI < gridTiles.length)
      for (int j = 0; j < j_iters; ++j) {
        int tempJ = startJ + jInc * j;
        if (tempJ > -1 && tempJ < gridTiles[0].length)
          if (gridTiles[startI][tempJ]) {
            vector v = new vector(startI * gridSize, tempJ * gridSize);
            v.x += v.x < area.centerX() ? extraX : 0;
            v.y += v.y < area.centerY() ? extraY : 0;
            return v;
          }
      }

    // adjust index's, switch sides
    jInc *= -1;
    iInc *= -1;

    startI = startI == rightI ? leftI : rightI;
    startJ = startJ == topJ ? bottomJ : topJ;

    if (startJ > -1 && startJ < gridTiles[0].length)
      for (int i = 0; i < i_iters; ++i) {
        int tempI = startI + iInc * i;
        if (tempI > -1 && tempI < gridTiles.length)
          if (gridTiles[tempI][startJ]) {
            vector v = new vector(tempI * gridSize, startJ * gridSize);
            v.x += v.x < area.centerX() ? extraX : 0;
            v.y += v.y < area.centerY() ? extraY : 0;
            return v;
          }
      }

    if (startI > -1 && startI < gridTiles.length)
      for (int j = 0; j < j_iters; ++j) {
        int tempJ = startJ + jInc * j;
        if (tempJ > -1 && tempJ < gridTiles[0].length)
          if (gridTiles[startI][tempJ]) {
            vector v = new vector(startI * gridSize, tempJ * gridSize);
            v.x += v.x < area.centerX() ? extraX : 0;
            v.y += v.y < area.centerY() ? extraY : 0;
            return v;
          }
      }

    //// Log.d( TAG , "comingFrom = " + comingFrom );
    //// Log.d( TAG , "area = " + area );
    //// Log.d( TAG , "leftI:" + leftI + " topJ" + topJ + " rightI:" + rightI + " bottomJ:" +
    // bottomJ );

    // Log.e( TAG , "Trying to path to unwalkable tile(startI=" +startI+" , startJ=" + startJ +
    // ")");
    return null;
  }