/*
- makeairbase.sqf - players must attack a prefab airbase to take down a large tower and destroy 4 aircraft
- discovery leads to lots of enemy troops spawning so stealth is key
- taking out the aircraft reduces the enemy aircraft respawn frequency in the main mission
- taking down the tower reduces the enemy spetznaz raiders who attack Blufor comms (to be implemented)
- Once Blufor comms are taken out/occupied by the raiders, U/ Support menu goes down.
- Periodically, after destruction, the airbase comes back online

//build requirements:
4 helipads on which to spawn choppers with a c4only sqf on them
1 invis helipad on which to spawn a tower
2 barracks markers in which to spawn enemy units
spawn 4 sentry teams at barracks then send them out on patrol - waypoints beong the corners of the AO square
2 sensors - blufor detected/ blufor present to control AI spawning
2 publicvariables for tower/ planes killed
1 objective marker linked to alive base
1 loop to rebuild base periodically
mines/wire

*/

//add to init
//EGG_EVO_masts =["Land_Communication_F"];

if !(isServer) exitwith{};


_deleteairbase =
{
	{_handle = [_x] execVM "scripts\delete.sqf"} forEach [_airguard1,_airguard2,_airguard3,_airguard4,_airguard5,_airguardref1,_airguardref2];
	{deletevehicle _x} forEach [_heli,_heli1,_heli2,_heli3,mast,_helir,_helir1];
};

_EGG_EVO_airmakebase =
{
	_allvecsNG = EGG_EVO_eastveh1;
	_maxNG = (count _allvecsNG)-1;
	_allvecsG = EGG_EVO_eastveh9;
	_maxG = (count _allvecsG)-1;

	_posh1 = GetPos airheli1;
	_posh2 = GetPos airheli2;
	_posh3 = GetPos airheli3;
	_posh4 = GetPos airheli4;

	_dirh1 = Getdir airheli1;
	_dirh2 = Getdir airheli2;
	_dirh3 = Getdir airheli3;
	_dirh4 = Getdir airheli4;

	_allvecsM = EGG_EVO_masts;
	_maxVm = (count _allvecsM)-1;

	_allspawnsairm = ["airmast1","airmast2","airmast3"];
	_maxm = (count _allspawnsairm)-1;

	_posmast = GetMarkerPos (_allspawnsairm select round random _maxm);
	sleep 2;

	_heli = createVehicle [(_allvecsG select round random _maxG), _posh1, [], 0, "NONE"];_heli setdir _dirh1;Sleep EGG_EVO_GlobalSleep;
	_heli1 = createVehicle [(_allvecsG select round random _maxG), _posh2, [], 0, "NONE"];_heli1 setdir _dirh2;Sleep EGG_EVO_GlobalSleep;
	_heli2 = createVehicle [(_allvecsNG select round random _maxNG), _posh3, [], 0, "NONE"];_heli2 setdir _dirh3;Sleep EGG_EVO_GlobalSleep;
	_heli3 = createVehicle [(_allvecsNG select round random _maxNG), _posh4, [], 0, "NONE"];_heli3 setdir _dirh4;Sleep EGG_EVO_GlobalSleep;
	mast = createVehicle [(_allvecsM select round random _maxVm), _posmast, [], 0, "NONE"];
	sleep 5;

	if ({not (isnull _x)} forEach [_heli,_heli1,_heli2,_heli3,mast]) then {hint "Enemy airbase online"};

	{_c4air = [_x] execVM "scripts\expansion\airc4only.sqf"} forEach [_heli,_heli1,_heli2,_heli3,mast];
	_airtobj = [mast] execVM "scripts\expansion\airobj.sqf";
	{_c4airH = [_x] execVM "scripts\expansion\airobjH.sqf"} forEach [_heli,_heli1,_heli2,_heli3];

	{_x addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}]} forEach [_heli,_heli1,_heli2,_heli3,mast];
	{[_x] call EGG_EVO_Lock}forEach [_heli,_heli1,_heli2,_heli3];

	_returnarray4 = [_heli,_heli1,_heli2,_heli3];
	_returnarray4;

