-- =======================================================================================================================
-- Скрипт: PAX 4.4 (Passenger Announcements for X-Plane)
-- Описание: В скрипте реализовано озвучивание информационных сообщений старшего бортпроводника для самолетов:
-- - Flight Factor A320 Ultimate (BSS SoundPack)
-- - Toliss A319
-- - Zibo B737-800
-- - Flight Factor B777 (BSS SoundPack)
-- - Flight Factor B757-200
-- - Flight Factor B767-300
-- Авиакомпании: AFL (АК Аэрофлот), SDM (АК Россия)

-- Дата: 01.04.2019
-- Автор: Алексей Сидоров
-- alexey.sidorov@mail.ru
-- https://t.me/Lehis
-- https://vc.com/alexey.sidorov

-- Включение плагина происходит при открытии передней левой двери и включении переключателя Застегните ремни в положение On

-- =======================================================================================================================
if PLANE_ICAO == "A320" or PLANE_ICAO == "B738" or PLANE_ICAO == "A319" or string.find(PLANE_ICAO, "B75") or string.find(PLANE_ICAO, "B76") or string.find(PLANE_ICAO, "B77") then
  
	--Настройки ========================================================================================================================
	--local company_icao = "AFL" --Аэрофлот
	local company_icao = "SDM" --Россия

	local auto = 1 -- Способ обработки некоторых триггеров: 1 - все триггеры отрабатываются автоматически, 0 - триггеры в наборе, перед взлетом и при посадке воспроизводятся после только чаймов 

	local volume = 4 --Начальная громкость (от 1 до 5)

	--Множители уровня громкости
	local a319_gain = 0.7 --A319
	local a320_gain = 1.1 --A320
	local b738_gain = 0.9 --B738
	local b757_gain = 0.7 --B757
	local b767_gain = 0.6 --B767
	local b777_gain = 1 --B777
	
	local afl_gain = 1 --AFL
	local sdm_gain = 1 --SDM

	local view_int_gain = 1 --Вид внутри самолета
	local view_ext_gain = 0.3 --Внешний вид

	local engine_on_gain = 0.7 --Двигатели включены
	local engine_off_gain = 0.5 --Двигатели выключены

	local on_air_gain = 0.8 --В воздухе
	local on_ground_gain = 0.6 --На земле

	local in_cockpit_gain = 1 --В кабине
	local in_cabin_gain = 2 --В салоне

	local cameraz = -12 --граница между кабиной и салоном

	--Макросы и команды FluWithLua ========================================================================================================================
	add_macro("PAX: disable for this flight", "disable_pa()")
	add_macro("PAX: Volume Down", "voldn()") 
	add_macro("PAX: Volume Up", "volup()") 

	create_command("FlyWithLua/PAX/volup", "PAX: Volume Up", "", "", "volup()") 
	create_command("FlyWithLua/PAX/voldn", "PAX: Volume Down","", "","voldn()")
	
	function volup() --увеличение громкости
		if volume < 5 then
			volume = volume + 1
		end
	end 

	function voldn() --уменьшение громкости
		if volume > 0 then
			volume = volume - 1
		end
	end 

	function disable_pa() --выключение плагина
		enable = 0
	end 

	--Загрузка звуков ========================================================================================================================
	local pa_welcome = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_welcome.wav") --Приветствие пассажиров
	local pa_armed = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_armed.wav") --Армирование дверей
	local pa_greetings = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_greetings.wav") --Приветствие СБ
	local pa_safety = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_safety.wav") --Безопасность
	local pa_prepare = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_prepare.wav") --Подготовка к взлету
	local pa_takeoff = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_takeoff.wav") --Взлет
	local pa_climb = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_climb.wav") --Набор
	local pa_bonus = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_bonus.wav") --Программа премирования
	local pa_cruise = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_cruise.wav") --Эшелон
	local pa_turb = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_turb.wav") --Турбулентность
	local pa_descent = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_descent.wav") --Снижение
	local pa_approach = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_approach.wav") --Завершение обслуживания
	local pa_landing = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_landing.wav") --Посадка
	local pa_destination = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_destination.wav") --Прибытие
	local pa_taxi = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_taxi.wav") --В движении  
	local ps_applause = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/ps_applause.wav") --Аплодисменты
	local cm_parking = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/cm_parking.wav") --На стоянке
	local pa_disarmed = load_WAV_file(SCRIPT_DIRECTORY .. "PAX/" .. company_icao .. "/pa_disarmed.wav") --Дизармирование дверей

	--Переменные ========================================================================================================================
	local enable = 0

	local pa_welcome_played = 0
	local pa_welcome_count = 0
	local pa_armed_played = 0
	local pa_greetings_played = 0
	local pa_safety_played = 0
	local pa_prepare_played = 0
	local pa_takeoff_played = 0
	local pa_climb_played = 0
	local pa_bonus_played = 0
	local pa_cruise_played = 0
	local pa_turb_played = 0
	local pa_descent_played = 0
	local pa_approach_played = 0
	local pa_landing_played = 0
	local pa_destination_played = 0
	local pa_taxi_played = 0
	local ps_applause_played = 0
	local cm_parking_played = 0
	local pa_disarmed_played = 0
	local gear_up = 0

	local door_pos = 0 --триггер текущего состояния двери
	local door_open = 0 --триггер что дверь открывалась
	local airborn = 0 --триггер взлета

	local chime = 0 --триггер чаймов
	local off_time = 0
	local on_time = 0
	local second_click = 0

	local result_volume = 0 --результирующая громкость (0.1 - 1)

	local in_air_fpm = 9999

	local event_time = 0 --для подсчета временных интервалов

	local stblt_chg_pos = 0
	local stblt_last_pos = seatbelt_sign_pos
	
	local welcome_time = os.clock()


	--Датарефы и настройки========================================================================================================================

	dataref("engine_running", "sim/flightmodel/engine/ENGN_running",0) --запущенный двигатель
	dataref("g_force", "sim/flightmodel/forces/g_nrml") --сила тяжести
	dataref("flaps", "sim/flightmodel2/controls/flap_handle_deploy_ratio") --0,0.25,0.5,0.75,1
	dataref("gnd_stat", "sim/flightmodel/forces/faxil_gear") --статус - на земле
	dataref("alt", "sim/cockpit2/gauges/indicators/altitude_ft_pilot") --высота в футах
	dataref("viewext", "sim/graphics/view/view_is_external") --внешний вид
	dataref("paused", "sim/time/paused") --пауза
	dataref("prb", "sim/cockpit2/controls/parking_brake_ratio") --стояночный тормоз 
	dataref("turb", "sim/weather/turbulence[0]") --турбулентность
	dataref("spd", "sim/flightmodel/position/indicated_airspeed") --воздушная скорость
	dataref("fpm", "sim/flightmodel/position/vh_ind_fpm") --вертикальная скорость
	dataref("camera_z_position", "sim/aircraft/view/acf_peZ") --позиция камеры
	dataref("turb", "sim/weather/turbulence[0]") --турбулентность
	dataref("gear","sim/cockpit2/controls/gear_handle_down") --шасси
	
	--Датарефы и настройки в зависимости от самолета ========================================================================================================================
	
	--sim/cockpit2/switches/door_open,model/controls/door_1l
	if PLANE_ICAO == "A320" then
		dataref("door", "model/controls/door_1l") --левая передная дверь
		dataref("seatbelt_sign_pos", "model/controls/light_belts")
		dataref("strob_lights","model/controls/light_strobe") --стробы
		cameraz = -12 --положение камеры
	end
	if PLANE_ICAO == "B738" then
		dataref("door", "737u/doors/L1") --левая передная дверь
		dataref("seatbelt_sign_pos", "sim/cockpit/switches/fasten_seat_belts") --положение тумблера Пристегните ремни
		dataref("strob_lights","sim/cockpit/electrical/strobe_lights_on") --стробы
		cameraz = -14 --положение камеры
	end
	if PLANE_ICAO == "A319" then
		dataref("door", "anim/doorFL") --левая передная дверь
		dataref("seatbelt_sign_pos", "AirbusFBW/SeatBeltSignsOn") --положение тумблера Пристегните ремни
		dataref("strob_lights","sim/cockpit/electrical/strobe_lights_on") --стробы
		cameraz = -11 --положение камеры
	end
	if string.find(PLANE_ICAO, "B75") then
		dataref("door", "anim/door/FL") --левая передная дверь
		dataref("seatbelt_sign_pos", "sim/cockpit/switches/fasten_seat_belts") --положение тумблера Пристегните ремни
		dataref("strob_lights","sim/cockpit/electrical/strobe_lights_on") --стробы
		cameraz = -18 --положение камеры
	end
	if string.find(PLANE_ICAO, "B76") then
		dataref("door", "1-sim/anim/doors/FL/anim") --левая передная дверь
		dataref("seatbelt_sign_pos", "sim/cockpit/switches/fasten_seat_belts") --положение тумблера Пристегните ремни
		dataref("strob_lights","sim/cockpit/electrical/strobe_lights_on") --стробы
		cameraz = -22 --положение камеры
	end
	if string.find(PLANE_ICAO, "B77") then
		dataref("door", "anim/LFmov/door") --левая передная дверь
		dataref("seatbelt_sign_pos", "anim/13/switch") --положение тумблера Пристегните ремни
		dataref("strob_lights","sim/cockpit/electrical/strobe_lights_on") --стробы
		cameraz = -31 --положение камеры
	end

	--Функции ========================================================================================================================
	
	--Настройка громкости
	function setvol()
		result_volume = volume / 5

		--Множитель для типа самолета
		if PLANE_ICAO == "A319" then
			result_volume = result_volume * a319_gain
		end
		if PLANE_ICAO == "A320" then
			result_volume = result_volume * a320_gain
		end
		if PLANE_ICAO == "B738" then
			result_volume = result_volume * b738_gain
		end
		if string.find(PLANE_ICAO, "B75") then
			result_volume = result_volume * b757_gain
		end
		if string.find(PLANE_ICAO, "B76") then
			result_volume = result_volume * b767_gain
		end
		if string.find(PLANE_ICAO, "B77") then
			result_volume = result_volume * b777_gain
		end

		--Множитель для авиакомпании
		if company_icao == "AFL" then
			result_volume = result_volume * afl_gain
		end
		if company_icao == "SDM" then
			result_volume = result_volume * sdm_gain
		end
			
		--Множитель для вида: внутри/снаружи
		if viewext == 0 then
			result_volume = result_volume * view_int_gain
		else
			result_volume = result_volume * view_ext_gain
		end

		--Множитель для режима работы двигателей: включены/выключены
		if engine_running == 1 then 
			result_volume = result_volume * engine_on_gain
		else
			result_volume = result_volume * engine_off_gain
		end

		--Множитель для состояния: в полете/на земле
		if gnd_stat == 0 then 
			result_volume = result_volume * on_air_gain
		else
			result_volume = result_volume * on_ground_gain
		end

		--Множитель для вида: кабина/салон
		if camera_z_position < cameraz then
			result_volume = result_volume * in_cockpit_gain
		else
			result_volume = result_volume * in_cabin_gain
		end

		if result_volume > 1 then result_volume = 1 end
		if result_volume < 0.1 then result_volume = 0.1 end

		--Установка результирующего уровня
		set_sound_gain(pa_welcome, result_volume)
		set_sound_gain(pa_armed, result_volume) 
		set_sound_gain(pa_greetings, result_volume)
		set_sound_gain(pa_safety, result_volume) 
		set_sound_gain(pa_prepare, result_volume) 
		set_sound_gain(pa_takeoff, result_volume) 
		set_sound_gain(pa_climb, result_volume) 
		set_sound_gain(pa_bonus, result_volume) 
		set_sound_gain(pa_cruise, result_volume) 
		set_sound_gain(pa_turb, result_volume) 
		set_sound_gain(pa_descent, result_volume) 
		set_sound_gain(pa_approach, result_volume) 
		set_sound_gain(pa_landing, result_volume) 
		set_sound_gain(pa_destination, result_volume) 
		set_sound_gain(pa_taxi, result_volume) 
		set_sound_gain(cm_parking, 0.5) 
		set_sound_gain(pa_disarmed, result_volume) 
		set_sound_gain(ps_applause, 0.7) 
	end  

	function onesecondlogic() --раз в секунду
	  
		--Положение дверей
		if PLANE_ICAO == "A320" then
			door_pos = door 
		end
		if PLANE_ICAO == "B738" then
			door_pos = door 
		end
		if PLANE_ICAO == "A319" then
			if door > 0 then
				door_pos = 1
			else
				door_pos = 0
			end
		end
		if string.find(PLANE_ICAO, "B75") then
			door_pos = door 
		end
		if string.find(PLANE_ICAO, "B76") then
			if door > 0.5 then
				door_pos = 1
			else
				door_pos = 0
			end
		end
		if string.find(PLANE_ICAO, "B77") then
			if door > 0 then
				door_pos = 1
			else
				door_pos = 0
			end
		end

		--Включение плагина
		if seatbelt_sign_pos > 0 and door_pos == 1 then
			enable = 1
		end

		if enable == 1 then

			--airborn = 1 --Для тестов!
			--door_open = 1  --Для тестов!

			setvol() --установка громкости

			--Проверка открытия двери
			if door_pos == 1 then
				door_open = 1
			end

			--Проверка уборки шасси
			if gear == 0 and gear_up == 0 then
				gear_up = 1
			end

			--Сброс проигрывания Посадка при уборке шасси (уход на второй круг)
			if gear == 0 and gnd_stat == 0 and airborn == 1 then
				pa_landing_played = 0
			end 

			--Повтор проигрывания pa_welcome через 3 минуты (не более 2 раз)
			if os.clock() > welcome_time + 180 and pa_welcome_played == 1 then 
				pa_welcome_played = 0
			end

			--Проверка взлета
			if gnd_stat == 0 and fpm > 350 and alt > 300 and airborn == 0 then
				airborn = 1
				stblt_chg_pos = 0
				chime = 0
			end

			--Размещение пассажиров pa_welcome
			if seatbelt_sign_pos > 0 and door_pos == 1 and gnd_stat ~= 0 and paused == 0 and pa_welcome_played == 0 and pa_welcome_count < 2 then
				play_sound(pa_welcome)
				pa_welcome_played = 1
				pa_welcome_count = pa_welcome_count + 1
				welcome_time = os.clock()
				stblt_chg_pos = 0
				chime = 0
			end

			--Армирование дверей pa_armed
			if door_open == 1 and door_pos == 0 and gnd_stat ~= 0 and paused == 0 and pa_armed_played == 0 then
				stop_sound (pa_welcome)
				play_sound (pa_armed)
				pa_welcome_played = 1
				pa_armed_played = 1
				pa_welcome_count = 2
				event_time = os.clock()
				chime = 0
			end

			--Приветствие СБ pa_greetings
			if os.clock() > event_time + 15 and door_open == 1 and door_pos == 0 and gnd_stat ~= 0 and paused == 0 and pa_armed_played == 1 and pa_greetings_played == 0 then
				stop_sound(pa_welcome)
				stop_sound(pa_armed)
				play_sound(pa_greetings)
				pa_armed_played = 1
				pa_greetings_played = 1
				pa_welcome_played = 1
				pa_welcome_count = 2
				event_time = 0
				chime = 0
			end

			--Взлет pa_takeoff
			if (chime == 1 or strob_lights == 1 and auto == 1) and flaps > 0 and spd < 40 and door_pos == 0 and gnd_stat ~= 0 and paused == 0 and pa_takeoff_played == 0 then
				stop_sound (pa_welcome)
				stop_sound (pa_armed)
				stop_sound (pa_safety)
				stop_sound (pa_prepare)
				play_sound(pa_takeoff)
				pa_welcome_played = 1
				pa_armed_played = 1
				pa_safety_played = 1
				pa_prepare_played = 1
				pa_takeoff_played = 1
				chime = 0
			end

			--Посадка pa_landing
			if (chime == 1 or auto == 1 and flaps > 0.6 and fpm < -350 and alt < 6000) and gear == 1 and gear_up == 1 and spd > 100 and airborn == 1 and gnd_stat == 0 and paused == 0 and pa_landing_played == 0 then
				stop_sound(pa_descent)
				stop_sound(pa_approach)
				play_sound(pa_landing)
				pa_descent_played = 1
				pa_approach_played = 1
				pa_landing_played = 1
				chime = 0
			end

			--Сброс проигрывания турбулентности
			if seatbelt_sign_pos == 0 then
				pa_turb_played = 0 
			end

			--На стоянке cm_parking
			if airborn == 1 and engine_running == 0 and prb ~= 0 and airborn == 1 and gear == 1 and gnd_stat ~= 0 and paused == 0 and cm_parking_played == 0 then
				stop_sound(pa_destination)
				stop_sound(pa_taxi)
				play_sound(cm_parking)
				pa_destination_played = 1
				pa_taxi_played = 1
				cm_parking_played = 1
				event_time = os.clock()
			end

			--Дизармирование дверей pa_disarmed
			if os.clock() > event_time + 5 and airborn == 1 and engine_running == 0 and prb ~= 0 and airborn == 1 and gear == 1 and gnd_stat ~= 0 and paused == 0 and cm_parking_played == 1 and pa_disarmed_played == 0 then
				stop_sound(pa_destination)
				stop_sound(pa_taxi)
				stop_sound(cm_parking)
				play_sound(pa_disarmed)
				pa_destination_played = 1
				pa_taxi_played = 1
				cm_parking_played = 1
				pa_disarmed_played = 1
				event_time = 0
			end

		end
	end

	function tensecondlogic() --раз в десять секунд
		if enable == 1 then

			--Сброс после посадки, выключения табло Пристегните ремни и открытии двери 1L
			if airborn == 1 and gear == 1 and door_pos == 1 and seatbelt_sign_pos == 0 then
				airborn = 0
				gear_up = 0
				pa_welcome_played = 0
				pa_welcome_count = 0        
				pa_armed_played = 0
				pa_greetings_played = 0
				pa_safety_played = 0
				pa_prepare_played = 0
				pa_takeoff_played = 0
				pa_climb_played = 0
				pa_bonus_played = 0
				pa_cruise_played = 0
				pa_turb_played = 0
				pa_descent_played = 0
				pa_approach_played = 0
				pa_landing_played = 0
				pa_destination_played = 0
				pa_taxi_played = 0
				pa_disarmed_played = 0
				cm_parking_played = 0
				ps_applause_played = 0
				stblt_chg_pos = 0
				chime = 0
			end		

			--Демонстрация pa_safety
			if door_open == 1 and door_pos == 0 and (engine_running == 1 or prb == 0) and gnd_stat ~= 0 and paused == 0 and pa_safety_played == 0 then
				stop_sound (pa_welcome)
				stop_sound (pa_armed)
				stop_sound (pa_greetings)
				play_sound (pa_safety)
				pa_welcome_played = 1
				pa_armed_played = 1
				pa_greetings_played = 1
				pa_safety_played = 1
				pa_welcome_count = 2
				event_time = os.clock()
				chime = 0
			end

			--Подготовка к взлету pa_prepare
			if os.clock() > event_time + 225 and door_open == 1 and door_pos == 0 and (engine_running == 1 or prb == 0) and flaps > 0 and gnd_stat ~= 0 and paused == 0 and pa_safety_played == 1 and pa_prepare_played == 0 then
				stop_sound (pa_welcome)
				stop_sound (pa_armed)
				stop_sound (pa_greetings)
				stop_sound (pa_safety)
				play_sound (pa_prepare)
				pa_welcome_played = 1
				pa_armed_played = 1
				pa_greetings_played = 1
				pa_safety_played = 1
				pa_welcome_count = 2
				pa_prepare_played = 1
				event_time = 0
			end
				
			--Набор pa_climb
			if (chime == 1 or alt > 11000 and auto == 1 and fpm > 350) and spd > 200 and alt > 6000 and airborn == 1 and gnd_stat == 0 and paused == 0 and pa_climb_played == 0 then
				play_sound(pa_climb)
				pa_climb_played = 1
				chime = 0
				stblt_chg_pos = 0
				event_time = os.clock()
			end

			--Программа премирования pa_bonus
			if os.clock() > event_time + 60 and airborn == 1 and gnd_stat == 0 and paused == 0 and pa_climb_played == 1 and pa_bonus_played == 0 then
				stop_sound(pa_climb)
				play_sound(pa_bonus)
				pa_climb_played = 1
				pa_bonus_played = 1
				event_time = 0
				chime = 0
			end

			--Эшелон pa_cruise
			if stblt_chg_pos == 1 and seatbelt_sign_pos == 0 and chime == 0 then
				if alt > 10000 and spd > 200 and airborn == 1 and gnd_stat == 0 and paused == 0 and pa_cruise_played == 0 then 
					stop_sound(pa_turb)
					stop_sound(pa_climb)
					stop_sound(pa_bonus)
					play_sound(pa_cruise) 
					pa_climb_played = 1 
					pa_bonus_played = 1 
					pa_cruise_played = 1 
					stblt_chg_pos = 0
					chime = 0
				end
			end

			--Турбулентность pa_turb
			if stblt_chg_pos == 1 and seatbelt_sign_pos > 0 and chime == 0 then
				if turb > 2 and fpm < 450 and fpm > -450 and alt > 10000 and spd > 200 and airborn == 1 and gnd_stat == 0 and paused == 0 and pa_climb_played == 1 and pa_cruise_played == 1 and pa_bonus_played == 1 and pa_turb_played == 0 then
					stop_sound(pa_climb)
					stop_sound(pa_cruise)
					stop_sound(pa_bonus)
					play_sound(pa_turb)
					pa_climb_played = 1 
					pa_bonus_played = 1 
					pa_cruise_played = 1 
					pa_turb_played = 1
					stblt_chg_pos = 0
					chime = 0
				end
			end

			--Снижение pa_descent
			if stblt_chg_pos == 1 and seatbelt_sign_pos > 0 and chime == 0 then
				if fpm < -450 and alt > 10000 and spd > 200 and airborn == 1 and gnd_stat == 0 and paused == 0 and pa_descent_played == 0 then
					stop_sound(pa_climb)
					stop_sound(pa_bonus)
					stop_sound(pa_cruise)
					stop_sound(pa_turb)
					play_sound(pa_descent)
					pa_climb_played = 1 
					pa_bonus_played = 1 
					pa_cruise_played = 1 
					pa_turb_played = 1
					pa_descent_played = 1
					stblt_chg_pos = 0
					chime = 0
				end
			end

			--Завершение обслуживания pa_approach
			if seatbelt_sign_pos > 0 then
				if fpm < -450 and alt < 10000 and alt > 6000 and spd > 200 and airborn == 1 and gnd_stat == 0 and paused == 0 and pa_approach_played == 0 and pa_climb_played == 1 then
					stop_sound(pa_climb)
					stop_sound(pa_bonus)
					stop_sound(pa_cruise)
					stop_sound(pa_turb)
					stop_sound(pa_descent)
					play_sound(pa_approach)
					pa_climb_played = 1 
					pa_bonus_played = 1 
					pa_cruise_played = 1 
					pa_turb_played = 1
					pa_descent_played = 1
					pa_approach_played = 1
					stblt_chg_pos = 0
					chime = 0
				end
			end

			--Аплодисменты ps_applause
			if PLANE_ICAO == "A320" or PLANE_ICAO == "B738" or PLANE_ICAO == "A319" or string.find(PLANE_ICAO, "B77") then
				if in_air_fpm ~= 9999 and in_air_fpm ~= 0 and math.floor(in_air_fpm) > -255 and spd < 70 and airborn == 1 and gear == 1 and gnd_stat ~= 0 and paused == 0 and ps_applause_played == 0 then
					play_sound(ps_applause)
					ps_applause_played = 1
				end 
			end

			--Прибытие pa_destination
			if spd < 40 and strob_lights == 0 and airborn == 1 and gnd_stat ~= 0 and paused == 0 and pa_destination_played == 0 then
				play_sound(pa_destination)
				pa_destination_played = 1
				event_time = os.clock()
			end

			--Длительное руление pa_taxi
			if os.clock() > event_time + 180 and spd < 30 and strob_lights == 0 and airborn == 1 and gnd_stat ~= 0 and paused == 0 and pa_destination_played == 1 and pa_taxi_played == 0 then
				stop_sound(pa_destination)
				play_sound(pa_taxi)
				pa_destination_played = 1
				pa_taxi_played = 1
				event_time = 0
			end
	  
		end
	end

	function everyframelogic() --каждый кадр
		if enable == 1 then

			--Определение вертикальной скорости при посадке
			if airborn == 1 and gnd_stat == 0 and fpm < 0 and spd > 10 then
				in_air_fpm = fpm
			end 

			--Определение смены позиции выключателя Пристегните ремни
			if stblt_last_pos ~= seatbelt_sign_pos then
				stblt_chg_pos = 1
				stblt_last_pos = seatbelt_sign_pos
			end

			--Чаймы
			if seatbelt_sign_pos == 0 then
				off_time = os.clock()
				second_click = 1
				chime = 0
			end

			if seatbelt_sign_pos > 0 and second_click == 0 then
				on_time = os.clock()
			end  

			if seatbelt_sign_pos > 0 and second_click == 1 then
				second_click = 0
				if off_time - on_time < 2 then
					chime = 1
				else
					chime = 0
				end
			end
		  
		end
	end

	do_sometimes("tensecondlogic()") 
	do_often("onesecondlogic()") 
	do_every_frame ("everyframelogic()")

	print("PAX 4.4 (Passenger Announcements for X-Plane) loaded successfully") 

	local starting_time = os.clock()

	function on_screen_display() 
		local pos = 0 
		--pos = big_bubble(20, pos, "Volume: " .. tostring(result_volume)) 
		--pos = big_bubble(20, pos, "Seat Belt: " .. tostring(seatbelt_sign_pos)) 
		--pos = big_bubble(20, pos, "Door: " .. tostring(door_pos)) 
		--pos = big_bubble(20, pos, "Chime: " .. tostring(chime)) 
		--pos = big_bubble(20, pos, "Parking Brake: " .. tostring(prb)) 
		--pos = big_bubble(20, pos, "Flaps: " .. tostring(flaps))
		--pos = big_bubble(20, pos, "Strobe Lights: " .. tostring(strob_lights)) 
		--pos = big_bubble(20, pos, "Speed: " .. tostring(spd)) 
		--pos = big_bubble(20, pos, "Altitude: " .. tostring(alt)) 
		--pos = big_bubble(20, pos, "Vertical Speed: " .. tostring(fpm)) 
		--pos = big_bubble(20, pos, "Camera pozition: " .. tostring(camera_z_position)) 
		--pos = big_bubble(20, pos, "On ground: " .. tostring(gnd_stat)) 
		--pos = big_bubble(20, pos, "Airborn: " .. tostring(airborn)) 
		--pos = big_bubble(20, pos, "Camera pozition: " .. PLANE_ICAO) 
		if os.clock() < starting_time + 10 then 
		  pos = big_bubble(20, pos, "PAX 4.4 (Passenger Announcements for X-Plane) loaded successfully") 
		end 
	end
	
	do_every_draw("on_screen_display()")

end

