-- librain.lua

--[[
 Librain plugin for the Laminar Research Beechcraft Baron 58 by Paul Blair (toadlife) - toadlife@gmail.com
 Version 1.0
 
 Thank you to:
 
 * 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/"

-- pairs of dataref/value
local drf	= {
	"librain/verbose"			, 0,
	"librain/debug_draw"		, 0,
	"librain/wipers_visible"	, 0,
	
-- windscreen

	"librain/glass_0/gravity_factor"	, 1,
	"librain/glass_0/gravity_point/x"	, 0.5,
	"librain/glass_0/gravity_point/y"	, 1.1,	
	"librain/glass_0/max_tas"			, 100,
	"librain/glass_0/max_thrust"		, 0,
	"librain/glass_0/slant_factor"		,  0.6,
	"librain/glass_0/thrust_factor"		,  0,
	"librain/glass_0/thrust_point/x"	,  0.5,
	"librain/glass_0/thrust_point/y"	, -3,
	"librain/glass_0/wind_factor"		,  1.0,
	"librain/glass_0/wind_normal"		,  0.8,
	"librain/glass_0/wind_point/x"		,  0.5,
	"librain/glass_0/wind_point/y"		, -3,
	"librain/glass_0/obj/filename"		, obj_path .. "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/obj/load"			,  1,

	
-- side windows

	"librain/glass_1/gravity_factor"	, 1,
	"librain/glass_1/gravity_point/x"	, 0.5,
	"librain/glass_1/gravity_point/y"	, 2.0,
	"librain/glass_1/max_thrust"		, 0,
	"librain/glass_1/slant_factor"		,  0.6,
	"librain/glass_1/thrust_factor"		,  0.2,
	"librain/glass_1/thrust_point/x"	,  2,
	"librain/glass_1/thrust_point/y"	, 0.5,
	"librain/glass_1/wind_factor"		,  1.0,
	"librain/glass_1/wind_normal"		,  0.2,
	"librain/glass_1/wind_point/x"		,  1.5,
	"librain/glass_1/wind_point/y"		, 0.0,
	"librain/glass_1/max_tas"			, 100,
	"librain/glass_1/obj/filename"		, obj_path .. "side_windows.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/obj/load"			,  1,
	
	"librain/z_depth_obj_0/filename"	, obj_path .. "mask.obj",
	"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 .. "window_frame_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"		,  1,
	
	"librain/z_depth_obj_2/filename"	, obj_path .. "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 .. "cpwindowmask.obj",
	"librain/z_depth_obj_3/pos_offset/x",  0,
	"librain/z_depth_obj_3/pos_offset/y",  0,
	"librain/z_depth_obj_3/pos_offset/z", 0,
	"librain/z_depth_obj_3/load"		,  0,
	
	"librain/z_depth_obj_4/filename"	, obj_path .. "passwindowmask.obj",
	"librain/z_depth_obj_4/pos_offset/x",  0,
	"librain/z_depth_obj_4/pos_offset/y",  0,
	"librain/z_depth_obj_4/pos_offset/z", 0,
	"librain/z_depth_obj_4/load"		,  0,

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

	
local done = false
local windowopen = false
local cpdooropen = false
local passdooropen = false
function update()
	
	-- Copilot's Door: sim/cockpit2/switches/door_open[0]
	-- Passenger Door: sim/cockpit2/switches/door_open[1]
	-- Pilot's Window Door: sim/cockpit2/switches/door_open[2]
	
-- 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 captain's window is open. If it is, load the z_depth object for it
		if ((get(globalProperty("sim/cockpit2/switches/door_open[2]"))) == 1) and not windowopen then
			set(globalProperty("librain/z_depth_obj_1/load"), 0)
			set(globalProperty("librain/z_depth_obj_2/load"), 1)
			windowopen = true
		elseif ((get(globalProperty("sim/cockpit2/switches/door_open[2]"))) == 0) and windowopen then
			set(globalProperty("librain/z_depth_obj_1/load"), 1)
			set(globalProperty("librain/z_depth_obj_2/load"), 0)
			windowopen = false
		end
		
		-- Check to see if copilots door is open. If it is, load the z_depth object for it
		if ((get(globalProperty("sim/cockpit2/switches/door_open[0]"))) == 1) and not cpdooropen then
			set(globalProperty("librain/z_depth_obj_3/load"), 1)
			cpdooropen = true
		elseif ((get(globalProperty("sim/cockpit2/switches/door_open[0]"))) == 0) and cpdooropen then
			set(globalProperty("librain/z_depth_obj_3/load"), 0)
			cpdooropen = false
		end
		
		-- Check to see if passenger door is open. If it is, load the z_depth object for it
		if ((get(globalProperty("sim/cockpit2/switches/door_open[1]"))) == 1) and not passdooropen then
			set(globalProperty("librain/z_depth_obj_4/load"), 1)
			passdooropen = true
		elseif ((get(globalProperty("sim/cockpit2/switches/door_open[1]"))) == 0) and passdooropen then
			set(globalProperty("librain/z_depth_obj_4/load"), 0)
			passdooropen = 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 Laminar Research Beechcraft Baron 58 (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

