//enemynumdiv = default 1
//values[] = {4,3,2,1};
//texts[] = {"Very Low", "Low", "Medium", "High"};

//modify to account for enemynumdiv, e.g. 6,9,12,15 squad size
//Squadsize = 6,9,12,15

EGG_EVO_Erec =
{
	_placetag = _this select 0;
	_list = _this select 1;
	_disable = _this select 2;
	_towncount = _this select 3;
	_inf = 0; 
	_mec = 0; 
	_stat = 0; 
	_rwait = 240;
	_radio = objNull; 
	_enemycount = ((6-enemynumdiv)*0.2);
	_rwait =0;
	_inf = round( ( (EGG_EVO_Infantry select _towncount) select 0)* _enemycount);
	_transport = 3;
	_tanks = 4;
	_mec = round( ( ( (EGG_EVO_Mechanized select _towncount) select 0) - (_transport+_tanks) )* _enemycount);
	_stat = round( ( ( ( (EGG_EVO_Mechanized select _towncount) select 0) - (_transport+_tanks) ) /2 )* _enemycount);
	_radio = EGG_EVO_radios select _towncount;
	_bunkno = _stat;//5;
	_newunits = [];
	_rds = [];
	_type = ""; 
	_offobj = objNull;
	_taginf = "INF";
	_tagmec = "MEC";
	_c4 = [_radio] execVM "scripts\c4only.sqf";
	_rtobj = [_radio] execVM "scripts\rtobj.sqf";
	
	_pos = position _list;
	_pos1 = [(_pos select 0)+300,(_pos select 1),(_pos select 2)];
	_pos2 = [(_pos select 0),(_pos select 1)+300,(_pos select 2)];
	_pos3 = [(_pos select 0)-300,(_pos select 1),(_pos select 2)];
	_pos4 = [(_pos select 0),(_pos select 1)-300,(_pos select 2)];

	_outpoints = [_pos1,_pos2,_pos3,_pos4];	

//Officer
	_grpO = createGroup (east);
	_type = EGG_EVO_meofficer select 0;
	_offobj = _grpO createUnit [_type, _pos, [], 300, "NONE"];Sleep EGG_EVO_GlobalSleep;
    //_offobj = createVehicle [_type, _pos, [], 300, "NONE"];Sleep EGG_EVO_GlobalSleep;
//	_offobj addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}];
	_recy = [objnull,_grpO] execVM "scripts\grecycle.sqf";
	//_offobj setVehicleInit "Ocap = [this] execVM 'scripts\submit.sqf'";
	_pobj = [_offobj] execVM "scripts\objoff.sqf";
	//processInitCommands;
	
	_sCommand = format ["[{
		[(objectFromNetID '%1')] execVM 'scripts\submit.sqf';
		}]", (netId _offobj) ];

	[call compile format["%1",_sCommand],"BIS_fnc_spawn", true, true] spawn BIS_fnc_MP; 
		
	Sleep 3;

//Radio defence
	if(_inf > Squadsize) then
	{
		_grp = createGroup (east);
		_type = EGG_EVO_east1 select 0;
		_unit = _grp createUnit [_type, position _radio, [], 10, "FORM"];Sleep EGG_EVO_GlobalSleep;
//manage unit count
		_unit addEventHandler ["killed", {handle = [_this select 0,"INF"] execVM "scripts\mobjbury.sqf"}];		
		_wp = _grp addWaypoint [position _radio, 0];
		_wp2 = _grp addWaypoint [position _radio, 50];
		_wp3 = _grp addWaypoint [position _radio, 50];
		_wp4 = _grp addWaypoint [position _radio, 50];
		_wp5 = _grp addWaypoint [position _radio, 50];
		[_grp, 5] setWaypointType "CYCLE";
		[_grp] call EGG_EVO_FillInf;
		_inf=_inf-(Squadsize+1);
		_recy = [objnull,_grp] execVM "scripts\grecycle.sqf";
	};

