// Attached to enemy prisoners and officers

_unit = _this select 0;
_user = _this select 1;
_usergroup = group _user;

//
_pos = getmarkerpos "det";

//[_unit] join grpNull;
_unit setCaptive true;
_unit switchMove "AmovPercMstpSsurWnonDnon";
_unit disableAI "AUTOTARGET";

removeheadgear _unit;
removebackpack _unit;
removevest _unit;
removeuniform _unit;
removeallassigneditems _unit;

//bringing back POW transport to detention camp if parameter 19 is set to ON

if (POWfactor ==1) then
{
	[_unit] join group _user;
	_unit setDamage 0;
	_unit enableAI "MOVE";
//	_unit doFollow (leader _usergroup);
//	_usergroup setFormation "FILE";
	_unit doFollow _user;

	hint format ["POW Detained: %1. Transport him to POW Camp for questioning", (name _unit)];
	_MissionBriefingLong = localize "str_t04_instructions_i_30";//escort back to base
	_MissionBriefingShort = localize "STR_M04t29_0";//rtb
	_MissionBriefingHUD = localize "EVO_059";//rtb

	_cap1 = _user createSimpleTask ["cap1"];
	_cap1 setSimpleTaskDescription [_MissionBriefingLong,	_MissionBriefingShort,_MissionBriefingHUD];
	_cap1 setSimpleTaskDestination (getMarkerPos "det");
	_cap1 settaskstate "Created";
	_user setCurrentTask _cap1;

	for [{_loop=0}, {_loop<1}, {_loop=_loop}] do
	{
		if (vehicle _user != _user) then {_unit assignAsCargo vehicle _user; [_unit] orderGetIn true};
		if (vehicle _user == _user) then {unassignvehicle _unit; [_unit] orderGetIn false;	_unit doFollow _user};
		if ( (speed _unit < 1) and (vehicle _unit == _unit) and (getposATL _unit select 2 >0) ) then {_unit playMove "AmovPercMstpSsurWnonDnon"};
		if (count weapons _unit > 0) then {_loop=1};
		if ((_unit distance _pos) < 20) then {_loop=1};
		if (vehicle _unit distance vehicle _user > 80) then {_loop=1};
		if !(alive _unit) then {_loop=1};
		if (vehicle _unit distance vehicle _user > 60) then {hint "Your prisoner is about to escape"};
		sleep 4.0;
	};
	
	if (count weapons _unit > 0 or vehicle _unit distance vehicle player > 80 or !(alive _unit)) exitwith 
	{
		_cap1 settaskstate "Failed";
		hint "Task Failed \n POW was lost";

		if (alive _unit) then
		{
			_unit setCaptive false;
			_grp = creategroup (east);
			[_unit] join _grp;
			_wp = _grp addWaypoint [getMarkerPos "mobj11", 0];
			_unit enableAI "TARGET";
			_unit enableAI "AUTOTARGET";
			_unit setBehaviour "COMBAT";
			_unit removealleventhandlers "killed";
			sleep 360.0;
			deletevehicle _unit;
			deletegroup _grp;
		};
	};
};

WaitUntil {( (!(alive _unit)) or (isNull EGG_EVO_sop) )};

if !(alive _unit) exitwith 
{
	if (!isNil "_cap1") then {
		_cap1 settaskstate "Failed";
	};
	hint "Task Failed: POW was killed";
};

_type = typeof _unit;
if (_type in EGG_EVO_enemyOff) then
{	
	_txtpasshead = format["%1 at %2",localize "STR_M04t66",(EGG_EVO_Townnames select EGG_EVO_MissionProgress)];//Officer Captured - at - city
	_txtpassbody = format["+%1 %2",EGG_EVO_offscore,localize "STR_M04t0_5"];//Points
	["#0000FF",_txtpasshead,_txtpassbody] call EGG_EVO_Message;

	if(EGG_EVO_MissionProgress == 0) then {EGG_EVO_sot = "cityA2"};
	if(EGG_EVO_MissionProgress == 1) then {EGG_EVO_sot = "cityB2"};
	if(EGG_EVO_MissionProgress == 2) then {EGG_EVO_sot = "cityC2"};
	if(EGG_EVO_MissionProgress == 3) then {EGG_EVO_sot = "cityD2"};
	if(EGG_EVO_MissionProgress == 4) then {EGG_EVO_sot = "cityE2"};
	if(EGG_EVO_MissionProgress == 5) then {EGG_EVO_sot = "cityF2"};
	if(EGG_EVO_MissionProgress == 6) then {EGG_EVO_sot = "cityG2"};
	if(EGG_EVO_MissionProgress == 7) then {EGG_EVO_sot = "cityH2"};
	if(EGG_EVO_MissionProgress == 8) then {EGG_EVO_sot = "cityI2"};
	if(EGG_EVO_MissionProgress == 9) then {EGG_EVO_sot = "cityJ2"};
	if(EGG_EVO_MissionProgress == 10) then {EGG_EVO_sot = "cityK2"};
	EGG_EVO_sor = EGG_EVO_offscore;EGG_EVO_sop = player;publicVariable "EGG_EVO_sor";publicVariable "EGG_EVO_sot";publicVariable "EGG_EVO_sop";
}
else
{
	_txtpasshead = format["%1 at %2",localize "STR_M04t67",(EGG_EVO_Townnames select EGG_EVO_MissionProgress)];//Prisoner Captured - at - city
	_txtpassbody = format["+%1 %2",EGG_EVO_powscore,localize "STR_M04t0_5"];//points
	["#0000FF",_txtpasshead,_txtpassbody] call EGG_EVO_Message;
	EGG_EVO_sot = format["%1",_unit];EGG_EVO_sor = EGG_EVO_powscore;EGG_EVO_sop = player;publicVariable "EGG_EVO_sor";publicVariable "EGG_EVO_sot";publicVariable "EGG_EVO_sop";
};

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

sleep 5;
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;

