-- View scripts
-- Copyright (C) 2004, Eagle Dynamics.

-- Don't change existing values, they are requested from Ñ++!
-- Use these indices in Snap and CockpitLocalPoint tables.
dofile("Scripts/Database/wsTypes.lua")
PlaneIndex = {
iSu_27 = 1,
iSu_33 = 2,
iSu_25 = 3,
iSu_39 = 4,		-- Su-39 or Su-25T
iMiG_29 = 5,
iMiG_29K = 6,
iA_10 = 7,
iF_15 = 8,
iKA_50 = 9,
iMI_24W = 10,
iKA_52 = 11,
iMI_28N = 12,
iAH_64A = 13,
iAH_64D = 14,
iAH_1W = 15,
iOH_58D = 16,
iSea_Lynx = 17,
iMI_8MT = 18,
iMI_26 = 20,
iKA_27 = 21,
iCH_47D = 22,
iCH_53E = 23,
iSH_60B = 24,
iUH_60A = 25,
iSH_3H = 26,
iAB_212 = 27,
}
PlaneIndexByType =
{
	[MiG_29]  = PlaneIndex.iMiG_29,
	[Su_27]   = PlaneIndex.iSu_27,
	[Su_33]   = PlaneIndex.iSu_33,
	[F_15]    = PlaneIndex.iF_15,
	[Su_25]   = PlaneIndex.iSu_25,
	[A_10]    = PlaneIndex.iA_10,
	[MIG_29K] = PlaneIndex.iMiG_29K,
	[Su_39]   = PlaneIndex.iSu_39,
	[MiG_29G] = PlaneIndex.iMiG_29,
	[MiG_29C] = PlaneIndex.iMiG_29,
	[Su_25T]  = PlaneIndex.iSu_39,
	[KA_50]   = PlaneIndex.iKA_50,
	[MI_24W]   = PlaneIndex.iMI_24W,
	[KA_52]   = PlaneIndex.iKA_52,
	[MI_28N]   = PlaneIndex.iMI_28N,
	[AH_64A]   = PlaneIndex.iAH_64A,
	[AH_64D]   = PlaneIndex.iAH_64D,
	[AH_1W]   = PlaneIndex.iAH_1W,
	[OH_58D]   = PlaneIndex.iOH_58D,
	[Sea_Lynx]   = PlaneIndex.iSea_Lynx,
	[MI_8MT]   = PlaneIndex.iMI_8MT,
	[MI_26]   = PlaneIndex.iMI_26,
	[KA_27]   = PlaneIndex.iKA_27,
	[CH_47D]   = PlaneIndex.iCH_47D,
	[CH_53E]   = PlaneIndex.iCH_53E,
	[SH_60B]   = PlaneIndex.iSH_60B,
	[UH_60A]   = PlaneIndex.iUH_60A,
	[SH_3H]   = PlaneIndex.iSH_3H,
	[AB_212]   = PlaneIndex.iAB_212,
}

function GetPlaneIndex(plane_type)
	if plane_type > 0 and 
	   plane_type < 10 then
	   return plane_type
	end
	return  1 --PlaneIndex[plane_type] or PlaneIndex.iSu_27
end

CockpitMouse = true
CockpitMouseSpeedSlow = 1.0
CockpitMouseSpeedNormal = 10.0
CockpitMouseSpeedFast = 20.0
CockpitKeyboardAccelerationSlow = 5.0
CockpitKeyboardAccelerationNormal = 30.0
CockpitKeyboardAccelerationFast = 80.0
CockpitKeyboardZoomAcceleration = 300.0
DisableSnapViewsSaving = false
UseDefaultSnapViews = true
CockpitPanStepHor = 45.0
CockpitPanStepVert = 30.0
CockpitNyMove = true

CockpitHAngleAccelerateTimeMax = 0.15
CockpitVAngleAccelerateTimeMax = 0.15
CockpitZoomAccelerateTimeMax = 0.2

function GetSnapView(iPlane, iKey)
	if UseDefaultSnapViews then
	      s = DefaultSnapView.Snap[GetPlaneIndex(iPlane)][iKey]
	else
		  s = 				  Snap[GetPlaneIndex(iPlane)][iKey]
	end
	return s.hAngle, 
		   s.vAngle,
		   s.viewAngle,
		   s.x_trans,
		   s.y_trans,
		   s.z_trans,
		   s.rollAngle
end

function SetSnapView(iPlane, iKey, hAngle, vAngle, viewAngle,x_trans,y_trans,z_trans,rollAngle)
	local s = nil
	if UseDefaultSnapViews then
	      s = DefaultSnapView.Snap[GetPlaneIndex(iPlane)][iKey]
	else
		  s = 				  Snap[GetPlaneIndex(iPlane)][iKey]
	end
	s.hAngle      = hAngle	
	s.vAngle      = vAngle	
	s.viewAngle   = viewAngle	
	s.x_trans     = x_trans
	s.y_trans     = y_trans
	s.z_trans     = z_trans
	s.rollAngle   = rollAngle
