-- define property table
defineProperty("eng1_rpm_rads", globalPropertyf("sim/flightmodel2/engines/engine_rotation_speed_rad_sec[0]"))  -- 1st engine rotation rad/sec
defineProperty("eng2_rpm_rads", globalPropertyf("sim/flightmodel2/engines/engine_rotation_speed_rad_sec[1]"))  -- 2nd engine rotation rad/sec
defineProperty("first_eng_fail", globalPropertyf("sim/operation/failures/rel_engfai0"))  -- fail of first engine
defineProperty("first_eng_fire", globalPropertyf("sim/operation/failures/rel_engfir0"))  -- fire of first engine

defineProperty("flag_deg", globalPropertyf("sim/custom/xap/flag_deg"))  -- flag position
defineProperty("switch_2", globalPropertyi("sim/cockpit2/switches/custom_slider_on[1]"))
defineProperty("wind_dir", globalPropertyf("sim/weather/wind_direction_degt"))  -- wind direction
defineProperty("wind_speed", globalPropertyf("sim/weather/wind_speed_kt"))  -- wind speed
defineProperty("true_hdg", globalPropertyf("sim/flightmodel/position/magpsi"))  -- mag heading
defineProperty("sideslip", globalPropertyf("sim/cockpit2/gauges/indicators/sideslip_degrees"))  -- side slip

defineProperty("wingretract", globalPropertyf("sim/cockpit2/controls/wing_retraction_ratio"))

defineProperty("flight_time", globalPropertyf("sim/time/total_flight_time_sec"))  -- local time since aircraft was loaded 
defineProperty("ground_speed", globalPropertyf("sim/flightmodel/position/groundspeed")) -- true airspeed
defineProperty("speedbrake", globalPropertyf("sim/cockpit2/controls/speedbrake_ratio")) -- speedbrake position
defineProperty("engn_1_oil_temp", globalPropertyf("sim/flightmodel/engine/ENGN_oil_temp_c[0]")) -- engine 1 oil temp
defineProperty("engn_2_oil_temp", globalPropertyf("sim/flightmodel/engine/ENGN_oil_temp_c[1]")) -- engine 2 oil temp
defineProperty("engn_1_CHT", globalPropertyf("sim/flightmodel/engine/ENGN_CHT_c[0]")) -- engine 1 cylinder head temp
defineProperty("engn_2_CHT", globalPropertyf("sim/flightmodel/engine/ENGN_CHT_c[1]")) -- engine 2 cylinder head temp
defineProperty("engn_1_oil_press", globalPropertyf("sim/flightmodel/engine/ENGN_oil_press_psi[0]")) -- engine 1 oil press
defineProperty("engn_2_oil_press", globalPropertyf("sim/flightmodel/engine/ENGN_oil_press_psi[1]")) -- engine 2 oil press
defineProperty("battery_on", globalPropertyf("sim/cockpit/electrical/battery_on")) -- baterry
defineProperty("generator_on", globalPropertyf("sim/cockpit/electrical/generator_on[0]")) -- generator
defineProperty("aileron_trim", globalPropertyf("sim/cockpit2/controls/aileron_trim")) -- aileron trim
defineProperty("bus_1_volt", globalPropertyf("sim/cockpit2/electrical/bus_volts[0]")) -- bus 1 voltage
defineProperty("chatter", globalPropertyi("sim/operation/sound/radio_chatter_on")) -- radio chatter on/off
defineProperty("markers", globalPropertyi("sim/cockpit2/radios/actuators/audio_marker_enabled")) -- audio markers on/off
defineProperty("ralt_height", globalPropertyf("sim/cockpit2/gauges/indicators/radio_altimeter_height_ft_pilot"))

defineProperty("local_time", globalPropertyf("sim/time/local_time_sec")) -- local time
defineProperty("local_time_sec", globalPropertyf("sim/cockpit2/clock_timer/local_time_seconds")) -- local time sec
defineProperty("avionics_power", globalPropertyf("sim/cockpit2/switches/avionics_power_on")) -- avionics power switch 


