// makes roaming Hips

if !(isServer) exitwith{};

for [{_loop=0}, {_loop<1}, {_loop=_loop}] do
{
	_aircomms = 12;
	if (EGG_aircomms ==0) then {_aircomms = 24};
	//reinfdelay = 120, 180, 240, 300
	_rf = 0.5*(reinfdelay * _aircomms);
//test
//_rf=10;
	sleep (round random _rf);

	_startsf = ["EnemyAir01","EnemyAir02","EnemyAir03","EnemyAir04","EnemyAir05","EnemyAir06","EnemyAir07","EnemyAir08"];
	_poscreate = getmarkerpos (_startsf select (round random 7));
	_pilot = createGroup (east);

	_rndx = random 600;
	_rndy = random 600;
	_kpos = [(_poscreate select 0)-300 +_rndx,(_poscreate select 1)-300+_rndy,1500];

	_allunits = EGG_EVO_mepilot;
	_maxu = (count _allunits)-1;
	
	_allvecs = EGG_EVO_eastveh1;
	_max = (count _allvecs)-1;

	_upos = getmarkerpos "centerp";

	_heli0 = createVehicle [_allvecs select (round random _max), _kpos, [], 50, "FLY"];Sleep EGG_EVO_GlobalSleep;
	_heli0 setposATL [getpos _heli0 select 0, getpos _heli0 select 1, 1500];
	_heli0 setdir (random 100);

//calculate aircrew
	if((_heli0 emptyPositions "driver") > 0) then 
	{
		_unit = _pilot createUnit [(_allunits select round random _maxu), _upos, [], 5, "NONE"];
		_unit AssignasDriver _heli0;
		_unit moveinDriver _heli0;
	};
	Sleep EGG_EVO_GlobalSleep;
	
	if((_heli0 emptyPositions "gunner") > 0) then 
	{
		_unit = _pilot createUnit [(_allunits select round random _maxu), _upos, [], 5, "NONE"];
		_unit AssignasGunner _heli0;
		_unit moveinGunner _heli0;
	};		
	Sleep EGG_EVO_GlobalSleep;
	
	if((_heli0 emptyPositions "commander") > 0) then 
	{
		_unit = _pilot createUnit [(_allunits select round random _maxu), _upos, [], 5, "NONE"];
		_unit AssignasCommander _heli0;
		_unit moveinCommander _heli0;
	};
	
	_heli0 engineon true;
	_type0 = Typeof _heli0;

//adding second heli

	_heli1 = createVehicle [_allvecs select (round random _max), _kpos, [], 50, "FLY"];Sleep EGG_EVO_GlobalSleep;
	_heli1 setposATL [getpos _heli1 select 0, getpos _heli1 select 1, 1500];
	_heli1 setdir (random 100);
	_sumark = [_heli1] execVM "scripts\air\sumarker2.sqf";

//calculate aircrew
	if((_heli1 emptyPositions "driver") > 0) then 
	{
		_unit = _pilot createUnit [(_allunits select round random _maxu), _upos, [], 5, "NONE"];
		_unit AssignasDriver _heli1;
		_unit moveinDriver _heli1;
	};
	Sleep EGG_EVO_GlobalSleep;
	if((_heli1 emptyPositions "gunner") > 0) then 
	{
		_unit = _pilot createUnit [(_allunits select round random _maxu), _upos, [], 5, "NONE"];
		_unit AssignasGunner _heli1;
		_unit moveinGunner _heli1;
	};		
	Sleep EGG_EVO_GlobalSleep;
	if((_heli1 emptyPositions "commander") > 0) then 
	{
		_unit = _pilot createUnit [(_allunits select round random _maxu), _upos, [], 5, "NONE"];
		_unit AssignasCommander _heli1;
		_unit moveinCommander _heli1;
	};
	_heli1 engineon true;
	_type1 = Typeof _heli1;

/*
	(units _pilot select 0) AssignasDriver _heli0;
	(units _pilot select 0) moveInDriver _heli0;
	(units _pilot select 1) moveInTurret[_heli0,[0]];
	(units _pilot select 2) moveInTurret[_heli0,[1]];
	(units _pilot select 3) moveInTurret[_heli0,[2]];

	(units _pilot select 4) AssignasDriver _heli1;
	(units _pilot select 4) moveInDriver _heli1;
	(units _pilot select 5) moveInTurret[_heli1,[0]];
	(units _pilot select 6) moveInTurret[_heli1,[1]];
	(units _pilot select 7) moveInTurret[_heli1,[2]];
*/

//adding troops

	_maxcrew0 = 0;
	_maxcrew1 = 0;
	if (enemynumdiv==1) then 
	{
		_maxcrew0 = getNumber (configFile >> "CfgVehicles" >> _type0 >> "transportSoldier");
		_maxcrew1 = getNumber (configFile >> "CfgVehicles" >> _type1 >> "transportSoldier");
	};

	_helicount = 2;
	_z = 0;
	while {_z < _helicount} do 
	{
		if (_z ==0) then {_vec = _heli0; _maxcrew = _maxcrew0};
		if (_z ==1) then {_vec = _heli1; _maxcrew = _maxcrew1};
		_Crewcargo = EGG_EVO_east1;
		_ccargo = (count _Crewcargo)-1;
		_n = 0;
		while {_n < _maxcrew} do 
		{
			_unit = _pilot createUnit [_Crewcargo select (round random _ccargo), _upos, [], 0, "NONE"];
			_unit AssignasCargo _vec;
			_unit moveinCargo _vec;
			_n = _n+1;
			Sleep 0.1;
		};
		_z=_z+1;
		Sleep 0.1;
	};

	[_pilot, 4] spawn EGG_EVO_skill;
	_pilot setFormation "WEDGE";
	_pilot setCombatMode "YELLOW";

	_handle = [_pilot] execVM "scripts\air\flightpath.sqf";
	{_x addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}]} forEach (units _pilot);

	_heli0 addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}];
	_spawne = [_heli0] spawn {[_this select 0] call EGG_EVO_idelSVEC};
