; $Header: SubAvoidSub.txt   Mon Jan 19 11:53:11 EST 2004   mike $
; $Revision: 2 $
; $Copyrt1: Copyright (c) 2003, Sonalysts, Inc.  All rights reserved. $
;
; SubAvoidSubDef

var PreventingSnorkel
var PreventingComms
var LegTimer
var DoctrinePriority

func AvoidTgt {
	; steer evasive
	IF TIME > LegTimer THEN {
		LegTimer = ( 480 + RND 120 )

		SETPERSIST LegTimer
		IF TgtID $= "HOSTILE" THEN {
			SteerEvasive
			LegTimer = ( 480 + RND 120 )
			; DebugOut "Avoiding Hostile Sub"
		} ELSE {
			SteerAbeam
			LegTimer = ( 180 + RND 120 )
			; DebugOut "Avoiding Non-Hostile Sub"
		} ENDIF

		; Set Depth Opposite Layer
		 IF TgtAlt < LAYER THEN {
		 		SETALT ( -100 - RND 50 )
			} ELSE {
				; set depth to 300 ft below layer
				SETALT ( LAYER - 200 ) 
			} ENDIF

		; setmin avoid speed
		IF OWNSPD < 6 THEN {
			SETSPD 10
		} ENDIF 

		LegTimer += TIME
	} ENDIF
}

IF Init THEN {

	PreventingSnorkel = true
	PreventingComms = true
	SetEntVar "TooBusyForComms" ( GetEntVar "TooBusyForComms" + 1 )
	SetEntVar "TooBusyToSnorkel" ( GetEntVar "TooBusyToSnorkel" + 1 )

} ELSEIF Not LostTrack THEN {
	
	SetPriority DoctrinePriority

	IF tgtrng < 10000 THEN {

		IF TgtID $= "HOSTILE" OR ( TgtSource $= "Active Intercept" ) THEN {
			IF NOT ( Order $= "Track" ) AND Not ( TgtID $= "FRIEND" ) THEN {
;				DebugOut "Switching from Avoid to Attack"
				IF PreventingSnorkel THEN {
					SetEntVar "TooBusyForComms" ( GetEntVar "TooBusyForComms" - 1 )
					SetEntVar "TooBusyToSnorkel" ( GetEntVar "TooBusyToSnorkel" - 1 )
				} ENDIF
				SetTactic "SubAtkSub"
			} ELSE {
;				DebugOut "Avoiding Active Intercept Contact or Unassigned Contact"
				AvoidTgt
			} ENDIF
		} ELSE {
			IF tgtrng < 2000 THEN {
				AvoidTgt
			} ENDIF
		} ENDIF

	} ELSE {

		IF PreventingSnorkel THEN {
			PreventingSnorkel = false
			PreventingComms = false
			SetEntVar "TooBusyForComms" ( GetEntVar "TooBusyForComms" - 1 )
			SetEntVar "TooBusyToSnorkel" ( GetEntVar "TooBusyToSnorkel" - 1 )
		} ENDIF

	} ENDIF
} ENDIF

