
----------------------------------------------------------------------------------------------------------
-- Special controls and custom datarefs for Alize-BR1050 by RFN.
----------------------------------------------------------------------------------------------------------

----------------------------------- LOCATE AND/OR CREATE DATAREFS AND COMMANDS -----------------------------------
----ELEC----
Elec_bus_on = find_dataref("sim/cockpit2/electrical/bus_volts[0]")

----ARM SELECT----
BTN_Select_Arms = create_dataref("custom/ALZ/SelectArms","number") --(0=arret, 1=soute, 2=fuseaux)--
SW_Psoute = create_dataref("custom/ALZ/Psoute","number")
SW_Pfuseaux = create_dataref("custom/ALZ/Pfuseaux","number")
Doors_Soute = find_dataref("sim/cockpit2/switches/custom_slider_on[00]")
Doors_fuseaux = find_dataref("sim/cockpit2/switches/custom_slider_on[03]")

----START MGT----
SW_dem_vent = create_dataref("custom/ALZ/Dem_Vent_SW","number") --(0=ventil, 1=neutral, 2=start)--
SW_Rpump = find_dataref("sim/cockpit2/fuel/fuel_tank_pump_on[0]")
SW_Lpump = find_dataref("sim/cockpit2/fuel/fuel_tank_pump_on[1]")
Startup_running = find_dataref("sim/operation/prefs/startup_running")

----FUEL MGT----
Fuel_Dump_R = create_dataref("custom/ALZ/FuelDumpR","number")
Fuel_Dump_L = create_dataref("custom/ALZ/FuelDumpL","number")
Fuel_Qty_R = find_dataref("sim/flightmodel/weight/m_fuel[0]")
Fuel_Qty_L = find_dataref("sim/flightmodel/weight/m_fuel[1]")
SW_Fuel_Intercom = create_dataref("custom/ALZ/SW_Fuel_Intercom","number")
Fuel_XFER = find_dataref("sim/cockpit2/fuel/fuel_tank_transfer_to","number") --(0 =arret, 1 =DG, 3 =GD)

----FLAPS---
flaps_switch = create_dataref("custom/ALZ/flaps_switch","number") --(0=up, 1=neutral, 2=down)

----WINGS FOLDING---
wings_fold = find_dataref("sim/cockpit2/controls/dihedral_ratio")
wings_latch = create_dataref("custom/ALZ/wings_latch","number")
wings_fold_SW = create_dataref("custom/ALZ/wings_fold_SW","number")

----AUTOPILOT---
PA_switch = create_dataref("custom/ALZ/PA_switch","number")
PA_servos = find_dataref("sim/cockpit2/autopilot/servos_on")

----BIP---
BIP_SW = create_dataref("custom/ALZ/BIP_SW","number")
BIP_red = create_dataref("custom/ALZ/BIP_red","number")
BIP_green = create_dataref("custom/ALZ/BIP_green","number")
BIP_amber = create_dataref("custom/ALZ/BIP_amber","number")
AoA_val = find_dataref("sim/flightmodel2/misc/AoA_angle_degrees")
BIP_ext_red = find_dataref("sim/cockpit2/switches/generic_lights_switch[2]")
BIP_ext_amber = find_dataref("sim/cockpit2/switches/generic_lights_switch[1]")

----MISC---
Ladder_pos = create_dataref("custom/ALZ/Ladder_pos","number")
Grounded = find_dataref("sim/flightmodel2/gear/on_ground[0]")
Gspeed = find_dataref("sim/flightmodel/position/groundspeed")
Chocks_vis = create_dataref("custom/ALZ/Chocks_vis","number")
Dihedral = find_dataref("sim/flightmodel2/controls/dihedral_ratio")
Avitab_on = find_dataref("avitab/brightness","number")
Deice_stat = find_dataref("sim/cockpit2/ice/ice_static_heat_on_pilot")

----ADF---
ADF1 = find_dataref("sim/cockpit2/radios/actuators/adf1_power")
ADF2 = find_dataref("sim/cockpit2/radios/actuators/adf2_power")

