//uber air patrol for serious flyers

_user = _this select 0;
_var = _this select 1;
_usergroup = (group player);
_pos = [0,0,0];
_starts = [];
_obj1 = [];
_guard=grpNull;

_txtcommon = localize "STR_M04t0_5";
_txtfailhead = localize "STR_M04t2_6";//failed
_txtpasshead = localize "STR_M04t1_9";//complete
_txtpassbody = format["+%1 %2",_var,_txtcommon];

_MissionBriefingLong = localize "STR_M04t13_1";
_MissionBriefingHUD = localize "STR_M04t5_1";
_MissionBriefingShort = localize "STR_M04t12";
_txtfailbody = localize "STR_M04t14_1";

_deletemapat =
{
	EGG_EVO_Onmission=false;
	player removeSimpleTask _obj1;
	sleep 12.0;
	_handle = [_guard] execVM "scripts\delete.sqf";
};

_killpilots =
{
	_chop = _this select 0;
	_crew = _this select 1;
	waitUntil {( (({alive _x} count crew _chop) < 1) || (not (canmove _chop)) )};
	sleep 1.0;
	{_x setDamage 1} foreach _crew;
};

_Fail = 
{
	_obj1 settaskstate "Failed";
	["#FF0000",_txtfailhead,_txtfailbody] call EGG_EVO_Message;
	[] call _deletemapat;
};

[West,"HQ"] SideChat localize "STR_M04t64";
_pos = getmarkerpos "centerp";

_obj1 = player createSimpleTask ["obj1"];
_obj1 setSimpleTaskDescription [_MissionBriefingLong,_MissionBriefingShort,_MissionBriefingHUD];
_obj1 setSimpleTaskDestination _pos;
_obj1 settaskstate "Created";
player setCurrentTask _obj1;

//map
[1800] execVM "scripts\missions\timer.sqf";
_mapview = [_pos,0] execVM "scripts\missions\mismap.sqf";

waitUntil {player distance _pos < 2500 or not EGG_EVO_Onmission};	
if (not EGG_EVO_Onmission) exitwith {[] call _Fail};

_guard = createGroup (east);

_allvecA = EGG_EVO_eastveh1;//unarmed choppers
_allvecB = EGG_EVO_eastveh2;//armed choppers
_allvecC = EGG_EVO_eastveh9;//planes
_maxA = (count _allvecA)-1;
_maxB = (count _allvecB)-1;
_maxC = (count _allvecC)-1;

_heli = createVehicle [(_allvecA select round random _maxA), _pos, [], 2500, "FLY"];Sleep EGG_EVO_GlobalSleep;
_heli1 = createVehicle [(_allvecA select round random _maxA), _pos, [], 2500, "FLY"];Sleep EGG_EVO_GlobalSleep;

_heli2 = createVehicle [(_allvecB select round random _maxB), _pos, [], 2500, "FLY"];Sleep EGG_EVO_GlobalSleep;
_heli3 = createVehicle [(_allvecB select round random _maxB), _pos, [], 2500, "FLY"];Sleep EGG_EVO_GlobalSleep;
_heli4 = createVehicle [(_allvecB select round random _maxB), _pos, [], 2500, "FLY"];Sleep EGG_EVO_GlobalSleep;
_heli5 = createVehicle [(_allvecB select round random _maxB), _pos, [], 2500, "FLY"];Sleep EGG_EVO_GlobalSleep;
_heli6 = createVehicle [(_allvecB select round random _maxB), _pos, [], 2500, "FLY"];Sleep EGG_EVO_GlobalSleep;

_heli7 = createVehicle [(_allvecC select round random _maxC), _pos, [], 2500, "FLY"];Sleep EGG_EVO_GlobalSleep;
_heli8 = createVehicle [(_allvecC select round random _maxC), _pos, [], 2500, "FLY"];Sleep EGG_EVO_GlobalSleep;
_heli9 = createVehicle [(_allvecC select round random _maxC), _pos, [], 2500, "FLY"];Sleep EGG_EVO_GlobalSleep;
_heli10 = createVehicle [(_allvecC select round random _maxC), _pos, [], 2500, "FLY"];Sleep EGG_EVO_GlobalSleep;
_heli11 = createVehicle [(_allvecC select round random _maxC), _pos, [], 2500, "FLY"];Sleep EGG_EVO_GlobalSleep;

_allunits = EGG_EVO_mepilot;
_maxu = (count _allunits)-1;

//calculate crew
{
	[_x] call EGG_EVO_Lock;
	_spawne = [_x] spawn {[_this select 0] call EGG_EVO_idelSVEC};
	_x addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}];

	if((_x emptyPositions "driver") > 0) then 
	{
		_unit = _guard createUnit [(_allunits select round random _maxu), _pos, [], 5, "FORM"];
		_unit AssignasDriver _x;
		_unit moveinDriver _x;
	};
	Sleep 0.1;
	if((_x emptyPositions "commander") > 0) then 
	{
		_unit = _guard createUnit [(_allunits select round random _maxu), _pos, [], 5, "FORM"];
		_unit AssignasCommander _x;
		_unit moveinCommander _x;
	};
	Sleep 0.1;
	_x engineon true;
} forEach [_heli,_heli1,_heli2,_heli3,_heli4,_heli5,_heli6,_heli7,_heli8,_heli9,_heli10,_heli11];

