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

Gunbag - Optimise weapon taking/storing code #10053

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions addons/gunbag/functions/fnc_offGunbagCallback.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* None
*
* Example:
* [player, target] call ace_gunbag_fnc_offGunbagCallback
* [player, cursorObject] call ace_gunbag_fnc_offGunbagCallback
*
* Public: No
*/
Expand All @@ -23,39 +23,28 @@ private _gunbag = backpackContainer _target;
private _state = _gunbag getVariable [QGVAR(gunbagWeapon), []];

if (_state isEqualTo []) exitWith {
[localize LSTRING(empty)] call EFUNC(common,displayTextStructured);
[LLSTRING(empty)] call EFUNC(common,displayTextStructured);
};

_state params ["_weapon", "_items", "_magazines"];

_unit addWeapon _weapon;
[_unit, _weapon, true, _magazines] call EFUNC(common,addWeapon);

// Game will auto add magazines from player's inventory, put these back in player inventory as they will be overwritten
([_unit, _weapon] call EFUNC(common,getWeaponState)) params ["", "", "_addedMags", "_addedAmmo"];
// Add attachments
{
if (((_x select 0) != "") && {(_addedMags select _forEachIndex) != ""}) then {
TRACE_2("Re-adding mag",_x,_addedMags select _forEachIndex);
_unit addMagazine [_addedMags select _forEachIndex, _addedAmmo select _forEachIndex];
};
} forEach _magazines;

removeAllPrimaryWeaponItems _unit;

{
_unit addWeaponItem [_weapon, _x];
} forEach (_items + _magazines);
_unit addWeaponItem [_weapon, _x, true];
} forEach (_items select {_x != ""});

_unit selectWeapon _weapon;

_magazines = _magazines apply {_x select 0};
private _mass = [_weapon, _items, _magazines apply {_x select 0}] call FUNC(calculateMass);

private _mass = [_weapon, _items, _magazines] call FUNC(calculateMass);

// remove virtual load
// Remove virtual load
[_target, _gunbag, -_mass] call EFUNC(movement,addLoadToUnitContainer);

_gunbag setVariable [QGVAR(gunbagWeapon), [], true];

// play sound
// Play sound
if (["ace_backpacks"] call EFUNC(common,isModLoaded)) then {
[_target, _gunbag] call EFUNC(backpacks,backpackOpened);
};
63 changes: 23 additions & 40 deletions addons/gunbag/functions/fnc_swapGunbagCallback.sqf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "..\script_component.hpp"
/*
* Author: Ir0n1E and mjc4wilton
* Author: Ir0n1E, mjc4wilton
* Swap primary weapon and weapon in gunbag.
*
* Arguments:
Expand All @@ -11,66 +11,49 @@
* None
*
* Example:
* [player, target] call ace_gunbag_fnc_swapGunbagCallback
* [player, cursorObject] call ace_gunbag_fnc_swapGunbagCallback
*
* Public: No
*/

params ["_unit", "_target"];
private _currentWeapon = primaryWeapon _unit; //Get Current Weapon
private _gunbag = backpackContainer _target;


//---Set up current weapon for storing
private _currentWeaponState = [_unit, _currentWeapon] call EFUNC(common,getWeaponState); //Gets weapon attachments

/*
* example return value _state
* [["","","optic_Aco",""],["arifle_MX_GL_ACO_F","GL_3GL_F"],["30Rnd_65x39_caseless_mag","1Rnd_HE_Grenade_shell"],[30,1]]
*/
// Set up current weapon for storing
private _gunbag = backpackContainer _target;
private _currentItems = (getUnitLoadout _unit) select 0;

_currentWeaponState params ["_currentWeaponItems", "", "_currentWeaponMagazines", "_currentWeaponAmmo"]; //Extract Weapon Attachments to separate arrays
private _currentMagazines = _currentItems select [4, 2];
_currentItems deleteRange [4, 2];

private _currentWeaponMass = [_currentWeapon, _currentWeaponItems, _currentWeaponMagazines] call FUNC(calculateMass);
private _currentWeapon = _currentItems deleteAt 0;

{
_currentWeaponMagazines set [_forEachIndex, [_x, _currentWeaponAmmo select _forEachIndex]];
} forEach _currentWeaponMagazines;
private _currentMass = [_currentWeapon, _currentItems, _currentMagazines apply {_x select 0}] call FUNC(calculateMass);

