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("flakEH running"); // diag_log(_event); _unit = _event select 0; // diag_log(format["i have a unit: %1", _unit]); _projectile = _event select 6; // diag_log(format["i have a projectile in flight: %1", _projectile]); deleteVehicle _projectile; // diag_log("i have deleted the projectile"); // diag_log(_unit weaponsTurret [0] select _primaryTurret); _weapon = _unit weaponsTurret [0] select _primaryTurret; _munitionConversionRate = _fullAmmoCount - _flakRoundsEvery; // diag_log(format["munition count: %1", _unit ammo _weapon]); // diag_log(format["munition replacement at: %1", _munitionConversionRate]); if (_unit ammo _weapon < _munitionConversionRate) then { _unit setAmmo [_weapon, _fullAmmoCount]; // diag_log(format["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(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(format["setting target to %1", _target]); _targetPosition = _x select 0; _hold = _i; }; } forEach _possibleTargets; }; }; // diag_log(format["i have found target coordinates: %1", _targetPosition]); if ((count _targetPosition) > 0) then { // diag_log("calculating new projectile placement"); _targetX = _targetPosition select 0; _targetY = _targetPosition select 1; _targetZ = _targetPosition select 2; // diag_log(format["checking target altitude: %1", _targetZ]); if (_targetZ > _minimumAltitude) then { // diag_log("target is above minimum height, proceeding"); if !(lineIntersects [getPos _unit, _targetPosition, _unit, _target]) then { // diag_log("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( format["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"]; }; }; }; };