//add cargo to choppers
{
	_type = Typeof _x;
	_maxcrew = getNumber (configFile >> "CfgVehicles" >> _type >> "transportSoldier");
	_n = 0;
	while {_n < _maxcrew} do 
	{
		_unit = _guard createUnit [(_allunits select round random _maxu), _pos, [], 5, "FORM"];
		_unit AssignasCargo _x;
		_unit moveinCargo _x;
		_n = _n+1;
		Sleep 0.1;
	};
} forEach [_heli,_heli1,_heli2,_heli3,_heli4,_heli5,_heli6];

//add gunners to armed aircraft
{
	if((_x emptyPositions "gunner") > 0) then 
	{
		_unit = _guard createUnit [(_allunits select round random _maxu), _pos, [], 5, "FORM"];
		_unit AssignasGunner _x;
		_unit moveinGunner _x;
	};		
	Sleep 0.1;
} forEach [_heli2,_heli3,_heli4,_heli5,_heli6,_heli7,_heli8,_heli9,_heli10,_heli11];

_pos1 = position _heli;
_pos2 = position _heli1;
_pos3 = position _heli2;
_pos4 = position _heli3;
_pos5 = position _heli4;
_pos6 = position _heli5;
_pos7 = position _heli6;
_pos8 = position _heli7;
_pos9 = position _heli8;
_pos10 = position _heli9;
_pos11 = position _heli10;
_pos12 = position _heli11;

_starts = [_pos1,_pos2,_pos3,_pos4,_pos5,_pos6,_pos7,_pos8,_pos9,_pos10,_pos11,_pos12];

{_killh1 = [_x,crew _x] spawn {[_this select 0,_this select 1] call _killpilots}} forEach [_heli,_heli1,_heli2,_heli3,_heli4,_heli5,_heli6,_heli7,_heli8,_heli9,_heli10,_heli11];

_guard setCombatMode "RED";
_guard setBehaviour "COMBAT";

{_x addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}]} forEach (units _guard);
_recy = [_user,_guard] execVM "scripts\grecycle.sqf";

_wp1 = _guard addWaypoint [_pos1, 100];
_wp2 = _guard addWaypoint [_pos2, 100];
_wp3 = _guard addWaypoint [_pos3, 100];
_wp4 = _guard addWaypoint [_pos4, 100];
_wp5 = _guard addWaypoint [_pos5, 100];
_wp6 = _guard addWaypoint [_pos6, 100];
_wp7 = _guard addWaypoint [_pos7, 100];
_wp8 = _guard addWaypoint [_pos8, 100];
_wp9 = _guard addWaypoint [_pos9, 100];
_wp10 = _guard addWaypoint [_pos10, 100];
[_guard, 10] setWaypointType "CYCLE";

_pos = (_starts select (round random 11));

_i=0;
while {(_i < (count _starts))} do 
{
	_npos = (_starts select _i);
	[_usergroup, 1] setWaypointPosition [_npos, 0];
	_search setMarkerPosLocal _npos;
	_usergroup setCurrentWaypoint [_usergroup, 1];	
	_obj1 setSimpleTaskDestination  _npos;

	Waituntil {( (player distance _npos < 300) or (({alive _x} count units _guard) < 1) or (not EGG_EVO_Onmission) )}; 
	_i = _i + 1;
	if (not EGG_EVO_Onmission) then {_i=100};
	if (({alive _x} count units _guard) < 1) then {_i=100};
	if (not (alive _heli) and not (alive _heli1) and not (alive _heli2) and not (alive _heli3) and not (alive _heli4) and not (alive _heli5) and not (alive _heli6) and not (alive _heli7) and not (alive _heli8) and not (alive _heli9) and not (alive _heli10) and not (alive _heli11)) then {_i=100}; 
	if ( (_i == 11) and (({alive _x} count units _guard) > 0) ) then {_i=0};
};

if (not EGG_EVO_Onmission) exitwith {[] call _Fail};

{if(isplayer _x) then {_x doFollow leader _x}} foreach units (group player);
["#FF0000",_txtpasshead,_txtpassbody] call EGG_EVO_Message;
[West,"HQ"] SideChat localize "STR_M04t65";playSound "Goodjob";
EGG_EVO_punit = _usergroup;
EGG_EVO_prew  = _var;
publicVariable "EGG_EVO_prew";
publicVariable "EGG_EVO_punit";
_obj1 settaskstate "Succeeded";
sleep 1.0;
[] call _deletemapat;