//add mines/wire

};

_EGG_EVO_airguard =
{
	_allspawnsairg = ["airbarracks1","airbarracks2","airbarracks3"];
	_maxag = (count _allspawnsairg)-1;
	_posg = GetMarkerPos (_allspawnsairg select round random _maxag);

	_airguard1 = createGroup (east);
	sleep 0.2;
	_airguard2 = createGroup (east);
	sleep 0.2;
	_airguard3 = createGroup (east);
	sleep 0.2;
	_airguard4 = createGroup (east);
	sleep 0.2;
	_airguard5 = createGroup (east);

	_allunits = EGG_EVO_east1;
	_maxu = (count _allunits)-1;

	sleep 5;
	{
		_posg = GetMarkerPos (_allspawnsairg select round random _maxag);
		_d = 0;
		while {_d <= (1+ round random 1)} do 
		{
			_unit = _x createUnit [(_allunits select round random _maxu), _posg, [], 5, "FORM"];
			_unit addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}];
			_d = _d+1;
			sleep 0.2;
		};
		_x setCombatMode "WHITE";
		_x setFormation "STAG COLUMN";
		_x setSpeedMode "LIMITED";
		_x setBehaviour "SAFE";
		[_x, 1] spawn EGG_EVO_skill;

		_recy = [player,_x] execVM "scripts\grecycle.sqf";
	}forEach [_airguard1,_airguard2,_airguard3,_airguard4,_airguard5];

	sleep 10;

//waypoints
	_wpos1 = getmarkerpos "airmast1";
	_wpos2 = getmarkerpos "airmast2";
	_wpos3 = getmarkerpos "airmast3";
	_wpos4 = getmarkerpos "airbarracks1";
	_wpos5 = getmarkerpos "airbarracks2";
	_wpos6 = getmarkerpos "airbarracks3";

	_wpos7 = getmarkerpos "airSW";
	_wpos8 = getmarkerpos "airNE";

	_wpos9 = [(_wpos8 select 0),(_wpos7 select 1),(_wpos8 select 2)];//SE
	_wpos10 = [(_wpos7 select 0),(_wpos8 select 1),(_wpos8 select 2)];//NW

	_wpos11 = GetPos airheli1;
	_wpos12 = GetPos airheli2;
	_wpos13 = GetPos airheli3;
	_wpos14 = GetPos airheli4;
	_wpos15 = GetPos mast;

	_allpointsA = [_wpos11,_wpos12,_wpos13,_wpos14,_wpos15];//targets
	_allpointsB = [_wpos1,_wpos2,_wpos3];//masts
	_allpointsC = [_wpos4,_wpos5,_wpos6];//barracks
	_allpointsD = [_wpos7,_wpos10,_wpos8,_wpos9];//perimeter

	_maxwpA = (count _allpointsA)-1;
	_maxwpB = (count _allpointsB)-1;
	_maxwpC = (count _allpointsC)-1;
	_maxwpD = (count _allpointsD)-1;

//guards patrolling clockwise
	_wp11 = _airguard1 addWaypoint [(_allpointsD select 0), 5];
	_wp12 = _airguard1 addWaypoint [(_allpointsD select 1), 5];
	_wp13 = _airguard1 addWaypoint [(_allpointsD select 2), 5];
	_wp14 = _airguard1 addWaypoint [(_allpointsD select 3), 5];
	[_airguard1, 4] setWaypointType "CYCLE";
sleep 20+random 10;
//guards patrolling anticlockwise
	_wp21 = _airguard2 addWaypoint [(_allpointsD select 3), 5];
	_wp22 = _airguard2 addWaypoint [(_allpointsD select 2), 5];
	_wp23 = _airguard2 addWaypoint [(_allpointsD select 1), 5];
	_wp24 = _airguard2 addWaypoint [(_allpointsD select 0), 5];
	[_airguard2, 4] setWaypointType "CYCLE";
