Skip to content

Commit

Permalink
Common - Improve addWeapon (acemod#10051)
Browse files Browse the repository at this point in the history
Improve `addWeapon`
  • Loading branch information
johnb432 authored and blake8090 committed Aug 18, 2024
1 parent 0cd03b3 commit a6a2cf5
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions addons/common/functions/fnc_addWeapon.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,33 @@
/*
* Author: commy2, johnb43
* 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:
* 0: Unit to add the weapon to <OBEJCT>
* 0: Unit to add the weapon to <OBJECT>
* 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:
* None
*
* 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
*/

params ["_unit", "_weapon"];
params [
["_unit", objNull, [objNull]],
["_weapon", "", [""]],
["_removeLinkedItems", false, [false]],
["_magazines", [], [[]]]
];

if (isNull _unit || {_weapon == ""}) exitWith {};

// Config case
private _compatibleMagazines = compatibleMagazines _weapon;
Expand Down Expand Up @@ -45,6 +56,35 @@ private _backpackMagazines = (magazinesAmmoCargo _backpack) select {
// Add 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
{
_uniform addMagazineAmmoCargo [_x select 0, 1, _x select 1];
Expand Down

0 comments on commit a6a2cf5

Please sign in to comment.