diff --git a/addons/laserpointer/XEH_PREP.hpp b/addons/laserpointer/XEH_PREP.hpp index c5dcd74a75..3237967007 100644 --- a/addons/laserpointer/XEH_PREP.hpp +++ b/addons/laserpointer/XEH_PREP.hpp @@ -1,4 +1,5 @@ PREP(drawLaserpoint); +PREP(getNearUnits); PREP(onDraw); PREP(switchLaserLightMode); diff --git a/addons/laserpointer/XEH_postInit.sqf b/addons/laserpointer/XEH_postInit.sqf index bfc617965d..f80a4494e0 100644 --- a/addons/laserpointer/XEH_postInit.sqf +++ b/addons/laserpointer/XEH_postInit.sqf @@ -7,32 +7,75 @@ if (!hasInterface) exitWith {}; GVAR(nearUnits) = []; +GVAR(index) = -1; +GVAR(laserClassesCache) = [] call CBA_fnc_createNamespace; +GVAR(redLaserUnits) = []; +GVAR(greenLaserUnits) = []; #include "initKeybinds.sqf" ["ace_settingsInitialized", { - //If not enabled, dont't add draw eventhandler or PFEH (for performance) - if (!GVAR(enabled)) exitWith {}; + // if not enabled, dont't add draw eventhandler or PFEH (for performance) + if !(GVAR(enabled)) exitWith {}; - // @todo. Maybe move to common? [{ - // handle RHS / bugged vehicle slots - private _camPosAGL = positionCameraToWorld [0,0,0]; - if !((_camPosAGL select 0) isEqualType 0) exitWith {}; + private _oldNearUnits = GVAR(nearUnits); + GVAR(nearUnits) = call FUNC(getNearUnits); - private _nearUnits = []; + // remove units that moved away { - _nearUnits append crew _x; - if (count _nearUnits > 10) exitWith { - _nearUnits resize 10; + GVAR(redLaserUnits) deleteAt (GVAR(redLaserUnits) find _x); + GVAR(greenLaserUnits) deleteAt (GVAR(greenLaserUnits) find _x); + } forEach (_oldNearUnits - GVAR(nearUnits)); + }, 5, []] call CBA_fnc_addPerFrameHandler; + + + private _fnc_processUnit = { + params ["_unit"]; + + private _weapon = currentWeapon _unit; + private _laser = [(_unit weaponAccessories _weapon) select 1] param [0, ""]; + + if (_laser isEqualTo "") exitWith {}; + + private _laserID = GVAR(laserClassesCache) getVariable _laser; + + if (isNil "_laserID") then { + _laserID = getNumber (configFile >> "CfgWeapons" >> _laser >> "ACE_laserpointer"); + GVAR(laserClassesCache) setVariable [_laser, _laserID]; + }; + + if (_unit isFlashlightOn _weapon) then { + if (_laserID isEqualTo 1) exitWith { + GVAR(redLaserUnits) pushBackUnique _unit; + GVAR(greenLaserUnits) deleteAt (GVAR(greenLaserUnits) find _unit); }; - } forEach nearestObjects [_camPosAGL, ["AllVehicles"], 50]; // when moving this, search also for units inside vehicles. currently breaks the laser in FFV - GVAR(nearUnits) = _nearUnits; + if (_laserID isEqualTo 2) exitWith { + GVAR(greenLaserUnits) pushBackUnique _unit; + GVAR(redLaserUnits) deleteAt (GVAR(redLaserUnits) find _unit); + }; + } else { + GVAR(redLaserUnits) deleteAt (GVAR(redLaserUnits) find _unit); + GVAR(greenLaserUnits) deleteAt (GVAR(greenLaserUnits) find _unit); + }; + }; - } , 5, []] call CBA_fnc_addPerFrameHandler; + // custom scheduler + [{ + params ["_fnc_processUnit"]; - addMissionEventHandler ["Draw3D", { - call FUNC(onDraw); - }]; + ACE_player call _fnc_processUnit; + + GVAR(index) = GVAR(index) + 1; + private _unit = GVAR(nearUnits) param [GVAR(index), objNull]; + + if (isNull _unit) exitWith { + GVAR(index) = -1; + }; + + _unit call _fnc_processUnit; + }, 0.1, _fnc_processUnit] call CBA_fnc_addPerFrameHandler; + + addMissionEventHandler ["Draw3D", FUNC(onDraw)]; }] call CBA_fnc_addEventHandler; diff --git a/addons/laserpointer/XEH_preInit.sqf b/addons/laserpointer/XEH_preInit.sqf index b47cf6628d..d2efe43e9c 100644 --- a/addons/laserpointer/XEH_preInit.sqf +++ b/addons/laserpointer/XEH_preInit.sqf @@ -6,4 +6,11 @@ PREP_RECOMPILE_START; #include "XEH_PREP.hpp" PREP_RECOMPILE_END; +["visionMode", { + params ["", "_visionMode"]; + + GVAR(isIR) = _visionMode isEqualTo 1; + GVAR(isTI) = _visionMode isEqualTo 2; +}] call CBA_fnc_addPlayerEventHandler; + ADDON = true; diff --git a/addons/laserpointer/functions/fnc_getNearUnits.sqf b/addons/laserpointer/functions/fnc_getNearUnits.sqf new file mode 100644 index 0000000000..6f31a99a2a --- /dev/null +++ b/addons/laserpointer/functions/fnc_getNearUnits.sqf @@ -0,0 +1,26 @@ +/* + * Author: commy2 + * Reports near units. + * + * Arguments: + * None + * + * Return Value: + * None + * + * Public: No + */ +#include "script_component.hpp" + +private _camPosAGL = positionCameraToWorld [0, 0, 0]; + +// handle RHS / bugged vehicle slots +if !((_camPosAGL select 0) isEqualType 0) exitWith {}; + +private _nearUnits = []; + +{ + _nearUnits append crew _x; +} forEach nearestObjects [_camPosAGL, ["AllVehicles"], MAX_LASER_RANGE]; + +_nearUnits diff --git a/addons/laserpointer/functions/fnc_onDraw.sqf b/addons/laserpointer/functions/fnc_onDraw.sqf index 1945dcf2b7..0559386827 100644 --- a/addons/laserpointer/functions/fnc_onDraw.sqf +++ b/addons/laserpointer/functions/fnc_onDraw.sqf @@ -12,30 +12,17 @@ */ #include "script_component.hpp" -private _isIR = currentVisionMode ACE_player; +// no lasers in thermal mode +if !(GVAR(isTI)) then { + private _brightness = 2 - call EFUNC(common,ambientBrightness); -if (_isIR == 2) exitWith {}; + { + // red laser. draw green dot anyway in IR mode + [_x, 100, GVAR(isIR), _brightness] call FUNC(drawLaserpoint); + } count GVAR(redLaserUnits); -_isIR = _isIR == 1; - -private _brightness = 2 - call EFUNC(common,ambientBrightness); - -{ - private _weapon = currentWeapon _x; - private _laser = (_x weaponAccessories _weapon) select 1; - - if (_laser != "") then { - private _cacheName = format [QGVAR(laser_%1), _laser]; - private _laserID = missionNamespace getVariable [_cacheName, -1]; - - if (missionNamespace getVariable [_cacheName, -1] == -1) then { - _laserID = getNumber (configFile >> "CfgWeapons" >> _laser >> "ACE_laserpointer"); - missionNamespace setVariable [_cacheName, _laserID]; - }; - - if (_laserID > 0 && {_x isFlashlightOn _weapon}) then { - [_x, 100, (_laserID == 2 || _isIR), _brightness] call FUNC(drawLaserpoint); - }; - }; - false -} count GVAR(nearUnits); + { + // green laser + [_x, 100, true, _brightness] call FUNC(drawLaserpoint); + } count GVAR(greenLaserUnits); +}; diff --git a/addons/laserpointer/script_component.hpp b/addons/laserpointer/script_component.hpp index b6323308fd..4b7b2e0d39 100644 --- a/addons/laserpointer/script_component.hpp +++ b/addons/laserpointer/script_component.hpp @@ -15,3 +15,5 @@ #endif #include "\z\ace\addons\main\script_macros.hpp" + +#define MAX_LASER_RANGE 50