Skip to content

Commit f03ee22

Browse files
authored
Improved core brain performance (#478)
* Improved core brain performance Adds performance improvements to brains.sqf evaluation logic-- as well as the danger.fsm handling of variables. * Linter and vehicle fixed
1 parent cf75b07 commit f03ee22

File tree

4 files changed

+51
-84
lines changed

4 files changed

+51
-84
lines changed

addons/danger/functions/fnc_brain.sqf

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
* evaluates danger causes and returns most dangerous state and appropriate response
55
*
66
* Arguments:
7-
* 0: unit doing the avaluation <OBJECT>
7+
* 0: unit doing the evaluation <OBJECT>
88
* 1: danger queue <ARRAY>
99
*
1010
* Return Value:
1111
* array
1212
*
1313
* Example:
14-
* [bob, []] call lambs_danger_fnc_dangerBrain;
14+
* [bob, []] call lambs_danger_fnc_brain;
1515
*
1616
* Public: No
1717
*/
@@ -27,6 +27,7 @@
2727
Hide actions
2828
5 DeadBodyGroup
2929
6 DeadBody
30+
7 Scream
3031
- Panic
3132
3233
Engage actions
@@ -35,76 +36,55 @@
3536
8 CanFire
3637
3738
Assess actions
38-
7 Scream
3939
10 Assess
4040
*/
41-
#define ACTION_IMMEDIATE 0
42-
#define ACTION_HIDE 1
43-
#define ACTION_ENGAGE 2
44-
#define ACTION_ASSESS 3
4541

4642
params ["_unit", ["_queue", []]];
4743

48-
// init ~ immediate action, hide, engage, assess
49-
private _return = [false, false, false, false];
50-
private _group = group _unit;
51-
5244
// empty queue ~ exit with assess!
5345
if (_queue isEqualTo []) exitWith {
5446
private _causeArray = [DANGER_ASSESS, getPosWorld _unit, time + GVAR(dangerUntil), assignedTarget _unit];
5547
_unit setVariable [QEGVAR(main,FSMDangerCauseData), _causeArray, EGVAR(main,debug_functions)]; // debug variable
56-
[false, false, false, true, _causeArray]
48+
[_causeArray, false, false, false]
5749
};
5850

59-
// modify priorities ~ own function!
60-
private _priorities = [_unit] call FUNC(brainAdjust);
61-
62-
// pick the most relevant danger cause
63-
private _priority = -1;
64-
private _index = -1;
65-
{
66-
private _cause = _x select 0;
67-
if ((_priorities select _cause) > _priority) then {
68-
_index = _forEachIndex;
69-
_priority = _priorities select _cause;
70-
};
71-
} forEach _queue;
51+
// modify priorities ~ Unused. Disabled for performance reasons. - nkenny
52+
// private _priorities = [_unit] call FUNC(brainAdjust);
53+
54+
// sort the most dangerous state
55+
_queue = _queue apply {[GVAR(fsmPriorities) select (_x select 0), _x]};
56+
_queue sort false;
7257

7358
// select cause
74-
private _causeArray = _queue select _index;
75-
_causeArray params ["_dangerCause", "_dangerPos", "", "_dangerCausedBy"]; // "_dangerUntil" - skipped for future use -nkenny
59+
private _causeArray = (_queue select 0) select 1;
60+
_causeArray params ["_dangerCause", "", "", "_dangerCausedBy"]; // "_dangerPos" and "_dangerUntil" are unused - nkenny
7661

7762
// debug variable
7863
_unit setVariable [QEGVAR(main,FSMDangerCauseData), _causeArray, EGVAR(main,debug_functions)];
7964

80-
// add selected cause to return array
81-
_return pushBack _causeArray;
82-
8365
// assess actions
8466
if (_dangerCause isEqualTo DANGER_ASSESS) exitWith {
85-
_return set [ACTION_ASSESS, true];
86-
_return
67+
[_causeArray, false, false, false]
8768
};
8869

8970
// immediate actions
90-
if (_dangerCause in [DANGER_HIT, DANGER_BULLETCLOSE, DANGER_EXPLOSION, DANGER_FIRE] && {getSuppression _unit < 0.9}) exitWith {
91-
_return set [ACTION_IMMEDIATE, (side _group) isNotEqualTo side (group _dangerCausedBy)];
92-
_return
71+
private _group = group _unit;
72+
if (_dangerCause in [DANGER_HIT, DANGER_BULLETCLOSE, DANGER_EXPLOSION, DANGER_FIRE]) exitWith {
73+
[_causeArray, (side _group) isNotEqualTo side (group _dangerCausedBy) && (getSuppression _unit < 0.9), false, false]
9374
};
9475

9576
// engage actions
9677
if (_dangerCause in [DANGER_ENEMYDETECTED, DANGER_ENEMYNEAR, DANGER_CANFIRE]) exitWith {
9778

98-
// gesture + share information
99-
if (_dangerCause isEqualTo DANGER_ENEMYNEAR && { (_group getVariable [QGVAR(contact), 0]) < time }) then {
100-
[_unit, ["gestureFreeze", "gesturePoint"] select (_unit distance2D _dangerPos < 50)] call EFUNC(main,doGesture);
79+
// share information
80+
if (_dangerCause isEqualTo DANGER_ENEMYNEAR) then {
81+
//[_unit, ["gestureFreeze", "gesturePoint"] select (_unit distance2D _dangerPos < 100)] call EFUNC(main,doGesture);
10182
[_unit, ["Combat", "Stealth"] select (behaviour _unit isEqualTo "STEALTH"), "contact", 100] call EFUNC(main,doCallout);
10283
[_unit, _dangerCausedBy, EGVAR(main,radioShout), true] call EFUNC(main,doShareInformation);
10384
};
10485

10586
// return
106-
_return set [ACTION_ENGAGE, (side _group) isNotEqualTo side (group _dangerCausedBy)];
107-
_return
87+
[_causeArray, false, false, (side _group) isNotEqualTo side (group _dangerCausedBy)]
10888
};
10989

11090
// hide actions
@@ -117,9 +97,8 @@ if (_panic || {_dangerCause in [DANGER_DEADBODYGROUP, DANGER_DEADBODY, DANGER_SC
11797
};
11898

11999
// return
120-
_return set [ACTION_HIDE, true];
121-
_return
100+
[_causeArray, false, true, false]
122101
};
123102

124103
// end
125-
_return
104+
[_causeArray, false, false, false]

addons/danger/functions/fnc_brainAssess.sqf

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
* handles assessment of own situation
55
*
66
* Arguments:
7-
* 0: unit doing the avaluation <OBJECT>
8-
* 1: type of data <NUMBER>
9-
* 2: position of danger <ARRAY>
10-
* 3: known target threatening unit <OBJECT>
7+
* 0: unit doing the evaluation <OBJECT>
8+
* 1: known target threatening unit <OBJECT>
119
*
1210
* Return Value:
1311
* number, timeout!
@@ -23,40 +21,38 @@
2321
10 Assess
2422
*/
2523

26-
params ["_unit", "", "", ["_target", objNull]];
24+
params ["_unit", ["_target", objNull]];
2725

2826
// timeout
29-
private _timeout = time + 3;
27+
private _timeout = time + 2;
28+
private _suppressed = (getSuppression _unit) isNotEqualTo 0;
3029

3130
// check if stopped
3231
if (
33-
!(_unit checkAIFeature "PATH")
32+
_suppressed
33+
|| !(_unit checkAIFeature "PATH")
3434
|| ((behaviour _unit)) isEqualTo "STEALTH"
3535
|| (currentCommand _unit) isEqualTo "STOP"
3636
|| (combatMode _unit) in ["BLUE", "GREEN"]
37-
|| (getUnitState _unit) in ["PLANNING", "DELAY"]
3837
) exitWith {_timeout};
3938

4039
// group memory
4140
private _groupMemory = (group _unit) getVariable [QEGVAR(main,groupMemory), []];
42-
private _notSuppressed = (getSuppression _unit) isEqualTo 0;
4341

4442
// sympathetic CQB/suppressive fire
45-
if (_groupMemory isNotEqualTo [] && _notSuppressed) exitWith {
43+
if (_groupMemory isNotEqualTo []) exitWith {
4644
[_unit, _groupMemory] call EFUNC(main,doAssaultMemory);
4745
_timeout
4846
};
4947

5048
// building
51-
if (RND(EGVAR(main,indoorMove)) && _notSuppressed && {_unit call EFUNC(main,isIndoor)}) exitWith {
49+
if (RND(EGVAR(main,indoorMove)) && {_unit call EFUNC(main,isIndoor)}) exitWith {
5250
[_unit, _target] call EFUNC(main,doReposition);
53-
_timeout - 1
51+
_timeout
5452
};
5553

5654
// reset look
57-
if ((getSuppression _unit) isEqualTo 0) then {
58-
_unit setUnitPosWeak "MIDDLE";
59-
};
55+
_unit setUnitPosWeak "MIDDLE";
6056
//_unit doWatch objNull;
6157

6258
// end

addons/danger/functions/fnc_brainVehicle.sqf

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,15 @@ if !((effectiveCommander _vehicle) isEqualTo _unit && {_unit call EFUNC(main,isA
2929
// no queue
3030
if (_queue isEqualTo []) then {_queue pushBack [10, getPosWorld _vehicle, time + GVAR(dangerUntil), assignedTarget _unit];};
3131

32-
// modify priorities ~ consider adding vehicle specific changes!
33-
private _priorities = _unit call FUNC(brainAdjust);
32+
// modify priorities ~ consider adding vehicle specific changes! - removed for performance ~ nkenny
33+
//private _priorities = _unit call FUNC(brainAdjust);
3434

3535
// pick the most relevant danger cause
36-
private _priority = -1;
37-
private _index = -1;
38-
{
39-
private _cause = _x select 0;
40-
if ((_priorities select _cause) > _priority) then {
41-
_index = _forEachIndex;
42-
_priority = _priorities select _cause;
43-
};
44-
} forEach _queue;
36+
_queue = _queue apply {[GVAR(fsmPriorities) select (_x select 0), _x]};
37+
_queue sort false;
4538

4639
// select cause
47-
private _causeArray = _queue select _index;
40+
private _causeArray = (_queue select 0) select 1;
4841
_causeArray params ["_cause", "_dangerPos", "", "_dangerCausedBy"]; // "_dangerUntil" may be re-implemented in the future ~ nkenny
4942

5043
// debug variable

addons/danger/scripts/lambs_danger.fsm

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*%FSM<COMPILE "G:\SteamLibrary\steamapps\common\Arma 3 Tools\FSMEditor\scriptedFSM.cfg, Danger">*/
1+
/*%FSM<COMPILE "scriptedFSM.cfg, Danger">*/
22
/*%FSM<HEAD>*/
33
/*
44
item0[] = {"Start_Danger",0,250,-75.000000,-175.000000,25.000000,-125.000000,0.000000,"Start Danger"};
@@ -16,7 +16,7 @@ item11[] = {"DEFINE_ENUM_BEG",-1,250,350.000000,-175.000000,700.000000,0.000000,
1616
item12[] = {"Immediate",4,218,-175.000000,425.000000,-75.000000,475.000000,6.000000,"Immediate"};
1717
item13[] = {"Hide",4,218,0.000000,425.000000,100.000000,475.000000,4.000000,"Hide"};
1818
item14[] = {"Engage",4,218,175.000000,425.000000,275.000000,475.000000,2.000000,"Engage"};
19-
item15[] = {"Roll_dodge",2,4346,-175.000000,600.000000,-75.000000,650.000000,0.000000,"Roll/dodge"};
19+
item15[] = {"Roll_dodge",2,250,-175.000000,600.000000,-75.000000,650.000000,0.000000,"Roll/dodge"};
2020
item16[] = {"Stay_firm",2,250,0.000000,600.000000,100.000000,650.000000,0.000000,"Stay firm"};
2121
item17[] = {"Attack",2,250,175.000000,600.000000,275.000000,650.000000,0.000000,"Attack"};
2222
item18[] = {"Check_self",2,250,175.000000,275.000000,275.000000,325.000000,0.000000,"Check self"};
@@ -112,8 +112,8 @@ link42[] = {36,24};
112112
link43[] = {37,3};
113113
link44[] = {38,39};
114114
link45[] = {41,40};
115-
globals[] = {0.000000,0,0,0,0,640,480,1,67,6316128,1,-1017.364929,1292.536743,1481.373169,-707.215454,933,884,1};
116-
window[] = {2,-1,-1,-1,-1,967,208,1648,208,3,951};
115+
globals[] = {0.000000,0,0,0,0,640,480,1,67,6316128,1,-1051.298584,1485.311401,1723.900391,-657.740051,933,876,1};
116+
window[] = {2,-1,-1,-1,-1,811,52,1492,52,3,951};
117117
*//*%FSM</HEAD>*/
118118
class FSM
119119
{
@@ -126,7 +126,6 @@ class FSM
126126
name = "Start_Danger";
127127
itemno = 0;
128128
init = /*%FSM<STATEINIT""">*/"//systemchat format [""%1 danger %2 %3s"", side _this, name _this, round time];" \n
129-
"private [""_timeout""];" \n
130129
"" \n
131130
"// add current data to queue for evaluation" \n
132131
"_queue pushBack [_dangerCause, _dangerPos, _dangerUntil, _dangerCausedBy];" \n
@@ -290,7 +289,7 @@ class FSM
290289
"private _evaluation = [_this, _queue] call lambs_danger_fnc_brain;" \n
291290
"" \n
292291
"// get variables" \n
293-
"_evaluation params [""_immediate"", ""_hide"", ""_engage"", ""_assess"", ""_cause""];" \n
292+
"_evaluation params [""_cause"", ""_immediate"", ""_hide"", ""_engage""];" \n
294293
"_cause params [""_dangerCause"", ""_dangerPos"", ""_dangerUntil"", ""_dangerCausedBy""];" \n
295294
"" \n
296295
"// reset queue" \n
@@ -357,7 +356,7 @@ class FSM
357356
init = /*%FSM<STATEINIT""">*/"//systemchat format [""%1 immediate %2"", side _this, name _this];" \n
358357
"" \n
359358
"// brain" \n
360-
"_timeout = [_this, _dangerCause, _dangerPos] call lambs_danger_fnc_brainReact;" \n
359+
"_dangerUntil = [_this, _dangerCause, _dangerPos] call lambs_danger_fnc_brainReact;" \n
361360
""/*%FSM</STATEINIT""">*/;
362361
precondition = /*%FSM<STATEPRECONDITION""">*/""/*%FSM</STATEPRECONDITION""">*/;
363362
class Links
@@ -369,7 +368,7 @@ class FSM
369368
priority = 0.000000;
370369
to="Reset_foot";
371370
precondition = /*%FSM<CONDPRECONDITION""">*/""/*%FSM</CONDPRECONDITION""">*/;
372-
condition=/*%FSM<CONDITION""">*/"time > _timeout"/*%FSM</CONDITION""">*/;
371+
condition=/*%FSM<CONDITION""">*/"time > _dangerUntil"/*%FSM</CONDITION""">*/;
373372
action=/*%FSM<ACTION""">*/""/*%FSM</ACTION""">*/;
374373
};
375374
/*%FSM</LINK>*/
@@ -384,7 +383,7 @@ class FSM
384383
init = /*%FSM<STATEINIT""">*/"//systemchat format [""%1 hide %2"", side _this, name _this];" \n
385384
"" \n
386385
"// brain" \n
387-
"_timeout = [_this, _dangerCause, _dangerPos] call lambs_danger_fnc_brainHide;"/*%FSM</STATEINIT""">*/;
386+
"_dangerUntil = [_this, _dangerCause, _dangerPos] call lambs_danger_fnc_brainHide;"/*%FSM</STATEINIT""">*/;
388387
precondition = /*%FSM<STATEPRECONDITION""">*/""/*%FSM</STATEPRECONDITION""">*/;
389388
class Links
390389
{
@@ -395,7 +394,7 @@ class FSM
395394
priority = 0.000000;
396395
to="Reset_foot";
397396
precondition = /*%FSM<CONDPRECONDITION""">*/""/*%FSM</CONDPRECONDITION""">*/;
398-
condition=/*%FSM<CONDITION""">*/"time > _timeout"/*%FSM</CONDITION""">*/;
397+
condition=/*%FSM<CONDITION""">*/"time > _dangerUntil"/*%FSM</CONDITION""">*/;
399398
action=/*%FSM<ACTION""">*/""/*%FSM</ACTION""">*/;
400399
};
401400
/*%FSM</LINK>*/
@@ -410,7 +409,7 @@ class FSM
410409
init = /*%FSM<STATEINIT""">*/"//systemchat format [""%1 engage %2"", side _this, name _this];" \n
411410
"" \n
412411
"// brain" \n
413-
"_timeout = [_this, _dangerCause, _dangerCausedBy] call lambs_danger_fnc_brainEngage;"/*%FSM</STATEINIT""">*/;
412+
"_dangerUntil = [_this, _dangerCause, _dangerCausedBy] call lambs_danger_fnc_brainEngage;"/*%FSM</STATEINIT""">*/;
414413
precondition = /*%FSM<STATEPRECONDITION""">*/""/*%FSM</STATEPRECONDITION""">*/;
415414
class Links
416415
{
@@ -421,7 +420,7 @@ class FSM
421420
priority = 0.000000;
422421
to="Reset_foot";
423422
precondition = /*%FSM<CONDPRECONDITION""">*/""/*%FSM</CONDPRECONDITION""">*/;
424-
condition=/*%FSM<CONDITION""">*/"time > _timeout" \n
423+
condition=/*%FSM<CONDITION""">*/"time > _dangerUntil" \n
425424
"|| {count _queue >3}"/*%FSM</CONDITION""">*/;
426425
action=/*%FSM<ACTION""">*/""/*%FSM</ACTION""">*/;
427426
};
@@ -578,7 +577,7 @@ class FSM
578577
init = /*%FSM<STATEINIT""">*/"//systemchat format [""%1 assess %2"", side _this, name _this];" \n
579578
"" \n
580579
"// brain" \n
581-
"_timeout = [_this, _dangerCause, _dangerPos, _dangerCausedby] call lambs_danger_fnc_brainAssess;"/*%FSM</STATEINIT""">*/;
580+
"_dangerUntil = [_this, _dangerCausedby] call lambs_danger_fnc_brainAssess;"/*%FSM</STATEINIT""">*/;
582581
precondition = /*%FSM<STATEPRECONDITION""">*/""/*%FSM</STATEPRECONDITION""">*/;
583582
class Links
584583
{
@@ -589,7 +588,7 @@ class FSM
589588
priority = 0.000000;
590589
to="Reset_foot";
591590
precondition = /*%FSM<CONDPRECONDITION""">*/""/*%FSM</CONDPRECONDITION""">*/;
592-
condition=/*%FSM<CONDITION""">*/"time > _timeout"/*%FSM</CONDITION""">*/;
591+
condition=/*%FSM<CONDITION""">*/"time > _dangerUntil"/*%FSM</CONDITION""">*/;
593592
action=/*%FSM<ACTION""">*/""/*%FSM</ACTION""">*/;
594593
};
595594
/*%FSM</LINK>*/

0 commit comments

Comments
 (0)