2018-09-17 19:19:29 +00:00
|
|
|
#include "script_component.hpp"
|
2015-01-30 20:32:21 +00:00
|
|
|
/*
|
2015-02-01 20:56:19 +00:00
|
|
|
* Author: Pabst Mirror (based on Explosive attach by Garth de Wet (LH))
|
|
|
|
* Approves placement of the lightObject, scans for an appropriate location and attaches
|
2015-03-27 16:14:04 +00:00
|
|
|
* A player can release the attachObject with it floating in mid-air.
|
2019-12-07 20:58:21 +00:00
|
|
|
* This will use lineIntersectsSurfaces to scan towards the center of the vehicle to find a collision
|
2021-04-26 16:56:11 +00:00
|
|
|
* Arma's collision detection is of couse terrible and often misses collisions (difference between what we see and collision LOD)
|
2015-03-27 16:14:04 +00:00
|
|
|
* So it does multiple scans at slighly different angles
|
|
|
|
* This is VERY computationaly intensive, but doesn't happen that often.
|
2015-02-01 20:56:19 +00:00
|
|
|
*
|
|
|
|
* Arguments:
|
2015-03-27 17:08:05 +00:00
|
|
|
* 0: Unit (player) <OBJECT>
|
|
|
|
* 1: attachToVehicle <OBJECT>
|
2015-11-30 16:09:26 +00:00
|
|
|
* 2: Item Classname (CfgWeapon/CfgMagazine) <STRING>
|
2015-03-27 17:08:05 +00:00
|
|
|
* 3: Light Vehicle Classname <STRING>
|
|
|
|
* 4: On Attach Text <STRING>
|
|
|
|
* 5: Starting Pos of dummy item <ARRAY>
|
2015-02-01 20:56:19 +00:00
|
|
|
*
|
|
|
|
* Return Value:
|
2017-06-08 13:31:51 +00:00
|
|
|
* None
|
2015-02-01 20:56:19 +00:00
|
|
|
*
|
|
|
|
* Example:
|
2015-03-27 17:08:05 +00:00
|
|
|
* No
|
2015-02-01 20:56:19 +00:00
|
|
|
*
|
|
|
|
* Public: No
|
|
|
|
*/
|
2015-01-30 20:32:21 +00:00
|
|
|
|
2017-10-10 14:39:59 +00:00
|
|
|
params ["_unit", "_attachToVehicle", "_itemClassname", "_itemVehClass", "_onAttachText", "_startingPosition"];
|
|
|
|
TRACE_6("params",_unit,_attachToVehicle,_itemClassname,_itemVehClass,_onAttachText,_startingPosition);
|
2015-01-30 20:32:21 +00:00
|
|
|
|
2017-10-10 14:39:59 +00:00
|
|
|
private _startingOffset = _attachToVehicle worldToModel _startingPosition;
|
2015-08-06 18:17:59 +00:00
|
|
|
|
2017-10-10 14:39:59 +00:00
|
|
|
private _startDistanceFromCenter = vectorMagnitude _startingOffset;
|
|
|
|
private _closeInUnitVector = vectorNormalized (_startingOffset vectorFromTo [0,0,0]);
|
2015-01-31 09:05:07 +00:00
|
|
|
|
2017-10-10 14:39:59 +00:00
|
|
|
private _closeInMax = _startDistanceFromCenter;
|
|
|
|
private _closeInMin = 0;
|
2015-01-31 09:05:07 +00:00
|
|
|
|
2015-03-26 22:49:11 +00:00
|
|
|
while {(_closeInMax - _closeInMin) > 0.01} do {
|
2017-10-10 14:39:59 +00:00
|
|
|
private _closeInDistance = (_closeInMax + _closeInMin) / 2;
|
2015-03-26 22:49:11 +00:00
|
|
|
// systemChat format ["Trying %1 from %2 start %3", _closeInDistance, [_closeInMax, _closeInMin], _startDistanceFromCenter];
|
2017-10-10 14:39:59 +00:00
|
|
|
private _endPosTestOffset = _startingOffset vectorAdd (_closeInUnitVector vectorMultiply _closeInDistance);
|
2015-02-01 20:56:19 +00:00
|
|
|
_endPosTestOffset set [2, (_startingOffset select 2)];
|
2017-10-10 14:39:59 +00:00
|
|
|
private _endPosTest = _attachToVehicle modelToWorldVisual _endPosTestOffset;
|
2015-01-31 09:05:07 +00:00
|
|
|
|
2017-10-10 14:39:59 +00:00
|
|
|
private _doesIntersect = false;
|
2015-01-30 20:32:21 +00:00
|
|
|
{
|
2015-03-26 22:49:11 +00:00
|
|
|
if (_doesIntersect) exitWith {};
|
2017-10-10 14:39:59 +00:00
|
|
|
private _startingPosShifted = _startingPosition vectorAdd _x;
|
|
|
|
private _startASL = if (surfaceIsWater _startingPosShifted) then {_startingPosShifted} else {ATLtoASL _startingPosShifted};
|
2015-02-01 20:56:19 +00:00
|
|
|
{
|
2017-10-10 14:39:59 +00:00
|
|
|
private _endPosShifted = _endPosTest vectorAdd _x;
|
|
|
|
private _endASL = if (surfaceIsWater _startingPosShifted) then {_endPosShifted} else {ATLtoASL _endPosShifted};
|
2015-02-01 20:56:19 +00:00
|
|
|
|
2017-08-22 18:30:56 +00:00
|
|
|
#ifdef DRAW_ATTACH_SCAN
|
|
|
|
[{
|
|
|
|
params ["_args", "_idPFH"];
|
|
|
|
_args params ["_startingPosShifted", "_endPosShifted", "_timeAdded"];
|
|
|
|
drawLine3D [_startingPosShifted, _endPosShifted, [1,0,0,1]];
|
|
|
|
if (_timeAdded + 5 < CBA_missionTime) then {
|
|
|
|
[_idPFH] call CBA_fnc_removePerFrameHandler;
|
|
|
|
};
|
|
|
|
}, 0, [_startingPosShifted, _endPosShifted, CBA_missionTime]] call CBA_fnc_addPerFrameHandler;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Default max results is 1, so take only first subarray and select parentObject (object itself or parent of proxy)
|
|
|
|
private _intersectObject = ((lineIntersectsSurfaces [_startASL, _endASL, _unit]) param [0, objNull]) param [3, objNull];
|
|
|
|
if (_attachToVehicle == _intersectObject) exitWith {_doesIntersect = true};
|
2015-02-01 20:56:19 +00:00
|
|
|
} forEach [[0,0,0.045], [0,0,-0.045], [0,0.045,0], [0,-0.045,0], [0.045,0,0], [-0.045,0,0]];
|
|
|
|
} forEach [[0,0,0], [0,0,0.05], [0,0,-0.05]];
|
2015-03-27 05:12:39 +00:00
|
|
|
|
2015-03-26 22:49:11 +00:00
|
|
|
if (_doesIntersect) then {
|
|
|
|
_closeInMax = _closeInDistance;
|
|
|
|
} else {
|
|
|
|
_closeInMin = _closeInDistance;
|
|
|
|
};
|
2015-01-30 20:32:21 +00:00
|
|
|
};
|
|
|
|
|
2017-10-10 14:39:59 +00:00
|
|
|
private _closeInDistance = (_closeInMax + _closeInMin) / 2;
|
2015-03-26 22:49:11 +00:00
|
|
|
|
2015-03-27 05:12:39 +00:00
|
|
|
//Checks (too close to center or can't attach)
|
2015-03-27 16:14:04 +00:00
|
|
|
if (((_startDistanceFromCenter - _closeInDistance) < 0.1) || {!([_attachToVehicle, _unit, _itemClassname] call FUNC(canAttach))}) exitWith {
|
2015-03-26 22:49:11 +00:00
|
|
|
TRACE_2("no valid spot found",_closeInDistance,_startDistanceFromCenter);
|
2015-05-28 19:59:04 +00:00
|
|
|
[localize LSTRING(Failed)] call EFUNC(common,displayTextStructured);
|
2015-01-31 17:43:58 +00:00
|
|
|
};
|
2015-01-31 09:05:07 +00:00
|
|
|
|
2015-03-26 22:49:11 +00:00
|
|
|
//Move it out slightly, for visability sake (better to look a little funny than be embedded//sunk in the hull and be useless)
|
2015-01-31 09:05:07 +00:00
|
|
|
_closeInDistance = (_closeInDistance - 0.0085);
|
|
|
|
|
2015-01-30 20:32:21 +00:00
|
|
|
//Create New 'real' Object
|
2017-10-10 14:39:59 +00:00
|
|
|
private _endPosTestOffset = _startingOffset vectorAdd (_closeInUnitVector vectorMultiply _closeInDistance);
|
2015-01-31 09:05:07 +00:00
|
|
|
_endPosTestOffset set [2, (_startingOffset select 2)];
|
2017-10-10 14:39:59 +00:00
|
|
|
private _attachedObject = _itemVehClass createVehicle (getPos _unit);
|
2015-01-31 09:05:07 +00:00
|
|
|
_attachedObject attachTo [_attachToVehicle, _endPosTestOffset];
|
2015-01-30 20:32:21 +00:00
|
|
|
|
|
|
|
//Remove Item from inventory
|
2015-03-27 16:14:04 +00:00
|
|
|
_unit removeItem _itemClassname;
|
2015-01-30 20:32:21 +00:00
|
|
|
|
2015-08-28 21:57:16 +00:00
|
|
|
//Add Object to attached array
|
2017-10-10 14:39:59 +00:00
|
|
|
private _attachList = _attachToVehicle getVariable [QGVAR(attached), []];
|
2015-08-28 21:57:16 +00:00
|
|
|
_attachList pushBack [_attachedObject, _itemClassname];
|
|
|
|
_attachToVehicle setVariable [QGVAR(attached), _attachList, true];
|
2021-10-05 17:32:38 +00:00
|
|
|
[QGVAR(attached), [_attachedObject, _itemClassname, false]] call CBA_fnc_localEvent;
|
2015-01-30 20:32:21 +00:00
|
|
|
|
2017-12-16 15:20:43 +00:00
|
|
|
[_onAttachText, 2] call EFUNC(common,displayTextStructured);
|