defineProperty("test_data_1", globalPropertyf("cl300/test_data_1"))
defineProperty("test_data_2", globalPropertyf("cl300/test_data_2"))
defineProperty("test_data_3", globalPropertyf("cl300/test_data_3"))
defineProperty("test_data_4", globalPropertyf("cl300/test_data_4"))
defineProperty("test_data_5", globalPropertyf("cl300/test_data_5"))

defineProperty("switch_1", globalPropertyi("sim/cockpit2/switches/custom_slider_on[11]"))
FMSCDU_popup = findCommand("sim/FMS/CDU_popup")


--datarefs for reflection animations
defineProperty("l_engn_rev_rat", globalPropertyf("cl300/l_engn_rev_rat"))
defineProperty("r_engn_rev_rat", globalPropertyf("cl300/r_engn_rev_rat"))
defineProperty("ailer_l", globalPropertyf("cl300/ailer_l"))
defineProperty("ailer_r", globalPropertyf("cl300/ailer_r"))
defineProperty("aileron1_deg_0", globalPropertyf("sim/flightmodel2/wing/aileron1_deg[0]"))
defineProperty("aileron1_deg_1", globalPropertyf("sim/flightmodel2/wing/aileron1_deg[1]"))
defineProperty("flapref_l", globalPropertyf("cl300/flapref_l"))
defineProperty("flapref_r", globalPropertyf("cl300/flapref_r"))

defineProperty("gear_deploy_ratio_0", globalPropertyf("sim/flightmodel2/gear/deploy_ratio[0]"))
defineProperty("gear_deploy_ratio_1", globalPropertyf("sim/flightmodel2/gear/deploy_ratio[1]"))
defineProperty("gear_deploy_ratio_2", globalPropertyf("sim/flightmodel2/gear/deploy_ratio[2]"))
defineProperty("acf_gear_deploy_1", globalPropertyf("sim/aircraft/parts/acf_gear_deploy[1]"))
defineProperty("acf_gear_deploy_2", globalPropertyf("sim/aircraft/parts/acf_gear_deploy[2]"))
defineProperty("lg_gear_0", globalPropertyf("cl300/lg_gear_0"))
defineProperty("lg_gear_1", globalPropertyf("cl300/lg_gear_1"))
defineProperty("lg_gear_2", globalPropertyf("cl300/lg_gear_2"))
-----


defineProperty("dcp_refs_menu", globalPropertyi("cl300/dcp_refs_menu"))

defineProperty("baggage_door", globalPropertyf("sim/cockpit2/switches/custom_slider_on[5]"))
defineProperty("lavat_door", globalPropertyf("sim/cockpit2/switches/custom_slider_on[6]"))
defineProperty("jalousie1", globalPropertyf("cl300/jalousie1"))
defineProperty("jalousie2", globalPropertyf("cl300/jalousie2"))
defineProperty("jalousie3", globalPropertyf("cl300/jalousie3"))
defineProperty("jalousie4", globalPropertyf("cl300/jalousie4"))
defineProperty("jalousie5", globalPropertyf("cl300/jalousie5"))
defineProperty("jalousie6", globalPropertyf("cl300/jalousie6"))

defineProperty("jalousie7", globalPropertyf("cl300/jalousie7"))
defineProperty("jalousie8", globalPropertyf("cl300/jalousie8"))
defineProperty("jalousie9", globalPropertyf("cl300/jalousie9"))
defineProperty("jalousie10", globalPropertyf("cl300/jalousie10"))
defineProperty("jalousie11", globalPropertyf("cl300/jalousie11"))
defineProperty("jalousie12", globalPropertyf("cl300/jalousie12"))
defineProperty("table1", globalPropertyf("cl300/table1"))
defineProperty("table2", globalPropertyf("cl300/table2"))
defineProperty("table3", globalPropertyf("cl300/table3"))
defineProperty("table4", globalPropertyf("cl300/table4"))

defineProperty("rel_lagear1", globalPropertyi("sim/operation/failures/rel_lagear1"))
defineProperty("rel_lagear2", globalPropertyi("sim/operation/failures/rel_lagear2"))
defineProperty("rel_lagear3", globalPropertyi("sim/operation/failures/rel_lagear3"))

defineProperty("dcp_menu1_rot", globalPropertyi("cl300/dcp_menu1_rot"))

defineProperty("eq_ped_nobrk", globalPropertyi("sim/joystick/eq_ped_nobrk"))

defineProperty("electr_ess_p_status", globalPropertyi("cl300/electr_ess_p_status"))
defineProperty("lan_gear_warn", globalPropertyi("cl300/lan_gear_warn"))

defineProperty("paused", globalPropertyi("sim/time/sim_speed"))

defineProperty("heading_AHARS_deg_mag_pilot", globalPropertyf("sim/cockpit2/gauges/indicators/heading_AHARS_deg_mag_pilot"))
defineProperty("hsi_obs_deg_mag_pilot", globalPropertyf("sim/cockpit2/radios/actuators/hsi_obs_deg_mag_pilot"))
defineProperty("crc_butt", globalPropertyi("cl300/crc_butt"))

defineProperty("nosewheelspd", globalPropertyf("sim/flightmodel/misc/nosewheel_speed"))
defineProperty("tire_rotation_speed_rad_sec_0", globalPropertyf("sim/flightmodel2/gear/tire_rotation_speed_rad_sec[0]")) -- JackZero: New
defineProperty("tire_rotation_speed_rad_sec_1", globalPropertyf("sim/flightmodel2/gear/tire_rotation_speed_rad_sec[1]")) -- JackZero: New
defineProperty("tire_rotation_speed_rad_sec_2", globalPropertyf("sim/flightmodel2/gear/tire_rotation_speed_rad_sec[2]")) -- JackZero: New


defineProperty("slip_deg", globalPropertyf("sim/cockpit2/gauges/indicators/slip_deg")) -- JackZero: Added for gravity xflow
defineProperty("nav1_bearing_deg_mag", globalPropertyf("sim/cockpit2/radios/indicators/nav1_bearing_deg_mag"))
defineProperty("nav2_bearing_deg_mag", globalPropertyf("sim/cockpit2/radios/indicators/nav2_bearing_deg_mag"))
defineProperty("gps_bearing_deg_mag", globalPropertyf("sim/cockpit2/radios/indicators/gps_bearing_deg_mag"))
defineProperty("nav1_display_horizontal", globalPropertyi("sim/cockpit2/radios/indicators/nav1_display_horizontal"))
defineProperty("nav2_display_horizontal", globalPropertyi("sim/cockpit2/radios/indicators/nav2_display_horizontal"))
defineProperty("destination_type", globalPropertyi("sim/cockpit/gps/destination_type"))


defineProperty("gpu_state", globalPropertyi("cl300/gpu_state")) -- JackZero: New DataRef: 0 = gpu not connectable, 1 = gpu can connect, 2 = gpu connected
defineProperty("gpu_mode",  globalPropertyi("cl300/gpu_mode"))  -- JackZero: New DataRef: 0 = auto, 1 = manual


defineProperty("rudlim_test", globalPropertyi("cl300/rudlim_test"))
defineProperty("taws_test_aur", globalPropertyf("cl300/taws_test_aur"))
defineProperty("fire_test_aur", globalPropertyf("cl300/fire_test_aur"))
defineProperty("probs_test", globalPropertyi("cl300/probs_test"))
defineProperty("ice_test", globalPropertyi("cl300/ice_test"))
defineProperty("annun1_aur", globalPropertyf("cl300/annun1_aur"))
defineProperty("annun2_aur", globalPropertyf("cl300/annun2_aur"))

defineProperty("voice_rec_test", globalPropertyi("cl300/voice_rec_test"))
defineProperty("voice_rec_test_lt", globalPropertyi("cl300/voice_rec_test_lt"))

defineProperty("test_rot", globalPropertyi("cl300/test_rot"))
defineProperty("test_push", globalPropertyi("cl300/test_push"))


defineProperty("stall_warn_test", globalPropertyi("cl300/stall_warn_test"))


defineProperty("eicas_3d_p", globalPropertyi("cl300/eicas_3d_p"))

defineProperty("radio_altimeter_bug_ft_pilot", globalPropertyf("sim/cockpit2/gauges/actuators/radio_altimeter_bug_ft_pilot"))
defineProperty("groundspeed", globalPropertyf("sim/flightmodel/position/groundspeed"))

defineProperty("elevator_trim", globalPropertyf("sim/cockpit2/controls/elevator_trim"))
-- JackZero: Linking rudder trim to trim switch:
defineProperty("rudder_trim", globalPropertyf("sim/cockpit2/controls/rudder_trim"))
defineProperty("trim_rud", globalPropertyf("cl300/trim_rud"))
-- JackZero: End

defineProperty("refspds_v1", globalPropertyi("cl300/refspds_v1"))
defineProperty("refspds_vr", globalPropertyi("cl300/refspds_vr"))
defineProperty("refspds_v2", globalPropertyi("cl300/refspds_v2"))
defineProperty("refspds_vt", globalPropertyi("cl300/refspds_vt"))
defineProperty("refspds_vga", globalPropertyi("cl300/refspds_vga"))
defineProperty("refspds_vref", globalPropertyi("cl300/refspds_vref"))

defineProperty("airdat1_fault", globalPropertyi("cl300/airdat1_fault"))
defineProperty("airdat2_fault", globalPropertyi("cl300/airdat2_fault"))



defineProperty("EFIS_1_selection_pilot", globalPropertyi("sim/cockpit2/EFIS/EFIS_1_selection_pilot"))
defineProperty("EFIS_2_selection_pilot", globalPropertyi("sim/cockpit2/EFIS/EFIS_2_selection_pilot"))
defineProperty("HSI_source_select_pilot", globalPropertyi("sim/cockpit2/radios/actuators/HSI_source_select_pilot"))
defineProperty("dcp_navsrc", globalPropertyi("cl300/dcp_navsrc"))
defineProperty("autop_brgsrc", globalPropertyi("cl300/autop_brgsrc"))
defineProperty("dcp_refs", globalPropertyi("cl300/dcp_refs"))

defineProperty("EFIS_airport_on", globalPropertyi("sim/cockpit2/EFISEFIS/EFIS_airport_on"))
defineProperty("EFIS_fix_on", globalPropertyi("sim/cockpit2/EFIS/EFIS_fix_on"))
defineProperty("EFIS_vor_on", globalPropertyi("sim/cockpit2/EFIS/EFIS_vor_on"))
defineProperty("EFIS_ndb_on", globalPropertyi("sim/cockpit2/EFIS/EFIS_ndb_on"))

defineProperty("map_mode", globalPropertyi("sim/cockpit2/EFIS/map_mode"))
defineProperty("map_range", globalPropertyi("sim/cockpit2/EFIS/map_range"))
defineProperty("dcp_range", globalPropertyi("cl300/dcp_range"))

defineProperty("ping_pong_2", globalPropertyf("sim/graphics/animation/ping_pong_2"))
defineProperty("master_warning", globalPropertyi("sim/cockpit2/annunciators/master_warning"))
defineProperty("master_caution", globalPropertyi("sim/cockpit/warnings/annunciators/master_caution"))
defineProperty("mast_caut", globalPropertyf("cl300/mast_caut"))
defineProperty("mast_warn", globalPropertyf("cl300/mast_warn"))
defineProperty("red_cas", globalPropertyi("cl300/red_cas"))
defineProperty("yellow_cas", globalPropertyi("cl300/yellow_cas"))

defineProperty("mfd_sel_state", globalPropertyi("cl300/mfd_sel_state"))
defineProperty("mfd_sel_state_h", globalPropertyi("cl300/mfd_sel_state_h"))

defineProperty("rev_tune", globalPropertyi("cl300/rev_tune"))
defineProperty("com1_freq_hz", globalPropertyi("sim/cockpit/radios/com1_freq_hz"))

defineProperty("gear_handle_status", globalPropertyi("sim/cockpit/switches/gear_handle_status"))

defineProperty("audio_selection_nav1", globalPropertyi("sim/cockpit2/radios/actuators/audio_selection_nav1"))
defineProperty("audio_selection_nav2", globalPropertyi("sim/cockpit2/radios/actuators/audio_selection_nav2"))
defineProperty("audio_selection_adf1", globalPropertyi("sim/cockpit2/radios/actuators/audio_selection_adf1"))
defineProperty("audio_selection_adf2", globalPropertyi("sim/cockpit2/radios/actuators/audio_selection_adf2"))
defineProperty("audio_dme_enabled", globalPropertyi("sim/cockpit2/radios/actuators/audio_dme_enabled"))
defineProperty("audio_marker_enabled", globalPropertyi("sim/cockpit2/radios/actuators/audio_marker_enabled"))

defineProperty("aud_vol_12_l", globalPropertyi("cl300/aud_vol_12_l"))
defineProperty("aud_vol_12_r", globalPropertyi("cl300/aud_vol_12_r"))
defineProperty("aud_vol_15_l", globalPropertyi("cl300/aud_vol_15_l"))
defineProperty("aud_vol_15_r", globalPropertyi("cl300/aud_vol_15_r"))
defineProperty("aud_vol_16_l", globalPropertyi("cl300/aud_vol_16_l"))
defineProperty("aud_vol_16_r", globalPropertyi("cl300/aud_vol_16_r"))


defineProperty("flight_time", globalPropertyf("sim/time/total_flight_time_sec"))
defineProperty("local_time_sec", globalPropertyf("sim/cockpit2/clock_timer/local_time_seconds")) -- local time sec

defineProperty("acf_has_fuel_all", globalPropertyi("sim/aircraft/overflow/acf_has_fuel_all"))
defineProperty("acf_has_fuel_any", globalPropertyi("sim/aircraft/overflow/acf_has_fuel_any"))
defineProperty("fuel_tank_selector", globalPropertyi("sim/cockpit2/fuel/fuel_tank_selector"))
defineProperty("fuel_tank_selector_left", globalPropertyi("sim/cockpit2/fuel/fuel_tank_selector_left"))
defineProperty("fuel_tank_selector_right", globalPropertyi("sim/cockpit2/fuel/fuel_tank_selector_right"))
defineProperty("m_fuel_total", globalPropertyf("sim/flightmodel/weight/m_fuel_total"))
defineProperty("m_fuel1", globalPropertyf("sim/flightmodel/weight/m_fuel1"))
defineProperty("m_fuel2", globalPropertyf("sim/flightmodel/weight/m_fuel2"))


defineProperty("acf_lanlite_the", globalPropertyf("sim/aircraft/view/acf_lanlite_the"))

defineProperty("pressure_diffential_psi", globalPropertyf("sim/cockpit/pressure/cabin_pressure_differential_psi"))
defineProperty("pressure_ditch_h", globalPropertyf("cl300/pressure_ditch_h"))
defineProperty("pressure_emer_depr_h", globalPropertyi("cl300/pressure_emer_depr_h"))
defineProperty("pressure_manrate", globalPropertyf("cl300/pressure_manrate"))
defineProperty("altitude_ft_copilot", globalPropertyi("sim/cockpit2/gauges/indicators/altitude_ft_copilot"))
defineProperty("altitude_ft_pilot", globalPropertyi("sim/cockpit2/gauges/indicators/altitude_ft_pilot"))

defineProperty("transparent_panel", globalPropertyi("sim/graphics/settings/transparent_panel"))

defineProperty("frame_rate_period", globalPropertyf("sim/operation/misc/frame_rate_period"))

defineProperty("dump_all", globalPropertyi("sim/cockpit/pressure/dump_all"))

defineProperty("ice_all_on", globalPropertyi("sim/cockpit2/ice/ice_all_on"))
defineProperty("ice_surfce_heat_left_on", globalPropertyi("sim/cockpit2/ice/ice_surfce_heat_left_on"))
defineProperty("ice_surfce_heat_right_on", globalPropertyi("sim/cockpit2/ice/ice_surfce_heat_right_on"))
defineProperty("ice_window_heat_on", globalPropertyi("sim/cockpit2/ice/ice_window_heat_on"))
defineProperty("ice_pitot_heat_on_pilot", globalPropertyi("sim/cockpit2/ice/ice_pitot_heat_on_pilot"))
defineProperty("ice_pitot_heat_on_copilot", globalPropertyi("sim/cockpit2/ice/ice_pitot_heat_on_copilot"))
defineProperty("ice_AOA_heat_on", globalPropertyi("sim/cockpit2/ice/ice_AOA_heat_on"))
defineProperty("ice_inlet_heat_on", globalPropertyi("sim/cockpit2/ice/ice_inlet_heat_on"))
defineProperty("ice_detect_on", globalPropertyi("sim/cockpit2/ice/ice_detect_on"))

defineProperty("machno", globalPropertyf("sim/flightmodel/misc/machno"))
defineProperty("jet_sync_mode", globalPropertyi("sim/cockpit2/switches/jet_sync_mode"))

defineProperty("autopilot_on", globalPropertyi("sim/cockpit2/autopilot/autopilot_on")) -- JackZero: Moved here from pfd.lua
defineProperty("altitude_hold_status", globalPropertyi("sim/cockpit2/autopilot/altitude_hold_status"))
defineProperty("TOGA_lateral_status", globalPropertyi("sim/cockpit2/autopilot/TOGA_lateral_status")) -- JackZero: Moved here from pfd.lua
defineProperty("roll_status", globalPropertyi("sim/cockpit2/autopilot/roll_status")) -- JackZero: Moved here from pfd.lua


defineProperty("autop_vvi_dial", globalPropertyi("cl300/autop_vvi_dial"))
defineProperty("autop_crs_dial", globalPropertyi("cl300/autop_crs_dial"))
defineProperty("autop_hdg_dial", globalPropertyi("cl300/autop_hdg_dial"))
defineProperty("autop_spd_dial", globalPropertyi("cl300/autop_spd_dial"))
defineProperty("autop_alt_dial", globalPropertyi("cl300/autop_alt_dial"))

defineProperty("autopilot_mode", globalPropertyi("sim/cockpit/autopilot/autopilot_mode"))


defineProperty("timer_running", globalPropertyi("sim/cockpit2/clock_timer/timer_running"))
defineProperty("clock_timer_h", globalPropertyi("cl300/clock_timer_h"))

defineProperty("yaw_damper_on", globalPropertyi("sim/cockpit2/switches/yaw_damper_on"))

defineProperty("autop_yd_h", globalPropertyi("cl300/autop_yd_h"))
defineProperty("autop_xfr_h", globalPropertyi("cl300/autop_xfr_h"))
defineProperty("autop_ap_h", globalPropertyi("cl300/autop_ap_h"))
defineProperty("autop_alt_h", globalPropertyi("cl300/autop_alt_h"))
defineProperty("autop_vnav_h", globalPropertyi("cl300/autop_vnav_h"))
defineProperty("autop_vs_h", globalPropertyi("cl300/autop_vs_h"))
defineProperty("autop_flc_h", globalPropertyi("cl300/autop_flc_h"))
defineProperty("autop_bc_h", globalPropertyi("cl300/autop_bc_h"))
defineProperty("autop_appr_h", globalPropertyi("cl300/autop_appr_h"))
defineProperty("autop_hdg_h", globalPropertyi("cl300/autop_hdg_h"))
defineProperty("autop_nav_h", globalPropertyi("cl300/autop_nav_h"))
defineProperty("autop_fdir_h", globalPropertyi("cl300/autop_fdir_h"))
defineProperty("half_bank_h", globalPropertyi("cl300/half_bank_h"))
defineProperty("bank_angle_mode", globalPropertyi("sim/cockpit2/autopilot/bank_angle_mode"))
defineProperty("servos_on", globalPropertyi("sim/cockpit2/autopilot/servos_on"))

defineProperty("warn_bb", globalPropertyi("sim/operation/prefs/warn_bb"))


defineProperty("rudd_ped_l_sw", globalPropertyi("cl300/rudd_ped_l_sw"))
defineProperty("pedals_fwd_bwd_l", globalPropertyf("cl300/pedals_fwd_bwd_l"))
defineProperty("rudd_ped_r_sw", globalPropertyi("cl300/rudd_ped_r_sw"))
defineProperty("pedals_fwd_bwd_r", globalPropertyf("cl300/pedals_fwd_bwd_r"))


defineProperty("heading_AHARS_deg_mag_pilot", globalPropertyf("sim/cockpit2/gauges/indicators/heading_AHARS_deg_mag_pilot"))
defineProperty("heading_dial_deg_mag_pilot", globalPropertyf("sim/cockpit2/autopilot/heading_dial_deg_mag_pilot"))
defineProperty("fgp_s_hdg", globalPropertyi("cl300/fgp_s_hdg"))

defineProperty("s_master_gain", globalPropertyi("cl300/s_master_gain"))

defineProperty("aud_voice_l_sw", globalPropertyi("cl300/aud_voice_l_sw"))
defineProperty("aud_pa_r_h", globalPropertyi("cl300/aud_pa_r_h"))
defineProperty("aud_pa_r", globalPropertyf("cl300/aud_pa_r"))
defineProperty("aud_cab_r_h", globalPropertyi("cl300/aud_cab_r_h"))
defineProperty("aud_cab_r", globalPropertyf("cl300/aud_cab_r"))
defineProperty("aud_hf2_r_h", globalPropertyi("cl300/aud_hf2_r_h"))
defineProperty("aud_hf2_r", globalPropertyf("cl300/aud_hf2_r"))
defineProperty("aud_hf1_r_h", globalPropertyi("cl300/aud_hf1_r_h"))
defineProperty("aud_hf1_r", globalPropertyf("cl300/aud_hf1_r"))
defineProperty("aud_com3_r_h", globalPropertyi("cl300/aud_com3_r_h"))
defineProperty("aud_com3_r", globalPropertyf("cl300/aud_com3_r"))
defineProperty("aud_com2_r_h", globalPropertyi("cl300/aud_com2_r_h"))
defineProperty("aud_com2_r", globalPropertyf("cl300/aud_com2_r"))
defineProperty("aud_com1_r_h", globalPropertyi("cl300/aud_com1_r_h"))
defineProperty("aud_com1_r", globalPropertyf("cl300/aud_com1_r"))

defineProperty("audio_com_selection", globalPropertyi("sim/cockpit2/radios/actuators/audio_com_selection"))


defineProperty("aud_pa_l_h", globalPropertyi("cl300/aud_pa_l_h"))
defineProperty("aud_pa_l", globalPropertyf("cl300/aud_pa_l"))
defineProperty("aud_cab_l_h", globalPropertyi("cl300/aud_cab_l_h"))
defineProperty("aud_cab_l", globalPropertyf("cl300/aud_cab_l"))
defineProperty("aud_hf2_l_h", globalPropertyi("cl300/aud_hf2_l_h"))
defineProperty("aud_hf2_l", globalPropertyf("cl300/aud_hf2_l"))
defineProperty("aud_hf1_l_h", globalPropertyi("cl300/aud_hf1_l_h"))
defineProperty("aud_hf1_l", globalPropertyf("cl300/aud_hf1_l"))
defineProperty("aud_com3_l_h", globalPropertyi("cl300/aud_com3_l_h"))
defineProperty("aud_com3_l", globalPropertyf("cl300/aud_com3_l"))
defineProperty("aud_com2_l_h", globalPropertyi("cl300/aud_com2_l_h"))
defineProperty("aud_com2_l", globalPropertyf("cl300/aud_com2_l"))
defineProperty("aud_com1_l_h", globalPropertyi("cl300/aud_com1_l_h"))
defineProperty("aud_com1_l", globalPropertyf("cl300/aud_com1_l"))



defineProperty("comps_head_l_h", globalPropertyi("cl300/comps_head_l_h"))
defineProperty("comps_head_l", globalPropertyf("cl300/comps_head_l"))
defineProperty("comps_head_r_h", globalPropertyi("cl300/comps_head_r_h"))
defineProperty("comps_head_r", globalPropertyf("cl300/comps_head_r"))

defineProperty("elt_h", globalPropertyi("cl300/elt_h"))
defineProperty("elt", globalPropertyf("cl300/elt"))

defineProperty("dcu_b_h", globalPropertyi("cl300/dcu_b_h"))
defineProperty("dcu_b", globalPropertyf("cl300/dcu_b"))
defineProperty("dcu_a_h", globalPropertyi("cl300/dcu_a_h"))
defineProperty("dcu_a", globalPropertyf("cl300/dcu_a"))

defineProperty("cab_ac_h", globalPropertyi("cl300/cab_ac_h"))
defineProperty("cab_ac", globalPropertyf("cl300/cab_ac"))
defineProperty("cab_dc_h", globalPropertyi("cl300/cab_dc_h"))
defineProperty("cab_dc", globalPropertyf("cl300/cab_dc"))

defineProperty("oxigen_terap_h", globalPropertyi("cl300/oxigen_terap_h"))
defineProperty("oxigen_terap", globalPropertyf("cl300/oxigen_terap"))

defineProperty("fire_engn_r_h", globalPropertyi("cl300/fire_engn_r_h"))
defineProperty("fire_engn_r", globalPropertyf("cl300/fire_engn_r"))
defineProperty("fire_engn_l_h", globalPropertyi("cl300/fire_engn_l_h"))
defineProperty("fire_engn_l", globalPropertyf("cl300/fire_engn_l"))
defineProperty("fire_apu_h", globalPropertyi("cl300/fire_apu_h"))
defineProperty("fire_apu", globalPropertyf("cl300/fire_apu"))
defineProperty("fire_ext_2_h", globalPropertyi("cl300/fire_ext_2_h"))
defineProperty("fire_ext_2", globalPropertyf("cl300/fire_ext_2"))
defineProperty("fire_ext_1_h", globalPropertyi("cl300/fire_ext_1_h"))
defineProperty("fire_ext_1", globalPropertyf("cl300/fire_ext_1"))
defineProperty("fire_autoapr_h", globalPropertyi("cl300/fire_autoapr_h"))
defineProperty("fire_autoapr", globalPropertyf("cl300/fire_autoapr"))
defineProperty("fire_extinguisher_on_l", globalPropertyi("sim/cockpit2/engine/actuators/fire_extinguisher_on[0]"))
defineProperty("fire_extinguisher_on_r", globalPropertyi("sim/cockpit2/engine/actuators/fire_extinguisher_on[1]"))



defineProperty("trim_pusher_h", globalPropertyi("cl300/trim_pusher_h"))
defineProperty("trim_pusher", globalPropertyf("cl300/trim_pusher"))

defineProperty("hydr_altn_flp_h", globalPropertyi("cl300/hydr_altn_flp_h"))
defineProperty("hydr_altn_flp", globalPropertyf("cl300/hydr_altn_flp"))
defineProperty("hydr_l_sov_h", globalPropertyi("cl300/hydr_l_sov_h"))
defineProperty("hydr_l_sov", globalPropertyf("cl300/hydr_l_sov"))
defineProperty("hydr_r_sov_h", globalPropertyi("cl300/hydr_r_sov_h"))
defineProperty("hydr_r_sov", globalPropertyf("cl300/hydr_r_sov"))

