// resupplies towns with reinforcements

//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
//B_Parachute_F or B_Parachute_02_F - replace when available

	_spawn = [0,0,0];
	_spawns = [];
	_radio = objNull;
	_alist = objNull;
	_tag = "";
	_type = "";
	_wtime = 0;
	_radio = EGG_EVO_radios select EGG_EVO_MissionProgress;
	_pos = position _radio;

	_alist = EGG_EVO_DetectWest;
	_curtownInf = round(((EGG_EVO_Infantry select EGG_EVO_MissionProgress) select 0)/enemynumdiv);
	_basetownInf = round(((EGG_EVO_Infantry select EGG_EVO_MissionProgress) select 1)/enemynumdiv);
	_curtownMec = round(((EGG_EVO_Mechanized select EGG_EVO_MissionProgress) select 0)/enemynumdiv);
	_basetownMec = round(((EGG_EVO_Mechanized select EGG_EVO_MissionProgress) select 1)/enemynumdiv);
	_helitarg = EGG_EVO_helipads select EGG_EVO_MissionProgress;

	debuglog format["reinforce.sqf CurTownInfantry:%1 BaseTownInfantry:%2 CurTownMech:%3 BaseTownMech:%4 ",_curtownInf,_basetownInf,_curtownMec,_basetownMec];
	
	sleep 1;

	EGG_EVO_mKamazOpen =
	{
		sleep reinfdelay;
		
		_guardr = grpNull;
		_unit = objNull;
		_pos2 = objNull;
		_max = objnull;
		_alist = EGG_EVO_DetectWest;
		_radio = EGG_EVO_radios select EGG_EVO_MissionProgress;
		_pos = position _radio;
		_allunits = EGG_EVO_east1;
		_max = (count _allunits)-1;
		_guardr = createGroup (east);
		_spawns = EGG_EVO_Allspawns select EGG_EVO_MissionProgress;
		_pos2 = GetMarkerPos (_spawns select (round random 2));
		_allvecs = EGG_EVO_mevconvoyb;
		_maxA = (count _allvecs)-1;
		
/*
		_ural = createVehicle [(_allvecs select (round random _maxA)), _pos2, _spawns, 2, "NONE"];
		[_ural] call EGG_EVO_Lock;
		_ural addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}];
		Sleep 0.2;
*/
		_unit = _guardr createUnit [(_allunits select (round random _max)), _pos2, [], 10, "NONE"];
		[_unit] join _guardr;
/*
		_unit assignAsDriver _ural;
		_unit moveInDriver _ural;
		Sleep 0.2;
		_utype = TypeOf _ural;
*/
//		_maxcrew = getNumber (configFile >> "CfgVehicles" >> _utype >> "transportSoldier");
		_maxcrew = 10; //
		_n = 0;
		while {_n < _maxcrew} do 
		{
			_unit = _guardr createUnit [_allunits select (round random _max), _pos2, [], 10, "NONE"];
//			_unit AssignAsCargo _ural;
//			_unit moveinCargo _ural;
			_n = _n+1;
			Sleep 0.1;
		};
		
		[_guardr, 3] spawn EGG_EVO_skill;
//		[position _alist,_guardr,_radio,_alist] call EGG_EVO_Erefway;
		_wp = _guardr addWaypoint [_pos, 100];
		_wp2 = _guardr addWaypoint [_pos, 40];
		_wp3 = _guardr addWaypoint [_pos, 20];
		[_guardr, 1] setWaypointType "MOVE";
//		[_guardr, 2] setWaypointType "GETOUT";
//		[_guardr, 3] setWaypointType "SAD";

		{_x addEventHandler ["killed", {handle = [_this select 0,"INF"] execVM "scripts\mobjbury.sqf"}]} forEach (units _guardr);
		_recy = [objnull,_guardr] execVM "scripts\grecycle.sqf";
		_guardr setFormation "COLUMN";
//		sleep 60;
//		waitUntil {(_ural distance _pos < 50) or not (canmove _ural)};
//		sleep 90;
//		deleteVehicle _ural;
	};

