

--*************************************************************************************--
--** 					               CONSTANTS                    				 **--
--*************************************************************************************--

NUM_DROP = 93
NUM_DROP_1 = 31
NUM_DROP_2 = 18
NUM_DROP_3 = 24
NUM_DROP_4 = 28
NUM_DROP_5 = 32
NUM_DROP_6 = 29
NUM_DROP_7 = 24

precip_acf_ratio = 0
hud_mode = 0
load_plane = 0

left_clr_timer = 0
right_clr_timer = 0
left_clr_timer_trig = 0
right_clr_timer_trig = 0
left_new_cycle = 0
right_new_cycle = 0
cleared_wndshld = 0

first_ttt = 0

--*************************************************************************************--
--** 					            GLOBAL VARIABLES                				 **--
--*************************************************************************************--

--*************************************************************************************--
--** 					            LOCAL VARIABLES                 				 **--
--*************************************************************************************--
--*************************************************************************************--
--** 				             FIND X-PLANE DATAREFS            			    	 **--
--*************************************************************************************--

simDR_rain_acf_ratio	= find_dataref("sim/weather/precipitation_on_aircraft_ratio")
simDR_ground_speed		= find_dataref("sim/flightmodel/position/groundspeed")

XE_DR_active			= find_dataref("env/active")
XE_DR_rain				= find_dataref("env/rain")
XE_DR_snow				= find_dataref("env/snow")
XE_DR_rain2				= find_dataref("sim/weather/rain_percent")

simDR_theta 					= find_dataref("sim/flightmodel/position/theta")
simDR_phi						= find_dataref("sim/flightmodel/position/phi")
simDR_pitch_elec				= find_dataref("sim/cockpit2/gauges/indicators/pitch_electric_deg_pilot")
simDR_radio_height_pilot_ft		= find_dataref("sim/cockpit2/gauges/indicators/radio_altimeter_height_ft_pilot")
simDR_vpath						= find_dataref("sim/flightmodel/position/vpath")

simDR_window_ice	= find_dataref("sim/flightmodel/failures/window_ice")
simDR_rel_eng_ice	= find_dataref("sim/flightmodel/failures/inlet_ice_per_engine")
simDR_rel_wing_ice	= find_dataref("sim/flightmodel/failures/frm_ice")
simDR_rel_wing_ice2	= find_dataref("sim/flightmodel/failures/frm_ice2")

simDR_pex			= find_dataref("sim/aircraft/view/acf_peX")
simDR_pey			= find_dataref("sim/aircraft/view/acf_peY")
simDR_pez			= find_dataref("sim/aircraft/view/acf_peZ")

simDR_vr_enable		= find_dataref("sim/graphics/VR/enabled")
simDR_inside		= find_dataref("sim/operation/sound/inside_ratio")

simDR_ax		= find_dataref("sim/flightmodel/position/local_ax")
simDR_ay		= find_dataref("sim/flightmodel/position/local_ay")
simDR_az		= find_dataref("sim/flightmodel/position/local_az")

simDR_wind_spd				= find_dataref("sim/cockpit2/gauges/indicators/wind_speed_kts")
simDR_OAT					= find_dataref("sim/cockpit2/temperature/outside_air_temp_degc")

--*************************************************************************************--
--** 				               FIND X-PLANE COMMANDS                   	    	 **--
--*************************************************************************************--

simCMD_def_view			= find_command("sim/view/default_view")

--*************************************************************************************--
--** 				              FIND CUSTOM DATAREFS             			    	 **--
--*************************************************************************************--

B738DR_left_wiper_ratio		= find_dataref("laminar/B738/others/left_wiper_ratio")
B738DR_right_wiper_ratio	= find_dataref("laminar/B738/others/right_wiper_ratio")

B738DR_l_side_temp 				= find_dataref("laminar/B738/ice/l_side_temp")
B738DR_l_fwd_temp 				= find_dataref("laminar/B738/ice/l_fwd_temp")
B738DR_r_side_temp 				= find_dataref("laminar/B738/ice/r_side_temp")
B738DR_r_fwd_temp 				= find_dataref("laminar/B738/ice/r_fwd_temp")

B738DR_left_wiper_up		= find_dataref("laminar/B738/others/left_wiper_up")
B738DR_right_wiper_up		= find_dataref("laminar/B738/others/right_wiper_up")

B738DR_hud_stow				= find_dataref("laminar/B738/HUD/HUD_stow")
B738DR_hud_disable			= find_dataref("laminar/B738/hud_disable")

B738DR_left_wiper_pos		= find_dataref("laminar/B738/switches/left_wiper_pos")
B738DR_right_wiper_pos		= find_dataref("laminar/B738/switches/right_wiper_pos")

B738DR_particle_rain		= find_dataref("laminar/B738/particle_rain")

B738DR_tab_static			= find_dataref("laminar/B738/tab/static")
B738DR_fo_tab_static		= find_dataref("laminar/B738/tab/fo_static")
------

SkiDR_init					= find_dataref("librain/initialize")
-- SkiDR_init_ok				= find_dataref("librain/init_success")
------

xplane_zibofile				= find_dataref("zibomod/CRC")

B738DR_cabin_temp				= find_dataref("laminar/B738/cabin_temp")
B738DR_window_heat_l_side_pos	= find_dataref("laminar/B738/ice/window_heat_l_side_pos")
B738DR_window_heat_l_fwd_pos	= find_dataref("laminar/B738/ice/window_heat_l_fwd_pos")
B738DR_window_heat_r_side_pos	= find_dataref("laminar/B738/ice/window_heat_r_side_pos")
B738DR_window_heat_r_fwd_pos	= find_dataref("laminar/B738/ice/window_heat_r_fwd_pos")
B738DR_ac_tnsbus1_status		= find_dataref("laminar/B738/electric/ac_tnsbus1_status")
B738DR_ac_tnsbus2_status		= find_dataref("laminar/B738/electric/ac_tnsbus2_status")

B738DR_monster_icing			= find_dataref("laminar/B738/ice/monster_icing")
B738DR_passengers_hide			= find_dataref("laminar/B738/effects/passengers_hide")

B738DR_layout_0_hide		= find_dataref("laminar/B738/layout_0_hide")
B738DR_layout_1_hide		= find_dataref("laminar/B738/layout_1_hide")
B738DR_passengers_0_hide	= find_dataref("laminar/B738/effects/passengers_0_hide")
B738DR_passengers_1_hide	= find_dataref("laminar/B738/effects/passengers_1_hide")

simDR_int_ver				= find_dataref("sim/version/xplane_internal_version")
simDR_HUD_on				= find_dataref("sim/cockpit/electrical/HUD_on")

--*************************************************************************************--
--** 				        CREATE READ-ONLY CUSTOM DATAREFS               	         **--
--*************************************************************************************--

B738DR_horizon				= create_dataref("laminar/B738/hud/horizon", "number")
B738DR_73x					= find_dataref("laminar/B738/73x")
B738DR_b737_variant			= find_dataref("zibomod/b737_variant")

B738DR_hud_displ_disable	= create_dataref("laminar/B738/hud_displ_disable", "number")
B738DR_hud_displ_vr_disable	= create_dataref("laminar/B738/hud_displ_vr_disable", "number")
B738DR_freeze_monster		= create_dataref("laminar/B738/freeze_monster", "number")
B738DR_freeze_eng1			= create_dataref("laminar/B738/freeze_eng1", "number")
B738DR_freeze_eng2			= create_dataref("laminar/B738/freeze_eng2", "number")
B738DR_freeze_wing1			= create_dataref("laminar/B738/freeze_wing1", "number")
B738DR_freeze_wing2			= create_dataref("laminar/B738/freeze_wing2", "number")

B738DR_eng_ice_hide			= create_dataref("laminar/B738/effects/eng_ice_hide", "number")
B738DR_wing_ice_hide		= create_dataref("laminar/B738/effects/wing_ice_hide", "number")

function B738DR_holdover_time_handl() end
B738DR_holdover_time		= create_dataref("laminar/B738/de_ice_truck/holdover_time", "number", B738DR_holdover_time_handl)

--*************************************************************************************--
--** 				       READ-WRITE CUSTOM DATAREF HANDLERS     	        	     **--
--*************************************************************************************--

function B738DR_align_horizon_DRhandler() end
function B738DR_align_horizon2_DRhandler() end
function B738DR_align_horizon3_DRhandler() end

function B738_rain_ratio_DRhandler() end
function B738_rain_rate1_DRhandler() end
function B738_rain_rate2_DRhandler() end
function B738_rain_size_DRhandler() end
function B738_rain_life_DRhandler() end
function B738_rain_speed_DRhandler() end
function B738_rain_angle_left_DRhandler() end

function B738_freeze_effect_DRhandler() end

function B738DR_enable_librain_vr_DRhandler() end


--*************************************************************************************--
--** 				       CREATE READ-WRITE CUSTOM DATAREFS                         **--
--*************************************************************************************--
B738DR_rain_ratio		= create_dataref("laminar/B738/effect/rain_ratio", "number", B738_rain_ratio_DRhandler)
B738DR_rain_rate1		= create_dataref("laminar/B738/effect/rain_rate1", "number", B738_rain_rate1_DRhandler)
B738DR_rain_rate2		= create_dataref("laminar/B738/effect/rain_rate2", "number", B738_rain_rate2_DRhandler)
B738DR_rain_size		= create_dataref("laminar/B738/effect/rain_size", "number", B738_rain_size_DRhandler)
B738DR_rain_life		= create_dataref("laminar/B738/effect/rain_life", "number", B738_rain_life_DRhandler)
B738DR_rain_speed		= create_dataref("laminar/B738/effect/rain_speed", "number", B738_rain_speed_DRhandler)
B738DR_rain_angle_left	= create_dataref("laminar/B738/effect/rain_angle_left", "number", B738_rain_angle_left_DRhandler)

B738DR_freeze_effect	= create_dataref("laminar/B738/freeze_effect", "number", B738_freeze_effect_DRhandler)

B738DR_align_horizon	= create_dataref("laminar/B738/hud/align_horizon", "number", B738DR_align_horizon_DRhandler)
B738DR_align_horizon2	= create_dataref("laminar/B738/hud/align_horizon2", "number", B738DR_align_horizon2_DRhandler)
B738DR_align_horizon3	= create_dataref("laminar/B738/hud/align_horizon3", "number", B738DR_align_horizon3_DRhandler)

