if (isServer and (isNull player)) exitWith {};
if (isNil "RUG_DSAI_TerminalDistance") then {RUG_DSAI_TerminalDistance = 1000;};
if (isNil "RUG_DSAI_CycleTime") then {RUG_DSAI_CycleTime = 5};

if (count _this > 1) exitWith {{nil= [_x] execVM "RUG_ArabDSAIinit.sqf"} forEach _this};
_units=_this select 0;

_RUG_DSAI_Behaviour = compile loadfile "RUG_DSAI\DSAI_ArabBehaviour.sqf";
//_RUG_DSAI_Fleeing = compile loadfile "RUG_DSAI\DSAI_Fleeing.sqf";
_RUG_DSAI_Wounded = compile loadfile "RUG_DSAI\DSAI_ArabWounded.sqf";
_RUG_DSAI_OwnKilled = compile loadfile "RUG_DSAI\DSAI_ArabOwnKilled.sqf";
_RUG_DSAI_EnemyKilled = compile loadfile "RUG_DSAI\DSAI_ArabEnemyKilled.sqf";
_RUG_DSAI_Misc = compile loadfile "RUG_DSAI\DSAI_ArabMisc.sqf";
_RUG_DSAI_Generic = compile loadfile "RUG_DSAI\DSAI_Generic.sqf";
_RUG_DSAI_Contact = compile loadfile "RUG_DSAI\DSAI_ArabContact.sqf";
_RUG_DSAI_UnderFire = compile loadfile "RUG_DSAI\DSAI_ArabUnderFire.sqf";

_BehaviourCheck=[_RUG_DSAI_Behaviour, _units] execVM "RUG_DSAI\Scripts\DSAI_BehaviourCheck.sqf";
//nil=[_RUG_DSAI_Fleeing, _units] execVM "RUG_DSAI\Scripts\DSAI_FleeingCheck.sqf";
_WoundedCheck=[_RUG_DSAI_Wounded, _units] execVM "RUG_DSAI\Scripts\DSAI_WoundedCheck.sqf";
_OwnKilledCheck=[_RUG_DSAI_OwnKilled, _units] execVM "RUG_DSAI\Scripts\DSAI_OwnKilledCheck.sqf";
_EnemyKilledCheck=[_RUG_DSAI_EnemyKilled, _units] execVM "RUG_DSAI\Scripts\DSAI_EnemyKilledCheck.sqf";
_GenericCheck=[_RUG_DSAI_Generic, _units]execVM "RUG_DSAI\Scripts\DSAI_GenericCheck.sqf";
_ContactCheck=[_RUG_DSAI_Contact, _units] execVM "RUG_DSAI\Scripts\DSAI_ContactCheck.sqf";
_UnderFireCheck=[_RUG_DSAI_UnderFire, _units] execVM "RUG_DSAI\Scripts\DSAI_UnderFireCheck.sqf";

{_x addeventHandler ["fired", {nil=[_this, "DSAI_ArabMisc.sqf"] execVM "RUG_DSAI\Scripts\DSAI_Misc_Fireweapon.sqf";}]} forEach _units;

//Eternal loop that terminates/restarts scripts based on player distance. Note that the two Misc scripts have the TerminalDistance built into their scripts, and will thus automatically end without needing termination.;

_Running = false;
while {true} do 
{

if (((vehicle Player) distance (vehicle (leader (_units select 0)))) < RUG_DSAI_TerminalDistance && !_Running) then
{
//hint "Started!";

_BehaviourCheck=[_RUG_DSAI_Behaviour, _units] execVM "RUG_DSAI\Scripts\DSAI_BehaviourCheck.sqf";
//_FleeingCheck=[_RUG_DSAI_Fleeing, _units] execVM "RUG_DSAI\Scripts\DSAI_FleeingCheck.sqf";
_WoundedCheck=[_RUG_DSAI_Wounded, _units] execVM "RUG_DSAI\Scripts\DSAI_WoundedCheck.sqf";
_OwnKilledCheck=[_RUG_DSAI_OwnKilled, _units] execVM "RUG_DSAI\Scripts\DSAI_OwnKilledCheck.sqf";
_EnemyKilledCheck=[_RUG_DSAI_EnemyKilled, _units] execVM "RUG_DSAI\Scripts\DSAI_EnemyKilledCheck.sqf";
_GenericCheck=[_RUG_DSAI_Generic, _units] execVM "RUG_DSAI\Scripts\DSAI_GenericCheck.sqf";
_ContactCheck=[_RUG_DSAI_Contact, _units] execVM "RUG_DSAI\Scripts\DSAI_ContactCheck.sqf";
_UnderFireCheck=[_RUG_DSAI_UnderFire, _units] execVM "RUG_DSAI\Scripts\DSAI_UnderFireCheck.sqf";

//{nil=[_x, _RUG_DSAI_Misc] execVM "RUG_DSAI\Scripts\DSAI_Misc_Reload.sqf"} forEach _units;
{nil=[_x, _RUG_DSAI_Misc] execVM "RUG_DSAI\Scripts\DSAI_Misc_FormationLeader.sqf"} forEach _units;
_Running = true;
};

if (((vehicle Player) distance (vehicle (leader (_units select 0)))) > RUG_DSAI_TerminalDistance) then
{
//hint "Terminated!";
terminate _BehaviourCheck;
terminate _FleeingCheck;
terminate _WoundedCheck;
terminate _OwnKilledCheck;
terminate _EnemyKilledCheck;
terminate _GenericCheck;
terminate _ContactCheck;
terminate _UnderFireCheck;
//{_x removeEventHandler ["fired", _FireWeaponEH]} forEach _units;
_Running = false;
};

sleep 10;
};