Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve taskCamp, taskCamp and taskGarrison waypoints with eventhandlers #259

Merged
merged 6 commits into from
Jun 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions addons/main/XEH_PREP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ PREP(isNight);
PREP(showDialog);
PREP(parseData);

PREP(removeEventhandlers);

SUBPREP(GroupAction,doGroupAssault);
SUBPREP(GroupAction,doGroupFlank);
SUBPREP(GroupAction,doGroupSuppress);
Expand Down
20 changes: 20 additions & 0 deletions addons/main/functions/fnc_removeEventhandlers.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "script_component.hpp"
/*
* Author: nkenny
* Removes eventhandlers assigned by lambs modules
*
* Arguments:
* 0: Unit to clean up <OBJECT>
*
* Return Value:
* nil
*
* Example:
* [bob] call lambs_main_removeEventhandlers;
*
* Public: No
*/
params ["_unit", "_ehs"];
{
_unit removeEventHandler _x;
} forEach _ehs;
11 changes: 11 additions & 0 deletions addons/wp/XEH_preInit.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,16 @@ if (isServer) then {
_this spawn FUNC(taskRush);
}] call CBA_fnc_addEventhandler;

[QGVAR(taskCampReset), {
params ["_unit"];
{
_x enableAI 'ANIM';
_x enableAI 'PATH';
[_x, _x getVariable [QGVAR(eventhandlers), []]] call EFUNC(main,removeEventhandlers);
_x setVariable [QGVAR(eventhandlers), nil];
} foreach (units _unit);
[_unit, "", 2] call EFUNC(main,doAnimation);
_unit setUnitPos "AUTO";
}] call CBA_fnc_addEventhandler;

