Jump to content

Recommended Posts

ADAPT SILENCERS TO COMBAT/STEALTH SITUATION

 

Hi all,

Did you remark that AIs don't adapt their muzzle sound suppressor (silencer) with the ordered situation (STEALTH / COMBAT)?.

 

Here is a little code for adding/removing existing silencer on current weapon.

Notes:

- by "existing", I means the silencer must be on weapon or inside unit's gear. Furthermore, as you can start with an already equipped silencer and no more room in gear, this script will record that in a variable and will be able to re-fit the silencer on weapon instead of losing him due to the lack of room. (a kind of extra pocket).

- MP compatible but I intentionally worked at idle for better performance. If your unit's localization (PC owning it) is supposed to change, You'll need to make the variable broadcast (adding true as 3rd parameter).

  And, that's for the specific case your unit need the "extra pocket".

- players are not concerned, but it's easy, for automatic adapted muzzle, to remove the   isPlayer   condition.

- for performance, this script is not "on each framed". It could be. I chose to fire the code using the "animChanged" EH. In some cases, the unit can start to fire without changing its animation.

   So, try to order the new behavior in advance (Anims often change anyway).

- some silencer can be non-removable, hard-coded with model, or simply don't exist! There is nothing to do with that. It's up to you to choose the right weapon.

 

Tested on Vanilla and CUP. For other mods, have your own test. This will depend on how silencer classes are named.

 

version Nov. 20th 2018

 

parameters:   NONE


 

{
  _x addEventHandler ["AnimChanged", {
    params ["_unit","","_silencer"];
    if (!isplayer _unit) then {
      if (isnil {_unit getVariable "MGI_silencer"}) then {
        _unit setVariable ["MGI_silencer",[]]
      };
      _silencers = (items _unit select {["_snds",_x] call bis_fnc_instring}) + (weapons _unit apply {_unit weaponAccessories _x  param [0, ""]} select {_x != ""}) + (_unit getVariable "MGI_silencer");
      call {
        if (behaviour _unit == "COMBAT" && {isNull objectParent _unit} && {_unit weaponAccessories currentMuzzle _unit param [0, ""] != ""}) exitWith {
          _silencer = _unit weaponAccessories currentMuzzle _unit param [0, ""];
          call {
            if (currentWeapon _unit isEqualTo primaryWeapon _unit) exitWith {
              _unit removePrimaryWeaponItem _silencer;
            };
            if (currentWeapon _unit isEqualTo handGunWeapon _unit) exitWith {
              _unit removeHandGunItem _silencer;
            };
          };
          call {
            if (_unit canAddItemToBackpack _silencer) exitWith {
              _unit addItemToBackpack _silencer;
            };
            if (_unit canAddItemToVest _silencer) exitWith {
              _unit addItemToVest _silencer;
            };
            if (_unit canAddItemToUniform _silencer) exitWith {
              _unit addItemToUniform _silencer;
            };
            (_unit getVariable "MGI_silencer") pushBackUnique _silencer;
          };
        };
        if (behaviour _unit == "stealth" &&  {isNull objectParent _unit} && {_unit weaponAccessories currentMuzzle _unit param [0, ""] == ""}) exitWith {
          {_unit addWeaponItem [currentWeapon _unit,_x]} count _silencers;
          _silencer = _unit weaponAccessories currentMuzzle _unit param [0, ""];
          if (_silencer in (_unit getVariable ["MGI_silencer",[]])) then {
            _unit setVariable ["MGI_silencer", (_unit getVariable "MGI_silencer") - [_silencer]];
          } else {
            if (_silencer in items _unit) then {_unit removeitem _silencer};
          };
        };
      };
    };
  }]
} forEach (playableUnits + switchableUnits);

 

 

 

 

 

 

  • Like 2
  • Thanks 4

Share this post


Link to post
Share on other sites

Nice.

 

For those

if (_unit canAddItemToBackpack _silencer) exitWith {
	_unit addItemToBackpack _silencer;
};
if (_unit canAddItemToVest _silencer) exitWith {
	_unit addItemToVest _silencer;
};
if (_unit canAddItemToUniform _silencer) exitWith {
	_unit addItemToUniform _silencer;
};

you can use

if (_unit canAddItem _silencer) exitWith {
    _unit addItem _silencer;
};

 

  • Like 1

Share this post


Link to post
Share on other sites
3 minutes ago, M1ke_SK said:

Nice.

 

For those


if (_unit canAddItemToBackpack _silencer) exitWith {
	_unit addItemToBackpack _silencer;
};
if (_unit canAddItemToVest _silencer) exitWith {
	_unit addItemToVest _silencer;
};
if (_unit canAddItemToUniform _silencer) exitWith {
	_unit addItemToUniform _silencer;
};

you can use


if (_unit canAddItem _silencer) exitWith {
    _unit addItem _silencer;
};

 

 

i don't remember exactly, but it seemed to me, addItem can add the silencer straight on the weapon, what's I wanted to avoid of course. And addItemCargo was not accurate for units.

I'm still fond of a better working solution.

Share this post


Link to post
Share on other sites

You mean to avoid "addWeaponItem". I don't think that "addItem" is adding attachments to weapon, it is adding magazines however.

Share this post


Link to post
Share on other sites

Thank you very much !
I've changed the code a bit to use any mods.
 

_silencers = (items _unit select {
		["_snds",_x] call bis_fnc_instring
		|| (getnumber( (configfile >> "CfgWeapons") >> _x >> 'ItemInfo' >> 'type') == 101
			&& {(getnumber ((configfile >> "CfgWeapons") >> _x>> 'ItemInfo' >> 'AmmoCoef'>> 'audibleFire')) > 0})
		}
	  ) + (weapons _unit apply {_unit weaponAccessories _x  param [0, ""]} select {_x != ""}) + (_unit getVariable "MGI_silencer");

 

  • Like 1

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

×