//land patrol

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

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

_MissionBriefingLong = localize "STR_M04t10_1";//Follow the patrol route. You must eliminate any enemy units you find.
_MissionBriefingHUD = localize "STR_M04t5_1";//patrol
_MissionBriefingShort = localize "STR_M04t9_1";//Land Patrol
_txtfailbody = localize "STR_M04t11";//The area remains in enemy hands.

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

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

[West,"HQ"] SideChat localize "STR_M04t64";//good luck

_possw = getmarkerpos "LpatSW";
_posne = getmarkerpos "LpatNE";

_xmax = ((_posne select 0)-(_possw select 0));
_ymax = ((_posne select 1)-(_possw select 1));

_pos = [((_possw select 0)+(random _xmax)),((_possw select 1)+(random _ymax)),0];
sleep 0.2;
_pos1 = [((_possw select 0)+(random _xmax)),((_possw select 1)+(random _ymax)),0];
sleep 0.2;
_pos2 = [((_possw select 0)+(random _xmax)),((_possw select 1)+(random _ymax)),0];
sleep 0.2;
_pos3 = [((_possw select 0)+(random _xmax)),((_possw select 1)+(random _ymax)),0];
sleep 0.2;

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

//map
[1800] execVM "scripts\missions\timer.sqf";
_mapview = [_pos,0] 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);
_guardb = createGroup (east);
_allvecA = EGG_EVO_mevheavyb+EGG_EVO_mevlighta+EGG_EVO_mevmed;
_maxA = (count _allvecA)-1;
_allunitsC = EGG_EVO_mecrew;
_maxC = (count _allunitsC)-1;
_allunits = EGG_EVO_meguardc;
_max = (count _allunits)-1;

_heli = createVehicle [(_allvecA select round random _maxA), _pos, [], 20, "NONE"];Sleep EGG_EVO_GlobalSleep;
_heli1 = createVehicle [(_allvecA select round random _maxA), _pos1, [], 20, "NONE"];Sleep EGG_EVO_GlobalSleep;
_heli2 = createVehicle [(_allvecA select round random _maxA), _pos2, [], 20, "NONE"];Sleep EGG_EVO_GlobalSleep;

//calculate crew
{
	_x addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}];
	if((_x emptyPositions "driver") > 0) then 
	{
		_unit = _guard createUnit [(_allunitsC select round random _maxC), _pos, [], 5, "FORM"];
		_unit AssignasDriver _x;
		_unit moveinDriver _x;
	};
	Sleep 0.1;
	if((_x emptyPositions "commander") > 0) then 
	{
		_unit = _guard createUnit [(_allunitsC select round random _maxC), _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 = _guard createUnit [(_allunits select round random _max), _pos, [], 5, "FORM"];
		_unit AssignasCargo _x;
		_unit moveinCargo _x;
		_n = _n+1;
		Sleep 0.1;
	};
	if((_x emptyPositions "gunner") > 0) then 
	{
		_unit = _guard createUnit [(_allunitsC select round random _maxC), _pos, [], 5, "FORM"];
		_unit AssignasGunner _x;
		_unit moveinGunner _x;
	};		
	Sleep 0.1;

} forEach [_heli,_heli1,_heli2];

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

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

[_guard, 2] spawn EGG_EVO_skill;
[_guardb, 3] spawn EGG_EVO_skill;

_starts = [_pos,_pos1,_pos2,_pos3];
_smax = (count _starts)-1;

_guard setCombatMode "YELLOW";
_guard setBehaviour "AWARE";

_d = 0;
while {_d <= Squadsize} do 
{
	_unit = _guardb createUnit [(_allunits select round random _max), _pos3, [], 30, "FORM"];Sleep EGG_EVO_GlobalSleep;
	_d = _d+1;
	sleep 0.5;
};

_guardb setCombatMode "WHITE";
_guardb setBehaviour "STEALTH";
_guardb setformation "DIAMOND";

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

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


_i=0;
while {(_i < (_smax+1))} do 
{
	_npos = (_starts select _i);
	_obj1 setSimpleTaskDestination _npos;	
	Waituntil {( (player distance _npos < 50) or ( (({alive _x} count units _guard) < 1) and (({alive _x} count units _guardb) < 1) ) or (not EGG_EVO_Onmission) )};
	_i = _i + 1;
	if (not EGG_EVO_Onmission) then {_i=100};
	if ( (({alive _x} count units _guard) < 1) and (({alive _x} count units _guardb) < 1) ) then {_i=100};
	if ( (_i == (_smax+1)) and (({alive _x} count units _guard) > 0) and (({alive _x} count units _guardb) > 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 _deletemlpat;