sleep 20+random 10;

//guards patrolling randomly/targets
	_wp31 = _airguard3 addWaypoint [(_allpointsA select round random _maxwpA), 5];
	_wp32 = _airguard3 addWaypoint [(_allpointsC select round random _maxwpC), 5];
	_wp33 = _airguard3 addWaypoint [(_allpointsB select round random _maxwpB), 5];
	_wp34 = _airguard3 addWaypoint [(_allpointsD select round random _maxwpD), 5];
	[_airguard3, 4] setWaypointType "CYCLE";
sleep 20+random 10;

//guards patrolling targets
	_wp41 = _airguard4 addWaypoint [(_allpointsA select round random _maxwpA), 5];
	_wp42 = _airguard4 addWaypoint [(_allpointsC select round random _maxwpC), 5];
	_wp43 = _airguard4 addWaypoint [(_allpointsA select round random _maxwpA), 5];
	_wp44 = _airguard4 addWaypoint [(_allpointsB select round random _maxwpB), 5];
	[_airguard4, 4] setWaypointType "CYCLE";
sleep 20+random 10;

//guards patrolling targets
	_wp51 = _airguard5 addWaypoint [(_allpointsD select round random _maxwpD), 5];
	_wp52 = _airguard5 addWaypoint [(_allpointsB select round random _maxwpB), 5];
	_wp53 = _airguard5 addWaypoint [(_allpointsA select round random _maxwpA), 5];
	_wp54 = _airguard5 addWaypoint [(_allpointsB select round random _maxwpB), 5];
	_wp55 = _airguard5 addWaypoint [(_allpointsC select round random _maxwpC), 5];
	_wp56 = _airguard5 addWaypoint [(_allpointsD select round random _maxwpD), 5];
	_wp57 = _airguard5 addWaypoint [(_allpointsA select round random _maxwpA), 5];
	[_airguard5, 7] setWaypointType "CYCLE";

	_returnarray3 = [_airguard1,_airguard2,_airguard3,_airguard4,_airguard5];
	_returnarray3;

};


