Skip to content

Commit

Permalink
Headless - Code optimisation (#9873)
Browse files Browse the repository at this point in the history
* Headless optimisations

* Swapped blacklist for unitIsUAV check

* Moved UAV check

* Update addons/headless/functions/fnc_transferGroups.sqf

Co-authored-by: Grim <69561145+LinkIsGrim@users.noreply.github.com>

* Update addons/headless/functions/fnc_transferGroups.sqf

Co-authored-by: Grim <69561145+LinkIsGrim@users.noreply.github.com>

* Update fnc_transferGroups.sqf

---------

Co-authored-by: Grim <69561145+LinkIsGrim@users.noreply.github.com>
  • Loading branch information
johnb432 and LinkIsGrim authored Apr 4, 2024
1 parent e6cc5fc commit 0d401b2
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 40 deletions.
4 changes: 2 additions & 2 deletions addons/headless/XEH_postInit.sqf
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "script_component.hpp"

["ace_settingsInitialized", {
["CBA_settingsInitialized", {
// Register and remove HCs if not client that is not server and distribution or end mission enabled
if ((!hasInterface || isServer) && {XGVAR(enabled) || XGVAR(endMission) != 0}) then {
if (isServer) then {
// Request rebalance on any unit spawn (only if distribution enabled)
if (XGVAR(enabled)) then {
["AllVehicles", "initPost", LINKFUNC(handleSpawn), nil, nil, true] call CBA_fnc_addClassEventHandler;
["CAManBase", "initPost", LINKFUNC(handleSpawn), nil, nil, true] call CBA_fnc_addClassEventHandler;
};
// Add disconnect EH
addMissionEventHandler ["HandleDisconnect", {call FUNC(handleDisconnect)}];
Expand Down
1 change: 0 additions & 1 deletion addons/headless/XEH_preInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ if (isServer) then {
GVAR(headlessClients) = [];
GVAR(inRebalance) = false;
GVAR(endMissionCheckDelayed) = false;
GVAR(blacklistType) = [BLACKLIST_UAV];
[QXGVAR(headlessClientJoined), LINKFUNC(handleConnectHC)] call CBA_fnc_addEventHandler;
};

Expand Down
7 changes: 2 additions & 5 deletions addons/headless/functions/fnc_handleConnectHC.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@ params ["_headlessClient"];

// Exit if HC already registered
// No need to check if distribution or end mission enabled, as if disabled this will never run
if (_headlessClient in GVAR(headlessClients)) exitWith {};

// Register for use
GVAR(headlessClients) pushBack _headlessClient;
if (GVAR(headlessClients) pushBackUnique _headlessClient == -1) exitWith {};

if (XGVAR(log)) then {
INFO_1("Registered HC: %1",_headlessClient);
};

// Exit if AI distribution is disabled
if (!XGVAR(enabled)) exitWith {true};
if (!XGVAR(enabled)) exitWith {};

// Rebalance
[true] call FUNC(rebalance);
8 changes: 3 additions & 5 deletions addons/headless/functions/fnc_handleDisconnect.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

params ["_object"];
TRACE_1("HandleDisconnect",_this);
TRACE_1("HandleDisconnect",_object);

// Exit if not HC
if !(_object in GVAR(headlessClients)) exitWith {
Expand All @@ -28,9 +28,7 @@ if !(_object in GVAR(headlessClients)) exitWith {
if (CBA_missionTime < 150) then {
TRACE_1("Mission start delay",CBA_missionTime);
GVAR(endMissionCheckDelayed) = true;
[{
call FUNC(endMissionNoPlayers);
}, [], 150 - CBA_missionTime] call CBA_fnc_waitAndExecute;
[LINKFUNC(endMissionNoPlayers), [], 150 - CBA_missionTime] call CBA_fnc_waitAndExecute;
} else {
// End instantly or after delay
if (XGVAR(endMission) == 1) then {
Expand All @@ -39,7 +37,7 @@ if !(_object in GVAR(headlessClients)) exitWith {
} else {
TRACE_2("Delayed 60s end",GVAR(endMission),CBA_missionTime);
GVAR(endMissionCheckDelayed) = true;
[FUNC(endMissionNoPlayers), [], 60] call CBA_fnc_waitAndExecute;
[LINKFUNC(endMissionNoPlayers), [], 60] call CBA_fnc_waitAndExecute;
};
};
};
Expand Down
17 changes: 6 additions & 11 deletions addons/headless/functions/fnc_handleSpawn.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,22 @@
* Handles AI spawn and requests a rebalance if applicable.
*
* Arguments:
* 0: Object <OBJECT>
* 0: Unit <OBJECT>
*
* Return Value:
* None
*
* Example:
* [object] call ace_headless_fnc_handleSpawn
* [cursorObject] call ace_headless_fnc_handleSpawn
*
* Public: No
*/

params ["_object"];
TRACE_1("Spawn",_object);
params ["_unit"];
TRACE_1("Spawn",_unit);

// Exit if HC transferring disabled or object not a unit (including unit inside vehicle) or is player
if (!(_object in allUnits) || {isPlayer _object}) exitWith {};

// Exit and blacklist if of blacklist type
if ({_object isKindOf _x} count GVAR(blacklistType) > 0) exitWith {
_object setVariable [QXGVAR(blacklist), true];
};
// Exit if unit is player or UAV crew
if (isPlayer _unit || {unitIsUAV _unit}) exitWith {};

// Rebalance
[false] call FUNC(rebalance);
2 changes: 1 addition & 1 deletion addons/headless/functions/fnc_rebalance.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ TRACE_3("Rebalance",GVAR(inRebalance),GVAR(headlessClients),_force);
if (GVAR(inRebalance) || {GVAR(headlessClients) isEqualTo []}) exitWith {};

// Transfer after rebalance delay
[FUNC(transferGroups), [_force], XGVAR(Delay)] call CBA_fnc_waitAndExecute;
[LINKFUNC(transferGroups), _force, XGVAR(delay)] call CBA_fnc_waitAndExecute;

// Currently in rebalance flag
GVAR(inRebalance) = true;
12 changes: 7 additions & 5 deletions addons/headless/functions/fnc_transferGroups.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ private _numTransferredHC3 = 0;
_transfer = false;
};

// No transfer if player in this group
if (isPlayer _x) exitWith {
// No transfer if player or UAV in this group
if (isPlayer _x || {unitIsUAV _x}) exitWith {
_transfer = false;
};

Expand All @@ -89,14 +89,16 @@ private _numTransferredHC3 = 0;
_transfer = false;
};

// No transfer if vehicle unit is in or crew in that vehicle is blacklisted
if (vehicle _x != _x && {(vehicle _x) getVariable [QXGVAR(blacklist), false]}) exitWith {
private _vehicle = objectParent _x;

// No transfer if the vehicle the unit is in or if the crew in that vehicle is blacklisted
if ((_vehicle getVariable [QXGVAR(blacklist), false]) || {unitIsUAV _vehicle}) exitWith {
_transfer = false;
};

// Save gear if unit about to be transferred with current loadout (naked unit work-around)
if (XGVAR(transferLoadout) == 1) then {
_x setVariable [QGVAR(loadout), [_x] call CBA_fnc_getLoadout, true];
_x setVariable [QGVAR(loadout), _x call CBA_fnc_getLoadout, true];
};
} forEach (units _x);
};
Expand Down
16 changes: 7 additions & 9 deletions addons/headless/initSettings.inc.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[ELSTRING(common,Enabled), LSTRING(EnabledDesc)],
format ["ACE %1", LLSTRING(Module)],
false,
true,
1,
{[QGVAR(enabled), _this] call EFUNC(common,cbaSettings_settingChanged)},
true
] call CBA_fnc_addSetting;
Expand All @@ -15,9 +15,8 @@
[LSTRING(Delay), LSTRING(DelayDesc)],
format ["ACE %1", LLSTRING(Module)],
[0, 60, 15, -1],
true,
{[QGVAR(delay), _this] call EFUNC(common,cbaSettings_settingChanged)},
true
1,
{[QGVAR(delay), _this] call EFUNC(common,cbaSettings_settingChanged)}
] call CBA_fnc_addSetting;

[
Expand All @@ -26,7 +25,7 @@
[LSTRING(EndMission), LSTRING(EndMissionDesc)],
format ["ACE %1", LLSTRING(Module)],
[[0, 1, 2], [ELSTRING(Common,Disabled), LSTRING(Instant), LSTRING(Delayed)], 0],
true,
1,
{[QGVAR(delay), _this] call EFUNC(common,cbaSettings_settingChanged)},
true
] call CBA_fnc_addSetting;
Expand All @@ -37,9 +36,8 @@
[LSTRING(Log), LSTRING(LogDesc)],
format ["ACE %1", LLSTRING(Module)],
false,
true,
{[QGVAR(enabled), _this] call EFUNC(common,cbaSettings_settingChanged)},
true
1,
{[QGVAR(enabled), _this] call EFUNC(common,cbaSettings_settingChanged)}
] call CBA_fnc_addSetting;

[
Expand All @@ -48,7 +46,7 @@
[LSTRING(TransferLoadout), LSTRING(TransferLoadoutDesc)],
format ["ACE %1", LLSTRING(Module)],
[[0, 1, 2], [ELSTRING(Common,Disabled), LSTRING(TransferLoadoutCurrent), LSTRING(TransferLoadoutConfig)], 0],
true,
1,
{},
true // needs mission restart
] call CBA_fnc_addSetting;
1 change: 0 additions & 1 deletion addons/headless/script_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@
#include "\z\ace\addons\main\script_macros.hpp"

#define DELAY_DEFAULT 15
#define BLACKLIST_UAV "UAV", "UAV_AI_base_F", "B_UAV_AI", "O_UAV_AI", "I_UAV_AI"

0 comments on commit 0d401b2

Please sign in to comment.