Jump to content

Recommended Posts

MAGAZINES REPACK

 

Hi all,

Here is a simple script for repacking magazines by double-clicking on them in inventory. Just add it in init.sqf or better, in initPlayerLocal.sqf

 

parameters:   NONE

 

Spoiler

 


MGI_fnc_repack = { 
  _idc = ctrlIDC (_this select 0); 
  _selectedIndex = _this select 1; 
  player setVariable ["MGImags", +((magazinesAmmoFull player) select {(lbdata [_idc,_selectedIndex]) in _x})]; 
  player setVariable ["MGIselect", true]; 
  false 
}; 
 
0 = [] spawn { 
  while {true} do { 
    waitUntil {uisleep 0.5; !(isnull (finddisplay 602)) && isnil {player getVariable "MGIselect"}}; 
    if !(isNull (findDisplay 602)) then { 
      ((findDisplay 602) displayCtrl 619) ctrlSetEventHandler ["LBDblClick", "_this  call MGI_fnc_repack"]; 
      ((findDisplay 602) displayCtrl 633) ctrlSetEventHandler ["LBDblClick", "_this  call MGI_fnc_repack"]; 
      ((findDisplay 602) displayCtrl 638) ctrlSetEventHandler ["LBDblClick", "_this  call MGI_fnc_repack"]; 
    }; 
    waitUntil {uisleep 0.5; !isnil {player getVariable "MGIselect"} or isNull (findDisplay 602)}; 
    if (!isnil {player getVariable "MGIselect"}) then { 
      private ["_mag","_wpnType","_wpn"]; 
      if (player getVariable ["MGImags",[]] isEqualTo []) then { 
        _mag = ""; 
        _wpn = "" 
      } else { 
        _cnts = 0; 
        _mag = (player getVariable "MGImags") select 0 select 0; 
        _fullcnt = getnumber (configFile >> "cfgmagazines" >>_mag >> "count"); 
       if (_fullcnt > 0 && _mag in getArray (configFile >> "cfgWeapons" >> currentWeapon player >> "magazines") && currentWeapon player != "") then { 
          { 
            _wpnType = _x select 3; 
            switch (_wpnType) do { 
              case 1: {_wpn = primaryWeapon player}; 
              case 2: {_wpn = handGunWeapon player}; 
              case 4: {_wpn = secondaryWeapon player}; 
              default {_wpn =""}; 
            }; 
            _cnts = _cnts + (_x select 1); 
            player setammo [_wpn,0]; 
          } count (player getVariable ["MGImags",[]]); 
          _remainingShots = _cnts mod _fullcnt; 
          {player removeMagazineGlobal _mag} count  (player getVariable ["MGImags",[]]); 
          player addMagazines [_mag, floor (_cnts/_fullcnt)]; 
          if (_remainingShots >0) then { 
            player addMagazine [_mag,_remainingShots]; 
          }; 
          reload player; 
        }; 
      }; 
    }; 
    player setVariable ["MGIselect", nil]; 
  }; 
};

 

 

 

 

MODIFIED 01/11/19

Have fun!

  • Like 4
  • Thanks 2

Share this post


Link to post
Share on other sites

Thanks for this! With your permission, I would like to add this code to my mod

 

I hope that would be fine? 

Cheers vd

  • Like 2

Share this post


Link to post
Share on other sites

Hi @pierremgi

 

I am messing around with your code, trying to reverse engineer it so it can be used as holdaction. This I attempt as the double left click is interfering with the ravage food/drink consummation feature. As in, when I drink, I also repack my mag;)

 

I am having a a hard (but fun) time replicating your "MGImags" variable to suit my need as I do not fully understand the following line:

 

  player setVariable ["MGImags", +((magazinesAmmoFull player) select {(lbdata [_idc,_selectedIndex]) in _x})];

I believe I understand that you are gathering the magazines in an array here.

magazinesAmmoFull player = all magazines in the weapon and inventory

_idc = the location (uniform, vest, backpack) referred to by 619,633,638

_selectedIndex = the index position 0,1,2,... in the uniform, vest or backpack

 

from the (magazinesAmmoFull player), you seem to be selecting out only specific information via expression (array select expression).

Is this gathering the mags that are the same type as the one you LB double clicked?

 

I am trying to do the same here, this should filter out any magazines that do not have the same classname as the first magazine in my "magaziesAmmoFull player" array. (once this all works, I ll have to find a way to consider all different magazine types in my inventory).

_magtype1 = magazinesAmmoFull player select 0 select 0;
private _Magarray = +((magazinesAmmoFull player) select {_magtype1 in _x});

 

 

Unfortunately I am now stuck at the next part. The below line does not define _wpn and I do not understand why.

I assume it still has to do with how I create my _Magarray, meaning that I do still not replicate your "MGImags" array correctly.

 

