PHP Code:
//UNITS IN PLAYER'S GROUP WILL AUTOMATICALLY KNEEL/CROUCH AND SHOOT IF THEY HAVE LINE OF SIGHT WITH NEAR ENEMIES
// Needs beta 93666 or greater
// Line of sight stuff adapted from SaOk
// Azimuth stuff adapted from CarlGustaffa
// TPW 20120617
private ["_unit","_near","_nearunits","_a","_b","_dirTo","_eyeD","_eyePb","_eyePa","_eyeDV","_ang","_tint","_lint","_vm","_dist","_esp","_usp","_camo","_ka","_formula"];
//Start hint
sleep 1;
hintsilent "TPW LOS active";
//Angle of sun - adapted from CarlGustaffa
[] spawn {
private ["_lat","_day","_hour"];
while {true} do
{
_lat = -1 * getNumber(configFile >> "CfgWorlds" >> worldName >> "latitude");
_day = 360 * (dateToNumber date);
_hour = (daytime / 24) * 360;
tpw_los_sunangle = round (((12 * cos(_day) - 78) * cos(_lat) * cos(_hour)) - (24 * sin(_lat) * cos(_day)));
sleep 300;
};
};
//Main function
tpw_los =
{
{
// remove this line for playersquad only
_side = side _x; if ((_side == west) || (_side == east) || (_side == resistance))then {
_unit = _x;
_vm = (currentVisionMode _unit); if (_vm == 1) then {_vm = -1} else {_vm = 1};
switch (side _unit) do {
case east: {tpw_enemyside = west};
case west: {tpw_enemyside = east};
case resistance: {tpw_enemyside = east};
};
_nextTime = _unit getVariable ["NextTime", -1];
if(_nextTime == -1) then {_unit setVariable ["NextTime", diag_tickTime + random 1];};
if(diag_tickTime >= _nextTime) then
{
_unit setunitpos "AUTO";
_unit setVariable ["NextTime", diag_tickTime + random 1];
_nearunits = (getposatl _unit) nearentities [["man","car"],100];
{
_near = _x;
tpw_los_cansee = 0;
if (side _near == tpw_enemyside) then
{
//Line of sight stuff adapted from SaOk
_a = _unit;
_b = _near;
_eyeDV = eyeDirection _a;
_eyeD = ((_eyeDV select 0) atan2 (_eyeDV select 1));
_dirTo = ([_b, _a] call BIS_fnc_dirTo);
_ang = abs (_dirTo - _eyeD);
_eyePa = eyePos _a;
_eyePb = eyePos _b;
_tInt = terrainIntersect [_eyePa, _eyePb];
_lInt = lineIntersects [_eyePa, _eyePb];
if (((_ang > 120) && (_ang < 240)) && !(_lInt) && !(_tInt)) then
{
//Other factors affecting visibility of enemy
_ka = _unit knowsabout _near;
_dist = (_unit distance _near);
_esp = abs (speed _near);
_usp = abs (speed _unit);
_camo = getnumber (configfile >> "CfgVehicles" >> (typeof _near) >> "Camouflage");
//Magic visibility formula
_formula = (_vm * (tpw_los_sunangle)) + (_esp * 6) - (_usp * 2) - _dist + random 40;
if (_dist < 25) then {_formula = 200};
if (_camo > 0.5) then {tpw_los_cansee = _formula};
};
};
if (tpw_los_cansee > 0) exitwith
{
_unit setunitpos "MIDDLE";
_unit lookat _near;
_unit dofire _near;
//Debugging
//hint format ["%1 \n%2 \n%3m \n%4\n%5",(name _unit),(typeof _near),round (_unit distance _near),round tpw_los_cansee];
};
} foreach _nearunits;
};
;
//} foreach units group player; // player's squad
};}forEach allUnits; // Everyone
};
[tpw_los,0.5] call cba_fnc_addPerFrameHandler;