From 7d5fca3c5a0ddce6fb94066b35a3077152fe8a77 Mon Sep 17 00:00:00 2001
From: jaynus <walter.pearce@idi-systems.com>
Date: Sun, 5 Apr 2015 12:55:17 -0700
Subject: [PATCH] DIR guidance on Javelin missiles/Titan. TODO: The guidance
 codes need re-works.

---
 addons/wep_javelin/functions/fnc_fired.sqf | 119 ++++++++++++++++++++-
 1 file changed, 118 insertions(+), 1 deletion(-)

diff --git a/addons/wep_javelin/functions/fnc_fired.sqf b/addons/wep_javelin/functions/fnc_fired.sqf
index 149f829292..4e94989677 100644
--- a/addons/wep_javelin/functions/fnc_fired.sqf
+++ b/addons/wep_javelin/functions/fnc_fired.sqf
@@ -5,7 +5,124 @@ TRACE_1("Launch", _this);
 PARAMS_7(_shooter,_weapon,_muzzle,_mode,_ammo,_magazine,_projectile);
 
 FUNC(guidance_Javelin_LOBL_DIR_PFH) = {
+	TRACE_1("enter", _this);
+	private["_pitch", "_yaw", "_wentTerminal", "_target", "_targetPos", "_curVelocity", "_missile", 
+            "_launchPos", "_targetStartPos", "_defPitch", "_defYaw"];
+	_args = _this select 0;
+	//PARAMS_7(_shooter,_weapon,_muzzle,_mode,_ammo,_magazine,_projectile);
+	_shooter = _args select 0;
+	_missile = _args select 6;
+	
+	if((count _args) > 7) then {
+		_saveArgs = _args select 7;
+		_target = _saveArgs select 0;
+		_targetStartPos = _saveArgs select 1;
+        _launchPos = _saveArgs select 2;
+        _wentTerminal = _saveArgs select 3;
+	} else {
+        _wentTerminal = false;
+        _launchPos = getPosASL _shooter;
+        _target = ACE_player getVariable[QGVAR(currentTarget), objNull];
+        _targetStartPos = ACE_player getVariable[QGVAR(currentTargetPos), [0,0,0]];
+	};
+	
+    if(!alive _missile || isNull _missile || isNull _target) exitWith {
+		[(_this select 1)] call cba_fnc_removePerFrameHandler;
+	};
     
+    _targetPos = getPosASL _target;
+	_curVelocity = velocity _missile;
+	
+    TRACE_4("", _target, _targetPos, _launchPos, _targetStartPos);
+
+	_addHeight = [0,0,0];
+	if(!isNil "_target") then {
+		
+		_yVec = vectorDir _missile;
+		_zVec = vectorUp _missile;
+		_xVec = vectorNormalized (_yVec vectorCrossProduct _zVec);
+		
+		_missilePos = getPosASL _missile;
+		// player sideChat "G!";
+
+		TRACE_4("Phase Check", _launchPos, _missilePos, _targetPos, (_missilePos distance _targetPos));
+		if((count _targetPos) > 0) then {
+			_distanceToTarget = [(_missilePos select 0), (_missilePos select 1), (_targetPos select 2)]  vectorDistance _targetPos;	
+
+			if( (_missilePos select 2) < (_targetPos select 2) + 60 && !_wentTerminal) then {
+				_addHeight = [0,0,(_targetPos select 2) + 120];
+				
+                _defPitch = 0.15;
+                _defYaw = 0.035;
+                
+                TRACE_1("Climb phase", _addHeight);
+			} else {
+				_wentTerminal = true;
+				_this set[2, _wentTerminal];
+				
+                _defPitch = 0.15;
+                _defYaw = 0.035;
+                
+                TRACE_1("TERMINAL", "");
+			};
+			_targetPos = _targetPos vectorAdd _addHeight;
+
+			_targetVectorSeeker = [_missile, [_xVec, _yVec, _zVec], _targetPos] call FUNC(translateToWeaponSpace);
+			TRACE_5("", _missile, _xVec, _yVec, _zVec, _targetPos);
+            
+            _yaw = 0.0;
+            _pitch = 0.0;
+               
+            if((_targetVectorSeeker select 0) < 0) then {
+                _yaw = -_defYaw;
+            } else {
+                if((_targetVectorSeeker select 0) > 0) then {
+                    _yaw = _defYaw;
+                };
+            };
+
+            if((_targetVectorSeeker select 2) < 0) then {
+                _pitch = -_defPitch;
+            } else {
+                if((_targetVectorSeeker select 2) > 0) then {
+                    _pitch = _defPitch;
+                };
+            };
+			
+			TRACE_3("", _targetVectorSeeker, _pitch, _yaw);
+			
+	#ifdef DEBUG_MODE_FULL
+			drawLine3D [(ASLtoATL _targetPos) vectorAdd _addHeight, ASLtoATL _targetPos, [0,1,0,1]];
+			
+			_light = "#lightpoint" createVehicleLocal (getPos _missile);
+			_light setLightBrightness 1.0;
+			_light setLightAmbient [1.0, 0.0, 0.0];
+			_light setLightColor [1.0, 0.0, 0.0];
+			
+			drawIcon3D ["\a3\ui_f\data\IGUI\Cfg\Cursors\selectover_ca.paa", [1,1,1,1], ASLtoATL _missilePos, 0.75, 0.75, 0, str _vectorTo, 1, 0.025, "TahomaB"];
+			drawLine3D [ASLtoATL _missilePos, ASLtoATL _targetPos, [1,0,0,1]];
+
+			MARKERCOUNT = MARKERCOUNT + 1;
+	#endif		
+	
+			if(accTime > 0) then {
+				TRACE_5("", _xVec, _yVec, _zVec, _yaw, _pitch);
+				_outVector = [_missile, [_xVec, _yVec, _zVec], [_yaw, 1/accTime, _pitch]] call FUNC(translateToModelSpace);
+				
+				_vectorTo = _missilePos vectorFromTo _outVector;
+				TRACE_3("", _missile, _outVector, _vectorTo);
+				_missile setVectorDirAndUp [_vectorTo, vectorUp _missile];
+			};
+			
+	#ifdef DEBUG_MODE_FULL
+			hintSilent format["d: %1", _distanceToTarget];
+	#endif
+		};
+	};
+    
+     _saveArgs = [_target,_targetStartPos, _launchPos, _wentTerminal];
+    _args set[7, _saveArgs ];
+    _this set[0, _args];
 };
 
 FUNC(guidance_Javelin_LOBL_TOP_PFH) = {
@@ -78,7 +195,7 @@ FUNC(guidance_Javelin_LOBL_TOP_PFH) = {
             _pitch = 0.0;
 
             // Stop jinking on terminal final decent
-            if((_missilePos distance _targetPos) < 50) then {
+            if((_missilePos distance _targetPos) < 150) then {
                 _defPitch = 0.0015;
                 _defYaw = 0.0015;
             };