//massg - assassinate enemy general

_user = _this select 0;
_var = _this select 1;
_usergroup = (group player);
_allobj = [];
_pos = [0,0,0];
_obj1 = [];
_guardass=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_lib_chal_assassination_briefing";
_MissionBriefingHUD = localize "str_lib_chal_assassination_char_officer";
_MissionBriefingShort = localize "str_lib_chal_assassination";
_txtfailbody = localize "str_end_bardak_escaped_b_1";

_deleteass =
{
	EGG_EVO_Onmission=false;
	sleep 12.0;
	_handle = [_guardass] execVM "scripts\delete.sqf";
	player removeSimpleTask _obj1;
	sleep 90.0;
	{deletevehicle _x} forEach [_heli,_heli1,_heli2];
};

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

[West,"HQ"] SideChat localize "STR_M04t64";

_pos = position inthotel_roof;

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

[1800] execVM "scripts\missions\timer.sqf";
_mapview = [_pos,2] execVM "scripts\missions\mismap.sqf";
sleep 5;
[West,"HQ"] SideChat localize "str_lib_chal_assassination_briefing";

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

_pos = getMarkerPos "genstart";
_targ = getMarkerPos "genhq";

_obj1 setSimpleTaskDestination _pos;

_pos2 = [(_pos select 0),(_pos select 1) + 15,(_pos select 2)];
_pos3 = [(_pos select 0),(_pos select 1) + 30,(_pos select 2)];

_allvecA = EGG_EVO_mevconvoyb;
_allvecB = EGG_EVO_mevlighta;

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

_heli = createVehicle [(_allvecB select (round random _maxB)), _pos, [], 0, "NONE"];Sleep EGG_EVO_GlobalSleep;

_heli1 = createVehicle [(_allvecA select (round random _maxA)), _pos2, [], 0, "NONE"];Sleep EGG_EVO_GlobalSleep;

_heli2 = createVehicle [(_allvecA select (round random _maxA)), _pos3, [], 0, "NONE"];Sleep EGG_EVO_GlobalSleep;

//calculate crew
_guardass = createGroup (east);

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

_allunitsA = EGG_EVO_meofficer;
_maxuA = (count _allunitsA)-1;

{
	[_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"}];
	_x setdir 180;
	if((_x emptyPositions "driver") > 0) then 
	{
		_unit = _guardass createUnit [(_allunits select round random _maxu), _pos2, [], 5, "FORM"];
		_unit AssignasDriver _x;
		_unit moveinDriver _x;
	};
	Sleep 0.1;
	if((_x emptyPositions "commander") > 0) then 
	{
		_unit = _guardass createUnit [(_allunitsA select round random _maxuA), _pos, [], 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 = _guardass createUnit [(_allunits select round random _maxu), _pos3, [], 5, "FORM"];
		_unit AssignasCargo _x;
		_unit moveinCargo _x;
		_n = _n+1;
		Sleep 0.1;
	};
} forEach [_heli,_heli1,_heli2];
{
	if((_x emptyPositions "gunner") > 0) then 
	{
		_unit = _guardass createUnit [(_allunits select round random _maxu), _pos, [], 5, "FORM"];
		_unit AssignasGunner _x;
		_unit moveinGunner _x;
	};		
	Sleep 0.1;
} forEach [_heli];

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

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

_wp = _guardass addWaypoint [_targ, 10];

[_guardass, 1] setWaypointType "MOVE";

_pos = position (leader _guardass);

for [{_loop=0}, {_loop<1}, {_loop=_loop}] do
{
	sleep 1.001;
	_obj1 setSimpleTaskDestination  position (leader _guardass);
	if ( (({alive _x} count units _guardass) < 1) || (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 _user);

["#FF0000",_txtpasshead,_txtpassbody] call EGG_EVO_Message;

[West,"HQ"] SideChat localize "STR_M04t65";playSound "Goodjob";
sleep 5;
[West,"HQ"] SideChat localize "str_lib_chal_assassination_assassinated";

EGG_EVO_punit = _usergroup;
EGG_EVO_prew  = _var;
publicVariable "EGG_EVO_prew";
publicVariable "EGG_EVO_punit";
_obj1 settaskstate "Succeeded";

sleep 1.0;
[] call _deleteass;
