//RENDITION MISSION

_user = _this select 0;
 _var = _this select 1;
_usergroup = (group player);
_pos = [0,0,0];
_trgobj3 = objNull;
_start = [];
EGG_EVO_sobj1 = false;
_obj1 = [];
_guard=grpNull;
_guardb=grpNull;
_guardc=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 "overview_c2b"; //cap officer
_MissionBriefingHUD = localize "strenemybaselocated"; //Enemy base located
_MissionBriefingShort = localize "STR_M04t24";//csar
_MissionBriefingHUD2 = localize "strwfcapture";//capture
_MissionBriefingHUD3 = localize "STR_M04t29_0";//rtb

_txthmid = localize "STR_M04t27_0";//a sec
_txtbmid = localize "str_t04_instructions_i_30";//escort back to base

_txtfailbody = localize "str_after_prizrak_killed_t_1";//pilot killed

_deletemsarc =
{
	deleteVehicle _trgobj3;
	deleteMarkerLocal _start;
	EGG_EVO_Onmission=false;
	player removeSimpleTask _obj1;
	sleep 2.0;
	{_handle = [_x] execVM "scripts\delete.sqf"} forEach [_guard,_guardb,_guardc,_pilot];
	{deleteVehicle _x} forEach [_heli,_heli1,_heli2,_heli3,_heli4,_heli5,_heli6];
};

_Fail = 
{
	_obj1 settaskstate "Failed";
	hint "Rendition: Mission Failed\nOfficer dead";
	["#FF0000",_txtfailhead,_txtfailbody] call EGG_EVO_Message;
	[] call _deletemsarc;
};

[West,"HQ"] SideChat localize "STR_M04t64";
_allobj = ["mrnd24","mrnd25","mrnd26"];
if (_var == 40) then
{
	_allobj = ["mrnd24","mrnd25","mrnd26","mrnd27","mrnd28","mrnd29"]
};
if (_var == 30) then
{
	_allobj = ["mrnd27","mrnd28","mrnd29","mrnd30","mrnd23"]
};
_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;

if(_var == 20) then
{
	[1800] execVM "scripts\missions\timer.sqf";
};

if(_var == 30) then
{
	[2100] execVM "scripts\missions\timer.sqf";
};

if(_var == 40) then
{
	[2400] 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};

//officer to capture
_pilot = createGroup (civilian);//officer
_allunitso = EGG_EVO_meofficer;
_maxo = (count _allunitso)-1;
_hostage = _pilot createUnit [(_allunitso select round random _maxo), _pos1, [], 0, "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;

//bodyguards
_guardc = createGroup (east);//2 bodyguards
_allunitsA = EGG_EVO_meguardc;
_maxA = (count _allunitsA)-1;
_j = 0;
while {_j <= 2} do 
{
	_unit = _guardc createUnit [(_allunitsA select round random _maxA), _pos1, [], 5, "NONE"];Sleep EGG_EVO_GlobalSleep;
	_j = _j+1;
};

//main group
_guardb = createGroup (east);//12 infantry
_j = 0;
while {_j <= Squadsize} do 
{
	_unit = _guardb createUnit [(_allunitsA select round random _maxA), _pos2, [], 10, "FORM"];Sleep EGG_EVO_GlobalSleep;
	_j = _j+1;
};

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

//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, [], 0, "NONE"];Sleep EGG_EVO_GlobalSleep;
_heli1 = createVehicle [(_allvecB select round random _maxC), _pos3, [], 0, "NONE"];
_heli2 = createVehicle [(_allvecC select round random _maxD), _pos4, [], 0, "NONE"];
_heli3 = createVehicle [(_allvecD select round random _maxB), _pos5, [], 0, "NONE"];

_guard = createGroup (east);//4 vehicles and 9 crew
_allunits = EGG_EVO_meguardd;
_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;
	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;
	_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), _pos, [], 5, "FORM"];
		_unit AssignasCargo _x;
		_unit moveinCargo _x;
		_n = _n+1;
		Sleep 0.1;
	};
} forEach [_heli,_heli1,_heli2,_heli3];


_heli4 = createVehicle [(EGG_EVO_meflag select 0), _pos1, [], 20, "NONE"];Sleep EGG_EVO_GlobalSleep;
_heli5 = createVehicle [(EGG_EVO_mfhq select 0), _pos1, [], 20, "NONE"];Sleep EGG_EVO_GlobalSleep;
_heli6 = createVehicle [(EGG_EVO_mehq select 0), _pos1, [], 10, "NONE"];Sleep EGG_EVO_GlobalSleep;
{
	_spawne = [_x] spawn {[_this select 0] call EGG_EVO_idelSVEC};
	_x addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}];
} forEach [_heli4,_heli5,_heli6];

//////////////////////////////////////////////

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

_recy = [_user,_pilot] execVM "scripts\grecycle.sqf";
_recy = [_user,_guard] execVM "scripts\grecycle.sqf";
_recy = [_user,_guardb] execVM "scripts\grecycle.sqf";	
_recy = [_user,_guardc] 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;

//_pos = position vehicle (units _pilot select 0);
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};

sleep 1.0;

_obj1 setSimpleTaskDescription [_MissionBriefingLong,_MissionBriefingShort,_MissionBriefingHUD3];

_posdrop = getmarkerpos "det";

_obj1 setSimpleTaskDestination _posdrop;
["#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 _posdrop < 20) 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;

if (POWfactor ==1) then
{
	_hostage doMove (getMarkerPos "det");
	WaitUntil { not (Alive _hostage) || (_hostage distance getmarkerpos "det") < 5};
	sleep 1;
	_hostage setcaptive true; 
	[_hostage] join group POW1;//grpNull;
	_hostage disableAI "AUTOTARGET";
	_hostage setBehaviour "Careless";
	_hostage allowFleeing 0;
	_nul = _hostage addeventhandler ["HandleDamage", {_this select 3}];//can add penalty here
	_hostage playAction "SitDown";
	sleep 1;
	_hostage disableAI "ANIM";
} else 
{
	[_hostage] join grpNull;
	sleep 20;
	deletevehicle _hostage;
};

{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";
sleep 5;
[West,"HQ"] SideChat localize "str_m04t66";//officer cap

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

[] call _deletemsarc;

sleep 5;
_type = typeof _hostage;
if (_type in EGG_EVO_enemyOff) then 
{
	EGG_EVO_opowcount=EGG_EVO_opowcount+1
} else 
{
	EGG_EVO_powcount=EGG_EVO_powcount+1
};
_txtpasshead = format["%1:",localize "STR_intel_report"];//intellgence report
_txtpassbody = format["%1:%2 %3:%4",localize "STR_M04t66",EGG_EVO_opowcount,localize "STR_M04t67",EGG_EVO_powcount];//officer captured, prisoner captured
["#0000FF",_txtpasshead,_txtpassbody] call EGG_EVO_Message;