defineProperty("aux_hpump_h", globalPropertyi("cl300/aux_hpump_h"))
defineProperty("l_hpump_h", globalPropertyi("cl300/l_hpump_h"))
defineProperty("r_hpump_h", globalPropertyi("cl300/r_hpump_h"))
defineProperty("ptu_h", globalPropertyi("cl300/ptu_h"))

defineProperty("hd_l_engn_state", globalPropertyi("cl300/hd_l_engn_state")) -- JackZero: New Datarefs
defineProperty("hd_r_engn_state", globalPropertyi("cl300/hd_r_engn_state")) -- JackZero: New Datarefs
defineProperty("hd_l_pump_state", globalPropertyi("cl300/hd_l_pump_state")) -- JackZero: New Datarefs
defineProperty("hd_r_pump_state", globalPropertyi("cl300/hd_r_pump_state")) -- JackZero: New Datarefs
defineProperty("hd_aux_state",    globalPropertyi("cl300/hd_aux_state")) 	-- JackZero: New Datarefs
defineProperty("hd_ptu_state",    globalPropertyi("cl300/hd_ptu_state")) 	-- JackZero: New Datarefs

defineProperty("gpu_on", globalPropertyi("sim/cockpit/electrical/gpu_on"))
defineProperty("APU_switch", globalPropertyi("sim/cockpit/engine/APU_switch")) -- JackZero: Added for right fuel pump control
defineProperty("APU_N1_percent", globalPropertyf("sim/cockpit2/electrical/APU_N1_percent")) -- JackZero: Added for right fuel pump control
defineProperty("APU_generator_on", globalPropertyi("sim/cockpit2/electrical/APU_generator_on"))
defineProperty("APU_running", globalPropertyi("sim/cockpit2/electrical/APU_running"))
defineProperty("generator_on_l", globalPropertyi("sim/cockpit2/electrical/generator_on[0]"))
defineProperty("generator_on_r", globalPropertyi("sim/cockpit2/electrical/generator_on[1]"))




defineProperty("electr_apugen_h", globalPropertyi("cl300/electr_apugen_h"))
defineProperty("electr_apugen", globalPropertyf("cl300/electr_apugen"))
defineProperty("electr_gen_r_h", globalPropertyi("cl300/electr_gen_r_h"))
defineProperty("electr_gen_r", globalPropertyf("cl300/electr_gen_r"))
defineProperty("electr_gen_l_h", globalPropertyi("cl300/electr_gen_l_h"))
defineProperty("electr_gen_l", globalPropertyf("cl300/electr_gen_l"))
defineProperty("electr_stbi_h", globalPropertyi("cl300/electr_stbi_h"))
defineProperty("electr_stbi", globalPropertyf("cl300/electr_stbi"))
defineProperty("electr_extp_h", globalPropertyi("cl300/electr_extp_h"))
defineProperty("electr_extp", globalPropertyf("cl300/electr_extp"))
defineProperty("electr_extp_avail", globalPropertyf("cl300/electr_extp_avail"))

defineProperty("gen_amps_l",   globalPropertyf("cl300/gen_amps_l"))   -- JackZero: New Datarefs
defineProperty("gen_amps_r",   globalPropertyf("cl300/gen_amps_r"))   -- JackZero: New Datarefs
defineProperty("gen_amps_apu", globalPropertyf("cl300/gen_amps_apu")) -- JackZero: New Datarefs
defineProperty("gen_volts_l",   globalPropertyf("cl300/gen_volts_l"))   -- JackZero: New Datarefs
defineProperty("gen_volts_r",   globalPropertyf("cl300/gen_volts_r"))   -- JackZero: New Datarefs
defineProperty("gen_volts_apu", globalPropertyf("cl300/gen_volts_apu")) -- JackZero: New Datarefs
defineProperty("gpu_volts",     globalPropertyf("cl300/gpu_volts"))     -- JackZero: New Datarefs

defineProperty("antice_wngsource_r", globalPropertyi("cl300/antice_wngsource_r"))
defineProperty("antice_wshld_r_h", globalPropertyi("cl300/antice_wshld_r_h"))
defineProperty("antice_wshld_r", globalPropertyf("cl300/antice_wshld_r"))
defineProperty("antice_wshld_l_h", globalPropertyi("cl300/antice_wshld_l_h"))
defineProperty("antice_wshld_l", globalPropertyf("cl300/antice_wshld_l"))
defineProperty("antice_engn_r_h", globalPropertyi("cl300/antice_engn_r_h"))
defineProperty("antice_engn_r", globalPropertyf("cl300/antice_engn_r"))
defineProperty("antice_engn_l_h", globalPropertyi("cl300/antice_engn_l_h"))
defineProperty("antice_engn_l", globalPropertyf("cl300/antice_engn_l"))
defineProperty("antice_probe_r_h", globalPropertyi("cl300/antice_probe_r_h"))
defineProperty("antice_probe_r", globalPropertyf("cl300/antice_probe_r"))
defineProperty("antice_probe_l_h", globalPropertyi("cl300/antice_probe_l_h"))
defineProperty("antice_probe_l", globalPropertyf("cl300/antice_probe_l"))
defineProperty("antice_wing_h", globalPropertyi("cl300/antice_wing_h"))
defineProperty("antice_wing", globalPropertyf("cl300/antice_wing"))
defineProperty("antice_wings_xbleed_valve", globalPropertyi("cl300/antice_wings_xbleed_valve"))
defineProperty("antice_psi_l", globalPropertyf("cl300/antice_psi_l")) -- JackZero: New Dataref
defineProperty("antice_psi_r", globalPropertyf("cl300/antice_psi_r")) -- JackZero: New Dataref


defineProperty("acf_hstb_trim_dn", globalPropertyi("sim/aircraft/controls/acf_hstb_trim_dn"))
defineProperty("acf_hstb_trim_up", globalPropertyi("sim/aircraft/controls/acf_hstb_trim_up"))
defineProperty("trim_stab", globalPropertyi("cl300/trim_stab"))


defineProperty("cabin_vvi_fpm", globalPropertyf("sim/cockpit2/pressurization/actuators/cabin_vvi_fpm"))
defineProperty("cabin_altitude_ft", globalPropertyf("sim/cockpit2/pressurization/actuators/cabin_altitude_ft"))
defineProperty("pressure_man_h", globalPropertyi("cl300/pressure_man_h"))
defineProperty("pressure_man", globalPropertyf("cl300/pressure_man"))
defineProperty("pressure_ditch_h", globalPropertyi("cl300/pressure_ditch_h"))
defineProperty("pressure_ditch", globalPropertyf("cl300/pressure_ditch"))
defineProperty("pressure_emer_depr_h", globalPropertyi("cl300/pressure_emer_depr_h"))
defineProperty("pressure_emer_depr", globalPropertyf("cl300/pressure_emer_depr"))


defineProperty("aircond_tempr_cock_ddial", globalPropertyf("cl300/aircond_tempr_cock_ddial"))
defineProperty("aircond_tempr_cock_c",     globalPropertyf("cl300/aircond_tempr_cock_c")) -- JackZero: New dataref
defineProperty("aircond_tempr_cock_duct",  globalPropertyf("cl300/aircond_tempr_cock_duct")) -- JackZero: New dataref
defineProperty("aircond_tempr_cab_ddial",  globalPropertyf("cl300/aircond_tempr_cab_ddial"))
defineProperty("aircond_tempr_cab_c",      globalPropertyf("cl300/aircond_tempr_cab_c")) -- JackZero: New dataref
defineProperty("aircond_tempr_cab_duct",   globalPropertyf("cl300/aircond_tempr_cab_duct")) -- JackZero: New dataref
defineProperty("aircond_cockpit_temp", globalPropertyf("cl300/aircond_cockpit_temp"))
defineProperty("aircond_cabin_temp", globalPropertyf("cl300/aircond_cabin_temp"))
defineProperty("aircond_man_temp_h", globalPropertyi("cl300/aircond_man_temp_h"))
defineProperty("aircond_man_temp", globalPropertyf("cl300/aircond_man_temp"))
defineProperty("aircond_ramair_h", globalPropertyi("cl300/aircond_ramair_h"))
defineProperty("aircond_ramair", globalPropertyf("cl300/aircond_ramair"))
defineProperty("aircond_airsource", globalPropertyi("cl300/aircond_airsource"))
defineProperty("aircond_has_pack", globalPropertyf("cl300/aircond_has_pack")) -- JackZero: New dataref
defineProperty("aircond_has_trim", globalPropertyf("cl300/aircond_has_trim")) -- JackZero: New dataref

defineProperty("taws_ter", globalPropertyf("cl300/taws_ter"))
defineProperty("taws_flp", globalPropertyf("cl300/taws_flp"))
defineProperty("taws_gs", globalPropertyf("cl300/taws_gs"))
defineProperty("taws_ter_h", globalPropertyi("cl300/taws_ter_h"))
defineProperty("taws_flp_h", globalPropertyi("cl300/taws_flp_h"))
defineProperty("taws_gs_h", globalPropertyi("cl300/taws_gs_h"))

defineProperty("nws_h", globalPropertyi("cl300/nws_h"))
defineProperty("nws", globalPropertyf("cl300/nws"))

defineProperty("fuel_xflow_up_h", globalPropertyi("cl300/fuel_xflow_up_h"))
defineProperty("fuel_xflow_dn_h", globalPropertyi("cl300/fuel_xflow_dn_h"))
defineProperty("fuel_xflow_up", globalPropertyf("cl300/fuel_xflow_up"))
defineProperty("fuel_xflow_dn", globalPropertyf("cl300/fuel_xflow_dn"))
defineProperty("fuel_xpump_r", globalPropertyi("cl300/fuel_xpump_r"))
defineProperty("fuel_xpump_l", globalPropertyi("cl300/fuel_xpump_l"))
defineProperty("fuel_tank_transfer_from", globalPropertyi("sim/cockpit2/fuel/fuel_tank_transfer_from"))
defineProperty("fuel_tank_transfer_to", globalPropertyi("sim/cockpit2/fuel/fuel_tank_transfer_to"))
defineProperty("fuel_balanced", globalPropertyi("cl300/fuel_balanced"))
defineProperty("fuel_imbalanced", globalPropertyi("cl300/fuel_imbalanced"))
defineProperty("fuel_pump_l_astate", globalPropertyi("cl300/fuel_pump_l_astate")) -- JackZero: Added
defineProperty("fuel_pump_r_astate", globalPropertyi("cl300/fuel_pump_r_astate")) -- JackZero: Added
defineProperty("fuel_used", globalPropertyf("cl300/fuel_used")) -- JackZero: Added


defineProperty("rev_air_dat", globalPropertyi("cl300/rev_air_dat"))
defineProperty("rev_att_hd", globalPropertyi("cl300/rev_att_hd"))
defineProperty("rev_tune", globalPropertyi("cl300/rev_tune"))
defineProperty("rev_pan_l", globalPropertyi("cl300/rev_pan_l"))
defineProperty("rev_pan_r", globalPropertyi("cl300/rev_pan_r"))

defineProperty("bleed_air_mode", globalPropertyi("sim/cockpit2/pressurization/actuators/bleed_air_mode"))
defineProperty("bleed_xbleed_h", globalPropertyi("cl300/bleed_xbleed_h"))
defineProperty("bleed_xbleed", globalPropertyf("cl300/bleed_xbleed"))
defineProperty("bleed_en_l_h", globalPropertyi("cl300/bleed_en_l_h"))
defineProperty("bleed_en_l", globalPropertyf("cl300/bleed_en_l"))
defineProperty("bleed_en_r_h", globalPropertyi("cl300/bleed_en_r_h"))
defineProperty("bleed_en_r", globalPropertyf("cl300/bleed_en_r"))
defineProperty("bleed_apu_h", globalPropertyi("cl300/bleed_apu_h"))
defineProperty("bleed_apu", globalPropertyf("cl300/bleed_apu"))
defineProperty("bleed_psi_l", globalPropertyf("cl300/bleed_psi_l")) -- JackZero: New dataref
defineProperty("bleed_psi_r", globalPropertyf("cl300/bleed_psl_r")) -- JackZero: New dataref

defineProperty("altitude_dial_ft", globalPropertyf("sim/cockpit2/autopilot/altitude_dial_ft"))


defineProperty("acf_face_jet", globalPropertyf("sim/aircraft/engine/acf_face_jet"))
defineProperty("ENGN_FF_l", globalPropertyf("sim/flightmodel/engine/ENGN_FF_[0]"))
defineProperty("ENGN_FF_r", globalPropertyf("sim/flightmodel/engine/ENGN_FF_[1]"))
defineProperty("fuel_pump_on_0", globalPropertyi("sim/cockpit2/engine/actuators/fuel_pump_on[0]"))
defineProperty("fuel_pump_on_1", globalPropertyi("sim/cockpit2/engine/actuators/fuel_pump_on[1]"))

defineProperty("ENGN_running_l", globalPropertyi("sim/flightmodel/engine/ENGN_running[0]"))
defineProperty("ENGN_running_r", globalPropertyi("sim/flightmodel/engine/ENGN_running[1]"))
defineProperty("engn_mach_status", globalPropertyi("cl300/engn_mach_status"))
defineProperty("engn_mach_h", globalPropertyi("cl300/engn_mach_h"))
defineProperty("engn_mach", globalPropertyf("cl300/engn_mach"))
defineProperty("engn_ign_h", globalPropertyi("cl300/engn_ign_h"))
defineProperty("engn_ign", globalPropertyf("cl300/engn_ign"))
defineProperty("igniter_on_0", globalPropertyi("sim/cockpit2/engine/actuators/igniter_on[0]"))
defineProperty("igniter_on_1", globalPropertyi("sim/cockpit2/engine/actuators/igniter_on[1]"))
defineProperty("en_starter_l", globalPropertyi("cl300/en_starter_l"))
defineProperty("en_starter_r", globalPropertyi("cl300/en_starter_r"))
defineProperty("en_but_run_l", globalPropertyi("cl300/en_but_run_l"))
defineProperty("en_but_run_r", globalPropertyi("cl300/en_but_run_r"))

defineProperty("autop_airspeed_dial", globalPropertyf("sim/cockpit2/autopilot/airspeed_dial_kts_mach"))
defineProperty("airspeed_is_mach", globalPropertyf("sim/cockpit/autopilot/airspeed_is_mach"))

defineProperty("thrust_reverser_deploy_ratio_0", globalPropertyf("sim/flightmodel2/engines/thrust_reverser_deploy_ratio[0]"))
defineProperty("thrust_reverser_deploy_ratio_1", globalPropertyf("sim/flightmodel2/engines/thrust_reverser_deploy_ratio[1]"))

defineProperty("engine_speed_rpm_0", globalPropertyf("sim/cockpit2/engine/indicators/engine_speed_rpm[0]")) -- JackZero: Required for helpers
defineProperty("engine_speed_rpm_1", globalPropertyf("sim/cockpit2/engine/indicators/engine_speed_rpm[1]")) -- JackZero: Required for helpers


defineProperty("carets", globalPropertyi("cl300/carets"))
defineProperty("CLB_n1", globalPropertyf("cl300/CLB_n1"))
defineProperty("TO_n1", globalPropertyf("cl300/TO_n1"))
defineProperty("TO_n1_l", globalPropertyf("cl300/TO_n1_l")) -- JackZero: New Dataref
defineProperty("TO_n1_r", globalPropertyf("cl300/TO_n1_r")) -- JackZero: New Dataref


defineProperty("ISA_t", globalPropertyf("cl300/ISA_t"))
defineProperty("POINT_thrust_0", globalPropertyf("sim/flightmodel/engine/POINT_thrust[0]"))
defineProperty("POINT_thrust_1", globalPropertyf("sim/flightmodel/engine/POINT_thrust[1]"))
defineProperty("acf_tmax", globalPropertyf("sim/aircraft/engine/acf_tmax"))
defineProperty("fadec_coeff1", globalPropertyf("cl300/fadec_coeff1"))
defineProperty("fadec_coeff2", globalPropertyf("cl300/fadec_coeff2"))

defineProperty("onground_any", globalPropertyf("sim/flightmodel/failures/onground_any"))
defineProperty("onground_all", globalPropertyf("sim/flightmodel/failures/onground_all"))
defineProperty("radio_altimeter_height_ft_pilot", globalPropertyf("sim/cockpit2/gauges/indicators/radio_altimeter_height_ft_pilot"))

defineProperty("flaprat0", globalPropertyf("sim/flightmodel2/wing/flap1_deg[0]"))
defineProperty("flaprat1", globalPropertyf("sim/flightmodel2/wing/flap1_deg[1]"))
defineProperty("flaprat", globalPropertyf("sim/flightmodel/controls/flaprat"))
defineProperty("flaprqst", globalPropertyf("sim/flightmodel/controls/flaprqst"))

defineProperty("acf_sbrk_up", globalPropertyi("sim/aircraft/controls/acf_sbrk_up"))
defineProperty("acf_splr_up", globalPropertyi("sim/aircraft/controls/acf_splr_up"))
defineProperty("ground_sp_h", globalPropertyi("cl300/ground_sp_h"))
defineProperty("sbrk_h", globalPropertyf("cl300/sbrk_h"))
defineProperty("speedbrake_ratio", globalPropertyf("sim/cockpit2/controls/speedbrake_ratio"))
defineProperty("sbrkrat", globalPropertyf("sim/flightmodel/controls/sbrkrat"))
defineProperty("sbrkrqst", globalPropertyf("sim/flightmodel/controls/sbrkrqst"))
defineProperty("speedbrake1_deg0", globalPropertyf("sim/flightmodel2/wing/speedbrake1_deg[0]"))
defineProperty("speedbrake1_deg1", globalPropertyf("sim/flightmodel2/wing/speedbrake1_deg[1]"))
defineProperty("speedbrake2_deg0", globalPropertyf("sim/flightmodel2/wing/speedbrake2_deg[0]"))
defineProperty("speedbrake2_deg1", globalPropertyf("sim/flightmodel2/wing/speedbrake2_deg[1]"))


defineProperty("rel_fc_rud_L", globalPropertyf("sim/operation/failures/rel_fc_rud_L"))
defineProperty("rel_fc_rud_R", globalPropertyf("sim/operation/failures/rel_fc_rud_R"))
defineProperty("wing1r_spo2def", globalPropertyf("sim/flightmodel/controls/wing1r_spo2def"))
defineProperty("wing1l_spo2def", globalPropertyf("sim/flightmodel/controls/wing1l_spo2def"))
defineProperty("wing1r_spo1def", globalPropertyf("sim/flightmodel/controls/wing1r_spo1def"))
defineProperty("wing1l_spo1def", globalPropertyf("sim/flightmodel/controls/wing1l_spo1def"))
defineProperty("ground_spoil_h", globalPropertyi("cl300/ground_spoil_h"))
defineProperty("flight_spoil_h", globalPropertyi("cl300/flight_spoil_h"))
defineProperty("flight_spoil", globalPropertyf("cl300/flight_spoil"))
defineProperty("rel_fc_ail_L", globalPropertyi("sim/operation/failures/rel_fc_ail_L"))
defineProperty("rel_fc_ail_R", globalPropertyi("sim/operation/failures/rel_fc_ail_R"))



defineProperty("barometer_current_inhg", globalPropertyf("sim/weather/barometer_current_inhg"))
defineProperty("temperature_ambient_c", globalPropertyf("sim/weather/temperature_ambient_c"))

defineProperty("rembf1", globalPropertyi("cl300/rembf1"))
defineProperty("rembf2", globalPropertyi("cl300/rembf2"))

defineProperty("rembf4", globalPropertyi("cl300/rembf4"))
defineProperty("rembf5", globalPropertyi("cl300/rembf5"))

defineProperty("rembf6", globalPropertyi("cl300/rembf6"))
defineProperty("rembf7", globalPropertyi("cl300/rembf7"))
defineProperty("rembf8", globalPropertyi("cl300/rembf8"))

defineProperty("rembf11", globalPropertyi("cl300/rembf11"))
defineProperty("rembf12", globalPropertyi("cl300/rembf12"))

defineProperty("rembf13", globalPropertyi("cl300/rembf13"))
defineProperty("rembf14", globalPropertyi("cl300/rembf14"))

defineProperty("rel_AOA", globalPropertyf("sim/operation/failures/rel_AOA"))
defineProperty("rel_ice_detect", globalPropertyf("sim/operation/failures/rel_ice_detect"))
defineProperty("rel_pitot", globalPropertyf("sim/operation/failures/rel_pitot"))
defineProperty("rel_pitot2", globalPropertyf("sim/operation/failures/rel_pitot2"))
defineProperty("rel_engfai0", globalPropertyf("sim/operation/failures/rel_engfai0"))
defineProperty("rel_engfai1", globalPropertyf("sim/operation/failures/rel_engfai1"))

defineProperty("steer_handle", globalPropertyf("cl300/steer_handle"))
defineProperty("rembf10", globalPropertyi("cl300/rembf10"))

--controls overrides

defineProperty("dtr_s_l", globalPropertyi("cl300/s_l"))
defineProperty("dtr_s_r", globalPropertyi("cl300/s_r"))

defineProperty("ENGN_mixt_0", globalPropertyi("sim/flightmodel/engine/ENGN_mixt[0]"))
defineProperty("ENGN_mixt_1", globalPropertyi("sim/flightmodel/engine/ENGN_mixt[1]"))
defineProperty("ENGN_N1_L", globalPropertyf("sim/flightmodel/engine/ENGN_N1_[0]"))
defineProperty("ENGN_N1_R", globalPropertyf("sim/flightmodel/engine/ENGN_N1_[1]"))
defineProperty("ENGN_thro_0", globalPropertyf("sim/flightmodel/engine/ENGN_thro[0]"))
defineProperty("ENGN_thro_1", globalPropertyf("sim/flightmodel/engine/ENGN_thro[1]"))
defineProperty("ENGN_thro_use_0", globalPropertyf("sim/flightmodel/engine/ENGN_thro_use[0]"))
defineProperty("ENGN_thro_use_1", globalPropertyf("sim/flightmodel/engine/ENGN_thro_use[1]"))
defineProperty("override_throttles", globalPropertyi("sim/operation/override/override_throttles"))
defineProperty("override_cs", globalPropertyi("sim/operation/override/override_control_surfaces"))
defineProperty("override_fc", globalPropertyi("sim/operation/override/override_flightcontrol"))
defineProperty("override_gearbrake", globalPropertyf("sim/operation/override/override_gearbrake"))


defineProperty("parkbrake", globalPropertyf("sim/flightmodel/controls/parkbrake"))

defineProperty("velocity_vx", globalPropertyf("sim/flightmodel/position/local_vx"))
defineProperty("velocity_vy", globalPropertyf("sim/flightmodel/position/local_vy"))
defineProperty("velocity_vz", globalPropertyf("sim/flightmodel/position/local_vz"))


defineProperty("hydr_pres_1", globalPropertyf("sim/cockpit2/hydraulics/indicators/hydraulic_pressure_1"))
defineProperty("hydr_pres_2", globalPropertyf("sim/cockpit2/hydraulics/indicators/hydraulic_pressure_2"))
defineProperty("electric_hydraulic_pump_on", globalPropertyi("sim/cockpit2/switches/electric_hydraulic_pump_on"))



defineProperty("acf_elev_dn", globalPropertyf("sim/aircraft/controls/acf_elev_dn"))
defineProperty("acf_elev_up", globalPropertyf("sim/aircraft/controls/acf_elev_up"))
defineProperty("hstab1_elv1def", globalPropertyf("sim/flightmodel/controls/hstab1_elv1def"))
defineProperty("hstab2_elv1def", globalPropertyf("sim/flightmodel/controls/hstab2_elv1def"))
defineProperty("elevator1_deg0", globalPropertyf("sim/flightmodel2/wing/elevator1_deg[0]"))



defineProperty("wing1l_ail1def", globalPropertyf("sim/flightmodel/controls/wing1l_ail1def"))
defineProperty("wing1r_ail1def", globalPropertyf("sim/flightmodel/controls/wing1r_ail1def"))
defineProperty("wing1l_elv1def", globalPropertyf("sim/flightmodel/controls/wing1l_elv1def"))
defineProperty("wing1r_elv1def", globalPropertyf("sim/flightmodel/controls/wing1r_elv1def"))
defineProperty("ldruddef", globalPropertyf("sim/flightmodel/controls/ldruddef"))
defineProperty("rdruddef", globalPropertyf("sim/flightmodel/controls/rdruddef"))
defineProperty("yoke_pitch_ratio", globalPropertyf("sim/cockpit2/controls/yoke_pitch_ratio"))
defineProperty("yoke_roll_ratio", globalPropertyf("sim/cockpit2/controls/yoke_roll_ratio"))
defineProperty("yoke_heading_ratio", globalPropertyf("sim/cockpit2/controls/yoke_heading_ratio"))
defineProperty("nosewheel_steer_on", globalPropertyf("sim/cockpit2/controls/nosewheel_steer_on"))

defineProperty("acf_nw_steerdeg1", globalPropertyf("sim/aircraft/gear/acf_nw_steerdeg1"))
defineProperty("acf_nw_steerdeg2", globalPropertyf("sim/aircraft/gear/acf_nw_steerdeg2"))

defineProperty("l_brake_add", globalPropertyf("sim/flightmodel/controls/l_brake_add"))
defineProperty("r_brake_add", globalPropertyf("sim/flightmodel/controls/r_brake_add"))




defineProperty("cloud_base_1", globalPropertyf("sim/weather/cloud_base_msl_m[0]"))
defineProperty("cloud_tops_1", globalPropertyf("sim/weather/cloud_tops_msl_m[0]"))


--

--Throttle



defineProperty("ENGN_N1_L", globalPropertyf("sim/flightmodel/engine/ENGN_N1_[0]"))
defineProperty("ENGN_N1_R", globalPropertyf("sim/flightmodel/engine/ENGN_N1_[1]"))

defineProperty("throttle_ratio_all", globalPropertyf("sim/cockpit2/engine/actuators/throttle_ratio_all"))
defineProperty("gust_lock", globalPropertyf("cl300/gust_lock"))
defineProperty("gust_lock_anm", globalPropertyf("cl300/gust_lock_anm"))

defineProperty("thr_anm1", globalPropertyf("cl300/thr_anm1"))
defineProperty("thr_anm2", globalPropertyf("cl300/thr_anm2"))

defineProperty("sim_thr_anm1", globalPropertyf("sim/cockpit2/engine/actuators/throttle_ratio[0]"))
defineProperty("sim_thr_anm2", globalPropertyf("sim/cockpit2/engine/actuators/throttle_ratio[1]"))


defineProperty("prop_mode_0", globalPropertyi("sim/cockpit2/engine/actuators/prop_mode[0]"))
defineProperty("prop_mode_1", globalPropertyi("sim/cockpit2/engine/actuators/prop_mode[1]"))
defineProperty("reverser_on_1", globalPropertyi("sim/cockpit2/annunciators/reverser_on[0]"))
defineProperty("reverser_on_2", globalPropertyi("sim/cockpit2/annunciators/reverser_on[1]"))


---


defineProperty("menu_type", globalPropertyi("cl300/menu_type"))

