Skip to content

Commit

Permalink
CSW - Fix AI reloading from GroundWeaponHolder (#8399)
Browse files Browse the repository at this point in the history
* fix fnc_ai_handleFired

* remove debug messages

* use banana instead of FakePrimaryWeapon

* switch from dummy item to new weaponholder

* use exitWith instead of break
  • Loading branch information
LinkIsGrim authored Sep 25, 2021
1 parent 82a8350 commit a4f24cb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 59 deletions.
79 changes: 26 additions & 53 deletions addons/common/functions/fnc_removeSpecificMagazine.sqf
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#include "script_component.hpp"
/*
* Author: esteldunedain
* Removes a magazine from the unit that has an specific ammo count
* Removes a magazine from the unit or object that has a specific ammo count
*
* Arguments:
* 0: Unit <OBJECT>
* 1: Magazine <STRING>
* 2: Ammo count <NUMBER>
*
* Return Value:
* None
* Magazine Removed <BOOL>
*
* Example:
* [bob, "magazine", 5] call ace_common_fnc_removeSpecificMagazine
Expand All @@ -21,62 +21,35 @@ params [["_unit", objNull, [objNull]], ["_magazineType", "", [""]], ["_ammoCount

private _isRemoved = false;

// Check uniform
private _magazines = magazinesAmmoCargo uniformContainer _unit select {_x select 0 == _magazineType};
private _index = _magazines find [_magazineType, _ammoCount];
private _fnc_removeMagazine = {
params ["_container", "_magArray"];
_magArray params ["_magazineType", "_ammoCount"];

if (_index > -1) exitWith {
{
_unit removeItemFromUniform (_x select 0);
false
} count _magazines;
private _allMagazines = magazinesAmmoCargo _container;
private _specificMagazineIndex = _allMagazines findIf {_x isEqualTo _magArray};
_allMagazines deleteAt _specificMagazineIndex;

{
if (!_isRemoved && (_x isEqualTo [_magazineType,_ammoCount])) then {
_isRemoved = true;
} else {
(uniformContainer _unit) addMagazineAmmoCargo [_x select 0, 1, _x select 1];
if (_specificMagazineIndex > -1) exitWith {
clearMagazineCargoGlobal _container;
private _containerType = typeOf _container;
if (_containerType in ["GroundWeaponHolder", "WeaponHolderSimulated"]) then {
_container = createVehicle [_containerType, getPosATL _container, [], 0, "CAN_COLLIDE"];
};
false
} count _magazines;
{
_container addMagazineAmmoCargo [_x select 0, 1, _x select 1];
} forEach _allMagazines;
true
};
false
};

// Check vest
_magazines = magazinesAmmoCargo vestContainer _unit select {_x select 0 == _magazineType};
_index = _magazines find [_magazineType, _ammoCount];

if (_index > -1) exitWith {
{
_unit removeItemFromVest (_x select 0);
false
} count _magazines;

{
if (!_isRemoved && (_x isEqualTo [_magazineType,_ammoCount])) then {
_isRemoved = true;
} else {
(vestContainer _unit) addMagazineAmmoCargo [_x select 0, 1, _x select 1];
};
false
} count _magazines;
private _containerArray = [_unit];
if (_unit isKindOf "CAManBase") then {
_containerArray = [uniformContainer _unit, vestContainer _unit, backpackContainer _unit];
};

// Check backpack
_magazines = magazinesAmmoCargo backpackContainer _unit select {_x select 0 == _magazineType};
_index = _magazines find [_magazineType, _ammoCount];
{
if ([_x, [_magazineType, _ammoCount]] call _fnc_removeMagazine) exitWith {_isRemoved = true};
} forEach _containerArray;

if (_index > -1) exitWith {
{
_unit removeItemFromBackpack (_x select 0);
false
} count _magazines;

{
if (!_isRemoved && (_x isEqualTo [_magazineType,_ammoCount])) then {
_isRemoved = true;
} else {
(backpackContainer _unit) addMagazineAmmoCargo [_x select 0, 1, _x select 1];
};
false
} count _magazines;
};
_isRemoved
8 changes: 2 additions & 6 deletions addons/csw/functions/fnc_ai_handleFired.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private _reloadNeededAmmo = -1;
};
} forEach _cswMagazines;
} forEach _compatibleMags;
} forEach ([_gunner] + (_staticWeapon nearEntities [["groundWeaponHolder", "ReammoBox_F"], 10]));
} forEach ([_gunner] + (_staticWeapon nearSupplies 10));
if (_reloadMag == "") exitWith {TRACE_1("could not find mag",_reloadMag);};

// Figure out what we can add from the magazines we have
Expand All @@ -74,11 +74,7 @@ TRACE_4("",_reloadSource,_reloadMag,_reloadNeededAmmo,_bestAmmoToSend);
if (_bestAmmoToSend == -1) exitWith {ERROR("No ammo");};

// Remove the mag from the source
if (_reloadSource isKindOf "CaManBase") then {
[_reloadSource, _reloadMag, _bestAmmoToSend] call EFUNC(common,removeSpecificMagazine);
} else {
[_reloadSource, _reloadMag, 1, _bestAmmoToSend] call CBA_fnc_removeMagazineCargo;
};
[_reloadSource, _reloadMag, _bestAmmoToSend] call EFUNC(common,removeSpecificMagazine);

private _timeToLoad = 1;
if (!isNull(configOf _staticWeapon >> QUOTE(ADDON) >> "ammoLoadTime")) then {
Expand Down

0 comments on commit a4f24cb

Please sign in to comment.