; Combined UnderSea-To-Surface AI And UnderSea-To-Surface Player Missile Doctrine For
; -Reinforce Alert- Mod v 1.46
; Rev.2
var PreenableCourse
var RunToEnable
var Runout
var lastX
var lastY
var run
var Waypoints
var DecoyLogic
var Spoofed
var UWD
VAR ZALT
var TINIT
VAR NUM_TH
VAR ZZZConf
var TX
VAR TY
var talt
var temp
var Delta
var AltCruise
var SpdCruise
var PreSet
var FinalPhazeN
var TP
var d
var cd
var inair
var TRD_Switch
var mission
var percmalf
var weapeffectv
var crushevnt
var otkaztime
var detonpercnt
var dfs
var msek
var wrang
var WLS
 
Func malfunction {
if ( ( ownalt > 0 ) and ( pitch > 45 ) ) then {
	SetPriority 255
	SetPitch -80
} elseif ( ( ownalt > 0 ) and ( pitch < 45 ) ) then {
if inair then {
	SetPriority 255
	setrudder -5
if ( 90 > ( abs ( preenablecourse - owncrs ) mod 360 ) ) then {
	detonate
} endif
} else {
	SetPriority 255
	SetPitch -60
} endif
} endif
IF OwnAlt > 0 THEN {
	UWD = 1
} ELSEIF ( OwnAlt < 0 ) AND UWD THEN {
	If dfs == 1 Then {
		Detonate
	} else {
		Shutdown
	} endif
} ENDIF
}
IF INIT THEN {
	TP = -1
	cd = 1
	lastX = OwnX
	lastY = OwnY
	RunToEnable = 100
	PreSet = 1
	ZALT = ( MAXALT * 3.28 )
	TINIT = ( TIME + 0.5 )
	FinalPhazeN = 3000
	SpdCruise = MaxSpd
	dfs = -1
IF ( DBUser1Flags & 1 ) == 1 THEN { SetSnorkel On TRD_Switch = 1 } ENDIF

} ELSEIF ( Runout != -1 ) AND ( run > Runout ) THEN {
	Runout = -1
	SHUTDOWN
} ELSEif ( tp != -1 ) and ( time > tp ) then {
	detonate
} else {
	IF PreSet THEN {
		PreSet = 0
		DecoyLogic = 40
		Spoofed = RND 100
		IF ( OwnName $= "Harpoon UGM" ) THEN { AltCruise = 30 DecoyLogic = 70 weapeffectv = 8.5 Wrang = 129640 } ENDIF
		IF ( OwnName $= "Harpoon User" ) THEN { AltCruise = 30 DecoyLogic = 70 inair = 1 weapeffectv = 8.5 Wrang = 129640 } ENDIF
		IF ( OwnName $= "TASM UGM-109 B" ) THEN { AltCruise = 50 weapeffectv = 7 Wrang = 463000 } ENDIF
		IF ( OwnName $= "SS-N-22 Sunburn UGM" ) THEN { AltCruise = 66 DecoyLogic = 70 weapeffectv = 8 Wrang = 120380 } ENDIF
		IF ( OwnName $= "Penguin ASM" ) THEN { AltCruise = 49 DecoyLogic = 75 inair = 1 weapeffectv = 8 Wrang = 55000 } ENDIF
		IF ( OwnName $= "Marte ER User" ) THEN { AltCruise = 30 DecoyLogic = 80 inair = 1 weapeffectv = 8 Wrang = 96310 } ENDIF
		IF ( OwnName $= "Kh-35 Uran ASM" ) THEN { AltCruise = 30 DecoyLogic = 70 inair = 1 weapeffectv = 7 Wrang = 140380 } ENDIF
		IF ( OwnName $= "Exocet UGM User" ) THEN { AltCruise = 49 DecoyLogic = 60 weapeffectv = 7 Wrang = 64820 } ENDIF
		IF ( OwnName $= "C 802 UGM User" ) THEN { AltCruise = 99 DecoyLogic = 60 weapeffectv = 7 Wrang = 120380 } ENDIF
		IF ( OwnName $= "SSN-27 Sizzler SSM SuperSonic" ) THEN { AltCruise = 33 DecoyLogic = 80 inair = 1 weapeffectv = 10 Wrang = 23005 } ENDIF
	if malfunctionweap then {
		detonpercnt = rnd 10
		msek = ( ( maxspd * 1852 ) / 3600 )
		WLS = ( ( ( Wrang / msek ) * 2 ) - 20 )
		percmalf = rnd 10
		if percmalf >= weapeffectv then {
			crushevnt = 1
			otkaztime = ( time + 20 + rnd WLS )
		if percmalf > 9.7 then {
			otkaztime = ( Time + 3 )
		} endif
	} endif
} endif
} ENDIF
	IF TRD_Switch THEN {
 		IF ( OwnAlt > -10 ) AND ( mission != 1 ) THEN {
;			DebugOut "Booster OFF"
 			SetPriority 255
			SetSnorkel OFF
	 		mission = 1
		} ENDIF
	} ENDIF
if ownalt > 0 then {
	uwd = 1
} elseif ( ownalt < 0 ) and uwd then {
	if malfunctionweap and ( dfs == -1 ) then {
		Shutdown
	} elseif not malfunctionweap then {
		Shutdown
	} endif
} endif
	run += xyrng ( OwnX - lastX ) ( OwnY - lastY )
	lastX = OwnX
	lastY = OwnY
IF ( crushevnt == 1 ) AND ( TIME >= OtkazTime ) THEN {
	IF detonpercnt >= 9.5 THEN { dfs = 1 } ELSE { dfs = 0 } ENDIF
		malfunction
} ENDIF
	IF ( TINIT < TIME ) AND ( OWNALT > 0 ) THEN {
		SETPRIORITY 252	SETALT AltCruise
		SETPRIORITY 252	SetSpd SpdCruise
		IF NUM_TH == 0 THEN {
			IF Waypoints THEN {
				IF ( OwnName $= "Harpoon User" ) Then { SETPRIORITY 224 TerminalHome } ELSE { SETPRIORITY 224 PreenableCourse } ENDIF
			} ELSE {
				SETPRIORITY 224 SetCrs PreenableCourse
			} ENDIF
		} else {
			temp = XYRng ( tx - ownx ) ( ty - owny )
			if d and cd then {
				cd = 0
				tp = ( ownspd * 0.5144444 )
				tp = ( time + 3 + ( temp / tp ) )
			} endif
			if temp > 1000 then {
				d = 1
				SETPRIORITY 250 steerxy ( tx - ownx ) ( ty - owny )
				if ( temp < 5000 ) or ( talt > 50 ) then {
					DELTA = ( TALT - OWNALT + 30 )
					temp = XYBRG ABS DELTA ( temp * 3.28 )
					if DELTA < 0 then { temp = ( -1 * temp ) } endif
					SETPRIORITY 253 SetSpd SpdCruise
					SETPRIORITY 253 SETPERSIST 1 SETPITCH TEMP
				} endif
			} endif
		} endif
	} ENDIF
	IF RunToEnable != -1 THEN {
		IF run >= RunToEnable THEN {
			ENABLE
			RunToEnable = -1
		} ENDIF
	} ENDIF
	IF NEWTRACK then {
	IF ( TgtType $= "PLANE" ) OR ( TgtType $= "HELO" ) THEN { Drop } ELSE {
		IF TgtType $= "CM" THEN {
			IF Spoofed > DecoyLogic THEN {
				SetTactic "TH_ST"
		} ELSEIF Spoofed <= DecoyLogic THEN {
			Drop
			} endif
		} ELSEIF Tgtconf > ZZZConf THEN {
			SetTactic "TH_ST"
		} ELSE {
			SetTactic "SBROS"
		} ENDIF
	} ENDIF
	} ENDIF
} ENDIF