William David Lloyd, Royal Engineers