ADDON = true;
4 changes: 2 additions & 2 deletions addons/wp/functions/Modules/fnc_moduleGarrison.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ switch (_mode) do {
[
[LSTRING(Groups_DisplayName), "DROPDOWN", LSTRING(Groups_ToolTip), _groups apply { format ["%1 - %2 (%3 m)", side _x, groupId _x, round ((leader _x) distance _logic)] }, 0],
[LSTRING(Module_TaskGarrison_Radius_DisplayName), "SLIDER", LSTRING(Module_TaskGarrison_Radius_ToolTip), [10, 500], [2, 1], TASK_GARRISON_SIZE, 2],
[LSTRING(Module_TaskGarrison_ExitCondition_DisplayName), "DROPDOWN", LSTRING(Module_TaskGarrison_ExitCondition_Tooltip), [LSTRING(Random), LSTRING(All), LSTRING(FiredNear), LSTRING(Fired), LSTRING(Hit), LSTRING(None)], TASK_GARRISON_EXITCONDITIONS + 2],
[LSTRING(Module_TaskGarrison_ExitCondition_DisplayName), "DROPDOWN", LSTRING(Module_TaskGarrison_ExitCondition_Tooltip), [LSTRING(Random), LSTRING(All), LSTRING(None), LSTRING(Hit), LSTRING(Fired), LSTRING(FiredNear), LSTRING(Suppressed)], TASK_GARRISON_EXITCONDITIONS + 2],
[LSTRING(Module_TaskGarrison_SortByHeight_DisplayName), "BOOLEAN", LSTRING(Module_TaskGarrison_SortByHeight_Tooltip), TASK_GARRISON_SORTBYHEIGHT],
[LSTRING(Module_TaskGarrison_Teleport_DisplayName), "BOOLEAN", LSTRING(Module_TaskGarrison_Teleport_Tooltip), TASK_GARRISON_TELEPORT],
[LSTRING(Module_TaskGarrison_Patrol_DisplayName), "BOOLEAN", LSTRING(Module_TaskGarrison_Patrol_Tooltip), TASK_GARRISON_PATROL]
Expand Down Expand Up @@ -61,7 +61,7 @@ switch (_mode) do {
[
[LSTRING(Centers_DisplayName), "DROPDOWN", LSTRING(Centers_ToolTip), _targets apply { format ["%1 (%2 m)", vehicleVarName _x, round (_x distance _logic)] }, 0],
[LSTRING(Module_TaskGarrison_Radius_DisplayName), "SLIDER", LSTRING(Module_TaskGarrison_Radius_ToolTip), [10, 500], [2, 1], TASK_GARRISON_SIZE, 2],
[LSTRING(Module_TaskGarrison_ExitCondition_DisplayName), "DROPDOWN", LSTRING(Module_TaskGarrison_ExitCondition_Tooltip), [LSTRING(Random), LSTRING(All), LSTRING(FiredNear), LSTRING(Fired), LSTRING(Hit), LSTRING(None)], TASK_GARRISON_EXITCONDITIONS + 2],
[LSTRING(Module_TaskGarrison_ExitCondition_DisplayName), "DROPDOWN", LSTRING(Module_TaskGarrison_ExitCondition_Tooltip), [LSTRING(Random), LSTRING(All), LSTRING(None), LSTRING(Hit), LSTRING(Fired), LSTRING(FiredNear), LSTRING(Suppressed)], TASK_GARRISON_EXITCONDITIONS + 2],
[LSTRING(Module_TaskGarrison_SortByHeight_DisplayName), "BOOLEAN", LSTRING(Module_TaskGarrison_SortByHeight_Tooltip), TASK_GARRISON_SORTBYHEIGHT],
[LSTRING(Module_TaskGarrison_Teleport_DisplayName), "BOOLEAN", LSTRING(Module_TaskGarrison_Teleport_Tooltip), TASK_GARRISON_TELEPORT],
[LSTRING(Module_TaskGarrison_Patrol_DisplayName), "BOOLEAN", LSTRING(Module_TaskGarrison_Patrol_Tooltip), TASK_GARRISON_PATROL]
Expand Down
40 changes: 15 additions & 25 deletions addons/wp/functions/fnc_taskCamp.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ private _units = (units _group) select {!isPlayer _x && {isNull objectParent _x}
if (_pos isEqualTo []) then { _pos = _group; };
_pos = _pos call CBA_fnc_getPos;

// remove all waypoints
//[_group] call CBA_fnc_clearWaypoints;

// orders
_group setBehaviour "SAFE";
_group setSpeedMode "LIMITED";
Expand Down Expand Up @@ -172,18 +169,18 @@ private _dir = random 360;
{
_dir = _dir + (360 / count _units) - random (180 / count _units);
private _range = 1.35 + random 3.3;
private _pos2 = [(_pos select 0) + (sin _dir) * _range, (_pos select 1) + (cos _dir) * _range, 0];
private _campPos = [(_pos select 0) + (sin _dir) * _range, (_pos select 1) + (cos _dir) * _range, 0];

// teleport
if (_teleport) then {
_x setPos _pos2;
_x setDir (_x getDir _pos);
_x setPos _campPos;
};

// execute move
doStop _x;
_x doMove _pos2;
_x setDestination [_pos2, "LEADER DIRECT", false];
_x doMove _campPos;
_x setDestination [_campPos, "LEADER DIRECT", false];

// sort anims
private _anims = _unarmedAnims;
Expand All @@ -201,28 +198,21 @@ private _dir = random 360;
_unit disableAI "ANIM";
_unit disableAI "PATH";
_unit setDir (_unit getDir _center);
_unit addEventHandler ["Hit", {
_unit setUnitPos "MIDDLE";
private _handleHit = _unit addEventHandler ["Hit", {
params ["_unit"];
{
[_x, "ANIM"] remoteExec ["enableAI", _x];
[_x, "PATH"] remoteExec ["enableAI", _x];
} foreach units _unit;
[_unit, "", 2] call EFUNC(main,doAnimation);

_unit removeEventHandler ["Hit", _thisEventHandler];
[QGVAR(taskCampReset), _unit, _unit] call CBA_fnc_targetEvent;
}];
_unit addEventHandler ["FiredNear", {
private _handleFiredNear = _unit addEventHandler ["FiredNear", {
params ["_unit"];
{
[_x, "ANIM"] remoteExec ["enableAI", _x];
[_x, "PATH"] remoteExec ["enableAI", _x];
} foreach units _unit;
[_unit, "", 2] call EFUNC(main,doAnimation);

_unit removeEventHandler ["FiredNear", _thisEventHandler];
[QGVAR(taskCampReset), _unit, _unit] call CBA_fnc_targetEvent;
}];
}, [_x, _pos2, _pos, selectRandom _anims]] call CBA_fnc_waitUntilAndExecute;

private _handleSuppressed = _unit addEventHandler ["Suppressed", {
params ["_unit"];
[QGVAR(taskCampReset), _unit, _unit] call CBA_fnc_targetEvent;
}];
_unit setVariable [QGVAR(eventhandlers), [["Hit", _handleHit], ["FiredNear", _handleFiredNear], ["Suppressed", _handleSuppressed]]];
}, [_x, _campPos, _pos, selectRandom _anims]] call CBA_fnc_waitUntilAndExecute;
} forEach _units;

// waypoint and end state
Expand Down
78 changes: 47 additions & 31 deletions addons/wp/functions/fnc_taskGarrison.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -73,32 +73,31 @@ _group enableAttack false;
_group setVariable [QEGVAR(main,currentTactic), "taskGarrison", EGVAR(main,debug_functions)];

// declare units + sort vehicles + tweak count to match house positions
private _units = units _group;
_units = _units select {isNull objectParent _x};
private _units = (units _group) select {!isPlayer _x && {isNull objectParent _x}};

// add sub patrols
reverse _units;
if (_patrol) then {
private _group2 = createGroup [(side _group), true];
[_units deleteAt 0] join _group2;
if (count _units > 4) then { [_units deleteAt 0] join _group2; };
private _patrolGroup = createGroup [(side _group), true];
[_units deleteAt 0] join _patrolGroup;
if (count _units > 4) then { [_units deleteAt 0] join _patrolGroup; };

// performance
if (dynamicSimulationEnabled _group) then {
[_group2, true] remoteExecCall ["enableDynamicSimulation", 2];
[_patrolGroup, true] remoteExecCall ["enableDynamicSimulation", 2];
};

// id
_group2 setGroupIDGlobal [format ["Patrol (%1)", groupId _group2]];
_patrolGroup setGroupIDGlobal [format ["Patrol (%1)", groupId _patrolGroup]];

// orders
if (_area isEqualTo []) then {
[_group2, _pos, _radius, 4, nil, true] call FUNC(taskPatrol);
[_patrolGroup, _pos, _radius, 4, nil, true] call FUNC(taskPatrol);
} else {
private _area2 = +_area;
_area2 set [0, (_area2 select 0) * 2];
_area2 set [1, (_area2 select 1) * 2];
[_group2, _pos, _radius, 4, _area2, true] call FUNC(taskPatrol);
[_patrolGroup, _pos, _radius, 4, _area2, true] call FUNC(taskPatrol);
};
};

Expand All @@ -119,49 +118,71 @@ _units = _units - [objNull];
// enter buildings
if (count _units > count _houses) then {_units resize (count _houses);};
private _fnc_addEventHandler = {
params ["_type"];
params ["_unit", "_type"];
if (_type == 0) exitWith {};
if (_type == -2) then {
_type = floor (random 3);
_type = floor (random 4);
};

// variables
private _ehs = _unit getVariable [QGVAR(eventhandlers), []];

// add handlers
switch (_type) do {
case 0: {
_x addEventHandler ["Hit", {
case 1: {
private _handle = _unit addEventHandler ["Hit", {
params ["_unit"];
[_unit, "PATH"] remoteExec ["enableAI", _unit];
_unit setCombatMode "RED";
_unit removeEventHandler ["Hit", _thisEventHandler];
[_unit, _unit getVariable [GVAR(eventhandlers), []]] call EFUNC(main,removeEventhandlers);
_unit setVariable [QGVAR(eventhandlers), nil];
}];
_ehs pushBack ["Hit", _handle];
};
case 1: {
_x addEventHandler ["Fired", {
case 2: {
private _handle = _unit addEventHandler ["Fired", {
params ["_unit"];
[_unit, "PATH"] remoteExec ["enableAI", _unit];
_unit setCombatMode "RED";
_unit removeEventHandler ["Fired", _thisEventHandler];
[_unit, _unit getVariable [GVAR(eventhandlers), []]] call EFUNC(main,removeEventhandlers);
_unit setVariable [QGVAR(eventhandlers), nil];
}];
_ehs pushBack ["Fired", _handle];
};
case 2: {
_x addEventHandler ["FiredNear", {
case 3: {
private _handle = _unit addEventHandler ["FiredNear", {
params ["_unit", "_shooter", "_distance"];
if (side _unit != side _shooter && {_distance < (10 + random 10)}) then {
[_unit, "PATH"] remoteExec ["enableAI", _unit];
_unit doMove (getPosATL _shooter);
_unit setCombatMode "RED";
_unit removeEventHandler ["FiredNear", _thisEventHandler];
[_unit, _unit getVariable [GVAR(eventhandlers), []]] call EFUNC(main,removeEventhandlers);
_unit setVariable [QGVAR(eventhandlers), nil];
};
}];
_ehs pushBack ["FiredNear", _handle];
};
case 3: {
// DO NOTHING
case 4: {
private _handle = _unit addEventHandler ["Suppressed", {
params ["_unit"];
[_unit, "PATH"] remoteExec ["enableAI", _unit];
_unit setCombatMode "RED";
[_unit, _unit getVariable [GVAR(eventhandlers), []]] call EFUNC(main,removeEventhandlers);
_unit setVariable [QGVAR(eventhandlers), nil];
}];
_ehs pushBack ["Suppressed", _handle];
};
};

// set EH
_unit setVariable [QGVAR(eventhandlers), _ehs];
};
// spread out
{
// prepare
doStop _x;
private _house = _houses deleteAt 0;

// move and delay stopping + stance
if (_teleport) then {
if (surfaceIsWater _house) then {
Expand Down Expand Up @@ -190,18 +211,14 @@ private _fnc_addEventHandler = {
};

if (_exitCondition == -1) then {
for "_i" from 0 to 2 do {
_i call _fnc_addEventHandler;
for "_i" from 0 to 4 do {
[_x, _i] call _fnc_addEventHandler;
};
} else {
_exitCondition call _fnc_addEventHandler;
[_x, _exitCondition] call _fnc_addEventHandler;
};
// end
true
} count _units;

// end with patrol
// disabled!
} forEach _units;

// waypoint
_pos set [2, 0]; // Stop Waypoints from Flying
Expand All @@ -214,6 +231,5 @@ if (EGVAR(main,debug_functions)) then {
["%1 taskGarrison: %2 garrisoned", side _group, groupID _group] call EFUNC(main,debugLog);
};


// end
true
47 changes: 21 additions & 26 deletions addons/wp/functions/fnc_taskHunt.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
*
* Public: Yes
*/

if !(canSuspend) exitWith {
_this spawn FUNC(taskHunt);
};
// 1. FIND TRACKER

// init
params [
["_group", grpNull, [grpNull, objNull]],
["_radius", TASK_HUNT_SIZE, [0]],
Expand All @@ -36,11 +38,22 @@ params [
["_enableReinforcement", TASK_HUNT_ENABLEREINFORCEMENT, [false]]
];

// functions ---

// shoot flare
private _fnc_flare = {
params ["_leader"];
private _shootflare = "F_20mm_Red" createvehicle (_leader ModelToWorld [0, 0, 200]);
_shootflare setVelocity [0, 0, -10];
};

// functions end ---

// sort grp
if (!local _group) exitWith {false};
if (_group isEqualType objNull) then { _group = group _group; };

// 2. SET GROUP BEHAVIOR
// orders
_group setbehaviour "SAFE";
_group setSpeedMode "LIMITED";
_group enableAttack false;
Expand All @@ -53,16 +66,7 @@ if (_enableReinforcement) then {
_group setVariable [QEGVAR(danger,enableGroupReinforce), true, true];
};

// FUNCTIONS -------------------------------------------------------------

// FLARE SCRIPT
private _fnc_flare = {
params ["_leader"];
private _shootflare = "F_20mm_Red" createvehicle (_leader ModelToWorld [0, 0, 200]);
_shootflare setVelocity [0, 0, -10];
};

// 3. DO THE HUNT SCRIPT! ---------------------------------------------------
// hunt loop
waitUntil {

// performance
Expand All @@ -73,35 +77,26 @@ waitUntil {

// settings
private _combat = (behaviour (leader _group)) isEqualTo "COMBAT";
private _onFoot = (isNull objectParent (leader _group));
private _onFoot = isNull (objectParent (leader _group));

// give orders
if (!isNull _target) then {
_group move (_target getPos [random (linearConversion [50, 1000, (leader _group) distance _target, 25, 300, true]), random 360]);
_group move (_target getPos [random (linearConversion [50, 1000, (leader _group) distance2D _target, 25, 300, true]), random 360]);
_group setFormDir ((leader _group) getDir _target);
_group setSpeedMode "NORMAL";
_group enableGunLights "forceOn";
_group enableIRLasers true;

// debug
if (EGVAR(main,debug_functions)) then {["%1 taskHunt: %2 targets %3 at %4M", side _group, groupID _group, name _target, floor (leader _group distance _target)] call EFUNC(main,debugLog);};
if (EGVAR(main,debug_functions)) then {["%1 taskHunt: %2 targets %3 at %4M", side _group, groupID _group, name _target, floor (leader _group distance2D _target)] call EFUNC(main,debugLog);};

// flare
if (!_combat && {_onFoot} && {RND(0.8)}) then { [leader _group] call _fnc_flare; };

// suppress nearby buildings
if (_combat && {(nearestBuilding _target distance2D _target < 25)}) then {
{
[_x, getPosASL _target] call EFUNC(main,doSuppress);
true
} count units _group;
};
};

// WAIT FOR IT! / end
// wait for it or end
sleep _cycle;
((units _group) findIf {_x call EFUNC(main,isAlive)} == -1)

(units _group) findIf {_x call EFUNC(main,isAlive)} == -1
};

// end
Expand Down
Loading