-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathfnc_setupExplosive.sqf
More file actions
296 lines (232 loc) · 12.7 KB
/
fnc_setupExplosive.sqf
File metadata and controls
296 lines (232 loc) · 12.7 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "..\script_component.hpp"
/*
* Author: Garth 'L-H' de Wet
* Starts the setup process for the passed explosive. Player only.
*
* Arguments:
* 0: Target (not used) <OBJECT>
* 1: Unit <OBJECT> (default: objNull)
* 2: Magazine classname of explosive to place <STRING> (default: "")
*
* Return Value:
* None
*
* Example:
* [objNull, player, "SatchelCharge_Remote_Mag"] call ace_explosives_fnc_setupExplosive
*
* Public: Yes
*/
#define TEST_LENGTH 0.1
#define PLACE_RANGE_MAX 1
#define PLACE_RANGE_MIN 0.025
params ["", ["_unit", objNull, [objNull]], ["_magClassname", "", [""]]];
TRACE_2("params",_unit,_magClassname);
if (!alive _unit || {_magClassname == ""}) exitWith {};
// Get setup object vehicle and model
private _configMagazine = configFile >> "CfgMagazines" >> _magClassname;
private _setupObjectClass = getText (_configMagazine >> QGVAR(setupObject));
if (!isClass (configFile >> "CfgVehicles" >> _setupObjectClass)) exitWith {
ERROR("Bad Vehicle");
};
private _p3dModel = getText (configFile >> "CfgVehicles" >> _setupObjectClass >> "model");
// "" will crash game!
if (_p3dModel == "") exitWith {
ERROR("No Model");
};
[_unit, "forceWalk", QUOTE(ADDON), true] call EFUNC(common,statusEffect_set);
[_unit, "blockThrow", QUOTE(ADDON), true] call EFUNC(common,statusEffect_set);
// Show mouse buttons
[LLSTRING(PlaceAction), LELSTRING(common,Cancel), LLSTRING(ScrollAction)] call EFUNC(interaction,showMouseHint);
_unit setVariable [QGVAR(placeActionEH), [_unit, "DefaultAction", {true}, {GVAR(placeAction) = PLACE_APPROVE}] call EFUNC(common,AddActionEventHandler)];
_unit setVariable [QGVAR(cancelActionEH), [_unit, "zoomtemp", {true}, {GVAR(placeAction) = PLACE_CANCEL}] call EFUNC(common,AddActionEventHandler)];
// Display to show virtual object
(QGVAR(virtualAmmo) call BIS_fnc_rscLayer) cutRsc [QGVAR(virtualAmmo), "PLAIN", 0, false];
((uiNamespace getVariable [QGVAR(virtualAmmoDisplay), displayNull]) displayCtrl 800851) ctrlSetModel _p3dModel;
// Make sure it has a trigger that works when attached (e.g. no tripwires that only do pressurePlate)
private _supportedTriggers = getArray (_configMagazine >> "ACE_Triggers" >> "SupportedTriggers");
private _aceTriggers = configFile >> "ACE_Triggers";
private _isAttachable = _supportedTriggers findIf {(getNumber (_aceTriggers >> _x >> "isAttachable")) == 1} != -1;
private _isSticky = getNumber (_configMagazine >> QGVAR(isSticky)) == 1;
GVAR(pfeh_running) = true;
GVAR(placeAction) = PLACE_WAITING;
GVAR(TweakedAngle) = 0;
[{
BEGIN_COUNTER(pfeh);
disableSerialization;
params ["_args", "_pfhID"];
_args params ["_unit", "_magClassname", "_setupObjectClass", "_isAttachable", "_isSticky"];
private _lookDirVector = ((positionCameraToWorld [0, 0, 0]) call EFUNC(common,positionToASL)) vectorFromTo ((positionCameraToWorld [0, 0, 10]) call EFUNC(common,positionToASL));
private _basePosASL = eyePos _unit;
// If external, show explosive over the right shoulder
if (cameraView == "EXTERNAL") then {
_basePosASL = _basePosASL vectorAdd ((positionCameraToWorld [0.3, 0, 0]) vectorDiff (positionCameraToWorld [0, 0, 0]));
};
if ((stance _unit) == "PRONE") then {
// If prone, lower base and increase up angle of look - Makes it much easier to attach to underside of vehicles
_basePosASL vectorAdd [0, 0, -0.3];
_lookDirVector = ((positionCameraToWorld [0, 0, 0]) call EFUNC(common,positionToASL)) vectorFromTo ((positionCameraToWorld [0, 3, 10]) call EFUNC(common,positionToASL));
};
private _cameraAngle = (_lookDirVector select 0) atan2 (_lookDirVector select 1);
private _testPositionIsValid = {
private _testBase = _basePosASL vectorAdd (_lookDirVector vectorMultiply _this);
private _return = true;
{
private _testPos = _testBase vectorAdd [(_x select 0) * (cos _cameraAngle), (_x select 0) * (sin _cameraAngle), _x select 1];
#ifdef DEBUG_MODE_FULL
drawLine3D [(eyePos _unit) call EFUNC(common,ASLToPosition), _testPos call EFUNC(common,ASLToPosition), [1, 0, 0, 1]];
#endif
if ((lineIntersectsSurfaces [eyePos _unit, _testPos, _unit]) isNotEqualTo []) exitWith {
_return = false;
};
} forEach [[0, 0], [-TEST_LENGTH, -TEST_LENGTH], [TEST_LENGTH, -TEST_LENGTH], [-TEST_LENGTH, TEST_LENGTH], [TEST_LENGTH, TEST_LENGTH]];
_return
};
private _distanceFromBase = PLACE_RANGE_MAX;
private _badPosition = !(_distanceFromBase call _testPositionIsValid);
private _attachVehicle = objNull;
if (_isAttachable && _badPosition) then {
private _testBase = _basePosASL vectorAdd _lookDirVector;
{
private _testPos = _testBase vectorAdd [(_x select 0) * (cos _cameraAngle), (_x select 0) * (sin _cameraAngle), _x select 1];
private _intersectObject = ((lineIntersectsSurfaces [eyePos _unit, _testPos, _unit]) param [0, objNull]) param [3, objNull];
if (!isNull _intersectObject) exitWith {
_attachVehicle = _intersectObject;
};
} forEach [[0, 0], [-TEST_LENGTH, -TEST_LENGTH], [TEST_LENGTH, -TEST_LENGTH], [-TEST_LENGTH, TEST_LENGTH], [TEST_LENGTH, TEST_LENGTH]];
if (
!isNull _attachVehicle && (
// Allow attaching to non-unit objects (if set) or any vehicle
(_isSticky && {!(_attachVehicle isKindOf "Man")}) ||
{_attachVehicle isKindOf "Car"} ||
{_attachVehicle isKindOf "Tank"} ||
{_attachVehicle isKindOf "Air"} ||
{_attachVehicle isKindOf "Ship"}
) &&
{PLACE_RANGE_MIN call _testPositionIsValid}
) then {
private _min = PLACE_RANGE_MIN;
private _max = PLACE_RANGE_MAX;
for "_" from 0 to 6 do {
_distanceFromBase = (_min + _max) / 2;
if (_distanceFromBase call _testPositionIsValid) then {
_min = _distanceFromBase;
} else {
_max = _distanceFromBase;
};
};
_badPosition = false;
_distanceFromBase = ((_min + _max) / 2 + 0.075) min 1;
} else {
_attachVehicle = objNull;
};
};
private _virtualPosASL = _basePosASL vectorAdd (_lookDirVector vectorMultiply _distanceFromBase);
// Update mouse hint
private _ctrlTextLMB = (uiNamespace getVariable [QEGVAR(interaction,mouseHint), displayNull]) displayCtrl 2420;
if (_badPosition) then {
_ctrlTextLMB ctrlSetText LLSTRING(BlockedAction);
} else {
_ctrlTextLMB ctrlSetText localize ([LSTRING(AttachAction), LSTRING(PlaceAction)] select (isNull _attachVehicle));
};
private _terrainHeightASL = getTerrainHeightASL _virtualPosASL;
// Don't allow Placing below terrain
if (_terrainHeightASL > (_virtualPosASL select 2)) then {
_virtualPosASL set [2, _terrainHeightASL];
};
// Don't allow placing in a bad position
if (_badPosition && {GVAR(placeAction) == PLACE_APPROVE}) then {
GVAR(placeAction) = PLACE_WAITING;
};
if (_unit != ACE_player ||
{!([_unit, objNull, ["isNotSwimming"]] call EFUNC(common,canInteractWith))} ||
{!([_unit, _magClassname] call EFUNC(common,hasMagazine))}
) then {
GVAR(placeAction) = PLACE_CANCEL;
};
if (GVAR(placeAction) != PLACE_WAITING) then {
_pfhID call CBA_fnc_removePerFrameHandler;
GVAR(pfeh_running) = false;
[_unit, "forceWalk", QUOTE(ADDON), false] call EFUNC(common,statusEffect_set);
[_unit, "blockThrow", QUOTE(ADDON), false] call EFUNC(common,statusEffect_set);
[] call EFUNC(interaction,hideMouseHint);
[_unit, "DefaultAction", _unit getVariable [QGVAR(placeActionEH), -1]] call EFUNC(common,removeActionEventHandler);
[_unit, "zoomtemp", _unit getVariable [QGVAR(cancelActionEH), -1]] call EFUNC(common,removeActionEventHandler);
(QGVAR(virtualAmmo) call BIS_fnc_rscLayer) cutText ["", "PLAIN"];
if (GVAR(placeAction) == PLACE_APPROVE) then {
private _placeAngle = 0;
private _expSetupVehicle = _setupObjectClass createVehicle [0, 0, 0]; //(_virtualPosASL call EFUNC(common,ASLToPosition));
// Running it AGAIN in here because it needs the _expSetupVehicle Object for the boundingBox
private _intersect = lineIntersectsSurfaces [
eyePos _unit,
_basePosASL vectorAdd (_lookDirVector vectorMultiply PLACE_RANGE_MAX),
_unit, _expSetupVehicle, true, 1, "FIRE", "GEOM"
] param [0, []];
_virtualPosASL = if (_intersect isNotEqualTo []) then {
_intersect params ["_posASL", "_normal"];
if (_normal isEqualTo [0,0,0]) then {
_normal = surfaceNormal _posASL;
};
private _bbox = boundingBoxReal [_expSetupVehicle, "FireGeometry"];
private _offset = -((_bbox select 0) select 2);
_posASL vectorAdd (_normal vectorMultiply _offset);
} else {
_basePosASL vectorAdd (_lookDirVector vectorMultiply _distanceFromBase);
};
TRACE_1("Planting Mass",getMass _expSetupVehicle);
// If the object is too heavy, it can kill a player if it collides
if (getMass _expSetupVehicle > 5) then {
_expSetupVehicle setMass 5;
};
if (isNull _attachVehicle) then {
_placeAngle = _cameraAngle - GVAR(tweakedAngle) + 180;
_expSetupVehicle setDir _placeAngle;
_expSetupVehicle setPosASL _virtualPosASL;
_placeAngle = _placeAngle + 180; // CfgAmmos seem to be 180 for some reason
} else {
// Terrain objects (seemingly) can't have objects attached to them, so create a dummy instead
private _modelOffset = if (_attachVehicle call CBA_fnc_isTerrainObject) then {
_attachVehicle = createVehicle ["Helper_Base_F", [0, 0, 0], [], 0, "CAN_COLLIDE"];
_attachVehicle setPosASL _virtualPosASL;
_attachVehicle setDir _placeAngle;
[0, 0, 0];
} else {
_attachVehicle worldToModel (_virtualPosASL call EFUNC(common,ASLToPosition));
};
_placeAngle = _cameraAngle - (getDir _attachVehicle) + 180;
_expSetupVehicle attachTo [_attachVehicle, _modelOffset];
_expSetupVehicle setVectorDirAndUp [[0, 0, -1], [sin _placeAngle, cos _placeAngle, 0]];
};
TRACE_1("Place angle",_placeAngle);
_expSetupVehicle setVariable [QGVAR(class), _magClassname, true];
_expSetupVehicle setVariable [QGVAR(direction), _placeAngle, true];
[_unit, _magClassname] call EFUNC(common,adjustMagazineAmmo);
[_unit, "PutDown"] call EFUNC(common,doGesture);
_unit setVariable [QGVAR(plantingExplosive), true];
[{_this setVariable [QGVAR(plantingExplosive), false]}, _unit, 1.5] call CBA_fnc_waitAndExecute;
// API
[QGVAR(setup), [_expSetupVehicle, _magClassname, _unit]] call CBA_fnc_globalEvent;
};
} else {
private _screenPos = worldToScreen (_virtualPosASL call EFUNC(common,ASLToPosition));
if (_badPosition || {_screenPos isEqualTo []}) then {
((uiNamespace getVariable [QGVAR(virtualAmmoDisplay), displayNull]) displayCtrl 800851) ctrlShow false;
} else {
private _display = uiNamespace getVariable [QGVAR(virtualAmmoDisplay), displayNull];
// Show the model on the hud in aprox the same size/location as it will be placed
(_display displayCtrl 800851) ctrlShow true;
private _realDistance = ((_virtualPosASL call EFUNC(common,ASLToPosition)) distance (positionCameraToWorld [0, 0, 0])) / ((call CBA_fnc_getFov) select 1);
_screenPos = [_screenPos select 0, _realDistance, _screenPos select 1];
(_display displayCtrl 800851) ctrlSetPosition _screenPos;
private _modelDir = [0, 0, -1];
private _modelUp = [0, -1, 0];
if (isNull _attachVehicle) then {
private _angle = acos (_lookDirVector select 2);
_modelUp = [0, cos _angle, sin _angle];
_modelDir = [cos GVAR(tweakedAngle), sin GVAR(tweakedAngle), 0] vectorCrossProduct _modelUp;
};
(_display displayCtrl 800851) ctrlSetModelDirAndUp [_modelDir, _modelUp];
};
};
END_COUNTER(pfeh);
}, 0, [_unit, _magClassname, _setupObjectClass, _isAttachable, _isSticky]] call CBA_fnc_addPerFrameHandler;
nil