Last active
July 5, 2021 16:43
-
-
Save Seb105/de6484d434a874c8fb829cb39658b465 to your computer and use it in GitHub Desktop.
Arma Weapon Swapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
isNil { | |
// Build list of all weapons and their compatible accessories | |
private _allWeapons = (([configFile >> "CfgWeapons", 0] call BIS_fnc_returnChildren) select { | |
getNumber (_x >> "scope") == 2 | |
&& (configName _x) isKindOf ["RifleCore", configFile >> "CfgWeapons"]; | |
}) apply { | |
_configName = configName _x; | |
[ | |
_configName, | |
[_configName, true] call CBA_fnc_compatibleMagazines, | |
_configName call CBA_fnc_compatibleItems | |
]; | |
}; | |
// Build list of all unique guns, and all used accessories for each gun. | |
private _uniquePrimaryWeapons = createHashMap; | |
{ | |
private _unit = _x; | |
private _unitPrimary = primaryWeapon _unit; | |
private _unitPrimaryMags = primaryWeaponMagazine _unit; | |
private _unitPrimaryAccessories = (primaryWeaponItems _unit) select {_x != ""}; | |
if (_unitPrimary == "") then {continue}; | |
if (_unitPrimary in _uniquePrimaryWeapons) then { | |
private _array = _uniquePrimaryWeapons get _unitPrimary; | |
{ | |
_array#0 pushBackUnique _x; | |
} forEach _unitPrimaryMags; | |
{ | |
_array#1 pushBackUnique _x; | |
} forEach _unitPrimaryAccessories; | |
} else { | |
_uniquePrimaryWeapons set [ | |
_unitPrimary, | |
[ | |
_unitPrimaryMags, | |
_unitPrimaryAccessories | |
] | |
] | |
} | |
} forEach playableUnits; | |
// Build zen_dialog "COMBO" for each unique weapon. | |
private _uiArray = []; | |
{ | |
private _baseWeapon = _x; | |
_y params ["_baseMags", "_baseAccessories"]; | |
private _compatibleWeapons = []; | |
{ | |
_x params ["_weapon", "_compatibleMagazines", "_compatibleAccessories"]; | |
// If this weapon can fit all magazines and accessories for the original gun then it is suitable. | |
if ( | |
{_compatibleMagazines find _x != -1} count _baseMags == count _baseMags | |
&& {_compatibleAccessories find _x != -1} count _baseAccessories == count _baseAccessories | |
) then { | |
_compatibleWeapons pushBackUnique _weapon; | |
} | |
} forEach _allWeapons; | |
private _toolTip = format [ | |
"%1 ->", | |
getText (configFile >> "CfgWeapons" >> _baseWeapon >> "displayName") | |
]; | |
private _index = _compatibleWeapons find _baseWeapon; | |
private _prettyNames = _compatibleWeapons apply { | |
[ | |
getText (configFile >> "CfgWeapons" >> _x >> "displayName"), | |
_x, | |
getText (configFile >> "CfgWeapons" >> _x >> "picture") | |
] | |
}; | |
// Used to build zen dialog | |
_uiArray pushBack [ | |
"COMBO", | |
_toolTip, | |
[_compatibleWeapons, _prettyNames, _index] | |
]; | |
} forEach _uniquePrimaryWeapons; | |
// Create zen dialog | |
[ | |
"Weapon Swapper", | |
_uiArray, | |
{ | |
params ["_dialog_data","_uniquePrimaryWeapons"]; | |
private _replacements = createHashMap; | |
{ | |
private _oldWeapon = _x; | |
private _newWeapon = (_dialog_data#_forEachIndex); | |
_replacements set [_oldWeapon, _newWeapon]; | |
} forEach _uniquePrimaryWeapons; | |
{ | |
private _unit = _x; | |
private _loadout = getUnitLoadout _unit; | |
private _currentWeapon = primaryWeapon _unit; | |
if (_currentWeapon == "") then {continue}; | |
private _newWeapon = _replacements getOrDefault [_currentWeapon, _currentWeapon]; | |
_loadout#0 set [0, _newWeapon]; | |
_unit setUnitLoadout _loadout; | |
} forEach playableUnits; | |
save3DENInventory playableUnits; | |
}, | |
{}, | |
_uniquePrimaryWeapons | |
] call zen_dialog_fnc_create; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment