//water patrol mission

_user = _this select 0;
_var = _this select 1;
_usergroup = (group player);
_pos = [0,0,0];
_obj1 = [];
_guard=grpNull;
_starts = [];

_txtcommon = "points";//points
_txtfailhead = "failed";//failed
_txtpasshead = "complete";//complete
_txtpassbody = format["+%1 %2",_var,_txtcommon];

_MissionBriefingLong = "The enemy have been spotted in RHIBs. Follow the patrol route. You must eliminate any enemy units you find.";//The enemy have been spotted in RHIBs. Follow the patrol route. You must eliminate any enemy units you find.
_MissionBriefingHUD = "WATER PATROL";//PATROL
_MissionBriefingShort = "Water Patrol";//Water Patrol
_txtfailbody = "The boats have escaped.";//The boats have escaped.

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

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

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

_allpos = ["wpat1","wpat2","wpat3","wpat4","wpat5","wpat6"];
_max = (count _allpos)-1;

_r = round random _max;
_rand =  _allpos select _r;
_pos = getMarkerPos _rand;
sleep 1;

_r2 = round random _max;
_rand =  _allpos select _r2;
_pos1 = getMarkerPos _rand;
sleep 1;

_r3 = round random _max;
_rand =  _allpos select _r3;
_pos2 = getMarkerPos _rand;
sleep 1;

_r4 = round random _max;
_rand =  _allpos select _r4;
_pos3 = getMarkerPos _rand;

_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";

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

_guard = createGroup (east);

_allvecA = EGG_EVO_eastveh7;
_allvecB = EGG_EVO_eastveh8;//armed
_allunitsA = EGG_EVO_mecrew;
_allunitsB = EGG_EVO_east2;//divers
_maxA = (count _allvecA)-1;
_maxB = (count _allvecB)-1;
_maxuA = (count _allunitsA)-1;
_maxuB = (count _allunitsB)-1;

_heli = createVehicle [(_allvecB select round random _maxB), _pos1, [], 20, "FORM"];Sleep EGG_EVO_GlobalSleep;
_heli1 = createVehicle [(_allvecA select round random _maxA), _pos1, [], 20, "FORM"];Sleep EGG_EVO_GlobalSleep;
_heli2 = createVehicle [(_allvecA select round random _maxA), _pos1, [], 30, "FORM"];Sleep EGG_EVO_GlobalSleep;
_heli3 = createVehicle [(_allvecB select round random _maxB), _pos1, [], 30, "FORM"];Sleep EGG_EVO_GlobalSleep;

//calculate crew
{
	[_x] call EGG_EVO_Lock;
	_x addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}];
_spawn = [_x] spawn {[_this select 0] call EGG_EVO_idelSVEC};

	if((_x emptyPositions "driver") > 0) then 
	{
		_unit = _guard createUnit [(_allunitsA select round random _maxuA), _pos1, [], 5, "FORM"];
		_unit AssignasDriver _x;
		_unit moveinDriver _x;
	};
	Sleep 0.1;
	if((_x emptyPositions "commander") > 0) then 
	{
		_unit = _guard createUnit [(_allunitsA select round random _maxuA), _pos1, [], 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 [(_allunitsB select round random _maxuB), _pos1, [], 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 [(_allunitsA select round random _maxuA), _pos1, [], 5, "FORM"];
		_unit AssignasGunner _x;
		_unit moveinGunner _x;
	};		
	Sleep 0.1;
} forEach [_heli,_heli3];

[_guard, 3] spawn EGG_EVO_skill;
_guard setCombatMode "YELLOW";
_guard setBehaviour "AWARE";

//_x addRating -2000; used to use this to stop friendly kill problem with RHIBs

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

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

_i=0;
while {(_i < (_max+1))} do 
{
	_mpos =  _allpos select _i;
	_npos = getMarkerPos _mpos;
	_obj1 setSimpleTaskDestination _npos;
	Waituntil {( (player distance _npos < 100) or (({alive _x} count units _guard) < 1) or (not EGG_EVO_Onmission) )};
	_i = _i + 1;
	if (not EGG_EVO_Onmission) then {_i=100};
	if (({alive _x} count units _guard) < 1) then {_i=100};
	if ( (_i == (_max+1)) and (({alive _x} count units _guard) > 0) ) then {_i=0};
	sleep 1.0;
};

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";
EGG_EVO_punit = _usergroup;
EGG_EVO_prew  = _var;
publicVariable "EGG_EVO_prew";
publicVariable "EGG_EVO_punit";
_obj1 settaskstate "Succeeded";

sleep 1.0;
[] call _deletemwpat;
