Skip to content

Commit

Permalink
Cargo & Dragging - Disable UAV AI when being dragged, carried or cargo (
Browse files Browse the repository at this point in the history
#10100)

* Disable UAV AI when being dragged, carried or cargo

* Update addons/common/functions/fnc_disableAiUAV.sqf

* Improve dragging/carrying conditions, fixed duplicate JIP
  • Loading branch information
johnb432 authored Jul 22, 2024
1 parent 05c7f84 commit b6e9cab
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 26 deletions.
11 changes: 11 additions & 0 deletions addons/cargo/functions/fnc_loadItem.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ if (_item isEqualType objNull) then {

// Some objects below water will take damage over time, eventually becoming "water logged" and unfixable (because of negative z attach)
[_item, "blockDamage", QUOTE(ADDON), true] call EFUNC(common,statusEffect_set);

// Prevent UAVs from firing
private _UAVCrew = _item call EFUNC(common,getVehicleUAVCrew);

if (_UAVCrew isNotEqualTo []) then {
{
[_x, true] call EFUNC(common,disableAiUAV);
} forEach _UAVCrew;

_item setVariable [QGVAR(isUAV), _UAVCrew, true];
};
};

// Invoke listenable event
Expand Down
12 changes: 12 additions & 0 deletions addons/cargo/functions/fnc_unloadItem.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ if (_object isEqualType objNull) then {

[QEGVAR(zeus,addObjects), [[_object], _objectCurators]] call CBA_fnc_serverEvent;
};

// Reenable UAV crew
private _UAVCrew = _object getVariable [QGVAR(isUAV), []];

if (_UAVCrew isNotEqualTo []) then {
// Reenable AI
{
[_x, false] call EFUNC(common,disableAiUAV);
} forEach _UAVCrew;

_object setVariable [QGVAR(isUAV), nil, true];
};
} else {
_object = createVehicle [_item, _emptyPosAGL, [], 0, "NONE"];

Expand Down
1 change: 1 addition & 0 deletions addons/common/XEH_PREP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ PREP(deviceKeyFindValidIndex);
PREP(deviceKeyRegisterNew);
PREP(deprecateComponent);
PREP(disableAI);
PREP(disableAiUAV);
PREP(disableUserInput);
PREP(displayIcon);
PREP(displayText);
Expand Down
24 changes: 24 additions & 0 deletions addons/common/XEH_postInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,30 @@
_object lockInventory (_set > 0);
}] call CBA_fnc_addEventHandler;

[QGVAR(disableAiUAV), {
params ["_unit", "_disable"];

if (_disable) then {
private _features = ["AUTOTARGET", "TARGET", "WEAPONAIM"/*, "FIREWEAPON"*/, "RADIOPROTOCOL"]; // TODO: Uncomment in 2.18

// Save current status
_unit setVariable [QGVAR(featuresAiUAV), _features apply {[_x, _unit checkAIFeature _x]}];

{
_unit enableAIFeature [_x, false];
} forEach _features;
} else {
// Restore previous status
private _features = _unit getVariable [QGVAR(featuresAiUAV), []];

{
_unit enableAIFeature [_x select 0, _x select 1];
} forEach _features;

_unit setVariable [QGVAR(featuresAiUAV), nil];
};
}] call CBA_fnc_addEventHandler;

