Gunbag - Optimise weapon taking/storing code (#10053)

Optimise gunbag code
This commit is contained in:
johnb432 2024-06-05 15:22:19 +02:00 committed by GitHub
parent 06f47e600d
commit 738a32dba9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 78 deletions

View File

@ -11,7 +11,7 @@
* None * None
* *
* Example: * Example:
* [player, target] call ace_gunbag_fnc_offGunbagCallback * [player, cursorObject] call ace_gunbag_fnc_offGunbagCallback
* *
* Public: No * Public: No
*/ */
@ -23,39 +23,28 @@ private _gunbag = backpackContainer _target;
private _state = _gunbag getVariable [QGVAR(gunbagWeapon), []]; private _state = _gunbag getVariable [QGVAR(gunbagWeapon), []];
if (_state isEqualTo []) exitWith { if (_state isEqualTo []) exitWith {
[localize LSTRING(empty)] call EFUNC(common,displayTextStructured); [LLSTRING(empty)] call EFUNC(common,displayTextStructured);
}; };
_state params ["_weapon", "_items", "_magazines"]; _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 // Add attachments
([_unit, _weapon] call EFUNC(common,getWeaponState)) params ["", "", "_addedMags", "_addedAmmo"];
{ {
if (((_x select 0) != "") && {(_addedMags select _forEachIndex) != ""}) then { _unit addWeaponItem [_weapon, _x, true];
TRACE_2("Re-adding mag",_x,_addedMags select _forEachIndex); } forEach (_items select {_x != ""});
_unit addMagazine [_addedMags select _forEachIndex, _addedAmmo select _forEachIndex];
};
} forEach _magazines;
removeAllPrimaryWeaponItems _unit;
{
_unit addWeaponItem [_weapon, _x];
} forEach (_items + _magazines);
_unit selectWeapon _weapon; _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); [_target, _gunbag, -_mass] call EFUNC(movement,addLoadToUnitContainer);
_gunbag setVariable [QGVAR(gunbagWeapon), [], true]; _gunbag setVariable [QGVAR(gunbagWeapon), [], true];
// play sound // Play sound
if (["ace_backpacks"] call EFUNC(common,isModLoaded)) then { if (["ace_backpacks"] call EFUNC(common,isModLoaded)) then {
[_target, _gunbag] call EFUNC(backpacks,backpackOpened); [_target, _gunbag] call EFUNC(backpacks,backpackOpened);
}; };

View File

