diff --git a/addons/magazinerepack/CfgEventHandlers.hpp b/addons/magazinerepack/CfgEventHandlers.hpp
index f0a9f14d91..b928bc2de6 100644
--- a/addons/magazinerepack/CfgEventHandlers.hpp
+++ b/addons/magazinerepack/CfgEventHandlers.hpp
@@ -1,4 +1,3 @@
-
class Extended_PreInit_EventHandlers {
class ADDON {
init = QUOTE(call COMPILE_FILE(XEH_preInit));
diff --git a/addons/magazinerepack/CfgSounds.hpp b/addons/magazinerepack/CfgSounds.hpp
new file mode 100644
index 0000000000..1390397e5e
--- /dev/null
+++ b/addons/magazinerepack/CfgSounds.hpp
@@ -0,0 +1,12 @@
+class CfgSounds {
+ class GVAR(soundMagazineFinished) {
+ name = QGVAR(soundMagazineFinished);
+ sound[]={QUOTE(PATHTOF(sounds\magrepack_finished.wav)),1,1};
+ titles[]={};
+ };
+ class GVAR(soundRoundFinished) {
+ name = QGVAR(soundRoundFinished);
+ sound[] = {QUOTE(PATHTOF(sounds\magrepack_single.wav)),1,1};
+ titles[] = {};
+ };
+};
diff --git a/addons/magazinerepack/CfgVehicles.hpp b/addons/magazinerepack/CfgVehicles.hpp
index ecb732f8fd..acd1c76c3d 100644
--- a/addons/magazinerepack/CfgVehicles.hpp
+++ b/addons/magazinerepack/CfgVehicles.hpp
@@ -1,18 +1,17 @@
-
class CfgVehicles {
- class Man;
- class CAManBase: Man {
- class ACE_SelfActions {
- class ACE_RepackMagazines {
- displayName = "$STR_ACE_MagazineRepack_RepackMagazines";
- condition = QUOTE(true);
- statement = QUOTE([_player] call FUNC(magazineRepack));
- showDisabled = 0;
- priority = -2;
- icon = PATHTOF(UI\repack_ca.paa);
- hotkey = "R";
- enableInside = 1;
- };
+ class Man;
+ class CAManBase: Man {
+ class ACE_SelfActions {
+ class ACE_RepackMagazines {
+ displayName = "$STR_ACE_MagazineRepack_RepackMagazines";
+ condition = QUOTE(true);
+ statement = QUOTE([_player] call FUNC(openSelectMagazineUI));
+ showDisabled = 0;
+ priority = -2;
+ icon = QUOTE(PATHTOF(UI\repack_ca.paa));
+ hotkey = "R";
+ enableInside = 1;
+ };
+ };
};
- };
};
diff --git a/addons/magazinerepack/README.md b/addons/magazinerepack/README.md
index 49a3c9d531..28b7c3115a 100644
--- a/addons/magazinerepack/README.md
+++ b/addons/magazinerepack/README.md
@@ -3,10 +3,10 @@ ace_magazinerepack
Adds the ability to consolidate multiple half-empty magazines.
-
## Maintainers
The people responsible for merging changes to this component or answering potential questions.
- [commy2](https://github.com/commy2)
- [esteldunedain](https://github.com/esteldunedain)
+- [PabstMirror](https://github.com/PabstMirror)
diff --git a/addons/magazinerepack/XEH_preInit.sqf b/addons/magazinerepack/XEH_preInit.sqf
index 5caba1ffac..7cd7107f54 100644
--- a/addons/magazinerepack/XEH_preInit.sqf
+++ b/addons/magazinerepack/XEH_preInit.sqf
@@ -2,8 +2,10 @@
ADDON = false;
-PREP(magazineRepack);
-PREP(magazineRepackCallback);
+PREP(magazineRepackFinish);
+PREP(magazineRepackProgress);
PREP(openSelectMagazineUI);
+PREP(simulateRepackEvents);
+PREP(startRepackingMagazine);
ADDON = true;
diff --git a/addons/magazinerepack/config.cpp b/addons/magazinerepack/config.cpp
index 91a6f5abf5..514a74350a 100644
--- a/addons/magazinerepack/config.cpp
+++ b/addons/magazinerepack/config.cpp
@@ -5,7 +5,7 @@ class CfgPatches {
units[] = {};
weapons[] = {};
requiredVersion = REQUIRED_VERSION;
- requiredAddons[] = {"ace_common","ace_interaction"};
+ requiredAddons[] = {"ace_interaction"};
author[] = {"commy2","CAA-Picard"};
authorUrl = "https://github.com/commy2/";
VERSION_CONFIG;
@@ -13,6 +13,7 @@ class CfgPatches {
};
#include "CfgEventHandlers.hpp"
+#include "CfgSounds.hpp"
#include "CfgVehicles.hpp"
class ACE_Settings {
diff --git a/addons/magazinerepack/functions/fnc_magazineRepack.sqf b/addons/magazinerepack/functions/fnc_magazineRepack.sqf
deleted file mode 100644
index 472b80e2bd..0000000000
--- a/addons/magazinerepack/functions/fnc_magazineRepack.sqf
+++ /dev/null
@@ -1,77 +0,0 @@
-// by commy2, esteldunedain
-#include "script_component.hpp"
-
-private ["_unit", "_magazines", "_ammos", "_repackTime", "_magazine", "_ammo", "_count", "_index", "_i", "_j", "_ammoToTransfer", "_ammoAvailable", "_ammoNeeded"];
-
-_unit = _this select 0;
-
-_magazines = [];
-_ammos = [];
-_repackTime = [];
-
-// get all mags and ammo count
-{
- _magazine = _x select 0;
- _ammo = _x select 1;
-
- _count = getNumber (configfile >> "CfgMagazines" >> _magazine >> "count");
-
- if (_ammo != _count && {_count > 1}) then { // additional checks here
- if !(_magazine in _magazines) then {
- _index = count _magazines;
- _magazines set [_index, _magazine];
- _ammos set [_index, [_ammo]];
- } else {
- _index = _magazines find _magazine;
- _ammos set [_index, (_ammos select _index) + [_ammo]];
- };
- };
-} forEach magazinesAmmoFull _unit;
-
-// Remove invalid magazines
-{
- if (count _x < 2) then {
- _magazines set [_forEachIndex, -1];
- _ammos set [_forEachIndex, [-1]];
- };
-} forEach _ammos;
-_magazines = _magazines - [-1];
-_ammos = _ammos - [[-1]];
-
-{
- // Calculate actual ammo to transfer during repack
- _count = getNumber (configfile >> "CfgMagazines" >> (_magazines select _forEachIndex) >> "count");
-
- // Sort Ascending
- _list = _x call BIS_fnc_sortNum;
-
- ["MagazineRepack", _list] call EFUNC(common,log);
-
- _i = 0;
- _j = count _x - 1;
- _ammoToTransfer = 0;
- _ammoAvailable = 0;
- while {_i < _j} do {
- _ammoNeeded = _count - (_list select _j);
- _exit = false;
- while {_i < _j && {!_exit}} do {
- _ammoAvailable = _list select _i;
- if (_ammoAvailable >= _ammoNeeded) then {
- _list set [_i, _ammoAvailable - _ammoNeeded];
- _ammoToTransfer = _ammoToTransfer + _ammoNeeded;
- _exit = true;
- } else {
- _ammoNeeded = _ammoNeeded - _ammoAvailable;
- _ammoToTransfer = _ammoToTransfer + _ammoAvailable;
- _i = _i + 1;
- };
- };
- _j = _j - 1;
- };
-
- _repackTime set [_forEachIndex, _ammoToTransfer * GVAR(TimePerAmmo) + (count _x) * GVAR(TimePerMagazine)];
-} forEach _ammos;
-
-["MagazineRepack", [_magazines, _repackTime]] call EFUNC(common,log);
-
-[_unit, _magazines, _repackTime] call FUNC(openSelectMagazineUI);
diff --git a/addons/magazinerepack/functions/fnc_magazineRepackCallback.sqf b/addons/magazinerepack/functions/fnc_magazineRepackCallback.sqf
deleted file mode 100644
index f209806f9c..0000000000
--- a/addons/magazinerepack/functions/fnc_magazineRepackCallback.sqf
+++ /dev/null
@@ -1,106 +0,0 @@
-// by commy2
-#include "script_component.hpp"
-
-private ["_unit", "_magazine", "_ammo", "_ammoCount", "_fullMagazinesCount", "_restAmmo", "_isLoaded", "_weapon", "_reloadAction", "_text", "_picture"];
-
-_unit = ACE_player; //_this select 0;
-_magazine = _this select 1;
-
-// exit if the last magazine of this type was taken out of the backpack
-if !(_magazine in magazines _unit) exitWith {};
-
-// get current ammo count
-_ammo = 0;
-{
- if (_x select 0 == _magazine) then {
- _ammo = _ammo + (_x select 1);
- };
-} forEach magazinesAmmoFull _unit;
-
-// how many rounds fit in one mag
-_ammoCount = getNumber (configFile >> "CfgMagazines" >> _magazine >> "count");
-
-// calculate new vaules
-_fullMagazinesCount = floor (_ammo / _ammoCount);
-_restAmmo = _ammo - _fullMagazinesCount * _ammoCount;
-
-// remove old magazines
-_unit removeMagazines _magazine;
-
-_isLoaded = false;
-// reload rifle
-if (_magazine in primaryWeaponMagazine _unit) then {
- _weapon = primaryWeapon _unit;
-
- if (_fullMagazinesCount > 0) then {
- _unit setAmmo [_weapon, _ammoCount];
- _fullMagazinesCount = _fullMagazinesCount - 1;
- } else {
- _unit setAmmo [_weapon, _restAmmo];
- _restAmmo = 0;
- };
-
- if (_weapon == currentWeapon _unit) then {
- _reloadAction = getText (configFile >> "CfgWeapons" >> _weapon >> "reloadAction");
- _unit playActionNow _reloadAction;
- };
-
- _isLoaded = true;
-};
-
-// reload pistol
-if (_magazine in handgunMagazine _unit) then {
- _weapon = handgunWeapon _unit;
-
- if (_fullMagazinesCount > 0) then {
- _unit setAmmo [_weapon, _ammoCount];
- _fullMagazinesCount = _fullMagazinesCount - 1;
- } else {
- _unit setAmmo [_weapon, _restAmmo];
- _restAmmo = 0;
- };
-
- if (_weapon == currentWeapon _unit) then {
- _reloadAction = getText (configFile >> "CfgWeapons" >> _weapon >> "reloadAction");
- _unit playActionNow _reloadAction;
- };
-
- _isLoaded = true;
-};
-
-// reload rocket launcher (just in case ...)
-if (_magazine in secondaryWeaponMagazine _unit) then {
- _weapon = secondaryWeapon _unit;
-
- if (_fullMagazinesCount > 0) then {
- _unit setAmmo [_weapon, _ammoCount];
- _fullMagazinesCount = _fullMagazinesCount - 1;
- } else {
- _unit setAmmo [_weapon, _restAmmo];
- _restAmmo = 0;
- };
-
- if (_weapon == currentWeapon _unit) then {
- _reloadAction = getText (configFile >> "CfgWeapons" >> _weapon >> "reloadAction");
- _unit playActionNow _reloadAction;
- };
-
- _isLoaded = true;
-};
-
-// add new magazines
-for "_a" from 1 to _fullMagazinesCount do {
- _unit addMagazine _magazine;
-};
-
-if (_restAmmo > 0) then {
- _unit addMagazine [_magazine, _restAmmo];
-};
-
-// display text if successful
-_text = format [localize "STR_ACE_MagazineRepack_RepackedMagazinesDetail", [_fullMagazinesCount, _fullMagazinesCount + 1] select _isLoaded, _restAmmo];
-_picture = getText (configFile >> "CfgMagazines" >> _magazine >> "picture");
-
-_text = parseText format ["
%2
%3", _picture, localize "STR_ACE_MagazineRepack_RepackedMagazines", _text];
-
-[_text] call EFUNC(common,displayTextStructured);
diff --git a/addons/magazinerepack/functions/fnc_magazineRepackFinish.sqf b/addons/magazinerepack/functions/fnc_magazineRepackFinish.sqf
new file mode 100644
index 0000000000..038b7fb151
--- /dev/null
+++ b/addons/magazinerepack/functions/fnc_magazineRepackFinish.sqf
@@ -0,0 +1,52 @@
+/*
+ * Author: PabstMirror (based on repack from commy2, esteldunedain, Ruthberg)
+ * Simulates repacking a set of magazines.
+ * Returns the timing and magazines counts at every stage.
+ *
+ * Arguments:
+ * 0: Arguments [classname,lastAmmoStatus,events]
+ * 1: Elapsed Time
+ * 2: Total Time Repacking Will Take
+ * 3: Error Code
+ *
+ * Return Value:
+ * Nothing
+ *
+ * Example:
+ * (args from progressBar) call ace_magazinerepack_fnc_magazineRepackFinish
+ *
+ * Public: No
+ */
+#include "script_component.hpp"
+
+PARAMS_4(_args,_elapsedTime,_totalTime,_errorCode);
+EXPLODE_2_PVT(_args,_magazineClassname,_lastAmmoCount);
+_fullMagazineCount = getNumber (configfile >> "CfgMagazines" >> _magazineClassname >> "count");
+
+_structuredOutputText =
+
+if (_errorCode == 0) then {
+ format ["%1
", (localize "STR_ACE_MagazineRepack_RepackComplete")];
+} else {
+ format ["%1
", (localize "STR_ACE_MagazineRepack_RepackInterrupted")];
+};
+
+_picture = getText (configFile >> "CfgMagazines" >> _magazineClassname >> "picture");
+_structuredOutputText = _structuredOutputText + format ["
", _picture];
+
+_fullMags = 0;
+_partialMags = 0;
+{
+ EXPLODE_2_PVT(_x,_xClassname,_xCount);
+ if ((_xClassname == _magazineClassname) && {_xCount > 0}) then {
+ if (_xCount == _fullMagazineCount) then {
+ _fullMags = _fullMags + 1;
+ } else {
+ _partialMags = _partialMags + 1;
+ };
+ };
+} forEach (magazinesAmmoFull ACE_player);
+
+_structuredOutputText = _structuredOutputText + format [("" + (localize "STR_ACE_MagazineRepack_RepackedMagazinesCount") + ""), _fullMags, _partialMags];
+
+[parseText _structuredOutputText] call EFUNC(common,displayTextStructured);
diff --git a/addons/magazinerepack/functions/fnc_magazineRepackProgress.sqf b/addons/magazinerepack/functions/fnc_magazineRepackProgress.sqf
new file mode 100644
index 0000000000..64ce05d12d
--- /dev/null
+++ b/addons/magazinerepack/functions/fnc_magazineRepackProgress.sqf
@@ -0,0 +1,78 @@
+/*
+ * Author: PabstMirror (based on repack from commy2, esteldunedain, Ruthberg)
+ * Handles each frame durring the repack progressBar.
+ * On each event (repacked bullet or move to new mag) it plays a sound and syncs up the new magazines to the player.
+ *
+ * Arguments:
+ * 0: Arguments [classname,lastAmmoStatus,events]
+ * 1: Elapsed Time
+ * 2: Total Time Repacking Will Take
+ *
+ * Return Value:
+ * Keep going (on missing mags return false)
+ *
+ * Example:
+ * (args from progressBar) call ace_magazinerepack_fnc_magazineRepackProgress
+ *
+ * Public: No
+ */
+#include "script_component.hpp"
+
+private ["_currentAmmoCount", "_addedMagazines", "_missingAmmo", "_index", "_updateMagazinesOnPlayerFnc"];
+
+PARAMS_3(_args,_elapsedTime,_totalTime);
+EXPLODE_3_PVT(_args,_magazineClassname,_lastAmmoCount,_simEvents);
+if ((count _simEvents) == 0) exitWith {ERROR("No Event"); false};
+EXPLODE_3_PVT((_simEvents select 0),_nextEventTime,_nextEventIsBullet,_nextEventMags);
+
+if (_nextEventTime > _elapsedTime) exitWith {true};//waiting on next event
+
+//Verify we aren't missing any ammo
+_currentAmmoCount = [];
+{
+ EXPLODE_2_PVT(_x,_xClassname,_xCount);
+ if (_xClassname == _magazineClassname) then {
+ _currentAmmoCount pushBack _xCount;
+ };
+} forEach (magazinesAmmo ACE_player); //only inventory mags
+
+//Go through mags we currently have and check off the ones we should have
+_addedMagazines = +_currentAmmoCount;
+_missingAmmo = false;
+{
+ if (_x > 0) then {
+ _index = _addedMagazines find _x;
+ if (_index != -1) then {
+ _addedMagazines deleteAt _index;
+ } else {
+ _missingAmmo = true;
+ };
+ };
+} forEach _lastAmmoCount;
+
+if (_missingAmmo) exitWith {false}; //something removed ammo that was being repacked (could be other players or scripts)
+
+_updateMagazinesOnPlayerFnc = {
+ ACE_player removeMagazines _magazineClassname; //remove inventory magazines
+ {
+ if (_x > 0) then {
+ ACE_player addMagazine [_magazineClassname, _x];
+ };
+ } forEach (_addedMagazines + _nextEventMags);
+ _args set [1, _nextEventMags]; //store the new magazine
+};
+
+if (_nextEventIsBullet) then {
+ playSound QGVAR(soundRoundFinished);
+ if ((((count _simEvents) % 3) == 0) || {(count _simEvents) == 1}) then {
+ //For performance - only update mags every 3 bullets (or if it's the last event)
+ call _updateMagazinesOnPlayerFnc;
+ };
+} else {
+ playSound QGVAR(soundMagazineFinished);
+ call _updateMagazinesOnPlayerFnc;
+};
+
+_simEvents deleteAt 0; //pop off the event
+
+true;
diff --git a/addons/magazinerepack/functions/fnc_openSelectMagazineUI.sqf b/addons/magazinerepack/functions/fnc_openSelectMagazineUI.sqf
index b1a31c266c..e96e84e9a1 100644
--- a/addons/magazinerepack/functions/fnc_openSelectMagazineUI.sqf
+++ b/addons/magazinerepack/functions/fnc_openSelectMagazineUI.sqf
@@ -1,40 +1,59 @@
-// by commy2
+/*
+ * Author: PabstMirror (based on repack from commy2, esteldunedain, Ruthberg)
+ * Opens the selectMenu UI to chose which magazine to repack.
+ * Only shows classnames that have 2+ partial magazines
+ *
+ * Arguments:
+ * 0: Unit (player)