
----------------------------------------------------------------------------------------------------------
-- Special controls and custom datarefs for F8E-Crusader by BERJU83.
----------------------------------------------------------------------------------------------------------

----------------------------------- LOCATE AND/OR CREATE DATAREFS AND COMMANDS -----------------------------------

----Autopilot APC ---
PA_speed = find_dataref("sim/cockpit2/autopilot/airspeed_dial_kts_mach")
AP_SW = find_dataref("sim/cockpit2/autopilot/servos_on")
FD_SW = find_dataref("sim/cockpit2/autopilot/servos_on")
Grounded = find_dataref("sim/flightmodel2/gear/on_ground[1]")
Gear_pos = find_dataref("sim/cockpit2/controls/gear_handle_down")
WBrake_pos = find_dataref("sim/cockpit2/controls/parking_brake_ratio")
Rbrake_pos = find_dataref("sim/cockpit2/controls/right_brake_ratio")
Lbrake_pos = find_dataref("sim/cockpit2/controls/left_brake_ratio")
Autothr_DR = find_dataref("sim/cockpit2/autopilot/autothrottle_enabled")
APC_SW = create_dataref("custom/F8E/APC_SW","number")


--------------------------------- VARIABLES ---------------------------------
--Brake strength--
BrStr = 0.1
------------------------------- FUNCTIONS: COMMANDS CALLBACK -------------------------------

-----------------------RUNTIME------------------

function flight_start()
    
    APC_SW = 0
end

function after_physics()
    if Grounded == 0 and Gear_pos == 1 and APC_SW == 1 and WBrake_pos < BrStr and Rbrake_pos < BrStr and Lbrake_pos < BrStr then
              APC_SW = APC_SW
              cmdATHon:once()
              cmdFDon:once() 
              
    elseif Grounded == 1 or Gear_pos < 1 or APC_SW == 0 or WBrake_pos > BrStr or Rbrake_pos > BrStr or Lbrake_pos > BrStr then
              APC_SW = 0
              cmdFDoff:once()
    end
end

---- APC SW---
function cmd_APC_on(phase, duration)
      if phase == 0 then
        APC_SW = 1
      end
end

function cmd_APC_off(phase, duration)
      if phase == 0 then 
        APC_SW = 0
      end
end

------------------------------- LOCATE AND/OR CREATE COMMANDS -------------------------------

---COMMANDS APC---
cmdATHon = find_command("sim/autopilot/autothrottle_arm")
cmdFDon = find_command("sim/autopilot/autothrottle_on")
cmdFDoff = find_command("sim/autopilot/autothrottle_hard_off")
cmdATHoff = find_command("sim/autopilot/autothrottle_off")
cmdAPCon = create_command("custom/F8E/APC_on"," ",cmd_APC_on)
cmdAPCoff = create_command("custom/F8E/APC_off"," ",cmd_APC_off)
