; for 1.46 version
; rev.2
var PreenableCourse
var RunToEnable
var Runout
var lastX
var lastY
var run
var Waypoints
var Drowned
var Aim
VAR NUM_TH
VAR ZZZConf
var talt
var temp
var Delta
var FinalPhazeN
var DecoyLogic
var ReflectVal
var TP
var d
var cd
var tBrg
var OrderAlt
var Zig
var Mode
var SUPER
var FIRST
var LMPX
var LMPY
var SX
var SY
var Dist
var InitTime
var DeltaX
var PointDost
var Points
var Senon
var imitation
var vzvod
var wkl
var otkl
var trng
var homing
var Straight
var go
var old_t_alt
var rng
var alt
var tg
var ALTCRUISE
var Terminal
var mission
var percmalf
var weapeffectv
var crushevnt
var otkaztime
var detonpercnt
var dfs
var uwd
Func malfunction {
if ( ( ownalt > 0 ) and ( pitch > 45 ) ) then {
	SetPitch -80
} elseif ( ( ownalt > 0 ) and ( pitch < 45 ) ) then {
	SetPitch -60
	maxspd = 0
} 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 {
	DIST = 20000
	InitTime = Time
	TP = -1
	NUM_TH = 0
	ZZZConf = 0
	Drowned = 0
	Aim = 0
	lastX = OwnX
	lastY = OwnY
	SETPRIORITY 251
	SetAlt 48
        OrderAlt = 48
	SetSpd 580
;	RunToEnable = 100
	Runout = -1
	FinalPhazeN = 3500
	DecoyLogic = 80
	FIRST = 1
	points = 1
	SENON = 1
	delta = 1000000
	setsnorkel on
	dfs = -1
	RunOut = 300024
	Terminal = "TH_ST"
IF ( OwnName $= "3M-54E1 Club SSM AI" ) THEN { weapeffectv = 12 } ENDIF
IF ( OwnName $= "3M-54E1 Club ASCM" ) THEN { weapeffectv = 8.5 } ENDIF
} ELSE {
	IF FIRST THEN {
detonpercnt = rnd 10
if malfunctionweap then {
	percmalf = rnd 10
	if percmalf >= weapeffectv then {
		crushevnt = 1
		otkaztime = ( time + 20 + rnd 7180 )
	if percmalf > 9.5 then {
		otkaztime = ( Time + 1 )
		} endif
	} endif
} endif
If ( ( RunToEnable < 1 ) And ( WayPtX != 0 ) ) Then {
	Zig = 1
} ELSE {
	Straight = 1
} Endif
If Zig Then {
	LMPX = WayPtX
	LMPY = WayPtY
	SX = WayPtX
	SY = WayPtY
	if runout == 1852 then { mode = 1 SUPER = 1 Points = 1
		} ELSEif runout == 3704 then { mode = 2 SUPER = 1
		} ELSEif runout == 5556 then { mode = 3 SUPER = 1
		} ELSEif runout == 7408 then { mode = 4 SUPER = 1
		} ELSEif runout >= 9260 then { mode = 5 SUPER = 1
	} ELSE {
		mode = 1
		SUPER = 1
	} endif
} endif
	FIRST = 0
} ENDIF
} ENDIF
 	IF ( OwnAlt > -10 ) AND ( mission != 1 ) THEN {
;		DebugOut "Booster OFF"
 		SetPriority 255
 		SetSnorkel OFF
 		mission = 1
	} ENDIF
if ( crushevnt == 1 ) and ( time >= otkaztime ) then {
	if detonpercnt >= 9.5 then { dfs = 1 } else { dfs = 0 } endif
		malfunction
} 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
	IF OwnAlt >= ( MaxAlt + 600 ) THEN { Detonate } ENDIF
IF Straight AND NOT FIRST THEN {
	run += xyrng ( OwnX - lastX ) ( OwnY - lastY )
	lastX = OwnX
	lastY = OwnY
IF ( Runout != -1 ) AND ( run > Runout ) AND ( NUM_TH == 0 ) THEN { Runout = -1 SHUTDOWN } ENDIF
	IF RunToEnable != -1 THEN {
		IF run >= RunToEnable THEN {
			ReflectVal = RND 100
			ENABLE
			RunToEnable = -1
		} ENDIF
	} ENDIF
} ENDIF
IF ZIG AND NOT FIRST Then {
	SetSpd MaxSpd
DELTAX = xyrng ( OwnX - lastX ) ( OwnY - lastY )
run += DELTAX
lastX = OwnX
lastY = OwnY
IF ( Time >= InitTime ) and ( go == 0 ) THEN {
	go = 1
	IF SUPER THEN { otkl = 1
} ENDIF
	} ELSEIF ( Time < InitTime ) and ( ownalt > 0 ) THEN {
		SetPriority 250 SETCRS XYBRG ( WayPtX - OWNX ) ( WayPtY - OWNY )
} ENDIF
IF ( Homing == 0 ) and go THEN { SetPriority 250 TerminalHome } ENDIF
	IF not homing or ( trng > 4000 ) then {
	DELTA = ( TERRAINALT - old_t_alt )
	rng = 1100
	ALT = ( OWNALT - TERRAINALT )
	IF 70 < abs DELTA THEN { TG = ( TIME + 20 ) } ENDIF
	temp = XYBRG DELTA ( DELTAX * 3.28 )
old_t_alt = TERRAINALT
	if SUPER then {
		IF MODE != 5 THEN {
			IF NOT pointdost THEN {
				IF POINTS == MODE then {
				SX = WayPtX
				SY = WayPtY
				pointdost = 1
				} ENDIF
			} ELSE {
			DELTA = XYRNG ( SX - OWNX ) ( SY - OWNY )
				IF 9260 > DELTA THEN {
					if SENON THEN {
					SENON = 0
					SensorEnable "ARGS-54E1" 1
					} ENDIF

					IF NOT HOMING THEN {
					imitation = 1
					trng = DELTA
						if ( vzvod == -1 ) and ( delta < 270 ) then { vzvod = ( time + 1 )
						} endif

						if delta < 80 then { Detonate
						} endif
					} ELSE { imitation = 0
					} ENDIF
				} ENDIF
			} endif
		} ELSE {
		DELTA = XYRNG ( WayPtX - OWNX ) ( WayPtY - OWNY )
			IF 6000 > DELTA THEN {
				if SENON THEN {
				SENON = 0
				wkl = 1
				SensorEnable "ARGS-54E1" 1
				} ENDIF
				IF ( NOT HOMING ) and ( POINTS == 4 ) THEN {
				imitation = 1
				trng = DELTA
					if ( vzvod == -1 ) and ( delta < 270 ) then { vzvod = ( time + 1 )
					} endif
					if delta < 80 then { Detonate
					} endif
				} ELSE { imitation = 0
				} ENDIF
			} elseif wkl then { wkl = 0 otkl = 1
			} endif
		} ENDIF
	} endif
	IF ( LMPX != WayPtX ) OR ( LMPY != WayPtY ) THEN {
		points += 1
		LMPX = WayPtX
		LMPY = WayPtY
	} endif
if otkl then {
	otkl = 0
	SENON = 1
	SensorEnable "ARGS-54E1" 0
} endif
	IF LOSTTRACK THEN { Homing = 0 } endif
} endif
} ENDIF
IF NEWTRACK then {
	IF TgtType $= "CM" THEN {
		IF ReflectVal > DecoyLogic THEN {
		SetTactic "TH_ST"
		} ELSEIF ReflectVal <= DecoyLogic THEN {
		Drop
	} endif
} ELSEIF Tgtconf > ZZZConf THEN {
	SetTactic Terminal
} ELSE {
	SetTactic "SBROS"
	} ENDIF
} ENDIF
IF NUM_TH == 0 THEN {
	IF Waypoints THEN {
		SETPRIORITY 220 TerminalHome
	} ELSE {
		SETPRIORITY 220 SetCrs PreenableCourse
		SETPRIORITY 220 SetAlt 48
	} 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