end

function WriteSnapViews(fileName)
	local file = io.open(fileName, "w")
	if file then
		io.output(file)
		local t = {}
	if UseDefaultSnapViews then
		Serialize("Snap", DefaultSnapView.Snap, t)
	else
		Serialize("Snap", Snap, t)
	end
		io.close(file)		
	end
end
-- Camera view angle limits {view angle min, view angle max}.
CameraViewAngleLimits = {}
CameraViewAngleLimits[PlaneIndex.iSu_27] = {20.0, 140.0}
CameraViewAngleLimits[PlaneIndex.iSu_33] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iSu_25] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iSu_39] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iMiG_29] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iMiG_29K] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iA_10] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iF_15] = {20.0, 140.0}
CameraViewAngleLimits[PlaneIndex.iKA_50] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iMI_24W] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iKA_52] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iMI_28N] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iAH_64A] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iAH_64D] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iAH_1W] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iOH_58D] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iSea_Lynx] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iMI_8MT] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iMI_26] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iKA_27] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iCH_47D] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iCH_53E] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iSH_60B] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iUH_60A] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iSH_3H] = {20.0, 120.0}
CameraViewAngleLimits[PlaneIndex.iAB_212] = {20.0, 120.0}

function GetCameraViewAngleLimits(iType)
	local p = CameraViewAngleLimits[iType]
	return p[1], p[2]
end

function SetCameraViewAngleLimits(iType, viewAngMin, viewAngMax)
	local p = CameraViewAngleLimits[iType]
	p[1] = viewAngMin
	p[2] = viewAngMax
end

CameraAngleRestriction = {}
CameraAngleRestriction[PlaneIndex.iSu_27] =  {1,60,0.4}
CameraAngleRestriction[PlaneIndex.iSu_33] =  {1,60,0.4}
CameraAngleRestriction[PlaneIndex.iSu_25] =  {1,60,0.4}
CameraAngleRestriction[PlaneIndex.iSu_39] =  {1,60,0.4}
CameraAngleRestriction[PlaneIndex.iMiG_29] = {1,60,0.4}
CameraAngleRestriction[PlaneIndex.iMiG_29K] ={1,60,0.4}
CameraAngleRestriction[PlaneIndex.iA_10]  =  {1,90,0.5}
CameraAngleRestriction[PlaneIndex.iF_15]  =  {1,60,0.4}
CameraAngleRestriction[PlaneIndex.iKA_50] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iMI_24W] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iKA_52] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iMI_28N] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iAH_64A] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iAH_64D] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iAH_1W] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iOH_58D] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iSea_Lynx] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iMI_8MT] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iMI_26] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iKA_27] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iCH_47D] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iCH_53E] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iSH_60B] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iUH_60A] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iSH_3H] =  {0,60,0.4}
CameraAngleRestriction[PlaneIndex.iAB_212] =  {0,60,0.4}


function GetCameraAngleRestriction(iType)
	local p = CameraAngleRestriction[iType]
	return  p[1], p[2], p[3]
end 


-- HUD displacement for the HUD-only view (for cockpit builders).
-- Y-axis - up/down, Z-axis - left/right, in meters. X-axis (size) not implemented yet. 
HUDOnlyPoint = {}
HUDOnlyPoint[PlaneIndex.iSu_27] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iSu_33] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iSu_25] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iSu_39] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iMiG_29] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iMiG_29K] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iA_10] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iF_15] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iKA_50] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iMI_24W] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iKA_52] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iMI_28N] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iAH_64A] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iAH_64D] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iAH_1W] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iOH_58D] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iSea_Lynx] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iMI_8MT] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iMI_26] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iKA_27] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iCH_47D] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iCH_53E] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iSH_60B] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iUH_60A] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iSH_3H] = {0.0, 0.0, 0.0}
HUDOnlyPoint[PlaneIndex.iAB_212] = {0.0, 0.0, 0.0}


function GetHUDOnlyPoint(iType)
	local p = HUDOnlyPoint[iType]
	return p[1], p[2], p[3]
end

