SaOK, that's a really excellent implementation of the new lineintersects function.
I tried your formula in a very small script which just makes AI units in the player's group kneel/crouch if they have line of sight of an enemy less than 150m away. Seems to work pretty well and doesn't cripple the computer.
Uncommenting this line: //0 = _unit fireAtTarget [_enemy]; , and decreasing the sleep =10 to sleep=1 or less, might also help in CQB as per maturin's suggestion
PHP Code:
//UNITS IN PLAYER"S GROUP WILL AUTOMATICALLY KNEEL/CROUCH IF THEY HAVE LINE OF SIGHT OF NEAR ENEMIES
// Original idea by SaOK
//Enemies within 150m of unit
_nearenemies =
{
_unit = _this select 0;
_nearunits = nearestobjects [_unit,["man"],150];
{
if (side _x == east) then {[_unit, _x] call _los};
} foreach _nearunits;
};
//Does unit have line of sight with enemy?
_los =
{
_unit = _this select 0;
_enemy = _this select 1;
if (((abs(([_unit, _enemy] call BIS_fnc_dirTo) - direction _unit)) <= 90 || (abs(([_unit, _enemy] call BIS_fnc_dirTo) - direction _unit)) >= 270) && !(lineIntersects [[getposASL _enemy select 0, getposASL _enemy select 1,(getposASL _enemy select 2)+((_enemy selectionPosition "pilot") select 2)],[getposASL _unit select 0, getposASL _unit select 1,(getposASL _unit select 2)+((_unit selectionPosition "pilot") select 2)]])) then
{
_unit setunitpos "MIDDLE";
//0 = _unit fireAtTarget [_enemy];
};
};
//Main loop
while {true} do
{
{
_x setunitpos "UP";
[_x] call _nearenemies;
} foreach units group player;
sleep 10;
};