-- librain.lua



-- 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"			, 1,
	"librain/debug_draw"		, 0,
	"librain/wipers_visible"	, 0,

	"librain/glass_0/slant_factor"		,  1,
	"librain/glass_0/thrust_point/x"	,  0,
	"librain/glass_0/thrust_point/y"	, 0,
	"librain/glass_0/thrust_factor"		,  0,
	"librain/glass_0/max_thrust"		, 1500,
	"librain/glass_0/gravity_point/x"	, 0.7,
	"librain/glass_0/gravity_point/y"	, 0.85,
	"librain/glass_0/gravity_factor"	, 0.8,
	"librain/glass_0/wind_point/x"		,  180,
	"librain/glass_0/wind_point/y"		, 0,
	"librain/glass_0/wind_factor"		,  1.0,
	"librain/glass_0/wind_normal"		,  0.8,
	"librain/glass_0/max_tas"			, 80,
	"librain/glass_0/obj/filename"		, obj_path .. "windsheildglass.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.249000,
	"librain/glass_0/wiper_0/pivot/y"    ,  0.400000,
	"librain/glass_0/wiper_0/radius_inner", 0.110000,
	"librain/glass_0/wiper_0/radius_outer", 0.29000,
	"librain/glass_0/wiper_0/angle"      ,  0,
	"librain/glass_0/wiper_0/moving"     ,  1,
	"librain/glass_0/wiper_1/pivot/x"    ,  0.750000,
	"librain/glass_0/wiper_1/pivot/y"    ,  0.400000,
	"librain/glass_0/wiper_1/radius_inner", 0.110000,
	"librain/glass_0/wiper_1/radius_outer", 0.290000,
	"librain/glass_0/wiper_1/angle"     ,  0,
	"librain/glass_0/wiper_1/moving"     ,  1,
	"librain/glass_0/obj/load"			,  1,

	"librain/glass_1/slant_factor"		,  1.0,
	"librain/glass_1/thrust_point/x"	, 2.0,
	"librain/glass_1/thrust_point/y"	, 0.25,
	"librain/glass_1/thrust_factor"		, 0,
	"librain/glass_1/max_thrust"		, 1500,
	"librain/glass_1/gravity_point/x"	, 0.5,
	"librain/glass_1/gravity_point/y"	, 1.2,
	"librain/glass_1/gravity_factor"	, 0.7,
	"librain/glass_1/wind_point/x"		, 2.0,
	"librain/glass_1/wind_point/y"		, 0.25,
	"librain/glass_1/wind_factor"		,  1.0,
	"librain/glass_1/wind_normal"		,  0.0,
	"librain/glass_1/max_tas"			, 80,
	"librain/glass_1/obj/filename"		, obj_path .. "passengerglass.obj",
	"librain/z_depth_obj_0/pos_offset/x",  0,
	"librain/z_depth_obj_0/pos_offset/y",  0.02,
	"librain/z_depth_obj_0/pos_offset/z", -1.80,
	"librain/glass_1/obj/load"			, 1,

	"librain/z_depth_obj_0/filename"	, obj_path .. "passengermask.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 .. "cockpitmask.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/num_glass_use"				,  2,
	"librain/initialize"				,  1,
}

    --logInfo("creating data tables")
	--wiper_angle_deg = dataref_table("sim/flightmodel2/misc/wiper_angle_deg")
	--wiperspeed = dataref_table("sim/cockpit2/switches/wiperspeed")

	
local done = false

function update()
	-- do the initialization on first frame only
	if done then 
		if (get(globalProperty("sim/cockpit2/switches/wiper_speed"))) > 0 then
			--wipers are on, so let's update the wiper position!
			local wiper0angle = get(globalProperty("sim/flightmodel2/misc/wiper_angle_deg[0]")) * 0.918 * -.01
			local wiper1angle = get(globalProperty("sim/flightmodel2/misc/wiper_angle_deg[0]")) * 0.918 * .01
			--logInfo(wiper0angle .. wiper1angle)
			set(globalProperty("librain/glass_0/wiper_0/angle"), wiper0angle)
			set(globalProperty("librain/glass_0/wiper_1/angle"), wiper1angle)
		end
		-- wipers are off, so don't do anything
		return end
	done = true
	-- populate datarefs, last one being initialize
	for i = 1, #drf, 2 do
		set(globalProperty(drf[i]), drf[i+1])
	end
	logInfo("librain initialization complete")
end





-- Wiper 0 0, -0.450000
-- Wiper 1 0, 0.45
-- Angle modifier 0.918