/*
	EGG_EVO_MI17support =
	{	
		sleep reinfdelay;
		_pos = objNull;
		_unit = objNull;
		_vec = objNull;
		_heli0 = objnull;
		_maxh = objnull;
		_maxv = objnull;
		_alist = EGG_EVO_DetectWest;
		_maxh = (count EGG_EVO_east2)-1;
		_radio = EGG_EVO_radios select EGG_EVO_MissionProgress;
		_pos = position _radio;
		_postarg = position _helitarg;
		_posback = getmarkerpos "EnemyAir03";

		_pilot = createGroup (east);
		(EGG_EVO_mepilot select 0) createUnit [getmarkerpos "centerp", _pilot];Sleep EGG_EVO_GlobalSleep;
		(EGG_EVO_mepilot select 0) createUnit [getmarkerpos "centerp", _pilot];Sleep EGG_EVO_GlobalSleep;

		_pos1 = [(_pos select 0)-1000 -(random 2000),(_pos select 1)+2000 -(random 3000),(200 + random 100)];
		_pos2 = [(_pos select 0)+1000 +(random 2000),(_pos select 1)+2000 -(random 3000),(200 + random 100)];

		_allvecs2 = EGG_EVO_mevair3;
		_maxv = (count _allvecs2)-1;
		_heli0 = createVehicle [(_allvecs2 select round random _maxv),_pos1, [], 500, "FLY"];Sleep EGG_EVO_GlobalSleep;
		_heli0 setposATL [getpos _heli0 select 0, getpos _heli0 select 1, 200];
		_heli0 setdir (random 359);
		_heli0 engineon true;
		(units _pilot select 0) assignAsDriver _heli0;
		(units _pilot select 1) assignAsGunner _heli0;
		(units _pilot select 0) moveInDriver _heli0;
		(units _pilot select 1) moveInGunner _heli0;
		_sumark = [_heli0] execVM "scripts\air\sumarker.sqf";

		{_x setBehaviour "careless"} forEach (units _pilot);
		_wpx = _pilot addWaypoint [_postarg, 20];
		[_pilot, 1] setWaypointType "TR UNLOAD";
//		[_pilot, 1] setWaypointType "MOVE";

//adding
		_para = createGroup (east);
		_htype = TypeOf _heli0;
		_maxcrewh = getNumber (configFile >> "CfgVehicles" >> _htype >> "transportSoldier");
		_pos3 = getmarkerpos "centerp";
		_p = 0;
		while {_p < _maxcrewh} do 
		{
			_unit = _para createUnit [(EGG_EVO_east2 select round random _maxh), _pos3, [], 0, "NONE"];
			_unit assignAsCargo _heli0;
			_unit moveinCargo _heli0;
			_unit addEventHandler ["killed", {handle = [_this select 0,"INF"] execVM "scripts\mobjbury.sqf"}];
			_p = _p+1;
			Sleep 0.1;
		};
		[_para, 4] spawn EGG_EVO_skill;

		_recy = [objnull,_para] execVM "scripts\grecycle.sqf";
		{_x setBehaviour "combat"} forEach (units _para);

		(driver _heli0) commandMove _postarg;
		(driver _heli0) doMove _postarg;
//		_heli0 domove position _helitarg;
		_heli0 flyInHeight 150;

		waitUntil {(_heli0 distance _helitarg) < 100 or not (canmove _heli0)};

		if (canmove _heli0) then 
		{
//temp removal of parachuting activity
			_heli0 
			_para = createGroup (east);
			_count = 5;
			_i = 0;
			while {_i <= _count} do 
			{
				_posh = position _heli0;
				_unit = _para createUnit [EGG_EVO_east2 select (round (random _maxh)), [0,0,0], [], 300, "NONE"];
				_unit addEventHandler ["killed", {handle = [_this select 0,"INF"] execVM "scripts\mobjbury.sqf"}];
				[_unit] join _para;
				_vec = createVehicle ["B_Parachute_F", _posh, [], 20, 'NONE'];
				_vec setpos [_posh select 0,_posh select 1,(_posh select 2)- 12];
				_unit MoveInDriver _vec;
				sleep 0.7;
				_i = _i + 1;
			};
			
//			_heli0 setfuel 0.01;
//			waituntil {((getposATL _heli0 select 2) <1) or !(canMove _heli0)};


	//temporary override
//			_heli0 setposATL [_postarg select 0, _postarg select 1, 0];
//			_heli0 setfuel 0;
			_wp0 = _para addWaypoint [_pos, 20];
			[_para, 1] setWaypointType "SAD";
			{unassignvehicle _x; _x action ["getout", _heli0]} forEach (units _para);
//			[position _alist,_para,_radio,_alist] call EGG_EVO_Erefway;
			sleep 5;
			_heli0 setfuel 0.5;
			_wpx2 = _pilot addWaypoint [_posback, 20];
			[_pilot, 2] setWaypointType "MOVE";
			(driver _heli0) commandMove _posback;
			(driver _heli0) doMove _posback;
			sleep 90;
		};
		sleep 90;
		{deleteVehicle _x} forEach ([_heli0] + crew _heli0);
		if (!isNull _unit) then {deleteVehicle _unit};
		deleteGroup _pilot;
	};
*/
	
	EGG_EVO_mecreinf = 
	{
		sleep reinfdelay;
		_alist = EGG_EVO_DetectWest;
		_radio = EGG_EVO_radios select EGG_EVO_MissionProgress;
		_unit = objNull;
		_guardm = grpNull;
		_pos = objNull;
		_vec = objNull;
		_maxo = 0;
		_spawns = EGG_EVO_Allspawns select EGG_EVO_MissionProgress;
		_pos = GetMarkerPos (_spawns select (round random 2));
		_tag = "MEC";
		_allvec = EGG_EVO_eastarm; //mixed units reinforce
		_maxo = (count _allvec)-1;	
		_rds = [];
		_rds = (_pos nearRoads 10);
		waitUntil{count _rds > 0};
		_cardir = (getdir (_rds select 0));
		_degrees = [_pos,position _alist] call EGG_EVO_GetRelDir;
		_dirdif = (_cardir-_degrees);
		if((_dirdif > 89) or (_dirdif < -89)) then{_cardir=_cardir-180};	
		_pos = position (_rds select 0);
		_array = [_allvec select (round random _maxo),_pos,(east),20,_cardir,0,3] call EGG_EVO_CreateVehicle;
		_guardm = _array select 0;
		_vec = _array select 1;

		if (enemynumdiv != 1) then
		{
			_allunits = EGG_EVO_east1;
			_max = (count _allunits)-1;
			_utype = TypeOf _vec;
			_maxcrew = getNumber (configFile >> "CfgVehicles" >> _utype >> "transportSoldier");
			_n = 0;
			while {_n < _maxcrew} do 
			{
				_unit = _guardm createUnit [(_allunits select (round random _max)), _pos, [], 10, "NONE"];
				_unit AssignAsCargo _vec;
				_unit moveinCargo _vec;
				_n = _n+1;
				Sleep 0.1;
			};
			[_guardm, 3] spawn EGG_EVO_skill;
		};
		
		_guardm setFormation "COLUMN";
		[position _alist,_guardm,_radio,_alist] call EGG_EVO_Erefway;
//		[_guardm, 1] setWaypointCombatMode "RED";		

//adding
//		_posT = position _radio;
//		_wpm = _guardm addWaypoint [_posT, 70];
//		_wpm2 = _guardm addWaypoint [_posT, 35];
//		[_guardm, 1] setWaypointType "UNLOAD";
//		[_guardm, 2] setWaypointType "HOLD";

		{_x addEventHandler ["killed", {handle = [_this select 0,"MEC"] execVM "scripts\mobjbury.sqf"}]} forEach (units _guardm);
		_recy = [objnull,_guardm] execVM "scripts\grecycle.sqf";
	};

if(_curtownInf <= (_basetownInf) and alive _radio) then 
{
	_tag = "INF";	
	[] spawn EGG_EVO_mKamazOpen;
    //[] spawn EGG_EVO_MI17support;
	(EGG_EVO_Infantry select EGG_EVO_MissionProgress) set [0, _curtownInf+Squadsize];
};
sleep 3;
if(_curtownMec <= (_basetownMec) and alive _radio) then 
{
	[] spawn EGG_EVO_mecreinf;
	(EGG_EVO_Mechanized select EGG_EVO_MissionProgress) set [0, _curtownMec+1];
};
sleep 1;