Spoiler

{
  _wpnType = _x select 3;
  switch (_wpnType) do {
    case 1: {_wpn = primaryWeapon player};
    case 2: {_wpn = handGunWeapon player};
    case 4: {_wpn = secondaryWeapon player};
    default {_wpn =""; hint str _wpn};
  };
  _cnts = _cnts + (_x select 1);
  player setammo [_wpn,0];
} count (_Magarray);

 

 

This is where I am at with my holdaction version right now.

_wpn is undefined when running this code:

 

Spoiler

[
player,
"Repack Magazines",
"",
"",
"true",
"true",
{
},
{},
{
          _cnts = 0;
          _magtype1 = magazinesAmmoFull player select 0 select 0;
          private _Magarray = +((magazinesAmmoFull player) select {_magtype1 in _x});

          _mag = _Magarray select 0 select 0;
          _fullcnt = getnumber (configFile >> "cfgmagazines" >>_mag >> "count");
          if (_fullcnt > 0) then {
            {
              _wpnType = _x select 3;
              switch (_wpnType) do {
                case 1: {_wpn = primaryWeapon player};
                case 2: {_wpn = handGunWeapon player};
                case 4: {_wpn = secondaryWeapon player};
                default {_wpn =""; hint str _wpn};
              };
                _cnts = _cnts + (_x select 1);
            player setammo [_wpn,0];
            } count (_Magarray);
            _remainingShots = _cnts mod _fullcnt;
            {player removeMagazineGlobal _mag} count (_Magarray);
            player addMagazines [_mag, floor (_cnts/_fullcnt)];
            if (_remainingShots >0) then {
              player addMagazine [_mag,_remainingShots];
            };

            reload player;

          };
},
{
},
[],
2,
0,
false,
false
]call BIS_fnc_holdActionAdd;

 

 

I am using this with a contact dlc AKM and a couple of extra mags in the inventory.

 

I think if you could elaborate what...

  player setVariable ["MGImags", +((magazinesAmmoFull player) select {(lbdata [_idc,_selectedIndex]) in _x})];

..does different than..

 

magazinesAmmoFull player

...and...

 

_magtype1 = magazinesAmmoFull player select 0 select 0;
private _Magarray = +((magazinesAmmoFull player) select {_magtype1 in _x});

...that would already greatly help me to proceed.

 

Thanks!

VD

 

  • Like 1

Share this post


Link to post
Share on other sites
2 hours ago, Vandeanson said:

I am messing around with your code, trying to reverse engineer it so it can be used as holdaction. This I attempt as the double left click is interfering with the ravage food/drink consummation feature. As in, when I drink, I also repack my mag;)

 

I am having a a hard (but fun) time replicating your "MGImags" variable to suit my need as I do not fully understand the following line:

 


  player setVariable ["MGImags", +((magazinesAmmoFull player) select {(lbdata [_idc,_selectedIndex]) in _x})];

I believe I understand that you are gathering the magazines in an array here.

magazinesAmmoFull player = all magazines in the weapon and inventory

_idc = the location (uniform, vest, backpack) referred to by 619,633,638

_selectedIndex = the index position 0,1,2,... in the uniform, vest or backpack

 

from the (magazinesAmmoFull player), you seem to be selecting out only specific information via expression (array select expression).

Is this gathering the mags that are the same type as the one you LB double clicked?

 

 

Yes but that works with inventory (double click). Nothing to do with hold action.

 

2 hours ago, Vandeanson said:

I am trying to do the same here, this should filter out any magazines that do not have the same classname as the first magazine in my "magaziesAmmoFull player" array. (once this all works, I ll have to find a way to consider all different magazine types in my inventory).


_magtype1 = magazinesAmmoFull player select 0 select 0;
private _Magarray = +((magazinesAmmoFull player) select {_magtype1 in _x});

 

 

