ACE3/addons/attach/functions/fnc_detach.sqf

82 lines
2.6 KiB
Plaintext
Raw Normal View History

2015-01-11 20:39:49 +00:00
/*
2015-03-24 04:18:00 +00:00
* Author: eRazeri and esteldunedain
2015-02-01 20:56:19 +00:00
* Detach an item from a unit
*
* Arguments:
* 0: vehicle that it will be detached from (player or vehicle) <OBJECT>
* 1: unit doing the detaching (player) <OBJECT>
2015-02-01 20:56:19 +00:00
*
* Return Value:
* Nothing
*
* Example:
* [car, bob] call ace_attach_fnc_detach
2015-02-01 20:56:19 +00:00
*
* Public: No
*/
#include "script_component.hpp"
2015-01-11 20:39:49 +00:00
PARAMS_2(_attachToVehicle,_unit);
2015-04-16 17:36:12 +00:00
private ["_attachedObjects", "_attachedItems", "_itemDisplayName"];
2015-02-15 14:31:09 +00:00
_attachedObjects = _attachToVehicle getVariable [QGVAR(Objects), []];
_attachedItems = _attachToVehicle getVariable [QGVAR(ItemNames), []];
private ["_attachedObject", "_attachedIndex", "_itemName", "_minDistance", "_unitPos", "_objectPos"];
_attachedObject = objNull;
_attachedIndex = -1;
_itemName = "";
//Find closest attached object
_minDistance = 1000;
_unitPos = getPos _unit;
_unitPos set [2,0];
{
2015-02-01 20:56:19 +00:00
_objectPos = getPos _x;
_objectPos set [2, 0];
2015-02-15 14:31:09 +00:00
if (_objectPos distance _unitPos < _minDistance) then {
_minDistance = _objectPos distance _unitPos;
2015-02-01 20:56:19 +00:00
_attachedObject = _x;
2015-02-15 14:31:09 +00:00
_itemName = _attachedItems select _forEachIndex;
2015-02-01 20:56:19 +00:00
_attachedIndex = _forEachIndex;
};
2015-02-15 14:31:09 +00:00
} forEach _attachedObjects;
2015-01-11 20:39:49 +00:00
// Check if unit has an attached item
2015-02-15 14:31:09 +00:00
if (isNull _attachedObject || {_itemName == ""}) exitWith {ERROR("Could not find attached object")};
2015-01-11 20:39:49 +00:00
2015-02-15 14:31:09 +00:00
// Exit if can't add the item
if !(_unit canAdd _itemName) exitWith {
2015-05-28 19:59:04 +00:00
[localize LSTRING(Inventory_Full)] call EFUNC(common,displayTextStructured);
2015-01-11 20:39:49 +00:00
};
2015-02-15 14:31:09 +00:00
// Add item to inventory
_unit addItem _itemName;
if (toLower _itemName in ["b_ir_grenade", "o_ir_grenade", "i_ir_grenade"]) then {
2015-02-01 20:56:19 +00:00
// Hack for dealing with X_IR_Grenade effect not dissapearing on deleteVehicle
detach _attachedObject;
2015-02-15 14:31:09 +00:00
_attachedObject setPos ((getPos _unit) vectorAdd [0, 0, -1000]);
2015-02-01 20:56:19 +00:00
// Delete attached item after 0.5 seconds
2015-02-15 14:31:09 +00:00
[{deleteVehicle (_this select 0)}, [_attachedObject], 0.5, 0] call EFUNC(common,waitAndExecute);
2015-01-13 09:06:24 +00:00
} else {
2015-02-01 20:56:19 +00:00
// Delete attached item
deleteVehicle _attachedObject;
2015-01-11 20:39:49 +00:00
};
// Reset unit variables
2015-02-15 14:31:09 +00:00
_attachedObjects deleteAt _attachedIndex;
_attachedItems deleteAt _attachedIndex;
_attachToVehicle setVariable [QGVAR(Objects), _attachedObjects, true];
_attachToVehicle setVariable [QGVAR(ItemNames), _attachedItems, true];
2015-01-11 20:39:49 +00:00
// Display message
_itemDisplayName = getText (configFile >> "CfgWeapons" >> _itemName >> "displayName");
if (_itemDisplayName == "") then {
_itemDisplayName = getText (configFile >> "CfgMagazines" >> _itemName >> "displayName");
2015-01-11 20:39:49 +00:00
};
2015-05-28 19:59:04 +00:00
[format [localize LSTRING(Item_Detached), _itemDisplayName]] call EFUNC(common,displayTextStructured);