//Add a fix for BIS's zeus remoteControl module not reseting variables on DC when RC a unit
//This variable is used for isPlayer checks
if (isServer) then {
Expand Down
45 changes: 45 additions & 0 deletions addons/common/functions/fnc_disableAiUAV.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "..\script_component.hpp"
/*
* Author: johnb43
* Disables/Enables UAV AI crew members, can be run on any machine and is applied globally.
*
* Arguments:
* 0: Unit <OBJECT>
* 1: Disable AI <BOOL>
*
* Return Value:
* None
*
* Example:
* [cursorObject, true] call ace_common_fnc_disableAiUAV
*
* Public: No
*/

params [["_unit", objNull, [objNull]], ["_disable", true, [false]]];

// Allow disabling of Zeus remote controlled units
if (!alive _unit || {isPlayer _unit} || {!unitIsUAV _unit}) exitWith {};

if (_disable) then {
// Ignore if already disabled
if (!isNil "_jipID") exitWith {};

// Disable shooting and targeting on every machine
// Give predefined JIP ID, in case of simultaneous executions on different machines
private _jipID = [QGVAR(disableAiUAV), [_unit, _disable], QGVAR(disableAiUAV_) + hashValue _unit] call CBA_fnc_globalEventJIP;
[_jipID, _unit] call CBA_fnc_removeGlobalEventJIP;

_unit setVariable [QGVAR(disableAiUavJipID), _jipID, true];
} else {
// Restore shooting and targeting to each client's individual state prior to disabling
private _jipID = _unit getVariable QGVAR(disableAiUavJipID);

if (isNil "_jipID") exitWith {};

_jipID call CBA_fnc_removeGlobalEventJIP;

_unit setVariable [QGVAR(disableAiUavJipID), nil, true];

[QGVAR(disableAiUAV), [_unit, _disable]] call CBA_fnc_globalEvent;
};
4 changes: 2 additions & 2 deletions addons/dragging/functions/fnc_carryObject.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ private _UAVCrew = _target call EFUNC(common,getVehicleUAVCrew);

if (_UAVCrew isNotEqualTo []) then {
{
_target deleteVehicleCrew _x;
[_x, true] call EFUNC(common,disableAiUAV);
} forEach _UAVCrew;

_target setVariable [QGVAR(isUAV), true, true];
_target setVariable [QGVAR(isUAV), _UAVCrew, true];
};

// Check everything
Expand Down
4 changes: 2 additions & 2 deletions addons/dragging/functions/fnc_carryObjectPFH.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ if (_unit getHitPointDamage "HitLegs" >= 0.5) exitWith {
_idPFH call CBA_fnc_removePerFrameHandler;
};

// Drop static if crew is in it (UAV crew deletion may take a few frames)
if (_target isKindOf "StaticWeapon" && {!(_target getVariable [QGVAR(isUAV), false])} && {(crew _target) isNotEqualTo []}) exitWith {
// Drop static if either non-UAV crew or new UAV crew is in it (ignore saved UAV crew)
if (_target isKindOf "StaticWeapon" && {((crew _target) - (_target getVariable [QGVAR(isUAV), []])) isNotEqualTo []}) exitWith {
TRACE_2("static weapon crewed",_unit,_target);

[_unit, _target] call FUNC(dropObject_carry);
Expand Down
4 changes: 2 additions & 2 deletions addons/dragging/functions/fnc_dragObject.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ private _UAVCrew = _target call EFUNC(common,getVehicleUAVCrew);

if (_UAVCrew isNotEqualTo []) then {
{
_target deleteVehicleCrew _x;
[_x, true] call EFUNC(common,disableAiUAV);
} forEach _UAVCrew;

_target setVariable [QGVAR(isUAV), true, true];
_target setVariable [QGVAR(isUAV), _UAVCrew, true];
};

// Check everything
Expand Down
4 changes: 2 additions & 2 deletions addons/dragging/functions/fnc_dragObjectPFH.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ if (_unit distance _target > 10 && {(CBA_missionTime - _startTime) >= 1}) exitWi
_idPFH call CBA_fnc_removePerFrameHandler;
};

// Drop static if crew is in it (UAV crew deletion may take a few frames)
if (_target isKindOf "StaticWeapon" && {!(_target getVariable [QGVAR(isUAV), false])} && {(crew _target) isNotEqualTo []}) exitWith {
// Drop static if either non-UAV crew or new UAV crew is in it (ignore saved UAV crew)
if (_target isKindOf "StaticWeapon" && {((crew _target) - (_target getVariable [QGVAR(isUAV), []])) isNotEqualTo []}) exitWith {
TRACE_2("static weapon crewed",_unit,_target);

[_unit, _target] call FUNC(dropObject);
Expand Down
18 changes: 9 additions & 9 deletions addons/dragging/functions/fnc_dropObject.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ if (_unit getVariable ["ACE_isUnconscious", false]) then {
[_unit, "unconscious", 2] call EFUNC(common,doAnimation);
};

// Recreate UAV crew (add a frame delay or this may cause the vehicle to be moved to [0,0,0])
if (_target getVariable [QGVAR(isUAV), false]) then {
_target setVariable [QGVAR(isUAV), nil, true];
// Reenable UAV crew
private _UAVCrew = _target getVariable [QGVAR(isUAV), []];

if (_UAVCrew isNotEqualTo []) then {
// Reenable AI
{
[_x, false] call EFUNC(common,disableAiUAV);
} forEach _UAVCrew;

[{
params ["_target"];
if (!alive _target) exitWith {};
TRACE_2("restoring uav crew",_target,getPosASL _target);
createVehicleCrew _target;
}, [_target]] call CBA_fnc_execNextFrame;
_target setVariable [QGVAR(isUAV), nil, true];
};

// Fixes not being able to move when in combat pace
Expand Down
18 changes: 9 additions & 9 deletions addons/dragging/functions/fnc_dropObject_carry.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ if !(_target isKindOf "CAManBase") then {
[QEGVAR(common,fixFloating), _target, _target] call CBA_fnc_targetEvent;
};

// Recreate UAV crew (add a frame delay or this may cause the vehicle to be moved to [0,0,0])
if (_target getVariable [QGVAR(isUAV), false]) then {
_target setVariable [QGVAR(isUAV), nil, true];
// Reenable UAV crew
private _UAVCrew = _target getVariable [QGVAR(isUAV), []];

if (_UAVCrew isNotEqualTo []) then {
// Reenable AI
{
[_x, false] call EFUNC(common,disableAiUAV);
} forEach _UAVCrew;

[{
params ["_target"];
if (!alive _target) exitWith {};
TRACE_2("restoring uav crew",_target,getPosASL _target);
createVehicleCrew _target;
}, [_target]] call CBA_fnc_execNextFrame;
_target setVariable [QGVAR(isUAV), nil, true];
};

// Reset mass
Expand Down

0 comments on commit b6e9cab

Please sign in to comment.