defineProperty("rain_back", globalPropertyf("cl300/rain_back"))
defineProperty("rain_scr1", globalPropertyf("cl300/rain_scr1"))
defineProperty("rain_scr2", globalPropertyf("cl300/rain_scr2"))
defineProperty("rain_scr3", globalPropertyf("cl300/rain_scr3"))
defineProperty("rain_scr4", globalPropertyf("cl300/rain_scr4"))
defineProperty("rain_drop1", globalPropertyf("cl300/rain_drop1"))
defineProperty("rain_move1", globalPropertyf("cl300/rain_move1"))
defineProperty("rain_move2", globalPropertyf("cl300/rain_move2"))
defineProperty("rain_percent", globalPropertyf("sim/weather/rain_percent"))
defineProperty("airport_beacon_rot", globalPropertyf("sim/graphics/animation/airport_beacon_rotation"))

defineProperty("disp", globalPropertyf("cl300/disp"))
defineProperty("disp2", globalPropertyf("cl300/disp2"))
defineProperty("disp3", globalPropertyf("cl300/disp3"))

defineProperty("ind_speed", globalPropertyf("sim/flightmodel/position/indicated_airspeed"))
defineProperty("ground_speed", globalPropertyf("sim/flightmodel/position/groundspeed"))

defineProperty("acf_hdg", globalPropertyf("sim/flightmodel/position/psi"))

defineProperty("customslider4", globalPropertyi("cl300/customslider4"))
defineProperty("customslider4_r", globalPropertyf("cl300/customslider4_r"))
defineProperty("customslider3", globalPropertyi("cl300/customslider3"))
defineProperty("customslider3_r", globalPropertyf("cl300/customslider3_r"))
defineProperty("customslider2", globalPropertyi("cl300/customslider2"))
defineProperty("customslider2_r", globalPropertyf("cl300/customslider2_r"))
defineProperty("customslider1", globalPropertyi("cl300/customslider1"))
defineProperty("customslider1_r", globalPropertyf("cl300/customslider1_r"))
defineProperty("totaltime", globalPropertyf("sim/time/total_running_time_sec"))
defineProperty("rel_depres_fast", globalPropertyi("sim/operation/failures/rel_depres_fast"))



-- electrical
defineProperty("right_bat_h", globalPropertyi("cl300/right_bat_h"))
defineProperty("right_bat", globalPropertyf("cl300/right_bat"))
defineProperty("left_bat_h", globalPropertyi("cl300/left_bat_h"))
defineProperty("left_bat", globalPropertyf("cl300/left_bat"))
defineProperty("bus_tie_h", globalPropertyf("cl300/bus_tie_h"))
defineProperty("bus_tie", globalPropertyf("cl300/bus_tie"))
defineProperty("cross_tie", globalPropertyf("sim/cockpit2/electrical/cross_tie"))
defineProperty("electr_apugen_l", globalPropertyi("cl300/electr_apugen_l"))
defineProperty("electr_apugen_r", globalPropertyi("cl300/electr_apugen_r"))


defineProperty("elbus1", globalPropertyf("sim/cockpit2/electrical/bus_volts[0]"))
defineProperty("elbus2", globalPropertyf("sim/cockpit2/electrical/bus_volts[1]"))
defineProperty("elbus3", globalPropertyf("sim/cockpit2/electrical/bus_volts[2]"))

defineProperty("bat_on", globalPropertyi("sim/cockpit2/electrical/battery_on[0]"))
defineProperty("bat_on2", globalPropertyi("sim/cockpit2/electrical/battery_on[1]"))
defineProperty("bat_on3", globalPropertyi("sim/cockpit2/electrical/battery_on[2]"))

defineProperty("battery_amps_0", globalPropertyi("sim/cockpit2/electrical/battery_amps[0]"))
defineProperty("battery_amps_1", globalPropertyi("sim/cockpit2/electrical/battery_amps[1]"))
defineProperty("battery_amps_2", globalPropertyi("sim/cockpit2/electrical/battery_amps[2]"))

defineProperty("APU_generator_amps", globalPropertyi("sim/cockpit2/electrical/APU_generator_amps"))
defineProperty("generator_amps_l", globalPropertyf("sim/cockpit2/electrical/generator_amps[0]"))
defineProperty("generator_amps_r", globalPropertyf("sim/cockpit2/electrical/generator_amps[1]"))
defineProperty("gpu_amps", globalPropertyf("sim/cockpit/electrical/gpu_amps"))


--lights

defineProperty("avion_lit1", globalPropertyf("cl300/avion_lit1"))
defineProperty("avion_lit2", globalPropertyf("cl300/avion_lit2"))
defineProperty("avion_lit3", globalPropertyf("cl300/avion_lit3"))

defineProperty("stby_ins_lit", globalPropertyf("cl300/stby_ins_lt"))
defineProperty("stby_ins_dim_brt", globalPropertyi("cl300/stby_ins_dim_brt"))

defineProperty("crewlit", globalPropertyf("cl300/crewlit"))
defineProperty("crewlit_h", globalPropertyf("cl300/crewlit_h"))
defineProperty("crewlit2", globalPropertyf("cl300/crewlit2"))
defineProperty("crewlit2_h", globalPropertyf("cl300/crewlit2_h"))
defineProperty("entrlight", globalPropertyf("cl300/entrlight"))
defineProperty("entrlight_h", globalPropertyf("cl300/entrlight_h"))
defineProperty("cablight", globalPropertyf("cl300/cablight"))
defineProperty("cablight_h", globalPropertyf("cl300/cablight_h"))
defineProperty("emeright", globalPropertyf("cl300/emeright"))
defineProperty("emeright_h", globalPropertyf("cl300/emeright_h"))
defineProperty("landlight1", globalPropertyf("cl300/landlight1"))
defineProperty("landlight2", globalPropertyf("cl300/landlight2"))
defineProperty("landlight1_h", globalPropertyf("cl300/landlight1_h"))
defineProperty("landlight2_h", globalPropertyf("cl300/landlight2_h"))
defineProperty("xap_taxilight_h", globalPropertyf("cl300/xap_taxilight_h"))
defineProperty("xap_taxilight", globalPropertyf("cl300/xap_taxilight"))
defineProperty("xap_taxilight2", globalPropertyf("cl300/xap_taxilight2"))
defineProperty("beaconlights", globalPropertyi("sim/cockpit/electrical/beacon_lights_on"))
defineProperty("stroblights", globalPropertyi("sim/cockpit/electrical/strobe_lights_on"))
defineProperty("strobe_h", globalPropertyf("cl300/strobe_h"))
defineProperty("nav_h", globalPropertyf("cl300/nav_h"))
defineProperty("logo", globalPropertyf("cl300/logo"))
defineProperty("navlights", globalPropertyi("sim/cockpit/electrical/nav_lights_on"))
defineProperty("landlights", globalPropertyi("sim/cockpit/electrical/landing_lights_on"))
defineProperty("landlights_2", globalPropertyi("sim/cockpit2/switches/landing_lights_switch[0]"))
defineProperty("landlights_1", globalPropertyi("sim/cockpit2/switches/landing_lights_switch[1]"))
defineProperty("landlights_rat", globalPropertyi("sim/flightmodel2/lights/landing_lights_brightness_ratio"))


defineProperty("beac_brt_rat", globalPropertyf("sim/flightmodel2/lights/beacon_brightness_ratio"))

defineProperty("beac_brt", globalPropertyf("cl300/beac_brt"))

defineProperty("wing_insp_h", globalPropertyf("cl300/wing_insp_h"))
defineProperty("wing_insp", globalPropertyf("cl300/wing_insp"))
defineProperty("wing_insp_b", globalPropertyf("cl300/wing_insp_b"))


defineProperty("avpow", globalPropertyi("sim/cockpit2/switches/avionics_power_on"))
defineProperty("cockavl", globalPropertyf("cl300/cockavl"))
defineProperty("cockavl_p", globalPropertyf("cl300/cockavl_p"))
defineProperty("cockavl_m", globalPropertyf("cl300/cockavl_m"))
defineProperty("cockavr", globalPropertyf("cl300/cockavr"))
defineProperty("cockavr_p", globalPropertyf("cl300/cockavr_p"))
defineProperty("cockavr_m", globalPropertyf("cl300/cockavr_m"))

defineProperty("night_lights", globalPropertyf("sim/graphics/scenery/percent_lights_on"))

defineProperty("temp_cabin", globalPropertyf("cl300/temp_cabin"))
defineProperty("glass_ice_cockpit", globalPropertyf("cl300/glass_ice_cockpit"))
defineProperty("glass_ice_cockpit2", globalPropertyf("cl300/glass_ice_cockpit2"))


defineProperty("cmfd_h", globalPropertyf("cl300/cmfd_h"))
defineProperty("cmfd", globalPropertyf("cl300/cmfd"))

defineProperty("cpfd_h", globalPropertyf("cl300/cpfd_h"))
defineProperty("cpfd", globalPropertyf("cl300/cpfd"))

defineProperty("pmfd_h", globalPropertyf("cl300/pmfd_h"))
defineProperty("pmfd", globalPropertyf("cl300/pmfd"))

defineProperty("ppfd_h", globalPropertyf("cl300/ppfd_h"))
defineProperty("ppfd", globalPropertyf("cl300/ppfd"))


defineProperty("dome_h", globalPropertyf("cl300/dome_h"))
defineProperty("dome", globalPropertyf("cl300/dome"))

defineProperty("annun_h", globalPropertyf("cl300/annun_h"))
defineProperty("annun", globalPropertyf("cl300/annun"))

defineProperty("cbp_h", globalPropertyf("cl300/cbp_h"))
defineProperty("cbp", globalPropertyf("cl300/cbp"))

defineProperty("gshldr_h", globalPropertyf("cl300/gshldr_h"))
defineProperty("gshldr", globalPropertyf("cl300/gshldr"))

defineProperty("pedestal_h", globalPropertyf("cl300/pedestal_h"))
defineProperty("pedestal", globalPropertyf("cl300/pedestal"))

defineProperty("gshldl_h", globalPropertyf("cl300/gshldl_h"))
defineProperty("gshldl", globalPropertyf("cl300/gshldl"))

defineProperty("sunglases", globalPropertyi("sim/cockpit/electrical/sunglasses_on"))

defineProperty("view", globalPropertyf("sim/graphics/view/view_is_external"))

defineProperty("sunscreen21", globalPropertyf("cl300/sunscreen21"))
defineProperty("sunscreen11", globalPropertyf("cl300/sunscreen11"))

defineProperty("tire_steer_comm", globalPropertyf("sim/flightmodel2/gear/tire_steer_command_deg[0]"))
defineProperty("tire_steer_act", globalPropertyf("sim/flightmodel/parts/tire_steer_act[0]"))






defineProperty("cockpit_light_level_r", globalPropertyi("sim/graphics/misc/cockpit_light_level_r"))
defineProperty("cockpit_light_level_g", globalPropertyi("sim/graphics/misc/cockpit_light_level_g"))
defineProperty("cockpit_light_level_b", globalPropertyi("sim/graphics/misc/cockpit_light_level_b"))
defineProperty("cockpit_lights_on", globalPropertyi("sim/cockpit/electrical/cockpit_lights_on"))


defineProperty("gear_dep_0", globalPropertyf("sim/flightmodel2/gear/deploy_ratio[0]"))
defineProperty("taxi", globalPropertyf("sim/cockpit/electrical/taxi_light_on"))
defineProperty("emer_door_lit", globalPropertyf("cl300/emer_door_lit"))
defineProperty("entry_lit", globalPropertyf("sim/cockpit2/switches/custom_slider_on[0]"))
defineProperty("cabin_lit", globalPropertyf("sim/cockpit2/switches/custom_slider_on[4]"))
defineProperty("salon_lit", globalPropertyf("sim/flightmodel2/misc/custom_slider_ratio[4]"))
defineProperty("salon_lt_lvl", globalPropertyf("cl300/salon_lt_lvl"))
defineProperty("stair_lt_lvl", globalPropertyf("cl300/stair_lt_lvl"))
defineProperty("salon_lt_lvl_full", globalPropertyf("cl300/salon_lt_lvl_full"))

defineProperty("anns_lit", globalPropertyf("sim/flightmodel2/misc/custom_slider_ratio[2]"))
defineProperty("emer_door_open", globalPropertyf("cl300/customslider4_r"))

defineProperty("belt", globalPropertyi("cl300/belt"))
defineProperty("smoke", globalPropertyi("cl300/smoke"))
defineProperty("smokebelts_h", globalPropertyi("cl300/smokebelts_h"))

defineProperty("wave_limit_n", globalPropertyi("cl300/wave_limit_n")) -- wave limit value
defineProperty("turbulence0", globalPropertyi("sim/weather/turbulence[0]"))
defineProperty("turbulence1", globalPropertyi("sim/weather/turbulence[1]"))
defineProperty("turbulence2", globalPropertyi("sim/weather/turbulence[2]"))

defineProperty("starter_duration_l", globalPropertyi("sim/cockpit/engine/starter_duration[0]"))
defineProperty("starter_duration_r", globalPropertyi("sim/cockpit/engine/starter_duration[1]"))

-- JackZero: Dataref for flightplan access and fms based landing alt
defineProperty("flgtplan_show_entry", globalPropertyi("cl300/flgtplan_show_entry"))
defineProperty("pressure_lndg_alt_1", globalPropertyi("cl300/pressure_lndg_alt_1"))
defineProperty("pressure_lndg_alt_2", globalPropertyf("cl300/pressure_lndg_alt_2"))
-- JackZero: End

-- JackZero: Additional datarefs
defineProperty("engine_state_l", globalPropertyi("cl300/engine_state_l")) -- similar to carets but per engine and with reverse
defineProperty("engine_state_r", globalPropertyi("cl300/engine_state_r"))
defineProperty("engine_state_rpm_l", globalPropertyi("cl300/engine_state_rpm_l")) -- rpm > 1000, gustlock off, reversers < 0.1 ?
defineProperty("engine_state_rpm_r", globalPropertyi("cl300/engine_state_rpm_r"))
-- JackZero: End

-- JackZero: Update conditional Vspeeds
defineProperty("acf_Vno", globalPropertyf("sim/aircraft/view/acf_Vno"))
-- JackZero: End

defineProperty("use_xhsi", globalPropertyi("cl300/use_xhsi")) -- JackZero: XHSI present?
if get(use_xhsi) then
	defineProperty("xhsi_nd_pilot_map_zoomin",  globalPropertyi("xhsi/nd_pilot/map_zoomin"))
end


starter_l = findCommand("sim/starters/engage_starter_1")
starter_r = findCommand("sim/starters/engage_starter_2")




aut_servos_toggle = findCommand("sim/autopilot/servos_toggle")
aut_altitude_hold = findCommand("sim/autopilot/altitude_hold")
aut_vnav = findCommand("sim/autopilot/vnav")
aut_vertical_speed = findCommand("sim/autopilot/vertical_speed")
aut_level_change = findCommand("sim/autopilot/level_change")
aut_back_course = findCommand("sim/autopilot/back_course")
aut_approach = findCommand("sim/autopilot/approach")
aut_heading = findCommand("sim/autopilot/heading")
aut_NAV = findCommand("sim/autopilot/NAV")
aut_fdir_toggle = findCommand("sim/autopilot/fdir_toggle")

aut_airspeed_down = findCommand("sim/autopilot/airspeed_down")
aut_airspeed_up = findCommand("sim/autopilot/airspeed_up")
aut_heading_down = findCommand("sim/autopilot/heading_down")
aut_heading_up = findCommand("sim/autopilot/heading_up")
aut_altitude_down = findCommand("sim/autopilot/altitude_down")
aut_altitude_up = findCommand("sim/autopilot/altitude_up")
aut_vertical_speed_down = findCommand("sim/autopilot/vertical_speed_down")
aut_vertical_speed_up = findCommand("sim/autopilot/vertical_speed_up")
aut_yawd = findCommand("sim/systems/yaw_damper_toggle")

defineProperty("acf_flap_deftime", globalPropertyi("sim/aircraft/controls/acf_flap_deftime"))



clock_timer_start_stop = findCommand("sim/instruments/timer_start_stop")
clock_timer_reset = findCommand("sim/instruments/timer_reset")
knots_mach_toggle = findCommand("sim/autopilot/knots_mach_toggle")

-- JackZero: Click sound for BRG SRC...
click5 = loadSample('sounds/click5.wav')


throttle_use_0 = 0
throttle_use_1 = 0

--set(cross_tie, 0)

set(carets, 0)
set(xap_taxilight, 0)
set(emer_door_lit, 0)
--[=[
set(en_starter_l, 1)
set(en_starter_r, 1)

set(en_but_run_l, 1)
set(en_but_run_r, 1)--]=]

--set(left_bat_h, 1)
--set(right_bat_h, 1)

set(altitude_dial_ft, 5000)

--set(nws_h, 1)


set(pedals_fwd_bwd_l, 0)
set(pedals_fwd_bwd_r, 0)

set(stby_ins_dim_brt, 0)
--set(electr_stbi_h, 1)

set(clock_timer_h, 0)

set(engn_mach_status, 0)

set(ice_detect_on, 1)
--set(ice_all_on, 1)

--set(antice_wngsource_r, 1)

set(map_mode, 0)


set(EFIS_airport_on, 1)
set(EFIS_fix_on, 1)
set(EFIS_vor_on, 1)
set(EFIS_ndb_on, 1)

set(elevator_trim, -0.145)

set(radio_altimeter_bug_ft_pilot, 600)

set(warn_bb,1)

elec_on = 0


if get(timer_running) == 1 then
commandOnce(clock_timer_start_stop)
commandOnce(clock_timer_reset)
end

local gr_sp_deployed = 0
local speed_lim = get(ground_speed)
local anm_time = 0
local drop_anm_time = 0
local drop_anm_time2 = 0
local annun_coeff = 0
local gr_sp_armed = 0
local throttle_to = 0
local check_thr_rev = 0

spoiler_h_last = 0
gr_sp_deployed_last = 0

local temp_coeff = {0}
local baro_coeff = {0}

local s_l = 0
local s_r = 0

local r_cas = 0
local y_cas = 0

alt_coeff = 150

brgsrc_hide = {0,0,0}

ping_temp = {0,0,0,0}

thrust = {30363,0}

ISA_t_tmp = {get(ISA_t)}


rain_perc_tmp = {0}

thrust_temp_coeff = 0

counter_1 = 0
counter_2 = 0
counter_3 = 0
counter_4 = 0
counter_5 = 0
counter_6 = 0
counter_7 = 0
counter_8 = 0
counter_9 = 0
counter_10 = 0
counter_11 = 0
counter_12 = 0
counter_13 = 0
counter_14 = 0
counter_15 = 0
counter_16 = 0
counter_17 = 0
counter_18 = 0


time_now = {get(totaltime),get(totaltime),get(totaltime),get(totaltime),get(totaltime),get(totaltime),get(totaltime),get(totaltime)}
last_states = {get(customslider1), get(customslider2), get(customslider3), get(customslider4), get(rembf10),get(rembf11),get(rembf12), get(rembf2), get(rembf7), get(rembf1), get(rembf6), get(rembf4),get(rembf8),get(prop_mode_0),get(prop_mode_1), get(sbrk_h), gr_sp_deployed,get(en_starter_l),get(en_starter_r),get(clock_timer_h),get(engn_mach_status),get(ENGN_thro_0),get(engn_mach_h),get(onground_all), get(pressure_manrate), get(dcp_range),get(rembf5),get(rembf13),get(rembf14),get(speedbrake_ratio)} -- JackZero: Added speedbrake_ratio
last_states_r = {get(customslider1_r), get(customslider2_r), get(customslider3_r), get(customslider4_r)}

mach_hold_target = {0,0,0,0,0}

coms_last = {get(aud_com1_r_h),get(aud_com2_r_h),get(aud_com3_r_h),get(aud_com1_l_h),get(aud_com2_l_h),get(aud_com3_l_h),get(autop_brgsrc),get(dcp_navsrc),get(dcp_refs),get(mfd_sel_state), get(mfd_sel_state_h)}

refspds_last = {get(refspds_v1),get(refspds_vr),get(refspds_v2),get(refspds_vt),get(refspds_vga),get(refspds_vref),get(rev_air_dat)}

caution_last = {get(red_cas),get(yellow_cas)}

systest_last = {get(test_rot),get(test_push),}

starter_last = {get(starter_duration_l),get(starter_duration_r)}

running_last = {get(ENGN_running_l), get(ENGN_running_r), get(APU_running)} -- JackZero: new

-- JackZero: Moved here from pfd.lua, changed to now just store the argument
bank_alt_last = get(altitude_ft_pilot) > 31600

-- JackZero: Changed first item from "electr_extp_h" to "gpu_on", we want to register whether the gpu is really connected, not if the button is pressed in flight!
electric_last = {get(gpu_on),get(ENGN_running_l),get(ENGN_running_r),get(APU_running),get(dcp_menu1_rot)}
-- JackZero: End

-- JackZero: Linking rudder trim to trim switch:
ruddertrim_last = {get(rudder_trim),get(trim_rud)}
-- JackZero: End

-- JackZero: Update conditional Vspeeds
vspeed_last = {get(acf_Vno)}
--
trim_rud_tmp = 0.0

-- Commands

--Battery commands right_bat_h
bat_left = findCommand("cl300/electrical/bat_left")
 function bat_leftHandler(phase)
     if 0 == phase then
        if get(left_bat_h) > 0 then set(left_bat_h, 0) else set(left_bat_h, 1) end
     end
     return 1
 end
registerCommandHandler(bat_left, 1, bat_leftHandler);

bat_right = findCommand("cl300/electrical/bat_right")
 function bat_rightHandler(phase)
     if 0 == phase then
        if get(right_bat_h) > 0 then set(right_bat_h, 0) else set(right_bat_h, 1) end
     end
     return 1
 end
registerCommandHandler(bat_right, 1, bat_rightHandler);

bat_both = findCommand("cl300/electrical/bat_both")
 function bat_bothHandler(phase)
     if 0 == phase then
        if get(left_bat_h) > 0 and get(right_bat_h) > 0 then
		set(left_bat_h, 0)
		set(right_bat_h, 0)
		else
		set(left_bat_h, 1)
		set(right_bat_h, 1)
		end
     end
     return 1
 end
registerCommandHandler(bat_both, 1, bat_bothHandler);


--Engine run commands
engn_run_left = findCommand("cl300/engines/run_left")
 function engn_run_leftHandler(phase)
     if 0 == phase then
        if get(en_but_run_l) > 0 then set(en_but_run_l, 0) else set(en_but_run_l, 1) end
     end
     return 1
 end
registerCommandHandler(engn_run_left, 1, engn_run_leftHandler);

engn_run_right = findCommand("cl300/engines/run_right")
 function engn_run_rightHandler(phase)
     if 0 == phase then
        if get(en_but_run_r) > 0 then set(en_but_run_r, 0) else set(en_but_run_r, 1) end
     end
     return 1
 end
registerCommandHandler(engn_run_right, 1, engn_run_rightHandler);

engn_run_both = findCommand("cl300/engines/run_both")
 function engn_run_bothHandler(phase)
     if 0 == phase then
        if get(en_but_run_l) > 0 and get(en_but_run_r) > 0 then
		set(en_but_run_r, 0)
		set(en_but_run_l, 0)
		else
		set(en_but_run_r, 1)
		set(en_but_run_l, 1)
		end
     end
     return 1
 end
registerCommandHandler(engn_run_both, 1, engn_run_bothHandler);


--Rudder trim knob animation

rudder_trim_right_toggle = findCommand("sim/flight_controls/rudder_trim_right")
 function rudder_trim_right_toggleHandler(phase)
     if 1 == phase then
		set(trim_rud, 1)
		else
		set(trim_rud, 0)
     end
     return 1
 end
registerCommandHandler(rudder_trim_right_toggle, 0, rudder_trim_right_toggleHandler);

rudder_trim_left_toggle = findCommand("sim/flight_controls/rudder_trim_left")
 function rudder_trim_left_toggleHandler(phase)
     if 1 == phase then
		set(trim_rud, -1)
		else
		set(trim_rud, 0)
     end
     return 1
 end
registerCommandHandler(rudder_trim_left_toggle, 0, rudder_trim_left_toggleHandler);



createCommand("cl300/DCP/navsrc_toggle", "NAV Source button toggle")
navsrc_toggle = findCommand("cl300/DCP/navsrc_toggle")
 function navsrc_toggleHandler(phase)
     if 0 == phase then
        -- JackZero: Save/recall last selection:
        if get(dcp_navsrc) < 0 then
            set(dcp_navsrc, (get(HSI_source_select_pilot) + 1) % 3)
        else
            set(dcp_navsrc, (get(dcp_navsrc) + 1) % 3)
        end
     end
     return 1
 end
registerCommandHandler(navsrc_toggle, 1, navsrc_toggleHandler);
--Autopilot select +mouse wheel

createCommand("cl300/autop/autop_alt_dial_up", "Altitude select knob 100ft up")
autop_alt_dial_up = findCommand("cl300/autop/autop_alt_dial_up")
--counter_tmp = 0
 function autop_alt_dial_upHandler(phase)
	--counter_tmp = counter_tmp + 34*get(frame_rate_period)
     if 1 == phase then --and counter_tmp > 2 then
	 set(altitude_dial_ft, math.ceil(get(altitude_dial_ft)/100) * 100 + 100)
	 --counter_tmp = 0
     end
     return 1
 end
registerCommandHandler(autop_alt_dial_up, 1, autop_alt_dial_upHandler);

createCommand("cl300/autop/autop_alt_dial_dn", "Altitude select knob 100ft down")
autop_alt_dial_dn = findCommand("cl300/autop/autop_alt_dial_dn")
 function autop_alt_dial_dnHandler(phase)
     if 1 == phase and get(altitude_dial_ft) > 0 then
	 set(altitude_dial_ft, math.ceil(get(altitude_dial_ft)/100) * 100 - 100)
     end
     return 1
 end
registerCommandHandler(autop_alt_dial_dn, 0, autop_alt_dial_dnHandler);

--
createCommand("cl300/DCP/brgsrc_toggle", "BRG Source button toggle")
brgsrc_toggle = findCommand("cl300/DCP/brgsrc_toggle")
 function brgsrc_toggleHandler(phase)
     if 0 == phase then
		playSample(click5, 0)
        -- JackZero: Save/recall last selection:
        if get(autop_brgsrc) < 0 then
            set(autop_brgsrc, -get(autop_brgsrc)-1)
        else
            if     get(autop_brgsrc) == 0 then if get(EFIS_1_selection_pilot) == 0 then set(EFIS_1_selection_pilot, 2) else set(EFIS_1_selection_pilot,  0) end
            elseif get(autop_brgsrc) == 1 then if get(EFIS_2_selection_pilot) == 0 then set(EFIS_2_selection_pilot, 2) else set(EFIS_2_selection_pilot,  0) end
			end
        end
     end
     return 1
 end
registerCommandHandler(brgsrc_toggle, 1, brgsrc_toggleHandler);

