diff --git a/addons/attach/CfgEventHandlers.hpp b/addons/attach/CfgEventHandlers.hpp index 3daad1425a..c050fbd1c7 100644 --- a/addons/attach/CfgEventHandlers.hpp +++ b/addons/attach/CfgEventHandlers.hpp @@ -8,3 +8,24 @@ class Extended_PostInit_EventHandlers { clientInit = QUOTE( call COMPILE_FILE(XEH_clientInit) ); }; }; +class Extended_GetIn_EventHandlers { + class All { + class ADDON { + getIn = QUOTE(_this call FUNC(handleGetIn)); + }; + }; +}; +class Extended_GetOut_EventHandlers { + class All { + class ADDON { + getOut = QUOTE(_this call FUNC(handleGetOut)); + }; + }; +}; +class Extended_Killed_EventHandlers { + class All { + class ADDON { + killed = QUOTE(_this call FUNC(handleKilled)); + }; + }; +}; diff --git a/addons/attach/XEH_preInit.sqf b/addons/attach/XEH_preInit.sqf index 06c6ed1b01..17b8490a77 100644 --- a/addons/attach/XEH_preInit.sqf +++ b/addons/attach/XEH_preInit.sqf @@ -7,6 +7,9 @@ PREP(canAttach); PREP(canDetach); PREP(detach); PREP(getChildrenAttachActions); +PREP(handleGetIn); +PREP(handleGetOut); +PREP(handleKilled); PREP(placeApprove); ADDON = true; diff --git a/addons/attach/functions/fnc_attach.sqf b/addons/attach/functions/fnc_attach.sqf index bd363b1307..6362edb33c 100644 --- a/addons/attach/functions/fnc_attach.sqf +++ b/addons/attach/functions/fnc_attach.sqf @@ -17,15 +17,14 @@ */ #include "script_component.hpp" -private ["_itemVehClass", "_onAtachText", "_selfAttachPosition", "_attachedItem", "_tempObject", "_actionID", "_model"]; -params ["_attachToVehicle","_unit","_args"]; +params ["_attachToVehicle","_unit","_args", ["_silentScripted", false]]; _args params [["_itemClassname","", [""]]]; -TRACE_3("params",_attachToVehicle,_unit,_itemClassname); +TRACE_4("params",_attachToVehicle,_unit,_itemClassname,_silentScripted); + +private ["_itemVehClass", "_onAtachText", "_selfAttachPosition", "_attachedItem", "_tempObject", "_actionID", "_model"]; //Sanity Check (_unit has item in inventory, not over attach limit) -if ((_itemClassname == "") || {!(_this call FUNC(canAttach))}) exitWith {ERROR("Tried to attach, but check failed");}; - -_selfAttachPosition = [_unit, [-0.05, 0, 0.12], "rightshoulder"]; +if ((_itemClassname == "") || {(!_silentScripted) && {!(_this call FUNC(canAttach))}}) exitWith {ERROR("Tried to attach, but check failed");}; _itemVehClass = getText (configFile >> "CfgWeapons" >> _itemClassname >> "ACE_Attachable"); _onAtachText = getText (configFile >> "CfgWeapons" >> _itemClassname >> "displayName"); @@ -40,12 +39,13 @@ if (_itemVehClass == "") exitWith {ERROR("no ACE_Attachable for Item");}; _onAtachText = format [localize LSTRING(Item_Attached), _onAtachText]; if (_unit == _attachToVehicle) then { //Self Attachment - _unit removeItem _itemClassname; // Remove item _attachedItem = _itemVehClass createVehicle [0,0,0]; - _attachedItem attachTo _selfAttachPosition; - [_onAtachText] call EFUNC(common,displayTextStructured); - _attachToVehicle setVariable [QGVAR(Objects), [_attachedItem], true]; - _attachToVehicle setVariable [QGVAR(ItemNames), [_itemClassname], true]; + _attachedItem attachTo [_unit, [-0.05, 0, 0.12], "rightshoulder"]; + if (!_silentScripted) then { + _unit removeItem _itemClassname; // Remove item + [_onAtachText] call EFUNC(common,displayTextStructured); + }; + _unit setVariable [QGVAR(attached), [[_attachedItem, _itemClassname]], true]; } else { GVAR(placeAction) = PLACE_WAITING; diff --git a/addons/attach/functions/fnc_canAttach.sqf b/addons/attach/functions/fnc_canAttach.sqf index 20a49c09be..37d92e8908 100644 --- a/addons/attach/functions/fnc_canAttach.sqf +++ b/addons/attach/functions/fnc_canAttach.sqf @@ -17,14 +17,17 @@ */ #include "script_component.hpp" -private ["_attachLimit", "_attachedObjects","_playerPos"]; params ["_attachToVehicle","_player","_args"]; _args params [["_itemClassname","", [""]]]; -TRACE_3("params",_attachToVehicle,_unit,_itemClassname); +TRACE_3("params",_attachToVehicle,_player,_itemClassname); + +private ["_attachLimit", "_attachedObjects"]; _attachLimit = [6, 1] select (_player == _attachToVehicle); -_attachedObjects = _attachToVehicle getVariable [QGVAR(Objects), []]; +_attachedObjects = _attachToVehicle getVariable [QGVAR(attached), []]; -_playerPos = (ACE_player modelToWorldVisual (ACE_player selectionPosition "pilot")); - -(canStand _player) && {(_attachToVehicle distance _player) < 7} && {alive _attachToVehicle} && {(count _attachedObjects) < _attachLimit} && {_itemClassname in ((itemsWithMagazines _player) + [""])}; +((_player == _attachToVehicle) || {canStand _player}) && +{(_attachToVehicle distance _player) < 7} && +{alive _attachToVehicle} && +{(count _attachedObjects) < _attachLimit} && +{_itemClassname in ((itemsWithMagazines _player) + [""])}; diff --git a/addons/attach/functions/fnc_canDetach.sqf b/addons/attach/functions/fnc_canDetach.sqf index ba3182ddea..303d0baf44 100644 --- a/addons/attach/functions/fnc_canDetach.sqf +++ b/addons/attach/functions/fnc_canDetach.sqf @@ -16,25 +16,17 @@ */ #include "script_component.hpp" -private ["_attachedObjects", "_inRange"]; params ["_attachToVehicle", "_unit"]; TRACE_2("params",_attachToVehicle,_unit); -_attachedObjects = _attachToVehicle getVariable [QGVAR(Objects), []]; +private ["_inRange"]; _inRange = false; -if (_unit == _attachToVehicle) then { - _inRange = count _attachedObjects > 0; -} else { - //Scan if unit is within range (using 2d distance) - private ["_unitPos", "_objectPos"]; - _unitPos = getPos _unit; - _unitPos set [2,0]; - { - _objectPos = getPos _x; - _objectPos set [2, 0]; - if (_objectPos distance _unitPos < 4) exitWith {_inRange = true}; - } forEach _attachedObjects; -}; +{ + _x params ["_xObject"]; + if (((getPos _unit) distance2d (getPos _xObject)) < 4) exitWith {_inRange = true}; +} forEach (_attachToVehicle getVariable [QGVAR(attached), []]); -canStand _unit && {_inRange} && {alive _attachToVehicle} +_inRange && +{(_unit == _attachToVehicle) || {canStand _unit}} && +{alive _attachToVehicle} diff --git a/addons/attach/functions/fnc_detach.sqf b/addons/attach/functions/fnc_detach.sqf index 98f482f17b..ec389b5c25 100644 --- a/addons/attach/functions/fnc_detach.sqf +++ b/addons/attach/functions/fnc_detach.sqf @@ -16,15 +16,12 @@ */ #include "script_component.hpp" -private ["_attachedObjects", "_attachedItems", "_itemDisplayName", - "_attachedObject", "_attachedIndex", "_itemName", "_minDistance", - "_unitPos", "_objectPos" -]; params ["_attachToVehicle","_unit"], TRACE_2("params",_attachToVehicle,_unit); -_attachedObjects = _attachToVehicle getVariable [QGVAR(Objects), []]; -_attachedItems = _attachToVehicle getVariable [QGVAR(ItemNames), []]; +private ["_attachedList", "_itemDisplayName", "_attachedObject", "_attachedIndex", "_itemName", "_minDistance", "_unitPos", "_objectPos"]; + +_attachedList = _attachToVehicle getVariable [QGVAR(attached), []]; _attachedObject = objNull; _attachedIndex = -1; @@ -32,18 +29,17 @@ _itemName = ""; //Find closest attached object _minDistance = 1000; -_unitPos = getPos _unit; -_unitPos set [2,0]; + { - _objectPos = getPos _x; - _objectPos set [2, 0]; - if (_objectPos distance _unitPos < _minDistance) then { - _minDistance = _objectPos distance _unitPos; - _attachedObject = _x; - _itemName = _attachedItems select _forEachIndex; + _x params ["_xObject", "_xItemName"]; + + if (((getPos _unit) distance2d (getPos _xObject)) < _minDistance) then { + _minDistance = ((getPos _unit) distance2d (getPos _xObject)); + _attachedObject = _xObject; + _itemName = _xItemName; _attachedIndex = _forEachIndex; }; -} forEach _attachedObjects; +} forEach _attachedList; // Check if unit has an attached item if (isNull _attachedObject || {_itemName == ""}) exitWith {ERROR("Could not find attached object")}; @@ -68,10 +64,8 @@ if (toLower _itemName in ["b_ir_grenade", "o_ir_grenade", "i_ir_grenade"]) then }; // Reset unit variables -_attachedObjects deleteAt _attachedIndex; -_attachedItems deleteAt _attachedIndex; -_attachToVehicle setVariable [QGVAR(Objects), _attachedObjects, true]; -_attachToVehicle setVariable [QGVAR(ItemNames), _attachedItems, true]; +_attachedList deleteAt _attachedIndex; +_attachToVehicle setVariable [QGVAR(attached), _attachedList, true]; // Display message _itemDisplayName = getText (configFile >> "CfgWeapons" >> _itemName >> "displayName"); diff --git a/addons/attach/functions/fnc_handleGetIn.sqf b/addons/attach/functions/fnc_handleGetIn.sqf new file mode 100644 index 0000000000..415fdddf8a --- /dev/null +++ b/addons/attach/functions/fnc_handleGetIn.sqf @@ -0,0 +1,38 @@ +/* + * Author: PabstMirror + * Handles when a unit gets in to a vehicle. + * + * Arguments: + * 0: vehicle + * 1: dunno + * 2: unit + * + * Return Value: + * None + * + * Example: + * [car2, x, player] call ACE_attach_fnc_handleGetIn + * + * Public: No + */ +#include "script_component.hpp" + +if (!isServer) exitWith {}; + +params ["", "", "_unit"]; +TRACE_1("params",_unit); + +private ["_attachedList"]; + +_attachedList = _unit getVariable [QGVAR(attached), []]; +if ((count _attachedList) == 0) exitWith {}; + +(_attachedList select 0) params ["_xObject", "_xItemName"]; +if (!isNull _xObject) then { + detach _xObject; + _xObject setPos ((getPos _unit) vectorAdd [0, 0, -1000]); + [{deleteVehicle (_this select 0)}, [_xObject], 2] call EFUNC(common,waitAndExecute); + (_attachedList select 0) set [0, objNull]; +}; + +_unit setVariable [QGVAR(attached), _attachedList, true]; diff --git a/addons/attach/functions/fnc_handleGetOut.sqf b/addons/attach/functions/fnc_handleGetOut.sqf new file mode 100644 index 0000000000..57fb69cf15 --- /dev/null +++ b/addons/attach/functions/fnc_handleGetOut.sqf @@ -0,0 +1,35 @@ +/* + * Author: PabstMirror + * Handles when a unit gets in to a vehicle. + * + * Arguments: + * 0: vehicle + * 1: dunno + * 2: unit + * + * Return Value: + * None + * + * Example: + * [car2, x, player] call ACE_attach_fnc_handleGetOut + * + * Public: No + */ +#include "script_component.hpp" + +if (!isServer) exitWith {}; + +params ["", "", "_unit"]; +TRACE_1("params",_unit); + +private ["_attachedList"]; + +_attachedList = _unit getVariable [QGVAR(attached), []]; +if ((count _attachedList) == 0) exitWith {}; + +(_attachedList select 0) params ["_xObject", "_xItemName"]; +if (isNull _xObject) then { + TRACE_1("null attached when exiting vehicle, scripted reattach",_xItemName); + _unit setVariable [QGVAR(attached), [], true]; + [_unit, _unit, _xItemName, true] call FUNC(attach); +}; diff --git a/addons/attach/functions/fnc_handleKilled.sqf b/addons/attach/functions/fnc_handleKilled.sqf new file mode 100644 index 0000000000..5fb33c2abe --- /dev/null +++ b/addons/attach/functions/fnc_handleKilled.sqf @@ -0,0 +1,33 @@ +/* + * Author: PabstMirror + * Handles when vehicle or man is killed. + * + * Arguments: + * 0: DeadVehicle + * + * Return Value: + * None + * + * Example: + * [bob1] call ACE_attach_fnc_handleKilled + * + * Public: No + */ +#include "script_component.hpp" + +if (!isServer) exitWith {}; + +params ["_deadUnit"]; + +private ["_attachedList"]; + +_attachedList = _deadUnit getVariable [QGVAR(attached), []]; + +if ((count _attachedList) == 0) exitWith {}; + +{ + _x params ["_xObject", "_xItemName"]; + detach _xObject; +} forEach _attachedList; + +_deadUnit setVariable [QGVAR(attached), nil, true]; diff --git a/addons/attach/functions/fnc_placeApprove.sqf b/addons/attach/functions/fnc_placeApprove.sqf index 0656e7880f..b8e2cc17fb 100644 --- a/addons/attach/functions/fnc_placeApprove.sqf +++ b/addons/attach/functions/fnc_placeApprove.sqf @@ -25,7 +25,7 @@ */ #include "script_component.hpp" -private ["_startingOffset", "_startDistanceFromCenter", "_closeInUnitVector", "_closeInMax", "_closeInMin", "_closeInDistance", "_endPosTestOffset", "_endPosTest", "_doesIntersect", "_startingPosShifted", "_startASL", "_endPosShifted", "_endASL", "_attachedObject", "_currentObjects", "_currentItemNames"]; +private ["_startingOffset", "_startDistanceFromCenter", "_closeInUnitVector", "_closeInMax", "_closeInMin", "_closeInDistance", "_endPosTestOffset", "_endPosTest", "_doesIntersect", "_startingPosShifted", "_startASL", "_endPosShifted", "_endASL", "_attachedObject", "_attachList"]; params ["_unit", "_attachToVehicle", "_itemClassname", "_itemVehClass", "_onAtachText", "_startingPosition"]; TRACE_6("params",_unit,_attachToVehicle,_itemClassname,_itemVehClass,_onAtachText,_startingPosition); @@ -87,12 +87,9 @@ _attachedObject attachTo [_attachToVehicle, _endPosTestOffset]; //Remove Item from inventory _unit removeItem _itemClassname; -//Add Object to ACE_AttachedObjects and ACE_AttachedItemNames -_currentObjects = _attachToVehicle getVariable [QGVAR(Objects), []]; -_currentObjects pushBack _attachedObject; -_attachToVehicle setVariable [QGVAR(Objects), _currentObjects, true]; -_currentItemNames = _attachToVehicle getVariable [QGVAR(ItemNames), []]; -_currentItemNames pushBack _itemClassname; -_attachToVehicle setVariable [QGVAR(ItemNames), _currentItemNames, true]; +//Add Object to attached array +_attachList = _attachToVehicle getVariable [QGVAR(attached), []]; +_attachList pushBack [_attachedObject, _itemClassname]; +_attachToVehicle setVariable [QGVAR(attached), _attachList, true]; [_onAtachText] call EFUNC(common,displayTextStructured);