L_N2_speed = find_dataref("sim/cockpit2/engine/indicators/N2_percent[0]") -- Left N2 speed in %
R_N2_speed = find_dataref("sim/cockpit2/engine/indicators/N2_percent[1]") -- Right N2 speed in %
fail = find_dataref("sim/operation/failures/rel_engfai0")
fire = find_dataref("sim/operation/failures/rel_engfir0")
fail2 = find_dataref("sim/operation/failures/rel_engfai1")
fire2 = find_dataref("sim/operation/failures/rel_engfir1")
enghealth_l = find_dataref("sim/aircraft2/engine/engine_health[0]")
enghealth_r = find_dataref("sim/aircraft2/engine/engine_health[1]")

l_degredation = 0
r_degredation = 0
l_new_health = 100
r_new_health = 100
rand_l = math.random(80,120) / 100
rand_r = math.random(80,120) / 100
faill = rand_l * 0.25
failr = rand_r * 0.25

	
function degrade_engine()
		rand_l = math.random(80,120) / 100
		rand_r = math.random(80,120) / 100
		--print("Random Left:" .. rand_l .. " Random Right:" .. rand_r)
		if L_N2_speed > 100.8 then   -- if % greater than 100
			l_degredation = ((L_N2_speed - 100.8) / 2000) * rand_l -- calculate how much to degrade health
			l_new_health = enghealth_l - l_degredation
			enghealth_l = l_new_health -- degrade health
			if enghealth_l < 0.25 then   -- what happens if engine health is less than 25%
				print("Failed left Engine! Health: " .. enghealth_l)
				fail = 6  -- engine stops running    
			end
		end
	   
		if R_N2_speed > 100.8 then   -- if % greater than 100
			r_degredation = ((R_N2_speed - 100.8) / 2000) * rand_r -- calculate how much to degrade health
			r_new_health = enghealth_r - r_degredation
			enghealth_r = r_new_health -- degrade health
			if enghealth_r < 0.25 then   -- what happens if engine health is less than 25%
				print("Failed Right Engine! Health: " .. enghealth_r)
				fail2 = 6  -- engine stops running    
			end
		end
end

run_at_interval(degrade_engine, 0.25)