B738DR_enable_librain_vr	= create_dataref("laminar/B738/effect/enable_librain_vr", "number", B738DR_enable_librain_vr_DRhandler)

--*************************************************************************************--
--** 				             CUSTOM COMMAND HANDLERS            			     **--
--*************************************************************************************--


function B738DR_hide_windows_xt_DRhandler() end


--*************************************************************************************--
--** 				              CREATE CUSTOM COMMANDS              			     **--
--*************************************************************************************--


B738DR_kill_effect				= find_dataref("laminar/B738/perf/kill_effect")
B738DR_kill_windshield			= find_dataref("laminar/B738/perf/kill_windshield")


B738DR_hide_windows_xt = create_dataref("laminar/B738/effect/hide_windows_xt", "number", B738DR_hide_windows_xt_DRhandler)

B738DR_window_effect_L = create_dataref("laminar/B738/effect/windowL", "number")
B738DR_window_effect_LS = create_dataref("laminar/B738/effect/windowLS", "number")
B738DR_window_effect_R = create_dataref("laminar/B738/effect/windowR", "number")
B738DR_window_effect_RS = create_dataref("laminar/B738/effect/windowRS", "number")

B738DR_window_effect_L02 = create_dataref("laminar/B738/effect/windowL02", "array[9]")

B738DR_window_effect_LS02 = create_dataref("laminar/B738/effect/windowLS02", "number")

B738DR_window_effect_R02 = create_dataref("laminar/B738/effect/windowR02", "array[9]")

B738DR_window_effect_RS02 = create_dataref("laminar/B738/effect/windowRS02", "number")

B738DR_window_effect_animL = create_dataref("laminar/B738/effect/window_animL", "array[7]")
B738DR_window_effect_animR = create_dataref("laminar/B738/effect/window_animR", "array[7]")

B738DR_window_effect_animL02 = create_dataref("laminar/B738/effect/window_animL02", "array[72]")

B738DR_window_effect_animR02 = create_dataref("laminar/B738/effect/window_animR02", "array[72]")

B738DR_window_effect_cabin = create_dataref("laminar/B738/effect/window_cabin", "number")

B738DR_effect_test = create_dataref("laminar/B738/effect/test", "number")
--*************************************************************************************--
--** 				             X-PLANE COMMAND HANDLERS               	    	 **--
--*************************************************************************************--



--*************************************************************************************--
--** 				             REPLACE X-PLANE COMMANDS                  	    	 **--
--*************************************************************************************--

function B738_align_hud_view_CMDhandler(phase, duration)
	if phase == 0 then
		simCMD_def_view:once()
		simDR_pex = -0.525
		simDR_pey = 1.6475	--1.640148	--1.646821	--1.6479	--1.653
	end
end


--*************************************************************************************--
--** 				              WRAP X-PLANE COMMANDS                  	    	 **--
--*************************************************************************************--

B738CMD_align_hud_view	= create_command("laminar/B738/hud/align_view", "Align HUD view", B738_align_hud_view_CMDhandler)

--*************************************************************************************--
--** 					           OBJECT CONSTRUCTORS         		        		 **--
--*************************************************************************************--



--*************************************************************************************--
--** 				                  CREATE OBJECTS              	     			 **--
--*************************************************************************************--



--*************************************************************************************--
--** 				                 SYSTEM FUNCTIONS           	    			 **--
--*************************************************************************************--



--*************************************************************************************--
--** 				               XLUA EVENT CALLBACKS       	        			 **--
--*************************************************************************************--

--wiper_up_table = { [0] = 10, [1] = 20, [2] = 30, [3] = 40, [4] = 50, [5] = 57, [6] = 66, [7] = 75 }
raw_table('wiper_up_table')
wiper_up_table = { [0] =  5, [1] = 10, [2] = 20, [3] = 30, [4] = 40, [5] = 50, [6] = 55, [7] = 70 }
raw_table('wiper_dn_table')
wiper_dn_table = { [0] =  5, [1] = 10, [2] = 20, [3] = 30, [4] = 40, [5] = 50, [6] = 55, [7] = 64 }

raw_table('rndm_tab')
rndm_tab = 
	{
		[0] = {[0] = 0, [1] = 1, [2] = 2, [3] = 3, [4] = 4, [5] = 5, [6] = 6, [7] = 7},
		[1] = {[0] = 1, [1] = 3, [2] = 2, [3] = 0, [4] = 7, [5] = 6, [6] = 5, [7] = 4},
		[2] = {[0] = 7, [1] = 6, [2] = 5, [3] = 4, [4] = 3, [5] = 2, [6] = 1, [7] = 0},
		[3] = {[0] = 5, [1] = 4, [2] = 6, [3] = 3, [4] = 7, [5] = 0, [6] = 2, [7] = 1},
		[4] = {[0] = 3, [1] = 6, [2] = 2, [3] = 1, [4] = 5, [5] = 3, [6] = 0, [7] = 4}
	}

i = 0
ii = 0

rain_ratio = 0
rain_acf = 0
timer_rain = 0

left_out_window_ratio = 0
right_out_window_ratio = 0
left_side_window_ratio = 0
right_side_window_ratio = 0

left_out_window_ratio_t = 0
right_out_window_ratio_t = 0
left_side_window_ratio_t = 0
right_side_window_ratio_t = 0

cabin_window_ratio = 0

was_left_frost = 0
was_right_frost = 0
was_left_side_frost = 0
was_right_side_frost = 0

raw_table('animL_ratio_tgt')
animL_ratio_tgt = {}
raw_table('animR_ratio_tgt')
animR_ratio_tgt = {}
raw_table('animL_ratio')
animL_ratio = {}
raw_table('animR_ratio')
animR_ratio = {}
raw_table('animL_transp_ratio')
animL_transp_ratio = {}
raw_table('animR_transp_ratio')
animR_transp_ratio = {}
raw_table('animL_ratio_tgt_w')
animL_ratio_tgt_w = {}
raw_table('animR_ratio_tgt_w')
animR_ratio_tgt_w = {}

for i = 0, 6 do
	animL_ratio_tgt[i] = 0
	animR_ratio_tgt[i] = 0
	animL_ratio[i] = 0
	animR_ratio[i] = 0
	animL_transp_ratio[i] = 0
	animR_transp_ratio[i] = 0
	animL_ratio_tgt_w[i] = 0
	animR_ratio_tgt_w[i] = 0
end

local L02_timer = 0
local L02_cnt = 0
local R02_timer = 0
local R02_cnt = 0
raw_table('L_anim_timer')
local L_anim_timer = {}
raw_table('L_anim_cnt')
local L_anim_cnt = {}
raw_table('R_anim_timer')
local R_anim_timer = {}
raw_table('R_anim_cnt')
local R_anim_cnt = {}

raw_table('rndm_L')
local rndm_L = {}
raw_table('rndm_R')
local rndm_R = {}

for i = 0, 7 do
	L_anim_timer[i] = 0
	L_anim_cnt[i] = 0
	R_anim_timer[i] = 0
	R_anim_cnt[i] = 0
	rndm_L[i] = {}
	rndm_R[i] = {}
	for ii = 0, 7 do
		rndm_L[i][ii] = ii
		rndm_R[i][ii] = ii
	end
end

-- function B738_rescale(in1, out1, in2, out2, x)
    -- if x < in1 then return out1 end
    -- if x > in2 then return out2 end
	-- if in1 == in2 then
		-- return out1
	-- else
		-- return out1 + (out2 - out1) * (x - in1) / (in2 - in1)
	-- end
-- end

function B738_rescale(res1_in, res1_out, res2_in, res2_out, res_x)
	local result = 0
	if res1_in <= res2_in then
		if (res_x < res1_in) then
			result = res1_out
		elseif (res_x > res2_in) then
			result = res2_out
		elseif (res2_in == res1_in) then
			result = res1_out
		else
			result = res1_out + (res2_out - res1_out) * (res_x - res1_in) / (res2_in - res1_in)
		end
	else
		if (res_x > res1_in) then
			result = res1_out
		elseif (res_x < res2_in) then
			result = res2_out
		elseif (res2_in == res1_in) then
			result = res1_out
		else
			result = res1_out + (res2_out - res1_out) * (res_x - res1_in) / (res2_in - res1_in)
		end
	end
	
	return result
end

function B738_set_animation_rate(current_value, target, min, max, speed)
	local calc_result = 0
	if target >= (max - 0.001) and current_value >= (max - 0.01) then
        return max
    elseif target <= (min + 0.001) and current_value <= (min + 0.01) then
        return min
    elseif current_value > max then
		return max
    elseif current_value < min then
		return min
	elseif current_value < target then
		calc_result = current_value + (SIM_PERIOD / speed)
		if calc_result > target then
			calc_result = target
		end
		return calc_result
	else
		calc_result = current_value - (SIM_PERIOD / speed)
		if calc_result < target then
			calc_result = target
		end
        return calc_result
    end
end

function dump_leg_append(data_str)
	local fms_line = ""
	
	local file_name2 = "zibo_log.txt"
	local file_navdata2 = io.open(file_name2, "a")
	
	if file_navdata2 ~= nil then
		fms_line = data_str
		file_navdata2:write(fms_line)
		file_navdata2:close()
	end
end


function rain_triger()
	
	
	local gnd_spd = simDR_ground_speed
	gnd_spd = math.min(gnd_spd, 236)
	gnd_spd = math.max(gnd_spd, 5)
	
	local rain_spd_ratio = B738_rescale(5, 1, 236, 2, gnd_spd)
	
	rain_acf = precip_acf_ratio * rain_spd_ratio
	rain_acf = math.min(rain_acf, 0.5)
	rain_acf = math.max(rain_acf, 0)
	
	local tmp = rain_acf
	if tmp < 0.2 then
		rain_ratio = 2 - B738_rescale(0, 0.8, 0.2, 1.6, tmp)
	else
		rain_ratio = 2 - B738_rescale(0.2, 1.6, 0.5, 1.99, tmp)
	end
	
	timer_rain = timer_rain + SIM_PERIOD

end