----TRAIN RADOME---
Gear_rad = create_dataref("custom/ALZ/Gear_rad","number")          --(voyant train radome)
Alt_min = find_dataref("sim/cockpit2/gauges/indicators/altitude_ft_pilot")
Radome_pos = find_dataref("sim/flightmodel2/misc/custom_slider_ratio[02]")
Gear_pos = find_dataref("sim/aircraft/parts/acf_gear_deploy[array]")

----TRANSPONDER---
xpdr_mode = find_dataref("sim/cockpit2/radios/actuators/transponder_mode")
xpdr_code = find_dataref("sim/cockpit2/radios/actuators/transponder_code")
--------------------------------- VARIABLES ---------------------------------
AoA_amb = 5
AoA_red = 6
Aoa_greL = 4
AoA_greH = 7


------------------------------- FUNCTIONS: COMMANDS CALLBACK -------------------------------

-----------------------RUNTIME------------------
function flight_start()
  flaps_switch = 1
  BTN_Select_Arms = 0
  SW_Psoute = 0
  SW_Pfuseaux = 0
  SW_dem_vent = 1
  wings_latch = 1
  Avitab_on = 0
  Deice_stat = 0
  xpdr_mode = 0
  ADF1 = 0
  ADF2 = 0
  
  --(auto_start)--
  if Startup_running == 1 
      then SW_Rpump = 1  SW_Lpump = 1 
      else SW_Rpump = 0 SW_Lpump = 0
  end
  
end

function after_physics()
	  --(cales et chandelles)--
	if Elec_bus_on == 0 and Grounded == 1 and Gspeed < 1 and Dihedral == 1 then
		Chocks_vis = 1
	else
		Chocks_vis = 0
	end
	
	  --(voyants BIP)--
  if Elec_bus_on > 15 then
      if BIP_SW == 1 then BIP_amber = 1 BIP_green = 1 BIP_red = 1 BIP_ext_red = 1 BIP_ext_amber = 1
      else BIP_amber = 0 BIP_green = 0 BIP_red = 0
      end
      if BIP_SW == 2 then
          if AoA_amb > AoA_val then BIP_amber = 1 BIP_ext_amber = 1
          else BIP_amber = 0
          end
          if AoA_val > AoA_red then BIP_red = 1 BIP_ext_red = 1
          else BIP_red = 0
          end
          if AoA_val > Aoa_greL and AoA_val < AoA_greH then BIP_green = 1 
          else BIP_green = 0
          end
      end
  else BIP_amber = 0 BIP_green = 0 BIP_red = 0
  end
  
  	  --(voyant train radome)--
  if Elec_bus_on > 15 and Alt_min < 1640 then
      if Radome_pos > 0.2 or Gear_pos < 0.8 then Gear_rad = 1 else Gear_rad = 0 end
  else Gear_rad = 0
  end
        
    --(Transponder)--
  if xpdr_mode == 1 then xpdr_code = 1200
  elseif xpdr_mode == 2 then xpdr_code = xpdr_code
  elseif xpdr_mode > 2 then xpdr_code = 7700
  end
  
  -- (ADF Synchro)--
  ADF2 = ADF1
end

	
-----CDE FLAPS-----------------------

function cmd_flaps_dn(phase, duration)
	if phase == 0 then 
    flaps_switch = 2
    cmdflapsmovedn:start()
  end
 	if phase == 2 then 
    flaps_switch = 1
    cmdflapsmovedn:stop()
  end
end
 
function cmd_flaps_up(phase, duration)
	if phase == 0 then 
    flaps_switch = 0
    cmdflapsmoveup:start()
  end
 	if phase == 2 then 
    flaps_switch = 1
    cmdflapsmoveup:stop()
  end 
end

-----CDE ARMES-----------------------
function cmd_select_arms_up(phase, duration)
	if phase == 0 then 
    BTN_Select_Arms = BTN_Select_Arms + 1
  end
  if BTN_Select_Arms > 2 then BTN_Select_Arms = 2
  end
end

