//destroy armour mission

_user = _this select 0;
_var = _this select 1;
_usergroup = (group player);
_allobj = [];
_pos = [0,0,0];
_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_M04t21_0";
_MissionBriefingHUD = localize "STR_M04t19";
_MissionBriefingShort = localize "STR_M04t20";
_txtfailbody = localize "STR_M04t22_0";

_deletemdst =
{
	EGG_EVO_Onmission=false;
	player removeSimpleTask _obj1;
//	{_x removeSimpleTask _obj1} forEach (units _usergroup);
	sleep 12.0;
	_handle = [_guard] execVM "scripts\delete.sqf";
};

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

[West,"HQ"] SideChat localize "STR_M04t64";
if(_var == 10) then
{	
	_allobj = ["mrnd26","mrnd5","mrnd15","mrnd17","mrnd29"];
};
if(_var == 20) then
{
	_allobj = ["mrnd22","mrnd23","mrnd24","mrnd16","mrnd15","mrnd5","mrnd17","mrnd12","mrnd31","mrnd10","mrnd28","mrnd4"];
};
_maxo = count _allobj;
_r = round random (_maxo - 1);
_rand =  _allobj select _r;
_pos = getMarkerPos _rand;

_obj1 = player createSimpleTask ["obj1"];
//{_obj1 = _x createSimpleTask ["obj1"]} forEach (units _usergroup);
_obj1 setSimpleTaskDescription [_MissionBriefingLong,_MissionBriefingShort,_MissionBriefingHUD];
_obj1 setSimpleTaskDestination _pos;
_obj1 settaskstate "Created";
player setCurrentTask _obj1;
//{_x setCurrentTask _obj1} forEach (units _usergroup);

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

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

_guard = createGroup (east);

_allvecA = EGG_EVO_mevmeda;//tanks or bmps
_allvecB = EGG_EVO_eastveh10;//AA
if(_var == 10) then
{
	_allvecA = EGG_EVO_mevmeda;
	_sk=3;
};
if(_var == 20) then
{
	_allvecA = EGG_EVO_mevheavya;
	_sk=4;
};

_maxA = (count _allvecA)-1;
_maxB = (count _allvecB)-1;

_allunitsC = EGG_EVO_mecrew;
_maxC = (count _allunitsC)-1;

_allunits = EGG_EVO_meguardc;
_max = (count _allunits)-1;

_heli = createVehicle [(_allvecA select round random _maxA), _pos, [], 200, "NONE"];Sleep EGG_EVO_GlobalSleep;
_pos = position _heli;
_heli1 = createVehicle [(_allvecA select round random _maxA), _pos, [], 200, "NONE"];Sleep EGG_EVO_GlobalSleep;
_heli2 = createVehicle [(_allvecB select round random _maxB), _pos, [], 200, "NONE"];Sleep EGG_EVO_GlobalSleep;

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

	if((_x emptyPositions "driver") > 0) then 
	{
		_unit = _guard createUnit [(_allunitsC select round random _maxC), _pos, [], 5, "FORM"];
		_unit AssignasDriver _x;
		_unit moveinDriver _x;
	};
	Sleep 0.1;
	if((_x emptyPositions "commander") > 0) then 
	{
		_unit = _guard createUnit [(_allunitsC select round random _maxC), _pos, [], 5, "FORM"];
		_unit AssignasCommander _x;
		_unit moveinCommander _x;
	};
	Sleep 0.1;
	if((_x emptyPositions "gunner") > 0) then 
	{
		_unit = _guard createUnit [(_allunitsC select round random _maxC), _pos, [], 5, "FORM"];
		_unit AssignasGunner _x;
		_unit moveinGunner _x;
	};		
	Sleep 0.1;
	_x engineon true;
	_type = Typeof _x;
	_maxcrew = getNumber (configFile >> "CfgVehicles" >> _type >> "transportSoldier");
	_n = 0;
	while {_n < _maxcrew} do 
	{
		_unit = _guard createUnit [(_allunits select round random _max), _pos, [], 5, "FORM"];
		_unit AssignasCargo _x;
		_unit moveinCargo _x;
		_n = _n+1;
		Sleep 0.1;
	};
} forEach [_heli,_heli1,_heli2];

_j = 0;
while {_j <= Squadsize} do 
{
	_unit = _guard createUnit [(_allunitsC select round random _maxC), _pos, [], 30, "FORM"];Sleep EGG_EVO_GlobalSleep;
	_j = _j+1;
};

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

[_guard, _sk] spawn EGG_EVO_skill;

_guard setFormation "DIAMOND";
_guard setBehaviour "AWARE";
_guard setCombatMode "RED";
sleep 30;
{commandStop _x}forEach (units _guard);

for [{_loop=0}, {_loop<1}, {_loop=_loop}] do
{
	sleep 1.001;
	_obj1 setSimpleTaskDestination  position (leader _guard);
	if ( (({alive _x} count units _guard) < 1) or (not EGG_EVO_Onmission) ) then {_loop=1;};
};

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 _deletemdst;
