Arma Reforger Explorer
1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Toggle main menu visibility
Loading...
Searching...
No Matches
SCR_AILookAtThreatSystemLogic.c
Go to the documentation of this file.
1
4
class
SCR_AILookAtThreatSystemLogic
:
AITaskScripted
5
{
6
// Outputs
7
protected
static
const
string
PORT_CURRENT_POSITION
=
"CurrentPos"
;
// Position where we currently want to look at (major or minor threat)
8
protected
static
const
string
PORT_RESET_LOOK
=
"ResetLook"
;
9
protected
static
const
string
PORT_ACTIVE
=
"Active"
;
10
protected
static
const
string
PORT_RAISE_WEAPON
=
"RaiseWeapon"
;
11
protected
static
const
string
PORT_USE_OPTICS
=
"UseOptics"
;
12
13
protected
static
const
int
STATE_IDLE
= 0;
// Nothing to look at, although this shouldn't be possible.
14
protected
static
const
int
STATE_OBSERVING_MAJOR
= 1;
// Looking at major sector (most dangerous)
15
protected
static
const
int
STATE_OBSERVING_MINOR
= 2;
// Looking at minor sector
16
protected
static
const
int
STATE_INTERMISSION
= 3;
// A pause after looking
17
18
protected
static
const
float
DANGER_THRESHOLD_RAISE_WEAPON
= 0.5;
// Danger of sector beyond which we raise weapon
19
protected
static
const
float
DISTANCE_USE_OPTICS_SQ
= 200*200;
20
21
protected
SCR_AISectorThreatFilter
m_ThreatFilter
;
22
protected
IEntity
m_MyEntity
;
23
protected
int
m_iSectorMajor
;
24
protected
int
m_iSectorMinor
;
25
26
protected
int
m_iState
;
27
protected
int
m_iStateAfterIntermission
;
28
protected
float
m_fTimer
;
29
protected
float
m_fTimerThreshold
;
30
31
protected
WorldTimestamp
m_TimestampLastUpdate
;
32
33
[
Attribute
(
"1"
,
UIWidgets
.EditBox)]
34
protected
float
m_fLookDurationMajor
;
35
36
[
Attribute
(
"1"
,
UIWidgets
.EditBox)]
37
protected
float
m_fLookDurationMinor
;
38
39
[
Attribute
(
"0"
,
UIWidgets
.EditBox,
"Duration of waiting after looking"
)]
40
protected
float
m_fIntermissionDuration
;
41
42
[
Attribute
(
"1"
,
UIWidgets
.EditBox,
"Timers will be randomized between 1.0 and this value"
)]
43
protected
float
m_fTimerRandomizationCoefficient
;
44
45
[
Attribute
(
"0"
,
UIWidgets
.ComboBox,
"Evaluate if we should raise weapon, and use optics?"
)]
46
protected
bool
m_bEvaluateEquipmentUsage
;
47
48
//---------------------------------------------------------------------------
49
override
void
OnInit
(AIAgent owner)
50
{
51
SCR_AIUtilityComponent utility = SCR_AIUtilityComponent.Cast(owner.FindComponent(SCR_AIUtilityComponent));
52
m_ThreatFilter
= utility.m_SectorThreatFilter;
53
m_MyEntity
= utility.m_OwnerEntity;
54
55
Reset
();
56
}
57
58
//---------------------------------------------------------------------------
59
override
void
OnAbort
(AIAgent owner,
Node
nodeCausingAbort)
60
{
61
Reset
();
62
}
63
64
//---------------------------------------------------------------------------
65
protected
void
Reset
()
66
{
67
m_iSectorMajor
= -1;
68
m_iSectorMinor
= -1;
69
m_iState
=
STATE_IDLE
;
70
m_fTimer
= 0;
71
m_fTimerThreshold
= 0;
72
}
73
74
//---------------------------------------------------------------------------
75
override
ENodeResult
EOnTaskSimulate
(AIAgent owner,
float
dt)
76
{
77
if
(!
m_ThreatFilter
)
78
return
ENodeResult
.FAIL;
79
80
// Update timestamp, calculate timeSlice
81
WorldTimestamp
worldTimestamp =
GetGame
().GetWorld().GetTimestamp();
82
float
timeSlice_s = worldTimestamp.DiffSeconds(
m_TimestampLastUpdate
);
83
m_TimestampLastUpdate
= worldTimestamp;
84
85
bool
resetLook =
false
;
// We reset it whenever we alternate sectors
86
int
prevState =
m_iState
;
87
88
//-------------------------------------------------
89
/*
90
The logic of the state machine:
91
Alternate looking at major sector, and then at minor sector, with different duration.
92
If major sector from threat system changes, switch to looking at it.
93
*/
94
95
int
sectorMajor, sectorMinor;
96
m_ThreatFilter
.GetActiveSectors(sectorMajor, sectorMinor);
97
98
switch
(
m_iState
)
99
{
100
// Nothing to look at right now
101
case
STATE_IDLE
:
102
{
103
if
(sectorMajor != -1)
// There is now something to look at
104
SwitchState_ObserveMajorSector
();
105
break
;
106
}
107
108
case
STATE_OBSERVING_MAJOR
:
109
{
110
m_fTimer
+= timeSlice_s;
111
112
if
(sectorMajor == -1)
113
SwitchState_Idle
();
// Nothing to look at any more
114
else
if
(sectorMajor !=
m_iSectorMajor
)
115
{
116
// Major sector changed, so something important has happened
117
// Reset the looking in BT, restart the timer
118
resetLook =
true
;
119
SwitchState_ObserveMajorSector
();
120
}
121
else
if
(
m_fTimer
>
m_fTimerThreshold
)
122
{
123
// Looked at it too much
124
if
(
m_fIntermissionDuration
!= 0)
125
SwitchState_Intermission
(
STATE_OBSERVING_MINOR
);
126
else
if
(sectorMinor != -1)
127
SwitchState_ObserveMinorSector
();
128
}
129
130
break
;
131
}
132
133
case
STATE_OBSERVING_MINOR
:
134
{
135
m_fTimer
+= timeSlice_s;
136
137
if
(sectorMajor !=
m_iSectorMajor
&& sectorMajor != -1)
138
{
139
// Major sector changed, so something important has happened
140
// Start looking at major threat sector again
141
resetLook =
true
;
142
SwitchState_ObserveMajorSector
();
143
}
144
else
if
(sectorMinor == -1)
// Minor sector is no longer relevant
145
{
146
if
(sectorMajor != -1)
147
SwitchState_ObserveMajorSector
();
148
else
149
SwitchState_Idle
();
150
}
151
else
if
(
m_fTimer
>
m_fTimerThreshold
)
152
{
153
if
(
m_fIntermissionDuration
!= 0)
154
SwitchState_Intermission
(
STATE_OBSERVING_MAJOR
);
155
else
if
(sectorMajor != -1)
156
SwitchState_ObserveMajorSector
();
157
else
158
SwitchState_Idle
();
159
}
160
161
break
;
162
}
163
164
case
STATE_INTERMISSION
:
165
{
166
m_fTimer
+= timeSlice_s;
167
168
if
(sectorMajor !=
m_iSectorMajor
&& sectorMajor != -1)
169
{
170
// Major sector changed, so something important has happened
171
// Start looking at major threat sector again
172
resetLook =
true
;
173
SwitchState_ObserveMajorSector
();
174
}
175
else
if
(
m_fTimer
>
m_fTimerThreshold
)
176
{
177
// After pause is over, look at the next sector, if it's valid
178
if
(
m_iStateAfterIntermission
==
STATE_OBSERVING_MAJOR
)
179
{
180
if
(sectorMajor != -1)
181
SwitchState_ObserveMajorSector
();
182
else
183
SwitchState_Idle
();
184
}
185
else
186
{
187
if
(sectorMinor != -1)
188
SwitchState_ObserveMinorSector
();
189
else
if
(sectorMajor != -1)
190
SwitchState_ObserveMajorSector
();
191
else
192
SwitchState_Idle
();
193
}
194
}
195
196
break
;
197
}
198
}
199
200
m_iSectorMajor
= sectorMajor;
201
m_iSectorMinor
= sectorMinor;
202
203
204
//-------------------------------------------------
205
// Propagate data to outputs
206
207
// Reset
208
if
(
m_iState
!= prevState)
// Also reset if state changed
209
resetLook =
true
;
210
if
(resetLook)
211
SetVariableOut
(
PORT_RESET_LOOK
, resetLook);
212
213
// Active
214
bool
active =
m_iState
==
STATE_OBSERVING_MAJOR
||
m_iState
==
STATE_OBSERVING_MINOR
;
215
SetVariableOut
(
PORT_ACTIVE
, active);
216
217
// Position and Radius
218
if
(active)
219
{
220
int
sectorId;
221
if
(
m_iState
==
STATE_OBSERVING_MAJOR
)
222
sectorId =
m_iSectorMajor
;
223
else
224
sectorId =
m_iSectorMinor
;
225
226
vector
threatPos =
m_ThreatFilter
.GetSectorPos(sectorId);
227
SetVariableOut
(
PORT_CURRENT_POSITION
, threatPos);
228
}
229
230
// Optional: Evaluate if we should raise weapon, and use optics
231
if
(
m_bEvaluateEquipmentUsage
)
232
{
233
bool
raiseWeapon =
false
;
234
bool
useOptics =
false
;
235
if
(
m_iSectorMajor
!= -1)
236
{
237
float
dangerValue =
m_ThreatFilter
.GetSectorDanger(
m_iSectorMajor
);
238
raiseWeapon = dangerValue >
DANGER_THRESHOLD_RAISE_WEAPON
;
239
float
distanceSq =
vector
.DistanceSq(
m_ThreatFilter
.GetSectorPos(
m_iSectorMajor
),
m_MyEntity
.GetOrigin());
240
useOptics = distanceSq >
DISTANCE_USE_OPTICS_SQ
;
241
}
242
243
// Raise weapon
244
SetVariableOut
(
PORT_RAISE_WEAPON
, raiseWeapon);
245
SetVariableOut
(
PORT_USE_OPTICS
, useOptics);
246
}
247
248
return
ENodeResult
.SUCCESS;
249
}
250
251
protected
void
SwitchState_ObserveMajorSector
()
252
{
253
m_iState
=
STATE_OBSERVING_MAJOR
;
254
m_fTimer
= 0;
255
m_fTimerThreshold
=
m_fLookDurationMajor
;
256
m_fTimerThreshold
=
Math
.RandomFloat(
m_fTimerThreshold
,
m_fTimerRandomizationCoefficient
*
m_fTimerThreshold
);
257
}
258
259
protected
void
SwitchState_ObserveMinorSector
()
260
{
261
m_iState
=
STATE_OBSERVING_MINOR
;
262
m_fTimer
= 0;
263
m_fTimerThreshold
=
m_fLookDurationMinor
;
264
m_fTimerThreshold
=
Math
.RandomFloat(
m_fTimerThreshold
,
m_fTimerRandomizationCoefficient
*
m_fTimerThreshold
);
265
}
266
267
protected
void
SwitchState_Idle
()
268
{
269
m_iState
=
STATE_IDLE
;
270
m_fTimer
= 0;
271
}
272
273
protected
void
SwitchState_Intermission
(
int
stateAfterIntermission)
274
{
275
m_iState
=
STATE_INTERMISSION
;
276
m_iStateAfterIntermission
= stateAfterIntermission;
277
m_fTimer
= 0;
278
m_fTimerThreshold
=
m_fIntermissionDuration
;
279
m_fTimerThreshold
=
Math
.RandomFloat(
m_fTimerThreshold
,
m_fTimerRandomizationCoefficient
*
m_fTimerThreshold
);
280
}
281
282
//---------------------------------------------------------------------------
283
protected
static
ref
TStringArray
s_aVarsOut
= {
PORT_ACTIVE
,
PORT_RESET_LOOK
,
PORT_CURRENT_POSITION
,
PORT_RAISE_WEAPON
,
PORT_USE_OPTICS
};
284
override
TStringArray
GetVariablesOut
() {
return
s_aVarsOut
; };
285
286
override
static
bool
VisibleInPalette
() {
return
true
; }
287
}
GetGame
ArmaReforgerScripted GetGame()
Definition
game.c:1398
AITaskScripted
Definition
AITaskScripted.c:13
IEntity
Definition
IEntity.c:13
Math
Definition
Math.c:13
Node
Definition
Node.c:13
Node::SetVariableOut
proto void SetVariableOut(string name, void val)
SCR_AILookAtThreatSystemLogic
Definition
SCR_AILookAtThreatSystemLogic.c:5
SCR_AILookAtThreatSystemLogic::m_MyEntity
IEntity m_MyEntity
Definition
SCR_AILookAtThreatSystemLogic.c:22
SCR_AILookAtThreatSystemLogic::EOnTaskSimulate
override ENodeResult EOnTaskSimulate(AIAgent owner, float dt)
Definition
SCR_AILookAtThreatSystemLogic.c:75
SCR_AILookAtThreatSystemLogic::PORT_CURRENT_POSITION
static const string PORT_CURRENT_POSITION
Definition
SCR_AILookAtThreatSystemLogic.c:7
SCR_AILookAtThreatSystemLogic::m_iSectorMinor
int m_iSectorMinor
Definition
SCR_AILookAtThreatSystemLogic.c:24
SCR_AILookAtThreatSystemLogic::m_iSectorMajor
int m_iSectorMajor
Definition
SCR_AILookAtThreatSystemLogic.c:23
SCR_AILookAtThreatSystemLogic::PORT_ACTIVE
static const string PORT_ACTIVE
Definition
SCR_AILookAtThreatSystemLogic.c:9
SCR_AILookAtThreatSystemLogic::PORT_RESET_LOOK
static const string PORT_RESET_LOOK
Definition
SCR_AILookAtThreatSystemLogic.c:8
SCR_AILookAtThreatSystemLogic::GetVariablesOut
override TStringArray GetVariablesOut()
Definition
SCR_AILookAtThreatSystemLogic.c:284
SCR_AILookAtThreatSystemLogic::m_iStateAfterIntermission
int m_iStateAfterIntermission
Definition
SCR_AILookAtThreatSystemLogic.c:27
SCR_AILookAtThreatSystemLogic::m_fLookDurationMinor
float m_fLookDurationMinor
Definition
SCR_AILookAtThreatSystemLogic.c:37
SCR_AILookAtThreatSystemLogic::PORT_USE_OPTICS
static const string PORT_USE_OPTICS
Definition
SCR_AILookAtThreatSystemLogic.c:11
SCR_AILookAtThreatSystemLogic::SwitchState_ObserveMajorSector
void SwitchState_ObserveMajorSector()
Definition
SCR_AILookAtThreatSystemLogic.c:251
SCR_AILookAtThreatSystemLogic::OnInit
override void OnInit(AIAgent owner)
Definition
SCR_AILookAtThreatSystemLogic.c:49
SCR_AILookAtThreatSystemLogic::STATE_INTERMISSION
static const int STATE_INTERMISSION
Definition
SCR_AILookAtThreatSystemLogic.c:16
SCR_AILookAtThreatSystemLogic::VisibleInPalette
static override bool VisibleInPalette()
Definition
SCR_AILookAtThreatSystemLogic.c:286
SCR_AILookAtThreatSystemLogic::SwitchState_Intermission
void SwitchState_Intermission(int stateAfterIntermission)
Definition
SCR_AILookAtThreatSystemLogic.c:273
SCR_AILookAtThreatSystemLogic::m_ThreatFilter
SCR_AISectorThreatFilter m_ThreatFilter
Definition
SCR_AILookAtThreatSystemLogic.c:21
SCR_AILookAtThreatSystemLogic::STATE_OBSERVING_MAJOR
static const int STATE_OBSERVING_MAJOR
Definition
SCR_AILookAtThreatSystemLogic.c:14
SCR_AILookAtThreatSystemLogic::m_fTimerRandomizationCoefficient
float m_fTimerRandomizationCoefficient
Definition
SCR_AILookAtThreatSystemLogic.c:43
SCR_AILookAtThreatSystemLogic::m_bEvaluateEquipmentUsage
bool m_bEvaluateEquipmentUsage
Definition
SCR_AILookAtThreatSystemLogic.c:46
SCR_AILookAtThreatSystemLogic::Reset
void Reset()
Definition
SCR_AILookAtThreatSystemLogic.c:65
SCR_AILookAtThreatSystemLogic::m_TimestampLastUpdate
WorldTimestamp m_TimestampLastUpdate
Definition
SCR_AILookAtThreatSystemLogic.c:31
SCR_AILookAtThreatSystemLogic::m_fIntermissionDuration
float m_fIntermissionDuration
Definition
SCR_AILookAtThreatSystemLogic.c:40
SCR_AILookAtThreatSystemLogic::m_fTimer
float m_fTimer
Definition
SCR_AILookAtThreatSystemLogic.c:28
SCR_AILookAtThreatSystemLogic::DISTANCE_USE_OPTICS_SQ
static const float DISTANCE_USE_OPTICS_SQ
Definition
SCR_AILookAtThreatSystemLogic.c:19
SCR_AILookAtThreatSystemLogic::SwitchState_ObserveMinorSector
void SwitchState_ObserveMinorSector()
Definition
SCR_AILookAtThreatSystemLogic.c:259
SCR_AILookAtThreatSystemLogic::OnAbort
override void OnAbort(AIAgent owner, Node nodeCausingAbort)
Definition
SCR_AILookAtThreatSystemLogic.c:59
SCR_AILookAtThreatSystemLogic::m_fLookDurationMajor
float m_fLookDurationMajor
Definition
SCR_AILookAtThreatSystemLogic.c:34
SCR_AILookAtThreatSystemLogic::m_iState
int m_iState
Definition
SCR_AILookAtThreatSystemLogic.c:26
SCR_AILookAtThreatSystemLogic::PORT_RAISE_WEAPON
static const string PORT_RAISE_WEAPON
Definition
SCR_AILookAtThreatSystemLogic.c:10
SCR_AILookAtThreatSystemLogic::SwitchState_Idle
void SwitchState_Idle()
Definition
SCR_AILookAtThreatSystemLogic.c:267
SCR_AILookAtThreatSystemLogic::s_aVarsOut
static ref TStringArray s_aVarsOut
Definition
SCR_AILookAtThreatSystemLogic.c:283
SCR_AILookAtThreatSystemLogic::m_fTimerThreshold
float m_fTimerThreshold
Definition
SCR_AILookAtThreatSystemLogic.c:29
SCR_AILookAtThreatSystemLogic::DANGER_THRESHOLD_RAISE_WEAPON
static const float DANGER_THRESHOLD_RAISE_WEAPON
Definition
SCR_AILookAtThreatSystemLogic.c:18
SCR_AILookAtThreatSystemLogic::STATE_IDLE
static const int STATE_IDLE
Definition
SCR_AILookAtThreatSystemLogic.c:13
SCR_AILookAtThreatSystemLogic::STATE_OBSERVING_MINOR
static const int STATE_OBSERVING_MINOR
Definition
SCR_AILookAtThreatSystemLogic.c:15
SCR_AISectorThreatFilter
Definition
SCR_AISectorThreatFilter.c:95
UIWidgets
Definition
attributes.c:40
WorldTimestamp
Definition
WorldTimestamp.c:26
vector
Definition
vector.c:13
ENodeResult
ENodeResult
Definition
ENodeResult.c:13
Attribute
SCR_FieldOfViewSettings Attribute
Definition
SendGoalMessage.c:170
TStringArray
array< string > TStringArray
Definition
Types.c:385
scripts
Game
AI
Behavior
SCR_AILookAtThreatSystemLogic.c
Generated by
1.17.0