//rescue agent 

_user = _this select 0;
 _var = _this select 1;
_usergroup = (group player);
_pos = [0,0,0];
_trgobj3 = objNull;
_start = [];
EGG_EVO_sobj1 = false;
_obj1 = [];
_mcsardga=grpNull;
_mcsardgb=grpNull;
_mcsardgc=grpNull;
_pilot=grpNull;
_allobj = [];

_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_start_razor_s_1";//meet agent long
_MissionBriefingHUD = localize "str_som_rescue_task_desc";//find and rescue
_MissionBriefingShort = localize "str_som_rescue_briefing_2";//rescue OP

_MissionBriefingHUD2 = localize "task_olegich_name";//meet agent
_MissionBriefingHUD3 = localize "str_som_escort_task_short";//escort

_txthmid = localize "STR_M04t27_0"; //area secure
_txtbmid = localize "str_som_escort_task_short";// escort

_txtfailbody = localize "str_som_transportation_debriefing_end2";//Disaster here. All of our passengers have been killed. Over.

_deletemcsard =
{
	deleteVehicle _trgobj3;
	deleteMarkerLocal _start;
	EGG_EVO_Onmission=false;
	player removeSimpleTask _obj1;
	sleep 1;
	{_handle = [_x] execVM "scripts\delete.sqf"} forEach [_mcsardga,_mcsardgb,_mcsardgc,_pilot];
	sleep 90;
	{deletevehicle _x} forEach [_heli,_heli1,_heli2,_heli3,_heli4,_heli5,_heli6];
};

_Fail = 
{
	_obj1 settaskstate "Failed";
	["#FF0000",_txtfailhead,_txtfailbody] call EGG_EVO_Message;
	hint "Rescue Agent: Mission Failed\nAgent dead";
	[] call _deletemcsard;
};

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

_allobj = ["mrx1","mrx2","mrx3","mrx4","mrx5","mrx6","mrx7","mrx8","mrx9","mrx10","mrnd29","mrnd30"];

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

_rndx = random 200;
_rndy = random 200;
_rndbx = random 200;
_rndby = random 200;
_rndcx = random 200;
_rndcy = random 200;
_rnddx = random 200;
_rnddy = random 200;

_pos1 = [(_pos select 0)-50,(_pos select 1)-50,0];
_pos2 = [(_pos select 0)-100 +_rndbx,(_pos select 1)-100+_rndby,0];
_pos3 = [(_pos select 0)-100 +_rndcx,(_pos select 1)-100+_rndcy,0];
_pos4 = [(_pos select 0)-100 +_rndx,(_pos select 1)-100+_rndy,0];
_pos5 = [(_pos select 0)-100 +_rnddx,(_pos select 1)-100+_rnddy,0];

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

[1800] execVM "scripts\missions\timer.sqf";

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

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

//AGENT
_pilot = createGroup (civilian);//agent
_allunitso = EGG_EVO_magent;
_maxo = (count _allunitso)-1;
_hostage = _pilot createUnit [(_allunitso select round random _maxo), _pos1, [], 10, "NONE"];Sleep EGG_EVO_GlobalSleep;
{
	_x disableAI "MOVE";
	_x allowfleeing 0;
	_x setBehaviour "Careless";
	removeallweapons _x;
	_x setCaptive true;
	commandStop _x;
//	_x addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}]
} forEach (units _pilot);
sleep 1.0;
{
	_x switchMove "AmovPercMstpSsurWnonDnon";
} forEach units _pilot;

//GUARDS

_mcsardga = createGroup (east);//2 commanders
sleep 1;

_allunitsB = EGG_EVO_meofficer;
_maxB = (count _allunitsB)-1;

_j = 0;
while {_j <= 2} do 
{
	_unit = _mcsardga createUnit [(_allunitsB select round random _maxB), _pos1, [], 10, "NONE"];Sleep EGG_EVO_GlobalSleep;
	_j = _j+1;
};
//{_x addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}]} forEach (units _mcsardga);
[_mcsardga, 5] spawn EGG_EVO_skill;

_mcsardga setCombatMode "WHITE";
_mcsardga setBehaviour "SAFE";

//VEHICLES
_allvecA = EGG_EVO_mevlight;
_allvecB = EGG_EVO_mevmed;
_allvecC = EGG_EVO_mevheavya;
_allvecD = EGG_EVO_mevheavyb;

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

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

_heli1 = createVehicle [(_allvecB select round random _maxB), _pos3, [], 10, "NONE"];Sleep EGG_EVO_GlobalSleep;

_heli2 = createVehicle [(_allvecC select round random _maxC), _pos4, [], 10, "NONE"];Sleep EGG_EVO_GlobalSleep;

_heli3 = createVehicle [(_allvecD select round random _maxD), _pos5, [], 10, "NONE"];Sleep EGG_EVO_GlobalSleep;

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

_mcsardgb = createGroup (east);//4 vehicles and 9 insurgent crew
sleep 1;

_allunitsJ = EGG_EVO_meguardd;
_maxJ = (count _allunitsJ)-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 = _mcsardgb createUnit [(_allunitsJ select round random _maxJ), _pos, [], 5, "FORM"];
		_unit AssignasDriver _x;
		_unit moveinDriver _x;
	};
	Sleep 0.1;
	if((_x emptyPositions "commander") > 0) then 
	{
		_unit = _mcsardgb createUnit [(_allunitsJ select round random _maxJ), _pos, [], 5, "FORM"];
		_unit AssignasCommander _x;
		_unit moveinCommander _x;
	};
	Sleep 0.1;
	if((_x emptyPositions "gunner") > 0) then 
	{
		_unit = _mcsardgb createUnit [(_allunitsJ select round random _maxJ), _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 = _mcsardgb createUnit [(_allunitsJ select round random _maxJ), _pos, [], 5, "FORM"];
		_unit AssignasCargo _x;
		_unit moveinCargo _x;
		_n = _n+1;
		Sleep 0.1;
	};
} forEach [_heli,_heli1,_heli2,_heli3];