function window_ratio()
	
	local ratio_tgt = 0
	local ratio_time = 0
	local k = 0
	local kk = 0
	local tmp2 = 0
	local ratio_tgt2 = 0
	
	local int, frac = math.modf(os.clock())
	local seed = math.random(1, frac*1000.0)
	math.randomseed(seed)
	local rndm = 0
	local kkk = 0
	
	local gnd_spd = simDR_ground_speed
	gnd_spd = math.min(gnd_spd, 236)
	gnd_spd = math.max(gnd_spd, 5)
	
	local tmp = precip_acf_ratio * B738_rescale(5, 1, 236, 4.5, gnd_spd)	-- 2.5
	tmp = math.min(tmp, 1)
	tmp = math.max(tmp, 0)
	
	local ratio_time_rain2 = 0
	if tmp < 0.1 then
		ratio_time_rain2 = B738_rescale(0, 0.11, 0.1, 0.37, tmp)	-- window transp
	elseif tmp < 0.4 then
		ratio_time_rain2 = B738_rescale(0.1, 0.37, 0.4, 0.72, tmp)	-- window transp
	else
		ratio_time_rain2 = B738_rescale(0.4, 0.72, 1, 0.87, tmp)	-- window transp
	end
	
	-- local ratio_time_rain3 = 0
	-- if tmp < 0.1 then
		-- ratio_time_rain3 = 40 - B738_rescale(0, 0, 0.1, 21.5, tmp)	-- window spd
	-- elseif tmp < 0.4 then
		-- ratio_time_rain3 = 40 - B738_rescale(0.1, 21.5, 0.4, 36.8, tmp)	-- window spd
	-- else
		-- ratio_time_rain3 = 40 - B738_rescale(0.4, 36.8, 1, 38.7, tmp)	-- window spd
	-- end
	
	local ratio_time_rain3 = 0
	if tmp < 0.15 then
		ratio_time_rain3 = B738_rescale(0, 30, 0.15, 5, tmp)
	else
		ratio_time_rain3 = B738_rescale(0.15, 5, 1, 0.01, tmp)
	end
	
	--local ratio_time_rain = B738_rescale(0, 0, 1, 0.77, precip_acf_ratio)
	local ratio_time_rain = 0
	tmp = precip_acf_ratio
	if tmp < 0.1 then
		ratio_time_rain = B738_rescale(0, 0.11, 0.1, 0.37, tmp)	-- window transp
	elseif tmp < 0.4 then
		ratio_time_rain = B738_rescale(0.1, 0.37, 0.4, 0.72, tmp)	-- window transp
	else
		ratio_time_rain = B738_rescale(0.4, 0.72, 1, 0.87, tmp)	-- window transp
	end
	
	if rain_acf > 0 then
		ratio_time_rain3 = ratio_time_rain3 / 2
		ratio_time_rain2 = ratio_time_rain2 / 2
		ratio_time_rain = ratio_time_rain / 2
	end
	
	-----
	-- LEFT OUT --
	ratio_time = ratio_time_rain3
	if rain_acf > 0 then
		ratio_tgt = ratio_time_rain2
	else
		ratio_tgt = 0
	end
	if ratio_tgt < left_out_window_ratio then
		ratio_time = ratio_time * 2.3
	end
	if B738DR_l_fwd_temp < 0.2 and B738DR_freeze_effect <= 0.001 then	--and simDR_ground_speed < 70 then
		was_left_frost = 1
		if ratio_tgt < left_out_window_ratio_t or ratio_tgt == 0 then
			ratio_time = 480
		end
		ratio_tgt = 0.977
	elseif B738DR_l_fwd_temp < 0.2 and B738DR_freeze_effect > 0.001 then
		was_left_frost = 1
		if ratio_tgt < left_out_window_ratio_t or ratio_tgt == 0 then
			--ratio_time = 240
			ratio_time = B738_rescale(0, 240, 1.0, 60, B738DR_freeze_effect)
		end
		ratio_tgt = 0.977
	--elseif B738DR_l_fwd_temp < 0.2 and was_left_frost == 1 then
	elseif was_left_frost == 1 then
		ratio_time = 240
	end
	--if was_left_frost == 0 then
	if rain_acf > 0 or was_left_frost == 0 then
		if ratio_tgt == 0 then
			left_out_window_ratio = B738_set_animation_rate(left_out_window_ratio, ratio_tgt, 0, 1, ratio_time)
		else
			left_out_window_ratio = B738_set_animation_rate(left_out_window_ratio, 1, 0, 1, ratio_time)
		end
	end
	--if left_out_window_ratio == ratio_tgt or was_left_frost == 1 then
	if left_out_window_ratio == 1 or ratio_tgt == 0 or was_left_frost == 1 then
		left_out_window_ratio_t = B738_set_animation_rate(left_out_window_ratio_t, ratio_tgt, 0, 0.977, math.max(ratio_time, 2))
	end
	B738DR_window_effect_L = left_out_window_ratio_t
	
	if ratio_tgt == 0 then
		for k = 0, 8 do
			B738DR_window_effect_L02[k] = B738_set_animation_rate(B738DR_window_effect_L02[k], 0, 0, 1, ratio_time)
		end
		L02_timer = 0
		L02_cnt = 0
	elseif left_out_window_ratio > 0 then
		if left_out_window_ratio < 0.1 then
			B738DR_window_effect_L02[0] = 1
		elseif left_out_window_ratio < 0.2 then
			B738DR_window_effect_L02[0] = 1
			B738DR_window_effect_L02[1] = 1
		elseif left_out_window_ratio < 0.3 then
			B738DR_window_effect_L02[0] = 1
			B738DR_window_effect_L02[1] = 1
			B738DR_window_effect_L02[2] = 1
		elseif left_out_window_ratio < 0.4 then
			B738DR_window_effect_L02[0] = 1
			B738DR_window_effect_L02[1] = 1
			B738DR_window_effect_L02[2] = 1
			B738DR_window_effect_L02[3] = 1
		elseif left_out_window_ratio < 0.5 then
			B738DR_window_effect_L02[0] = 1
			B738DR_window_effect_L02[1] = 1
			B738DR_window_effect_L02[2] = 1
			B738DR_window_effect_L02[3] = 1
			B738DR_window_effect_L02[4] = 1
		elseif left_out_window_ratio < 0.6 then
			B738DR_window_effect_L02[0] = 1
			B738DR_window_effect_L02[1] = 1
			B738DR_window_effect_L02[2] = 1
			B738DR_window_effect_L02[3] = 1
			B738DR_window_effect_L02[4] = 1
			B738DR_window_effect_L02[5] = 1
		elseif left_out_window_ratio < 0.7 then
			B738DR_window_effect_L02[0] = 1
			B738DR_window_effect_L02[1] = 1
			B738DR_window_effect_L02[2] = 1
			B738DR_window_effect_L02[3] = 1
			B738DR_window_effect_L02[4] = 1
			B738DR_window_effect_L02[5] = 1
			B738DR_window_effect_L02[6] = 1
		elseif left_out_window_ratio < 0.8 then
			B738DR_window_effect_L02[0] = 1
			B738DR_window_effect_L02[1] = 1
			B738DR_window_effect_L02[2] = 1
			B738DR_window_effect_L02[3] = 1
			B738DR_window_effect_L02[4] = 1
			B738DR_window_effect_L02[5] = 1
			B738DR_window_effect_L02[6] = 1
			B738DR_window_effect_L02[7] = 1
		else
			L02_timer = L02_timer + SIM_PERIOD
			tmp2 = ratio_time / 10
			tmp2 = math.max(0.2, tmp2)
			if L02_timer > tmp2 then
				L02_timer = 0
				L02_cnt = L02_cnt + 1
				if L02_cnt > 7 then
					L02_cnt = 0
				end
				B738DR_window_effect_L02[L02_cnt] = 1
			end
			
			for k = 0, 7 do
				B738DR_window_effect_L02[k] = B738_set_animation_rate(B738DR_window_effect_L02[k], 0, 0, 1, 1)
			end
		end
		
		if left_out_window_ratio_t == ratio_tgt then
			B738DR_window_effect_L02[8] = B738_set_animation_rate(B738DR_window_effect_L02[8], 0.6, 0, 1, 2)
		else
			if left_out_window_ratio > 0.5 then
				B738DR_window_effect_L02[8] = B738_rescale(0.5, 0, 1, 1, left_out_window_ratio)
			end
		end
	end
	
	-----
	-- LEFT SIDE --
	ratio_time = 40
	if rain_acf > 0 then
		ratio_tgt = ratio_time_rain
	else
		ratio_tgt = 0
	end
	if ratio_tgt < left_side_window_ratio then
		ratio_time = ratio_time * 2
	end
	if B738DR_l_side_temp < 0.2 and B738DR_freeze_effect <= 0.001 then	--and simDR_ground_speed < 70 then
		was_left_side_frost = 1
		if ratio_tgt < left_side_window_ratio_t then
			ratio_time = 480
		end
		ratio_tgt = 0.977
	elseif B738DR_l_side_temp < 0.2 and B738DR_freeze_effect > 0.001 then
		was_left_side_frost = 1
		if ratio_tgt < left_side_window_ratio_t then
			--ratio_time = 240
			ratio_time = B738_rescale(0, 240, 1.0, 60, B738DR_freeze_effect)
		end
		ratio_tgt = 0.977
	--elseif B738DR_l_side_temp < 0.2 and was_left_side_frost == 1 then
	elseif was_left_side_frost == 1 then
		ratio_time = 240
	else
		if ratio_time_rain >= left_side_window_ratio_t then
			was_left_side_frost = 0
		end
	end
	--if was_left_side_frost == 0 then
	if rain_acf > 0 or was_left_side_frost == 0 then
		left_side_window_ratio = B738_set_animation_rate(left_side_window_ratio, ratio_tgt, 0, 0.977, ratio_time)
	end
	if left_side_window_ratio == ratio_tgt or was_left_side_frost == 1 then
		left_side_window_ratio_t = B738_set_animation_rate(left_side_window_ratio_t, ratio_tgt, 0, 0.977, ratio_time)
	end
	
	
	B738DR_window_effect_LS02 = left_side_window_ratio_t
	tmp = math.min(left_side_window_ratio, 0.3)
	tmp = math.max(tmp, 0)
	if ratio_tgt < left_side_window_ratio then
		tmp2 = B738_rescale(0, 0, 0.21, 1, tmp)
		if tmp2 > left_side_window_ratio then
			B738DR_window_effect_LS = left_side_window_ratio
		else
			B738DR_window_effect_LS = tmp2
		end
	else
		B738DR_window_effect_LS = B738_rescale(0, 0, 0.3, 1, tmp)
	end
	-----
	-- RIGHT OUT --
	ratio_time = ratio_time_rain3
	if rain_acf > 0 then
		ratio_tgt = ratio_time_rain2
	else
		ratio_tgt = 0
	end
	if ratio_tgt < right_out_window_ratio then
		ratio_time = ratio_time * 2.3
	end
	if B738DR_r_fwd_temp < 0.2 and B738DR_freeze_effect <= 0.001 then	--and simDR_ground_speed < 70 then
		was_right_frost = 1
		if ratio_tgt < right_out_window_ratio_t or ratio_tgt == 0 then
			ratio_time = 480
		end
		ratio_tgt = 0.977
	elseif B738DR_r_fwd_temp < 0.2 and B738DR_freeze_effect > 0.001 then
		was_right_frost = 1
		if ratio_tgt < right_out_window_ratio_t or ratio_tgt == 0 then
			--ratio_time = 240
			ratio_time = B738_rescale(0, 240, 1.0, 60, B738DR_freeze_effect)
		end
		ratio_tgt = 0.977
	--elseif B738DR_r_fwd_temp < 0.2 and was_right_frost == 1 then
	elseif was_right_frost == 1 then
		ratio_time = 240
	end
	if rain_acf > 0 or was_right_frost == 0 then
		if ratio_tgt == 0 then
			right_out_window_ratio = B738_set_animation_rate(right_out_window_ratio, ratio_tgt, 0, 1, ratio_time)
		else
			right_out_window_ratio = B738_set_animation_rate(right_out_window_ratio, 1, 0, 1, ratio_time)
		end
	end
	--if right_out_window_ratio == ratio_tgt or was_right_frost == 1 then
	if right_out_window_ratio == 1 or ratio_tgt == 0 or was_right_frost == 1 then
		right_out_window_ratio_t = B738_set_animation_rate(right_out_window_ratio_t, ratio_tgt, 0, 0.977, math.max(ratio_time, 2))
	end
	B738DR_window_effect_R = right_out_window_ratio_t
	
	if ratio_tgt == 0 then
		for k = 0, 8 do
			B738DR_window_effect_R02[k] = B738_set_animation_rate(B738DR_window_effect_R02[k], 0, 0, 1, ratio_time)
		end
		R02_timer = 0
		R02_cnt = 0
	elseif right_out_window_ratio > 0 then
		if right_out_window_ratio < 0.1 then
			B738DR_window_effect_R02[0] = 1
		elseif right_out_window_ratio < 0.2 then
			B738DR_window_effect_R02[0] = 1
			B738DR_window_effect_R02[1] = 1
		elseif right_out_window_ratio < 0.3 then
			B738DR_window_effect_R02[0] = 1
			B738DR_window_effect_R02[1] = 1
			B738DR_window_effect_R02[2] = 1
		elseif right_out_window_ratio < 0.4 then
			B738DR_window_effect_R02[0] = 1
			B738DR_window_effect_R02[1] = 1
			B738DR_window_effect_R02[2] = 1
			B738DR_window_effect_R02[3] = 1
		elseif right_out_window_ratio < 0.5 then
			B738DR_window_effect_R02[0] = 1
			B738DR_window_effect_R02[1] = 1
			B738DR_window_effect_R02[2] = 1
			B738DR_window_effect_R02[3] = 1
			B738DR_window_effect_R02[4] = 1
		elseif right_out_window_ratio < 0.6 then
			B738DR_window_effect_R02[0] = 1
			B738DR_window_effect_R02[1] = 1
			B738DR_window_effect_R02[2] = 1
			B738DR_window_effect_R02[3] = 1
			B738DR_window_effect_R02[4] = 1
			B738DR_window_effect_R02[5] = 1
		elseif right_out_window_ratio < 0.7 then
			B738DR_window_effect_R02[0] = 1
			B738DR_window_effect_R02[1] = 1
			B738DR_window_effect_R02[2] = 1
			B738DR_window_effect_R02[3] = 1
			B738DR_window_effect_R02[4] = 1
			B738DR_window_effect_R02[5] = 1
			B738DR_window_effect_R02[6] = 1
		elseif right_out_window_ratio < 0.8 then
			B738DR_window_effect_R02[0] = 1
			B738DR_window_effect_R02[1] = 1
			B738DR_window_effect_R02[2] = 1
			B738DR_window_effect_R02[3] = 1
			B738DR_window_effect_R02[4] = 1
			B738DR_window_effect_R02[5] = 1
			B738DR_window_effect_R02[6] = 1
			B738DR_window_effect_R02[7] = 1
		else
			R02_timer = R02_timer + SIM_PERIOD
			tmp2 = ratio_time / 10
			tmp2 = math.max(0.2, tmp2)
			if R02_timer > tmp2 then
				R02_timer = 0
				R02_cnt = R02_cnt + 1
				if R02_cnt > 7 then
					R02_cnt = 0
				end
				B738DR_window_effect_R02[R02_cnt] = 1
			end
			
			for k = 0, 7 do
				B738DR_window_effect_R02[k] = B738_set_animation_rate(B738DR_window_effect_R02[k], 0, 0, 1, 1)
			end
		end
		
		if right_out_window_ratio_t == ratio_tgt then
			B738DR_window_effect_R02[8] = B738_set_animation_rate(B738DR_window_effect_R02[8], 0.6, 0, 1, 2)
		else
			if right_out_window_ratio > 0.5 then
				B738DR_window_effect_R02[8] = B738_rescale(0.5, 0, 1, 1, right_out_window_ratio)
			end
		end
	end
	
	-----
	-- RIGHT SIDE --
	ratio_time = 40
	if rain_acf > 0 then
		ratio_tgt = ratio_time_rain
	else
		ratio_tgt = 0
	end
	if ratio_tgt < right_side_window_ratio then
		ratio_time = ratio_time * 2
	end
	if B738DR_r_side_temp < 0.2 and B738DR_freeze_effect <= 0.001 then	--and simDR_ground_speed < 70 then
		was_right_side_frost = 1
		if ratio_tgt < right_side_window_ratio_t then
			ratio_time = 480
		end
		ratio_tgt = 0.977
	elseif B738DR_r_side_temp < 0.2 and B738DR_freeze_effect > 0.001 then
		was_right_side_frost = 1
		if ratio_tgt < right_side_window_ratio_t then
			--ratio_time = 240
			ratio_time = B738_rescale(0, 240, 1.0, 60, B738DR_freeze_effect)
		end
		ratio_tgt = 0.977
	--elseif B738DR_r_side_temp < 0.2 and was_right_side_frost == 1 then
	elseif was_right_side_frost == 1 then
		ratio_time = 240
	else
		if ratio_time_rain >= right_side_window_ratio_t then
			was_right_side_frost = 0
		end
	end
	--if was_right_side_frost == 0 then
	if rain_acf > 0 or was_right_side_frost == 0 then
		right_side_window_ratio = B738_set_animation_rate(right_side_window_ratio, ratio_tgt, 0, 0.977, ratio_time)
	end
	if right_side_window_ratio == ratio_tgt or was_right_side_frost == 1 then
		right_side_window_ratio_t = B738_set_animation_rate(right_side_window_ratio_t, ratio_tgt, 0, 0.977, ratio_time)
	end
	
	
	B738DR_window_effect_RS02 = right_side_window_ratio_t
	tmp = math.min(right_side_window_ratio, 0.3)
	tmp = math.max(tmp, 0)
	if ratio_tgt < right_side_window_ratio then
		tmp2 = B738_rescale(0, 0, 0.21, 1, tmp)
		if tmp2 > right_side_window_ratio then
			B738DR_window_effect_RS = right_side_window_ratio
		else
			B738DR_window_effect_RS = tmp2
		end
	else
		B738DR_window_effect_RS = B738_rescale(0, 0, 0.3, 1, tmp)
	end
	
	-----
	-- LEFT IN --
	--
	for k = 0, 7 do
		ratio_time = ratio_time_rain3
		if rain_acf > 0 then
			if B738DR_left_wiper_ratio > wiper_up_table[k] and B738DR_left_wiper_up == 1 then
				animL_ratio_tgt[k] = 0
				animL_ratio_tgt_w[k] = 1
			elseif B738DR_left_wiper_ratio < wiper_dn_table[k] and B738DR_left_wiper_up == 0 then
				animL_ratio_tgt[k] = 0
				animL_ratio_tgt_w[k] = 1
			else
				animL_ratio_tgt[k] = ratio_time_rain2
			end
		else
			if was_left_frost == 0 then
				if B738DR_left_wiper_ratio > wiper_up_table[k] and B738DR_left_wiper_up == 1 then
					animL_ratio_tgt_w[k] = 1
				elseif B738DR_left_wiper_ratio < wiper_dn_table[k] and B738DR_left_wiper_up == 0 then
					animL_ratio_tgt_w[k] = 1
				end
			end
			animL_ratio_tgt[k] = 0
		end
		if animL_ratio_tgt[k] < animL_ratio[k] then
			ratio_time = ratio_time * 2.3
		end
		if B738DR_l_fwd_temp < 0.2 and B738DR_freeze_effect <= 0.001 then	--and simDR_ground_speed < 70 then
			was_left_frost = 1
			if animL_ratio_tgt[k] < animL_transp_ratio[k] or animL_ratio_tgt[k] == 0 then
				ratio_time = 480
				animL_ratio_tgt_w[k] = 0
			end
			animL_ratio_tgt[k] = 0.977
		elseif B738DR_l_fwd_temp < 0.2 and B738DR_freeze_effect > 0.001 then
			was_left_frost = 1
			if animL_ratio_tgt[k] < animL_transp_ratio[k] or animL_ratio_tgt[k] == 0 then
				--ratio_time = 240
				ratio_time = B738_rescale(0, 240, 1.0, 60, B738DR_freeze_effect)
				animL_ratio_tgt_w[k] = 0
			end
			animL_ratio_tgt[k] = 0.977
		--elseif B738DR_l_fwd_temp < 0.2 and was_left_frost == 1 then
		elseif was_left_frost == 1 then
			ratio_time = 240
			if ratio_time_rain2 >= animL_transp_ratio[k] then
				was_left_frost = 0
			end
		else
			if ratio_time_rain2 >= animL_transp_ratio[k] then
				was_left_frost = 0
			end
		end
		
		if rain_acf > 0 and animL_ratio_tgt_w[k] == 1 and was_left_frost == 0 then
			animL_ratio[k] = B738_set_animation_rate(animL_ratio[k], 0, 0, 1, 0.051)
			animL_transp_ratio[k] = B738_set_animation_rate(animL_transp_ratio[k], 0, 0, 0.977, 0.1)
			ratio_tgt2 = 0
		else
			ratio_tgt2 = 1
			if rain_acf > 0 or was_left_frost == 0 then
				if animL_ratio_tgt_w[k] == 1 and was_left_frost == 0 then
					animL_ratio[k] = B738_set_animation_rate(animL_ratio[k], 0, 0, 1, 0.051)
					animL_transp_ratio[k] = B738_set_animation_rate(animL_transp_ratio[k], 0, 0, 0.977, 0.1)
					ratio_tgt2 = 0
				elseif animL_ratio_tgt[k] == 0 then
					animL_ratio[k] = B738_set_animation_rate(animL_ratio[k], animL_ratio_tgt[k], 0, 1, ratio_time)
				else
					animL_ratio[k] = B738_set_animation_rate(animL_ratio[k], 1, 0, 1, ratio_time)
				end
			end
			--if animL_ratio[k] == animL_ratio_tgt[k] or was_left_frost == 1 then
			if animL_ratio[k] == 1 or animL_ratio_tgt[k] == 0 or was_left_frost == 1 then
				animL_transp_ratio[k] = B738_set_animation_rate(animL_transp_ratio[k], animL_ratio_tgt[k], 0, 0.977, math.max(ratio_time, 2))
			end
		end
		if animL_ratio[k] == 0 then
			animL_ratio_tgt_w[k] = 0
		end
		
		B738DR_window_effect_animL[k] = animL_transp_ratio[k]
		
		tmp = k * 9
		if ratio_tgt2 == 0 then
			for kk = 0, 8 do
				B738DR_window_effect_animL02[tmp+kk] = animL_ratio[k]
			end
			L_anim_timer[k] = -ratio_time
			L_anim_cnt[k] = 0
			rndm = math.random(0, 5)
			if rndm < 1 then
				for kkk = 0, 7 do
					rndm_L[k][kkk] = rndm_tab[0][kkk]
				end
			elseif rndm < 2 then
				for kkk = 0, 7 do
					rndm_L[k][kkk] = rndm_tab[1][kkk]
				end
			elseif rndm < 3 then
				for kkk = 0, 7 do
					rndm_L[k][kkk] = rndm_tab[2][kkk]
				end
			elseif rndm < 4 then
				for kkk = 0, 7 do
					rndm_L[k][kkk] = rndm_tab[3][kkk]
				end
			else
				for kkk = 0, 7 do
					rndm_L[k][kkk] = rndm_tab[4][kkk]
				end
			end
		elseif animL_ratio_tgt[k] == 0 then
			for kk = 0, 8 do
				B738DR_window_effect_animL02[tmp+kk] = B738_set_animation_rate(B738DR_window_effect_animL02[tmp+kk], 0, 0, 1, ratio_time)
			end
			L_anim_timer[k] = 0
			L_anim_cnt[k] = 0
		elseif animL_ratio[k] > 0 then
			if animL_ratio[k] < 0.111 then
				B738DR_window_effect_animL02[tmp+rndm_L[k][0]] = 1
			elseif animL_ratio[k] < 0.222 then
				B738DR_window_effect_animL02[tmp+rndm_L[k][0]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][1]] = 1
			elseif animL_ratio[k] < 0.333 then
				B738DR_window_effect_animL02[tmp+rndm_L[k][0]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][1]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][2]] = 1
			elseif animL_ratio[k] < 0.444 then
				B738DR_window_effect_animL02[tmp+rndm_L[k][0]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][1]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][2]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][3]] = 1
			elseif animL_ratio[k] < 0.555 then
				B738DR_window_effect_animL02[tmp+rndm_L[k][0]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][1]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][2]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][3]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][4]] = 1
			elseif animL_ratio[k] < 0.666 then
				B738DR_window_effect_animL02[tmp+rndm_L[k][0]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][1]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][2]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][3]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][4]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][5]] = 1
			elseif animL_ratio[k] < 0.777 then
				B738DR_window_effect_animL02[tmp+rndm_L[k][0]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][1]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][2]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][3]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][4]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][5]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][6]] = 1
			elseif animL_ratio[k] < 0.888 then
				B738DR_window_effect_animL02[tmp+rndm_L[k][0]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][1]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][2]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][3]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][4]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][5]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][6]] = 1
				B738DR_window_effect_animL02[tmp+rndm_L[k][7]] = 1
			else
				L_anim_timer[k] = L_anim_timer[k] + SIM_PERIOD
				tmp2 = ratio_time / 20
				tmp2 = math.max(0.2, tmp2)
				if L_anim_timer[k] > tmp2 then
					L_anim_timer[k] = 0
					L_anim_cnt[k] = L_anim_cnt[k] + 1
					if L_anim_cnt[k] > 7 then
						L_anim_cnt[k] = 0
					end
					B738DR_window_effect_animL02[tmp+L_anim_cnt[k]] = 1
				end
				
				for kk = 0, 7 do
					B738DR_window_effect_animL02[tmp+kk] = B738_set_animation_rate(B738DR_window_effect_animL02[tmp+kk], 0, 0, 1, 1)
				end
			end
			
			if animL_transp_ratio[k] == animL_ratio_tgt[k] then
				B738DR_window_effect_animL02[tmp+8] = B738_set_animation_rate(B738DR_window_effect_animL02[tmp+8], 0.6, 0, 1, 2)
			else
				if animL_ratio[k] > 0.5 then
					B738DR_window_effect_animL02[tmp+8] = B738_rescale(0.5, 0, 1, 1, animL_ratio[k])
				end
			end
		end
		
	end
	
	-----
	-- RIGHT IN --
	
	--
	for k = 0, 7 do
		ratio_time = ratio_time_rain3
		if rain_acf > 0 then
			if B738DR_right_wiper_ratio > wiper_up_table[k] and B738DR_right_wiper_up == 1 then
				animR_ratio_tgt[k] = 0
				animR_ratio_tgt_w[k] = 1
			elseif B738DR_right_wiper_ratio < wiper_dn_table[k] and B738DR_right_wiper_up == 0 then
				animR_ratio_tgt[k] = 0
				animR_ratio_tgt_w[k] = 1
			else
				animR_ratio_tgt[k] = ratio_time_rain2
			end
		else
			if was_right_frost == 0 then
				if B738DR_right_wiper_ratio > wiper_up_table[k] and B738DR_right_wiper_up == 1 then
					animR_ratio_tgt_w[k] = 1
				elseif B738DR_right_wiper_ratio < wiper_dn_table[k] and B738DR_right_wiper_up == 0 then
					animR_ratio_tgt_w[k] = 1
				end
			end
			animR_ratio_tgt[k] = 0
		end
		if animR_ratio_tgt[k] < animR_ratio[k] then
			ratio_time = ratio_time * 2.3
		end
		if B738DR_r_fwd_temp < 0.2 and B738DR_freeze_effect <= 0.001 then	--and simDR_ground_speed < 70 then
			was_right_frost = 1
			if animR_ratio_tgt[k] < animR_transp_ratio[k] or animR_ratio_tgt[k] == 0 then
				ratio_time = 480
				animR_ratio_tgt_w[k] = 0
			end
			animR_ratio_tgt[k] = 0.977
		elseif B738DR_r_fwd_temp < 0.2 and B738DR_freeze_effect > 0.001 then
			was_right_frost = 1
			if animR_ratio_tgt[k] < animR_transp_ratio[k] or animR_ratio_tgt[k] == 0 then
				--ratio_time = 240
				ratio_time = B738_rescale(0, 240, 1.0, 60, B738DR_freeze_effect)
				animR_ratio_tgt_w[k] = 0
			end
			animR_ratio_tgt[k] = 0.977
		--elseif B738DR_r_fwd_temp < 0.2 and was_right_frost == 1 then
		elseif was_right_frost == 1 then
			ratio_time = 240
			if ratio_time_rain2 >= animR_transp_ratio[k] then
				was_right_frost = 0
			end
		else
			if ratio_time_rain2 >= animR_transp_ratio[k] then
				was_right_frost = 0
			end
		end
		
		if rain_acf > 0 and animR_ratio_tgt_w[k] == 1 and was_right_frost == 0 then
			animR_ratio[k] = B738_set_animation_rate(animR_ratio[k], 0, 0, 1, 0.051)
			animR_transp_ratio[k] = B738_set_animation_rate(animR_transp_ratio[k], 0, 0, 0.977, 0.1)
			ratio_tgt2 = 0
		else
			ratio_tgt2 = 1
			if rain_acf > 0 or was_right_frost == 0 then
				if animR_ratio_tgt_w[k] == 1 and was_right_frost == 0 then
					animR_ratio[k] = B738_set_animation_rate(animR_ratio[k], 0, 0, 1, 0.051)
					animR_transp_ratio[k] = B738_set_animation_rate(animR_transp_ratio[k], 0, 0, 0.977, 0.1)
					ratio_tgt2 = 0
				elseif animR_ratio_tgt[k] == 0 then
					animR_ratio[k] = B738_set_animation_rate(animR_ratio[k], animR_ratio_tgt[k], 0, 1, ratio_time)
				else
					animR_ratio[k] = B738_set_animation_rate(animR_ratio[k], 1, 0, 1, ratio_time)
				end
			end
			--if animR_ratio[k] == animR_ratio_tgt[k] or was_right_frost == 1 then
			if animR_ratio[k] == 1 or animR_ratio_tgt[k] == 0 or was_right_frost == 1 then
				animR_transp_ratio[k] = B738_set_animation_rate(animR_transp_ratio[k], animR_ratio_tgt[k], 0, 0.977, math.max(ratio_time, 2))
			end
		end
		if animR_ratio[k] == 0 then
			animR_ratio_tgt_w[k] = 0
		end
		
		B738DR_window_effect_animR[k] = animR_transp_ratio[k]
		
		tmp = k * 9
		if ratio_tgt2 == 0 then
			for kk = 0, 8 do
				B738DR_window_effect_animR02[tmp+kk] = animR_ratio[k]
			end
			R_anim_timer[k] = -ratio_time
			R_anim_cnt[k] = 0
			rndm = math.random(0, 5)
			if rndm < 1 then
				for kkk = 0, 7 do
					rndm_R[k][kkk] = rndm_tab[0][kkk]
				end
			elseif rndm < 2 then
				for kkk = 0, 7 do
					rndm_R[k][kkk] = rndm_tab[1][kkk]
				end
			elseif rndm < 3 then
				for kkk = 0, 7 do
					rndm_R[k][kkk] = rndm_tab[2][kkk]
				end
			elseif rndm < 4 then
				for kkk = 0, 7 do
					rndm_R[k][kkk] = rndm_tab[3][kkk]
				end
			else
				for kkk = 0, 7 do
					rndm_R[k][kkk] = rndm_tab[4][kkk]
				end
			end
		elseif animR_ratio_tgt[k] == 0 then
			for kk = 0, 8 do
				B738DR_window_effect_animR02[tmp+kk] = B738_set_animation_rate(B738DR_window_effect_animR02[tmp+kk], 0, 0, 1, ratio_time)
			end
			R_anim_timer[k] = 0
			R_anim_cnt[k] = 0
		elseif animR_ratio[k] > 0 then
			if animR_ratio[k] < 0.111 then
				B738DR_window_effect_animR02[tmp+rndm_R[k][0]] = 1
			elseif animR_ratio[k] < 0.222 then
				B738DR_window_effect_animR02[tmp+rndm_R[k][0]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][1]] = 1
			elseif animR_ratio[k] < 0.333 then
				B738DR_window_effect_animR02[tmp+rndm_R[k][0]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][1]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][2]] = 1
			elseif animR_ratio[k] < 0.444 then
				B738DR_window_effect_animR02[tmp+rndm_R[k][0]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][1]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][2]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][3]] = 1
			elseif animR_ratio[k] < 0.555 then
				B738DR_window_effect_animR02[tmp+rndm_R[k][0]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][1]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][2]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][3]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][4]] = 1
			elseif animR_ratio[k] < 0.666 then
				B738DR_window_effect_animR02[tmp+rndm_R[k][0]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][1]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][2]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][3]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][4]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][5]] = 1
			elseif animR_ratio[k] < 0.777 then
				B738DR_window_effect_animR02[tmp+rndm_R[k][0]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][1]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][2]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][3]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][4]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][5]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][6]] = 1
			elseif animR_ratio[k] < 0.888 then
				B738DR_window_effect_animR02[tmp+rndm_R[k][0]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][1]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][2]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][3]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][4]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][5]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][6]] = 1
				B738DR_window_effect_animR02[tmp+rndm_R[k][7]] = 1
			else
				R_anim_timer[k] = R_anim_timer[k] + SIM_PERIOD
				tmp2 = ratio_time / 20
				tmp2 = math.max(0.2, tmp2)
				if R_anim_timer[k] > tmp2 then
					R_anim_timer[k] = 0
					R_anim_cnt[k] = R_anim_cnt[k] + 1
					if R_anim_cnt[k] > 7 then
						R_anim_cnt[k] = 0
					end
					B738DR_window_effect_animR02[tmp+R_anim_cnt[k]] = 1
				end
				
				for kk = 0, 7 do
					B738DR_window_effect_animR02[tmp+kk] = B738_set_animation_rate(B738DR_window_effect_animR02[tmp+kk], 0, 0, 1, 1)
				end
			end
			
			if animR_transp_ratio[k] == animR_ratio_tgt[k] then
				B738DR_window_effect_animR02[tmp+8] = B738_set_animation_rate(B738DR_window_effect_animR02[tmp+8], 0.6, 0, 1, 2)
			else
				if animR_ratio[k] > 0.5 then
					B738DR_window_effect_animR02[tmp+8] = B738_rescale(0.5, 0, 1, 1, animR_ratio[k])
				end
			end
		end
		
	end
	
	-- Cabin windows
	ratio_time = 40
	if rain_acf > 0 then
		ratio_tgt = ratio_time_rain
	else
		ratio_tgt = 0
	end
	if ratio_tgt < cabin_window_ratio then
		ratio_time = ratio_time * 2
	end
	cabin_window_ratio = B738_set_animation_rate(cabin_window_ratio, ratio_tgt, 0, 0.977, ratio_time)
	tmp = math.min(cabin_window_ratio, 0.3)
	tmp = math.max(tmp, 0)
	if ratio_tgt < cabin_window_ratio then
		tmp2 = B738_rescale(0, 0, 0.21, 1, tmp)
		if tmp2 > cabin_window_ratio then
			B738DR_window_effect_cabin = cabin_window_ratio
		else
			B738DR_window_effect_cabin = tmp2
		end
	else
		B738DR_window_effect_cabin = B738_rescale(0, 0, 0.3, 1, tmp)
	end
	