defineProperty("prop_hide", globalPropertyi("sim/custom/xap/prop_broke")) -- propeller hide


defineProperty("reverse", globalPropertyi("sim/flightmodel/engine/ENGN_propmode[0]")) -- reverse
defineProperty("driveshaft", globalPropertyi("sim/operation/failures/rel_pshaft0")) -- drive shaft failure

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("wave_height", globalPropertyf("sim/weather/wave_amplitude")) -- amplitude of waves
defineProperty("wave_limit_n", globalPropertyi("sim/custom/xap/wave_limit_n")) -- wave limit value

starter_1 = findCommand("sim/starters/engage_starter_1")  -- simulator command for starter 1
starter_2 = findCommand("sim/starters/engage_starter_2")  -- simulator command for starter 2
white_smoke = findCommand("sim/flight_controls/smoke_toggle")



defineProperty("gear_1_vert_force", globalPropertyi("sim/flightmodel2/gear/tire_vertical_force_n_mtr[0]"))
defineProperty("gear_2_vert_force", globalPropertyi("sim/flightmodel2/gear/tire_vertical_force_n_mtr[1]"))
defineProperty("gears_status", globalPropertyi("sim/cockpit/switches/gear_handle_status"))

defineProperty("tire_1_blowout", globalPropertyi("sim/operation/failures/rel_tire1"))
defineProperty("tire_2_blowout", globalPropertyi("sim/operation/failures/rel_tire2"))

defineProperty("autobrake", globalPropertyi("sim/cockpit/switches/auto_brake_settings"))


defineProperty("camera_type", globalPropertyi("sim/graphics/view/view_type"))
defineProperty("flaps_pos", globalPropertyf("sim/flightmodel/controls/flaprat")) -- flaps position
defineProperty("l_flap_state", globalPropertyi("sim/operation/failures/rel_L_flp_off")) -- left flap state
defineProperty("r_flap_state", globalPropertyi("sim/operation/failures/rel_R_flp_off")) -- left flap state

defineProperty("white_smoke_d", globalPropertyi("sim/flightmodel/failures/smoking")) -- white smoke

defineProperty("hd_position", globalPropertyf("sim/flightmodel/position/R")) -- heading

defineProperty("fuel_sel", globalPropertyi("sim/cockpit/engine/fuel_tank_selector")) -- fuel_selector
defineProperty("startup_running", globalPropertyi("sim/operation/prefs/startup_running")) -- start each flight with engines running

defineProperty("fov", globalPropertyf("sim/graphics/view/field_of_view_deg")) -- field of view
defineProperty("park_brake", globalPropertyf("sim/flightmodel/controls/parkbrake")) -- field of view

left_brake = findCommand("sim/flight_controls/left_brake")
right_brake = findCommand("sim/flight_controls/right_brake")

local time_last = get(flight_time)
local passed = get(flight_time) - time_last  -- time of each frame
   
local rad_sec = 9.5493    -- rpm
local rpm_counter_1 = 0
local rpm_counter_2 = 0
local first_eng_last = false
local first_eng_must_fail = false

local rpm_1_fail = false
local rpm_2_fail = false
local last_wings = get(switch_2)

local brake_time = 0

set(first_eng_fail, 0)
set(first_eng_fire, 0)
set(camera_type, 1026)
set(fuel_sel, 4)
set(white_smoke_d, 0)
set(wave_limit_n, 1)
set(markers, 0)
set(chatter, 0)
set(autobrake, 0)


-- Smoke with starters

 function starterHandler(phase)
     if 0 == phase then
        -- command start
	 set(white_smoke_d, 0)
         commandOnce(white_smoke);
     elseif 2 == phase then
 	commandOnce(white_smoke);
	set(white_smoke_d, 0)
        -- command end
     end
     return 1
 end

