Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disposable - Updated cba_disposable_fnc_replaceMagazineCargo #1662

Merged
17 changes: 10 additions & 7 deletions addons/disposable/XEH_preInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ GVAR(NormalLaunchers) = [] call CBA_fnc_createNamespace;
GVAR(LoadedLaunchers) = [] call CBA_fnc_createNamespace;
GVAR(UsedLaunchers) = [] call CBA_fnc_createNamespace;
GVAR(magazines) = [];
GVAR(BackpackLaunchers) = createHashMap;
GVAR(allowedSlotsLaunchers) = createHashMap;
GVAR(MagazineLaunchers) = [] call CBA_fnc_createNamespace;

private _cfgWeapons = configFile >> "CfgWeapons";
Expand Down Expand Up @@ -59,7 +59,8 @@ private _cfgMagazines = configFile >> "CfgMagazines";
continue;
};

private _fitsInBackpacks = TYPE_BACKPACK in getArray (configFile >> "CfgWeapons" >> _loadedLauncher >> "WeaponSlotsInfo" >> "allowedSlots");
private _configLoadedLauncher = _cfgWeapons >> _loadedLauncher;
_loadedLauncher = configName _configLoadedLauncher;

GVAR(LoadedLaunchers) setVariable [_launcher, _loadedLauncher];
GVAR(UsedLaunchers) setVariable [_launcher, _usedLauncher];
Expand All @@ -72,14 +73,12 @@ private _cfgMagazines = configFile >> "CfgMagazines";
GVAR(MagazineLaunchers) setVariable [_magazine, _loadedLauncher];
};

if (_fitsInBackpacks) then {
GVAR(BackpackLaunchers) set [_loadedLauncher, true];
};
GVAR(allowedSlotsLaunchers) set [_loadedLauncher, getArray (_configLoadedLauncher >> "WeaponSlotsInfo" >> "allowedSlots")];

// check if mass entries add up
private _massLauncher = getNumber (_cfgWeapons >> _launcher >> "WeaponSlotsInfo" >> "mass");
private _massMagazine = getNumber (_cfgMagazines >> _magazine >> "mass");
private _massLoadedLauncher = getNumber (_cfgWeapons >> _loadedLauncher >> "WeaponSlotsInfo" >> "mass");
private _massLoadedLauncher = getNumber (_configLoadedLauncher >> "WeaponSlotsInfo" >> "mass");
private _massUsedLauncher = getNumber (_cfgWeapons >> _usedLauncher >> "WeaponSlotsInfo" >> "mass");

if (_massLauncher != _massUsedLauncher) then {
Expand All @@ -92,7 +91,11 @@ private _cfgMagazines = configFile >> "CfgMagazines";
} forEach configProperties [configFile >> "CBA_DisposableLaunchers", "isArray _x"];

["CBA_settingsInitialized", {
["All", "InitPost", {call FUNC(replaceMagazineCargo)}, nil, nil, true] call CBA_fnc_addClassEventHandler;
["All", "InitPost", {
params ["_object"];

[typeOf _object, _object] call FUNC(replaceMagazineCargo);
}, nil, nil, true] call CBA_fnc_addClassEventHandler;
}] call CBA_fnc_addEventHandler;

ADDON = true;
63 changes: 32 additions & 31 deletions addons/disposable/fnc_replaceMagazineCargo.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,64 @@ Description:
Replaces disposable launcher magazines with loaded disposable launchers.

Parameters:
_box - Any object with cargo <OBJECT>
_containerType - typeOf _container <STRING>
_container - Any object with cargo <OBJECT>

Returns:
Nothing.

Examples:
(begin example)
_box call cba_disposable_fnc_replaceMagazineCargo
[typeOf _container, _container] call cba_disposable_fnc_replaceMagazineCargo
(end)

Author:
commy2
commy2, johnb43
---------------------------------------------------------------------------- */

if (!GVAR(replaceDisposableLauncher)) exitWith {};

params ["_box"];
if (!local _box) exitWith {};
if (missionNamespace getVariable [QGVAR(disableMagazineReplacement), false]) exitWith {};

private _uniformContainer = uniformContainer _box;
if (!isNull _uniformContainer) then {
_uniformContainer call FUNC(replaceMagazineCargo);
};
params ["_containerType", "_container"];

private _vestContainer = vestContainer _box;
if (!isNull _vestContainer) then {
_vestContainer call FUNC(replaceMagazineCargo);
};
if (!local _container) exitWith {};

private _backpackContainer = backpackContainer _box;
if (!isNull _backpackContainer) then {
_backpackContainer call FUNC(replaceMagazineCargo);
private _containers = everyContainer _container;

if (_container isKindOf "CAManBase") then {
_containers append ([
[uniform _container, uniformContainer _container],
[vest _container, vestContainer _container],
[backpack _container, backpackContainer _container]
] select {!isNull (_x select 1)});
};

// Replace all magazines recursively
{
_x call FUNC(replaceMagazineCargo);
} forEach everyBackpack _box;
} forEach _containers;

if (magazineCargo _box arrayIntersect GVAR(magazines) isEqualTo []) exitWith {};
private _magazines = (magazineCargo _container) select {_x in GVAR(magazines)};

private _magazines = magazinesAmmoCargo _box;
clearMagazineCargoGlobal _box;
if (_magazines isEqualTo []) exitWith {};

private _isBackpack = getNumber (configOf _box >> "isBackpack") == 1;
// Check if a uniform, vest, backpack or something else entirely
_containerType = if (getNumber (configOf _container >> "isBackpack") == 1) then {
TYPE_BACKPACK
} else {
// If uniform or vest, this config will be defined, otherwise it will default to 0
getNumber (configFile >> "CfgWeapons" >> _containerType >> "ItemInfo" >> "type")
};

// Replace magazines with disposable launchers
{
_x params ["_magazine", "_ammo"];
_container addMagazineCargoGlobal [_x, -1];

if (_magazine in GVAR(magazines)) then {
private _loadedLauncher = GVAR(MagazineLaunchers) getVariable _magazine;
private _loadedLauncher = GVAR(MagazineLaunchers) getVariable _x;

// As addWeaponCargoGlobal ignores allowedSlots, check here if launcher is allowed to be placed in a backpack
if (!_isBackpack || {_loadedLauncher in GVAR(BackpackLaunchers)}) then {
_box addWeaponCargoGlobal [_loadedLauncher, 1];
};
} else {
_box addMagazineAmmoCargo [_magazine, 1, _ammo];
// Slot restrictions only apply if uniform, vest or backpack
// If slot restrictions apply, remove magazine but don't add weapon
if (_containerType == 0 || {_containerType in (GVAR(allowedSlotsLaunchers) getOrDefault [_loadedLauncher, []])}) then {
_container addWeaponCargoGlobal [_loadedLauncher, 1];
};
} forEach _magazines;