end

function hud_displ()
	
	if simDR_int_ver < 120000 then
		local hud_displ_disable = 0
		local hud_displ_vr_disable = 0
		local hud_displ_x_min = 0
		local hud_displ_x_max = 0
		local hud_displ_y_min = 0
		local hud_displ_y_max = 0
		
		if B738DR_hud_disable == 1 then
			hud_displ_vr_disable = 1
			hud_displ_disable = 1
		elseif simDR_vr_enable == 0 then
			hud_displ_vr_disable = 1
		else
			hud_displ_disable = 1
			if B738DR_hud_stow > 0.12 then
				hud_displ_vr_disable = 1
			elseif simDR_pez < -15.98 or simDR_pez > -15.47 then
				hud_displ_vr_disable = 1
			else
				-- hud_displ_y_min = B738_rescale(-15.98, 0.626, -15.47, 0.684, simDR_pez)
				-- hud_displ_y_max = B738_rescale(-15.98, 0.780, -15.47, 0.745, simDR_pez)
				hud_displ_y_min = B738_rescale(-15.98, 0.931, -15.47, 0.989, simDR_pez)
				hud_displ_y_max = B738_rescale(-15.98, 1.085, -15.47, 1.050, simDR_pez)
				if simDR_pey < hud_displ_y_min or simDR_pey > hud_displ_y_max then
					hud_displ_vr_disable = 1
				else
					hud_displ_x_min = B738_rescale(-15.98, -0.632, -15.47, -0.553, simDR_pez)
					hud_displ_x_max = B738_rescale(-15.98, -0.402, -15.47, -0.476, simDR_pez)
					if simDR_pex < hud_displ_x_min or simDR_pex > hud_displ_x_max then
						hud_displ_vr_disable = 1
					end
				end
			end
		end
		B738DR_hud_displ_disable = hud_displ_disable
		B738DR_hud_displ_vr_disable = hud_displ_vr_disable
	else
		if B738DR_hud_stow > 0.12 then
			if simDR_HUD_on ~= 0 then
				simDR_HUD_on = 0
			end
		else
			if simDR_HUD_on == 0 then
				simDR_HUD_on = 1
			end
		end
	end
