; SS-N-27 ASM Doctrine For -RA- Addon v 1.40

var PreenableCourse
var RunToEnable
var Runout
var lastX
var lastY
var run
var Waypoints
var Fragments
var Drowned
var Aim
VAR NUM_TH
VAR ZZZConf
var talt
var temp
var Delta
var FinalPhazeN
var DecoyLogic
var Spoofed
var TP
var d
var cd
var trng

IF INIT THEN {
	TP = -1
	NUM_TH = 0
	ZZZConf = 0
	Fragments = DBIdxFromRecNum 1792
	Drowned = 0
	Aim = 0
	lastX = OwnX
	lastY = OwnY
	SETPRIORITY 251
	SetAlt 50
	SetSpd 580
	RunToEnable = 100
	Runout = -1
	FinalPhazeN = 3500
	DecoyLogic = 80
IF ( OwnName $= "3M-51 Alfa ASM" ) THEN { RunOut = 222240 } ENDIF
IF ( OwnName $= "3M-54E1 Club ASCM" ) THEN { RunOut = 300024 } ENDIF
IF ( ( OwnAlt > 0 ) AND ( OwnAlt < 15 ) ) Then { ShutDown } ENDIF
} elseIF ( Runout != -1 ) AND ( run >= Runout ) THEN {
;	DEBUGOUT "Fuel Lost"
	Runout = -1
	SHUTDOWN
} ELSEif ( tp != -1 ) and ( time > tp ) then {
	detonate
} else {
	run += xyrng ( OwnX - lastX ) ( OwnY - lastY )
	lastX = OwnX
	lastY = OwnY
	IF OwnAlt >= 5 THEN { Drowned = 1 } ELSEIF ( OwnAlt < 5 ) AND Drowned THEN { Shutdown } ENDIF
	IF OwnAlt >= ( MaxAlt + 600 ) THEN { Detonate } ENDIF
	; Enable
	IF RunToEnable != -1 THEN {
		IF run >= RunToEnable THEN {
			Spoofed = RND 100
			ENABLE
			RunToEnable = -1
;			DEBUGOUT "Sensor Enabled"
		} ENDIF
	} ENDIF

	; Home
	IF NEWTRACK then {
IF ( ( TgtType $= "PLANE" ) OR ( TgtType $= "HELO" ) ) THEN { Drop } ENDIF
		IF TgtType $= "CM" THEN {
			IF Spoofed > DecoyLogic THEN {
				IF Aim == 0 THEN { CounterMeasureIdx Fragments Aim += 1 } ENDIF
			SetTactic "TH_SSN27"
			} endif
		} ELSEIF Tgtconf > ZZZConf THEN {
			IF Aim == 0 THEN { CounterMeasureIdx Fragments Aim += 1 } ENDIF
			SetTactic "TH_SSN27"
		} ELSE {
			SetTactic "SBROS"
		} ENDIF
	} ENDIF

	IF NUM_TH == 0 THEN {
		IF Waypoints THEN {
			SETPRIORITY 220 TerminalHome
		} ELSE {
			SETPRIORITY 220 SetCrs PreenableCourse
			SETPRIORITY 220 SetAlt 50
		} ENDIF
	} else {
		SETPRIORITY 253 SETSPD maxspd
		if d and cd then {
			cd = 0
			tp = ( ownspd * 0.5144444 )
			tp = ( time + 3 + ( trng / tp ) )
		} elseif trng > 1000 then {
			d = 1
		} endif
		if ( trng > 2000 ) and ( trng < 6000 ) then {
			DELTA = ( TALT - OWNALT + 30 )
			temp = XYBRG ABS DELTA ( trng * 3 )
			if ( DELTA < 0 ) then { temp = ( -1 * temp ) } endif
			SETPRIORITY 253 SETPITCH TEMP
		} endif
	} endif
} ENDIF