-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathfnc_addPlayerEventHandler.sqf
213 lines (192 loc) · 8.38 KB
/
fnc_addPlayerEventHandler.sqf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "script_component.hpp"
/* ----------------------------------------------------------------------------
Function: CBA_fnc_addPlayerEventHandler
Description:
Adds a player event handler.
Possible events:
"unit" - player controlled unit changed
"weapon" - currently selected weapon change
"turretweapon" - currently selected turret weapon change
"muzzle" - currently selected muzzle change
"weaponMode" - currently selected weapon mode change
"loadout" - players loadout changed
"vehicle" - players current vehicle changed
"turret" - position in vehicle changed
"visionMode" - player changed to normal/night/thermal view
"cameraView" - camera mode changed ("Internal", "External", "Gunner" etc.)
"featureCamera" - camera changed (Curator, Arsenal, Spectator etc.)
"visibleMap" - opened or closed map
"group" - player group changes
"leader" - leader of player changes
Parameters:
_type - Event handler type. <STRING>
_function - Function to add to event. <CODE>
_applyRetroactively - Call function immediately if player is defined already (optional, default: false) <BOOL>
Returns:
_id - The ID of the event handler. <NUMBER>
Examples:
(begin example)
_id = ["unit", {systemChat str _this}] call CBA_fnc_addPlayerEventHandler;
(end)
Author:
commy2
---------------------------------------------------------------------------- */
SCRIPT(addPlayerEventHandler);
if (!hasInterface) exitWith {-1};
params [["_type", "", [""]], ["_function", {}, [{}]], ["_applyRetroactively", false, [false]]];
_type = toLower _type;
// Skip retroactive execution if no current unit.
if (_applyRetroactively && {isNull (missionNamespace getVariable [QGVAR(oldUnit), objNull])}) then {
_applyRetroactively = false;
};
private _id = switch (_type) do {
case "unit": {
if (_applyRetroactively) then {
[GVAR(oldUnit), objNull] call _function;
};
[QGVAR(unitEvent), _function] call CBA_fnc_addEventHandler // return id
};
case "weapon": {
if (_applyRetroactively) then {
[GVAR(oldUnit), currentWeapon GVAR(oldUnit), ""] call _function;
};
[QGVAR(weaponEvent), _function] call CBA_fnc_addEventHandler // return id
};
case "turretweapon": {
if (_applyRetroactively) then {
private _vehicle = vehicle GVAR(oldUnit);
private _turret = _vehicle unitTurret GVAR(oldUnit);
[GVAR(oldUnit), _vehicle currentWeaponTurret _turret, ""] call _function;
};
[QGVAR(turretWeaponEvent), _function] call CBA_fnc_addEventHandler // return id
};
case "muzzle": {
if (_applyRetroactively) then {
[GVAR(oldUnit), currentMuzzle GVAR(oldUnit), ""] call _function;
};
[QGVAR(muzzleEvent), _function] call CBA_fnc_addEventHandler // return id
};
case "weaponmode": {
if (_applyRetroactively) then {
[GVAR(oldUnit), currentWeaponMode GVAR(oldUnit), ""] call _function;
};
[QGVAR(weaponModeEvent), _function] call CBA_fnc_addEventHandler // return id
};
case "loadout": {
if (_applyRetroactively) then {
[GVAR(oldUnit), getUnitLoadout GVAR(oldUnit), []] call _function;
};
[QGVAR(loadoutEvent), _function] call CBA_fnc_addEventHandler // return id
};
case "vehicle": {
if (_applyRetroactively) then {
[GVAR(oldUnit), vehicle GVAR(oldUnit), objNull] call _function;
};
[QGVAR(vehicleEvent), _function] call CBA_fnc_addEventHandler // return id
};
case "turret": {
if (_applyRetroactively) then {
private _vehicle = vehicle GVAR(oldUnit);
private _turret = _vehicle unitTurret GVAR(oldUnit);
[GVAR(oldUnit), _turret, []] call _function;
};
[QGVAR(turretEvent), _function] call CBA_fnc_addEventHandler // return id
};
case "visionmode": {
if (_applyRetroactively) then {
[GVAR(oldUnit), currentVisionMode GVAR(oldUnit), -1] call _function;
};
[QGVAR(visionModeEvent), _function] call CBA_fnc_addEventHandler // return id
};
case "cameraview": {
if (_applyRetroactively) then {
[GVAR(oldUnit), cameraView, ""] call _function;
};
[QGVAR(cameraViewEvent), _function] call CBA_fnc_addEventHandler // return id
};
case "featurecamera": {
if (_applyRetroactively) then {
[GVAR(oldUnit), call CBA_fnc_getActiveFeatureCamera] call _function;
};
[QGVAR(featureCameraEvent), _function] call CBA_fnc_addEventHandler // return id
};
case "visiblemap": {
if (_applyRetroactively) then {
[GVAR(oldUnit), visibleMap] call _function;
};
[QGVAR(visibleMapEvent), _function] call CBA_fnc_addEventHandler // return id
};
case "group": {
if (_applyRetroactively) then {
[GVAR(oldUnit), GVAR(oldGroup), GVAR(oldGroup)] call _function; // backwards compatiblity
};
[QGVAR(groupEvent), _function] call CBA_fnc_addEventHandler // return id
};
case "leader": {
if (_applyRetroactively) then {
[GVAR(oldUnit), GVAR(oldLeader), GVAR(oldLeader)] call _function; //backwards compatiblity
};
[QGVAR(leaderEvent), _function] call CBA_fnc_addEventHandler // return id
};
default {-1};
};
if (_id != -1) then {
// add loop for polling if it doesn't exist yet
if (isNil QGVAR(playerEHInfo)) then {
GVAR(playerEHInfo) = [];
GVAR(oldUnit) = objNull;
GVAR(oldGroup) = grpNull;
GVAR(oldLeader) = objNull;
GVAR(oldWeapon) = "";
GVAR(oldTurretWeapon) = "";
GVAR(oldMuzzle) = [objNull, "", ""];
GVAR(oldWeaponMode) = [objNull, GVAR(oldMuzzle), ""];
GVAR(oldLoadout) = [];
GVAR(oldLoadoutNoAmmo) = [];
GVAR(oldVehicle) = objNull;
GVAR(oldTurret) = [];
GVAR(oldVisionMode) = -1;
GVAR(oldCameraView) = "";
GVAR(oldFeatureCamera) = "";
GVAR(oldVisibleMap) = false;
GVAR(oldState) = [];
GVAR(playerEHInfo) pushBack addMissionEventHandler ["EachFrame", {call FUNC(playerEvent)}];
GVAR(playerEHInfo) pushBack addMissionEventHandler ["Map", {
SCRIPT(playerEvent_Map);
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;
};
}];
// emulate change to first value from default one frame later
// using spawn-dc to not having to wait for postInit to complete
0 spawn {
{
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;
};
} call CBA_fnc_directCall;
};
GVAR(playerEHInfo) pushBack ([{
SCRIPT(playerEH_featureCamera);
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];
};
_id