end

function B738_vr_rain(input_rain_ratio)
	
	--local vr_rain_ratio = B738DR_rain_ratio
	local vr_rain_ratio = 0
	local vr_input_rain = 0
	local rain_rate = 0
	local rain_temp = 1.2
	local gnd_spd = 0
	
	if simDR_inside[0] ~= 0 then
		gnd_spd = simDR_ground_speed
		gnd_spd = math.min(gnd_spd, 80)
		gnd_spd = math.max(gnd_spd, 5)
		
		if input_rain_ratio < 0.2 and input_rain_ratio > 0.05 then
			rain_temp = math.floor(20 / input_rain_ratio) / 100
		end
		vr_input_rain = input_rain_ratio * B738_rescale(5, 1, 80, rain_temp, gnd_spd)
		--vr_input_rain = input_rain_ratio * B738_rescale(5, 1, 80, 1.2, gnd_spd)
		vr_input_rain = math.min(vr_input_rain, 1)
		
		if vr_input_rain < 0.20 then
			vr_rain_ratio = B738_rescale(0, 0, 0.20, 0.5, vr_input_rain)
		else
			vr_rain_ratio = B738_rescale(0.20, 0.5, 1.0, 1, vr_input_rain)
		end
	end
	
	if vr_rain_ratio < 0.5 then
		rain_rate = vr_rain_ratio
		B738DR_rain_size = 0.1
		B738DR_rain_life = B738_rescale(0, 0.8, 0.5, 0.5, vr_rain_ratio)
		B738DR_rain_speed = B738_rescale(0, 0, 0.5, 0.3, vr_rain_ratio)
	elseif vr_rain_ratio < 0.7 then
		rain_rate = B738_rescale(0.5, 0.5, 0.7, 1, vr_rain_ratio)
		B738DR_rain_size = B738_rescale(0.5, 0.1, 0.7, 0.4, vr_rain_ratio)
		B738DR_rain_life = B738_rescale(0.5, 0.5, 0.7, 0.5, vr_rain_ratio)
		B738DR_rain_speed = B738_rescale(0.5, 0.3, 0.7, 0.5, vr_rain_ratio)
	else
		rain_rate = 1
		B738DR_rain_size = B738_rescale(0.5, 0.4, 0.7, 0.9, vr_rain_ratio)
		B738DR_rain_life = 0.5
		B738DR_rain_speed = 0.5
	end
	
	if rain_rate ~= 0 then
		local loc_accel = math.max(math.abs(simDR_ax), math.abs(simDR_ay), math.abs(simDR_az))
		if loc_accel > 0.02 then
			if B738DR_rain_life > 0.5 then
				B738DR_rain_life = 0.5
				rain_rate = math.min(rain_rate * 2.5, 1)
				B738DR_rain_speed = math.min(B738DR_rain_speed * 2.5, 0.5)
				B738DR_rain_size = math.min(B738DR_rain_size * 1.5, 0.9)
			end
		end
		
		local wiper_spd_pos = 30
		local wiper_spd_pos2 = 50
		if B738DR_left_wiper_pos == 3 then
			wiper_spd_pos = 65
			wiper_spd_pos2 = 35
		end
		
		if left_new_cycle == 0 and B738DR_left_wiper_up == 1 and B738DR_left_wiper_ratio < 15 then
			left_new_cycle = 1
		elseif left_new_cycle == 0 and B738DR_left_wiper_up == 0 and B738DR_left_wiper_ratio > 65 then
			left_new_cycle = 1
		elseif B738DR_left_wiper_up == -1 
		or (B738DR_left_wiper_up == 1 and B738DR_left_wiper_ratio > wiper_spd_pos) 
		or (B738DR_left_wiper_up == 0 and B738DR_left_wiper_ratio < wiper_spd_pos2) then
			left_new_cycle = 0
		end
		
		--if B738DR_left_wiper_ratio < 5 and B738DR_left_wiper_up == 1 then
		if left_new_cycle == 1 then
			left_new_cycle = 2
			left_clr_timer = 0
			left_clr_timer_trig = B738_rescale(0.5, 0.8, 2, 2.5, math.max(B738DR_rain_life)) + SIM_PERIOD
		elseif left_new_cycle == 0 then
			left_clr_timer = 10
			left_clr_timer_trig = 10
		end
		
		wiper_spd_pos = 30
		wiper_spd_pos2 = 50
		if B738DR_right_wiper_pos == 3 then
			wiper_spd_pos = 65
			wiper_spd_pos2 = 35
		end
		
		if right_new_cycle == 0 and B738DR_right_wiper_up == 1 and B738DR_right_wiper_ratio < 15 then
			right_new_cycle = 1
		elseif right_new_cycle == 0 and B738DR_right_wiper_up == 0 and B738DR_right_wiper_ratio > 65 then
			right_new_cycle = 1
		elseif B738DR_right_wiper_up == -1 
		or (B738DR_right_wiper_up == 1 and B738DR_right_wiper_ratio > wiper_spd_pos) 
		or (B738DR_right_wiper_up == 0 and B738DR_right_wiper_ratio < wiper_spd_pos2) then
			right_new_cycle = 0
		end
		
		--if B738DR_right_wiper_ratio < 5 and B738DR_right_wiper_up == 1 then
		if right_new_cycle == 1 then
			right_new_cycle = 2
			right_clr_timer = 0
			right_clr_timer_trig = B738_rescale(0.5, 0.8, 2, 2.5, math.max(B738DR_rain_life)) + SIM_PERIOD
		elseif right_new_cycle == 0 then
			right_clr_timer = 10
			right_clr_timer_trig = 10
		end
		if left_clr_timer < left_clr_timer_trig then
			left_clr_timer = left_clr_timer + SIM_PERIOD
			B738DR_rain_rate1 = 0
		else
			B738DR_rain_rate1 = rain_rate
		end
		if right_clr_timer < right_clr_timer_trig then
			right_clr_timer = right_clr_timer + SIM_PERIOD
			B738DR_rain_rate2 = 0
		else
			B738DR_rain_rate2 = rain_rate
		end
		--gnd_spd = math.min(gnd_spd, 40)
		--B738DR_rain_angle_left = B738_rescale(5, -90, 40, -30, gnd_spd)
		--B738DR_rain_angle_right = B738_rescale(5, -90, 40, -30, gnd_spd)
	else
		B738DR_rain_rate1 = 0
		B738DR_rain_rate2 = 0
	end
	
				-- B738DR_window_effect_LS = 1	-- rain drop
				-- B738DR_window_effect_LS02 = 0.3	-- vanish
				-- B738DR_window_effect_RS = 1	-- rain drop
				-- B738DR_window_effect_RS02 = 0.3	-- vanish
				-- for h = 0, 7 do
					-- B738DR_window_effect_animL[h] = 0.3	-- vanish wiper
					-- B738DR_window_effect_animR[h] = 0.3	-- vanish wiper
				-- end
				-- B738DR_window_effect_L = 0.3	-- vanish outside
				-- B738DR_window_effect_R = 0.3	-- vanish outside
				
	local ratio_tgt = 0
	local ratio_time = 0
	local ratio_vanish = math.min(vr_input_rain, 0.2)
	--local ratio_drop = math.min(vr_input_rain, 0.2)
	
	if vr_input_rain ~= 0 then
		ratio_tgt = B738_rescale(0, 0, 0.2, 0.3, ratio_vanish)
	end
	ratio_time = B738_rescale(0, 60, 1, 6, vr_input_rain)
	if ratio_tgt == 0 then
		ratio_time = 60
	elseif ratio_tgt < left_out_window_ratio_t then
		ratio_time = ratio_time * 2
	end
	left_out_window_ratio_t = B738_set_animation_rate(left_out_window_ratio_t, ratio_tgt, 0, 0.3, ratio_time)
	B738DR_window_effect_LS02 = left_out_window_ratio_t
	B738DR_window_effect_RS02 = left_out_window_ratio_t
	
	B738DR_window_effect_LS = B738_rescale(0, 0, 0.3, 1, math.min(left_out_window_ratio_t, 0.3))
	B738DR_window_effect_RS = B738DR_window_effect_LS
	
	ratio_tgt = 0
	ratio_time = 0
	if vr_input_rain ~= 0 then
		ratio_tgt = B738_rescale(0, 0, 0.2, 0.3, ratio_vanish)
	end
	ratio_time = B738_rescale(0, 20, 1, 2, vr_input_rain)
	if ratio_tgt == 0 then
		ratio_time = 60
	elseif ratio_tgt < right_out_window_ratio_t then
		ratio_time = ratio_time * 2
	end
	right_out_window_ratio_t = B738_set_animation_rate(right_out_window_ratio_t, ratio_tgt, 0, 0.3, ratio_time)
	B738DR_window_effect_L = right_out_window_ratio_t
	B738DR_window_effect_R = right_out_window_ratio_t
	
	
	for h = 0, 7 do
		if ratio_vanish > 0 then
			if B738DR_left_wiper_ratio > wiper_up_table[h] and B738DR_left_wiper_up == 1 then
				animL_ratio_tgt[h] = 0
				animL_ratio_tgt_w[h] = 1
			elseif B738DR_left_wiper_ratio < wiper_dn_table[h] and B738DR_left_wiper_up == 0 then
				animL_ratio_tgt[h] = 0
				animL_ratio_tgt_w[h] = 1
			else
				animL_ratio_tgt[h] = B738_rescale(0, 0, 0.2, 0.3, ratio_vanish)
			end
		else
			animL_ratio_tgt[h] = 0
		end
		if animL_ratio_tgt_w[h] == 1 then
			animL_transp_ratio[h] = B738_set_animation_rate(animL_transp_ratio[h], 0, 0, 0.3, 0.1)
		else
			ratio_time = B738_rescale(0, 30, 1, 2, vr_input_rain)
			if animL_ratio_tgt[h] == 0 then
				ratio_time = 60
			elseif animL_ratio_tgt[h] < left_out_window_ratio_t then
				ratio_time = ratio_time * 2
			end
			animL_transp_ratio[h] = B738_set_animation_rate(animL_transp_ratio[h], animL_ratio_tgt[h], 0, 0.3, ratio_time)
		end
		if animL_transp_ratio[h] == 0 then
			animL_ratio_tgt_w[h] = 0
		end
		
		B738DR_window_effect_animL[h] = animL_transp_ratio[h]
		
		if ratio_vanish > 0 then
			if B738DR_right_wiper_ratio > wiper_up_table[h] and B738DR_right_wiper_up == 1 then
				animR_ratio_tgt[h] = 0
				animR_ratio_tgt_w[h] = 1
			elseif B738DR_right_wiper_ratio < wiper_dn_table[h] and B738DR_right_wiper_up == 0 then
				animR_ratio_tgt[h] = 0
				animR_ratio_tgt_w[h] = 1
			else
				animR_ratio_tgt[h] = B738_rescale(0, 0, 0.2, 0.3, ratio_vanish)
			end
		else
			animR_ratio_tgt[h] = 0
		end
		if animR_ratio_tgt_w[h] == 1 then
			animR_transp_ratio[h] = B738_set_animation_rate(animR_transp_ratio[h], 0, 0, 0.3, 0.1)
		else
			ratio_time = B738_rescale(0, 40, 1, 2, vr_input_rain)
			if animR_ratio_tgt[h] == 0 then
				ratio_time = 60
			elseif animR_ratio_tgt[h] < left_out_window_ratio_t then
				ratio_time = ratio_time * 2
			end
			animR_transp_ratio[h] = B738_set_animation_rate(animR_transp_ratio[h], animR_ratio_tgt[h], 0, 0.3, ratio_time)
		end
		if animR_transp_ratio[h] == 0 then
			animR_ratio_tgt_w[h] = 0
		end
		
		B738DR_window_effect_animR[h] = animR_transp_ratio[h]
	end