//Officer defence
	Sleep 2;
	if(_inf > Squadsize) then
	{
		Sleep 1;
		_grp = createGroup (east);
		_type = EGG_EVO_east1 select 0;
		_unit = _grp createUnit [_type, position _offobj, [], 10, "FORM"];Sleep EGG_EVO_GlobalSleep;
		_unit addEventHandler ["killed", {handle = [_this select 0,"INF"] execVM "scripts\mobjbury.sqf"}];		
		_wp = _grp addWaypoint [position _offobj, 100];
		_wp2 = _grp addWaypoint [position _offobj, 100];
		_wp3 = _grp addWaypoint [position _offobj, 100];
		_wp4 = _grp addWaypoint [position _offobj, 100];
		_wp5 = _grp addWaypoint [position _offobj, 100];
		[_grp, 5] setWaypointType "CYCLE";
		[_grp] call EGG_EVO_FillInf;
		_inf=_inf-(Squadsize+1);
		_recy = [objnull,_grp] execVM "scripts\grecycle.sqf";
	};

	//Radio defence2
	//player sideChat format ["Radio defence2:%1:%2",_i,_inf];
	if(_inf > Squadsize) then
	{
		Sleep 1;
		_grp = createGroup (east);
		_type = EGG_EVO_east1 select 0;
		_unit = _grp createUnit [_type, position _radio, [], 10, "FORM"];Sleep EGG_EVO_GlobalSleep;
		_unit addEventHandler ["killed", {handle = [_this select 0,"INF"] execVM "scripts\mobjbury.sqf"}];
		_wp = _grp addWaypoint [position _radio, 0];
		_wp2 = _grp addWaypoint [position _radio, 100];
		_wp3 = _grp addWaypoint [position _radio, 100];
		_wp4 = _grp addWaypoint [position _radio, 100];
		_wp5 = _grp addWaypoint [position _radio, 100];
		[_grp, 5] setWaypointType "CYCLE";
		[_grp] call EGG_EVO_FillInf;
		_inf=_inf-(Squadsize+1);
		_recy = [objnull,_grp] execVM "scripts\grecycle.sqf";
	};

//Spawn infantry
	Sleep 2;
	while {_inf > 0} do 
	{
		_grp = createGroup (east);
		_type = EGG_EVO_east1 select 0;
		_unit = _grp createUnit [_type, _pos, [], 300, "FORM"];Sleep EGG_EVO_GlobalSleep;
		_rds = (_unit nearRoads 50);
		if(count _rds > 0) then 
		{
			_unit setpos position (_rds select 0);
		};			
		_unit addEventHandler ["killed", {handle = [_this select 0,"INF"] execVM "scripts\mobjbury.sqf"}];
		_posasl = getPosASL _unit;
		if ((_posasl select 2) < 1.0) then 
		{
			_unit setpos _pos;
		};		
		if(round(random 1) ==1) then
		{
			_wp = _grp addWaypoint [_pos, 400];
			_wp2 = _grp addWaypoint [_pos, 400];
			_wp3 = _grp addWaypoint [_pos, 400];
			[_grp, 3] setWaypointType "CYCLE";
		};
		[_grp] call EGG_EVO_FillInf;
		_inf = _inf-(Squadsize+1);
		Sleep 0.1;
		_recy = [objnull,_grp] execVM "scripts\grecycle.sqf";
	};