_EGG_EVO_airalarm =
{
//spawn a group from barracks and a group from waypoint off base
//_airguardref1 and _airguardref2
	hint "Alarm!!!!";
	_allspawnsairref = ["cityBref1","cityBref2","cityKref1","cityKref3","cityAref1","cityAref2","cityAref3"];
	_maxr = (count _allspawnsairref)-1;
	_posr = GetMarkerPos (_allspawnsairref select round random _maxr);
	_allspawnsairg = ["airbarracks1","airbarracks2","airbarracks3"];
	_maxag = (count _allspawnsairg)-1;
	_posg = GetMarkerPos (_allspawnsairg select round random _maxag);

//waypoints
	_wpos1 = getmarkerpos "airmast1";
	_wpos2 = getmarkerpos "airmast2";
	_wpos3 = getmarkerpos "airmast3";
	_wpos4 = getmarkerpos "airbarracks1";
	_wpos5 = getmarkerpos "airbarracks2";
	_wpos6 = getmarkerpos "airbarracks3";

	_wpos7 = getmarkerpos "airSW";
	_wpos8 = getmarkerpos "airNE";

	_wpos9 = [(_wpos8 select 0),(_wpos7 select 1),(_wpos8 select 2)];//SE
	_wpos10 = [(_wpos7 select 0),(_wpos8 select 1),(_wpos8 select 2)];//NW

	_allpointsA = [_posh1,_posh2,_posh3,_posh4,_posmast];//targets
	_allpointsB = [_wpos1,_wpos2,_wpos3];//masts
	_allpointsC = [_wpos4,_wpos5,_wpos6];//barracks
	_allpointsD = [_wpos7,_wpos10,_wpos8,_wpos9];//perimeter

	_maxwpA = (count _allpointsA)-1;
	_maxwpB = (count _allpointsB)-1;
	_maxwpC = (count _allpointsC)-1;
	_maxwpD = (count _allpointsD)-1;

	_airguardref1 = createGroup (east);
	sleep 0.2;
	_airguardref2 = createGroup (east);
	_allunits = EGG_EVO_east1;
	_maxu = (count _allunits)-1;
	_allvecB = EGG_EVO_mevconvoyb; //trucks
	_maxB = (count _allvecB)-1;
	sleep 5;

	_posgr = GetMarkerPos (_allspawnsairg select round random _maxag);

	_d = 0;
	while {_d <= Squadsize} do 
	{
		_unit = _airguardref1 createUnit [(_allunits select round random _maxu), _posgr, [], 2, "FORM"];
		_unit addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}];
		_d = _d+1;
		sleep 0.2;
	};
	sleep 10;
	_airguardref1 setCombatMode "YELLOW";
	_airguardref1 setFormation "STAG COLUMN";
	_airguardref1 setSpeedMode "NORMAL";
	_airguardref1 setBehaviour "AWARE";
	[_airguardref1, 2] spawn EGG_EVO_skill;

	_recy = [player,_airguardref1] execVM "scripts\grecycle.sqf";
	_wp61 = _airguardref1 addWaypoint [(_allpointsD select round random _maxwpD), 5];
	_wp62 = _airguardref1 addWaypoint [(_allpointsB select round random _maxwpB), 5];
	_wp63 = _airguardref1 addWaypoint [(_allpointsA select round random _maxwpA), 5];
	_wp64 = _airguardref1 addWaypoint [(_allpointsB select round random _maxwpB), 5];
	_wp65 = _airguardref1 addWaypoint [(_allpointsC select round random _maxwpC), 5];
	_wp66 = _airguardref1 addWaypoint [(_allpointsD select round random _maxwpD), 5];
	_wp67 = _airguardref1 addWaypoint [(_allpointsA select round random _maxwpA), 5];
	[_airguardref1, 7] setWaypointType "CYCLE";

	_sumark = [(vehicle leader _airguardref1)] execVM "scripts\air\sumarker3.sqf";

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

//armed response team off-base
	_posr = GetMarkerPos (_allspawnsairref select round random _maxr);
	_helir = createVehicle [(_allvecB select round random _maxB), _posr, [], 0, "NONE"];Sleep EGG_EVO_GlobalSleep;
	_helir1 = createVehicle [(_allvecB select round random _maxB), _posr, [], 0, "NONE"];Sleep EGG_EVO_GlobalSleep;

//calculate crew
	{
		[_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 = _airguardref2 createUnit [(_allunits select round random _maxu), _posr, [], 5, "FORM"];
			_unit AssignasDriver _x;
			_unit moveinDriver _x;
		};
		Sleep 0.1;
		if((_x emptyPositions "commander") > 0) then 
		{
			_unit = _airguardref2 createUnit [(_allunits select round random _maxu), _posr, [], 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 = _airguardref2 createUnit [(_allunits select round random _maxu), _posr, [], 5, "FORM"];
			_unit AssignasCargo _x;
			_unit moveinCargo _x;
			_n = _n+1;
			Sleep 0.1;
		};
	} forEach [_helir,_helir1];
	_airguardref2 setFormation "COLUMN";
	_airguardref2 setSpeedMode "NORMAL";
	_airguardref2 setBehaviour "AWARE";
	[_airguardref2, 3] spawn EGG_EVO_skill;

	{_x addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}]} forEach (units _airguardref2);
	_recy = [objNull,_airguardref2] execVM "scripts\grecycle.sqf";
	sleep 10;
	_wp = _airguardref2 addWaypoint [(position mast), 20];
	[_airguardref2, 1] setWaypointType "GETOUT";
	_sumark2 = [(vehicle leader _airguardref2)] execVM "scripts\air\sumarker3.sqf";
	_returnarray2 = [_airguardref1,_airguardref2];
	_returnarray2;

};


