2023-09-12 18:58:10 +00:00
#include "..\script_component.hpp"
2017-12-03 06:22:49 +00:00
/*
2024-08-08 08:01:39 +00:00
* Author: Dystopian, johnb43
2017-12-03 06:22:49 +00:00
* Controls persistent laser state.
*
* Arguments:
* 0: Enabled <BOOL>
*
* Return Value:
* None
*
* Example:
* true call ace_common_fnc_switchPersistentLaser
*
* Public: No
*/
params ["_enabled"];
2024-08-08 08:01:39 +00:00
if (!hasInterface) exitwith {};
// Reset state
{
ACE_player setVariable [QGVAR(laserEnabled_) + str _x, nil];
} forEach [0, 1, 2];
2017-12-03 06:22:49 +00:00
if (!_enabled) exitWith {
if (isNil QGVAR(laserKeyDownEH)) exitWith {};
2024-08-08 08:01:39 +00:00
removeUserActionEventHandler ["headlights", "Activate", GVAR(laserKeyDownEH)];
2018-02-09 21:31:05 +00:00
["loadout", GVAR(laserLoadoutEH)] call CBA_fnc_removePlayerEventHandler;
2017-12-03 06:22:49 +00:00
["turret", GVAR(laserTurretEH)] call CBA_fnc_removePlayerEventHandler;
["vehicle", GVAR(laserVehicleEH)] call CBA_fnc_removePlayerEventHandler;
2018-02-09 21:31:05 +00:00
["weapon", GVAR(laserWeaponEH)] call CBA_fnc_removePlayerEventHandler;
2024-08-08 08:01:39 +00:00
GVAR(laserKeyDownEH) = nil;
GVAR(laserLoadoutEH) = nil;
GVAR(laserTurretEH) = nil;
GVAR(laserVehicleEH) = nil;
GVAR(laserWeaponEH) = nil;
2017-12-03 06:22:49 +00:00
};
2024-08-08 08:01:39 +00:00
private _fnc_getLightLaserState = {
private _currentWeapon = currentWeapon ACE_player;
if (_currentWeapon == "") exitWith {};
// Ignore in vehicle except FFV
if !(ACE_player call CBA_fnc_canUseWeapon) exitWith {};
private _weaponIndex = [ACE_player, _currentWeapon] call FUNC(getWeaponIndex);
if (_weaponIndex == -1) exitWith {};
// Light/laser state only changes in the next frame
2024-08-11 15:08:50 +00:00
// However, as by default changing attachment modes is CTRL + L, the vanilla EH triggers when lights are bound to L (even despite CBA intercepting keystroke)
// Therefore, add an extra frame of delay, after which the previous laser state will have been restored
2024-08-08 08:01:39 +00:00
[{
ACE_player setVariable [
QGVAR(laserEnabled_) + str (_this select 1),
ACE_player isIRLaserOn (_this select 0) || {ACE_player isFlashlightOn (_this select 0)}
];
2024-08-11 15:08:50 +00:00
}, [_currentWeapon, _weaponIndex], 2] call CBA_fnc_execAfterNFrames;
2024-08-08 08:01:39 +00:00
};
// Get current weapon light/laser state
call _fnc_getLightLaserState;
// Update state every time it's changed
GVAR(laserKeyDownEH) = addUserActionEventHandler ["headlights", "Activate", _fnc_getLightLaserState];
2024-08-11 15:08:50 +00:00
// Dropping weapons, as well as switching light/laser attachments turns off lights/lasers
GVAR(lastWeapons) = (getUnitLoadout ACE_player) select [0, 3];
2024-08-08 08:01:39 +00:00
// Monitor weapon addition/removal here
GVAR(laserLoadoutEH) = ["loadout", {
2024-08-11 15:08:50 +00:00
params ["_unit", "_loadout"];
2024-08-08 08:01:39 +00:00
2024-08-11 15:08:50 +00:00
private _weapons = _loadout select [0, 3];
2024-08-08 08:01:39 +00:00
if (_weapons isEqualTo GVAR(lastWeapons)) exitWith {};
GVAR(lastWeapons) = _weapons;
2017-12-03 06:22:49 +00:00
[
2024-08-08 08:01:39 +00:00
_unit,
_unit getVariable [QGVAR(laserEnabled_) + str ([_unit, currentWeapon _unit] call FUNC(getWeaponIndex)), false]
] call FUNC(setWeaponLightLaserState);
}] call CBA_fnc_addPlayerEventHandler;
private _fnc_switchPersistentLaserEH = {
params ["_unit"];
2017-12-03 06:22:49 +00:00
[
2024-08-08 08:01:39 +00:00
_unit,
_unit getVariable [QGVAR(laserEnabled_) + str ([_unit, currentWeapon _unit] call FUNC(getWeaponIndex)), false]
] call FUNC(setWeaponLightLaserState);
2017-12-03 06:22:49 +00:00
};
2024-08-08 08:01:39 +00:00
GVAR(laserTurretEH) = ["turret", _fnc_switchPersistentLaserEH] call CBA_fnc_addPlayerEventHandler;
GVAR(laserVehicleEH) = ["vehicle", _fnc_switchPersistentLaserEH] call CBA_fnc_addPlayerEventHandler;
GVAR(laserWeaponEH) = ["weapon", _fnc_switchPersistentLaserEH] call CBA_fnc_addPlayerEventHandler;