From 2bad2fac68214b4f8a4450489ec17622dc1a6597 Mon Sep 17 00:00:00 2001 From: mharis001 <34453221+mharis001@users.noreply.github.com> Date: Wed, 11 Dec 2019 12:53:34 -0500 Subject: [PATCH] Improve dragging getWeight function (#7261) --- addons/dragging/functions/fnc_getWeight.sqf | 104 +++++++++++++++----- addons/dragging/script_component.hpp | 1 - 2 files changed, 77 insertions(+), 28 deletions(-) diff --git a/addons/dragging/functions/fnc_getWeight.sqf b/addons/dragging/functions/fnc_getWeight.sqf index cd06b8a884..c9d978cf7d 100644 --- a/addons/dragging/functions/fnc_getWeight.sqf +++ b/addons/dragging/functions/fnc_getWeight.sqf @@ -1,45 +1,95 @@ #include "script_component.hpp" /* - * Author: L-H, edited by commy2, rewritten by joko // Jonas - * Returns the weight of a crate. + * Author: L-H, edited by commy2, rewritten by joko // Jonas, re-rewritten by mharis001 + * Returns the weight of the given object. + * Weight is calculated from the object's mass and its current inventory. * * Arguments: - * 0: Crate to get weight of + * 0: Object * * Return Value: - * Total Weight + * Weight * * Example: - * [Crate1] call ace_dragging_fnc_getweight; + * [_object] call ace_dragging_fnc_getWeight * * Public: No -*/ + */ params ["_object"]; -// Initialize the total weight. -private _totalWeight = 0; +private _weight = 0; +private _cfgWeapons = configFile >> "CfgWeapons"; +private _cfgGlasses = configFile >> "CfgGlasses"; +private _cfgVehicles = configFile >> "CfgVehicles"; +private _cfgMagazines = configFile >> "CfgMagazines"; + +// Add the masses of "regular" items in the object's inventory +// Handle separating CfgGlasses items from this cargo array +getItemCargo _object params ["_itemTypes", "_itemCounts"]; -// Cycle through all item types with their assigned config paths. { - _x params ["_items", "_getConfigCode"]; - _items params ["_item", "_count"]; - // Cycle through all items and read their mass out of the config. + private _itemConfig = if (isClass (_cfgGlasses >> _x)) then { + _cfgGlasses >> _x + } else { + _cfgWeapons >> _x >> "ItemInfo" + }; + + _weight = _weight + getNumber (_itemConfig >> "mass") * (_itemCounts select _forEachIndex); +} forEach _itemTypes; + +// Add the masses of magazine items in the object's inventory +getMagazineCargo _object params ["_magazineTypes", "_magazineCounts"]; + +{ + _weight = _weight + getNumber (_cfgMagazines >> _x >> "mass") * (_magazineCounts select _forEachIndex); +} forEach _magazineTypes; + +// Add the masses of backpack items in the object's inventory +getBackpackCargo _object params ["_backpackTypes", "_backpackCounts"]; + +{ + _weight = _weight + getNumber (_cfgVehicles >> _x >> "mass") * (_backpackCounts select _forEachIndex); +} forEach _backpackTypes; + +{ + _x params ["_weapon", "_muzzle", "_pointer", "_optic", "_primaryMagazine", "_secondaryMagazine", "_bipod"]; + + // Add the weapon's mass + _weight = _weight + getNumber (_cfgWeapons >> _weapon >> "WeaponSlotsInfo" >> "mass"); + + // Add the masses of the weapon's attachments if they exist { - // Multiply mass with amount of items and add the mass to the total weight. - _totalWeight = _totalWeight + (getNumber ((call _getConfigCode) >> "mass") * (_count select _forEachIndex)); - } forEach _item; - true -} count [ - //IGNORE_PRIVATE_WARNING ["_x"]; - [getMagazineCargo _object, {configFile >> "CfgMagazines" >> _x}], - [getBackpackCargo _object, {configFile >> "CfgVehicles" >> _x}], - [getItemCargo _object, {configFile >> "CfgWeapons" >> _x >> "ItemInfo"}], - [getWeaponCargo _object, {configFile >> "CfgWeapons" >> _x >> "WeaponSlotsInfo"}] -]; + if (_x != "") then { + _weight = _weight + getNumber (_cfgWeapons >> _x >> "ItemInfo" >> "mass"); + }; + } forEach [_muzzle, _pointer, _optic, _bipod]; -// add Weight of create to totalWeight -_totalWeight = _totalWeight + (getNumber (configFile >> "CfgVehicles" >> typeOf _object >> "mass")); + // Add the masses of the weapon's magazines if they exist + { + _x params ["_magazine"]; -// Mass in Arma isn't an exact amount but rather a volume/weight value. This attempts to work around that by making it a usable value. (sort of). -_totalWeight * 0.5 + if (!isNil "_magazine") then { + _weight = _weight + getNumber (_cfgMagazines >> _magazine >> "mass"); + }; + } forEach [_primaryMagazine, _secondaryMagazine]; +} forEach weaponsItemsCargo _object; + +// Add the mass of the object itself +// The mass of sub-containers such as vests was added through the items cargo +// The container object is generally of type SupplyX and has mass of zero +_weight = _weight + getNumber (_cfgVehicles >> typeOf _object >> "mass"); + +// Mass in Arma isn't an exact amount but rather a volume/weight value +// This attempts to work around that by making it a usable value (sort of) +// Note: this is done before the recursive calls to avoid reducing the weight multiple times +_weight = _weight * 0.5; + +// Handle sub-containers within the object's inventory +{ + _x params ["", "_container"]; + + _weight = _weight + (_container call FUNC(getWeight)); +} forEach everyContainer _object; + +_weight diff --git a/addons/dragging/script_component.hpp b/addons/dragging/script_component.hpp index 01b466972f..e7ad88c825 100644 --- a/addons/dragging/script_component.hpp +++ b/addons/dragging/script_component.hpp @@ -2,7 +2,6 @@ #define COMPONENT_BEAUTIFIED Dragging #include "\z\ace\addons\main\script_mod.hpp" -// #define DEBUG_ENABLED_DRAGGING // #define DEBUG_MODE_FULL // #define DISABLE_COMPILE_CACHE // #define ENABLE_PERFORMANCE_COUNTERS