-- librain.lua

--[[
 Librain plugin for the Freeware DHC6X Twin Otter Series by Pedrovl, Syd Adams, G.P. Lyons (gplv3), Leen de Jager (flybike), AFNavarro (Antonio), and Bob 439 (Bob Maheu) by Paul Blair (toadlife) - toadlife@gmail.com
 Version 1.0 - 2022-04-30
 
 Thank you to:
 
 * Pedrovl, Syd Adams, G.P. Lyons (gplv3), Leen de Jager (flybike), AFNavarro (Antonio), and Bob 439 (Bob Maheu) for the fantastic freeware plane
 * Lionel for the SASL/librain plugin tutorial (https://forums.x-plane.org/index.php?/forums/topic/186573-your-first-sasl-plugin-configuring-librain-plugin-for-dr401/)
 * Saso Kiselkov for the librain plugin
 * My son Christian for the (multiple) Blender lessons

]]--

-- check if librain.plugin is present, otherwise bail out
-- in both case send message to log file
if findPluginBySignature("skiselkov.librain") == NO_PLUGIN_ID then
	logInfo("librain.plugin by Saso Kiselkov not found, disabling rain effects")
	return
end
logInfo("librain.plugin by Saso Kiselkov found")


-- prefix for objects used by librain
local obj_path	= getAircraftPath() .. "/objects/librain/"
local acf_tailnum = get(globalProperty("sim/aircraft/view/acf_tailnum"))
local mainmask = "mask.obj"
local sidewindows = "side_windows.obj"

if (string.find(acf_tailnum, "DHC63L")) then
  mainmask = "mask_DHC63L.obj"
  sidewindows = "side_windows_DHC63L.obj"
end
	
-- pairs of dataref/value
local drf	= {
	"librain/verbose"			, 0,
	"librain/debug_draw"		, 0,
	"librain/wipers_visible"	, 0,
	
-- port windscreen
	"librain/glass_0/gravity_factor"	, 1,
	"librain/glass_0/gravity_point/x"	, 0.1,
	"librain/glass_0/gravity_point/y"	, 2,
	"librain/glass_0/max_tas"			, 100,
	"librain/glass_0/max_thrust"		, 0,
	"librain/glass_0/slant_factor"		,  0.7,
	"librain/glass_0/thrust_factor"		,  0,
	"librain/glass_0/thrust_point/x"	,  0.5,
	"librain/glass_0/thrust_point/y"	, -2,
	"librain/glass_0/wind_factor"		,  1.0,
	"librain/glass_0/wind_normal"		,  0.9,
	"librain/glass_0/wind_point/x"		,  0.5,
	"librain/glass_0/wind_point/y"		, -2,
	"librain/glass_0/obj/filename"		, obj_path .. "port_windscreen.obj",
	"librain/glass_0/obj/pos_offset/x"	,  0,
	"librain/glass_0/obj/pos_offset/y"	,  0,
	"librain/glass_0/obj/pos_offset/z"	, 0,
	"librain/glass_0/wiper_0/pivot/x"    ,  0.2,
	"librain/glass_0/wiper_0/pivot/y"    ,  0,
	"librain/glass_0/wiper_0/radius_inner", 0.40,
	"librain/glass_0/wiper_0/radius_outer", .74,
	"librain/glass_0/wiper_0/angle"      ,  0,
	"librain/glass_0/wiper_0/moving"     ,  1,
	"librain/glass_0/obj/load"			,  1,
	
-- starboard windscreen
	"librain/glass_1/gravity_factor"	, 1,
	"librain/glass_1/gravity_point/x"	, 0.9,
	"librain/glass_1/gravity_point/y"	, 2.0,
	"librain/glass_1/max_tas"			, 100,
	"librain/glass_1/max_thrust"		, 0,
	"librain/glass_1/slant_factor"		,  0.7,
	"librain/glass_1/thrust_factor"		,  0,
	"librain/glass_1/thrust_point/x"	,  0.5,
	"librain/glass_1/thrust_point/y"	, -2,
	"librain/glass_1/wind_factor"		,  1.0,
	"librain/glass_1/wind_normal"		,  0.9,
	"librain/glass_1/wind_point/x"		,  0.5,
	"librain/glass_1/wind_point/y"		, -2,
	"librain/glass_1/obj/filename"		, obj_path .. "starboard_windscreen.obj",
	"librain/glass_1/obj/pos_offset/x"	,  0,
	"librain/glass_1/obj/pos_offset/y"	,  0,
	"librain/glass_1/obj/pos_offset/z"	, 0,
	"librain/glass_1/wiper_0/pivot/x"    ,  0.7,
	"librain/glass_1/wiper_0/pivot/y"    ,  0,
	"librain/glass_1/wiper_0/radius_inner", 0.40,
	"librain/glass_1/wiper_0/radius_outer", 0.74,
	"librain/glass_1/wiper_0/angle"      ,  0,
	"librain/glass_1/wiper_0/moving"     ,  1,
	"librain/glass_1/obj/load"			,  1,

	
-- side windows

	"librain/glass_2/gravity_factor"	, 1,
	"librain/glass_2/gravity_point/x"	, 0.5,
	"librain/glass_2/gravity_point/y"	, 3.5,
	"librain/glass_2/max_thrust"		, 0,
	"librain/glass_2/slant_factor"		,  0.3,
	"librain/glass_2/thrust_factor"		,  0.1,
	"librain/glass_2/thrust_point/x"	,  1.5,
	"librain/glass_2/thrust_point/y"	, 0.5,
	"librain/glass_2/wind_factor"		,  1.0,
	"librain/glass_2/wind_normal"		,  0.2,
	"librain/glass_2/wind_point/x"		,  1.5,
	"librain/glass_2/wind_point/y"		, 0.0,
	"librain/glass_2/max_tas"			, 100,
	"librain/glass_2/obj/filename"		, obj_path .. sidewindows,
	"librain/glass_2/obj/pos_offset/x"	,  0,
	"librain/glass_2/obj/pos_offset/y"	,  0,
	"librain/glass_2/obj/pos_offset/z"	, 0,
	"librain/glass_2/obj/load"			,  1,
	
	"librain/z_depth_obj_0/filename"	, obj_path .. mainmask,
	"librain/z_depth_obj_0/pos_offset/x",  0,
	"librain/z_depth_obj_0/pos_offset/y",  0,
	"librain/z_depth_obj_0/pos_offset/z", 0,
	"librain/z_depth_obj_0/load"		,  1,

	"librain/z_depth_obj_1/filename"	, obj_path .. "copilot_window_mask.obj",
	"librain/z_depth_obj_1/pos_offset/x",  0,
	"librain/z_depth_obj_1/pos_offset/y",  0,
	"librain/z_depth_obj_1/pos_offset/z", 0,
	"librain/z_depth_obj_1/load"		,  0,
	
	"librain/z_depth_obj_2/filename"	, obj_path .. "pilot_window_mask.obj",
	"librain/z_depth_obj_2/pos_offset/x",  0,
	"librain/z_depth_obj_2/pos_offset/y",  0,
	"librain/z_depth_obj_2/pos_offset/z", 0,
	"librain/z_depth_obj_2/load"		,  0,
	
	"librain/z_depth_obj_3/filename"	, obj_path .. "rear_port_left_door_mask.obj",
	"librain/z_depth_obj_3/pos_offset/x",  0.015000,
	"librain/z_depth_obj_3/pos_offset/y",  0.160000,
	"librain/z_depth_obj_3/pos_offset/z", 0.010000,
	"librain/z_depth_obj_3/load"		,  0,
	
	"librain/z_depth_obj_4/filename"	, obj_path .. "rear_port_right_door_mask.obj",
	"librain/z_depth_obj_4/pos_offset/x",  0.030000,
	"librain/z_depth_obj_4/pos_offset/y",  0.160000,
	"librain/z_depth_obj_4/pos_offset/z", 0.010000,
	"librain/z_depth_obj_4/load"		,  0,
	
	"librain/z_depth_obj_5/filename"	, obj_path .. "rear_starboard_door_mask.obj",
	"librain/z_depth_obj_5/pos_offset/x",  -0.080000,
	"librain/z_depth_obj_5/pos_offset/y",  0.170000,
	"librain/z_depth_obj_5/pos_offset/z", 0,
	"librain/z_depth_obj_5/load"		,  0,

	"librain/num_glass_use"				,  3,
	"librain/initialize"				,  1,
}

	
local done = false
pilotdooropen = false
copilotdooropen = false
rearportleftdooropen = false
rearportrightdooropen = false
rearstarboarddooropen = false
--local wiperangle = 0
function update()
	
	-- Copilot Door: sim/cockpit2/switches/custom_slider_on[0]
	-- Pilot Door: sim/cockpit2/switches/custom_slider_on[1]
	-- Passenger Door Port Left: sim/cockpit2/switches/custom_slider_on[2]
	-- Passenger Door Port Right: sim/cockpit2/switches/custom_slider_on[3]
	-- Copilot Door: sim/cockpit2/switches/custom_slider_on[5]
	
-- Check to see if the plane has crashed and deinitialize librain if it has. Not doing so can lead to X-plane crashing
if ((get(globalProperty("sim/flightmodel2/misc/has_crashed"))) == 1) then
set(globalProperty("librain/initialize"), 0)
end
	-- do the initialization on first frame only
	if done then
		-- Check to see if the wipers are on
		if (get(globalProperty("sim/cockpit2/switches/wiper_speed"))) > 0 then
			-- wipers are on
			wipers_on = true
			wiperangle = get(globalProperty("sim/flightmodel2/misc/wiper_angle_deg[0]"))
			wiper0angle = math.rad(get(globalProperty("sim/flightmodel2/misc/wiper_angle_deg[0]")) * 0.25)
			wiper1angle = wiper0angle * -1
			set(globalProperty("librain/glass_0/wiper_0/pivot/x"), 0.2 + wiper0angle * 0.9)
			set(globalProperty("librain/glass_1/wiper_0/pivot/x"), 0.8 - wiper0angle * 0.9)
			set(globalProperty("librain/glass_0/wiper_0/pivot/y"), 0 - wiper0angle * 0.1)
			set(globalProperty("librain/glass_1/wiper_0/pivot/y"), 0 - wiper0angle * 0.1)
			set(globalProperty("librain/glass_0/wiper_0/angle"), wiper0angle)
			set(globalProperty("librain/glass_1/wiper_0/angle"), wiper1angle)
			oldwiperangle = wiperangle
		else
			-- wipers are off
			-- Reset the angle of the wipers to 0
			set(globalProperty("librain/glass_0/wiper_0/angle"), 0)
			set(globalProperty("librain/glass_1/wiper_0/angle"), 0)
		end
		
		-- Check to see if Copilot's door is open. If it is, load the z_depth object for it
		if ((get(globalProperty("sim/cockpit2/switches/custom_slider_on[0]"))) > 0) and not copilotdooropen then
			set(globalProperty("librain/z_depth_obj_1/load"), 1)
			copilotdooropen = true
		elseif ((get(globalProperty("sim/cockpit2/switches/custom_slider_on[0]"))) == 0) and copilotdooropen then
			set(globalProperty("librain/z_depth_obj_1/load"), 0)
			copilotdooropen = false
		end
		
		-- Check to see if Pilot's door is open. If it is, load the z_depth object for it
		if ((get(globalProperty("sim/cockpit2/switches/custom_slider_on[1]"))) > 0) and not pilotdooropen then
			set(globalProperty("librain/z_depth_obj_2/load"), 1)
			pilotdooropen = true
		elseif ((get(globalProperty("sim/cockpit2/switches/custom_slider_on[1]"))) == 0) and pilotdooropen then
			set(globalProperty("librain/z_depth_obj_2/load"), 0)
			pilotdooropen = false
		end
		
		-- Check to see if rear port left door is open. If it is, load the z_depth object for it
		if ((get(globalProperty("sim/cockpit2/switches/custom_slider_on[2]"))) > 0) and not rearportleftdooropen then
			set(globalProperty("librain/z_depth_obj_3/load"), 1)
			rearportleftdooropen = true
		elseif ((get(globalProperty("sim/cockpit2/switches/custom_slider_on[2]"))) == 0) and rearportleftdooropen then
			set(globalProperty("librain/z_depth_obj_3/load"), 0)
			rearportleftdooropen = false
		end
		
		-- Check to see if rear port right door is open. If it is, load the z_depth object for it
		if ((get(globalProperty("sim/cockpit2/switches/custom_slider_on[3]"))) > 0) and not rearportrightdooropen then
			set(globalProperty("librain/z_depth_obj_4/load"), 1)
			rearportrightdooropen = true
		elseif ((get(globalProperty("sim/cockpit2/switches/custom_slider_on[3]"))) == 0) and rearportrightdooropen then
			set(globalProperty("librain/z_depth_obj_4/load"), 0)
			rearportrightdooropen = false
		end
		
		-- Check to see if rear starboard door is open. If it is, load the z_depth object for it
		if ((get(globalProperty("sim/cockpit2/switches/custom_slider_on[5]"))) > 0) and not rearstarboarddooropen then
			set(globalProperty("librain/z_depth_obj_5/load"), 1)
			rearstarboarddooropen = true
		elseif ((get(globalProperty("sim/cockpit2/switches/custom_slider_on[5]"))) == 0) and rearstarboarddooropen then
			set(globalProperty("librain/z_depth_obj_5/load"), 0)
			rearstarboarddooropen = false
		end
		return end
	done = true
	set(globalProperty("librain/initialize"), 0)
	-- populate datarefs, last one being initialize
	for i = 1, #drf, 2 do
		set(globalProperty(drf[i]), drf[i+1])
	end
	logInfo("Librain plugin for the Pedrovl DHC6X Twin Otter Series (Version 1.0 by Paul Blair (toadlife) - toadlife@gmail.com")
	logInfo("librain initialization complete")
end

function aircraft_unload()
	set(globalProperty("librain/initialize"), 0)
end

function flight_crash()
	set(globalProperty("librain/initialize"), 0)
end

