From 9930ceb824230ca425c7ada00cd37e9bc25ef0c2 Mon Sep 17 00:00:00 2001 From: Dystopian Date: Thu, 30 Apr 2020 00:57:11 +0400 Subject: [PATCH 1/9] Add CBA_fnc_unitHasSpace --- addons/common/CfgFunctions.hpp | 1 + addons/common/fnc_unitHasSpace.sqf | 129 +++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 addons/common/fnc_unitHasSpace.sqf diff --git a/addons/common/CfgFunctions.hpp b/addons/common/CfgFunctions.hpp index fde194db6..c84cb7470 100644 --- a/addons/common/CfgFunctions.hpp +++ b/addons/common/CfgFunctions.hpp @@ -78,6 +78,7 @@ class CfgFunctions { PATHTO_FNC(addBinocularMagazine); PATHTO_FNC(removeBinocularMagazine); PATHTO_FNC(randomizeFacewear); + PATHTO_FNC(unitHasSpace); }; class Cargo { diff --git a/addons/common/fnc_unitHasSpace.sqf b/addons/common/fnc_unitHasSpace.sqf new file mode 100644 index 000000000..304953178 --- /dev/null +++ b/addons/common/fnc_unitHasSpace.sqf @@ -0,0 +1,129 @@ +#include "script_component.hpp" +/* ---------------------------------------------------------------------------- +Function: CBA_fnc_unitHasSpace + +Description: + Checks if unit has enough free space in inventory to store item. + + Doesn't take current unit load into account unlike canAdd command. + +Parameters: + _unit - Unit + _item - Item to store + _count - Item count (Default: 1) + _checkUniform - Check space in uniform (Default: true) + _checkVest - Check space in vest (Default: true) + _checkBackpack - Check space in backpack (Default: true) + +Returns: + True if unit has free space, false otherwise + +Examples: + (begin example) + [player, "acc_flashlight"] call CBA_fnc_unitHasSpace + [player, "30Rnd_556x45_Stanag", 7, false, true, false] call CBA_fnc_unitHasSpace + (end) + +Author: + Dystopian +---------------------------------------------------------------------------- */ +SCRIPT(unitHasSpace); + +params [ + "_unit", + "_item", + ["_count", 1], + ["_checkUniform", true], + ["_checkVest", true], + ["_checkBackpack", true] +]; + +#define TYPE_VEST 701 +#define TYPE_UNIFORM 801 +#define TYPE_BACKPACK 901 + +if (isNil QGVAR(itemMassAllowedSlots)) then { + LOG("create ns"); + GVAR(itemMassAllowedSlots) = [] call CBA_fnc_createNamespace; +}; + +private ["_mass", "_allowedSlots"]; +private _itemMassAllowedSlots = GVAR(itemMassAllowedSlots) getVariable _item; + +if (!isNil "_itemMassAllowedSlots") then { + _mass = _itemMassAllowedSlots # 0; + _allowedSlots = _itemMassAllowedSlots # 1; + TRACE_3("cache found",_item,_mass,_allowedSlots); +} else { + _allowedSlots = [TYPE_UNIFORM, TYPE_VEST, TYPE_BACKPACK]; + private _cfgWeaponsItem = configFile >> "CfgWeapons" >> _item; + if (isClass _cfgWeaponsItem) then { + private _cfgItemInfo = _cfgWeaponsItem >> "ItemInfo"; + private _cfgWeaponSlotsInfo = _cfgWeaponsItem >> "WeaponSlotsInfo"; + if (isNumber (_cfgItemInfo >> "mass")) then { + _mass = getNumber (_cfgItemInfo >> "mass"); + } else { + _mass = getNumber (_cfgWeaponSlotsInfo >> "mass"); + }; + + { + if (isArray _x) exitWith { + _allowedSlots = getArray _x; + }; + } forEach [ + _cfgWeaponsItem >> "allowedSlots", + _cfgItemInfo >> "allowedSlots", + _cfgWeaponSlotsInfo >> "allowedSlots" + ]; + } else { + private _cfgMagazinesItem = configFile >> "CfgMagazines" >> _item; + if (isClass _cfgMagazinesItem) then { + _mass = getNumber (_cfgMagazinesItem >> "mass"); + private _cfgAllowedSlots = _cfgMagazinesItem >> "allowedSlots"; + if (isArray _cfgAllowedSlots) then { + _allowedSlots = getArray _cfgAllowedSlots; + }; + } else { + private _cfgGlassesItem = configFile >> "CfgGlasses" >> _item; + if (isClass _cfgGlassesItem) then { + _mass = getNumber (_cfgGlassesItem >> "mass"); + } else { + _mass = -1; + _allowedSlots = []; + } + }; + }; + TRACE_3("caching",_item,_mass,_allowedSlots); + GVAR(itemMassAllowedSlots) setVariable [_item, [_mass, _allowedSlots]]; +}; + +if (_mass == -1) exitWith {false}; + +if ( + _checkUniform + && { + private _maxLoad = getContainerMaxLoad uniform _unit; + _maxLoad >= _mass * _count + _maxLoad * loadUniform _unit + } + && {TYPE_UNIFORM in _allowedSlots} +) exitWith {true}; + +if ( + _checkVest + && { + private _maxLoad = getContainerMaxLoad vest _unit; + _maxLoad >= _mass * _count + _maxLoad * loadVest _unit + } + && {TYPE_VEST in _allowedSlots} +) exitWith {true}; + +if ( + _checkBackpack + && { + private _maxLoad = getContainerMaxLoad backpack _unit; + _maxLoad >= _mass * _count + _maxLoad * loadBackpack _unit + } + && {TYPE_BACKPACK in _allowedSlots} +) exitWith {true}; + +false From 2afb01b9209b81e8a9202d49af6aa3e619a0b695 Mon Sep 17 00:00:00 2001 From: Dystopian Date: Sat, 2 May 2020 14:27:24 +0400 Subject: [PATCH 2/9] Optimize getVariable from cache --- addons/common/fnc_unitHasSpace.sqf | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/addons/common/fnc_unitHasSpace.sqf b/addons/common/fnc_unitHasSpace.sqf index 304953178..b6929b706 100644 --- a/addons/common/fnc_unitHasSpace.sqf +++ b/addons/common/fnc_unitHasSpace.sqf @@ -47,14 +47,9 @@ if (isNil QGVAR(itemMassAllowedSlots)) then { GVAR(itemMassAllowedSlots) = [] call CBA_fnc_createNamespace; }; -private ["_mass", "_allowedSlots"]; -private _itemMassAllowedSlots = GVAR(itemMassAllowedSlots) getVariable _item; - -if (!isNil "_itemMassAllowedSlots") then { - _mass = _itemMassAllowedSlots # 0; - _allowedSlots = _itemMassAllowedSlots # 1; - TRACE_3("cache found",_item,_mass,_allowedSlots); -} else { +(GVAR(itemMassAllowedSlots) getVariable [_item, []]) params ["_mass", "_allowedSlots"]; + +if (isNil "_mass") then { _allowedSlots = [TYPE_UNIFORM, TYPE_VEST, TYPE_BACKPACK]; private _cfgWeaponsItem = configFile >> "CfgWeapons" >> _item; if (isClass _cfgWeaponsItem) then { @@ -90,14 +85,14 @@ if (!isNil "_itemMassAllowedSlots") then { } else { _mass = -1; _allowedSlots = []; - } + }; }; }; TRACE_3("caching",_item,_mass,_allowedSlots); GVAR(itemMassAllowedSlots) setVariable [_item, [_mass, _allowedSlots]]; }; -if (_mass == -1) exitWith {false}; +if (_mass == -1) exitWith {false}; // item doesn't exist if ( _checkUniform From f161de349c4fe509271cef913ca9d8d5773a7d13 Mon Sep 17 00:00:00 2001 From: Dystopian Date: Sat, 2 May 2020 15:36:40 +0400 Subject: [PATCH 3/9] Improve allowedSlots check look --- addons/common/fnc_unitHasSpace.sqf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/addons/common/fnc_unitHasSpace.sqf b/addons/common/fnc_unitHasSpace.sqf index b6929b706..b1224fa03 100644 --- a/addons/common/fnc_unitHasSpace.sqf +++ b/addons/common/fnc_unitHasSpace.sqf @@ -96,29 +96,29 @@ if (_mass == -1) exitWith {false}; // item doesn't exist if ( _checkUniform + && {TYPE_UNIFORM in _allowedSlots} && { private _maxLoad = getContainerMaxLoad uniform _unit; _maxLoad >= _mass * _count + _maxLoad * loadUniform _unit } - && {TYPE_UNIFORM in _allowedSlots} ) exitWith {true}; if ( _checkVest + && {TYPE_VEST in _allowedSlots} && { private _maxLoad = getContainerMaxLoad vest _unit; _maxLoad >= _mass * _count + _maxLoad * loadVest _unit } - && {TYPE_VEST in _allowedSlots} ) exitWith {true}; if ( _checkBackpack + && {TYPE_BACKPACK in _allowedSlots} && { private _maxLoad = getContainerMaxLoad backpack _unit; _maxLoad >= _mass * _count + _maxLoad * loadBackpack _unit } - && {TYPE_BACKPACK in _allowedSlots} ) exitWith {true}; false From 910fdc69f002ee763927c34c7b23e9f44d93ce06 Mon Sep 17 00:00:00 2001 From: Dystopian Date: Sat, 2 May 2020 15:58:02 +0400 Subject: [PATCH 4/9] Use switch instead nested if --- addons/common/fnc_unitHasSpace.sqf | 58 +++++++++++++++--------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/addons/common/fnc_unitHasSpace.sqf b/addons/common/fnc_unitHasSpace.sqf index b1224fa03..7cf9ca1e1 100644 --- a/addons/common/fnc_unitHasSpace.sqf +++ b/addons/common/fnc_unitHasSpace.sqf @@ -43,7 +43,6 @@ params [ #define TYPE_BACKPACK 901 if (isNil QGVAR(itemMassAllowedSlots)) then { - LOG("create ns"); GVAR(itemMassAllowedSlots) = [] call CBA_fnc_createNamespace; }; @@ -52,40 +51,41 @@ if (isNil QGVAR(itemMassAllowedSlots)) then { if (isNil "_mass") then { _allowedSlots = [TYPE_UNIFORM, TYPE_VEST, TYPE_BACKPACK]; private _cfgWeaponsItem = configFile >> "CfgWeapons" >> _item; - if (isClass _cfgWeaponsItem) then { - private _cfgItemInfo = _cfgWeaponsItem >> "ItemInfo"; - private _cfgWeaponSlotsInfo = _cfgWeaponsItem >> "WeaponSlotsInfo"; - if (isNumber (_cfgItemInfo >> "mass")) then { - _mass = getNumber (_cfgItemInfo >> "mass"); - } else { - _mass = getNumber (_cfgWeaponSlotsInfo >> "mass"); - }; - - { - if (isArray _x) exitWith { - _allowedSlots = getArray _x; + private _cfgMagazinesItem = configFile >> "CfgMagazines" >> _item; + private _cfgGlassesItem = configFile >> "CfgGlasses" >> _item; + switch true do { + case (isClass _cfgWeaponsItem): { + private _cfgItemInfo = _cfgWeaponsItem >> "ItemInfo"; + private _cfgWeaponSlotsInfo = _cfgWeaponsItem >> "WeaponSlotsInfo"; + if (isNumber (_cfgItemInfo >> "mass")) then { + _mass = getNumber (_cfgItemInfo >> "mass"); + } else { + _mass = getNumber (_cfgWeaponSlotsInfo >> "mass"); }; - } forEach [ - _cfgWeaponsItem >> "allowedSlots", - _cfgItemInfo >> "allowedSlots", - _cfgWeaponSlotsInfo >> "allowedSlots" - ]; - } else { - private _cfgMagazinesItem = configFile >> "CfgMagazines" >> _item; - if (isClass _cfgMagazinesItem) then { + + { + if (isArray _x) exitWith { + _allowedSlots = getArray _x; + }; + } forEach [ + _cfgWeaponsItem >> "allowedSlots", + _cfgItemInfo >> "allowedSlots", + _cfgWeaponSlotsInfo >> "allowedSlots" + ]; + }; + case (isClass _cfgMagazinesItem): { _mass = getNumber (_cfgMagazinesItem >> "mass"); private _cfgAllowedSlots = _cfgMagazinesItem >> "allowedSlots"; if (isArray _cfgAllowedSlots) then { _allowedSlots = getArray _cfgAllowedSlots; }; - } else { - private _cfgGlassesItem = configFile >> "CfgGlasses" >> _item; - if (isClass _cfgGlassesItem) then { - _mass = getNumber (_cfgGlassesItem >> "mass"); - } else { - _mass = -1; - _allowedSlots = []; - }; + }; + case (isClass _cfgGlassesItem): { + _mass = getNumber (_cfgGlassesItem >> "mass"); + }; + default { + _mass = -1; + _allowedSlots = []; }; }; TRACE_3("caching",_item,_mass,_allowedSlots); From 527c5221b4c5de77c89c28ff058a2aba3b958bcd Mon Sep 17 00:00:00 2001 From: Dystopian Date: Sat, 2 May 2020 17:45:25 +0400 Subject: [PATCH 5/9] Add param checking --- addons/common/fnc_unitHasSpace.sqf | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/addons/common/fnc_unitHasSpace.sqf b/addons/common/fnc_unitHasSpace.sqf index 7cf9ca1e1..c8f7c7648 100644 --- a/addons/common/fnc_unitHasSpace.sqf +++ b/addons/common/fnc_unitHasSpace.sqf @@ -30,14 +30,16 @@ Author: SCRIPT(unitHasSpace); params [ - "_unit", - "_item", - ["_count", 1], - ["_checkUniform", true], - ["_checkVest", true], - ["_checkBackpack", true] + ["_unit", objNull, [objNull]], + ["_item", "", [""]], + ["_count", 1, [0]], + ["_checkUniform", true, [false]], + ["_checkVest", true, [false]], + ["_checkBackpack", true, [false]] ]; +if (_unit isEqualTo objNull || {_item isEqualTo ""}) exitWith {false}; + #define TYPE_VEST 701 #define TYPE_UNIFORM 801 #define TYPE_BACKPACK 901 From 8c846ec9149848c6ef5fc8ef9b35a1bb706a99ac Mon Sep 17 00:00:00 2001 From: Dystopian Date: Sat, 2 May 2020 17:52:10 +0400 Subject: [PATCH 6/9] Rename to canAddItem --- addons/common/CfgFunctions.hpp | 2 +- .../common/{fnc_unitHasSpace.sqf => fnc_canAddItem.sqf} | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) rename addons/common/{fnc_unitHasSpace.sqf => fnc_canAddItem.sqf} (96%) diff --git a/addons/common/CfgFunctions.hpp b/addons/common/CfgFunctions.hpp index c84cb7470..794c76817 100644 --- a/addons/common/CfgFunctions.hpp +++ b/addons/common/CfgFunctions.hpp @@ -78,7 +78,7 @@ class CfgFunctions { PATHTO_FNC(addBinocularMagazine); PATHTO_FNC(removeBinocularMagazine); PATHTO_FNC(randomizeFacewear); - PATHTO_FNC(unitHasSpace); + PATHTO_FNC(canAddItem); }; class Cargo { diff --git a/addons/common/fnc_unitHasSpace.sqf b/addons/common/fnc_canAddItem.sqf similarity index 96% rename from addons/common/fnc_unitHasSpace.sqf rename to addons/common/fnc_canAddItem.sqf index c8f7c7648..0609b0712 100644 --- a/addons/common/fnc_unitHasSpace.sqf +++ b/addons/common/fnc_canAddItem.sqf @@ -1,6 +1,6 @@ #include "script_component.hpp" /* ---------------------------------------------------------------------------- -Function: CBA_fnc_unitHasSpace +Function: CBA_fnc_canAddItem Description: Checks if unit has enough free space in inventory to store item. @@ -20,14 +20,14 @@ Returns: Examples: (begin example) - [player, "acc_flashlight"] call CBA_fnc_unitHasSpace - [player, "30Rnd_556x45_Stanag", 7, false, true, false] call CBA_fnc_unitHasSpace + [player, "acc_flashlight"] call CBA_fnc_canAddItem + [player, "30Rnd_556x45_Stanag", 7, false, true, false] call CBA_fnc_canAddItem (end) Author: Dystopian ---------------------------------------------------------------------------- */ -SCRIPT(unitHasSpace); +SCRIPT(canAddItem); params [ ["_unit", objNull, [objNull]], From 2861f84faa24ab2f872d70f7a8a137540d4b0958 Mon Sep 17 00:00:00 2001 From: Dystopian Date: Sat, 2 May 2020 18:47:24 +0400 Subject: [PATCH 7/9] Use isNull Co-authored-by: mharis001 <34453221+mharis001@users.noreply.github.com> --- addons/common/fnc_canAddItem.sqf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/common/fnc_canAddItem.sqf b/addons/common/fnc_canAddItem.sqf index 0609b0712..5839d7929 100644 --- a/addons/common/fnc_canAddItem.sqf +++ b/addons/common/fnc_canAddItem.sqf @@ -38,7 +38,7 @@ params [ ["_checkBackpack", true, [false]] ]; -if (_unit isEqualTo objNull || {_item isEqualTo ""}) exitWith {false}; +if (isNull _unit || {_item isEqualTo ""}) exitWith {false}; #define TYPE_VEST 701 #define TYPE_UNIFORM 801 From 3372fa5e4871fb5d636d8c98aab7fe873b2f049e Mon Sep 17 00:00:00 2001 From: Dystopian Date: Tue, 5 May 2020 21:37:40 +0400 Subject: [PATCH 8/9] Fix calculation for several containers --- addons/common/fnc_canAddItem.sqf | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/addons/common/fnc_canAddItem.sqf b/addons/common/fnc_canAddItem.sqf index 5839d7929..ba79859cf 100644 --- a/addons/common/fnc_canAddItem.sqf +++ b/addons/common/fnc_canAddItem.sqf @@ -100,8 +100,9 @@ if ( _checkUniform && {TYPE_UNIFORM in _allowedSlots} && { - private _maxLoad = getContainerMaxLoad uniform _unit; - _maxLoad >= _mass * _count + _maxLoad * loadUniform _unit + // 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}; @@ -109,8 +110,8 @@ if ( _checkVest && {TYPE_VEST in _allowedSlots} && { - private _maxLoad = getContainerMaxLoad vest _unit; - _maxLoad >= _mass * _count + _maxLoad * loadVest _unit + _count = _count - floor (getContainerMaxLoad vest _unit * (1 - loadVest _unit) / _mass); + _count <= 0 } ) exitWith {true}; @@ -118,8 +119,8 @@ if ( _checkBackpack && {TYPE_BACKPACK in _allowedSlots} && { - private _maxLoad = getContainerMaxLoad backpack _unit; - _maxLoad >= _mass * _count + _maxLoad * loadBackpack _unit + _count = _count - floor (getContainerMaxLoad backpack _unit * (1 - loadBackpack _unit) / _mass); + _count <= 0 } ) exitWith {true}; From 13ca845f57973b67b8280a036365163cd58e06cc Mon Sep 17 00:00:00 2001 From: Dystopian Date: Thu, 7 May 2020 20:27:03 +0400 Subject: [PATCH 9/9] Handle item zero mass --- addons/common/fnc_canAddItem.sqf | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/addons/common/fnc_canAddItem.sqf b/addons/common/fnc_canAddItem.sqf index ba79859cf..ee8b0587e 100644 --- a/addons/common/fnc_canAddItem.sqf +++ b/addons/common/fnc_canAddItem.sqf @@ -100,9 +100,12 @@ if ( _checkUniform && {TYPE_UNIFORM in _allowedSlots} && { - // 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 + _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}; @@ -110,8 +113,11 @@ if ( _checkVest && {TYPE_VEST in _allowedSlots} && { - _count = _count - floor (getContainerMaxLoad vest _unit * (1 - loadVest _unit) / _mass); - _count <= 0 + _mass == 0 + || { + _count = _count - floor (getContainerMaxLoad vest _unit * (1 - loadVest _unit) / _mass); + _count <= 0 + } } ) exitWith {true}; @@ -119,8 +125,11 @@ if ( _checkBackpack && {TYPE_BACKPACK in _allowedSlots} && { - _count = _count - floor (getContainerMaxLoad backpack _unit * (1 - loadBackpack _unit) / _mass); - _count <= 0 + _mass == 0 + || { + _count = _count - floor (getContainerMaxLoad backpack _unit * (1 - loadBackpack _unit) / _mass); + _count <= 0 + } } ) exitWith {true};