Common - Improve addWeapon (#10051)

Improve `addWeapon`
This commit is contained in:
johnb432 2024-06-01 18:52:18 +02:00 committed by GitHub
parent 1cca2db964
commit 99bb6983bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,22 +2,33 @@
/* /*
* Author: commy2, johnb43 * Author: commy2, johnb43
* Adds weapon to unit without taking a magazine. * Adds weapon to unit without taking a magazine.
* Same as CBA_fnc_addWeaponWithoutItems, but doesn't remove linked items. * Same as CBA_fnc_addWeaponWithoutItems, but doesn't remove linked items by default.
* *
* Arguments: * Arguments:
* 0: Unit to add the weapon to <OBEJCT> * 0: Unit to add the weapon to <OBJECT>
* 1: Weapon to add <STRING> * 1: Weapon to add <STRING>
* 2: If linked items should be removed or not <BOOL> (default: false)
* 3: Magazines that should be added to the weapon <ARRAY> (default: [])
* - 0: Magazine classname <STRING>
* - 1: Ammo count <NUMBER>
* *
* Return Value: * Return Value:
* None * None
* *
* Example: * Example:
* [player, "arifle_AK12_F"] call ace_common_fnc_addWeapon * [player, "arifle_MX_GL_F", true, [["30Rnd_65x39_caseless_mag", 30], ["1Rnd_HE_Grenade_shell", 1]]] call ace_common_fnc_addWeapon
* *
* Public: Yes * Public: Yes
*/ */
params ["_unit", "_weapon"]; params [
["_unit", objNull, [objNull]],
["_weapon", "", [""]],
["_removeLinkedItems", false, [false]],
["_magazines", [], [[]]]
];
if (isNull _unit || {_weapon == ""}) exitWith {};
// Config case // Config case
private _compatibleMagazines = compatibleMagazines _weapon; private _compatibleMagazines = compatibleMagazines _weapon;
@ -45,6 +56,35 @@ private _backpackMagazines = (magazinesAmmoCargo _backpack) select {
// Add weapon // Add weapon
_unit addWeapon _weapon; _unit addWeapon _weapon;
// This doesn't remove magazines, but linked items can't be magazines, so it's fine
if (_removeLinkedItems) then {
switch (_weapon call FUNC(getConfigName)) do {
case (primaryWeapon _unit): {
removeAllPrimaryWeaponItems _unit;
};
case (secondaryWeapon _unit): {
removeAllSecondaryWeaponItems _unit;
};
case (handgunWeapon _unit): {
removeAllHandgunItems _unit;
};
case (binocular _unit): {
removeAllBinocularItems _unit;
};
};
};
// Add magazines directly now, so that AI don't reload
if (_magazines isNotEqualTo []) then {
{
_x params [["_magazine", "", [""]], ["_ammoCount", -1, [0]]];
if (_magazine != "" && {_ammoCount > -1}) then {
_unit addWeaponItem [_weapon, [_magazine, _ammoCount], true];
};
} forEach _magazines;
};
// Add all magazines back // Add all magazines back
{ {
_uniform addMagazineAmmoCargo [_x select 0, 1, _x select 1]; _uniform addMagazineAmmoCargo [_x select 0, 1, _x select 1];