//{_x addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}]} forEach (units _mcsardgb);
[_mcsardgb, 4] spawn EGG_EVO_skill;
_mcsardgb setCombatMode "WHITE";
_mcsardgb setBehaviour "SAFE";

////////////////////////////////////
_heli4 = createVehicle [(EGG_EVO_meflag select 0), _pos1, [], 20, "NONE"];Sleep EGG_EVO_GlobalSleep;
_heli5 = createVehicle [(EGG_EVO_mehq select 0), _pos1, [], 20, "NONE"];Sleep EGG_EVO_GlobalSleep;
_heli6 = createVehicle [(EGG_EVO_mfhq select 0), _pos1, [], 10, "NONE"];Sleep EGG_EVO_GlobalSleep;

_recy = [_user,_heli4] execVM "scripts\recycle.sqf";
_recy = [_user,_heli5] execVM "scripts\recycle.sqf";
_recy = [_user,_heli6] execVM "scripts\recycle.sqf";

//PARTISANS
_mcsardgc = createGroup (east);//12 partisans
sleep 1;

_allunitsK = EGG_EVO_meguarde;
_maxK = (count _allunitsK)-1;

_d = 0;
while {_d <= Squadsize} do 
{
	_unit = _mcsardgc createUnit [(_allunitsK select round random _maxK), _pos, [], 6, "FORM"];Sleep EGG_EVO_GlobalSleep;
	_d = _d+1;
	sleep 0.5;
};
//{_x addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}]} forEach (units _mcsardgc);
[_mcsardgc, 3] spawn EGG_EVO_skill;
_mcsardgb setCombatMode "YELLOW";
_mcsardgb setBehaviour "SAFE";
_mcsardgb setformation "DIAMOND";

_wp1 = _mcsardgc addWaypoint [_pos, 10];
_wp2 = _mcsardgc addWaypoint [_pos1, 10];
_wp3 = _mcsardgc addWaypoint [_pos3, 10];
_wp4 = _mcsardgc addWaypoint [_pos2, 10];
[_mcsardgc, 4] setWaypointType "CYCLE";

_recy = [_user,_pilot] execVM "scripts\grecycle.sqf";
_recy = [_user,_mcsardga] execVM "scripts\grecycle.sqf";
_recy = [_user,_mcsardgb] execVM "scripts\grecycle.sqf";	
_recy = [_user,_mcsardgc] execVM "scripts\grecycle.sqf";	

_trgobj3 = createTrigger ["EmptyDetector", _pos];
_trgobj3 setTriggerActivation ["EAST", "NOT PRESENT", false];
_trgobj3 setTriggerArea [200, 200, 0, true];
_trgobj3 setTriggerTimeout [10, 10, 10, true ];
_trgobj3 setTriggerStatements ["this", "EGG_EVO_sobj1 = true", ""];

_start = createMarkerLocal ["look", _pos];
_start setMarkerColorLocal "ColorGreen";
_start setMarkerShapeLocal "RECTANGLE";
_start setMarkerSizeLocal [200, 200];
_start setMarkerBrushLocal "FDiagonal";

_obj1 setSimpleTaskDestination  _pos;

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

_obj1 setSimpleTaskDescription [_MissionBriefingLong,_MissionBriefingShort,_MissionBriefingHUD2];

for [{_loop=0}, {_loop<1}, {_loop=_loop}] do
{
	sleep 1.001;
	if (EGG_EVO_sobj1) then {_loop=1;};
	if (not EGG_EVO_Onmission) then {_loop=1;};
	if (not (alive _hostage)) then {_loop=1;};
};
if ( (not EGG_EVO_Onmission)||(not (alive _hostage)) ) exitwith {[] call _Fail};

_obj1 setSimpleTaskDescription [_MissionBriefingLong,_MissionBriefingShort,_MissionBriefingHUD3];
_obj1 setSimpleTaskDestination position dropoff; //Boathouse;
["#00FF00",_txthmid,_txtbmid] call EGG_EVO_Message;

[_hostage] join _usergroup;

_hostage setdamage 0;
_hostage setCaptive false;
_hostage enableAI "MOVE";
_hostage doFollow (leader group player);

for [{_loop=0}, {_loop<1}, {_loop=_loop}] do
{
	sleep 1.001;
	if (_hostage distance dropoff < 10) then {_loop=1};
	if (not (alive _hostage)) then {_loop=1};
	if (not EGG_EVO_Onmission) then {_loop=1;};
};
if ( (not EGG_EVO_Onmission)||(not (alive _hostage)) ) exitwith {[] call _Fail};

//_hostage action ["Eject",(vehicle _hostage)];	
_hostage action ["getOut",(vehicle _hostage)];	
unassignVehicle _hostage;
[_hostage] join grpNull;
{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";

[] call _deletemcsard;

//_hostage doMove position Boathouse;  //may not move as sticks on carrier
_hostage doMove (position safety);
sleep 20.0;

deleteVehicle _hostage;