//---Set up weapon in gunbag
private _newWeaponState = _gunbag getVariable [QGVAR(gunbagWeapon), []];
// Set up weapon in gunbag
private _newState = _gunbag getVariable [QGVAR(gunbagWeapon), []];

if (_newWeaponState isEqualTo []) exitWith {
if (_newState isEqualTo []) exitWith {
[LLSTRING(empty)] call EFUNC(common,displayTextStructured);
};

_newWeaponState params ["_newWeapon", "_newWeaponItems", "_newWeaponMagazines"];
_newState params ["_newWeapon", "_newItems", "_newMagazines"];

//---Swap Weapons
// Swap Weapons
_unit removeWeapon _currentWeapon;
_unit addWeapon _newWeapon;

// Game will auto add magazines from player's inventory, put these back in player inventory as they will be overwritten
([_unit, _newWeapon] call EFUNC(common,getWeaponState)) params ["", "", "_addedMags", "_addedAmmo"];
{
if (((_x select 0) != "") && {(_addedMags select _forEachIndex) != ""}) then {
TRACE_2("Re-adding mag",_x,_addedMags select _forEachIndex);
_unit addMagazine [_addedMags select _forEachIndex, _addedAmmo select _forEachIndex];
};
} forEach _newWeaponMagazines;

removeAllPrimaryWeaponItems _unit;
[_unit, _newWeapon, true, _newMagazines] call EFUNC(common,addWeapon);

// Add attachments
{
_unit addWeaponItem [_newWeapon, _x];
} forEach (_newWeaponItems + _newWeaponMagazines);
_unit addWeaponItem [_newWeapon, _x, true];
} forEach (_newItems select {_x != ""});

_unit selectWeapon _newWeapon;

_newWeaponMagazines = _newWeaponMagazines apply {_x select 0};
private _newMass = [_newWeapon, _newItems, _newMagazines apply {_x select 0}] call FUNC(calculateMass);

private _newWeaponMass = [_newWeapon, _newWeaponItems, _newWeaponMagazines] call FUNC(calculateMass);
// Update virtual load
[_target, _gunbag, _currentMass - _newMass] call EFUNC(movement,addLoadToUnitContainer);

// update virtual load
[_target, _gunbag, _currentWeaponMass - _newWeaponMass] call EFUNC(movement,addLoadToUnitContainer);
_gunbag setVariable [QGVAR(gunbagWeapon), [_currentWeapon, _currentWeaponItems, _currentWeaponMagazines], true]; //Replace weapon in gunbag
// Replace weapon in gunbag
_gunbag setVariable [QGVAR(gunbagWeapon), [_currentWeapon, _currentItems, _currentMagazines], true];
26 changes: 10 additions & 16 deletions addons/gunbag/functions/fnc_toGunbagCallback.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,32 @@
* None
*
* Example:
* [player, target] call ace_gunbag_fnc_toGunbagCallback
* [player, cursorObject] call ace_gunbag_fnc_toGunbagCallback
*
* Public: No
*/

params ["_unit", "_target"];

private _weapon = primaryWeapon _unit;
// Set up current weapon for storing
private _gunbag = backpackContainer _target;
private _items = (getUnitLoadout _unit) select 0;

private _state = [_unit, _weapon] call EFUNC(common,getWeaponState);
private _magazines = _items select [4, 2];
_items deleteRange [4, 2];

/*
* example return value _state
* [["","","optic_Aco",""],["arifle_MX_GL_ACO_F","GL_3GL_F"],["30Rnd_65x39_caseless_mag","1Rnd_HE_Grenade_shell"],[30,1]]
*/

_state params ["_items", "", "_magazines", "_ammo"];
private _weapon = _items deleteAt 0;

private _mass = [_weapon, _items, _magazines] call FUNC(calculateMass);

{
_magazines set [_forEachIndex, [_x, _ammo select _forEachIndex]];
} forEach _magazines;
private _mass = [_weapon, _items, _magazines apply {_x select 0}] call FUNC(calculateMass);

_unit removeWeapon _weapon;

// add virtual load
// Add virtual load
[_target, _gunbag, _mass] call EFUNC(movement,addLoadToUnitContainer);

_gunbag setVariable [QGVAR(gunbagWeapon), [_weapon, _items, _magazines], true];

// play sound
// Play sound
if (["ace_backpacks"] call EFUNC(common,isModLoaded)) then {
[_target, _gunbag] call EFUNC(backpacks,backpackOpened);
};