From 4a17bde51c14dbe418c1f4fe4c7627dde6226c1b Mon Sep 17 00:00:00 2001 From: commy2 Date: Sat, 27 Nov 2021 20:06:56 +0100 Subject: [PATCH 1/2] CBA_fnc_canAddItem for vehicles and crates --- addons/common/fnc_canAddItem.sqf | 88 ++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 39 deletions(-) diff --git a/addons/common/fnc_canAddItem.sqf b/addons/common/fnc_canAddItem.sqf index ee8b0587ef..80a30f6793 100644 --- a/addons/common/fnc_canAddItem.sqf +++ b/addons/common/fnc_canAddItem.sqf @@ -3,7 +3,7 @@ Function: CBA_fnc_canAddItem Description: - Checks if unit has enough free space in inventory to store item. + Checks if unit or object has enough free space in inventory to store item. Doesn't take current unit load into account unlike canAdd command. @@ -16,7 +16,7 @@ Parameters: _checkBackpack - Check space in backpack (Default: true) Returns: - True if unit has free space, false otherwise + True if unit or object has free space, false otherwise Examples: (begin example) @@ -45,10 +45,10 @@ if (isNull _unit || {_item isEqualTo ""}) exitWith {false}; #define TYPE_BACKPACK 901 if (isNil QGVAR(itemMassAllowedSlots)) then { - GVAR(itemMassAllowedSlots) = [] call CBA_fnc_createNamespace; + GVAR(itemMassAllowedSlots) = createHashMap; }; -(GVAR(itemMassAllowedSlots) getVariable [_item, []]) params ["_mass", "_allowedSlots"]; +(GVAR(itemMassAllowedSlots) getOrDefault [_item, []]) params ["_mass", "_allowedSlots"]; if (isNil "_mass") then { _allowedSlots = [TYPE_UNIFORM, TYPE_VEST, TYPE_BACKPACK]; @@ -91,46 +91,56 @@ if (isNil "_mass") then { }; }; TRACE_3("caching",_item,_mass,_allowedSlots); - GVAR(itemMassAllowedSlots) setVariable [_item, [_mass, _allowedSlots]]; + GVAR(itemMassAllowedSlots) set [_item, [_mass, _allowedSlots]]; }; if (_mass == -1) exitWith {false}; // item doesn't exist -if ( - _checkUniform - && {TYPE_UNIFORM in _allowedSlots} - && { - _mass == 0 - || { - // each time subtract whole number of items which can be put in container - _count = _count - floor (getContainerMaxLoad uniform _unit * (1 - loadUniform _unit) / _mass); - _count <= 0 +if (_unit isKindOf "CAManBase") then { + // is a person + if ( + _checkUniform + && {TYPE_UNIFORM in _allowedSlots} + && { + _mass == 0 + || { + // each time subtract whole number of items which can be put in container + _count = _count - floor (getContainerMaxLoad uniform _unit * (1 - loadUniform _unit) / _mass); + _count <= 0 + } } - } -) exitWith {true}; - -if ( - _checkVest - && {TYPE_VEST in _allowedSlots} - && { - _mass == 0 - || { - _count = _count - floor (getContainerMaxLoad vest _unit * (1 - loadVest _unit) / _mass); - _count <= 0 + ) exitWith {true}; + + if ( + _checkVest + && {TYPE_VEST in _allowedSlots} + && { + _mass == 0 + || { + _count = _count - floor (getContainerMaxLoad vest _unit * (1 - loadVest _unit) / _mass); + _count <= 0 + } } - } -) exitWith {true}; - -if ( - _checkBackpack - && {TYPE_BACKPACK in _allowedSlots} - && { - _mass == 0 - || { - _count = _count - floor (getContainerMaxLoad backpack _unit * (1 - loadBackpack _unit) / _mass); - _count <= 0 + ) exitWith {true}; + + if ( + _checkBackpack + && {TYPE_BACKPACK in _allowedSlots} + && { + _mass == 0 + || { + _count = _count - floor (getContainerMaxLoad backpack _unit * (1 - loadBackpack _unit) / _mass); + _count <= 0 + } } + ) exitWith {true}; + + false +} else { + // is a vehicle, crate etc. + _mass == 0 + || { + _count = _count - floor (maxLoad _unit * (1 - load _unit) / _mass); + _count <= 0 } -) exitWith {true}; - -false +}; From 26dcb6c476c1ca622a382d7d31e2237331de54b1 Mon Sep 17 00:00:00 2001 From: PabstMirror Date: Sat, 19 Mar 2022 13:35:00 -0500 Subject: [PATCH 2/2] Apply suggestions from code review Support 2.08's `setMaxLoad` Co-authored-by: GhostIsSpooky <69561145+Salluci@users.noreply.github.com> --- addons/common/fnc_canAddItem.sqf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/addons/common/fnc_canAddItem.sqf b/addons/common/fnc_canAddItem.sqf index 80a30f6793..cd7ac35e4c 100644 --- a/addons/common/fnc_canAddItem.sqf +++ b/addons/common/fnc_canAddItem.sqf @@ -105,7 +105,7 @@ if (_unit isKindOf "CAManBase") then { _mass == 0 || { // each time subtract whole number of items which can be put in container - _count = _count - floor (getContainerMaxLoad uniform _unit * (1 - loadUniform _unit) / _mass); + _count = _count - floor (maxLoad uniformContainer _unit * (1 - loadUniform _unit) / _mass); _count <= 0 } } @@ -117,7 +117,7 @@ if (_unit isKindOf "CAManBase") then { && { _mass == 0 || { - _count = _count - floor (getContainerMaxLoad vest _unit * (1 - loadVest _unit) / _mass); + _count = _count - floor (maxLoad vestContainer _unit * (1 - loadVest _unit) / _mass); _count <= 0 } } @@ -129,7 +129,7 @@ if (_unit isKindOf "CAManBase") then { && { _mass == 0 || { - _count = _count - floor (getContainerMaxLoad backpack _unit * (1 - loadBackpack _unit) / _mass); + _count = _count - floor (maxLoad backpackContainer _unit * (1 - loadBackpack _unit) / _mass); _count <= 0 } }