end

function clr_wndshld()
	
	local h = 0
	
	left_out_window_ratio = 0
	right_out_window_ratio = 0
	left_side_window_ratio = 0
	right_side_window_ratio = 0
	left_out_window_ratio_t = 0
	right_out_window_ratio_t = 0
	left_side_window_ratio_t = 0
	right_side_window_ratio_t = 0
	cabin_window_ratio = 0
	was_left_frost = 0
	was_right_frost = 0
	was_left_side_frost = 0
	was_right_side_frost = 0
	timer_rain = 0
	
	for h = 0, 71 do
		B738DR_window_effect_animL02[h] = 0
		B738DR_window_effect_animR02[h] = 0
		if h < 8 then
			B738DR_window_effect_animL[h] = 0
			B738DR_window_effect_animR[h] = 0
			animL_ratio_tgt_w[h] = 0
			animR_ratio_tgt_w[h] = 0
			animL_transp_ratio[h] = 0
			animR_transp_ratio[h] = 0
			animL_ratio[h] = 0
			animR_ratio[h] = 0
			B738DR_window_effect_L02[h] = 0
			B738DR_window_effect_R02[h] = 0
		elseif h < 9 then
			B738DR_window_effect_L02[h] = 0
			B738DR_window_effect_R02[h] = 0
		end
	end
	B738DR_window_effect_LS = 0
	B738DR_window_effect_RS = 0
	
	B738DR_window_effect_L = 0
	B738DR_window_effect_R = 0
	B738DR_window_effect_LS02 = 0
	B738DR_window_effect_RS02 = 0
