
; Unit on guard


	_unit = _this select 0
	_destination = "logic" camCreate [0,0,0]
	_destination setPos [(_this select 1), (_this select 2), 0]
	
	_isEngaging = false
	_madeToBase = false
	
	_magazine = "dune_M16_mag"
	_engageDIST = 15
	? "dune_RL" in weapons _unit : _engageDIST=40
	


	
	#Wait
	~0.01
	? !alive _unit : exit
	


	#Search
	? count TRAINED == 0 : goto "Continue"
	_listUnits =+ TRAINED
	
		_i = 0
		_distances = []
		#MakeDistances
		_dist = _unit distance (_listUnits select _i)
		_distances = _distances + [_dist]
		? _i < count _listUnits - 1 : _i=_i+1; goto "MakeDistances"

		_sorted = [[_distances,_listUnits], 0, count _distances - 1] call quicksort2
	
		_firstDist = (_sorted select 0) select 0
		_firstEnem = (_sorted select 1) select 0

		? _firstDist < _engageDIST : goto "Engage"
		
	#Continue
	_unit setCombatMode "BLUE"
;	_isEngaging = false
;	? _unit distance _destination > 6  &&  !_madeToBase : _unit move getPos _destination
;	? _unit distance _destination < 6 : _madeToBase = true
;	? _madeToBase  && speed _unit == 0 : _unit exec "Enemy\AttackBuilding.sqs"; exit
	goto "Wait"
	
	
	
	
	#Engage
	_unit move getpos _unit
	_unit setCombatMode "YELLOW"
	_unit reveal _firstEnem
	_unit doTarget _firstEnem
	_isEngaging = true
	
	@!alive _unit  ||  !alive _firstEnem  ||  _unit distance _firstEnem > _engageDIST
	goto "Wait"