; Reinforce Alert AI Shkval Doctrine v.1.41

var RunToEnable
var PreenableCourse
var SearchDepth
var Homing
var run
var Enabled
var RunOnceDone
var lastX
var lastY
var ImpactTime
var CrushDpth
var Explode
var TorpMaxDepth
var WNA
var WPS
var MxRng
var REGIME
var RTB
var InitTime
var RP
var raz

IF INIT THEN {
	lastX = OwnX
	lastY = OwnY
	Explode = ( RND 100 )
	ImpactTime = 0
	CrushDpth = 0
	Homing = 0
	SetAlt OwnAlt
	Enabled = 0
	RunOnceDone = 1
	RUN = 0
	WNA = 1
	InitTime = ( Time + 3 )
	RTB = ( RND 100 )
IF ( RTB >= 99 ) Then {
	REGIME = 1 ; Mad Detection Mode
} ELSE {
	REGIME = 2 ; By Target Range Detonation
} Endif
IF ( OwnName $= "Shkval Torpedo" ) Then { TorpMaxDepth = -1435 MxRng = 12501
} ELSEif ( OwnName $= "Great Prophet Torpedo" ) Then { TorpMaxDepth = -656 MxRng = 9999
} ELSEif ( OwnName $= "Barracuda Torpedo" ) Then { TorpMaxDepth = -984 MxRng = 10995 } ENDIF
} ELSE {
IF RunOnceDone THEN {
	RunOnceDone = 0
	SearchDepth = OwnAlt
	IF SearchDepth < TorpMaxDepth THEN { SearchDepth = ( TorpMaxDepth + 10 ) } ENDIF
	IF SearchDepth > -30 THEN { SearchDepth = -30 } ENDIF
	IF RunToEnable < 1851 THEN { RunToEnable = 1851 } ENDIF
} Endif
; ------------------------------------------------------------
If ( TgtSource $= "Visual" ) Then {
; This is SNAPSHOT against incoming torpedo,target range unknown. 
	Regime = 1
} Endif
IF ( REGIME == 2 ) AND NOT RAZ THEN {
; Range To Target Precisely Known
	RP = TgtRng
	RunToEnable = RP
	raz = 1
} ELSEIF ( REGIME == 1 ) AND NOT RAZ THEN {
	raz = 1
	Drop
} endif
IF NEWTRACK AND ( Homing == 0 ) THEN {
	If ( TIME > InitTime ) Then {
		IF ( ( TgtClass $= "Weap" ) AND ( TgtType $= "CM" ) ) THEN {
			wps = 1
			Detonate
			TimedDetonateTgt ( Time + 1 )
		} ENDIF
IF ( ( TgtClass $= "Mine" ) AND ( TgtType $= "Mine" ) ) THEN {
		wps = 1
		Detonate
	} ENDIF
		IF NOT WPS THEN {
			SetTactic "ShkvalAttack" Homing = 1
		} ENDIF
	} ENDIF
} ENDIF
; -------------------------------------------------------------
IF WNA THEN {
	SETPRIORITY 240
	SetSpd 20
	IF ( abs ( OwnCrs - PreEnableCourse ) < 1 ) AND ( abs ( OwnAlt - SearchDepth ) < 30 ) AND ( 0.5 > abs pitch ) THEN {
		WNA = 0
		SetPersist -1
		SETPRIORITY 250
		SetSpd MaxSpd
	} ENDIF
} ENDIF
	IF ( RUN > MxRng ) THEN { SETPRIORITY 255 DETONATE } ENDIF
; Factor Exceeding Maximum Depth
	IF ( OwnAlt <= TorpMaxDepth ) AND NOT CrushDpth THEN {
		CrushDpth = 1
		ImpactTime = ( Time + 10 )
;		DebugOut OwnName
;		DebugOut "Shkval Exceeding Maximum depth"
	} ENDIF
IF ( ( CrushDpth == 1 ) AND ( Time >= ImpactTime ) ) THEN {
; DebugOut "SuperCavitation Torpedo Has Been destroyed, depth high pressure"
	IF ( Explode >= 50 ) THEN { Detonate } ELSE { Shutdown } ENDIF
} ENDIF	
	run += xyrng ( OwnX - lastX ) ( OwnY - lastY )
	lastX = OwnX
	lastY = OwnY
IF NOT Enabled AND ( run > RunToEnable ) THEN {
	If Time > InitTime Then {
		If Regime == 2 Then {
			SETPRIORITY 255 Detonate
		} ELSE {
			Enabled = 1
			ENABLE
		} ENDIF 
	} ENDIF
} ENDIF
	SETPRIORITY 240
	SetCrs PreEnableCourse
	SETPRIORITY 240
	SetAlt SearchDepth
} ENDIF