//ambush convoy side 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";//points
_txtfailhead = localize "STR_M04t2_6";//failed
_txtpasshead = localize "STR_M04t1_9";//complete
_txtpassbody = format["+%1 %2",_var,_txtcommon];

_MissionBriefingLong = localize "STR_M04t17_1";//A supply convoy has been identified. We are tracking its movements and want you to eliminate it. Leave no survivors.
_MissionBriefingHUD = localize "STR_M04t15_0";//DESTROY CONVOY
_MissionBriefingShort = localize "STR_M04t16_0";//Ambush

_txtfailbody = localize "STR_M04t18_0";//The convoy reached it's destination.

_deleteass =
{
	EGG_EVO_Onmission=false;
	sleep 12.0;
	_handle = [_guard] execVM "scripts\delete.sqf";
	player removeSimpleTask _obj1;
//	{_x removeSimpleTask _obj1} forEach (units _usergroup);
	sleep 90;
	{deletevehicle _x} forEach [_heli,_heli1,_heli2,_heli3];
};

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

[West,"HQ"] SideChat localize "STR_M04t64";//GOOD LUCK WITH THE MISSION

if(_var == 10) then
{	
	_allobj = [["mrnd1","mrnd2"],["mrnd3","mrnd4"],["mrnd5","mrnd6"],["mrnd7","mrnd8"],["mrnd9","mrnd10"]];
};
if(_var == 20) then
{
	_allobj = [["mrnd11","mrnd12"],["mrnd13","mrnd14"],["mrnd15","mrnd16"],["mrnd17","mrnd18"],["mrnd19","mrnd20"]];

};

_max = (count _allobj)-1;
_r = round random _max;
_rand =  ((_allobj select _r) select 0);
_rand2 =  ((_allobj select _r) select 1);
_pos = getMarkerPos _rand;
_posd = getMarkerPos _rand2;

_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";
sleep 5;
[West,"HQ"] SideChat localize "str_som_ambush_briefing_1";

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

_guard = createGroup (east);
_pos2 = [(_pos select 0),(_pos select 1) - 15,(_pos select 2)];
_pos3 = [(_pos select 0),(_pos select 1) - 30,(_pos select 2)];
_pos4 = [(_pos select 0),(_pos select 1) - 45,(_pos select 2)];

_allvecA = EGG_EVO_mevlight;
_allvecB = EGG_EVO_mevmed;
_allvecC = EGG_EVO_mevconvoya;

if(_var == 20) then
{
	_allvecA = EGG_EVO_mevmed;
	_allvecB = EGG_EVO_mevheavya;
};

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

_heli = createVehicle [(_allvecA select round random _maxA), _pos, [], 0, "FORM"];Sleep EGG_EVO_GlobalSleep;

_heli1 = createVehicle [(_allvecC select round random _maxC), _pos2, [], 0, "FORM"];Sleep EGG_EVO_GlobalSleep;

_heli2 = createVehicle [(_allvecC select round random _maxC), _pos3, [], 0, "FORM"];Sleep EGG_EVO_GlobalSleep;

_heli3 = createVehicle [(_allvecB select round random _maxB), _pos4, [], 0, "FORM"];Sleep EGG_EVO_GlobalSleep;

//add in a foreach heli1, heli2, heli3, heli4 to add EH's and crews as per makespetz

_allunits = EGG_EVO_meguardf;
_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), _pos4, [], 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), _pos4, [], 5, "FORM"];
		_unit AssignasCommander _x;
		_unit moveinCommander _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 _maxu), _pos4, [], 5, "FORM"];
		_unit AssignasCargo _x;
		_unit moveinCargo _x;
		_n = _n+1;
		Sleep 0.1;
	};
} forEach [_heli,_heli1,_heli2,_heli3];
{
	if((_x emptyPositions "gunner") > 0) then 
	{
		_unit = _guard createUnit [(_allunits select round random _maxu), _pos4, [], 5, "FORM"];
		_unit AssignasGunner _x;
		_unit moveinGunner _x;
	};		
	Sleep 0.1;
} forEach [_heli,_heli3];

[_guard, 2] spawn EGG_EVO_skill;

_guard setFormation "COLUMN";
_guard setSpeedMode "LIMITED";
_guard setBehaviour "SAFE";

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

_currentobj = [];
_wp = _guard addWaypoint [_posd, 20];
_wp2 = _guard addWaypoint [_pos, 20];
_wp3 = _guard addWaypoint [_posd, 20];
[_guard, 3] setWaypointType "CYCLE";

_posl = position (leader _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";//MISSION COMPLETE, GOOD JOB!
sleep 5;
[West,"HQ"] SideChat localize "str_m07r04";//PHOENIX BASE, THIS IS NOVEMBER ONE. THE WHOLE CONVOY'S ON FIRE. RETURNING TO BASE. OVER.

EGG_EVO_punit = _usergroup;
EGG_EVO_prew  = _var;
publicVariable "EGG_EVO_prew";
publicVariable "EGG_EVO_punit";
_obj1 settaskstate "Succeeded";
sleep 1.0;
[] call _deleteass;
