2023-09-12 18:58:10 +00:00
#include "..\script_component.hpp"
2015-01-16 02:36:31 +00:00
/*
* Author: Commy2, based on KK_fnc_playerWeaponMulfunction from KillzoneKid
* Jam the weapon
*
2016-06-18 09:50:41 +00:00
* Arguments:
2015-02-08 22:54:12 +00:00
* 0: Unit <OBJECT>
* 1: Weapon <STRING>
2015-01-16 02:36:31 +00:00
*
2016-06-18 09:50:41 +00:00
* Return Value:
2015-01-16 02:36:31 +00:00
* None
2015-02-08 22:54:12 +00:00
*
2015-12-15 07:09:26 +00:00
* Example:
* [player, currentWeapon player] call ace_overheating_fnc_jamWeapon
*
2015-02-08 22:54:12 +00:00
* Public: No
2015-01-16 02:36:31 +00:00
*/
2015-01-11 16:42:31 +00:00
2015-12-15 07:09:26 +00:00
params ["_unit", "_weapon"];
TRACE_2("params",_unit,_weapon);
2015-01-11 16:42:31 +00:00
// don't jam a weapon with no rounds left
2015-12-15 07:09:26 +00:00
private _ammo = _unit ammo _weapon;
2021-10-14 15:47:52 +00:00
if (_ammo < 1) exitWith {};
2015-01-11 16:42:31 +00:00
2015-12-15 07:09:26 +00:00
private _jammedWeapons = _unit getVariable [QGVAR(jammedWeapons), []];
2015-01-11 16:42:31 +00:00
_jammedWeapons pushBack _weapon;
2015-01-13 03:23:14 +00:00
_unit setVariable [QGVAR(jammedWeapons), _jammedWeapons];
2015-01-11 16:42:31 +00:00
2021-10-14 15:47:52 +00:00
// Get jam types, select one from available types
// Cookoffs only happen on Fire and Dud, dud rounds are lost on jam clear.
// Reduce chance of duds as temp increases (functionally increasing the chance of the others but with fewer commands)
private _temp = 1 max (_unit getVariable [format [QGVAR(%1_temp), _weapon], 0]);
2021-10-17 22:00:16 +00:00
private _jamTypesAllowed = getArray (configFile >> 'CfgWeapons' >> currentWeapon _unit >> QGVAR(jamTypesAllowed));
2015-01-11 16:42:31 +00:00
2021-10-17 22:00:16 +00:00
if (_jamTypesAllowed isEqualTo []) then {
2021-10-14 15:47:52 +00:00
_jamTypesAllowed = ["Eject", 1, "Extract", 1, "Feed", 1, "Fire", 1, "Dud", (5 / (_temp / 5))];
} else {
for "_i" from count _jamTypesAllowed to 1 step -1 do {
private _jamCurretType = _jamTypesAllowed select _i;
if !(_jamCurretType in ["Eject", "Extract", "Feed", "Fire", "Dud"]) exitWith { // check config values and switch to default values if unusual value found
ERROR_2("Weapon '%1' has unexpected value %2 in QQGVAR(jamTypesAllowed). Expected values are 'Eject', 'Extract', 'Feed', 'Fire', 'Dud'.",_weapon,_jamCurretType);
_jamTypesAllowed = ["Eject", 1, "Extract", 1, "Feed", 1, "Fire", 1, "Dud", (5 / (_temp / 5))];
};
if (_jamCurretType == "Dud") then {
_jamTypesAllowed insert [_i, [5 / (_temp / 5)]];
} else {
_jamTypesAllowed insert [_i, [1]];
};
};
};
2021-11-08 18:06:31 +00:00
private _jamType = selectRandomWeighted _jamTypesAllowed;
_unit setVariable [format [QGVAR(%1_jamType), _weapon], _jamType];
2015-01-11 16:42:31 +00:00
2015-01-16 02:36:58 +00:00
// Stop current burst
2021-10-14 15:47:52 +00:00
_unit setAmmo [_weapon, 0];
// this is to re-activate the 'DefaultAction', so you can jam a weapon while full auto shooting
[{
params ["_unit", "_weapon", "_ammo"];
_unit setAmmo [_weapon, _ammo];
}, [_unit, _weapon, _ammo]] call CBA_fnc_execNextFrame;
2015-01-11 16:42:31 +00:00
2020-01-18 17:40:41 +00:00
if (_weapon == primaryWeapon _unit) then {
playSound QGVAR(jamming_rifle);
} else {
if (_weapon == handgunWeapon _unit) then {
playSound QGVAR(jamming_pistol);
};
};
2015-01-11 16:42:31 +00:00
// only display the hint once, after you try to shoot an already jammed weapon
2015-01-13 03:23:14 +00:00
GVAR(knowAboutJam) = false;
2015-01-11 16:42:31 +00:00
2021-11-08 18:06:31 +00:00
["ace_weaponJammed", [_unit, _weapon, _jamType]] call CBA_fnc_localEvent;
2016-04-11 21:29:33 +00:00
2015-01-13 03:23:14 +00:00
if (_unit getVariable [QGVAR(JammingActionID), -1] == -1) then {
2015-01-11 16:42:31 +00:00
2015-12-15 07:09:26 +00:00
private _condition = {
2021-10-14 15:47:52 +00:00
private _unit = _this select 1;
2024-08-02 11:59:18 +00:00
(weaponState _unit) params ["_currentWeapon", "_currentMuzzle"];
_unit call CBA_fnc_canUseWeapon
&& {_currentMuzzle in (_unit getVariable [QGVAR(jammedWeapons), []])}
&& {!(["ace_safemode"] call EFUNC(common,isModLoaded)) || {!([_unit, _currentWeapon, _currentMuzzle] call EFUNC(safemode,getWeaponSafety))}}
2015-01-11 16:42:31 +00:00
};
2015-12-15 07:09:26 +00:00
private _statement = {
2021-10-14 15:47:52 +00:00
params ["_zero","_one"];
playSound3D ["a3\sounds_f\weapons\Other\dry9.wss", _zero, false, eyePos _zero, 1, 1, 15];
if (!(missionNamespace getVariable [QGVAR(knowAboutJam), false]) && {_one ammo currentWeapon _one > 0} && {GVAR(DisplayTextOnJam)}) then {
private _jamType = _one getVariable [format [QGVAR(%1_jamType), currentWeapon _one], "None"];
private _jamMessage = localize LSTRING(FailureToFire);
switch true do {
case (_jamType isEqualTo "Eject"): {_jamMessage = localize LSTRING(FailureToEject)};
case (_jamType isEqualTo "Extract"): {_jamMessage = localize LSTRING(FailureToExtract)};
case (_jamType isEqualTo "Feed"): {_jamMessage = localize LSTRING(FailureToFeed)};
};
[
[localize LSTRING(WeaponJammed)],
[_jamMessage]
] call CBA_fnc_notify;
2015-12-15 07:09:26 +00:00
GVAR(knowAboutJam) = true;
};
};
2015-01-11 16:42:31 +00:00
2015-12-15 07:09:26 +00:00
private _id = [_unit, "DefaultAction", _condition, _statement] call EFUNC(common,addActionEventHandler);
2015-01-11 16:42:31 +00:00
2015-12-15 07:09:26 +00:00
_unit setVariable [QGVAR(JammingActionID), _id];
2015-01-11 16:42:31 +00:00
};