defineProperty("acf_has_SC_fd", globalPropertyi("sim/aircraft/view/acf_has_SC_fd"))
defineProperty("acf_has_DC_fd", globalPropertyi("sim/aircraft/overflow/acf_has_DC_fd"))
createCommand("cl300/pfd/fd_cue_toggle", "Flight Dir Cue Toggle")
fd_cue_toggle = findCommand("cl300/pfd/fd_cue_toggle")
 function fd_cue_toggleHandler(phase)
     if 0 == phase then
		if get(acf_has_SC_fd) > 0 then
		set(acf_has_SC_fd, 0)
		set(acf_has_DC_fd, 1)
		else
		set(acf_has_SC_fd, 1)
		set(acf_has_DC_fd, 0)
		end
     end
     return 1
 end
registerCommandHandler(fd_cue_toggle, 1, fd_cue_toggleHandler);



createCommand("cl300/engines/gust_lock_toggle", "Engines Gust Lock Toggle")
gust_lock_toggle = findCommand("cl300/engines/gust_lock_toggle")
 function gust_lock_toggleHandler(phase)
     if 0 == phase then
		if get(gust_lock) > 0 then set(gust_lock, 0) else set(gust_lock, 1) end
     end
     return 1
 end
registerCommandHandler(gust_lock_toggle, 1, gust_lock_toggleHandler);

-- lights & animations
lights_landing_left = findCommand("cl300/lights/landing_left")
 function lights_landing_leftHandler(phase)
     if 0 == phase then
        if get(landlight1_h) > 0 then set(landlight1_h, 0) else set(landlight1_h, 1) end
     end
     return 1
 end
registerCommandHandler(lights_landing_left, 1, lights_landing_leftHandler);

lights_landing_right = findCommand("cl300/lights/landing_right")
 function lights_landing_rightHandler(phase)
     if 0 == phase then
        if get(landlight2_h) > 0 then set(landlight2_h, 0) else set(landlight2_h, 1) end
     end
     return 1
 end
registerCommandHandler(lights_landing_right, 1, lights_landing_rightHandler);

lights_taxi = findCommand("cl300/lights/taxi")
 function lights_taxiHandler(phase)
     if 0 == phase then
        		if get(xap_taxilight_h) < 2 then set(xap_taxilight_h, get(xap_taxilight_h) + 1) else set(xap_taxilight_h, 0) end
     end
     return 1
 end
registerCommandHandler(lights_taxi, 1, lights_taxiHandler);

lights_emergency = findCommand("cl300/lights/emergency_lights")
 function lights_emergencyHandler(phase)
     if 0 == phase then
        if get(emeright_h) > 1 then set(emeright_h, 0) else set(emeright_h, get(emeright_h) + 1) end
     end
     return 1
 end
registerCommandHandler(lights_emergency, 1, lights_emergencyHandler);

lights_wing_insp = findCommand("cl300/lights/wing_insp_lights")
 function lights_wing_inspHandler(phase)
     if 0 == phase then
        if get(wing_insp_h) > 0 then set(wing_insp_h, 0) else set(wing_insp_h, 1) end
     end
     return 1
 end
registerCommandHandler(lights_wing_insp, 1, lights_wing_inspHandler);


lights_nav_logo = findCommand("cl300/lights/nav_logo_lights")
 function lights_nav_logoHandler(phase)
     if 0 == phase then
        if get(nav_h) > 1 then set(nav_h, 0) else set(nav_h, get(nav_h) + 1) end
     end
     return 1
 end
registerCommandHandler(lights_nav_logo, 1, lights_nav_logoHandler);

lights_bcn_strb = findCommand("cl300/lights/bcn_strb_lights")
 function lights_bcn_strbHandler(phase)
     if 0 == phase then
        if get(strobe_h) > 1 then set(strobe_h, 0) else set(strobe_h, get(strobe_h) + 1) end
     end
     return 1
 end
registerCommandHandler(lights_bcn_strb, 1, lights_bcn_strbHandler);

lights_smoke_belt = findCommand("cl300/lights/smoke_belt_lights")
 function lights_smoke_beltHandler(phase)
     if 0 == phase then
        if get(smokebelts_h) > 1 then set(smokebelts_h, 0) else set(smokebelts_h, get(smokebelts_h) + 1) end
     end
     return 1
 end
registerCommandHandler(lights_smoke_belt, 1, lights_smoke_beltHandler);

lights_cabin_lights = findCommand("cl300/lights/cabin_lights")
 function lights_cabin_lightsHandler(phase)
     if 0 == phase then
        if get(cablight_h) > 0 then set(cablight_h, 0) else set(cablight_h, 1) end
     end
     return 1
 end
registerCommandHandler(lights_cabin_lights, 1, lights_cabin_lightsHandler);

anim_main_door = findCommand("cl300/anim/main_door")
 function anim_main_doorHandler(phase)
     if 0 == phase then
        if get(customslider2) > 0 then set(customslider2, 0) else set(customslider2, 1) end
     end
     return 1
 end
registerCommandHandler(anim_main_door, 1, anim_main_doorHandler);

anim_inside_bagage_door = findCommand("cl300/anim/inside_bagage_door")
 function anim_inside_bagage_doorHandler(phase)
     if 0 == phase then
        if get(baggage_door) > 0 then set(baggage_door, 0) else set(baggage_door, 1) end
     end
     return 1
 end
registerCommandHandler(anim_inside_bagage_door, 1, anim_inside_bagage_doorHandler);

anim_emer_door = findCommand("cl300/anim/emer_door")
 function anim_emer_doorHandler(phase)
     if 0 == phase then
        if get(customslider4) > 0 then set(customslider4, 0) else set(customslider4, 1) end
     end
     return 1
 end
registerCommandHandler(anim_emer_door, 1, anim_emer_doorHandler);

anim_bagage_door = findCommand("cl300/anim/bagage_door")
 function anim_bagage_doorHandler(phase)
     if 0 == phase then
        if get(customslider3) > 0 then set(customslider3, 0) else set(customslider3, 1) end
     end
     return 1
 end
registerCommandHandler(anim_bagage_door, 1, anim_bagage_doorHandler);

anim_toilet_door = findCommand("cl300/anim/toilet_door")
 function anim_toilet_doorHandler(phase)
     if 0 == phase then
        if get(lavat_door) > 0 then set(lavat_door, 0) else set(lavat_door, 1) end
     end
     return 1
 end
registerCommandHandler(anim_toilet_door, 1, anim_toilet_doorHandler);

anim_window_blinds = findCommand("cl300/anim/window_blinds")
 function anim_window_blindsHandler(phase)
     if 0 == phase then
            if get(jalousie1) == 1 and get(jalousie2) == 1 and get(jalousie3) == 1 and get(jalousie4) == 1 and get(jalousie5) == 1 and get(jalousie6) == 1 and get(jalousie7) == 1 and get(jalousie8) == 1 and get(jalousie9) == 1 and get(jalousie10) == 1 and get(jalousie11) == 1 and get(jalousie12) == 1 then
                set(jalousie1, 0)
                set(jalousie2, 0)
                set(jalousie3, 0)
                set(jalousie4, 0)
                set(jalousie5, 0)
                set(jalousie6, 0)
                set(jalousie7, 0)
                set(jalousie8, 0)
                set(jalousie9, 0)
                set(jalousie10, 0)
                set(jalousie11, 0)
                set(jalousie12, 0)
            else
                set(jalousie1, 1)
                set(jalousie2, 1)
                set(jalousie3, 1)
                set(jalousie4, 1)
                set(jalousie5, 1)
                set(jalousie6, 1)
                set(jalousie7, 1)
                set(jalousie8, 1)
                set(jalousie9, 1)
                set(jalousie10, 1)
                set(jalousie11, 1)
                set(jalousie12, 1)
            end
     end
     return 1
 end
registerCommandHandler(anim_window_blinds, 1, anim_window_blindsHandler);

anim_tables = findCommand("cl300/anim/tables")
 function anim_tablesHandler(phase)
     if 0 == phase then
            if get(table1) == 1 and get(table2) == 1 and get(table3) == 1 and get(table4) == 1 then
                set(table1, 0)
                set(table2, 0)
                set(table3, 0)
                set(table4, 0)
            else
                set(table1, 1)
                set(table2, 1)
                set(table3, 1)
                set(table4, 1)
            end
     end
     return 1
 end
registerCommandHandler(anim_tables, 1, anim_tablesHandler);

-- Mach hold toggle
createCommand("cl300/mach_hold", "MACH Hold toggle")
mach_hold_c = findCommand("cl300/mach_hold")
 function mach_hold_cHandler(phase)
     if 0 == phase then
        if get(engn_mach_h) > 0 then
        set(engn_mach_h, 0)
        else
        set(engn_mach_h, 1)
        end
     end
     return 1
 end
registerCommandHandler(mach_hold_c, 1, mach_hold_cHandler);




--set(dcp_refs, -1)
-- DCP refs
dcp_refs_button = findCommand("cl300/DCP/dcp_refs_button")
 function dcp_refs_buttonHandler(phase)
     if 0 == phase then
        set(dcp_refs_menu, 0)
        if get(dcp_refs) < 4 then
        set(dcp_refs, get(dcp_refs) + 1)
        else
        set(dcp_refs, 0)
        end
     end
     return 1
 end
registerCommandHandler(dcp_refs_button, 1, dcp_refs_buttonHandler);


--[=[
cwstog = findCommand("sim/autopilot/control_wheel_steer")
 function cwstogHandler(phase)
     if 0 == phase then
    -- set(strobe_h, 2)
        if autop_last[1] - get(autop_airspeed_dial) ~= 0 then
        set(autop_airspeed_dial, autop_last[1])
        end
        else

        if get(airspeed_is_mach)== 0 and get(autop_airspeed_dial) < 2 then
        autop_last = {get(autop_airspeed_dial)}
        end
    end
     return 1
 end

 registerCommandHandler(cwstog, 1, cwstogHandler);
--]=]

bknltstog = findCommand("sim/lights/beacon_lights_toggle")
 function bknltstogHandler(phase)
     if 0 == phase then
        if get(strobe_h) == 1 then
        set(strobe_h, 2)
        elseif get(strobe_h) == 2 then set(strobe_h, 0)
        elseif get(strobe_h) == 0 then set(strobe_h, 1)
        end

     end
     return 1
 end

 registerCommandHandler(bknltstog, 1, bknltstogHandler);


strbltstog = findCommand("sim/lights/strobe_lights_toggle")
 function strbltstogHandler(phase)
     if 0 == phase then
        if get(strobe_h) == 1 then
        set(strobe_h, 2)
        elseif get(strobe_h) == 2 then set(strobe_h, 0)
        elseif get(strobe_h) == 0 then set(strobe_h, 1)
        end

     end
     return 1
 end

 registerCommandHandler(strbltstog, 1, strbltstogHandler);


navltstog = findCommand("sim/lights/nav_lights_toggle")
 function navltstogHandler(phase)
     if 0 == phase then
        if get(nav_h) == 1 then
        set(nav_h, 2)
        elseif get(nav_h) == 2 then set(nav_h, 0)
        elseif get(nav_h) == 0 then set(nav_h, 1)
        end

     end
     return 1
 end

 registerCommandHandler(navltstog, 1, navltstogHandler);



taxiltstog = findCommand("sim/lights/taxi_lights_toggle")
 function taxiltstogHandler(phase)
     if 0 == phase then
        if get(xap_taxilight_h) > 0.5 then
        set(xap_taxilight_h, 0)
        else
        set(xap_taxilight_h, 2)
        end

     end
     return 1
 end

 registerCommandHandler(taxiltstog, 1, taxiltstogHandler);



landltstog = findCommand("sim/lights/landing_lights_toggle")
 function landltstogHandler(phase)
     if 0 == phase then
        if get(landlight1_h) > 0.5 or get(landlight2_h) > 0.5 then
        set(landlight1_h, 0)
        set(landlight2_h, 0)
        else
        set(landlight1_h, 1)
        set(landlight2_h, 1)
        end
--[=[
print("ISA_T : ",ISA_t_tmp[1])
print("baro_coeff : ",baro_coeff[1])
print("thrust 1 : ,",thrust[1])
print("POINT_thrust_0 : ",get(POINT_thrust_0))
print("acf_tmax",get(acf_tmax))
--]=]
     end
     return 1
 end

 registerCommandHandler(landltstog, 1, landltstogHandler);




brakestog = findCommand("sim/flight_controls/speed_brakes_toggle")
 function brakestogHandler(phase)
     if 0 == phase then
        if get(speedbrake_ratio) > 0.5 then
            set(speedbrake_ratio, 0.03)
            else
            set(speedbrake_ratio, 0.97)
        end

     end
     return 0
 end

registerCommandHandler(brakestog, 1, brakestogHandler);

brakesdnone = findCommand("sim/flight_controls/speed_brakes_down_one")
 function brakesdnoneHandler(phase)
      if 0 == phase then
            set(speedbrake_ratio, get(speedbrake_ratio)+0.1)
            end
 end

registerCommandHandler(brakesdnone, 1, brakesdnoneHandler);

brakesupone = findCommand("sim/flight_controls/speed_brakes_up_one")
 function brakesuponeHandler(phase)
  if 0 == phase then
            set(speedbrake_ratio, get(speedbrake_ratio)-0.1)
            end
 end

registerCommandHandler(brakesupone, 1, brakesuponeHandler);

brakesdnall = findCommand("sim/flight_controls/speed_brakes_down_all")
 function brakesdnallHandler(phase)
  if 0 == phase then
            set(speedbrake_ratio, 0.97)
            end
 end

registerCommandHandler(brakesdnall, 1, brakesdnallHandler);

brakesupall = findCommand("sim/flight_controls/speed_brakes_up_all")
 function brakesupallHandler(phase)
  if 0 == phase then
            set(speedbrake_ratio, 0.03)
            end
 end --]=]

registerCommandHandler(brakesupall, 1, brakesupallHandler);

warn_butt = findCommand("sim/annunciator/clear_master_warning")
 function warn_buttHandler(phase)
     if 0 == phase then
        r_cas = 0
     end
     return 1
 end

registerCommandHandler(warn_butt, 1, warn_buttHandler);


caut_butt = findCommand("sim/annunciator/clear_master_caution")
 function caut_buttHandler(phase)
     if 0 == phase then
        y_cas = 0
     end
     return 1
 end

registerCommandHandler(caut_butt, 1, caut_buttHandler);
--//

-- apu generator
createCommand("xap/apugen_button", "APU generator toggle")
apugen_c = findCommand("xap/apugen_button")
 function apugen_cHandler(phase)
     if 0 == phase then
     if get(APU_running) == 1 then
        if get(electr_apugen_h) > 0 then
        set(electr_apugen_h, 0)
        else
        set(electr_apugen_h, 1)
        end
     end
     end
     return 1
 end
registerCommandHandler(apugen_c, 1, apugen_cHandler);

--
-- left generator
createCommand("xap/elec_gen_l_button", "Left generator toggle")
elec_gen_l_c = findCommand("xap/elec_gen_l_button")
 function elec_gen_l_cHandler(phase)
     if 0 == phase then
	 -- JackZero: Gen can only be switched on if engine is running
     if get(ENGN_running_l) == 1 then
        if get(electr_gen_l_h) > 0 then
			set(electr_gen_l_h, 0)
        else
			set(electr_gen_l_h, 1)
        end
     end
	 -- JackZero: End
     end
     return 1
 end
registerCommandHandler(elec_gen_l_c, 1, elec_gen_l_cHandler);

--
-- right generator
createCommand("xap/elec_gen_r_button", "Right generator toggle")
elec_gen_r_c = findCommand("xap/elec_gen_r_button")
 function elec_gen_r_cHandler(phase)
     if 0 == phase then
	 -- JackZero: Gen can only be switched on if engine is running
     if get(ENGN_running_r) == 1 then
        if get(electr_gen_r_h) > 0 then
			set(electr_gen_r_h, 0)
        else
			set(electr_gen_r_h, 1)
        end
     end
	 -- JackZero: End
     end
     return 1
 end
registerCommandHandler(elec_gen_r_c, 1, elec_gen_r_cHandler);

-- JackZero: Complete overhaul of bus tie code:
-- bus tie
createCommand("xap/bus_tie_h_button", "Bus tie toggle")
bus_tie_c = findCommand("xap/bus_tie_h_button")
 function bus_tie_cHandler(phase)
     if 0 == phase then
        if busTieSituation() then
            -- toggle tie if in tie 'situation'
            set(bus_tie_h, 1 - get(bus_tie_h))
        end
     end
     return 1
 end
registerCommandHandler(bus_tie_c, 1, bus_tie_cHandler);
--

function busTieSituation()
	-- don't use amps since it might be 0 if there is <1 amp power draw
    local gens = (get(APU_generator_on))
               + (get(generator_on_l))
               + (get(generator_on_r))
    if (gens > 1) then
        -- more then one gen supplying power, buses split
        return false;
    elseif (gens == 1) or (get(battery_amps_0) > 0 or get(battery_amps_1) > 0) then
        -- at least one gen or one bat supplying power, buses tied
        return true;
    else
        -- no power at all, buses split
        return false;
    end
end
-- JackZero: End

-- JackZero: Flightplan access and langing alt
local last_entry  = 0
local show_entry  = 0
local max_entries = 0
local dest_arpt = ""
local ldg_alt_i = 0

function flgtplan_show_next_handler(phase)
	if phase==0 then
		if show_entry < max_entries + 1 then
			show_entry = show_entry + 1
			set(flgtplan_show_entry, show_entry)
		end
	end
	return 1
end
registerCommandHandler(findCommand("cl300/flgtplan_show_next"),1,flgtplan_show_next_handler)

function flgtplan_show_prev_handler(phase)
	if phase==0 then
		if show_entry > 0 then
			show_entry = show_entry - 1
			set(flgtplan_show_entry, show_entry)
		end
	end
	return 1
end
registerCommandHandler(findCommand("cl300/flgtplan_show_prev"),1,flgtplan_show_prev_handler)
-- JackZero: End

local glass_temp = {get(temperature_ambient_c),get(temperature_ambient_c)}
local frame_factor

local fuel_reference = get(m_fuel_total)
local fuel_last      = fuel_reference

ias_mach_last = 0
switch_1_last = get(switch_1)







function update()

frame_factor = ((get(frame_rate_period)-0.02)*25 + 0.5)*get(paused)


-- New FMC

if switch_1_last ~= get(switch_1) then
	commandOnce(FMSCDU_popup)
	switch_1_last = get(switch_1)
end



--autoswitch to mach dial

if get(machno) < 0.4 then set(airspeed_is_mach, 0) end

if get(altitude_ft_pilot) > 35561 then
	if get(airspeed_is_mach) < 0.9 and ias_mach_last ~= 1 then
	commandOnce(knots_mach_toggle)
	ias_mach_last = 1
	end
end

if get(altitude_ft_pilot) < 30335 then
	if get(airspeed_is_mach) > 0 and ias_mach_last == 1 then
	commandOnce(knots_mach_toggle)
	ias_mach_last = 0
	end
end


--Salon lights

set(salon_lt_lvl, get(salon_lit))-- - (1- get( night_lights))*0.5)
set(stair_lt_lvl, get(entry_lit))-- - (1- get( night_lights))*0.8)
set(salon_lt_lvl_full, get(salon_lit))



--reflection animations

set(lg_gear_0, get(gear_deploy_ratio_0))
set(lg_gear_1, get(acf_gear_deploy_1))
set(lg_gear_2, get(acf_gear_deploy_2))

set(flapref_l, get(flaprat0))
set(flapref_r, get(flaprat1))

set(ailer_l,get(aileron1_deg_0))
set(ailer_r,get(aileron1_deg_1))

set(l_engn_rev_rat, get(thrust_reverser_deploy_ratio_0)*60)
set(r_engn_rev_rat, get(thrust_reverser_deploy_ratio_1)*60)
--

if get(elbus1) >= 10 or get(elbus2) >= 10 then elec_on = 1 else elec_on = 0 end

-- fire

if get(fire_engn_l_h) > 0 then set(fire_extinguisher_on_l, 1) else set(fire_extinguisher_on_l, 0) end
if get(fire_engn_r_h) > 0 then set(fire_extinguisher_on_r, 1) else set(fire_extinguisher_on_r, 0) end


-- JackZero: Update conditional Vspeeds
local vno = ((get(altitude_ft_pilot) < 8000) and 300 or math.min(320, get(ind_speed) * 0.83 / get(machno)))

if get(gear_deploy_ratio_0) + get(gear_deploy_ratio_1) + get(gear_deploy_ratio_2) > 0 then
    vno = math.min(vno, 250)
end
if get(flaprat) > 0 then
    vno = math.min(vno, get(flaprat) > 0.67 and 175 or 210)
end

if vspeed_last[1] ~= vno then
    set(acf_Vno, vno)
    vspeed_last[1] = vno
end
-- JackZero: End

-- RA MIN

if get(radio_altimeter_bug_ft_pilot) > 1000 then set(radio_altimeter_bug_ft_pilot, 1000) end
if get(radio_altimeter_bug_ft_pilot) < 0 then set(radio_altimeter_bug_ft_pilot, 0) end

-- JackZero: Linking rudder trim to trim switch:
if trim_rud_tmp ~= ruddertrim_last[2] then
    set(rudder_trim, trim_rud_tmp)
elseif get(rudder_trim) ~= ruddertrim_last[1] then
    trim_rud_tmp = get(rudder_trim)
end
-- JackZero: End



-- trim stab disabling

if get(trim_stab) == 1 then
set(acf_hstb_trim_dn, 0)
set(acf_hstb_trim_up, 0)
else
set(acf_hstb_trim_dn, 7)
set(acf_hstb_trim_up, 8)
end


if get(flight_spoil_h) == 1 or get(trim_stab) == 1 then
if get(autopilot_mode) > 1 then set(autopilot_mode, get(autopilot_mode) - 1) end
end


if coms_last[11] - get(mfd_sel_state_h) ~= 0 then set(mfd_sel_state, 1) end



-- brgsrc refs and navsrc buttons
if coms_last[7] - get(autop_brgsrc) == 0 and get(autop_brgsrc) > -1 then
brgsrc_hide[1] = brgsrc_hide[1] + 0.0008*frame_factor
else
brgsrc_hide[1] = 0
end

if brgsrc_hide[1] > 1 then
set(autop_brgsrc, -get(autop_brgsrc)-1)
brgsrc_hide[1] = 0
end


if coms_last[8] - get(dcp_navsrc) == 0 and get(dcp_navsrc) > -1 then
brgsrc_hide[2] = brgsrc_hide[2] + 0.0008*frame_factor
else
brgsrc_hide[2] = 0
end

if brgsrc_hide[2] > 1 then
set(dcp_navsrc, -1)
brgsrc_hide[2] = 0
end



if coms_last[9] - get(dcp_refs) == 0 and refspds_last[1] - get(refspds_v1) == 0 and refspds_last[2] - get(refspds_vr) == 0 and refspds_last[3] - get(refspds_v2) == 0 and refspds_last[4] - get(refspds_vt) == 0 and refspds_last[5] - get(refspds_vga) == 0 and refspds_last[6] - get(refspds_vref) == 0 and get(dcp_refs) ~= -1 then
brgsrc_hide[3] = brgsrc_hide[3] + 0.0008*frame_factor
else
brgsrc_hide[3] = 0
end

if brgsrc_hide[3] > 1 then
set(dcp_refs, -1)
brgsrc_hide[3] = 0
end



if electric_last[5] - get(dcp_menu1_rot) ~= 0 and get(mfd_sel_state) > 0 then brgsrc_hide[4] = 0 end

if coms_last[10] - get(mfd_sel_state) == 0 and get(mfd_sel_state) > 0 then
brgsrc_hide[4] = brgsrc_hide[4] + 0.0008*frame_factor
else
brgsrc_hide[4] = 0
end

if brgsrc_hide[4] > 1 then
set(mfd_sel_state, 0)
brgsrc_hide[4] = 0
end




if get(dcp_navsrc) == 0 then
set(HSI_source_select_pilot, 2)
elseif get(dcp_navsrc) == 1 then set(HSI_source_select_pilot, 0)
elseif get(dcp_navsrc) == 2 then set(HSI_source_select_pilot, 1)
end

-- map range

-- JackZero: added optional support for XHSI:
-- xhsi_nd_pilot_map_zoomin ,
if last_states[26] - get(dcp_range) > 0 then
	if get(use_xhsi) > 0 then
		local r = get(map_range)
		local z = get(xhsi_nd_pilot_map_zoomin)
		if r > 0 then
			set(map_range, r - 1)
		elseif r == 0 and z == 0 then
			set(map_range, 6)
			set(xhsi_nd_pilot_map_zoomin, 1)
		end
	else
		set(map_range, math.max(1, get(map_range) - 1))
	end
end
if last_states[26] - get(dcp_range) < 0 then
	if get(use_xhsi) > 0 then
		local r = get(map_range)
		local z = get(xhsi_nd_pilot_map_zoomin)
		if r < 6 then
			set(map_range, r + 1)
		elseif r == 6 and z == 1 then
			set(map_range, 0)
			set(xhsi_nd_pilot_map_zoomin, 0)
		end
	else
		set(map_range, math.min(6, get(map_range) + 1))
	end
end


-- audio panel
if coms_last[1] - get(aud_com1_r_h) ~= 0 and get(aud_com1_r_h) == 1 then
set(aud_com2_r_h, 0)
set(aud_com3_r_h, 0)
else
    if coms_last[2] - get(aud_com2_r_h) ~= 0 and get(aud_com2_r_h) == 1 then
    set(aud_com1_r_h, 0)
    set(aud_com3_r_h, 0)
    else
        if coms_last[3] - get(aud_com3_r_h) ~= 0 and get(aud_com3_r_h) == 1 then
        set(aud_com1_r_h, 0)
        set(aud_com2_r_h, 0)
        end
    end
end

if coms_last[4] - get(aud_com1_l_h) ~= 0 and get(aud_com1_l_h) == 1 then
set(aud_com2_l_h, 0)
set(aud_com3_l_h, 0)

else
    if coms_last[5] - get(aud_com2_l_h) ~= 0 and get(aud_com2_l_h) == 1 then
    set(aud_com1_l_h, 0)
    set(aud_com3_l_h, 0)

    else
        if coms_last[6] - get(aud_com3_l_h) ~= 0 and get(aud_com3_l_h) == 1 then
        set(aud_com1_l_h, 0)
        set(aud_com2_l_h, 0)
        set(audio_com_selection, 0)
        end
    end
end

if get(aud_com1_l_h) == 0 and get(aud_com2_l_h) == 0 then set(audio_com_selection, 0) end


if get(aud_com1_l_h) == 1 and get(aud_voice_l_sw) ~= 1 then set(audio_com_selection, 6) end
if get(aud_com2_l_h) == 1 and get(aud_voice_l_sw) ~= 1 then set(audio_com_selection, 7) end



