-- This script was written by SmoothChat  @   https://forums.x-plane.org/index.php?/forums/ with help from 
-- Mark Cellis, author of X-Camera, an essential addon for Xplane.
-- Version 1.1.2 FINAL   10 October 2019

-- This script is free for Non_Commercial use.

-- This LUA script will animate an x-camera external view (with viewID 30) to simulate a wingman view with the aircraft drifting around.

-- Follow these easy steps  (Requires X-Camera 2.3.3 beta2 or above)

--1  Install the script in your flywithlua/scripts folder (Do, a search to get the FlyWithLUA NG plugin)

--2. Load up your plane, open the x-camera  window and create an external view giving it an ID of 30, 31, 32 or 33 (essential). 
--    Select the "External Camera" and Enable "Level Camera" check boxes so the camera moves independently of the aircraft. 
--    Enable "Retain Mouse Look" and "Temporary Positioning" to aid with rotating your camera in the next steps. You might also like to enable the tracking options, 
--    but I think the effect looks more realistic without it (IMHO)

--3. With your mouse middle button, or joystick hat, rotate the view as desired.  Adjust the camera position with the movement keys. 
--   Use "Set Initial Zoom" button after using the mouse wheel to set the level of zoom required.  
--    Then hit the Save button on the x-camera screen. This will set the camera position and other attributes ready for the Wingman code to animate your view.

--4. Reload  your aircraft and navigate to your new view. It should begin moving because of the script. (Consider mapping some keys or joystick buttons to change views)

--5. Select another view, then return to View 30 etc, to check.

--6  The various parameters of the Wingman effect can be altered and saved for each aircraft via the "Show Wingman Settings" menu under "Plugins/FlyWithLua/FlyWithLua Macros".

--  X-camera stores your initial View details when you create and save ViewID 30 etc. The Wingman script save function stores the movement settings in a file named with the aircraft ICAO and Tailnumber

-- The Heading and Pitch of the view is not affected  (unless you press the reset button on the Wingman settings window.)

--  Reload your aircraft or the FlyWithLua scripts if things get confused.

-- Remember, this script saves the x,y,z and roll movements info.  X-camera saves the initial Location , Pitch and Heading of the camera.
-- To save the center of the view, open the Wingman settings and stop the movement, then momentarily change to another view.  
-- On your return to the ID30 etc view, the view will be centered on the starting point. Adjust each view to your liking and save with x-camera.

-- While in the X-Camera control panel, before editing a Wingman view, always cycle through all the views , to reset them, in case a script has moved a view and it gets saved in that state when you select "Save All".

-- If you want to "fly" your plane from a Wingman view, disable "Randomize Aileron" if you have "Aircraft Free Movement" enabled as the random Aileron could affect your flight controls. 

-- It is normal for "Randomize Aileron" to be disabled when clicking "Aircraft Free Movement" as a twitching aileron may effect you if you are "flying" via a Wingman view. You can over-ride this by clicking "Randomize Aileron" again.
-- Your plane is free to move, so it might drift out of view. Use your mouse to point to the aircraft again, or enable tracking in X-Camera. The centre position will reset when you select onother view and return.

-- Note: There will be no Wingman menu item until you have created at least 1 Wingman view, beginning at 30.  
-- Reload the aircraft to refresh the wingman script after any new X-Camera views.

-- DO NOT duplicate View ID numbers as this could confuse the script logic. Check that you haven't assigned the same numbers to any other view.

-- If you wish to change the starting view number or number of views , alter line 78 in this script.  Note: you should keep the wingman view ID consistent across all your planes, so best to keep it at the default (30). 
-- This script currently allows up to 4 Wingman views.

-- The Smoother Transition setting causes the motion to start at the outside edge and slowest part  of the movement so switching views and slow transistions between Wingman views are smoother at the end view. (Less tendency to jump as the movement re-commences). It also remembers the last camera roll value.

-- Smooth Transition off begins at the center of the motion and has a more accurate starting point. 

-- You can assign a button or keystroke to show/hide the wingman settings window. Look for "wingman" in the list of available commands in the x-plane joystick config screen