// Spawn Transport units (default 3)
	Sleep 2;
	while {_transport > 0} do 
	{
		_allvecs = EGG_EVO_eastveh3; 
		_max = (count _allvecs)-1;
		_array = [(_allvecs select round random _max),_pos,(east),300,180,0,3] call EGG_EVO_CreateVehicle;
		_grp = _array select 0;
		_vec = _array select 1;
		_rds = (_vec nearRoads 20);
		if(not Isnull (_rds select 0)) then
		{
			_vec setpos position (_rds select 0);
			_vec setdir (getdir (_rds select 0));
		}
		else
		{
			_vec setdir 180;
		};
		if(round(random 1) == 1) then
		{	
			_wp = _grp addWaypoint [(_outpoints select 0), 0];
			_wp2 = _grp addWaypoint [(_outpoints select 1), 0];
			_wp3 = _grp addWaypoint [(_outpoints select 2), 0];
			_wp4 = _grp addWaypoint [(_outpoints select 3), 0];
			[_grp, 4] setWaypointType "CYCLE";
		};		
		[_grp] call EGG_EVO_OrderSquad;
		_transport = _transport-1; //##9,8,7,6,5,4,3,2,1,0
		Sleep 1;
		_recy = [objnull,_grp] execVM "scripts\grecycle.sqf";
		{_x addEventHandler ["killed", {handle = [_this select 0,"MEC"] execVM "scripts\mobjbury.sqf"}]} forEach (units _grp);
	};

// Spawn Mechanised units (default 11)
	Sleep 2;
	while {_mec > 0} do 
	{
		_allvecs = EGG_EVO_eastveh4; 
		_max = (count _allvecs)-1;
		_array = [(_allvecs select round random _max),_pos,(east),300,180,0,3] call EGG_EVO_CreateVehicle;
		_grp = _array select 0;
		_vec = _array select 1;
		_rds = (_vec nearRoads 20);
		if(not Isnull (_rds select 0)) then
		{
			_vec setpos position (_rds select 0);
			_vec setdir (getdir (_rds select 0));
		}
		else
		{
			_vec setdir 180;
		};
		if(round(random 1) == 1) then
		{	
			_wp = _grp addWaypoint [(_outpoints select 0), 0];
			_wp2 = _grp addWaypoint [(_outpoints select 1), 0];
			_wp3 = _grp addWaypoint [(_outpoints select 2), 0];
			_wp4 = _grp addWaypoint [(_outpoints select 3), 0];
			[_grp, 4] setWaypointType "CYCLE";
		};		
		[_grp] call EGG_EVO_OrderSquad;
		_mec = _mec-1; //##9,8,7,6,5,4,3,2,1,0
		Sleep 1;
		_recy = [objnull,_grp] execVM "scripts\grecycle.sqf";
		{_x addEventHandler ["killed", {handle = [_this select 0,"MEC"] execVM "scripts\mobjbury.sqf"}]} forEach (units _grp);
	};

// Spawn Tank units (default 2)
	Sleep 2;
	while {_tanks > 0} do 
	{
		_allvecs = EGG_EVO_eastveh5; 
		_max = (count _allvecs)-1;
		_array = [(_allvecs select round random _max),_pos,(east),300,180,0,3] call EGG_EVO_CreateVehicle;
		_grp = _array select 0;
		_vec = _array select 1;
		_rds = (_vec nearRoads 20);
		if(not Isnull (_rds select 0)) then
		{
			_vec setpos position (_rds select 0);
			_vec setdir (getdir (_rds select 0));
		}
		else
		{
			_vec setdir 180;
		};
		if(round(random 1) == 1) then
		{	
			_wp = _grp addWaypoint [(_outpoints select 0), 0];
			_wp2 = _grp addWaypoint [(_outpoints select 1), 0];
			_wp3 = _grp addWaypoint [(_outpoints select 2), 0];
			_wp4 = _grp addWaypoint [(_outpoints select 3), 0];
			[_grp, 4] setWaypointType "CYCLE";
		};		
		[_grp] call EGG_EVO_OrderSquad;
		_tanks = _tanks-1; //##9,8,7,6,5,4,3,2,1,0
		Sleep 1;
		_recy = [objnull,_grp] execVM "scripts\grecycle.sqf";
		{_x addEventHandler ["killed", {handle = [_this select 0,"MEC"] execVM "scripts\mobjbury.sqf"}]} forEach (units _grp);
	};