if get(aud_voice_l_sw) == 1 then
set(audio_com_selection, 0)
set(audio_selection_nav1, 1)
set(audio_selection_nav2, 1)
set(audio_selection_adf1, 1)
set(audio_selection_adf2, 1)
set(audio_dme_enabled, 1)
set(audio_marker_enabled, 1)
else
    if get(aud_voice_l_sw) == 0 then
    set(audio_selection_nav1, 1)
    set(audio_selection_nav2, 1)
    set(audio_selection_adf1, 1)
    set(audio_selection_adf2, 1)
    set(audio_dme_enabled, 1)
    set(audio_marker_enabled, 1)
    else
    set(audio_selection_nav1, 0)
    set(audio_selection_nav2, 0)
    set(audio_selection_adf1, 0)
    set(audio_selection_adf2, 0)
    set(audio_dme_enabled, 0)
    set(audio_marker_enabled, 0)
    end
end

if get(aud_vol_12_l) == 0 or get(aud_vol_12_r) == 0 then
set(audio_marker_enabled, 0)
end

if get(aud_vol_15_l) == 0 or get(aud_vol_15_r) == 0 then
    set(audio_selection_nav1, 0)
end

if get(aud_vol_16_l) == 0 or get(aud_vol_16_r) == 0 then
    set(audio_selection_nav2, 0)
end

-- master warn/caut





if ping_temp[2] == 0 then ping_temp[1] = ping_temp[1] + 0.08*frame_factor end
if ping_temp[2] == 1 then ping_temp[1] = ping_temp[1] - 0.08*frame_factor end
if ping_temp[1] >= 1 then ping_temp[2] = 1 elseif ping_temp[1] <= 0 then ping_temp[2] = 0 end

if ping_temp[4] == 0 then ping_temp[3] = ping_temp[3] + 0.08*frame_factor end
if ping_temp[4] == 1 then ping_temp[3] = ping_temp[3] - 0.08*frame_factor end
if ping_temp[3] >= 1 then ping_temp[4] = 1 elseif ping_temp[3] <= 0 then ping_temp[4] = 0 end



if caution_last[1] - get(red_cas) ~= 0 and get(red_cas) > 0 then
r_cas = 1
ping_temp[1] = 0
else
    if caution_last[1] - get(red_cas) ~= 0 and get(red_cas) == 0 then
    r_cas = 0
    end
end

if caution_last[2] - get(yellow_cas) ~= 0 and get(yellow_cas) > 0 then
y_cas = 1
ping_temp[3] = 0
else
    if caution_last[2] - get(yellow_cas) ~= 0 and get(yellow_cas) == 0 then
    y_cas = 0
    end
end



if r_cas == 1 then
    if elec_on == 1 then
    set(mast_warn, ping_temp[1])
    else
    set(mast_warn, 0)
    end
else
    set(mast_warn, 0)
end--

if y_cas == 1 then
    if elec_on == 1 then
    set(mast_caut, ping_temp[3])
    else
    set(mast_caut, 0)
    end
else
    set(mast_caut, 0)
end



-- rev panel tune knob

if get(rev_tune) == 0 then
set(com1_freq_hz, 12150)
end



set(transparent_panel, 0)

-- autop panel knobs


if get(autop_hdg_dial) < 0 then
commandOnce(aut_heading_down)
end
if get(autop_hdg_dial) > 0 then
commandOnce(aut_heading_up)
end

if get(autop_spd_dial) < 0 then
commandOnce(aut_airspeed_down)
end
if get(autop_spd_dial) > 0 then
commandOnce(aut_airspeed_up)
end

if get(autop_alt_dial) < 0 then
commandOnce(aut_altitude_down)
end
if get(autop_alt_dial) > 0 then
commandOnce(aut_altitude_up)
end

if get(autop_vvi_dial) < 0 then
commandOnce(aut_vertical_speed_down)
end
if get(autop_vvi_dial) > 0 then
commandOnce(aut_vertical_speed_up)
end





-- clock reset button

if last_states[20] - get(clock_timer_h) ~= 0 and get(timer_running) == 0 and get(clock_timer_h) == 1 then
commandOnce(clock_timer_start_stop)
end

if last_states[20] - get(clock_timer_h) ~= 0 and get(timer_running) == 1 and get(clock_timer_h) == 2 then
commandOnce(clock_timer_start_stop)
end

if last_states[20] - get(clock_timer_h) ~= 0 and get(clock_timer_h) == 0 then
commandOnce(clock_timer_reset)
end




-- autopilot buttons
-- JackZero: Changed to remove interference with SmartCopilot, obviously linking the commands
-- directly to the cockpit object instead might be a better solution but I didn't want to include the .obj in the patch
if get(autop_fdir_h) == 1 then   set(autop_fdir_h, 0);  commandOnce(aut_fdir_toggle) end
if get(autop_ap_h  ) == 1 then   set(autop_ap_h,   0);  commandOnce(aut_servos_toggle) end
if get(autop_yd_h)   == 1 then   set(autop_yd_h,   0);  commandOnce(aut_yawd) end
if get(autop_nav_h)  == 1 then   set(autop_nav_h,  0);  commandOnce(aut_NAV) end
if get(autop_hdg_h)  == 1 then   set(autop_hdg_h,  0);  commandOnce(aut_heading) end
if get(autop_appr_h) == 1 then   set(autop_appr_h, 0);  commandOnce(aut_approach) end
if get(autop_bc_h)   == 1 then   set(autop_bc_h,   0);  commandOnce(aut_back_course) end

if get(autop_flc_h)  == 1 then
	set(autop_flc_h,  0)
	commandOnce(aut_level_change)
	if get(groundspeed) < 50 then
		if get(airspeed_is_mach) < 0.5 then
		set(autop_airspeed_dial, 150)
		else
		set(autop_airspeed_dial, 0.23)
		end
	end
end

if get(autop_vs_h)   == 1 then   set(autop_vs_h,   0);  commandOnce(aut_vertical_speed) end
if get(autop_vnav_h) == 1 then   set(autop_vnav_h, 0);  commandOnce(aut_vnav) end
if get(autop_alt_h)  == 1 then   set(autop_alt_h,  0);  commandOnce(aut_altitude_hold) end
if get(autop_fdir_h) == 1 then   set(autop_fdir_h, 0);  commandOnce(aut_fdir_toggle) end
if get(autop_fdir_h) == 1 then   set(autop_fdir_h, 0);  commandOnce(aut_fdir_toggle) end
if get(autop_fdir_h) == 1 then   set(autop_fdir_h, 0);  commandOnce(aut_fdir_toggle) end

-- JackZero: Moved here from pfd.lua to keep logic in single file and remove incompatibility with
-- SmartCopilot
if (get(altitude_ft_pilot) > 31600) ~= bank_alt_last then
	if bank_alt_last then -- changed from above TA to below TA, deactivate
		set(half_bank_h,0)
	else -- changed from below TA to above TA, activate
		set(half_bank_h,1)
	end
end

if get(half_bank_h) == 1 and get(autopilot_on) > 0 and get(roll_status) == 0 and get(TOGA_lateral_status) == 0 then
	set(bank_angle_mode,3)
else
	set(bank_angle_mode,0)
end
-- JackZero: End


set(cockpit_light_level_r, 1)
set(cockpit_light_level_g, 1)
set(cockpit_light_level_b, 1)
set(cockpit_lights_on, 1)



if get(fgp_s_hdg) == 1 then
set(heading_dial_deg_mag_pilot, get(heading_AHARS_deg_mag_pilot))
end





-- pedals adjastment
if get(elbus1) >= 10 and get(rudd_ped_l_sw) == 1 and get(pedals_fwd_bwd_l) < 1 then
set(pedals_fwd_bwd_l, get(pedals_fwd_bwd_l) + 0.01*frame_factor)
elseif get(elbus1) >= 10 and get(rudd_ped_l_sw) == -1 and get(pedals_fwd_bwd_l) > -1 then set(pedals_fwd_bwd_l, get(pedals_fwd_bwd_l) - 0.01*frame_factor)
end

if get(elbus1) >= 10 and get(rudd_ped_r_sw) == 1 and get(pedals_fwd_bwd_r) < 1 then
set(pedals_fwd_bwd_r, get(pedals_fwd_bwd_r) + 0.01*frame_factor)
elseif get(elbus1) >= 10 and get(rudd_ped_r_sw) == -1 and get(pedals_fwd_bwd_r) > -1 then set(pedals_fwd_bwd_r, get(pedals_fwd_bwd_r) - 0.01*frame_factor)
end


-- Turbulence limiter

--[=[if get(wave_limit_n) > 0 then
    if get(turbulence0) > 0.125 then
        set(turbulence0, 0.125)
    end
        if get(turbulence1) > 0.125 then
        set(turbulence1, 0.125)
    end
        if get(turbulence2) > 0.125 then    starter_time[1] = get(time_now)
        set(turbulence2, 0.125)
    end
end--]=]


-- fuel

-- JackZero: Moved down a few lines to keep fuel logic at one place, changed from running engines to shutoff valves
if get(en_but_run_l) == 1 and get(en_but_run_r) == 1 then
    set(fuel_tank_selector, 4)
elseif get(en_but_run_r) == 1 then
    set(fuel_tank_selector, 3)
elseif get(en_but_run_l) == 1 then
    set(fuel_tank_selector, 1)
else
    set(fuel_tank_selector, 0)
end

-- JackZero: Rework of the fuel logic:

-- Fuel pumps ------------
-- Moved code from mfd.lua
if get(fuel_xpump_l) == 1 then
	if get(en_but_run_l) == 1 and get(ENGN_N1_L) < 30 then
        set(fuel_pump_on_0, 1)
        set(fuel_pump_l_astate,1)
    else
        set(fuel_pump_on_0, 0)
        set(fuel_pump_l_astate,0)
	end
elseif get(fuel_xpump_l) == 2 then
    set(fuel_pump_on_0, 1)
    set(fuel_pump_l_astate,0)
else
    set(fuel_pump_on_0, 0)
    set(fuel_pump_l_astate,0)
end

if get(fuel_xpump_r) == 1 then
	if get(APU_switch) > 0 and get(ENGN_N1_R) < 30 then
        set(fuel_pump_on_1, 1)
        set(fuel_pump_r_astate,1)
	elseif get(en_but_run_r) == 1 and get(ENGN_N1_R) < 30 then
        set(fuel_pump_on_1, 1)
        set(fuel_pump_r_astate,1)
    else
        set(fuel_pump_on_1, 0)
        set(fuel_pump_r_astate,0)
	end
elseif get(fuel_xpump_r) == 2 then
    set(fuel_pump_on_1, 1)
    set(fuel_pump_r_astate,0)
else
    set(fuel_pump_on_1, 0)
    set(fuel_pump_r_astate,0)
end

-- Fuel mixture ------------
set(ENGN_mixt_0, (get(en_but_run_l) == 1 and (get(ENGN_N1_L) >= 30 or get(fuel_pump_on_0) == 1)) and 1 or 0)
set(ENGN_mixt_1, (get(en_but_run_r) == 1 and (get(ENGN_N1_R) >= 30 or get(fuel_pump_on_1) == 1)) and 1 or 0)

-- Fuel transfer ------------
local xfer_from = 0 -- 3: right / 1: left
local xfer_to   = 0
if get(fuel_xflow_dn_h) == 1 then
    -- electrical xflow
    if (get(fuel_pump_on_0) == 1) and (get(fuel_pump_on_1) == 0) then
        set(fuel_tank_transfer_from, 1)
    elseif (get(fuel_pump_on_1) == 1) and (get(fuel_pump_on_0) == 0) then
        set(fuel_tank_transfer_from, 3)
    else
        set(fuel_tank_transfer_from, 0)
    end
else
    set(fuel_tank_transfer_from, 0)
end

if get(fuel_xflow_up_h) == 1 and elec_on == 1 then
    -- gravity xflow:
    -- just a approximation, we assume that the weight in the right tank doubles per + 15 deg slip while the weight in the
    -- left tank is halved (and vice versa).
    local factor = math.pow(2,  get(slip_deg) / 15)
    local fuel_diff = get(m_fuel1) / factor - get(m_fuel2) * factor
    if fuel_diff > 4.5 then
        set(fuel_tank_transfer_to, 3)
    elseif fuel_diff < -4.5 then
        set(fuel_tank_transfer_to, 1)
    else
        set(fuel_tank_transfer_to, 0)
    end
else
    set(fuel_tank_transfer_to, 0)
end

-- JackZero: Since the previous logic wasn't very fault tollerant we'll now reset the fuel used counter once the first engine is started while on the ground.
if get(m_fuel_total) > fuel_last then
	-- on increase of fuel (e.g. in air refueling), update our reference value so fuel used doesn't decrease
	fuel_reference = fuel_reference + get(m_fuel_total) - fuel_last
end

if get(onground_any) == 1 then
	if (running_last[1] + running_last[2]) == 0 and (get(ENGN_running_l) + get(ENGN_running_r)) > 0 then
		-- on ground with the first engine starting, reset the fuel used
		fuel_reference = get(m_fuel_total)
	elseif (get(ENGN_running_l) + get(ENGN_running_r)) == 0 and (fuel_last - get(m_fuel_total) > 4.535 * get(paused) * get(frame_rate_period)) then
		-- on ground with no engines not running, update our reference if the fuel decreases by more then 10 lbs/s
		fuel_reference = fuel_reference + get(m_fuel_total) - fuel_last
	end
end

set(fuel_used, fuel_reference - get(m_fuel_total))
fuel_last = get(m_fuel_total)

-- Fuel CAS messages ------------
local fuel_delta = math.abs(get(m_fuel1) - get(m_fuel2))

-- JackZero: Changed calculation to kg... no need for permanent conversions!
if fuel_delta < 45 then
    set(fuel_balanced, (get(fuel_xflow_dn_h) > 0) and 1 or 0)
    set(fuel_imbalanced, 0)
elseif get(m_fuel_total) >= 6214 then
    set(fuel_balanced, (fuel_delta > 113) and 1 or 0)
elseif get(m_fuel_total) >= 4536 then
    set(fuel_balanced, (fuel_delta > 143) and 1 or 0)
elseif get(m_fuel_total) >= 2132 then
    set(fuel_balanced, (fuel_delta > 205) and 1 or 0)
else
    set(fuel_balanced, (fuel_delta > 272) and 1 or 0)
end

set(m_fuel2, get(m_fuel2) - get(frame_rate_period) * get(paused) * get(APU_running)*0.01512) -- JackZero: 120pph, don't use fuel while the sim is paused!

--ign

-- JackZero: Moved down to keep ignition logic in one place
if get(engn_ign_h) > 0 then
    set(igniter_on_0, 1)
    set(igniter_on_1, 1) -- JackZero: Fixed typo, set both sides
else
    -- JackZero: Disable ignition if not in startup sequence
    if s_l == 0 then set(igniter_on_0, 0) end
    if s_r == 0 then set(igniter_on_1, 0) end
end

if get(en_but_run_l) == 0 then set(igniter_on_0, 0) end
if get(en_but_run_r) == 0 then set(igniter_on_1, 0) end


-- JackZero: Removed double reading of en_starter_l/r
if last_states[18] < 2 and get(en_starter_l) == 2 then s_l = 1 end

if last_states[19] < 2 and get(en_starter_r) == 2 then s_r = 1 end

-- JackZero (moved up):
-- Close bleed valves if the respective engine isn't running.
if get(flight_time) > 3 then
if get(ENGN_running_l) == 0 then set(bleed_en_l_h, 0) end
if get(ENGN_running_r) == 0 then set(bleed_en_r_h, 0) end
if get(APU_running)    == 0 then set(bleed_apu_h,  0) end
end

-- XBLEED can only be open if at least one bleed source is available
if get(bleed_en_l_h) + get(bleed_en_r_h) + get(bleed_apu_h) == 0 then
	set(bleed_xbleed_h, 0)
end
-- JackZero: End

if s_l == 1 then
	set(bleed_xbleed_h, 1)
	set(bleed_en_l_h, 0)
	if get(ENGN_N1_L) < 11 and get(ENGN_mixt_0) > 0 then
        commandOnce(starter_l)
        if get(ENGN_N1_L) > 7 then
            set(igniter_on_0, 1)
        else
            set(igniter_on_0, 0)
        end
    else
        s_l = 0
    end
end

if s_r == 1 then
	set(bleed_xbleed_h, 1)
	set(bleed_en_r_h, 0)
    if get(ENGN_N1_R) < 11 and get(ENGN_mixt_1) > 0 then
        commandOnce(starter_r)
        if get(ENGN_N1_R) > 7 then
            set(igniter_on_1, 1)
        else
            set(igniter_on_1, 0)
        end
    else
        s_r = 0
    end
end

-- JackZero: Changed the order: Disable ignition first!
if get(en_starter_l) == 0 and get(ENGN_FF_l) < 0.03 then
    set(igniter_on_0, 0)
    commandOnce(starter_l)
end

if get(en_starter_r) == 0 and get(ENGN_FF_r) < 0.03 then
    set(igniter_on_1, 0)
    commandOnce(starter_r)
end

set(dtr_s_r, s_r)
set(dtr_s_l, s_l)

-- JackZero: Increased readability, changed to use datarefs, moved here from mfd.lua:
if get(aircond_airsource) == 0 or get(pressure_ditch_h) == 1 then -- OFF or DITCH
	set(aircond_has_pack, 0)
	set(aircond_has_trim, 0)
elseif get(aircond_airsource) == 1 then -- NORM
	set(aircond_has_pack, (get(bleed_apu_h) == 1 or get(bleed_en_r_h) == 1) and 1 or 0)
	set(aircond_has_trim, (get(bleed_apu_h) == 1 or get(bleed_en_r_h) == 1 or get(bleed_en_l_h) == 1) and 1 or 0)
elseif get(aircond_airsource) == 2 then -- PACKS
	set(aircond_has_pack, (get(bleed_en_l_h) == 1 or ((get(bleed_apu_h) == 1 or get(bleed_en_r_h) == 1) and get(bleed_xbleed_h) == 1)) and 1 or 0)
	set(aircond_has_trim, 0)
else -- TRIM
	set(aircond_has_pack, 0)
	set(aircond_has_trim, (get(bleed_apu_h) == 1 or get(bleed_en_r_h) == 1 or (get(bleed_en_l_h) == 1 and get(bleed_xbleed_h) == 1)) and 1 or 0)
end

if get(bleed_en_l_h) == 1 then
	if get(aircond_airsource) == 1 then
		if     get(bleed_en_r_h) == 1 and get(bleed_apu_h) == 1 then set(bleed_psi_l, 52)
		elseif get(bleed_en_r_h) == 1 or  get(bleed_apu_h) == 1 then set(bleed_psi_l, 51)
		else   						  set(bleed_psi_l, 48) end
	elseif (get(aircond_airsource) == 2 or get(aircond_airsource) == 3) and get(bleed_xbleed_h) == 1 then
		if     get(bleed_en_r_h) == 1 and get(bleed_apu_h) == 1 then set(bleed_psi_l, 52)
		elseif get(bleed_en_r_h) == 1 or  get(bleed_apu_h) == 1 then set(bleed_psi_l, 51)
		else						  set(bleed_psi_l, 50) end
	elseif get(aircond_airsource) == 2 then -- implies: not get(bleed_xbleed_h) == 1
		set(bleed_psi_l, 50)
	else
		set(bleed_psi_l, 53)
	end
else
	set(bleed_psi_l, 0)
end

if get(bleed_en_r_h) == 1 then
	if get(aircond_airsource) == 1 then
		if     get(bleed_en_l_h) == 1 and get(bleed_apu_h) == 1 then set(bleed_psi_r, 52)
		elseif get(bleed_en_l_h) == 1 or  get(bleed_apu_h) == 1 then set(bleed_psi_r, 51)
		else   						  set(bleed_psi_r, 48) end
	elseif (get(aircond_airsource) == 2 or get(aircond_airsource) == 3) and get(bleed_xbleed_h) == 1 then
		if     get(bleed_en_l_h) == 1 and get(bleed_apu_h) == 1 then set(bleed_psi_r, 52)
		elseif get(bleed_en_l_h) == 1 or  get(bleed_apu_h) == 1 then set(bleed_psi_r, 51)
		else   						  set(bleed_psi_r, 50) end
	elseif get(aircond_airsource) == 3 then -- implies: not get(bleed_xbleed_h) == 1
		set(bleed_psi_r, 50)
	else
		set(bleed_psi_r, 53)
	end
else
	set(bleed_psi_r, 0)
end


-- interior temperatures
-- JackZero: Increased readability

if get(aircond_has_pack) == 1 or get(aircond_has_trim) == 1 or get(aircond_ramair_h) == 1 and get(pressure_ditch_h) == 0 then

    local rate = get(customslider4_r)*0.001 + get(customslider2_r)*0.002

    if     get(aircond_tempr_cab_c) < get(aircond_tempr_cab_ddial) then set(aircond_tempr_cab_c, math.min(get(aircond_tempr_cab_ddial), get(aircond_tempr_cab_c) + (0.005*frame_factor + rate)))
    elseif get(aircond_tempr_cab_c) > get(aircond_tempr_cab_ddial) then set(aircond_tempr_cab_c, math.max(get(aircond_tempr_cab_ddial), get(aircond_tempr_cab_c) - (0.005*frame_factor + rate))) end

    if     get(aircond_tempr_cock_c) < get(aircond_tempr_cock_ddial) then set(aircond_tempr_cock_c, math.min(get(aircond_tempr_cock_ddial), get(aircond_tempr_cock_c) + (0.005*frame_factor + rate)))
    elseif get(aircond_tempr_cock_c) > get(aircond_tempr_cock_ddial) then set(aircond_tempr_cock_c, math.max(get(aircond_tempr_cock_ddial), get(aircond_tempr_cock_c) - (0.005*frame_factor + rate))) end

    if     get(aircond_tempr_cab_duct) < get(aircond_tempr_cab_ddial) then set(aircond_tempr_cab_duct, math.min(get(aircond_tempr_cab_ddial), get(aircond_tempr_cab_duct) + (0.025*frame_factor + rate)))
    elseif get(aircond_tempr_cab_duct) > get(aircond_tempr_cab_ddial) then set(aircond_tempr_cab_duct, math.max(get(aircond_tempr_cab_ddial), get(aircond_tempr_cab_duct) - (0.025*frame_factor + rate))) end

    if     get(aircond_tempr_cock_duct) < get(aircond_tempr_cock_ddial) then set(aircond_tempr_cock_duct, math.min(get(aircond_tempr_cock_ddial), get(aircond_tempr_cock_duct) + (0.025*frame_factor + rate)))
    elseif get(aircond_tempr_cock_duct) > get(aircond_tempr_cock_ddial) then set(aircond_tempr_cock_duct, math.max(get(aircond_tempr_cock_ddial), get(aircond_tempr_cock_duct) - (0.025*frame_factor + rate))) end

else

    local rate = get(customslider4_r)*0.003 + get(customslider2_r)*0.007

    if     get(aircond_tempr_cab_c) < get(temperature_ambient_c) then set(aircond_tempr_cab_c, math.min(get(temperature_ambient_c), get(aircond_tempr_cab_c) + (0.001*frame_factor + rate)))
    elseif get(aircond_tempr_cab_c) > get(temperature_ambient_c) then set(aircond_tempr_cab_c, math.max(get(temperature_ambient_c), get(aircond_tempr_cab_c) - (0.001*frame_factor + rate))) end

    if     get(aircond_tempr_cock_c) < get(temperature_ambient_c) then set(aircond_tempr_cock_c, math.min(get(temperature_ambient_c), get(aircond_tempr_cock_c) + (0.001*frame_factor + rate)))
    elseif get(aircond_tempr_cock_c) > get(temperature_ambient_c) then set(aircond_tempr_cock_c, math.max(get(temperature_ambient_c), get(aircond_tempr_cock_c) - (0.001*frame_factor + rate))) end

    if     get(aircond_tempr_cab_duct) < get(aircond_tempr_cab_c) then set(aircond_tempr_cab_duct, math.min(get(aircond_tempr_cab_c), get(aircond_tempr_cab_duct) + (0.01*frame_factor + rate)))
    elseif get(aircond_tempr_cab_duct) > get(aircond_tempr_cab_c) then set(aircond_tempr_cab_duct, math.max(get(aircond_tempr_cab_c), get(aircond_tempr_cab_duct) - (0.01*frame_factor + rate))) end

    if     get(aircond_tempr_cock_duct) < get(aircond_tempr_cock_c) then set(aircond_tempr_cock_duct, math.min(get(aircond_tempr_cock_c), get(aircond_tempr_cock_duct) + (0.01*frame_factor + rate)))
    elseif get(aircond_tempr_cock_duct) > get(aircond_tempr_cock_c) then set(aircond_tempr_cock_duct, math.max(get(aircond_tempr_cock_c), get(aircond_tempr_cock_duct) - (0.01*frame_factor + rate))) end

end


-- JackZero: Changed the target temp for the window heater from 1°C to 50°C, increased the heatup speed
if get(antice_wshld_l_h) > 0 and get(ice_window_heat_on) > 0 then -- the later verifies that the heater(s) are powered
    if glass_temp[1] < 50                     then glass_temp[1] = math.min(50,                     glass_temp[1] + 0.05*frame_factor) end
    if glass_temp[1] > 50                     then glass_temp[1] = math.max(50,                     glass_temp[1] - 0.01*frame_factor) end
else
    if glass_temp[1] < get(aircond_tempr_cock_c) then glass_temp[1] = math.min(get(aircond_tempr_cock_c), glass_temp[1] + 0.01*frame_factor) end
    if glass_temp[1] > get(aircond_tempr_cock_c) then glass_temp[1] = math.max(get(aircond_tempr_cock_c), glass_temp[1] - 0.01*frame_factor) end
end
if get(antice_wshld_r_h) > 0 and get(ice_window_heat_on) > 0 then -- the later verifies that the heater(s) are powered
    if glass_temp[2] < 50                     then glass_temp[2] = math.min(50,                     glass_temp[2] + 0.05*frame_factor) end
    if glass_temp[2] > 50                     then glass_temp[2] = math.max(50,                     glass_temp[2] - 0.01*frame_factor) end
else
    if glass_temp[2] < get(aircond_tempr_cock_c) then glass_temp[2] = math.min(get(aircond_tempr_cock_c), glass_temp[2] + 0.01*frame_factor) end
    if glass_temp[2] > get(aircond_tempr_cock_c) then glass_temp[2] = math.max(get(aircond_tempr_cock_c), glass_temp[2] - 0.01*frame_factor) end
end

if get(view) < 1 then
    set(temp_cabin, get(aircond_tempr_cock_c))
    set(glass_ice_cockpit, glass_temp[1])
    set(glass_ice_cockpit2, glass_temp[2])
else
    set(temp_cabin, -((get(night_lights)^0.4)*get(aircond_tempr_cab_c))+get(aircond_tempr_cab_c))
    set(glass_ice_cockpit, -((get(night_lights)^0.4)*glass_temp[1])+glass_temp[1])
    set(glass_ice_cockpit2, -((get(night_lights)^0.4)*glass_temp[2])+glass_temp[2])
end

-- JackZero: End

