
int pathfind(int x, int y, int dest_x, int dest_y){
  int path[20][15], i = 1, a, b, stop = 0, check;

  for(a=0; a < MAP_X; a++){
    for(b=0; b < MAP_Y; b++){
      path[a][b] = -1;
    }
  }

  path[dest_x][dest_y] = 0;

  while(stop == 0){
    check = 0;
    for(a=0; a < MAP_X; a++){
      for(b=0; b < MAP_Y; b++){
        if(path[a][b] == i-1){
          check = 1;
          if(b < MAP_Y-1 && path[a][b+1] == -1 && solids_array[a][b+1] == 0)   {path[a][b+1] = i;}
          if(b > 0      && path[x][b-1] == -1 && solids_array[a][b-1] == 0)   {path[a][b-1] = i;}
          if(a < MAP_X-1 && path[a+1][b] == -1 && solids_array[a+1][b] == 0)   {path[a+1][b] = i;}
          if(a > 0      && path[a-1][b] == -1 && solids_array[a-1][b] == 0)   {path[a-1][b] = i;}
          if(a < MAP_X-1 && b < MAP_Y-1 && path[a+1][b+1] == -1 && solids_array[a+1][b+1] == 0) {path[a+1][b+1] = i;}
          if(a > 0      && b < MAP_Y-1 && path[a-1][b+1] == -1 && solids_array[a-1][b+1] == 0) {path[a-1][b+1] = i;}
          if(a < MAP_X-1 && b > 0      && path[a+1][b-1] == -1 && solids_array[a+1][b-1] == 0) {path[a+1][b-1] = i;}
          if(a > 0      && b > 0      && path[a-1][b-1] == -1 && solids_array[a-1][b-1] == 0) {path[a-1][b-1] = i;}
        }
        if(path[x][y] > -1){stop = 1;}
      }
    }
    i++;
    if(check == 0){return ERROR; /*can't find route*/}
  }

  
  if(path[x][y] != 0){
    if(dest_x < x){
      if(dest_y < y)     {if(x > 0      && y > 0      && path[x-1][y-1] > -1 && path[x-1][y-1] < path[x][y] && solids_array[x-1][y-1] == 0 && solids_array[x-1][y] == 0 && solids_array[x][y-1] == 0) {return NORTHWEST;}}
      else if(dest_y > y){if(x > 0      && y < MAP_Y-1 && path[x-1][y+1] > -1 && path[x-1][y+1] < path[x][y] && solids_array[x-1][y+1] == 0 && solids_array[x-1][y] == 0 && solids_array[x][y+1] == 0) {return NORTHEAST;}}
      else               {if(x > 0                    && path[x-1][y] > -1   && path[x-1][y]   < path[x][y] && solids_array[x-1][y] == 0) {return NORTH;}}
    }

    else if(dest_x > x){
      if(dest_y < y)     {if(x < MAP_X-1 && y > 0      && path[x+1][y-1] > -1 && path[x+1][y-1] < path[x][y] && solids_array[x+1][y-1] == 0 && solids_array[x+1][y] == 0 && solids_array[x][y-1] == 0) {return SOUTHWEST;}}
      else if(dest_y > y){if(x < MAP_X-1 && y < MAP_Y-1 && path[x+1][y+1] > -1 && path[x+1][y+1] < path[x][y] && solids_array[x+1][y+1] == 0 && solids_array[x+1][y] == 0 && solids_array[x][y+1] == 0) {return SOUTHEAST;}}
      else               {if(x < MAP_X-1               && path[x+1][y] > -1   && path[x+1][y]   < path[x][y] && solids_array[x+1][y] == 0) {return SOUTH;}}
    }

    else if(dest_y < y){if(y > 0                    && path[x][y-1] > -1   && path[x][y-1] < path[x][y] && solids_array[x][y-1] == 0) {return EAST;}}
    else if(dest_y > y){if(y < MAP_Y-1               && path[x][y+1] > -1   && path[x][y+1] < path[x][y] && solids_array[x][y+1] == 0) {return EAST;}}

    else{ //check all
      if(y < MAP_Y-1               && path[x][y+1] > -1   && path[x][y+1] < path[x][y] && solids_array[x][y+1] == 0)     {return EAST;}
      if(y > 0                    && path[x][y-1] > -1   && path[x][y-1] < path[x][y] && solids_array[x][y-1] == 0)     {return WEST;}
      if(x < MAP_X-1               && path[x+1][y] > -1   && path[x+1][y] < path[x][y] && solids_array[x+1][y] == 0)     {return SOUTH;}
      if(x > 0                    && path[x-1][y] > -1   && path[x-1][y] < path[x][y] && solids_array[x-1][y] == 0)     {return NORTH;}
      if(x < MAP_X-1 && y < MAP_Y-1 && path[x+1][y+1] > -1 && path[x+1][y+1] < path[x][y] && solids_array[x+1][y+1] == 0 && solids_array[x+1][y] == 0 && solids_array[x][y+1] == 0) {return SOUTHEAST;}
      if(x > 0      && y < MAP_Y-1 && path[x-1][y+1] > -1 && path[x-1][y+1] < path[x][y] && solids_array[x-1][y+1] == 0 && solids_array[x-1][y] == 0 && solids_array[x][y+1] == 0) {return NORTHEAST;}
      if(x < MAP_X-1 && y > 0      && path[x+1][y-1] > -1 && path[x+1][y-1] < path[x][y] && solids_array[x+1][y-1] == 0 && solids_array[x+1][y] == 0 && solids_array[x][y-1] == 0) {return SOUTHWEST;}
      if(x > 0      && y > 0      && path[x-1][y-1] > -1 && path[x-1][y-1] < path[x][y] && solids_array[x-1][y-1] == 0 && solids_array[x-1][y] == 0 && solids_array[x][y-1] == 0) {return NORTHWEST;}
    }
  }

  return ERROR;
}
