ACE3/addons/csw/functions/fnc_assemble_pickupWeapon.sqf
johnb432 cd66f495ad
CSW - Improve tripod and weapon disassembly (#9915)
* Add disassembly for tripods when players have occupied launcher slots

* Update fnc_assemble_canDeployWeapon.sqf

* Update fnc_assemble_canPickupWeapon.sqf

* Revert file renaming (see #9777)

* Correct headers

* Update fnc_canPickupTripod.sqf

* Fixed checks and eject dead bodies

* Invert checks
2024-06-22 19:39:36 +02:00

131 lines
5.5 KiB
Plaintext

#include "..\script_component.hpp"
/*
* Author: tcvm
* Dismounts the weapon from the tripod and drops its backpack beside
*
* Arguments:
* 0: Static Weapon <OBJECT>
* 1: Unit <OBJECT>
*
* Return Value:
* None
*
* Example:
* [cursorObject, player] call ace_csw_fnc_assemble_pickupWeapon
*
* Public: No
*/
[{
params ["_staticWeapon", "_player"];
TRACE_2("assemble_pickupWeapon",_staticWeapon,_player);
private _weaponConfig = configOf _staticWeapon >> QUOTE(ADDON);
private _carryWeaponClassname = getText (_weaponConfig >> "disassembleWeapon");
if (!isClass (configFile >> "CfgWeapons" >> _carryWeaponClassname)) exitWith {
ERROR_1("bad weapon classname [%1]",_carryWeaponClassname);
};
private _turretClassname = getText (_weaponConfig >> "disassembleTurret");
// Turret classname can equal nothing if the deploy bag is the "whole" weapon. e.g Kornet, Metis, other ATGMs
if ((_turretClassname != "") && {!isClass (configFile >> "CfgVehicles" >> _turretClassname)}) exitWith {
ERROR_1("bad turret classname [%1]",_turretClassname);
};
private _onDisassembleFunc = getText (_weaponConfig >> "disassembleFunc");
private _pickupTime = getNumber (configFile >> "CfgWeapons" >> _carryWeaponClassname >> QUOTE(ADDON) >> "pickupTime");
TRACE_4("",typeOf _staticWeapon,_carryWeaponClassname,_turretClassname,_pickupTime);
private _onFinish = {
params ["_args"];
_args params ["_staticWeapon", "_player", "_carryWeaponClassname", "_turretClassname", "_onDisassembleFunc"];
TRACE_4("disassemble finish",_staticWeapon,_player,_carryWeaponClassname,_turretClassname);
private _weaponPos = (getPosATL _staticWeapon) vectorAdd [0, 0, 0.1];
private _weaponDir = getDir _staticWeapon;
private _carryWeaponMag = [];
private _carryWeaponMags = compatibleMagazines _carryWeaponClassname;
LOG("remove ammo");
{
_x params ["_xMag", "", "_xAmmo"];
if (_xAmmo == 0) then {continue};
private _carryMag = _xMag call FUNC(getCarryMagazine);
if (_carryWeaponMag isEqualTo [] && {_carryMag in _carryWeaponMags}) then {
TRACE_3("Adding mag to secondary weapon",_xMag,_xAmmo,_carryMag);
_carryWeaponMag = [_carryMag, _xAmmo];
DEC(_xAmmo);
};
if ((_xAmmo > 0) && {_carryMag != ""}) then {
TRACE_2("Removing ammo",_xMag,_carryMag);
[_player, _carryMag, _xAmmo] call FUNC(reload_handleReturnAmmo);
};
} forEach (magazinesAllTurrets _staticWeapon);
if (_turretClassname isNotEqualTo "") then {
private _cswTripod = createVehicle [_turretClassname, [0, 0, 0], [], 0, "NONE"];
// Delay a frame so weapon has a chance to be deleted
[{
params ["_cswTripod", "_weaponDir", "_weaponPos"];
_cswTripod setDir _weaponDir;
_cswTripod setPosATL _weaponPos;
_cswTripod setVelocity [0, 0, -0.05];
_cswTripod setVectorUp (surfaceNormal _weaponPos);
}, [_cswTripod, _weaponDir, _weaponPos]] call CBA_fnc_execNextFrame;
[_cswTripod, _staticWeapon] call (missionNamespace getVariable _onDisassembleFunc);
};
[{
params ["_player", "_weaponPos", "_carryWeaponClassname", "_carryWeaponMag", "_turretClassname"];
// Give the weapon to the player if possible
if ((alive _player) && {(secondaryWeapon _player) == ""}) exitWith {
[_player, _carryWeaponClassname] call CBA_fnc_addWeaponWithoutItems;
if (_carryWeaponMag isNotEqualTo []) then {
_player addWeaponItem [_carryWeaponClassname, _carryWeaponMag, true];
};
};
// If there is no turret, place the ground holder where the turret was
if (_turretClassname != "") then {
_weaponPos = _weaponPos getPos RELATIVE_DIRECTION(90);
};
// Create a new weapon holder (don't try to get an existing one, as no guarantee where it could be)
private _weaponHolder = createVehicle ["GroundWeaponHolder", [0, 0, 0], [], 0, "CAN_COLLIDE"];
_weaponHolder setDir random [0, 180, 360];
_weaponHolder setVehiclePosition [_weaponPos, [], 0, "CAN_COLLIDE"]; // places object on surface below
_weaponHolder addWeaponWithAttachmentsCargoGlobal [[_carryWeaponClassname, "", "", "", _carryWeaponMag, [], ""], 1];
}, [_player, _weaponPos, _carryWeaponClassname, _carryWeaponMag, _turretClassname]] call CBA_fnc_execNextFrame;
LOG("delete weapon");
// Eject dead units (all crew are dead or UAV at this point, otherwise condition would have failed), but ignore UAV units
{
if (unitIsUAV _x) then {
_staticWeapon deleteVehicleCrew _x;
} else {
moveOut _x;
};
} forEach (crew _staticWeapon);
deleteVehicle _staticWeapon;
LOG("end");
};
private _condition = {
params ["_args"];
_args params ["_staticWeapon"];
_staticWeapon call FUNC(assemble_canPickupWeapon)
};
[TIME_PROGRESSBAR(_pickupTime), [_staticWeapon, _player, _carryWeaponClassname, _turretClassname, _onDisassembleFunc], _onFinish, {}, LLSTRING(DisassembleCSW_progressBar), _condition] call EFUNC(common,progressBar);
}, _this] call CBA_fnc_execNextFrame;