params [
		["_event", [], []],
		["_maximumDistance", 2000, [0]],
		["_minimumAltitude", 0, [0]],
		["_primaryTurret", 0, [0]],
		["_fullAmmoCount", 2000, [0]],
		["_flakRoundsEvery", 2, [0]],
		["_speedDispersion", 20, [0]],
		["_distanceDispersion", 30, [0]]
];

// diag_log "[MILSIM] (ambience) flakEH running";
// diag_log text format["[MILSIM] (ambience) flakEH event: %1", _event];

_unit = _event select 0;
// diag_log text format["[MILSIM] (ambience) flahEH vehicle: %1", _unit];

_projectile = _event select 6;
// diag_log text format["[MILSIM] (ambience) flakEH projectile in flight: %1", _projectile];

deleteVehicle _projectile;
// diag_log text "[MILSIM] (ambience) flakEH projectile deleted");

// diag_log text format["[MILSIM] (ambience) flakEH primary turret: %1", _unit weaponsTurret [0] select _primaryTurret];

_weapon = _unit weaponsTurret [0] select _primaryTurret;

_munitionConversionRate = _fullAmmoCount - _flakRoundsEvery;

// diag_log text format["[MILSIM] (ambience) flakEH munition count: %1", _unit ammo _weapon];
// diag_log text format["[MILSIM] (ambience) flakEH munition replacement at: %1", _munitionConversionRate];

if (_unit ammo _weapon < _munitionConversionRate) then {
	_unit setAmmo [_weapon, _fullAmmoCount];
	// diag_log text format["[MILSIM] (ambience) flakEH replacing ammo count to: %1", _fullAmmoCount];

	_targetPosition = [];

	_target = 0;

	if (isPlayer (assignedGunner _unit)) then {

		_target = cursorTarget;

		if (_unit distance _target < _maximumDistance) then {
			_targetPosition = getPos _target;
		};

	} else {
		_possibleTargets = _unit nearTargets _maximumDistance;
		diag_log text format["ai has %1 possible targetting solutions", count _possibleTargets];

		if ((count _possibleTargets) > 0) then {

			_i = 0;
			_hold = 0;

			{	
				_i = _unit aimedAtTarget [_x select 4, _weapon];
				if (_i > _hold && (_x select 3) > 0) then {
					_target = _x select 4;
					diag_log text format["setting target to %1", _target];
					_targetPosition = _x select 0;
					_hold = _i;
				};
			} forEach _possibleTargets;

		};

	};
	
	// diag_log text format["[MILSIM] (ambience) flakEH target coordinates: %1", _targetPosition];

	if ((count _targetPosition) > 0) then {
		// diag_log text "[MILSIM] (ambience) flakEH calculating new projectile placement";

		_targetX = _targetPosition select 0;
		_targetY = _targetPosition select 1;
		_targetZ = _targetPosition select 2;

		// diag_log text format["[MILSIM] (ambience) flakEH checking target altitude: %1", _targetZ];

		if (_targetZ > _minimumAltitude) then {
			// diag_log text "[MILSIM] (ambience) flakEH target is above minimum height, proceeding";

			if !(lineIntersects [getPos _unit, _targetPosition, _unit, _target]) then {
			
				// diag_log text "[MILSIM] (ambience) flakEH intersection calculated";

				_flakDistance = ((speed _target * 0.8) * (_speedDispersion / 100)) + ((_unit distance _target) * (_distanceDispersion / 500));

				_distanceX = ((random (_flakDistance * 2)) - _flakDistance) + _targetX;
				_distanceY = ((random (_flakDistance * 2)) - _flakDistance) + _targetY;
				_distanceZ = ((random (_flakDistance * 2)) - _flakDistance) + _targetZ;
				// diag_log text format["[MILSIM] (ambience) flakEH target is distance: %1 / distance2D %2 from me, creating munition distance %3, distance2D %4 from me",  _unit distance _targetPosition, _unit distance2D _targetPosition, _unit distance [_distanceX, _distanceY, _distanceZ], _unit distance2D [_distanceX, _distanceY, _distanceZ]];
				_flak = createVehicle ["SmallSecondary", [_distanceX, _distanceY, _distanceZ], [], 0, "CAN_COLLIDE"];

			};
		};
	};
};