-- JackZero: START ==================================
-- JackZero: Moved hydraulic logic from mfd.lua

-- hdr engn logic -- JackZero: Simplified
if get(ENGN_running_l) > 0 and get(hydr_l_sov_h) > 0 then
	set(hd_l_engn_state, 1)
else
	set(hd_l_engn_state, 0)
end

if get(ENGN_running_r) > 0 and get(hydr_r_sov_h) > 0 then
	set(hd_r_engn_state, 1)
else
	set(hd_r_engn_state, 0)
end

-- JackZero: changed logic according to POH the L pump is usually powered by R gen and can be powered by APU gen or L gen and bus tie,
-- 			 it cannot be powered by battery power, the R pump is usually powered by L gen and can be powered by APU gen or R gen and
--           bus tie or L battery, but powering by from bat will only happen on AUTO if flaps are non-zero.
--           If only one gen is running, only one pump will be powered (preferably the right), not both.
-- auto: on if pressure is low (engine pump off) or flaps set -- get(generator_on_l) get(generator_on_r)

--                  left gen                 |   apu gen on                      linked via xtie        linked directly            |   right gen on                 linked via bus tie
local l_gen_power = get(generator_on_l) == 1 or (get(APU_generator_on) == 1 and (get(cross_tie) == 1 or get(generator_on_r) == 1)) or (get(generator_on_r) == 1 and get(cross_tie) == 1) or get(gpu_on) == 1
--                  right gen                |   apu gen on                      linked via xtie        linked directly            |   left gen on                  linked via bus tie
local r_gen_power = get(generator_on_r) == 1 or  get(APU_generator_on) == 1                                                        or (get(generator_on_l) == 1 and get(cross_tie) == 1) or get(gpu_on) == 1

local l_bat_power = get(bat_on) > 0 and get(elbus1) > 20 -- battery on and supplying enough power!
local gen_count   = get(generator_on_l) + get(generator_on_r) + get(APU_generator_on) + get(gpu_on) * 2

-- right elec hydr pump logic
if get(r_hpump_h) == 0 then -- OFF
	set(hd_r_pump_state, 0)
elseif get(r_hpump_h) == 2 then -- ON
	-- the right pump will run on battery power as long as there's still enough power:
	set(hd_r_pump_state, (l_gen_power or l_bat_power) and 1 or 0)
else -- AUTO
	-- on gen power, the pump will turn on on low pressure (engine pump off) OR set flaps
	-- on bat power, the pump will turn on on low pressure (engine pump off) AND set flaps
	if l_gen_power then
		set(hd_r_pump_state, (get(hd_r_engn_state) == 0 or (get(flaprat) + get(flaprqst) > 0)) and 1 or 0)
	elseif l_bat_power then
		set(hd_r_pump_state, (get(hd_r_engn_state) == 0 and (get(flaprat) + get(flaprqst) > 0)) and 1 or 0)
	else
		set(hd_r_pump_state, 0)
	end
end

-- only one elec pump per gen:
r_gen_power = r_gen_power and (gen_count > 1 or get(hd_r_pump_state) == 0)

-- left elec hydr pump logic
if get(l_hpump_h) == 0 then -- OFF
	set(hd_l_pump_state, 0)
elseif get(l_hpump_h) == 2 then -- ON
	set(hd_l_pump_state, r_gen_power and 1 or 0)
else -- AUTO
	if r_gen_power then
		set(hd_l_pump_state, (get(hd_l_engn_state) == 0 or (get(flaprat) + get(flaprqst) > 0)) and 1 or 0)
	else
		set(hd_l_pump_state, 0)
	end
end

set(electric_hydraulic_pump_on, (get(hd_l_pump_state) == 1 or get(hd_r_pump_state) == 1) and 1 or 0)

-- JackZero: AUX pump will turn if set to auto and powered
if get(aux_hpump_h) == 1 and get(elbus1) > 20 then
	set(hd_aux_state, 1)
else
	set(hd_aux_state, 0)
end

-- hdr ptu logic
-- on AUTO, the ptu opens if there is right system pressure, the left engine pump is off, the plane is in the air and
-- the gear is not in the commanded position
if get(ptu_h) == 0 then
	set(hd_ptu_state, 0) -- OFF
elseif get(ptu_h) == 2 then
	set(hd_ptu_state, 1) -- ON
else
	if (get(hd_r_pump_state) == 1 or get(hd_r_engn_state) == 1) and hd_l_engn_state == 0 and get(onground_any) == 0 and
		(3 * get(gear_handle_status) ~= (get(gear_deploy_ratio_0)+get(gear_deploy_ratio_1)+get(gear_deploy_ratio_2))) then
		set(hd_ptu_state, 1)
	else
		set(hd_ptu_state, 0)
	end
end

-- JackZero: END ==================

--[=[
if get(rel_AOA, 6) and get(rev_air_dat) == 1 then
    set(airdat2_fault, 1)
    set(airdat1_fault, 1)
end
    --]=]

-- rem before flight ice detect failure
if get(rembf1) == 0 and get(rembf6) == 0 then
set(rel_ice_detect, 6)
else
if last_states[10] - get(rembf1) ~= 0 or last_states[11] - get(rembf6) ~= 0 then
    if get(rembf1) == 1 or get(rembf6) == 1 then
    set(rel_ice_detect, 0)
    end
end
end

-- rem before flight nose gear failure
if get(rembf5) == 0 then
set(rel_lagear1, 6)
else
if last_states[27] - get(rembf5) ~= 0 then
    if get(rembf5) == 1 then
    set(rel_lagear1, 0)
    end
end
end
-- rem before flight left gear failure
if get(rembf13) == 0 then
set(rel_lagear2, 6)
else
if last_states[28] - get(rembf13) ~= 0 then
    if get(rembf13) == 1 then
    set(rel_lagear2, 0)
    end
end
end
-- rem before flight right gear failure
if get(rembf14) == 0 then
set(rel_lagear3, 6)
else
if last_states[29] - get(rembf14) ~= 0 then
    if get(rembf14) == 1 then
    set(rel_lagear3, 0)
    end
end
end


-- rem before flight AOA detect failure

if get(rembf8) == 0 and get(rembf4) == 1 and  get(rev_air_dat) == 2 then
set(rel_AOA, 6)
end

if get(rembf8) == 1 and get(rembf4) == 0 and  get(rev_air_dat) == 0 then
set(rel_AOA, 6)
end

if get(rembf8) == 1 and get(rembf4) == 0 and  get(rev_air_dat) == 1 then
set(rel_AOA, 6)
end


--if get(rembf8) == 0 and get(rembf4) == 1 and  get(rev_air_dat) == 1 then set(rel_AOA, 6) end



if get(rembf4) == 0 and get(rembf8) == 0 then
set(rel_AOA, 6)
else
if last_states[12] - get(rembf4) ~= 0 or last_states[13] - get(rembf8) ~= 0 or refspds_last[7] - get(rev_air_dat) ~= 0 then
    if get(rembf4) == 1 or get(rembf8) == 1 then
    set(rel_AOA, 0)
    end
end
end






