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


	
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) / 1000) * rand_l -- calculate how much to degrade health
			--print("Left Deg: " .. l_degredation)
			l_new_health = enghealth_l - l_degredation
			enghealth_l = l_new_health -- degrade health
			print("newhl:" .. l_new_health .. "enghl:" .. enghealth_l)
			if enghealth_l < 25.0 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) / 1000) * rand_r -- calculate how much to degrade health
			--print("Right Deg: " .. r_degredation)
			r_new_health = enghealth_r - r_degredation
			print("newhr:" .. r_new_health .. "enghr:" .. enghealth_r)
			enghealth_r = r_new_health -- degrade health
			if enghealth_r < 25.0 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)