-- HUD RGB color 
HUDColor = {}
HUDColor[PlaneIndex.iSu_27] = {0, 255, 0}
HUDColor[PlaneIndex.iSu_33] = {0, 255, 0}
HUDColor[PlaneIndex.iSu_25] = {255, 255, 0}
HUDColor[PlaneIndex.iSu_39] = {0, 255, 0}
HUDColor[PlaneIndex.iMiG_29] = {0, 255, 0}
HUDColor[PlaneIndex.iMiG_29K] = {0, 255, 0}
HUDColor[PlaneIndex.iA_10] = {0, 255, 0}
HUDColor[PlaneIndex.iF_15] = {0, 255, 0}
HUDColor[PlaneIndex.iKA_50] = {0, 255, 0}
HUDColor[PlaneIndex.iMI_24W] = {0, 255, 0}
HUDColor[PlaneIndex.iKA_52] = {0, 255, 0}
HUDColor[PlaneIndex.iMI_28N] = {0, 255, 0}
HUDColor[PlaneIndex.iAH_64A] = {0, 255, 0}
HUDColor[PlaneIndex.iAH_64D] = {0, 255, 0}
HUDColor[PlaneIndex.iAH_1W] = {0, 255, 0}
HUDColor[PlaneIndex.iOH_58D] = {0, 255, 0}
HUDColor[PlaneIndex.iSea_Lynx] = {0, 255, 0}
HUDColor[PlaneIndex.iMI_8MT] = {0, 255, 0}
HUDColor[PlaneIndex.iMI_26] = {0, 255, 0}
HUDColor[PlaneIndex.iKA_27] = {0, 255, 0}
HUDColor[PlaneIndex.iCH_47D] = {0, 255, 0}
HUDColor[PlaneIndex.iCH_53E] = {0, 255, 0}
HUDColor[PlaneIndex.iSH_60B] = {0, 255, 0}
HUDColor[PlaneIndex.iUH_60A] = {0, 255, 0}
HUDColor[PlaneIndex.iSH_3H] = {0, 255, 0}
HUDColor[PlaneIndex.iAB_212] = {0, 255, 0}


function GetHUDColor(iType)
	local p = HUDColor[iType]
	return p[1], p[2], p[3]
end

function NaturalHeadMoving(tang, roll, omz)
	local r = roll
	if r > 90.0 then
		r = 180.0 - r
	elseif roll < -90.0 then
		r = -180.0 - r
	end
	local hAngle = -0.25 * r
	local vAngle = math.min(math.max(0.0, 0.4 * tang + 45.0 * omz), 90.0)
	return hAngle, vAngle
end

ExternalMouse = true
ExternalMouseSpeedSlow = 1.0
ExternalMouseSpeedNormal = 5.0
ExternalMouseSpeedFast = 20.0
ExternalViewAngleMin = 3.0
ExternalViewAngleMax = 170.0
ExternalViewAngleDefault = 60.0
ExternalKeyboardZoomAcceleration = 30.0
ExternalKeyboardZoomAccelerateTimeMax = 1.0
ExplosionExpoTime = 4.0
ExternalKeyboardAccelerationSlow = 1.0
ExternalKeyboardAccelerationNormal = 10.0
ExternalKeyboardAccelerationFast = 30.0
ExternalHAngleAccelerateTimeMax = 3.0
ExternalVAngleAccelerateTimeMax = 3.0
ExternalDistAccelerateTimeMax = 3.0
ExternalHAngleLocalAccelerateTimeMax = 3.0
ExternalVAngleLocalAccelerateTimeMax = 3.0
ExternalAngleNormalDiscreteStep = 15.0/ExternalKeyboardAccelerationNormal -- When 'S' is pressed only
ChaseCameraNyMove = true
FreeCameraAngleIncrement = 3.0
FreeCameraDistanceIncrement = 200.0
FreeCameraLeftRightIncrement = 2.0
FreeCameraAltitudeIncrement = 2.0
FreeCameraScalarSpeedAcceleration = 0.1 
xMinMap = -300000
xMaxMap = 500000
yMinMap = -400000
yMaxMap = 200000
dxMap = 150000
dyMap = 100000

head_roll_shaking = true
head_roll_shaking_max = 45.0
head_roll_shaking_compensation_gain = 0.5

-- CameraJiggle() and CameraFloat() functions make camera position
-- dependent on FPS so be careful in using the Shift-J command with tracks, please.
function CameraJiggle(t)
	local rotX, rotY, rotZ
	rotX = 0.05 * math.random() * math.sin(37.0 * (t - 0.0))
	rotY = 0.05 * math.random() * math.sin(41.0 * (t - 1.0))
	rotZ = 0.05 * math.random() * math.sin(53.0 * (t - 2.0))
	return rotX, rotY, rotZ
end

function CameraFloat(t)
	local dX, dY, dZ
	dX = 0.61 * math.sin(0.7 * t) + 0.047 * math.sin(1.6 * t);
	dY = 0.43 * math.sin(0.6 * t) + 0.067 * math.sin(1.7 * t);
	dZ = 0.53 * math.sin(1.0 * t) + 0.083 * math.sin(1.9 * t);
	return dX, dY, dZ
end



