//EXTERMINATE CULT mission

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

//added but not verified
_txtcommon = localize "STR_M04t0_5";// points
_txtfailhead = localize "STR_M04t2_6";//ass failed
_txtpasshead = localize "STR_M04t1_9";// ass comp
_txtpassbody = format["+%1 %2",_var,_txtcommon];

_MissionBriefingLong = localize "strn_x_mission_kill_them_all";//armor spotted
_MissionBriefingHUD = localize "init.sqf2_14";// secure village
_MissionBriefingShort = localize "strn_x_mission_kill_them_all";// kill em all
_txtfailbody = localize "str_t04_instructions_i_43";// wtf

_deletemdst2 =
{
	EGG_EVO_Onmission=false;
	player removeSimpleTask _obj1;
	sleep 12.0;
	_handle = [_guard] execVM "scripts\delete.sqf";
	_handle = [_guarda] execVM "scripts\delete.sqf";
	_handle = [_guardb] execVM "scripts\delete.sqf";
	{deletevehicle _x} forEach [_heli,_heli1,_heli2,_heli3,_heli4,_heli5,_heli6];
};

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

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

//LOCATIONS VARY ON RANK

_allobjo = ["mrx1","mrx2","mrx3","mrx4"];

if(_var == 20) then
{
	_allobjo = ["mrx1","mrx2","mrx4","mrx5","mrx6"]
};
if(_var == 40) then
{
	_allobjo = ["mrx6","mrx7","mrx8","mrx9","mrx10","mrx4","mrx1"]
};

_maxo = (count _allobjo)-1;
_rand = _allobjo select (round random _maxo);
_pos = getMarkerPos _rand;

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

_mapview = [_pos,2] execVM "scripts\missions\mismap.sqf";

if(_var == 10) then
{	
	[420,_pos] execVM "scripts\missions\countdown.sqf";
};
if(_var == 20) then
{
	[600,_pos] execVM "scripts\missions\countdown.sqf";
};
if(_var == 40) then
{
	[780,_pos] execVM "scripts\missions\countdown.sqf";
};

if (_var != 10 && _var != 20 && _var != 40) then {hint "var error"; [] call _Fail};

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

//4 VEHICLES

_allvecA = EGG_EVO_mcvconvoya;
_allvecB = EGG_EVO_mcvconvoyb;
_allvecC = EGG_EVO_mcvconvoyc;
_allvecD = EGG_EVO_mcvconvoyd;

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

_heli = createVehicle [(_allvecA select round random _maxA), _pos, [], 100, "NONE"];Sleep EGG_EVO_GlobalSleep;
_pos = position _heli;
_heli1 = createVehicle [(_allvecB select round random _maxB), _pos, [], 100, "NONE"];Sleep EGG_EVO_GlobalSleep;
_heli2 = createVehicle [(_allvecC select round random _maxC), _pos, [], 100, "NONE"];Sleep EGG_EVO_GlobalSleep;
_heli3 = createVehicle [(_allvecD select round random _maxD), _pos, [], 100, "NONE"];Sleep EGG_EVO_GlobalSleep;

//GROUP 1 = 21 UNARMED CIVS
_guard = createGroup (east);
_allunits = EGG_EVO_civ2; 
_maxu = (count _allunits)-1;

{
	[_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;
	_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];

//24 ON FOOT

_allunitsF = EGG_EVO_civ1;
_maxF = (count _allunitsF)-1;

_guarda = createGroup (east);

_rndx = random 100;
_rndy = random 100;
_pos1 = [(_pos select 0)-50 +_rndx,(_pos select 1)-50+_rndy,0];

_extrmguard = Squadsize;
if (_var == 40) then {_extrmguard = (Squadsize *3)};

_d = 0;
while {_d <= _extrmguard} do 
{
	_unit = _guarda createUnit [(_allunitsF select round random _maxF), _pos1, [], 6, "FORM"];
	_d = _d+1;
	Sleep EGG_EVO_GlobalSleep;
};

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

//{_x addEventHandler ["killed", {_x setCombatMode "RED";commandStop _x;handle = [_this select 0] execVM "scripts\bury.sqf"}]} forEach (units _guarda);

//GROUP 3 = LARGE GROUP OF 18 ARMED VILLAGERS 

_rndx1 = random 200;
_rndy1 = random 200;
_pos2 = [(_pos select 0)-100 +_rndx1,(_pos select 1)-100+_rndy1,0];

_guardb = createGroup (east);

_allunitsFA = EGG_EVO_east5;//armed woodlanders
_maxFA = (count _allunitsFA)-1;

_extrmguard = 2;
if (_var == 40) then {_extrmguard = Squadsize};

_d = 0;
while {_d <= _extrmguard} do 
{
	_unit = _guardb createUnit [(_allunitsFA select round random _maxFA), _pos2, [], 50, "NONE"];
	_d = _d+1;
	Sleep EGG_EVO_GlobalSleep;
};

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

_heli4 = createVehicle [(EGG_EVO_MASH select 0), _pos1, [], 25, "NONE"];
_heli5 = createVehicle [(EGG_EVO_mfhq select 0), _pos1, [], 15, "NONE"];
_heli6 = createVehicle [(EGG_EVO_meflag select 0), _pos1, [], 0, "NONE"];

sleep 1.0;

_heli = assignedVehicle (units _guard select 0);
_heli1 = assignedVehicle (units _guard select 4);
_heli2 = assignedVehicle (units _guard select 6);
_heli3 = assignedVehicle (units _guard select 7);

_recy = [_user,_guard] execVM "scripts\grecycle.sqf";
_recy2 = [_user,_guarda] execVM "scripts\grecycle.sqf";
_recy3 = [_user,_guardb] execVM "scripts\grecycle.sqf";

_obj1 setSimpleTaskDestination  position (leader _guard);

for [{_loop=0}, {_loop<1}, {_loop=_loop}] do
{
	if (({alive _x} count units _guard) > 0) then {_obj1 setSimpleTaskDestination  position (leader _guard)};
	if ( (({alive _x} count units _guard) < 1) and (({alive _x} count units _guarda) > 0) ) then {_obj1 setSimpleTaskDestination  position (leader _guarda)};
	if ( (({alive _x} count units _guard) < 1)  and (({alive _x} count units _guarda) < 1) and (({alive _x} count units _guardb) > 0) ) then {_obj1 setSimpleTaskDestination  position (leader _guardb)};
	if ( (({alive _x} count units _guard) < 1) and (({alive _x} count units _guarda) < 1) and (({alive _x} count units _guardb) < 1)) then {_loop=1};
	if (not EGG_EVO_Onmission) then {_loop=1};
	sleep 1.001;
};
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_call_from_fob_m_3";
sleep 5;
[West,"HQ"] SideChat localize "str_nikolayevs_house_explosion_y_7";
sleep 5;
[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 _deletemdst2;