// Static Guns
	_i=0;
	while {_stat > 0} do 
	{
		//added mortar etc
		_allvecs = EGG_EVO_eastveh6;
		_max = (count _allvecs)-1;
		_array = [_allvecs select (round random _max),_pos,(east),200,180,0,4] call EGG_EVO_CreateVehicle;
		_grp = _array select 0;
//		{_x addEventHandler  ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}]} forEach (units _grp);
		{_x setDir 180} forEach (units _grp);
		{_x setBehaviour "combat"} forEach (units _grp);
		_stat = _stat-1; //##4,3,2,1,0
		_recy = [objnull,_grp] execVM "scripts\grecycle.sqf";
		Sleep 1;
	};

// Bunkers
	_i=0;
	_bunk_types = EGG_EVO_eastveh17;
	_bunk_max = (count _bunk_types)-1;
	while {_i < _bunkno} do
	{
		_thebunk = _bunk_types select (round random _bunk_max);
		_rand_bunk = random 400;
		_bunk = _thebunk createVehicle [(_pos select 0)+(_rand_bunk-200), (_pos select 1)+(_rand_bunk-200),0];
		_bunk setdir (random 180);
		_bunk addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}];
		_i = _i + 1;
		sleep 0.2;
	}; 
	
// Tripwires
	_i=0;
	_bomb_types = EGG_EVO_Trapweapons;
	_bomb_max = (count _bomb_types)-1;
	while {_i < 20} do
	{
		_thebomb = _bomb_types select (round random _bomb_max);
		_rand_bomb_x = random 400;
		_rand_bomb_y = random 400;
		_bomb = _thebomb createVehicle [(_pos select 0)+(_rand_bomb_x-200), (_pos select 1)+(_rand_bomb_y-200),0];
		_i = _i + 1;
		sleep 1;
	}; 
//	[_pos] execVM "scripts\support\dropdelmines.sqf";

// ANTI-AIR defence
	Sleep 1;
	_MakeAA =
	{
		Sleep 1;
		_respawnpoint = _outpoints select _curpoint;
		_allvecs = EGG_EVO_eastveh10;
		_max = (count _allvecs)-1;
		_vcl = createVehicle [(_allvecs select (round random _max)), _respawnpoint, [], 120, "NONE"];
		_vcl setdir random 359;	
		_grp = createGroup (east);
		_type = EGG_EVO_east1 select 0;
		_unit1 = _grp createUnit [_type, _respawnpoint, [], 0, "FORM"];Sleep EGG_EVO_GlobalSleep;
		_unit2 = _grp createUnit [_type, _respawnpoint, [], 0, "FORM"];Sleep EGG_EVO_GlobalSleep;
		_unit3 = _grp createUnit [_type, _respawnpoint, [], 0, "FORM"];Sleep EGG_EVO_GlobalSleep;
		[_unit1,_unit2,_unit3] join _grp;
		{_x addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}]} forEach (units _grp);
		_vcl addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}];
		[_grp, 0] spawn EGG_EVO_skill;
		sleep 1.0;
		_unit1 AssignAsCommander _vcl;
		_unit1 moveInCommander _vcl;
		_unit2 AssignAsGunner _vcl;
		_unit2 moveInGunner _vcl;
		_unit3 AssignAsDriver _vcl;
		_unit3 moveInDriver _vcl;
		[_vcl] call EGG_EVO_Lock;
		_unattended = [_vcl] spawn {[_this select 0] call EGG_EVO_idelSVEC};
		[_vcl] execVM "scripts\EVO_VecRAA.sqf";
		_recy = [objnull,_grp] execVM "scripts\grecycle.sqf";
		_curpoint =_curpoint+1;
	};
	
	_curpoint = 0;
	_AA=4;
	while {_AA > 0} do 
	{
		[] call _MakeAA;
		_AA=_AA-1;
		sleep 1;
	};

//reinforcements loop
	_rwait = reinfdelay;
	for [{_rloop=0}, {_rloop<1}, {_rloop=_rloop}] do
	{
		if (reinfdelay ==0) then {_rloop=1};
		if (not (alive _radio)) then {_rloop=1};
		if ("Man" countType list EGG_EVO_DetectWest > 0)then {_reinf = [] execVM "scripts\reinforce.sqf"};
		sleep _rwait;
	};
};