@ -1,6 +1,6 @@
#include "..\script_component.hpp" #include "..\script_component.hpp"
/* /*
* Author: Ir0n1E and mjc4wilton * Author: Ir0n1E, mjc4wilton
* Swap primary weapon and weapon in gunbag. * Swap primary weapon and weapon in gunbag.
* *
* Arguments: * Arguments:
@ -11,66 +11,49 @@
* None * None
* *
* Example: * Example:
* [player, target] call ace_gunbag_fnc_swapGunbagCallback * [player, cursorObject] call ace_gunbag_fnc_swapGunbagCallback
* *
* Public: No * Public: No
*/ */
params ["_unit", "_target"]; params ["_unit", "_target"];
private _currentWeapon = primaryWeapon _unit; //Get Current Weapon
// Set up current weapon for storing
private _gunbag = backpackContainer _target; private _gunbag = backpackContainer _target;
private _currentItems = (getUnitLoadout _unit) select 0;
private _currentMagazines = _currentItems select [4, 2];
_currentItems deleteRange [4, 2];
//---Set up current weapon for storing private _currentWeapon = _currentItems deleteAt 0;
private _currentWeaponState = [_unit, _currentWeapon] call EFUNC(common,getWeaponState); //Gets weapon attachments
/* private _currentMass = [_currentWeapon, _currentItems, _currentMagazines apply {_x select 0}] call FUNC(calculateMass);
* example return value _state
* [["","","optic_Aco",""],["arifle_MX_GL_ACO_F","GL_3GL_F"],["30Rnd_65x39_caseless_mag","1Rnd_HE_Grenade_shell"],[30,1]]
*/
_currentWeaponState params ["_currentWeaponItems", "", "_currentWeaponMagazines", "_currentWeaponAmmo"]; //Extract Weapon Attachments to separate arrays // Set up weapon in gunbag
private _newState = _gunbag getVariable [QGVAR(gunbagWeapon), []];
private _currentWeaponMass = [_currentWeapon, _currentWeaponItems, _currentWeaponMagazines] call FUNC(calculateMass); if (_newState isEqualTo []) exitWith {
{
_currentWeaponMagazines set [_forEachIndex, [_x, _currentWeaponAmmo select _forEachIndex]];
} forEach _currentWeaponMagazines;
//---Set up weapon in gunbag
private _newWeaponState = _gunbag getVariable [QGVAR(gunbagWeapon), []];
if (_newWeaponState isEqualTo []) exitWith {
[LLSTRING(empty)] call EFUNC(common,displayTextStructured); [LLSTRING(empty)] call EFUNC(common,displayTextStructured);
}; };
_newWeaponState params ["_newWeapon", "_newWeaponItems", "_newWeaponMagazines"]; _newState params ["_newWeapon", "_newItems", "_newMagazines"];
//---Swap Weapons // Swap Weapons
_unit removeWeapon _currentWeapon; _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, true, _newMagazines] call EFUNC(common,addWeapon);
([_unit, _newWeapon] call EFUNC(common,getWeaponState)) params ["", "", "_addedMags", "_addedAmmo"];
// Add attachments
{ {
if (((_x select 0) != "") && {(_addedMags select _forEachIndex) != ""}) then { _unit addWeaponItem [_newWeapon, _x, true];
TRACE_2("Re-adding mag",_x,_addedMags select _forEachIndex); } forEach (_newItems select {_x != ""});
_unit addMagazine [_addedMags select _forEachIndex, _addedAmmo select _forEachIndex];
};
} forEach _newWeaponMagazines;
removeAllPrimaryWeaponItems _unit;
{
_unit addWeaponItem [_newWeapon, _x];
} forEach (_newWeaponItems + _newWeaponMagazines);
_unit selectWeapon _newWeapon; _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 // Replace weapon in gunbag
[_target, _gunbag, _currentWeaponMass - _newWeaponMass] call EFUNC(movement,addLoadToUnitContainer); _gunbag setVariable [QGVAR(gunbagWeapon), [_currentWeapon, _currentItems, _currentMagazines], true];
_gunbag setVariable [QGVAR(gunbagWeapon), [_currentWeapon, _currentWeaponItems, _currentWeaponMagazines], true]; //Replace weapon in gunbag

View File

@ -11,38 +11,32 @@
* None * None
* *
* Example: * Example:
* [player, target] call ace_gunbag_fnc_toGunbagCallback * [player, cursorObject] call ace_gunbag_fnc_toGunbagCallback
* *
* Public: No * Public: No
*/ */
params ["_unit", "_target"]; params ["_unit", "_target"];
private _weapon = primaryWeapon _unit; // Set up current weapon for storing
private _gunbag = backpackContainer _target; 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];
/* private _weapon = _items deleteAt 0;
* 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 _mass = [_weapon, _items, _magazines apply {_x select 0}] call FUNC(calculateMass);
private _mass = [_weapon, _items, _magazines] call FUNC(calculateMass);
{
_magazines set [_forEachIndex, [_x, _ammo select _forEachIndex]];
} forEach _magazines;
_unit removeWeapon _weapon; _unit removeWeapon _weapon;
// add virtual load // Add virtual load
[_target, _gunbag, _mass] call EFUNC(movement,addLoadToUnitContainer); [_target, _gunbag, _mass] call EFUNC(movement,addLoadToUnitContainer);
_gunbag setVariable [QGVAR(gunbagWeapon), [_weapon, _items, _magazines], true]; _gunbag setVariable [QGVAR(gunbagWeapon), [_weapon, _items, _magazines], true];
// play sound // Play sound
if (["ace_backpacks"] call EFUNC(common,isModLoaded)) then { if (["ace_backpacks"] call EFUNC(common,isModLoaded)) then {
[_target, _gunbag] call EFUNC(backpacks,backpackOpened); [_target, _gunbag] call EFUNC(backpacks,backpackOpened);
}; };