//	_killh1 = [_heli0, (crew _heli0)] spawn {[_this select 0, _this select 1] call EGG_killpilots};

	_heli1 addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}];
	_spawnf = [_heli1] spawn {[_this select 0] call EGG_EVO_idelSVEC};
//	_killh2 = [_heli1, (crew _heli1)] spawn {[_this select 0, _this select 1] call EGG_killpilots};

//	_addkit = [_heli0] spawn {[_this select 0] call EGG_EVO_Fillcargo};
//	_addkit1 = [_heli1] spawn {[_this select 0] call EGG_EVO_Fillcargo};

	_heli0 addeventhandler ["HandleDamage", {if ((_this select 4) isKindOf "MissileCore") then { 1; } else { _this select 2; }; }];
	_heli1 addeventhandler ["HandleDamage", {if ((_this select 4) isKindOf "MissileCore") then { 1; } else { _this select 2; }; }];

	sleep 10.0;
	waitUntil {( ((not (alive _heli0)) AND (not (alive _heli1)) ) or ( (isNull (driver _heli0)) AND (isNull (driver _heli1)) ) )};

	sleep 1.0;
	if (alive _heli0) then {_heli0 setDamage 1};
	if (alive _heli1) then {_heli1 setDamage 1};
	sleep _rf;
	{if (alive _x) then {_x setDamage 1}} forEach (units _pilot);
};

/*
EGG_killpilots =
{
	_chop = _this select 0;
	_crew = _this select 1;

	waitUntil { ( ({alive _x} count units _chop == 0) or not (canmove _chop) or (isnull _chop) )};
	sleep 30;
	if (alive _chop) then {_chop setDamage 1};
	{if (alive _x) then {_x setDamage 1}} forEach (units _crew);
};
*/