if get(rembf4) == 0 then set(airdat1_fault, 1) else set(airdat1_fault, 0) end
if get(rembf8) == 0 then set(airdat2_fault, 1) else set(airdat2_fault, 0) end
----[=[
if get(rembf4) == 1 and get(rembf8) == 1 then
    if get(rel_AOA) == 6 then
    set(airdat1_fault, 1)
    set(airdat2_fault, 1)
    else
        if get(rel_AOA) == 0 then
        set(airdat1_fault, 0)
        set(airdat2_fault, 0)
        end
    end
end


if get(rel_AOA) == 6 then
    if get(rembf4) == 1 and get(rembf8) == 1 then set(airdat1_fault, 1) end
    if get(rembf4) == 0 and get(rembf8) == 1 then set(airdat1_fault, 1) end
    if get(rembf4) == 1 and get(rembf8) == 0 then set(airdat1_fault, 0) end
    if get(rembf4) == 0 and get(rembf8) == 0 then set(airdat1_fault, 1) end

    if get(rembf4) == 1 and get(rembf8) == 1 then set(airdat2_fault, 1) end
    if get(rembf4) == 0 and get(rembf8) == 1 then set(airdat2_fault, 0) end
    if get(rembf4) == 1 and get(rembf8) == 0 then set(airdat2_fault, 1) end
    if get(rembf4) == 0 and get(rembf8) == 0 then set(airdat2_fault, 1) end
else
    if get(rembf4) == 1 and get(rembf8) == 1 then set(airdat1_fault, 0) end
    if get(rembf4) == 0 and get(rembf8) == 1 then set(airdat1_fault, 1) end
    if get(rembf4) == 1 and get(rembf8) == 0 then set(airdat1_fault, 0) end
    if get(rembf4) == 0 and get(rembf8) == 0 then set(airdat1_fault, 1) end

    if get(rembf4) == 1 and get(rembf8) == 1 then set(airdat2_fault, 0) end
    if get(rembf4) == 0 and get(rembf8) == 1 then set(airdat2_fault, 0) end
    if get(rembf4) == 1 and get(rembf8) == 0 then set(airdat2_fault, 1) end
    if get(rembf4) == 0 and get(rembf8) == 0 then set(airdat2_fault, 1) end
end

-- rem before flight pitot failure
if get(rembf2) == 0 then
set(rel_pitot, 6)
else
if last_states[8] - get(rembf2) ~= 0 then
set(rel_pitot, 0)
end
end

if get(rembf7) == 0 then
set(rel_pitot2, 6)
else
if last_states[9] - get(rembf7) ~= 0 then
set(rel_pitot2, 0)
end
end










-- rem before flight engine failure
if get(rembf11) == 0 then
set(rel_engfai1, 6)
else
if last_states[6] - get(rembf11) ~= 0 then
set(rel_engfai1, 0)
end
end



if get(rembf12) == 0 then
set(rel_engfai0, 6)
else
if last_states[7] - get(rembf12) ~= 0 then
set(rel_engfai0, 0)
end
end















-- steer handle

if get(ground_speed) < 22 then
set(steer_handle, get(tire_steer_act))
else
set(steer_handle, 0)
end



-- spoilers and speedbrakes
if get(carets) > 1 and get(carets) < 5 and get(reverser_on_2) ~= 1 then
	throttle_to = 1 -- throttles in takeoff power, spoilers may be armed
elseif get(radio_altimeter_height_ft_pilot) > 200 then
	throttle_to = 1 -- workaround since the flight may have been started in air
end

-- JackZero: Simplified logic, check prop_mode instead of annunciator datarefs
--if get(prop_mode_0) == 3 or get(prop_mode_1) == 3 or get(reverser_on_1) == 1 or get(reverser_on_2) == 1 or get(throttle_ratio_all) < 0.05 then
--	check_thr_rev = 1 -- reversers deployed or idle power: ground spoilers may deploy
--else
--	check_thr_rev = 0
--end
if get(throttle_ratio_all) > 0.8 and get(reverser_on_2) ~= 1 then
throttle_to = 1
end

if get(throttle_ratio_all) < 0.02 and get(reverser_on_2) ~= 1 then
check_thr_rev = 1
elseif get(throttle_ratio_all) > 0.02 and get(reverser_on_2) == 1 then check_thr_rev = 1
elseif get(throttle_ratio_all) > 0.02 and get(reverser_on_2) ~= 1 then check_thr_rev = 0
end



if get(ground_sp_h) == 1 then
    if throttle_to == 1 and gr_sp_deployed == 0 then
    	-- AUTO: if throttle is in T/O position and spoilers aren't currently deployed, arm spoilers
		gr_sp_armed = 1
    end
elseif get(ground_sp_h) == 2 then
    -- ON: arm spoilers
	gr_sp_armed = 1
else
	-- OFF: unarm spoilers, reset T/O 'flag'
	gr_sp_armed = 0
    throttle_to = 0
end

if gr_sp_armed == 1 then
	-- JackZero: changed: we'll use any weight on wheel/wheel speed instead of weight on all wheels and only nose wheel speed
	if check_thr_rev == 1 and
			get(onground_any) +
			((math.max(0.2534 * get(tire_rotation_speed_rad_sec_0), 0.3175 * get(tire_rotation_speed_rad_sec_1), 0.3175 * get(tire_rotation_speed_rad_sec_2)) > 15) and 1 or 0) +
            ((get(radio_altimeter_height_ft_pilot) < 7) and 1 or 0) > 1 then
		gr_sp_deployed  = 1
	else
		gr_sp_deployed = 0
    end
else
    gr_sp_deployed = 0
end

if gr_sp_deployed == 1 and last_states[17] == 0 then
	-- spoilers just deployed, save time
	time_now[8] = get(totaltime)
end

-- stow spoilers after 30 seconds
if gr_sp_deployed == 1 and (get(totaltime) - time_now[8]) > 30 then
	gr_sp_deployed = 0
end

if gr_sp_deployed == 0 and last_states[17] == 1 then
	-- reset arming when spoilers are stowed
	gr_sp_armed = 0
	throttle_to = 0
end

-- JackZero: Simplified logic, moved down

if gr_sp_deployed == 1 then
    -- force spoilers fully deployed
	set(speedbrake_ratio, 1)
else
	-- Sync limit speedbrakes to the range 0.03 to 0.97, sync with sbrk_h:
	--set(sbrk_h, math.min(0.97, math.max(0.03, get(speedbrake_ratio))))
	if spoiler_h_last ~= get(sbrk_h) or gr_sp_deployed_last ~= gr_sp_deployed then
	set(speedbrake_ratio, math.min(0.97, math.max(0.03, get(sbrk_h))))
	spoiler_h_last = get(sbrk_h)
	gr_sp_deployed_last = gr_sp_deployed
	else
	set(sbrk_h, math.min(0.97, math.max(0.03, get(speedbrake_ratio))))
	set(speedbrake_ratio, math.min(0.97, math.max(0.03, get(speedbrake_ratio))))
	end
end
gr_sp_deployed_last = gr_sp_deployed

-- hydr (fl_contr etc)

if get(hydr_pres_1) < 1600 then -- JackZero: Reduced from 2100 psi
set(acf_elev_dn,12)
set(acf_elev_up,17)
set(tire_steer_comm, 0)
set(prop_mode_0, 1)
else
set(acf_elev_dn,20)
set(acf_elev_up,25)
end


if get(hydr_pres_2) < 1600 and get(hydr_pres_1) < 1600 then -- JackZero: Reduced from 2100 psi
set(acf_splr_up, 0)
set(acf_sbrk_up, 0)
set(speedbrake_ratio , 0)
set(acf_splr_up, 30)
    if get(flight_spoil_h) == 0 then
    set(acf_splr_up, 30)
    else

    set(acf_splr_up, 0)
    end
end

if get(hydr_pres_2) < 1600 then  -- JackZero: Reduced from 2100 psi
set(prop_mode_1, 1)
end

if get(rembf10) == 0 then
	--if get(hydr_pres_1) < 1000 and get(hydr_pres_1)<1000 then
	set(velocity_vx, 0)
	set(velocity_vz, 0)
	--end
end

if get(parkbrake) > 0.2 and get(ground_speed) < 1 and get(hydr_pres_1) < 1000 and get(hydr_pres_1)<1000 then
	set(velocity_vx, 0)
	set(velocity_vz, 0)
end

--[=[
if get(eq_ped_nobrk) ~= 0 then
-- brakes with chocks

    if get(rembf10) == 0 then
    set(override_gearbrake, 1)
    set(r_brake_add, 0.9)
    set(l_brake_add, 0.9)
		if get(hydr_pres_1) < 1000 and get(hydr_pres_1)<1000 then
		set(velocity_vx, 0)
		set(velocity_vz, 0)
		end
    else
        if last_states[5] - get(rembf10) ~= 0 then
        set(override_gearbrake, 1)
        set(r_brake_add, 0)
        set(l_brake_add, 0)
        set(override_gearbrake, 0)
        end
    end





    -- brakes when steer



    if get(parkbrake) < 0.2 then
        if get(yoke_heading_ratio) > 0.85 and get(rembf10) == 1 then
        set(override_gearbrake, 1)
        set(r_brake_add, 0)
        end
            if get(yoke_heading_ratio) < -0.85 and get(rembf10) == 1 then
            set(override_gearbrake, 1)
            set(l_brake_add, 0)
            end
    else
        if get(rembf10) == 1 then
        set(override_gearbrake, 0)
        end
    end

else
set(override_gearbrake, 0)
	if get(rembf10) == 0 then
	set(l_brake_add, 0.9)
	set(r_brake_add, 0.9)
		if get(hydr_pres_1) < 1000 and get(hydr_pres_1)<1000 then
		set(velocity_vx, 0)
		set(velocity_vz, 0)
		end
	end
if last_states[5] - get(rembf10) ~= 0 and get(rembf10) == 1 then
set(l_brake_add, 0)
set(r_brake_add, 0)
end
end



--]=]


-------------------
-------------------
--FADEC
-------------------
-------------------

--set(acf_face_jet, (get(barometer_current_inhg)+38.9)*0.0409026)

-- Throttle override
set(override_throttles, 1)

-- JackZero: Simplified, bugfixed (e.g. value == 0.85 unhandled)
if get(gust_lock) == 0 then
	local mthro = math.max(get(ENGN_thro_0),get(ENGN_thro_1))
	if     mthro > 0.95 then set(carets, 4)
	elseif mthro > 0.9  then set(carets, 3)
	elseif mthro > 0.85 then set(carets, 2)
	elseif mthro > 0.1  then set(carets, 1)
	elseif mthro >= 0   then set(carets, 0) end
else
	set(carets, 5)
end
-- JackZero: End

-- JackZero: New datarefs for external displays
if get(gust_lock) == 0 and get(engine_speed_rpm_0) > 300 then
	if get(thrust_reverser_deploy_ratio_0) < 0.1 and get(reverser_on_1) < 1 then
		set(engine_state_rpm_l, get(engine_speed_rpm_0) > 1000 and 1 or 0)
		if     get(ENGN_thro_0) > 0.95 then	set(engine_state_l, 4)
		elseif get(ENGN_thro_0) > 0.9  then	set(engine_state_l, 3)
		elseif get(ENGN_thro_0) > 0.85 then	set(engine_state_l, 2)
		elseif get(ENGN_thro_0) > 0.05 then	set(engine_state_l, 1)
		else                             	set(engine_state_l, 0) end
	else
		set(engine_state_rpm_l, 0)
		set(engine_state_l, -1)
	end
else
	set(engine_state_rpm_l, 0)
	set(engine_state_l, -2)
end

if get(gust_lock) == 0 and get(engine_speed_rpm_1) > 300 then
	if get(thrust_reverser_deploy_ratio_1) < 0.1 and get(reverser_on_2) < 1 then
		set(engine_state_rpm_r, get(engine_speed_rpm_1) > 1000 and 1 or 0)
		if     get(ENGN_thro_1) > 0.95 then	set(engine_state_r, 4)
		elseif get(ENGN_thro_1) > 0.9  then	set(engine_state_r, 3)
		elseif get(ENGN_thro_1) > 0.85 then	set(engine_state_r, 2)
		elseif get(ENGN_thro_1) > 0.05 then	set(engine_state_r, 1)
		else								set(engine_state_r, 0) end
	else
		set(engine_state_rpm_r, 0)
		set(engine_state_r, -1)
	end
else
	set(engine_state_rpm_r, 0)
	set(engine_state_r, -2)
end
-- JackZero: End


if  get(gust_lock_anm) < get(gust_lock) then
set(gust_lock_anm, get(gust_lock_anm) + 0.02*((get(frame_rate_period)-0.02)*25 + 0.5))
end
if  get(gust_lock_anm) > get(gust_lock) then
set(gust_lock_anm, get(gust_lock_anm) - 0.02*((get(frame_rate_period)-0.02)*25 + 0.5))
end

if get(gust_lock_anm) < 0 then set(gust_lock_anm, 0) end
if get(gust_lock_anm) > 1 then set(gust_lock_anm, 1) end


if ISA_t_tmp[1] < get(ISA_t) then ISA_t_tmp[1] = ISA_t_tmp[1] + 0.005 end
if ISA_t_tmp[1] > get(ISA_t) then ISA_t_tmp[1] = ISA_t_tmp[1] - 0.005 end

temp_coeff[1] = 0.2242*get(temperature_ambient_c) + 23.193

-- JackZero: Split for L/R engine:
if ISA_t_tmp[1] < 20 then
    baro_coeff[3] = -(get(barometer_current_inhg)-4.1)*((-(ISA_t_tmp[1]+55)*0.0064) + 0.651) + 94.1
else  -- JackZero: slight correction of the constant factor from +0.203 to +0.117 so there isn't a jump in the curve
    baro_coeff[3] = -(get(barometer_current_inhg)-4.1)*((ISA_t_tmp[1]-15)*0.01085+0.117) + 94.1
end

-- JackZero: Note that baro_coeff is our target N1
if get(engine_state_l) == 4 then
    baro_coeff[1] = baro_coeff[3] + 0.2
elseif get(engine_state_l) == 3 then
    baro_coeff[1] = baro_coeff[3]
elseif get(engine_state_l) < 3 then
    baro_coeff[1] = baro_coeff[3] - 0.3
end

if get(engine_state_r) == 4 then
    baro_coeff[2] = baro_coeff[3] + 0.2
elseif get(engine_state_r) == 3 then
    baro_coeff[2] = baro_coeff[3]
elseif get(engine_state_r) < 3 then
    baro_coeff[2] = baro_coeff[3] - 0.3
end

set(TO_n1,   math.max(baro_coeff[1], baro_coeff[2]))
set(TO_n1_l, baro_coeff[1])
set(TO_n1_r, baro_coeff[2])
-- JackZero: End

if throttle_use_0 < get(ENGN_thro_0) then
    throttle_use_0 = throttle_use_0 + get(frame_rate_period)*0.08
elseif throttle_use_0 > get(ENGN_thro_0) then
    throttle_use_0 = throttle_use_0 - get(frame_rate_period)*0.08
end

if math.max(get(ENGN_thro_0) - get(ENGN_thro_1), get(ENGN_thro_1) - get(ENGN_thro_0)) > 0.03 then
	if throttle_use_1 < get(ENGN_thro_1) then
		throttle_use_1 = throttle_use_1 + get(frame_rate_period)*0.08
	elseif throttle_use_1 > get(ENGN_thro_1) then
		throttle_use_1 = throttle_use_1 - get(frame_rate_period)*0.08
	end
else
	--if throttle_use_1 < get(ENGN_thro_0) then
		throttle_use_1 = throttle_use_0-- + get(frame_rate_period)*0.08
	--elseif throttle_use_1 > get(ENGN_thro_0) then
	--	throttle_use_1 = throttle_use_0 - get(frame_rate_period)*0.08
	--end
end


if throttle_use_0 > 1.0 then throttle_use_0 = 1.0 end
if throttle_use_0 < 0.0 then throttle_use_0 = 0.0 end
if throttle_use_1 > 1.0 then throttle_use_1 = 1.0 end
if throttle_use_1 < 0.0 then throttle_use_1 = 0.0 end

if get(ISA_t) > 0 then
thrust_temp_coeff =  get(ISA_t)*80
else
thrust_temp_coeff = 0
end

alt_coeff = 192

if get(ENGN_running_l) > 0 and get(ENGN_running_l) > 0 then
delta_thrust = -300
else
delta_thrust = 2222-300
end

if get(barometer_current_inhg) >= 29.92 and ISA_t_tmp[1] <= 15 then
thrust[1] = 30563 --+ 5550
elseif get(barometer_current_inhg) >= 29.92 and ISA_t_tmp[1] > 15 then thrust[1] = 30363 - (get(ISA_t)*30-450)  - (get(altitude_ft_pilot) / alt_coeff) - thrust_temp_coeff + delta_thrust
elseif get(barometer_current_inhg) < 29.92 and ISA_t_tmp[1] <= 15 and ISA_t_tmp[1] > 0 then thrust[1] = (get(barometer_current_inhg)+4.63)*879.086 - (get(altitude_ft_pilot) / alt_coeff) - thrust_temp_coeff  + delta_thrust
elseif get(barometer_current_inhg) < 29.92 and ISA_t_tmp[1] <= 0 then thrust[1] = (get(barometer_current_inhg)+4.63)*879.086 + (-ISA_t_tmp[1]*26.5) - (get(altitude_ft_pilot) / alt_coeff) - thrust_temp_coeff  + delta_thrust
elseif get(barometer_current_inhg) < 29.92 and ISA_t_tmp[1] > 15 then thrust[1] = (get(barometer_current_inhg)+4.63)*879.086 - (ISA_t_tmp[1]*30-450) - (get(altitude_ft_pilot) / alt_coeff) - thrust_temp_coeff  + delta_thrust


end

-- JackZero: Move a bit forward
-- crz + mach hold
if get(engn_mach_h) == 1 and get(engine_state_l) == 1 and get(engine_state_r) == 1 and get(jet_sync_mode) > 0 and get(altitude_hold_status) == 2 and math.abs(get(ENGN_N1_L) - get(ENGN_N1_R)) <= 0.15 then
    if last_states[23] ~= get(engn_mach_h) then -- mach hold just engaged
        set(engn_mach_status, 1)
        mach_hold_target = {get(ENGN_thro_0), get(ENGN_thro_1), get(ENGN_N1_L), get(ENGN_N1_R), get(machno)}
    end
else
    -- mach hold aborted
    set(engn_mach_status, 0)
end

-- JackZero: Split to a separate block for L/R throttle:
if get(gust_lock) == 0 then

    if get(engn_mach_status) == 1 then -- note: checking of engine state / carets not necessary... mach_status would already be reset in that case
        -- MACH HOLD -- JackZero: Also check for movement of R throttle, hold won't work on idle thrust (state 0)!
        if math.abs(mach_hold_target[1] - get(ENGN_thro_0)) < 0.02 and math.abs(mach_hold_target[2] - get(ENGN_thro_1)) < 0.02 then
            -- check the end condition first
            if math.abs(mach_hold_target[3] - get(ENGN_N1_L)) > 3 or math.abs(mach_hold_target[4] - get(ENGN_N1_R)) > 3 then
                set(engn_mach_status, 0)
            else
                if get(machno) < mach_hold_target[5] then
                    -- JackZero: the previous step of (mach_hold_target[5]-get(machno))*1000) without a multiplication by the frame rate
                    --           period resulted in a change of 150% max throttle on a difference of 1kt (0,0015 mach).
                    --           This resulted in the throttle permaently juming from 100% to 0%.
                    --           Added a math.min statement to limit the rate of change and maded it depend on the frame time.
                    --           Now at 50 fps the change rate has a maximum of 0.06% per frame (3% per second) at a difference of 1kt
                    --           and a minimum of at 50 fps results in a change of 0.01% per frame (0.5% per second).
                    set(ENGN_thro_use_0, get(ENGN_thro_use_0) + math.max(0.005, math.min(0.03, (mach_hold_target[5]-get(machno))*20))*get(frame_rate_period))
                    set(ENGN_thro_use_1, get(ENGN_thro_use_1) + math.max(0.005, math.min(0.03, (mach_hold_target[5]-get(machno))*20))*get(frame_rate_period))
                elseif get(machno) > mach_hold_target[5] then
                    set(ENGN_thro_use_0, get(ENGN_thro_use_0) - math.max(0.005, math.min(0.03, (get(machno)-mach_hold_target[5])*20))*get(frame_rate_period))
                    set(ENGN_thro_use_1, get(ENGN_thro_use_1) - math.max(0.005, math.min(0.03, (get(machno)-mach_hold_target[5])*20))*get(frame_rate_period))
                end
            end
        else
            set(engn_mach_status, 0)
        end
    end

    if get(engine_state_l) > 1 then
        -- LEFT ENGINE: CLB, TO, APR
        if get(ENGN_N1_L) < baro_coeff[1] then
            set(ENGN_thro_use_0, get(ENGN_thro_use_0) + ((baro_coeff[1]- get(ENGN_N1_L))*get(frame_rate_period)*0.005))
        elseif get(ENGN_N1_L) > baro_coeff[1] then
            set(ENGN_thro_use_0, get(ENGN_thro_use_0) - ((get(ENGN_N1_L) - baro_coeff[1])*get(frame_rate_period)*0.005))
        end
    end

    if get(engine_state_r) > 1 then
        -- RIGHT ENGINE: CLB, TO, APR
        if get(ENGN_N1_R) < baro_coeff[2] then
            set(ENGN_thro_use_1, get(ENGN_thro_use_1) + ((baro_coeff[2] - get(ENGN_N1_R))*get(frame_rate_period)*0.005))
        elseif get(ENGN_N1_R) > baro_coeff[2] then
            set(ENGN_thro_use_1, get(ENGN_thro_use_1) - ((get(ENGN_N1_R) - baro_coeff[2])*get(frame_rate_period)*0.005))
        end
    end

    -- don't use elseif, we might just have aborted mach hold...
    if (get(engine_state_l) == 1 and get(engn_mach_status) == 0) or get(engine_state_l) == 0 then
        -- LEFT ENGINE: CRZ/IDLE (no MACH HOLD)
        if get(ENGN_N1_L) > baro_coeff[1] then
            -- JackZero: N1 to high, reduce throttle
            throttle_use_0 = throttle_use_0 - get(frame_rate_period)*0.16
        end
        if get(onground_all) == 1 then
            set(ENGN_thro_use_0, throttle_use_0*0.01*baro_coeff[1]*1.195)
        else
            set(ENGN_thro_use_0, throttle_use_0*0.01*baro_coeff[1]*1.195 + ((0.85 - throttle_use_0)*1.1764)*(math.max((math.sqrt((get(altitude_ft_pilot) + 4000))/3),33.4)-33.4)*0.013)
        end
    end

    if (get(engine_state_r) == 1 and get(engn_mach_status) == 0) or get(engine_state_r) == 0 then
        -- RIGHT ENGINE: CRZ/IDLE (no MACH HOLD)
        if get(ENGN_N1_R) > baro_coeff[2] then
            -- JackZero: N1 to high, reduce throttle
            throttle_use_1 = throttle_use_1 - get(frame_rate_period)*0.16
        end
        if get(onground_all) == 1 then
            set(ENGN_thro_use_1, throttle_use_1*0.01*baro_coeff[2]*1.195)
        else
            set(ENGN_thro_use_1, throttle_use_1*0.01*baro_coeff[2]*1.195 + ((0.85 - throttle_use_1)*1.1764)*(math.max((math.sqrt((get(altitude_ft_pilot) + 4000))/3),33.4)-33.4)*0.013)
        end
    end

    -- JackZero: Merged code from previous blocks since this part relies on the engine supplying the most thrust:
    if get(thrust_reverser_deploy_ratio_0) < 0.1 and get(thrust_reverser_deploy_ratio_1) < 0.1 then
        local max_point_thrust = math.max(get(POINT_thrust_0),get(POINT_thrust_1))
        if max_point_thrust > 0 then
            if get(carets) > 1 and get(carets) < 5 then -- CLB, TO, APR
                if max_point_thrust < thrust[1] then
                    set(acf_tmax, get(acf_tmax) + (thrust[1] - max_point_thrust)*0.5)
                elseif max_point_thrust >= thrust[1] then
                    set(acf_tmax, get(acf_tmax) - (max_point_thrust - thrust[1])*0.5)
                end
            else -- CRZ
                local max_throttle_use = math.max(get(ENGN_thro_use_0),get(ENGN_thro_use_1))
                local tfactor = (get(engn_mach_status) == 1 and 0.79 or 0.8)
                if max_point_thrust < thrust[1] * max_throttle_use * tfactor then
                    set(acf_tmax, get(acf_tmax) + (thrust[1] * max_throttle_use - max_point_thrust)*0.5)
                elseif max_point_thrust >= thrust[1] * max_throttle_use * tfactor then
                    set(acf_tmax, get(acf_tmax) - (max_point_thrust - thrust[1] * max_throttle_use)*0.5)
                end
            end
        end
    end
end

-- JackZero: Fixed a bug, throttle_use is a local var, also lowered the threshold from .9 to .1
if get(thrust_reverser_deploy_ratio_0) > 0.1 then
    if get(prop_mode_0) == 1 then
        -- JackZero: Set throttle to zero during reverser stowing:
        throttle_use_0 = 0
    elseif get(ENGN_N1_L) > 70.7 then
        -- JackZero: N1 to high, reduce throttle
        throttle_use_0 = throttle_use_0 - get(frame_rate_period)*0.16
    end
    set(ENGN_thro_use_0, throttle_use_0*0.55)
end
if get(thrust_reverser_deploy_ratio_1) > 0.1 then
    if get(prop_mode_1) == 1 then
        -- JackZero: Set throttle to zero during reverser stowing:
        throttle_use_1 = 0
    elseif get(ENGN_N1_R) > 70.7 then
        -- JackZero: N1 to high, reduce throttle
        throttle_use_1 = throttle_use_1 - get(frame_rate_period)*0.16
    end
    set(ENGN_thro_use_1, throttle_use_1*0.55)
end
-- JackZero: End

-- JackZero: Reverse thrust when stowing the reversers (prop mode will already be 1)
if get(thrust_reverser_deploy_ratio_0) > 0.1 and get(prop_mode_0) == 1 then
    if get(POINT_thrust_0) > 0 then set(POINT_thrust_0, get(POINT_thrust_0) * -0.55) end
end
if get(thrust_reverser_deploy_ratio_1) > 0.1 and get(prop_mode_1) == 1 then
    if get(POINT_thrust_1) > 0 then set(POINT_thrust_1, get(POINT_thrust_1) * -0.55) end
end
-- JackZero: End

if get(ENGN_thro_use_0) > 0.951 then set(ENGN_thro_use_0, 0.951) end
if get(ENGN_thro_use_1) > 0.951 then set(ENGN_thro_use_1, 0.951) end
if get(ENGN_thro_use_0) <= 0 then set(ENGN_thro_use_0, 0) end
if get(ENGN_thro_use_1) <= 0 then set(ENGN_thro_use_1, 0) end

if  get(gust_lock) == 1 and get(thrust_reverser_deploy_ratio_1) < 0.9  and get(thrust_reverser_deploy_ratio_1) < 0.9 then
    set(ENGN_thro_use_0, 0)
    set(ENGN_thro_use_1, 0)
end


if get(gust_lock) == 1 then
    set(prop_mode_0, last_states[14])
    set(prop_mode_1, last_states[15])
    if get(reverser_on_1) ~= 1 and get(reverser_on_2) ~= 1 then
    set(throttle_ratio_all, 0)
    end
end

if get(reverser_on_1) == 1 then
set(thr_anm1, (-get(sim_thr_anm1)*0.24)+0.24)
else
    if get(gust_lock) == 0 then
        if get(sim_thr_anm1) > 0.85 and get(sim_thr_anm1) < 0.9 then
        set(thr_anm1, 0.75)
        elseif get(sim_thr_anm1) > 0.9 and get(sim_thr_anm1) < 0.95 then set(thr_anm1, 0.85)
        elseif get(sim_thr_anm1) > 0.95 and get(sim_thr_anm1) < 1.1 then set(thr_anm1, 1)
        elseif get(sim_thr_anm1) < 0.85 then set(thr_anm1, ((get(sim_thr_anm1)*0.62)+0.38)*0.80)
        end
    else
    set(thr_anm1, 0.3)
    end
end

if get(reverser_on_2) == 1 then
set(thr_anm2, (-get(sim_thr_anm2)*0.24)+0.24)
else
if get(gust_lock) == 0 then
    if get(sim_thr_anm2) > 0.85 and get(sim_thr_anm2) < 0.9 then
    set(thr_anm2, 0.75)
    elseif get(sim_thr_anm2) > 0.9 and get(sim_thr_anm2) < 0.95 then set(thr_anm2, 0.85)
    elseif get(sim_thr_anm2) > 0.95 and get(sim_thr_anm2) < 1.1 then set(thr_anm2, 1)
    elseif get(sim_thr_anm2) < 0.85 then set(thr_anm2, ((get(sim_thr_anm2)*0.62)+0.38)*0.80)
    end
else
set(thr_anm2, 0.3)
end
end


if get(onground_any) == 0 then
set(prop_mode_0, 1)
set(prop_mode_1, 1)
end

--[[
set(test_data_1, ISA_t_tmp[1])
set(test_data_2, baro_coeff[1])
set(test_data_3, thrust[1])
set(test_data_4, get(POINT_thrust_0))
set(test_data_5, get(acf_tmax))
--]]

if starter_last[1] - get(starter_duration_l) ~= 0 or starter_last[2] - get(starter_duration_r) ~= 0 then set(acf_tmax, 33363) end



if get(thrust_reverser_deploy_ratio_0) > 0.9 or get(thrust_reverser_deploy_ratio_1)> 0.9 then set(acf_tmax, get(acf_tmax) - (math.max(-get(POINT_thrust_0),-get(POINT_thrust_1))-thrust[1]*math.max(get(ENGN_thro_use_0),get(ENGN_thro_use_1)))*0.5) end

if get(acf_tmax) <= 6000 then set(acf_tmax, 6000) end
if get(acf_tmax) > 53363 then set(acf_tmax, 53363) end


--//rain animations
--[=[
if rain_perc_tmp[1] < get(rain_percent) then
rain_perc_tmp[1] = rain_perc_tmp[1] + 0.0008
end

if rain_perc_tmp[1] > get(rain_percent) then
rain_perc_tmp[1] = rain_perc_tmp[1] - 0.0008
end

if get(ground_speed) < 80 then
speed_lim = get(ground_speed)
else
speed_lim = 80
end

if get(totaltime) - time_now[5] < 10-rain_perc_tmp[1]*6 then
anm_time = ((get(totaltime) - time_now[5])/(10-rain_perc_tmp[1]*6))*360
else
time_now[5] = get(totaltime)
end




if get(totaltime) - time_now[6] < 14-rain_perc_tmp[1]*11 then
drop_anm_time = ((get(totaltime) - time_now[6])/(14-rain_perc_tmp[1]*11))*360
else
time_now[6] = get(totaltime)
end

if get(totaltime) - time_now[7] < 20-rain_perc_tmp[1]*15 then
drop_anm_time2 = ((get(totaltime) - time_now[7])/(20-rain_perc_tmp[1]*15))*360
else
time_now[7] = get(totaltime)
end

set(rain_move1, drop_anm_time)
set(rain_move2, drop_anm_time2)

set(rain_drop1, ((1-(0.1/(rain_perc_tmp[1]+0.1)))+0.9*rain_perc_tmp[1])*(1- 0.00015625*speed_lim*speed_lim)*1.5)

set(rain_back, ((1-(0.1/(rain_perc_tmp[1]+0.1)))+0.9*rain_perc_tmp[1])*(1- 0.00015625*speed_lim*speed_lim))



if anm_time > 90 and  anm_time < 270 then
set(rain_scr1, (1.5-(anm_time*1.5/270))*(1-(0.11/(rain_perc_tmp[1]+0.095))-0.15)*(1- 0.00015625*speed_lim*speed_lim))
else
set(rain_scr1, 0)
end


if anm_time > 180 and  anm_time < 360 then
set(rain_scr2, (2-((anm_time*2)/360))*(1-(0.11/(rain_perc_tmp[1]+0.095))-0.15)*(1- 0.00015625*speed_lim*speed_lim))
else
set(rain_scr2, 0)
end

if anm_time > 0 and  anm_time < 180 then
set(rain_scr3, (1-(anm_time*1/180))*(1-(0.11/(rain_perc_tmp[1]+0.095))-0.15)*(1- 0.00015625*speed_lim*speed_lim))
else
set(rain_scr3, 0)
end


if anm_time > 270 and  anm_time <= 360 then
set(rain_scr4, (3-((2*anm_time+180)/360))*(1-(0.11/(rain_perc_tmp[1]+0.095))-0.15)*(1- 0.00015625*speed_lim*speed_lim))
else
    if anm_time >= 0 and  anm_time < 90 then
    set(rain_scr4, (1-((anm_time+90)*1/180))*(1-(0.11/(rain_perc_tmp[1]+0.095))-0.15)*(1- 0.00015625*speed_lim*speed_lim))
    else
    --set(rain_scr4, 0)
    end
--set(rain_scr4, 0)
end

--]=]

--// custom sliders

if last_states[1]-get(customslider1) ~= 0 then
time_now[1] = get(totaltime)
last_states_r[1] = get(customslider1_r)
end
if last_states[2]-get(customslider2) ~= 0 then
time_now[2] = get(totaltime)
last_states_r[2] = get(customslider2_r)
end
if last_states[3]-get(customslider3) ~= 0 then
time_now[3] = get(totaltime)
last_states_r[3] = get(customslider3_r)
end
if last_states[4]-get(customslider4) ~= 0 then
time_now[4] = get(totaltime)
last_states_r[4] = get(customslider4_r)
end

--custstomslider1
if get(customslider1) == 1 and get(customslider1_r) < 1 then
set(customslider1_r, ((get(totaltime) - time_now[1]) / 0.5)+last_states_r[1])
if get(customslider1_r) > 1 then
set(customslider1_r, 1)
end
end

if get(customslider1) == 0 and get(customslider1_r) > 0 then
set(customslider1_r, (last_states_r[1]*0.5 - (get(totaltime) - time_now[1])) / 0.5)
if get(customslider1_r) < 0 then
set(customslider1_r, 0)
end
end

--custstomslider2
if get(customslider2) == 1 and get(customslider2_r) < 1 then
set(customslider2_r, ((get(totaltime) - time_now[2]) / 8)+last_states_r[2])
if get(customslider2_r) > 1 then
set(customslider2_r, 1)
end
end

if get(customslider2) == 0 and get(customslider2_r) > 0 then
set(customslider2_r, (last_states_r[2]*8 - (get(totaltime) - time_now[2])) / 8)
if get(customslider2_r) < 0 then
set(customslider2_r, 0)
end
end

--custstomslider3
if get(customslider3) == 1 and get(customslider3_r) < 1 then
set(customslider3_r, ((get(totaltime) - time_now[3]) / 4)+last_states_r[3])
if get(customslider3_r) > 1 then
set(customslider3_r, 1)
end
end

if get(customslider3) == 0 and get(customslider3_r) > 0 then
set(customslider3_r, (last_states_r[3]*4 - (get(totaltime) - time_now[3])) / 4)
if get(customslider3_r) < 0 then
set(customslider3_r, 0)
end
end

--custstomslider4 - emergency exit
if get(customslider4) == 1 and get(customslider4_r) < 1 then
set(customslider4_r, ((get(totaltime) - time_now[4]) / 4)+last_states_r[4])
if get(customslider4_r) > 1 then
set(customslider4_r, 1)
end
end

if get(customslider4) == 0 and get(customslider4_r) > 0 then
set(customslider4_r, (last_states_r[4]*4 - (get(totaltime) - time_now[4])) / 4)
if get(customslider4_r) < 0 then
set(customslider4_r, 0)
end
end

--// electrical

set(bat_on, get(left_bat_h))
set(bat_on2, get(right_bat_h))

-- JackZero: Complete overhaul of bus tie code:
-- BUS-TIE
if get(gpu_on) ~= 0 then
    -- force tie and button
    set(cross_tie, 1)
    set(bus_tie_h, 1)
    -- turn gens off, depress switches, unlink apugen
    set(electr_gen_l_h,   0)
    set(electr_gen_r_h,   0)
    set(electr_apugen_h,  0)
    set(APU_generator_on, 0)
    set(generator_on_l,   0)
    set(generator_on_r,   0)
	set(electr_apugen_r,  0)
	set(electr_apugen_l,  0)
else
    -- set gens according to switches
    set(APU_generator_on, get(electr_apugen_h))
    set(generator_on_l,   get(electr_gen_l_h))
    set(generator_on_r,   get(electr_gen_r_h))

    if busTieSituation() then
        set(cross_tie, get(bus_tie_h))
    else
        set(cross_tie, 0)
        set(bus_tie_h, 1) -- reset switch
    end

    if get(APU_generator_on) > 0 then
        if get(generator_amps_l) == 0 and get(generator_amps_r) > 0 then
            -- link apu to left bus
            set(electr_apugen_r, 0)
            set(electr_apugen_l, 1)
        else
            -- link to right bus
            set(electr_apugen_r, 1)
            set(electr_apugen_l, 0)
        end
    else
        -- dont link apu
        set(electr_apugen_r, 0)
        set(electr_apugen_l, 0)
    end
end

--JackZero: End

--if electric_last[1] - get(gpu_on) ~= 0 and get(gpu_on) == 1 then set(bus_tie_h, 1) end
--if electric_last[1] - get(gpu_on) ~= 0 and get(gpu_on) == 0 then set(bus_tie_h, 0) end

-- JackZero: Added functionality for manual toggle of GPU
if get(onground_all) == 1 and get(groundspeed) < 0.5 then
	if get(gpu_mode) == 0 then
		-- auto gpu
		set(gpu_state, 2)
		set(electr_extp_avail, 0.7)
		set(gpu_on, get(electr_extp_h))
	else
		-- manual gpu
		if get(gpu_state) == 0 then
			set(gpu_state, 1) -- set connectable
		end

		if get(gpu_state) == 2 then
			set(electr_extp_avail, 0.7)
			set(gpu_on, get(electr_extp_h))
		else
			set(electr_extp_avail, 0)
			set(gpu_on, 0)
		end
	end
else
	set(gpu_state, 0)
	set(electr_extp_avail, 0)
	set(electr_extp_h, 0)
	set(gpu_on, 0)
end
-- JackZero: End

-- JackZero: Move custom amps and volts logic here from mfd.lua, straightened code
if get(APU_generator_on) > 0 and get(generator_amps_l) > 0 then
	if get(generator_amps_r) > 0 then
		set(gen_amps_r, get(generator_amps_r)/2) -- correction for apugen feeding left bus
		set(gen_amps_apu, get(gen_amps_r))
	else
		set(gen_amps_r, 0)
		set(gen_amps_apu, 54) -- default for right bus feeding
	end
	set(gen_amps_l, get(generator_amps_l)*2.8) -- correction for apugen feeding to left bus
else
	set(gen_amps_r, get(generator_amps_r))
	set(gen_amps_l, get(generator_amps_l))
	if get(APU_generator_on) == 0 then
		set(gen_amps_apu, 0)
	elseif get(generator_amps_r) == 0 then
		set(gen_amps_apu, get(APU_generator_amps)) -- feeding both buses
	else
		set(gen_amps_apu, 25) -- default for left bus feeding
	end
end

-- voltage:
if get(ENGN_running_l) > 0  then
	set(gen_volts_l, 28.5 - get(gen_amps_l) / 150) -- -.1 volt per 15 amps
else
	set(gen_volts_l, 0)
end

if get(ENGN_running_r) > 0  then
	set(gen_volts_r, 28.5 - get(gen_amps_r) / 150) -- -.1 volt per 15 amps
else
	set(gen_volts_r, 0)
end

if get(APU_running) > 0  then
	set(gen_volts_apu, 28.5 - get(gen_amps_apu) / 150) -- -.1 volt per 15 amps
else
	set(gen_volts_apu, 0)
end

if get(gpu_on) == 1 then
	set(gpu_volts, 28.1)
elseif get(gpu_state) == 2 then
	set(gpu_volts, 28.4)
else
	set(gpu_volts, 0)
end
-- JackZero: End


set(bat_on3, get(electr_stbi_h))


-- JackZero:
if get(bleed_apu_h) == 1  then
    set(bleed_air_mode, 4)
elseif (get(bleed_en_l_h) + get(bleed_en_r_h) + get(bleed_xbleed_h)) > 1 then -- either both engines ON or one engine and xbleed ON
    set(bleed_air_mode, 2)
elseif get(bleed_en_l_h) == 1 then
    set(bleed_air_mode, 1)
elseif get(bleed_en_r_h) == 1 then
    set(bleed_air_mode, 3)
else
    set(bleed_air_mode, 0)
end
-- JackZero: End


--// Annuns lightning

local night_coeff = (get(night_lights)*get(night_lights)*0.8)+0.2
local night_coeff2 = (get(night_lights)*0.4)+0.6

if elec_on == 1 then
annun_coeff = (get(annun_h)*0.3)+0.7
elseif get(elbus1) >= 10 and get(elbus2) >= 10 then annun_coeff = (get(annun_h)*0.3)+0.7
elseif get(elbus1) < 10 and get(elbus2) < 10 then annun_coeff = 0
end



set(bleed_apu, get(bleed_apu_h)*annun_coeff)
set(electr_extp, get(gpu_on)*annun_coeff) -- JackZero: Moved down here to make brightness depend on the dim/brt switch

set(bleed_en_l, (1-get(bleed_en_l_h))*annun_coeff)
set(bleed_en_r, (1-get(bleed_en_r_h))*annun_coeff)
set(bleed_xbleed, get(bleed_xbleed_h)*annun_coeff)

set(annun, get(anns_lit)*annun_coeff)
set(entrlight, get(entrlight_h)*annun_coeff)
set(cablight, get(cablight_h)*annun_coeff)
set(right_bat, (1-get(right_bat_h))*annun_coeff)
set(left_bat, (1-get(left_bat_h))*annun_coeff)
set(bus_tie, get(cross_tie)*annun_coeff)

set(flight_spoil, get(flight_spoil_h)*annun_coeff)

set(fuel_xflow_up, get(fuel_xflow_up_h)*annun_coeff)
set(fuel_xflow_dn, get(fuel_xflow_dn_h)*annun_coeff)

set(nws, (1-get(nws_h))*annun_coeff)


set(taws_ter, (1-get(taws_ter_h))*annun_coeff)
set(taws_flp, (1-get(taws_flp_h))*annun_coeff)
set(taws_gs, (1-get(taws_gs_h))*annun_coeff)

set(aircond_man_temp, get(aircond_man_temp_h)*annun_coeff)
set(aircond_ramair, get(aircond_ramair_h)*annun_coeff)

set(pressure_man, get(pressure_man_h)*annun_coeff)
set(pressure_ditch, get(pressure_ditch_h)*annun_coeff)
set(pressure_emer_depr, get(pressure_emer_depr_h)*annun_coeff)





-- atni ice system
set(antice_wshld_r, (1-get(antice_wshld_r_h))*annun_coeff)
set(antice_wshld_l, (1-get(antice_wshld_l_h))*annun_coeff)
if (get(antice_wshld_l_h)) > 0 then set(ice_window_heat_on, 1) end
if (get(antice_wshld_r_h)) > 0 then set(ice_window_heat_on, 1) end
if (get(antice_wshld_l_h)) < 1 and (get(antice_wshld_r_h)) < 1 then set(ice_window_heat_on, 0) end
if (get(antice_wshld_l_h)) > 1 and (get(antice_wshld_r_h)) > 1 then set(ice_window_heat_on, 1) end

set(antice_engn_r, get(antice_engn_r_h)*annun_coeff)
set(antice_engn_l, get(antice_engn_l_h)*annun_coeff)
if get(antice_engn_r_h) > 0 then set(ice_inlet_heat_on, 1) end
if get(antice_engn_l_h) > 0 then set(ice_inlet_heat_on, 1) end
if get(antice_engn_l_h) < 1 and get(antice_engn_r_h) < 1 then set(ice_inlet_heat_on, 0) end

-- JackZero: Moved here from mfd.lua
-- antice xbleed valve
if get(antice_wing_h) > 0 and ((get(ENGN_running_l) > 0 and get(antice_wngsource_r) == 0) or (get(ENGN_running_r) > 0 and get(antice_wngsource_r) == 2)) then
    set(antice_wings_xbleed_valve, 1)
else
    set(antice_wings_xbleed_valve, 0)
end

if get(ENGN_running_l) > 0 and  get(antice_wngsource_r) < 2 then
    if get(antice_engn_l_h) > 0 and get(ice_inlet_heat_on) > 0 then
        if get(antice_wing_h) > 0 then set(antice_psi_l, get(antice_wngsource_r) == 0 and 34 or 35)
        else                           set(antice_psi_l, 37) end
    else
        if get(antice_wing_h) > 0 then set(antice_psi_l, get(antice_wngsource_r) == 0 and 35 or 36)
        else                           set(antice_psi_l, 38) end
    end
else
    set(antice_psi_l, 0)
end

if get(ENGN_running_r) > 0 and  get(antice_wngsource_r) > 0 then
    if get(antice_engn_r_h) > 0 and get(ice_inlet_heat_on) > 0 then
        if get(antice_wing_h) > 0 then set(antice_psi_r, get(antice_wngsource_r) == 2 and 34 or 35)
        else                           set(antice_psi_r, 37) end
    else
        if get(antice_wing_h) > 0 then set(antice_psi_r, get(antice_wngsource_r) == 2 and 35 or 36)
        else                           set(antice_psi_r, 38) end
    end
else
    set(antice_psi_r, 0)
end

-- JackZero: End


set(antice_probe_r, (1-get(antice_probe_r_h))*annun_coeff)
set(antice_probe_l, (1-get(antice_probe_l_h))*annun_coeff)

if get(antice_probe_l_h) > 0 then
set(ice_pitot_heat_on_pilot, 1)
set(ice_AOA_heat_on, 1)
else
set(ice_pitot_heat_on_pilot, 0)
end

if get(antice_probe_r_h) > 0 then
set(ice_pitot_heat_on_copilot, 1)
set(ice_AOA_heat_on, 1)
else
set(ice_pitot_heat_on_copilot, 0)
end

if get(antice_probe_r_h) < 1 and get(antice_probe_l_h) < 1 then set(ice_AOA_heat_on, 0) end





--
set(antice_wing, get(antice_wing_h)*annun_coeff)

if get(antice_wing_h) > 0 and get(ENGN_running_l) > 0 then
set(ice_surfce_heat_left_on, 1)
else
if get(antice_wing_h) > 0 and get(antice_wngsource_r) < 2 and get(ENGN_running_l) < 1 then
set(ice_surfce_heat_left_on, 0)
end
end

if get(antice_wing_h) > 0 and get(ENGN_running_r) > 0 then
set(ice_surfce_heat_right_on, 1)
else
if get(antice_wing_h) > 0 and get(antice_wngsource_r) > 0 and get(ENGN_running_r) < 1 then
set(ice_surfce_heat_right_on, 0)
end
end

if get(antice_wngsource_r) == 0 and get(ENGN_running_l) < 1 then
set(ice_surfce_heat_left_on, 0)
set(ice_surfce_heat_right_on, 0)
end
if get(antice_wngsource_r) == 2 and get(ENGN_running_r) < 1 then
set(ice_surfce_heat_left_on, 0)
set(ice_surfce_heat_right_on, 0)
end



if get(antice_wing_h) < 1 then
set(ice_surfce_heat_left_on, 0)
set(ice_surfce_heat_right_on, 0)
end


--]=]
--------





