Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Player Events - Add debug logging & make VisionModeChanged use engine event #1671

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions addons/events/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class CfgFunctions {
class CBA {
class Events {
PATHTO_FNC(addBISEventHandler);
PATHTO_FNC(addBISPlayerEventHandler);
PATHTO_FNC(addPlayerEventHandler);
PATHTO_FNC(removePlayerEventHandler);
PATHTO_FNC(addDisplayHandler);
Expand All @@ -13,6 +14,7 @@ class CfgFunctions {
PATHTO_FNC(removeKeyHandler);
PATHTO_FNC(addEventHandler);
PATHTO_FNC(addEventHandlerArgs);
PATHTO_FNC(removeBISPlayerEventHandler);
PATHTO_FNC(removeEventHandler);
PATHTO_FNC(localEvent);
PATHTO_FNC(globalEvent);
Expand Down
79 changes: 79 additions & 0 deletions addons/events/fnc_addBISPlayerEventHandler.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_addBISPlayerEventHandler

Description:
Adds an engine event handler just to the controlled entity.

Parameters:
_key - Unique identifier for the event. <STRING>
_eventType - Type of event to add. Can be any event supported by addEventHandler. <STRING>
_eventCode - Code to run when event is raised. <CODE>
_ignoreVirtual - Ignore virtual units (spectators, virtual zeus, UAV RC) [optional] (default: true) <BOOLEAN>

Returns:
Event was added <BOOLEAN>

Examples:
(begin example)
["TAG_MyFiredNearEvent", "FiredNear", {systemChat str _this}] call CBA_fnc_addBISPlayerEventHandler
(end)

Author:
PabstMirror
---------------------------------------------------------------------------- */
SCRIPT(addBISPlayerEventHandler);

if (canSuspend) exitWith {
[CBA_fnc_addBISPlayerEventHandler, _this] call CBA_fnc_directCall;
};

params [
["_key", "", [""]],
["_type", "", [""]],
["_code", {}, [{}]],
["_ignoreVirtual", true, [true]]
];

if (isNil QGVAR(playerEventsHash)) then { // first-run init
GVAR(playerEventsHash) = createHashMap;
["unit", {
params ["_newPlayer", "_oldPlayer"];

// uav check only applies to direct controlling UAVs from zeus, no effect on normal UAV operation
private _isVirtual = (unitIsUAV _newPlayer) || {getNumber (configOf _newPlayer >> "isPlayableLogic") == 1};

TRACE_4("",_newPlayer,_oldPlayer,_isVirtual,count GVAR(playerEventsHash));
{
_y params ["_type", "_code", "_ignoreVirtual"];

private _oldEH = _oldPlayer getVariable [_x, -1];
if (_oldEH != -1) then {
_oldPlayer removeEventHandler [_type, _oldEH];
_oldPlayer setVariable [_x, nil];
};

_oldEH = _newPlayer getVariable [_x, -1];
if (_oldEH != -1) then { continue }; // if respawned then var and EH already exists
if (_ignoreVirtual && _isVirtual) then { continue };

private _newEH = _newPlayer addEventHandler [_type, _code];
_newPlayer setVariable [_x, _newEH];
} forEach GVAR(playerEventsHash);
}, false] call CBA_fnc_addPlayerEventHandler;
};


_key = format [QGVAR(playerEvents_%1), toLowerANSI _key];
if (_key in GVAR(playerEventsHash)) exitWith { ERROR_1("bad key %1",_this); false};

GVAR(playerEventsHash) set [_key, [_type, _code, _ignoreVirtual]];

private _player = call CBA_fnc_currentUnit;
if (_ignoreVirtual && {(unitIsUAV _player) || {getNumber (configOf _player >> "isPlayableLogic") == 1}}) exitWith {};

// Add event now
private _newEH = _player addEventHandler [_type, _code];
_player setVariable [_key, _newEH];

true // return
8 changes: 8 additions & 0 deletions addons/events/fnc_addPlayerEventHandler.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ if (_id != -1) then {

params ["_data"]; // visibleMap is updated one frame later
if (_data isNotEqualTo GVAR(oldVisibleMap)) then {
LOG_2("visibleMap playerEvent - new: %1 - old: %2",_data,GVAR(oldVisibleMap));
GVAR(oldVisibleMap) = _data;
[QGVAR(visibleMapEvent), [call CBA_fnc_currentUnit, _data]] call CBA_fnc_localEvent;
};
Expand All @@ -181,6 +182,7 @@ if (_id != -1) then {
{
private _data = visibleMap;
if (_data isNotEqualTo GVAR(oldVisibleMap)) then {
LOG_2("visibleMap playerEvent - new: %1 - old: %2",_data,GVAR(oldVisibleMap));
GVAR(oldVisibleMap) = _data;
[QGVAR(visibleMapEvent), [call CBA_fnc_currentUnit, _data]] call CBA_fnc_localEvent;
};
Expand All @@ -192,11 +194,17 @@ if (_id != -1) then {

private _data = call CBA_fnc_getActiveFeatureCamera;
if (_data isNotEqualTo GVAR(oldFeatureCamera)) then {
LOG_2("featureCamera playerEvent - new: %1 - old: %2",_data,GVAR(oldFeatureCamera));
GVAR(oldFeatureCamera) = _data;
[QGVAR(featureCameraEvent), [call CBA_fnc_currentUnit, _data]] call CBA_fnc_localEvent;
};
}, 0.5] call CBA_fnc_addPerFrameHandler);

["CBA_visionModeChanged", "VisionModeChanged", {
params ["", "_newVisionMode", "", "_oldVisionMode"];
LOG_2("visionMode playerEvent - new: %1 - old: %2",_newVisionMode,_oldVisionMode);
[QGVAR(visionModeEvent), [focusOn, _newVisionMode, _oldVisionMode]] call CBA_fnc_localEvent;
}, false] call CBA_fnc_addBISPlayerEventHandler;
};

GVAR(playerEHInfo) pushBack [_type, _id];
Expand Down
33 changes: 13 additions & 20 deletions addons/events/fnc_playerEvent.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,14 @@ SCRIPT(playerEvent);
private _unit = missionNamespace getVariable ["bis_fnc_moduleRemoteControl_unit", player];
private _vehicle = vehicle _unit;

private _controlledEntity = _unit;
if (!isNull getConnectedUAV _unit) then {
private _uavControl = UAVControl getConnectedUAV _unit;
_uavControl = _uavControl param [(_uavControl find _unit) + 1, ""]; // Will be position STRING if actively controlling, or OBJECT if not.
if (_uavControl isEqualTo "DRIVER") exitWith {
_controlledEntity = driver getConnectedUAV _unit;
};

if (_uavControl isEqualTo "GUNNER") exitWith {
_controlledEntity = gunner getConnectedUAV _unit;
};
};

// Unlike CBA_fnc_turretPath, this will return [-1] when player is driver
private _turret = _vehicle unitTurret _unit;

private _state = [
_unit, group _unit, leader _unit,
currentWeapon _unit, currentMuzzle _unit, currentWeaponMode _unit,
getUnitLoadout _unit, _vehicle, _turret, _vehicle currentWeaponTurret _turret,
currentVisionMode _controlledEntity, cameraView
cameraView
];

if (_state isNotEqualTo GVAR(oldState)) then {
Expand All @@ -54,7 +41,7 @@ if (_state isNotEqualTo GVAR(oldState)) then {
"", "_newGroup", "_newLeader",
"_newWeapon", "_newMuzzle", "_newWeaponMode",
"_newLoadout", "", "", "_newTurretWeapon",
"_newVisionMode", "_newCameraView"
"_newCameraView"
];

// These events should fire if the context of the state has changed.
Expand All @@ -64,36 +51,43 @@ if (_state isNotEqualTo GVAR(oldState)) then {
_newWeaponMode = [_unit, _newMuzzle, _newWeaponMode];

if (_unit isNotEqualTo GVAR(oldUnit)) then {
LOG_2("unit playerEvent - new: %1 - old: %2",_unit,GVAR(oldUnit));
[QGVAR(unitEvent), [_unit, GVAR(oldUnit)]] call CBA_fnc_localEvent;
GVAR(oldUnit) = _unit;
};

if (_newGroup isNotEqualTo GVAR(oldGroup)) then {
LOG_2("group playerEvent - new: %1 - old: %2",_newGroup,GVAR(oldGroup));
[QGVAR(groupEvent), [_unit, GVAR(oldGroup), _newGroup]] call CBA_fnc_localEvent; // intentionally reversed order for backwards compatiblity
GVAR(oldGroup) = _newGroup;
};

if (_newLeader isNotEqualTo GVAR(oldLeader)) then {
LOG_2("leader playerEvent - new: %1 - old: %2",_newLeader,GVAR(oldLeader));
[QGVAR(leaderEvent), [_unit, GVAR(oldLeader), _newLeader]] call CBA_fnc_localEvent; // intentionally reversed order for backwards compatiblity
GVAR(oldLeader) = _newLeader;
};

if (_newWeapon isNotEqualTo GVAR(oldWeapon)) then {
LOG_2("weapon playerEvent - new: %1 - old: %2",_newWeapon,GVAR(oldWeapon));
[QGVAR(weaponEvent), [_unit, _newWeapon, GVAR(oldWeapon)]] call CBA_fnc_localEvent;
GVAR(oldWeapon) = _newWeapon;
};

if (_newTurretWeapon isNotEqualTo GVAR(oldTurretWeapon)) then {
LOG_2("turretWeapon playerEvent - new: %1 - old: %2",_newTurretWeapon,GVAR(oldTurretWeapon));
[QGVAR(turretWeaponEvent), [_unit, _newTurretWeapon, GVAR(oldTurretWeapon)]] call CBA_fnc_localEvent;
GVAR(oldTurretWeapon) = _newTurretWeapon;
};

if (_newMuzzle isNotEqualTo GVAR(oldMuzzle)) then {
LOG_2("muzzle playerEvent - new: %1 - old: %2",_newMuzzle select 2,GVAR(oldMuzzle) select 2);
[QGVAR(muzzleEvent), [_unit, _newMuzzle select 2, GVAR(oldMuzzle) select 2]] call CBA_fnc_localEvent;
GVAR(oldMuzzle) = _newMuzzle;
};

if (_newWeaponMode isNotEqualTo GVAR(oldWeaponMode)) then {
LOG_2("group playerEvent - new: %1 - old: %2",_newWeaponMode select 2,GVAR(oldWeaponMode) select 2);
[QGVAR(weaponModeEvent), [_unit, _newWeaponMode select 2, GVAR(oldWeaponMode) select 2]] call CBA_fnc_localEvent;
GVAR(oldWeaponMode) = _newWeaponMode;
};
Expand All @@ -111,6 +105,7 @@ if (_state isNotEqualTo GVAR(oldState)) then {
} forEach [primaryWeaponMagazine _unit, secondaryWeaponMagazine _unit, handgunMagazine _unit];

if (_newLoadoutNoAmmo isNotEqualTo GVAR(oldLoadoutNoAmmo)) then {
LOG_2("loadout playerEvent - new: %1 - old: %2",_newLoadout,GVAR(oldLoadout));
[QGVAR(loadoutEvent), [_unit, _newLoadout, GVAR(oldLoadout)]] call CBA_fnc_localEvent;
GVAR(oldLoadoutNoAmmo) = _newLoadoutNoAmmo;
};
Expand All @@ -119,21 +114,19 @@ if (_state isNotEqualTo GVAR(oldState)) then {
};

if (_vehicle isNotEqualTo GVAR(oldVehicle)) then {
LOG_2("vehicle playerEvent - new: %1 - old: %2",_newVehicle,GVAR(oldVehicle));
[QGVAR(vehicleEvent), [_unit, _vehicle, GVAR(oldVehicle)]] call CBA_fnc_localEvent;
GVAR(oldVehicle) = _vehicle;
};

if (_turret isNotEqualTo GVAR(oldTurret)) then {
LOG_2("turret playerEvent - new: %1 - old: %2",_newTurret,GVAR(oldTurret));
[QGVAR(turretEvent), [_unit, _turret, GVAR(oldTurret)]] call CBA_fnc_localEvent;
GVAR(oldTurret) = _turret;
};

if (_newVisionMode isNotEqualTo GVAR(oldVisionMode)) then {
[QGVAR(visionModeEvent), [_unit, _newVisionMode, GVAR(oldVisionMode)]] call CBA_fnc_localEvent;
GVAR(oldVisionMode) = _newVisionMode;
};

if (_newCameraView isNotEqualTo GVAR(oldCameraView)) then {
LOG_2("cameraView playerEvent - new: %1 - old: %2",_newCameraView,GVAR(oldCameraView));
[QGVAR(cameraViewEvent), [_unit, _newCameraView, GVAR(oldCameraView)]] call CBA_fnc_localEvent;
GVAR(oldCameraView) = _newCameraView; // This assignment may be returned.
};
Expand Down
46 changes: 46 additions & 0 deletions addons/events/fnc_removeBISPlayerEventHandler.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_removeBISPlayerEventHandler

Description:
Remove event handlers added via CBA_fnc_addBISPlayerEventHandler.

Parameters:
_key - Unique identifier for the event. <STRING>

Returns:
Event was removed <BOOLEAN>

Examples:
(begin example)
["TAG_MyFiredNearEvent"] call CBA_fnc_removeBISPlayerEventHandler
(end)

Author:
LinkIsGrim
---------------------------------------------------------------------------- */
SCRIPT(removeBISPlayerEventHandler);

if (canSuspend) exitWith {
[CBA_fnc_removeBISPlayerEventHandler, _this] call CBA_fnc_directCall;
};

params [
["_key", "", [""]]
];

if (isNil QGVAR(playerEventsHash)) exitWith {false};

_key = format [QGVAR(playerEvents_%1), toLowerANSI _key];
if !(_key in GVAR(playerEventsHash)) exitWith {false};

(GVAR(playerEventsHash) deleteAt _key) params ["_type", "", "_ignoreVirtual"];

private _player = call CBA_fnc_currentUnit;
private _ehIdx = _player getVariable [_key, -1]; // don't touch any events if it wasn't one we added
if (_ehIdx != -1) then {
_player removeEventHandler [_type, _ehIdx];
_player setVariable [_key, nil];
};

true // return
2 changes: 1 addition & 1 deletion addons/main/script_mod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define VERSION_AR MAJOR,MINOR,PATCHLVL,BUILD

// MINIMAL required version for the Mod. Components can specify others..
#define REQUIRED_VERSION 2.14
#define REQUIRED_VERSION 2.16

/*
// Defined DEBUG_MODE_NORMAL in a few CBA_fncs to prevent looped logging :)
Expand Down