
; Select unit and give orders

~1

	#Start
	~0.01
	@MSX < 0.915  &&  MSY > 0.036  &&  !PLACING
	
	? LBUTTON : goto "CheckPos"
	? SELECTED != camtarget && !(SELECTED in BUILT) && RBUTTON : goto "CheckPos"
	
	goto "Start"
	
	
	
	
	
	
	#CheckPos
	selection setPos [CUX, CUY, CUZ]

	
	
	#DetectAction
	_objects = TRAINED + ENEMIES
	? count _objects == 0 : goto "Start"
	_distances = []
	
	_i=0
	#MakeDistances
	_dist = selection distance (_objects select _i)
	_distances = _distances + [_dist]
	? _i < count _objects - 1 : _i=_i+1; goto "MakeDistances"
	
	_sorted = [[_distances,_objects], 0, count _distances - 1] call quicksort2
	_sorted1 = _sorted select 1
	
	_firstDist = (_sorted select 0) select 0
	_firstUnit = (_sorted select 1) select 0
	
	? LBUTTON : goto "Mark"
	? side SELECTED != MYSIDEVAR : goto "Start"
	? RBUTTON && _firstDist < 1.5 && side _firstUnit != MYSIDEVAR : goto "Attack"
	? RBUTTON : goto "Move"
	goto "Start"
	
	
	
	
	
	
	#Mark
	? _firstDist < 2 : goto "SelectedUNIT"
	
		_i = 0
		_range = 9
		#CheckBuilding
		_building = BUILT select _i
		_x1 = (getPos _building select 0) - _range
		_x2 = (getPos _building select 0) + _range
		_y1 = (getPos _building select 1) - _range
		_y2 = (getPos _building select 1) + _range
		? CUX > _x1 && CUX < _x2  &&  CUY > _y1 && CUY < _y2 : _building exec "Building\Mark.sqs"; goto "ReleaseLMB"
		? _i < count BUILT - 1 : _i=_i+1; goto "CheckBuilding"
	
	SELECTED = camtarget
	goto "ReleaseLMB"
	
	
	#SelectedUNIT
	_unit = ((_sorted select 1) select 0)
	? _unit != SELECTED  &&  SIDE _unit == MYSIDEVAR : playSound "YesSir"
	SELECTED = ((_sorted select 1) select 0)
	
	#ReleaseLMB
	@!LBUTTON
	goto "Start"
	
	
	
	
	
	
	
	#Move
	_id = SELECTED call findID
	COMMANDS set [_id, 1]
	
	_wp = WAYPOINTS select _id
	_wp setPos getpos SELECTION
	[_wp,SELECTED] exec "Unit\FinishMove.sqs"
	
;	_height = [selection,camtarget] call distZ
;	_height = _height * 100

;	? MSY < 50 : _fy = (0.009467 * MSY^2) + (-1.317333 * MSY) + 42.2
;	? MSY >= 50 : _fy = (0.002189 * MSY^2) + (-0.6315 * MSY) + 25.961111
;	_findA = 0.009411 * _fy + 0.444194
;	_findB = -0.470543 * _fy - 22.209643
;	_fx = _findA * MSX + _findB
	
;	_fx = _fx - _height
;	_fy = _fy - _height
	
;	selection setPos [(getpos camtarget select 0) + _fx, (getpos camtarget select 1) + _fy	, 0]
	
	playSound "Acknowledged"
	
	_dir = (((getpos selection select 0) - (getpos SELECTED select 0)) atan2 ((getpos selection select 1) - (getpos SELECTED select 1)))
	? _dir < 0 : _dir=_dir+360
	SELECTED setFormDir _dir
	
	? SELECTED distance selection < 5.5 : _dir exec "Unit\ForceMove.sqs"
	SELECTED move getpos selection
	Drop ["cl_water","","Billboard",1,2,[0,0,0],[0,0,0.02],0,1.2,1.1,0,[1.2,0,0],[[0.8,0.8,0,1]],[0,1,0],1,0.8,"","",SELECTION]
	@!RBUTTON
	goto "Start"
	
	
	
	
	
	
	#Attack
	_id = SELECTED call findID
	COMMANDS set [_id, 2]
	
	_wp = WAYPOINTS select _id
	_wp setPos getpos _firstUnit
	
	playSound "Engage"
	[SELECTED,_firstUnit,_wp] exec "Unit\Attack.sqs"
	_firstUnit exec "Unit\DropRedParticle.sqs"
	@!RBUTTON
	goto "Start"
	
	