end

function B738_ski()
	local ratio_tgt = 0
	local ratio_time = 0
	
	-- left out
	if B738DR_l_fwd_temp < 0.2 and B738DR_freeze_effect <= 0.001 then
		ratio_tgt = 0
		ratio_time = 480
	elseif B738DR_l_fwd_temp < 0.2 and B738DR_freeze_effect > 0.001 then
		ratio_time = 240
		ratio_tgt = 0.977
	else
		ratio_tgt = 0
		ratio_time = 180
	end
	left_out_window_ratio_t = B738_set_animation_rate(left_out_window_ratio_t, ratio_tgt, 0, 0.977, ratio_time)
	B738DR_window_effect_L = left_out_window_ratio_t
	
	-- right out
	if B738DR_r_fwd_temp < 0.2 and B738DR_freeze_effect <= 0.001 then
		ratio_tgt = 0
		ratio_time = 480
	elseif B738DR_r_fwd_temp < 0.2 and B738DR_freeze_effect > 0.001 then
		ratio_time = 240
		ratio_tgt = 0.977
	else
		ratio_tgt = 0
		ratio_time = 180
	end
	right_out_window_ratio_t = B738_set_animation_rate(right_out_window_ratio_t, ratio_tgt, 0, 0.977, ratio_time)
	B738DR_window_effect_R = right_out_window_ratio_t
	
	-- left in
	for k = 0, 7 do
		B738DR_window_effect_animL[k] = left_out_window_ratio_t
		B738DR_window_effect_animR[k] = right_out_window_ratio_t
	end
	
	-- LEFT SIDE --
	if B738DR_l_side_temp < 0.2 and B738DR_freeze_effect <= 0.001 then
		ratio_tgt = 0
		ratio_time = 480
	elseif B738DR_l_side_temp < 0.2 and B738DR_freeze_effect > 0.001 then
		-- ratio_time = B738_rescale(0, 240, 1.0, 60, B738DR_freeze_effect)
		ratio_time = 240
		ratio_tgt = 0.977
	else
		ratio_tgt = 0
		ratio_time = 180
	end
	left_side_window_ratio_t = B738_set_animation_rate(left_side_window_ratio_t, ratio_tgt, 0, 0.977, ratio_time)
	B738DR_window_effect_LS02 = left_side_window_ratio_t
	
	-- RIGHT SIDE --
	if B738DR_r_side_temp < 0.2 and B738DR_freeze_effect <= 0.001 then
		ratio_tgt = 0
		ratio_time = 480
	elseif B738DR_r_side_temp < 0.2 and B738DR_freeze_effect > 0.001 then
		ratio_time = 240
		ratio_tgt = 0.977
	else
		ratio_tgt = 0
		ratio_time = 180
	end
	right_side_window_ratio_t = B738_set_animation_rate(right_side_window_ratio_t, ratio_tgt, 0, 0.977, ratio_time)
	B738DR_window_effect_RS02 = right_side_window_ratio_t
	
	--*****************************************************************************************************
	