_pos = getMarkerPos "airbase";
EGG_EVO_DetectWestB = createTrigger ["EmptyDetector", _pos];
EGG_EVO_DetectWestB setTriggerActivation ["WEST", "PRESENT", true];
EGG_EVO_DetectWestB setTriggerArea [400, 400, 0, true];
EGG_EVO_DetectWestB setTriggerTimeout [2, 2, 2, true ];

EGG_EVO_DetectWestA = createTrigger ["EmptyDetector", _pos];
EGG_EVO_DetectWestA setTriggerActivation ["WEST", "EAST D", true];
EGG_EVO_DetectWestA setTriggerArea [200, 200, 0, true];
EGG_EVO_DetectWestA setTriggerTimeout [20, 20, 20, true ];

_returnarray2 = [];
_returnarray3 = [];
_returnarray4 = [];

_rf = (5 * reinfdelay);

//test
//_rf = 30;

_alarmON = 0;
_basemade = 0;
//mast=objnull;

for [{_loop=0}, {_loop<1}, {_loop=_loop}] do
{

	//make base and set sentries
	if (_basemade ==0) then 
	{
		_array1 = [] call _EGG_EVO_airmakebase;
		_heli = _array1 select 0;
		_heli1 = _array1 select 1;
		_heli2 = _array1 select 2;
		_heli3 = _array1 select 3;

		_array2 = [] call _EGG_EVO_airguard;
		sleep 1;
		_airguard1 = _array2 select 0;
		_airguard2 = _array2 select 1;
		_airguard3 = _array2 select 2;
		_airguard4 = _array2 select 3;
		_airguard5 = _array2 select 4;
		_basemade = 1;
		_alarmON =0;
		EGG_aircomms =1;
		publicVariable "EGG_aircomms";
	};
	sleep 5;

	//call reinforcements if detected
	if ((_basemade ==1) and ("Man" countType list EGG_EVO_DetectWestA >0) and (_alarmON ==0 ) )then
	{
		_array3 = [] call _EGG_EVO_airalarm;
		sleep 1;
		_airguardref1 = _array3 select 0;
		_airguardref2 = _array3 select 1;
		_alarmON =1;
	};

	//allow waves of reinforcements from barracks
	if (!isNil "_airguardref1") then
	{
		if (!isNil "_airguardref1") then
		{
			if ( (_basemade ==1) and ({alive _x} count (units _airguardref1) <1) and ({alive _x} count (units _airguardref2) <1) ) then {_alarmON =0};
		};
	};

	//reset sentries when all killed
	if ( _basemade ==1) then
	{
		_airguards = 0;
		
		if (!isNil "_airguard1") then {_airguards = _airguards + {alive _x} count (units _airguard1); };
		if (!isNil "_airguard2") then {_airguards = _airguards + {alive _x} count (units _airguard2); };
		if (!isNil "_airguard3") then {_airguards = _airguards + {alive _x} count (units _airguard3); };
		if (!isNil "_airguard4") then {_airguards = _airguards + {alive _x} count (units _airguard4); };
		if (!isNil "_airguard5") then {_airguards = _airguards + {alive _x} count (units _airguard5); };
		
		if (_airguards < 1) then { [] call _EGG_EVO_airguard };
	};

	//change global parameter if mission accomplished
	if ( (_basemade ==1) and !isNil "_heli" and !isNil "_heli1" and !isNil "_heli2" and !isNil "_heli3" and !isNil "mast") then
	{
		if (({(not (alive _x))} forEach [_heli,_heli1,_heli2,_heli3, mast]) ) then 
		{
			EGG_aircomms =0;
			publicVariable "EGG_aircomms";

			hint "good job!";

			playSound "CityClear";
			{_x addscore EGG_EVO_cityscore} forEach list EGG_EVO_DetectWestB;
			sleep 30;
			[] call _deleteairbase;
			_basemade = 0;
			_alarmON =0;
			sleep _rf;
		};
	};
	sleep 1.3;
};