registerCommandHandler(starter_1, 1, starterHandler);



-- -------------


function update()    -- all calculations causes every frame

set(battery_on, 1)

-- wings retraction
if get(ground_speed) > 3 then
      set (switch_2, last_wings)
end
set(wingretract, get(switch_2))

-- flag position

local wspeed = get(ground_speed)
if wspeed > 1 then wspeed = 1 end

local windsp_coeff = get(wind_speed)

if windsp_coeff > 1 then windsp_coeff = 1 end

r_ang=(get(wind_dir)-get(true_hdg))
loc_flag = r_ang - (r_ang*wspeed) 
set(flag_deg, get(sideslip)+ (loc_flag*windsp_coeff))

-- Heading with anchor
if get(flight_time) > 1 then
if get(ralt_height) < 10 then
if get(ground_speed) < 3 then
	if get(park_brake) > 0.9 and get(ground_speed) < 0.7 then
		set(hd_position, 0)
		set(velocity_vx, 0)
		set(velocity_vy, 0)
		set(velocity_vz, 0)
	end
    end
end
end


-- Disabling aileron trim
-- set(aileron_trim, 0)


-- Set wave height

if get(wave_limit_n) > 0 then
	if get(wave_height) > 0.115 then
		set(wave_height, 0.115)
	end
end


-- Tires blowouts

    if get(flight_time) > 3 then
	if get(gear_1_vert_force) > 16000 and get(gear_1_vert_force) < 19000 then 
		if get(gears_status) == 1 then
			set (tire_1_blowout, 6)
		end
	end

		if get(gear_2_vert_force) > 16000 and get(gear_1_vert_force) < 19000 then 
			if get(gears_status) == 1 then
			set (tire_2_blowout, 6)
		end
	end
    end





     -- time count between now and last frame
   passed = get(flight_time) - time_last

   -- engines limits failures
   -- RPM limit. 1650 RMP max 60 sec
   if get(eng1_rpm_rads) * rad_sec > 1655 then rpm_counter_1 = rpm_counter_1 + passed
   else rpm_counter_1 = rpm_counter_1 - passed
   end  

-- brakes when gears up
if get(gears_status) == 0 then
	brake_time = brake_time + passed
	if brake_time < 5 then
	commandOnce(left_brake, 0.4)
	commandOnce(right_brake, 0.4) 
	else brake_time = brake_time - passed
	end
	else brake_time = 0
end   

   if rpm_counter_1 > 60 then
      rpm_counter_1 = 60
	  rpm_1_fail = true
   end


   if rpm_counter_1 < 0 then 
      rpm_counter_1 = 0
	  rpm_1_fail = false
   end


   -- final engine failure logic
   -- calculate failure
   if rpm_1_fail then first_eng_must_fail = true
   else first_eng_must_fail = false
   end


      -- set fail if it must fail or set work if all parameters are fine
      if get(flight_time) > 3 then
	  if first_eng_must_fail ~= first_eng_last then
	    if first_eng_must_fail then 
	      set(first_eng_fail, 6)
	      set(first_eng_fire, 6)
            end
	  end
      end



if get(eng1_rpm_rads) * rad_sec > 1500 then
      if get(flight_time) > 3 then
	  if get(engn_1_oil_temp) > 100 or get(engn_1_CHT) > 363 or get(engn_1_oil_press)< 55 or get(engn_1_oil_press)> 75 then
	      set(first_eng_fail, 6)
	      set(first_eng_fire, 6)
	      local engn_temp = get(engn_1_oil_temp)
	      local CHT = get(engn_1_CHT)
	      local oil_pres = get(engn_1_oil_press)
	      print("Oil temp = ", engn_temp)
	      print("CHT = ", CHT)
	      print("Oil pres = ", oil_pres)
	  end
      end
end



 

   -- last variables

   time_last = get(flight_time)
   first_eng_last = first_eng_must_fail
   last_wings = get(switch_2)
end