Optimize Laserpointer (#4859)

* only process one unit every 0.1 seconds

* exitWith to keep indentation low

* use event to determine isIR and isTI

* correctly remove units with turned on lights from their arrays

* handle weaponAccessories command reporting nil

Conflicts:
	addons/laserpointer/XEH_postInit.sqf
This commit is contained in:
commy2 2017-02-10 19:47:08 +01:00 committed by Glowbal
parent e8418f6df0
commit bd7c8ce409
6 changed files with 106 additions and 38 deletions

View File

@ -1,4 +1,5 @@
PREP(drawLaserpoint);
PREP(getNearUnits);
PREP(onDraw);
PREP(switchLaserLightMode);

View File

@ -7,30 +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"];
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", {call FUNC(onDraw)}];
}] call CBA_fnc_addEventHandler;

View File

@ -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;

View File

@ -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

View File

@ -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);
};

View File

@ -15,3 +15,5 @@
#endif
#include "\z\ace\addons\main\script_macros.hpp"
#define MAX_LASER_RANGE 50