end

function B738_moster_freeze()
	B738DR_freeze_monster = B738DR_monster_icing * 0.9
	if simDR_rel_eng_ice[0] < 0.2 then
		B738DR_freeze_eng1 = B738_rescale(0, 0, 200, 0.5, math.min(simDR_rel_eng_ice[0] * 1000, 500))
	else
		B738DR_freeze_eng1 = B738_rescale(200, 0.5, 500, 0.9, math.min(simDR_rel_eng_ice[0] * 1000, 500))
	end
	if simDR_rel_eng_ice[1] < 0.2 then
		B738DR_freeze_eng2 = B738_rescale(0, 0, 200, 0.5, math.min(simDR_rel_eng_ice[1] * 1000, 500))
	else
		B738DR_freeze_eng2 = B738_rescale(200, 0.5, 500, 0.9, math.min(simDR_rel_eng_ice[1] * 1000, 500))
	end
	if simDR_rel_wing_ice < 0.005 then
		B738DR_freeze_wing1 = 0
	elseif simDR_rel_wing_ice < 0.2 then
		B738DR_freeze_wing1 = B738_rescale(5, 0, 200, 0.5, math.min(simDR_rel_wing_ice * 1000, 500))
	else
		B738DR_freeze_wing1 = B738_rescale(200, 0.5, 500, 0.9, math.min(simDR_rel_wing_ice * 1000, 500))
	end
	if simDR_rel_wing_ice2 < 0.005 then
		B738DR_freeze_wing2 = 0
	elseif simDR_rel_wing_ice2 < 0.2 then
		B738DR_freeze_wing2 = B738_rescale(5, 0, 200, 0.5, math.min(simDR_rel_wing_ice2 * 1000, 500))
	else
		B738DR_freeze_wing2 = B738_rescale(200, 0.5, 500, 0.9, math.min(simDR_rel_wing_ice2 * 1000, 500))
	end
	
	if B738DR_freeze_eng1 == 0 and B738DR_freeze_eng2 == 0 then
		B738DR_eng_ice_hide = 1
	else
		B738DR_eng_ice_hide = 0
	end
	
	if B738DR_freeze_wing1 == 0 and B738DR_freeze_wing2 == 0 then
		B738DR_wing_ice_hide = 1
	else
		B738DR_wing_ice_hide = 0
	end
	
end


--function aircraft_load()
--end

--function aircraft_unload() end

function flight_start()
	
	clr_wndshld()
	B738DR_rain_rate1 = 0
	B738DR_rain_rate2 = 0
	
	B738DR_align_horizon = 0	--26		-- basic multiplier up/down
	B738DR_align_horizon2 = 0	---60		-- move up/down by head position
	B738DR_align_horizon3 = 0	--2		-- multiplier by angle
	B738DR_hud_displ_disable = 1
	hud_mode = 0
	load_plane = 0
	
	left_clr_timer = 10
	right_clr_timer = 10
	left_clr_timer_trig = 10
	right_clr_timer_trig = 10
	left_new_cycle = 0
	right_new_cycle = 0
	cleared_wndshld = 2
	B738DR_holdover_time = 0
	B738DR_enable_librain_vr = 1
	first_ttt = 0
end

--function flight_crash() end

--function before_physics() end

function after_physics()

	local h = 0
	local tmp = 0
	
	hud_displ()
	--B738DR_horizon = (simDR_theta * B738DR_align_horizon) + B738DR_align_horizon2 + (simDR_phi * B738DR_align_horizon3)
	--B738DR_align_horizon = (-simDR_theta * 20.5) - 18.5
	if simDR_radio_height_pilot_ft < 2500 then
		hud_mode = 1
	end
	if simDR_radio_height_pilot_ft > 3000 then
		hud_mode = 0
	end
	hud_mode = 1
	if hud_mode == 0 then
		B738DR_align_horizon = 0
	else
		--B738DR_align_horizon = (-simDR_pitch_elec * 20.5 * B738DR_align_horizon3) - 18.5
		--B738DR_align_horizon = (-simDR_theta * 20.5) - 18.5
		B738DR_align_horizon = (-simDR_theta * B738DR_align_horizon2) + B738DR_align_horizon3
		--B738DR_align_horizon = ((simDR_pitch_elec - simDR_vpath) * B738DR_align_horizon2) + B738DR_align_horizon3
		B738DR_align_horizon = math.max(B738DR_align_horizon, -205)
		B738DR_align_horizon = math.min(B738DR_align_horizon, 0)
	end
	
	if simDR_int_ver < 120000 then
		if first_ttt == 0 then
			SkiDR_init = 0
		end
		
		if cleared_wndshld == 2 then
			cleared_wndshld = 0
			clr_wndshld()
		end
		
		if B738DR_b737_variant >= 0 then
			if B738DR_kill_windshield < 3 then
				B738DR_kill_windshield = 2
			else
				B738DR_kill_windshield = 3
			end
		end
		
		if B738DR_kill_windshield < 3 then
			if B738DR_kill_windshield == 0 then
				precip_acf_ratio = simDR_rain_acf_ratio
			else
				if XE_DR_active == 0 then
					precip_acf_ratio = XE_DR_rain2 * 0.2
				else
					precip_acf_ratio = math.max(XE_DR_rain, XE_DR_snow) * 0.2
				end
			end
			
			if load_plane == 0 then
				load_plane = 1
			else
				
				local enable_librain_vr = 0
				if simDR_vr_enable == 0 then
					enable_librain_vr = 1
				else
					if B738DR_enable_librain_vr ~= 0 then
						enable_librain_vr = 1
					end
				end
				--if simDR_vr_enable == 0 or B738DR_particle_rain == 0 then
				if B738DR_particle_rain == 0 or enable_librain_vr == 1 then
					--if B738DR_kill_windshield == 2 and simDR_vr_enable == 0 then
					if B738DR_kill_windshield == 2 and enable_librain_vr == 1 then
						if cleared_wndshld == 0 then
							cleared_wndshld = 1
							clr_wndshld()
						end
						B738_ski()
					else
						if cleared_wndshld == 1 then
							cleared_wndshld = 0
							clr_wndshld()
						end
						rain_triger()
						window_ratio()
						if timer_rain > rain_ratio then
							timer_rain = 0
						end
						B738DR_rain_rate1 = 0
						B738DR_rain_rate2 = 0
						SkiDR_init = 0
					end
				else
					if cleared_wndshld == 0 then
						cleared_wndshld = 1
						clr_wndshld()
					end
					--precip_acf_ratio = B738DR_rain_angle_left
					B738_vr_rain(precip_acf_ratio)
					SkiDR_init = 0
				end
			end
		else
			if cleared_wndshld == 0 then
				cleared_wndshld = 1
				clr_wndshld()
			end
			B738DR_rain_rate1 = 0
			B738DR_rain_rate2 = 0
			left_clr_timer = 10
			right_clr_timer = 10
			left_clr_timer_trig = 10
			right_clr_timer_trig = 10
			SkiDR_init = 0
		end
	else
		B738DR_kill_windshield = 3
	end
	B738_moster_freeze()
	first_ttt = 1
end

function after_replay()
	B738_moster_freeze()
end



--*************************************************************************************--
--** 				               SUB-MODULE PROCESSING       	        			 **--
--*************************************************************************************--

-- dofile("")