function cmd_select_arms_dn(phase, duration)
	if phase == 0 then 
    BTN_Select_Arms = BTN_Select_Arms - 1
  end
  if BTN_Select_Arms < 0 then BTN_Select_Arms = 0
  end
end

function cmd_open_soute(phase, duration)
	if phase == 0 then
	SW_Psoute = 1
      if SW_Psoute == 1 and BTN_Select_Arms == 1 and Elec_bus_on > 15 then 
      Doors_Soute = 1 
      end
	end
end

function cmd_close_soute(phase, duration)
	if phase == 0 then
	SW_Psoute = 0
  Doors_Soute = 0
	end
end

function cmd_open_fuseaux(phase, duration)
	if phase == 0 then
	SW_Pfuseaux = 1
	    if SW_Pfuseaux == 1 and BTN_Select_Arms == 2 and Elec_bus_on > 15 then 
      Doors_fuseaux = 1
      end 
	end
end

function cmd_close_fuseaux(phase, duration)
	if phase == 0 then
	SW_Pfuseaux = 0
	Doors_fuseaux = 0
	end
end	 
    
    ---FUEL DUMP MGT---
function cmd_Fuel_Dump_R(phase, duration)
	if phase == 1 then
	Fuel_Dump_R = 1
	Fuel_Qty_R = Fuel_Qty_R -0.5
	elseif phase == 2 then 
  Fuel_Dump_R = 0
  Fuel_Qty_R = Fuel_Qty_R
  end
end   
    
function cmd_Fuel_Dump_L(phase, duration)
	if phase == 1 then
	Fuel_Dump_L = 1
	Fuel_Qty_L = Fuel_Qty_L -1
	elseif phase == 2 then 
  Fuel_Dump_L = 0
  Fuel_Qty_L = Fuel_Qty_L
  end
end 

    ---FUEL TANKS INTERCOM---

function cmd_intercom_on(phase, duration)
	if phase == 1 then
  SW_Fuel_Intercom = 1
       if Fuel_Qty_R > Fuel_Qty_L then Fuel_XFER = 1
       elseif Fuel_Qty_R < Fuel_Qty_L then Fuel_XFER = 3
       end
  elseif phase == 2 then 
  SW_Fuel_Intercom = 0 
       Fuel_XFER = 0
  end
end
 
    ---ENGINE START---

function cmd_Vent_SW(phase, duration)
	if phase == 0 then 
    SW_dem_vent = SW_dem_vent - 1
    if SW_dem_vent < 2 then cmdEngIgnitoff:start() end
  end
  if SW_dem_vent < 0 then SW_dem_vent = 0
  end
   	if phase == 2 then  SW_dem_vent = 1 end 
end

function cmd_Start_SW(phase, duration)
	if phase == 0 then 
    SW_dem_vent = SW_dem_vent + 1
    if SW_dem_vent == 2 then cmdEngIgniton:start() end
  end
  if SW_dem_vent > 2 then SW_dem_vent = 2
  end
end  

    ---WINGS FOLD---
function cmd_wings_latch_on(phase, duration)
	if phase == 0 then
	wings_latch = 1
	end
end

function cmd_wings_latch_off(phase, duration)
	if phase == 0 then
	wings_latch = 0
	end
end

function cmd_wings_fold_on(phase, duration)
	if phase == 0 then
	wings_fold_SW = 1
	    if wings_latch == 0 then wings_fold = 1
	    end
	end
end

function cmd_wings_fold_off(phase, duration)
	if phase == 0 then
	wings_fold_SW = 0
	    if wings_latch == 0 then wings_fold = 0   
      end
	end
end


    ----AUTOPILOT----
function cmd_PA_on(phase, duration)
	if phase == 0 then
	PA_switch = 1
	cmdservoson:start()
	end
end

function cmd_PA_off(phase, duration)
	if phase == 0 then
	PA_switch = 0
	cmdservosoff:start()
	end
end

    ---BIP---
function cmd_BIP_on(phase, duration)
	if phase == 0 then 
    BIP_SW = BIP_SW + 1
  end
  if BIP_SW > 2 then BIP_SW = 2
  end
end