set(electr_apugen, get(electr_apugen_h)*annun_coeff)

set(electr_gen_r, (1-get(generator_on_r))*annun_coeff)
set(electr_gen_l, (1-get(generator_on_l))*annun_coeff)




set(electr_stbi, (1-get(electr_stbi_h))*annun_coeff)








set(hydr_altn_flp, get(hydr_altn_flp_h)*annun_coeff)
set(hydr_l_sov, (1-get(hydr_l_sov_h))*annun_coeff)
set(hydr_r_sov, (1-get(hydr_r_sov_h))*annun_coeff)

set(trim_pusher, (1-get(trim_pusher_h))*annun_coeff)


set(fire_engn_r, (get(fire_engn_r_h))*annun_coeff)
set(fire_engn_l, (get(fire_engn_l_h))*annun_coeff)
set(fire_apu, (get(fire_apu_h))*annun_coeff)
set(fire_ext_2, (get(fire_ext_2_h))*annun_coeff)
set(fire_ext_1, (get(fire_ext_1_h))*annun_coeff)
set(fire_autoapr, (1-get(fire_autoapr_h))*annun_coeff)

set(engn_ign, (get(engn_ign_h))*annun_coeff)

set(oxigen_terap, (get(oxigen_terap_h))*annun_coeff)

set(cab_ac, (1-get(cab_ac_h))*annun_coeff)
set(cab_dc, (1-get(cab_dc_h))*annun_coeff)

set(dcu_b, (1-get(dcu_b_h))*annun_coeff)
set(dcu_a, (1-get(dcu_a_h))*annun_coeff)

set(elt, (get(elt_h)))

set(comps_head_r, (get(comps_head_r_h))*annun_coeff)
set(comps_head_l, (get(comps_head_l_h))*annun_coeff)

set(aud_pa_l, (get(aud_pa_l_h)*elec_on))
set(aud_cab_l, (get(aud_cab_l_h)*elec_on))
set(aud_hf2_l, (get(aud_hf2_l_h)*elec_on))
set(aud_hf1_l, (get(aud_hf1_l_h)*elec_on))
set(aud_com3_l, (get(aud_com3_l_h)*elec_on))
set(aud_com2_l, (get(aud_com2_l_h)*elec_on))
set(aud_com1_l,(get(aud_com1_l_h)*elec_on))

set(aud_pa_r, (get(aud_pa_r_h)*elec_on))
set(aud_cab_r, (get(aud_cab_r_h)*elec_on))
set(aud_hf2_r, (get(aud_hf2_r_h)*elec_on))
set(aud_hf1_r, (get(aud_hf1_r_h)*elec_on))
set(aud_com3_r, (get(aud_com3_r_h)*elec_on))
set(aud_com2_r, (get(aud_com2_r_h)*elec_on))
set(aud_com1_r,(get(aud_com1_r_h)*elec_on))

set(engn_mach, (get(engn_mach_h))*annun_coeff)


-- aircond

if get(aircond_man_temp_h) > 0 then
set(aircond_tempr_cock_ddial, 40 + get(aircond_cockpit_temp)*30)
set(aircond_tempr_cab_ddial, 40 + get(aircond_cabin_temp)*30)
else
set(aircond_tempr_cock_ddial, 25 + get(aircond_cockpit_temp)*10)
set(aircond_tempr_cab_ddial, 25 + get(aircond_cabin_temp)*10)
end







-- to blank avionics when power is off

if get(elbus2) >=10 or get(electr_apugen_r) > 0 then
    if get(electr_ess_p_status) == 0 then
    set(avion_lit2, 1)
    set(cockavr, 1 - ((get(cmfd)*0.125) + (get(cpfd)*0.125) + (get(pmfd)*0.125) + (get(ppfd)*0.125)) )
    else
    set(avion_lit2, 0)
    set(cockavr, 0)
    end
else
set(avion_lit2, 0)
set(cockavr, 0)
end


if get(elbus3) >=10 and get(bat_on3) > 0 then
set(avion_lit3, 1)
else
set(avion_lit3, 0)
end

if get(rev_pan_l) == 1 then
if get(electr_ess_p_status) == 0 then set(cockavl_p, get(cockavl)) else set(cockavl_p, 0) end
set(cockavl_m, get(cockavl))
else
    if get(rev_pan_l) == 0 then
    if get(electr_ess_p_status) == 0 then set(cockavl_p, get(cockavl)) else set(cockavl_p, 0) end
    set(cockavl_m, 0)
    else
    set(cockavl_p, 0)
    set(cockavl_m, get(cockavl))
    end
end

if get(rev_pan_r) == 1 then
set(cockavr_p, get(cockavr))
set(cockavr_m, get(cockavr))
else
    if get(rev_pan_r) == 0 then
    set(cockavr_p, get(cockavr))
    set(cockavr_m, 0)
    else
    set(cockavr_p, 0)
    set(cockavr_m, get(cockavr))
    end
end



if get(landlight2_h) == 1 or get(landlight1_h) == 1 then
set(landlights, 1)
set(landlights_1, 1)
set(landlights_2, 1)
end
if get(landlight2_h) == 0 and get(landlight1_h) == 0 then
set(landlights, 0)
set(landlights_1, 0)
set(landlights_2, 0)
end


-- left electric bus


if get(elbus1) >=10 then

    set(gshldl, get(gshldl_h))
    set(pedestal, get(pedestal_h))
    set(ppfd, 1-get(ppfd_h))
    set(pmfd, 1-get(pmfd_h))
    set(wing_insp, get(wing_insp_h)*night_coeff)
    set(wing_insp_b, get(wing_insp_h))
    set(beac_brt, get(beac_brt_rat)*night_coeff)
    set(entry_lit, get(entrlight_h))

    set(crewlit, get(crewlit_h)*night_coeff2)
    set(crewlit2, get(crewlit2_h)*night_coeff2)

   if get(rembf10) == 1 and get(nws_h) == 1 and get(hydr_pres_1) > 1600 then -- JackZero: Reduced from 2100 psi
    set(acf_nw_steerdeg1, 35 + 30*(1-math.min(20, math.max(0, get(nosewheelspd)))/20))
    set(acf_nw_steerdeg2, 5)
    else
    set(acf_nw_steerdeg1, 0.1)
    set(acf_nw_steerdeg2, 0.1)
    end




        if get(strobe_h) == 2 then
        set(beaconlights, 1)
--        set(stroblights, 1)
        end
        if get(strobe_h) == 1 then
        set(beaconlights, 1)
--        set(stroblights, 0)
        end
        if get(strobe_h) == 0 then
        set(beaconlights, 0)
--        set(stroblights, 0)
        end

        set(avion_lit1, 1)
        set(cockavl, 1 - ((get(cmfd)*0.125) + (get(cpfd)*0.125) + (get(pmfd)*0.125) + (get(ppfd)*0.125)) )

-- left landing lights
        if get(landlight1_h) == 1 then
        --set(landlights, 1)
        set(landlights_1, 1)
        set(landlight1, 1)
        --set(landlight1, get(landlights_rat))
        else
        set(landlight1, 0)
        end




else

set(beac_brt, 0)
set(gshldl, 0)
set(pedestal, 0)
set(ppfd, 0)
set(pmfd, 0)
set(cockavl, 0)
set(wing_insp, 0)
set(wing_insp_b, 0)

set(beaconlights, 0)

set(cabin_lit, 0)
set(entrlight, 0)
set(entry_lit, 0)
set(crewlit, 0)
set(landlight1, 0)

set(avion_lit1, 0)

set(acf_nw_steerdeg1, 0.1)
set(acf_nw_steerdeg2, 0.1)

end


-- right electric bus
if get(elbus2) >=10 or get(electr_apugen_r) > 0 then

    set(cabin_lit, get(cablight_h))
    set(dome, get(dome_h)*night_coeff2*2)
    set(cbp, get(cbp_h))
    set(cpfd, 1-get(cpfd_h))
    set(cmfd, 1-get(cmfd_h))
    set(gshldr, get(gshldr_h))

    if get(strobe_h) == 2 then
        set(stroblights, 1)
    end
    if get(strobe_h) == 1 then
    set(stroblights, 0)
    end
    if get(strobe_h) == 0 then
    set(stroblights, 0)
    end

        if get(nav_h) == 2 then
        set(logo, night_coeff)
        set(navlights, 1)
        end
        if get(nav_h) == 1 then
        set(logo, 0)
        set(navlights, 1)
        end
        if get(nav_h) == 0 then
        set(logo, 0)
        set(navlights, 0)
        end

    -- taxilight
        if get(xap_taxilight_h) > 0 and get(gear_dep_0) > 0.6 then
        set(xap_taxilight2, 1)
        set(taxi, 1)
            if get(xap_taxilight_h) > 1 then
            set(xap_taxilight, 1)
            else
            set(xap_taxilight, 0)
            end
        else
        set(taxi, 0)
        set(xap_taxilight, 0)
        set(xap_taxilight2, 0)
        end

        if get(gear_dep_0) > 0.6 and get(xap_taxilight) == 2 then
        set(xap_taxilight2, 1)
        set(taxi, 1)
        elseif get(gear_dep_0) <= 0.6 then set(xap_taxilight2, 0)
        end
-- right landing light
        if get(landlight2_h) == 1 then
        --set(landlights, 1)
        --set(landlights_2, 1)
        --set(landlight2, get(landlights_rat))
        set(landlight2, 1)
        else
        set(landlight2, 0)
        end

else
set(stroblights, 0)
set(dome, 0)
set(cbp, 0)
set(gshldr, 0)
set(cpfd, 0)
set(cmfd, 0)
set(xap_taxilight, 0)
set(xap_taxilight2, 0)
set(landlight2, 0)
set(logo, 0)
set(navlights, 0)
set(cablight, 0)
end

--[=[f get(landlight1_h) == 1 and get(landlight2_h) == 1 then
set(acf_lanlite_the, -9)
elseif get(landlight1_h) == 1 or get(landlight2_h) == 1 then set(acf_lanlite_the, -9.5)
else
if get(xap_taxilight_h) < 2 then
set(acf_lanlite_the, -7)
else
set(acf_lanlite_the, -5.5)
end
end--]=]


--disabling eicas 3d
if get(avion_lit1) == 0 and get(avion_lit2) == 0 then set(eicas_3d_p,0) end





-- stby el logic
if get(elbus3) >=10 and get(bat_on3) > 0 and get(avion_lit3) ~= 0 then
set(stby_ins_lit, 0.5*get(stby_ins_dim_brt))
else
if get(elbus3) < 10 or get(avion_lit3) == 0 then set(stby_ins_lit, 1) end
end


if get(landlight1_h) == 0 then
set(landlights_1, 0)
end

if get(landlight2_h) == 0 then
--set(landlights_2, 0)
--set(landlights, 0)
end

-- emer lightning

if get(emeright_h) == 1 then
   if get(elbus2) <=10 then
   set(emeright, 1)
   else
   set(emeright, 0)
   end
end

if get(emeright_h) == 2 then
set(emeright, 1)
end

if get(emeright_h) == 0 then
set(emeright, 0)
end

-- sunscreen
 if get(view) == 0 then
    if get(sunscreen11) > 0.5 or get(sunscreen21) > 0.5 then
     set(sunglases, 1)
     elseif get(sunscreen11) <= 0.5 and get(sunscreen21) <= 0.5 then set(sunglases, 0)
 end
else
set(sunglases, 0)
end




--//Emergency door light logic



if get(emer_door_open) > 0.001 and get(salon_lit) > 0.2 then
    set(emer_door_lit, get(emer_door_open))
else
    set(emer_door_lit, 0)
end

--//Cabin signs
if get(smokebelts_h) == 1 and get(elbus1) >= 10 then
   set(belt, 1)
   set(smoke, 0)
   else
       if get(smokebelts_h) == 2 and get(elbus1) >= 10 then
       set(smoke, 1)
       set(belt, 1)
       else
       if get(smokebelts_h) == 0 or get(elbus1) < 10 then
       set(smoke, 0)
       set(belt, 0)
       end
   end
end

-- depress when on ground



-- depress with opened doors
if get(customslider2_r) > 0.1  or get(customslider4_r) > 0.1 or get(customslider3_r) > 0.1 then
set(dump_all, 1)
set(rel_depres_fast, 6)
else
    if last_states_r[2] -get(customslider2_r) ~= 0 or last_states_r[3] -get(customslider3_r) ~= 0 or last_states_r[4] -get(customslider4_r) ~= 0 then
        if get(customslider2_r) == 0 and get(customslider4_r) == 0 and get(customslider3_r) == 0 then
        set(dump_all, 0)
        set(rel_depres_fast, 0)
        end
    end
end



-- automatic pressurization
-- set(barometer_setting_in_hg_copilot, 29.92) -- JackZero: Removed... it doesn't make sense to force it to 29.92!

if get(pressure_man_h) < 1 then
set(cabin_vvi_fpm, 650)
    if get(onground_all) == 1 then set(dump_all, 1) end
    if last_states[24] - get (onground_all) ~= 0 and get(onground_all) == 0 then set(dump_all, 0) end
set(cabin_altitude_ft, 2.7858*(get(altitude_ft_copilot)/1000)*(get(altitude_ft_copilot)/1000)+53.968*(get(altitude_ft_copilot)/1000)-219.85)
else
    if get(customslider2_r) == 0 and get(customslider4_r) == 0 and get(customslider3_r) == 0 and get(pressure_emer_depr_h) == 0 and get(pressure_ditch_h) == 0 then
    set(dump_all, 0)
    end

set(cabin_vvi_fpm, math.abs(get(pressure_manrate)*get(pressure_manrate))*2500)
if get(pressure_manrate) < -0.14 then
set(cabin_altitude_ft, 0)
elseif get(pressure_manrate) > 0.14 then set(cabin_altitude_ft, 7850)
--(1+get(pressure_manrate))*(1+get(pressure_manrate))*4102-350)
end

end

if get(pressure_manrate) < 0.14 and get(pressure_manrate) > -0.14 then
set(pressure_manrate,0)
end

if get(pressure_emer_depr_h) == 1 then
set(dump_all, 1)
end

if get(pressure_ditch_h) == 1 then
set(dump_all, 1)
end


-- blocking doors with pressure
if get(pressure_diffential_psi) > 0.5 then
if get(customslider2_r) < 0.1 then
set(customslider2, 0)
set(customslider2_r, 0)
end
if get(customslider3_r) < 0.1 then
set(customslider3, 0)
set(customslider3_r, 0)
end
if get(customslider4_r) < 0.1 then
set(customslider4, 0)
set(customslider4_r, 0)

end
end


-- systems test
--systest_last = {get(test_rot),get(test_push)}




if get(test_rot) == 1 and  get(test_push) == 1 then
counter_1 = counter_1 + 0.05*frame_factor
else
    if get(test_rot) ~= 1 then
    counter_1 = 0
    end
end

if get(test_rot) == 2 and  get(test_push) == 1 then
counter_3 = counter_3 + 0.05*frame_factor
else
    if get(test_rot) ~= 2 then
    counter_3 = 0
    end
end

if counter_1 > 5 then counter_2 = counter_2 + 0.05*frame_factor end
if counter_3 > 5 then counter_4 = counter_4 + 0.05*frame_factor end

if counter_2 > 20 or counter_1 == 0 then
counter_1 = 0
counter_2 = 0
end


if counter_4 > 20 or counter_3 == 0 then
counter_3 = 0
counter_4 = 0
end

set(annun1_aur, counter_2)
set(annun2_aur, counter_4)

if counter_2 > 0 or counter_4 > 0 then
set(bleed_apu, annun_coeff)
set(bleed_en_l, annun_coeff)
set(bleed_en_r, annun_coeff)
set(bleed_xbleed, annun_coeff)
set(annun, annun_coeff)
set(entrlight, annun_coeff)
set(cablight, annun_coeff)
set(right_bat, annun_coeff)
set(left_bat, annun_coeff)
set(bus_tie, annun_coeff)
set(flight_spoil, annun_coeff)
set(fuel_xflow_up, annun_coeff)
set(fuel_xflow_dn, annun_coeff)
set(nws, annun_coeff)
set(taws_ter, annun_coeff)
set(taws_flp, annun_coeff)
set(taws_gs, annun_coeff)
set(aircond_man_temp, annun_coeff)
set(aircond_ramair, annun_coeff)
set(pressure_man, annun_coeff)
set(pressure_ditch, annun_coeff)
set(pressure_emer_depr, annun_coeff)
set(antice_wshld_r, annun_coeff)
set(antice_wshld_l, annun_coeff)
set(antice_probe_r, annun_coeff)
set(antice_probe_l, annun_coeff)
set(antice_engn_r, annun_coeff)
set(antice_engn_l, annun_coeff)
set(antice_wing, annun_coeff)
set(electr_apugen, annun_coeff)
set(electr_gen_r, annun_coeff)
set(electr_gen_l, annun_coeff)
set(electr_stbi, annun_coeff)
set(hydr_altn_flp, annun_coeff)
set(hydr_l_sov, annun_coeff)
set(hydr_r_sov, annun_coeff)
set(trim_pusher, annun_coeff)
set(fire_engn_r, annun_coeff)
set(fire_engn_l, annun_coeff)
set(fire_apu, annun_coeff)
set(fire_ext_2, annun_coeff)
set(fire_ext_1, annun_coeff)
set(fire_autoapr, annun_coeff)
set(engn_ign, annun_coeff)
set(oxigen_terap, annun_coeff)
set(cab_ac, annun_coeff)
set(cab_dc, annun_coeff)
set(dcu_b, annun_coeff)
set(dcu_a, annun_coeff)
set(comps_head_r, annun_coeff)
set(comps_head_l, annun_coeff)
set(engn_mach, annun_coeff)

set(mast_warn, 1)
set(mast_caut, 1)


end








-- probe det
if get(test_rot) == 3 and  get(test_push) == 1 then
counter_5 = counter_5 + 0.05*frame_factor
else
    if get(test_rot) ~= 3 then
    counter_5 = 0
    end
end

if counter_5 > 5 then counter_6 = counter_6 + 0.05*frame_factor end

if counter_6 > 18 or counter_5 == 0 then
counter_5 = 0
counter_6 = 0
end

set(probs_test, 0)
if counter_6 > 7 then
set(probs_test, 1)
end



-- ice det
if get(test_rot) == 5 and  get(test_push) == 1 then
counter_13 = counter_13 + 0.05*frame_factor
else
    if get(test_rot) ~= 5 then
    counter_13 = 0
    end
end

if counter_13 > 5 then counter_14 = counter_14 + 0.05*frame_factor end

if counter_14 > 18 or counter_13 == 0 then
counter_13 = 0
counter_14 = 0
end

set(ice_test, 0)
if counter_14 > 7 then
set(ice_test, 1)
end





--fire det test
if get(test_rot) == 6 and  get(test_push) == 1 then
counter_7 = counter_7 + 0.05*frame_factor
else
    if get(test_rot) ~= 6 then
    counter_7 = 0
    end
end

if counter_7 > 5 then counter_8 = counter_8 + 0.05*frame_factor end

if counter_8 > 10 or counter_7 == 0 then
counter_7 = 0
counter_8 = 0
end


set(fire_test_aur, counter_8)

--taws test
if get(test_rot) == 7 and  get(test_push) == 1 then
counter_9 = counter_9 + 0.05*frame_factor
else
    if get(test_rot) ~= 7 then
    counter_9 = 0
    end
end

if counter_9 > 5 then counter_10 = counter_10 + 0.05*frame_factor end

if counter_10 > 12 or counter_9 == 0 then
counter_9 = 0
counter_10 = 0
end


set(taws_test_aur, counter_10)


--rudd lim test
if get(test_rot) == 4 and  get(test_push) == 1 then
counter_11 = counter_11 + 0.05*frame_factor
else
    if get(test_rot) ~= 4 then
    counter_11 = 0
    end
end

if counter_11 > 5 then counter_12 = counter_12 + 0.05*frame_factor end

if counter_12 > 10 or counter_11 == 0 then
counter_11 = 0
counter_12 = 0
end

set(rudlim_test, 0)
if counter_12 > 0 then
set(rudlim_test, 1)
end

set(stall_warn_test, 0)
if counter_12 > 8 and counter_12 < 9 then
set(stall_warn_test, 1)
end


--voice rec test
if elec_on == 1 then
    if get(voice_rec_test) == 1 then
    counter_15 = counter_15 + 0.05*frame_factor
    else
    --counter_15 = 0
    end

    if counter_15 > 5 then counter_16 = counter_16 + 0.05*frame_factor end

    if counter_16 > 7 then
    counter_15 = 0
    counter_16 = 0
    end

else
counter_15 = 0
counter_16 = 0
end

if counter_16 > 0 then
set(voice_rec_test_lt, 1)
else
set(voice_rec_test_lt, 0)
end

if get(crc_butt) == 1 then
    if get(HSI_source_select_pilot) == 0 then
        if get(nav1_display_horizontal) ~= 0 then set(hsi_obs_deg_mag_pilot, get(nav1_bearing_deg_mag)) else set(hsi_obs_deg_mag_pilot, get(heading_AHARS_deg_mag_pilot)) end
    else
        if get(HSI_source_select_pilot) == 1 then
            if get(nav2_display_horizontal) ~= 0 then set(hsi_obs_deg_mag_pilot, get(nav2_bearing_deg_mag)) else set(hsi_obs_deg_mag_pilot, get(heading_AHARS_deg_mag_pilot)) end
        else
            if get(HSI_source_select_pilot) == 2 then
                if get(destination_type) ~= 0 then set(hsi_obs_deg_mag_pilot, get(gps_bearing_deg_mag)) else set(hsi_obs_deg_mag_pilot, get(heading_AHARS_deg_mag_pilot)) end
            else
            set(hsi_obs_deg_mag_pilot, get(heading_AHARS_deg_mag_pilot))
            end
        end
    end
end


--

-- landing gear warn

if get(onground_all) == 0 and get(gear_handle_status) == 0 and get(flaprqst) > 0.67 then
set(lan_gear_warn, 1)
else
set(lan_gear_warn, 0)
end


-- JackZero: Overhauled AutoGen code
-- auto gens, disable/enable gens on engine start/stop or gpu disconnect
if get(gpu_on) == 0 then
    if electric_last[2] ~= get(ENGN_running_l) or electric_last[1] > 0 then
        set(electr_gen_l_h, get(ENGN_running_l))
    end
    if electric_last[3] ~= get(ENGN_running_r) or electric_last[1] > 0 then
        set(electr_gen_r_h, get(ENGN_running_r))
    end
    if electric_last[4] ~= get(APU_running) or electric_last[1] > 0 then
        set(electr_apugen_h, get(APU_running))
    end
end
-- JackZero: End

if get(gust_lock) == 1 then
set(rel_fc_ail_L, 6)
set(rel_fc_ail_R, 6)
else
set(rel_fc_ail_L, 0)
set(rel_fc_ail_R, 0)
end

-- JackZero:
-- Set displayed waypoint:
max_entries = countFMSEntries()
show_entry  = get(flgtplan_show_entry)
if last_entry ~= show_entry then
	setDisplayedFMSEntry(show_entry)
	last_entry = show_entry
end


-- Set the fms landing alt:
if get(pressure_lndg_alt_1) ~= 0 then
	if ldg_alt_i == 0 then	-- process only every N frames
		for i = max_entries,0,-1 do
			local wtype, wid, wref, walt, wlat, wlong = getFMSEntryInfo(i)
			if wtype == NAV_AIRPORT then
				if wid ~= dest_arpt then
					dest_arpt = wid
					local ref = findNavAid(nil, wid, nil, nil, nil, NAV_AIRPORT)
					local typ, lat, lon, alt, freq, hdg, id, name, reg = getNavAidInfo(ref)
					set(pressure_lndg_alt_2, math.floor((alt * 3.28084 / 5000 - 1) * 100 + .5)/100)
				end
				break
			end
		end
		ldg_alt_i = -50
	else
		ldg_alt_i = ldg_alt_i + 1
	end
else
	ldg_alt_i = 0
end
-- JackZero: End

set(disp, get(gust_lock_anm))
set(disp2, get(gust_lock))
last_states = {get(customslider1), get(customslider2), get(customslider3), get(customslider4), get(rembf10),get(rembf11),get(rembf12), get(rembf2), get(rembf7), get(rembf1), get(rembf6), get(rembf4),get(rembf8),get(prop_mode_0),get(prop_mode_1), get(sbrk_h), gr_sp_deployed,get(en_starter_l),get(en_starter_r),get(clock_timer_h),get(engn_mach_status),get(ENGN_thro_0),get(engn_mach_h),get(onground_all), get(pressure_manrate), get(dcp_range),get(rembf5),get(rembf13),get(rembf14),get(speedbrake_ratio)} -- JackZero: Added speedbrake_ratio
--                 1                   2                   3                   4                   5            6            7             8            9            10           11           12          13          14               15                16       17                 18                19                20                 21                     22              23               24                 25                     26             27          28           29
coms_last = {get(aud_com1_r_h),get(aud_com2_r_h),get(aud_com3_r_h),get(aud_com1_l_h),get(aud_com2_l_h),get(aud_com3_l_h),get(autop_brgsrc),get(dcp_navsrc),get(dcp_refs),get(mfd_sel_state), get(mfd_sel_state_h)}
--                        1                2                    3                4                    5                6                7                    8            9                10                        11
refspds_last = {get(refspds_v1),get(refspds_vr),get(refspds_v2),get(refspds_vt),get(refspds_vga),get(refspds_vref),get(rev_air_dat)}
--                        1                2                3                4                5                    6                7
caution_last = {get(red_cas),get(yellow_cas)}
--
systest_last = {get(test_rot),get(test_push)}
--
starter_last = {get(starter_duration_l),get(starter_duration_r)}

running_last = {get(ENGN_running_l), get(ENGN_running_r), get(APU_running)} -- JackZero: New

-- JackZero: Moved here from pfd.lua, changed to now just store the argument
bank_alt_last = get(altitude_ft_pilot) > 31600
--
-- JackZero: See above
electric_last = {get(gpu_on),get(ENGN_running_l),get(ENGN_running_r),get(APU_running),get(dcp_menu1_rot)}
-- JackZero: End
-- JackZero: Linking rudder trim to trim switch:
ruddertrim_last = {get(rudder_trim),trim_rud_tmp}
-- JackZero: End
end