Yes, same as   +((magazinesAmmoFull player) select {_x#0 == (magazinesAmmoFull player select 0 select 0 )})
 

2 hours ago, Vandeanson said:

This is where I am at with my holdaction version right now.

_wpn is undefined when running this code:

 

 

When a local variable is undefined, define it. I did it in my code.

Don't reverse engine this kind of code for a hold action. Just think about what you want to do with it. For example, repacking the current magazine of the player. Mixing hold action and inventory controls is probably a waste of time.

 

I can understand how:  the double left click is interfering with the ravage food/drink consummation feature. As in, when I drink, I also repack my mag;)

In fact, double-clicking on a magazine like chemlight (and probably your case with drink) will not repack the current mags but reload your weapon (with an existing mag).

Looking for better feature.

 

EDITED: DONE.

Now, you can repack only the mags of your current weapon. So, the other mags like chemlights, drinks or foods should not interfere.

 

 

 

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

hi @pierremgi

 

Thanks for looking into it.

 

I have ended up going into a different direction as I wanted the script to be based on holdaction and some other variations.

 

Spoiler

fnc_VA_AA_magRep =
        {

            AA_VA_magRep = [
                              player,
                              VA_MagRepAddATxt,
                              "",
                              "",
                              "true",
                              "true",
                              {
                                player playActionNow "Medic";
                              },
                              {},
                              {
                                      _mag = VA_MagTypes select 0;
                                        VA_MagTypes deleteat 0;

                                        _cnts = 0;
                                        private _Magarray = +((magazinesAmmoFull player) select {_mag in _x});
                                        _gunmag = (magazinesAmmoFull player) select {true in _x};
                                        _Magarray = _Magarray - _gunmag;
                                        _fullcnt = getnumber (configFile >> "cfgmagazines" >>_mag >> "count");
                                        if (_fullcnt > 0) then {
                                          {

                                              _cnts = _cnts + (_x select 1);

                                          } count (_Magarray);
                                          _remainingShots = _cnts mod _fullcnt;
                                          {player removeMagazineGlobal _mag} count (_Magarray);
                                          player addMagazines [_mag, floor (_cnts/_fullcnt)];
                                          if (_remainingShots >0) then {
                                            player addMagazine [_mag,_remainingShots];
                                          };
                                          systemchat "Magazines repacked";
                                          player removeaction AA_VA_magRep;



                                          if (!(VA_MagTypes isequalto [])) then {
                                            _mag = VA_MagTypes select 0;
                                            _magDN = gettext (configfile >> "CfgMagazines" >> _mag >> "displayName");
                                            VA_MagRepAddATxt = str formatText [ "Repack %1", _magDN];
                                            [] spawn fnc_VA_AA_magRep};
                                          };
                              },
                              {    player switchaction "";
                              },
                              [],
                              6,
                              0,
                              false,
                              false
                            ]call BIS_fnc_holdActionAdd;
        };

player addEventHandler ["InventoryClosed",
                      {
                        VA_MagTypes = [];
                        private _allmagArrs1 = [];
                        private _allmagArrsCN = [];
                         _allmagArrs1 = magazinesAmmoCargo uniformContainer player;
                        _allmagArrs2 = magazinesAmmoCargo vestContainer player;
                        _allmagArrs3 = magazinesAmmoCargo backpackContainer player;

                        _allmagArrs1 append _allmagArrs2;
                        _allmagArrs1 append _allmagArrs3;


                            {
                                _mag = _x select 0;
                                _rounds = _x select 1;
                                _fullcnt = getnumber (configFile >> "cfgmagazines" >>_mag >> "count");
                                if (_rounds isequalto _fullcnt || _fullcnt isequalto 1) then {_allmagArrs1 = _allmagArrs1 - [_x]};
                              }foreach _allmagArrs1;

                              if (_allmagArrs1 isequalto []) exitwith{};

                        {_cn = _x select 0; _allmagArrsCN pushback _cn}foreach _allmagArrs1;

                              if (_allmagArrsCN isequalto []) exitwith{};

                        for "_i" from 1 to count _allmagArrsCN do {if (({_x == (_allmagArrsCN select 0)} count _allmagArrsCN) == 1) then {_allmagArrsCN deleteat 0} else {_allmagArrsCN deleteat 0; _allmagArrsCN pushback (_allmagArrsCN select 0)};};

                              if (_allmagArrsCN isequalto []) exitwith{};
                        {VA_MagTypes pushBackUnique _x}foreach _allmagArrsCN;

                        _mag = VA_MagTypes select 0;
                        _magDN = gettext (configfile >> "CfgMagazines" >> _mag >> "displayName");
                        VA_MagRepAddATxt = str formatText [ "Repack %1", _magDN];
                        [] spawn fnc_VA_AA_magRep;

                      }];



player addEventHandler ["InventoryOpened", {
                                            VA_MagTypes = [];
                                              }];

 

 

will need some more fine-tuning and optimization probably. I will also try to find a better matching animation.

 

At InventoryClose Eventhandler, a Holdaction to repack the first group of available mags is created.

 

If more types of mags (from other guns) can be repacked, an updated Holdaction is added while the old one is removed after completion (of the Holdaction).

 

The script will gather all magazines that are of interest in an array:

- from uniform, vest and backpack

- magazines with below 100% rounds of full capacity

- removes "fake" magazine items (filters out magazines or grenades with only 1 full capacity)

- ignores magazines that are loaded into the gun (i want to force players to take out the magazine)

- ignores magazines if only 1 magazine with below 100% rounds per type of magazine is present

 

this must be called in initplayerlocal.sqf and onplayerrespawn.qsf (or in a respawn eventhandler).

 

PS: it was great fun to work on this - thanks for the inspiration and  code provided!

 

cheers vd

  • Like 2

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×