function cmd_BIP_off(phase, duration)
	if phase == 0 then 
    BIP_SW = BIP_SW - 1
  end
  if BIP_SW < 0 then BIP_SW = 0
  end
end
  
   
    ---MISC---
function cmd_ladder_dn(phase, duration)
	if phase == 0 then
      if Grounded == 1 and Gspeed < 1 then 
      Ladder_pos = 1
      end
	end
end
function cmd_ladder_up(phase, duration)
	if phase == 0 then
	Ladder_pos = 0
	end
end

     
------------------------------- LOCATE AND/OR CREATE COMMANDS -------------------------------
---COMMANDS FLAPS-------
cmdflapsup = create_command("custom/ALZ/flaps_up","Flaps decrease",cmd_flaps_up)
cmdflapsdn = create_command("custom/ALZ/flaps_dn","Flaps increase",cmd_flaps_dn)
cmdflapsmoveup = find_command("sim/flight_controls/flaps_up")
cmdflapsmovedn = find_command("sim/flight_controls/flaps_down")

---COMMANDS ARMES-------
cmdselectarmsup =  create_command("custom/ALZ/select_armsup","select weapon",cmd_select_arms_up)
cmdselectarmsdn =  create_command("custom/ALZ/select_armsdn","select weapon",cmd_select_arms_dn)
cmdopensoute =  create_command("custom/ALZ/open_soute","soute open",cmd_open_soute)
cmdclosesoute =  create_command("custom/ALZ/close_soute","soute close",cmd_close_soute)
cmdopenfuseaux =  create_command("custom/ALZ/open_fuseaux","fuseaux open",cmd_open_fuseaux)
cmdclosefuseaux =  create_command("custom/ALZ/close_fuseaux","fuseaux close",cmd_close_fuseaux)

---COMMANDS ENGINE START -------
cmdVentSW = create_command("custom/ALZ/Vent_SW","Select Engine Ventilation",cmd_Vent_SW)
cmdStartSW = create_command("custom/ALZ/Start_SW","Select Engine Start",cmd_Start_SW)
cmdEngIgniton = find_command("sim/igniters/igniter_arm_on_1")
cmdEngIgnitoff = find_command("sim/igniters/igniter_arm_off_1")

---COMMANDS FUEL -------
cmdfueldumpR = create_command("custom/ALZ/Fuel_dump_right","Right Tank dump",cmd_Fuel_Dump_R)
cmdfueldumpL = create_command("custom/ALZ/Fuel_dump_left","Left Tank dump",cmd_Fuel_Dump_L)
cdmfuelintercomon = create_command("custom/ALZ/Fuel_Intercom_on","Tanks intercom ON",cmd_intercom_on)

----COMMANDS WINGS FOLDING---
cmdwingslatchon = create_command("custom/ALZ/wings_latch_on","Wings latch lever",cmd_wings_latch_on)
cmdwingslatchoff = create_command("custom/ALZ/wings_latch_off","Wings latch lever",cmd_wings_latch_off)
cmdwingsfoldon = create_command("custom/ALZ/wings_fold_on"," ",cmd_wings_fold_on)
cmdwingsfoldoff = create_command("custom/ALZ/wings_fold_off"," ",cmd_wings_fold_off)

----AUTOPILOT COMMANDS----
cmdPAon = create_command("custom/ALZ/PA_on"," ",cmd_PA_on)
cmdPAoff = create_command("custom/ALZ/PA_off"," ",cmd_PA_off)
cmdservoson = find_command("sim/autopilot/servos_on")
cmdservosoff = find_command("sim/autopilot/servos_off_any")

---COMMANDS BIP---
cmdBIPon = create_command("custom/ALZ/BIP_on"," ",cmd_BIP_on)
cmdBIPoff = create_command("custom/ALZ/BIP_off"," ",cmd_BIP_off)
   
---COMMANDS MISC -------
cmdladderdn = create_command("custom/ALZ/ladder_dn","Ladder Down",cmd_ladder_dn)
cmdladderup = create_command("custom/ALZ/ladder_up","Ladder Up",cmd_ladder_up)