; MG-74ME Korund Doctrine for DWX ver. 1.41

var CircleSearchPattern
var AcousticMode
var PreenableCourse
var PreenableSpeed
var PreenableDepth
var OperateDepth
var SpeedControl
var DepthControl
var CorrInterogg
var periodcount
var SearchDepth
var RunToEnable
var auto_mode_1
var auto_mode_2
var auto_mode_3
var wire_mode
var auto_mode
var avtospd_1
var t_spdhOFF
var mounted1
var mounted2
var mounted3
var impulse
var Ceiling
var enabled
var InitAlt
var mission
var fsrmin
var spdmin
var maxspd
var preset
var RunOut
var OrdSpd
var t_spdl
var ratio
var Floor
var Delay
var InitX
var InitY
var spd_h
var spd_l
var fuel
var lock
var dmin
var dmax
var run
var fsr
var rte
var spd
func counter {
if not lock then {
	delay = ( time + 4 )
	lock = 1
} endif
}
if init then {
	PreenableCourse = OwnCrs
	OperateDepth = OwnAlt
	SearchDepth = SearchDepth
	RunToEnable = 1
	SpeedControl = 0
	DepthControl = 0
	InitAlt = ownalt
	RunOut = 1
	Preset = 1
	fuel = 1000
	InitX = OwnX
	InitY = OwnY
	RTE = 1
	setsnorkel on
} else {
if not enabled then {
if ( rte > 1 ) and ( run > rte ) then {
	if not wire_mode then {
		enabled = -1
		rte = -1
		auto_mode = 1
		OperateDepth = SearchDepth
If ( CircleSearchPattern == 0 ) then {
	auto_mode_1 = 1
	OrdSpd = ABS ( avtospd_1 / 3.28 )
} endif
If ( CircleSearchPattern == 0 ) and ( AcousticMode == 0 ) then {
	auto_mode_2 = 1
} endif
If ( CircleSearchPattern == 1 ) then {
	auto_mode_3 = 1
	OrdSpd = ABS ( avtospd_1 / 3.28 )
} endif
	} endif
} endif
} endif
if preset then {
	avtospd_1 = Ceiling
 	if ceiling > -31 then {
		ceiling = -33
	} endif
if ( initalt < minalt ) then {
	initalt = minalt
} endif
	spdmin = 1
	dmin = ( 37100 / 1852 )
	dmax = ( 74200 / 1852 )
	fsrmin = ( fuel / ( dmin / ( spdmin / 3600 ) ) )
	ratio = ( ( ( fuel / ( dmax / ( maxspd / 3600 ) ) ) - fsrmin ) / ( maxspd - spdmin ) )
	preset = 0
	OrdSpd = preenablespeed
	rte = RunToEnable
if ( searchdepth == -36 ) or ( searchdepth == -364 ) or ( searchdepth == -692 ) or ( searchdepth == -1020 ) or ( searchdepth == -1348 ) or ( searchdepth == -1676 ) then {
	wire_mode = 1
} endif
} endif
	CorrInterogg = ( Time - CorrInterogg )
if ( ( ownalt < 0 ) and ( ( ownspd >= ( spdmin - 1 ) ) or ( ownspd <= ( maxspd + 1 ) ) ) ) then {
	fsr = ( fsrmin + ( ( ownspd - spdmin ) * ratio ) )
	fsr = ( fsr * corrInterogg )
	fuel = ( fuel - fsr )	
} endif
	CorrInterogg = Time
If ( auto_mode_3 == 0 ) and ( auto_mode_2 == 0 ) then {
	SetCrs PreenableCourse
	SetSpd OrdSpd
	SetAlt OperateDepth
} endif
	Run += XYRng ( OwnX - InitX ) ( OwnY - InitY )
		InitX = OwnX
		InitY = OwnY
IF ( RunToEnable != 1 ) then {
	IF ( RunToEnable == 0 ) then {
		SpeedControl = ( SpeedControl + 1 )
	} endif
	IF ( RunToEnable == -1 ) then {
		DepthControl = ( DepthControl + 1 )
	} endif
		RunToEnable = 1
} endif
; ACTIVE SONAR SWITCH
if ( SpeedControl == 3 ) then {
		counter
	if not impulse then {
		if ( Time >= Delay ) then {
;	DebugOut "Active Sonar ON"
		enable
		impulse = 1
	} endif
} endif
} elseif ( SpeedControl == 4 ) and impulse then {
;	DebugOut "Active Sonar OFF"
	preenable
	impulse = 0
} endif
; DEPTH AND PING CONTROL
	if ( SpeedControl > 0 ) then {
		if ( SpeedControl == 1 ) then {
			OperateDepth = Ceiling
;			DebugOut "Assign Upper Depth"
		} elseif ( SpeedControl == 2 ) then {
			OperateDepth = Floor
;			DebugOut "Assign Lower Depth"
		} elseif ( SpeedControl == 3 ) then {
;			DebugOut "Pinging At Current Depth"
			OperateDepth = OwnAlt
		} elseif ( SpeedControl == 4 ) then {
;			DebugOut "Stop Pinging"
			OperateDepth = OwnAlt
		} elseif ( SpeedControl >= 5 ) then {
;			DebugOut "Assign Initial Depth, Ready To New Cycle"
			OperateDepth = InitAlt
			SpeedControl = 0
			lock = 0
		} endif
	} endif
; battery discharge by active sonar impulse
if ( SpeedControl == 3 ) then {
	if Time >= PeriodCount then {
		if Time > Delay then {
			fuel = ( fuel - 20 )
	                PeriodCount = ( Time + 13 )
		} endif
	} endif
} endif
; SPEED CONTROL - VARIABLE VALUE
	if AcousticMode == 0 then {
	IF ( DepthControl == 1 ) then {
		if not mounted1 then { OrdSpd = ( 1 + rnd 4 ) mounted1 = 1 } endif
	} elseif ( DepthControl == 2 ) then {
		if not mounted2 then { OrdSpd = ( 6 + rnd 4 ) mounted2 = 1 } endif
	} elseif ( DepthControl == 3 ) then {
		if not mounted3 then { OrdSpd = ( 10 + rnd 6 ) mounted3 = 1 } endif
	} elseif ( DepthControl == 4 ) then {
		OrdSpd = 17
	} elseif ( DepthControl >= 5 ) then {
		OrdSpd = 0
		DepthControl = 0
		mounted1 = 0
		mounted2 = 0
		mounted3 = 0
	} endif
} endif
; SPEED CONTROL - DISCRETE VALUE
if AcousticMode == 1 then {
	if ( DepthControl == 1 ) then {
		OrdSpd = 2
	} elseif ( DepthControl == 2 ) then {
		OrdSpd = 5
	} elseif ( DepthControl == 3 ) then {
		OrdSpd = 10
	} elseif ( DepthControl == 4 ) then {
		OrdSpd = 17
	} elseif ( DepthControl >= 5 ) then {
		OrdSpd = 0
		DepthControl = 0
	} endif
} endif
	if ( fuel <= 0 ) AND ( RunOut != -1 ) then {
;		DebugOut "fuel lost, remove unit"
		runout = -1
		shutdown
	} endif
} endif
if auto_mode then {
	if auto_mode_3 then {
		SetPriority 255
		SetRudder 0.01
		SetSpd OrdSpd
		SetAlt OperateDepth
	} endif
if auto_mode_2 then {
	IF ( TIME > t_spdhOFF ) THEN {
		t_spdl = ( time + 90 + ( rnd 90 ) )
		t_spdhOFF = ( t_spdl + 480 + ( rnd 480 ) )
		spd_l = ( 2 + rnd 5 )
		spd_h = ABS ( avtospd_1 / 3.28 )
	} endif
if time > t_spdl then { spd = spd_h } else { spd = spd_l } endif
	SetSpd spd
	SetCrs PreenableCourse
	SetAlt OperateDepth
	} endif
} endif
if ( ownspd <= ( preenablespeed + 1 ) ) and ( mission != 1 ) then {
	setpriority 255
	setsnorkel off
	mission = 1
} endif
