-
Notifications
You must be signed in to change notification settings - Fork 1
/
AIBrainSenses.cs
359 lines (314 loc) · 10.1 KB
/
AIBrainSenses.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using System.Collections.Generic;
using ConVar;
using Rust.AI;
using UnityEngine;
public class AIBrainSenses
{
[ServerVar]
public static float UpdateInterval = 0.5f;
[ServerVar]
public static float HumanKnownPlayersLOSUpdateInterval = 0.2f;
[ServerVar]
public static float KnownPlayersLOSUpdateInterval = 0.5f;
private float knownPlayersLOSUpdateInterval = 0.2f;
public float MemoryDuration = 10f;
public float LastThreatTimestamp;
public float TimeInAgressiveState;
private static BaseEntity[] queryResults = new BaseEntity[64];
private static BasePlayer[] playerQueryResults = new BasePlayer[64];
private float nextUpdateTime;
private float nextKnownPlayersLOSUpdateTime;
private BaseEntity owner;
private BasePlayer playerOwner;
private IAISenses ownerSenses;
private float maxRange;
private float targetLostRange;
private float visionCone;
private bool checkVision;
private bool checkLOS;
private bool ignoreNonVisionSneakers;
private float listenRange;
private bool hostileTargetsOnly;
private bool senseFriendlies;
private bool refreshKnownLOS;
private EntityType senseTypes;
private IAIAttack ownerAttack;
public float TimeSinceThreat => Time.get_realtimeSinceStartup() - LastThreatTimestamp;
public SimpleAIMemory Memory { get; private set; } = new SimpleAIMemory();
public float TargetLostRange => targetLostRange;
public bool ignoreSafeZonePlayers { get; private set; }
public List<BaseEntity> Players => Memory.Players;
public void Init(BaseEntity owner, float memoryDuration, float range, float targetLostRange, float visionCone, bool checkVision, bool checkLOS, bool ignoreNonVisionSneakers, float listenRange, bool hostileTargetsOnly, bool senseFriendlies, bool ignoreSafeZonePlayers, EntityType senseTypes, bool refreshKnownLOS)
{
this.owner = owner;
MemoryDuration = memoryDuration;
ownerAttack = owner as IAIAttack;
playerOwner = owner as BasePlayer;
maxRange = range;
this.targetLostRange = targetLostRange;
this.visionCone = visionCone;
this.checkVision = checkVision;
this.checkLOS = checkLOS;
this.ignoreNonVisionSneakers = ignoreNonVisionSneakers;
this.listenRange = listenRange;
this.hostileTargetsOnly = hostileTargetsOnly;
this.senseFriendlies = senseFriendlies;
this.ignoreSafeZonePlayers = ignoreSafeZonePlayers;
this.senseTypes = senseTypes;
LastThreatTimestamp = Time.get_realtimeSinceStartup();
this.refreshKnownLOS = refreshKnownLOS;
ownerSenses = owner as IAISenses;
knownPlayersLOSUpdateInterval = ((owner is HumanNPC) ? HumanKnownPlayersLOSUpdateInterval : KnownPlayersLOSUpdateInterval);
}
public void Update()
{
if (!((Object)(object)owner == (Object)null))
{
UpdateSenses();
UpdateKnownPlayersLOS();
}
}
private void UpdateSenses()
{
if (Time.get_time() < nextUpdateTime)
{
return;
}
nextUpdateTime = Time.get_time() + UpdateInterval;
if (senseTypes != 0)
{
if (senseTypes == EntityType.Player)
{
SensePlayers();
}
else
{
SenseBrains();
if (senseTypes.HasFlag(EntityType.Player))
{
SensePlayers();
}
}
}
Memory.Forget(MemoryDuration);
}
public void UpdateKnownPlayersLOS()
{
//IL_0088: Unknown result type (might be due to invalid IL or missing references)
//IL_0098: Unknown result type (might be due to invalid IL or missing references)
if (Time.get_time() < nextKnownPlayersLOSUpdateTime)
{
return;
}
nextKnownPlayersLOSUpdateTime = Time.get_time() + knownPlayersLOSUpdateInterval;
foreach (BaseEntity player in Memory.Players)
{
if (!((Object)(object)player == (Object)null) && !player.IsNpc)
{
bool flag = ownerAttack.CanSeeTarget(player);
Memory.SetLOS(player, flag);
if (refreshKnownLOS && (Object)(object)owner != (Object)null && flag && Vector3.Distance(((Component)player).get_transform().get_position(), ((Component)owner).get_transform().get_position()) <= TargetLostRange)
{
Memory.SetKnown(player, owner, this);
}
}
}
}
private void SensePlayers()
{
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
int playersInSphere = BaseEntity.Query.Server.GetPlayersInSphere(((Component)owner).get_transform().get_position(), maxRange, playerQueryResults, AiCaresAbout);
for (int i = 0; i < playersInSphere; i++)
{
BasePlayer ent = playerQueryResults[i];
Memory.SetKnown(ent, owner, this);
}
}
private void SenseBrains()
{
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
int brainsInSphere = BaseEntity.Query.Server.GetBrainsInSphere(((Component)owner).get_transform().get_position(), maxRange, queryResults, AiCaresAbout);
for (int i = 0; i < brainsInSphere; i++)
{
BaseEntity ent = queryResults[i];
Memory.SetKnown(ent, owner, this);
}
}
private bool AiCaresAbout(BaseEntity entity)
{
//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
//IL_010a: Unknown result type (might be due to invalid IL or missing references)
//IL_015c: Unknown result type (might be due to invalid IL or missing references)
//IL_0167: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)entity == (Object)null)
{
return false;
}
if (!entity.isServer)
{
return false;
}
if (entity.EqualNetID(owner))
{
return false;
}
if (entity.Health() <= 0f)
{
return false;
}
if (!IsValidSenseType(entity))
{
return false;
}
BaseCombatEntity baseCombatEntity = entity as BaseCombatEntity;
BasePlayer basePlayer = entity as BasePlayer;
if ((Object)(object)basePlayer != (Object)null && basePlayer.IsDead())
{
return false;
}
if (ignoreSafeZonePlayers && (Object)(object)basePlayer != (Object)null && basePlayer.InSafeZone())
{
return false;
}
if (listenRange > 0f && (Object)(object)baseCombatEntity != (Object)null && baseCombatEntity.TimeSinceLastNoise <= 1f && baseCombatEntity.CanLastNoiseBeHeard(((Component)owner).get_transform().get_position(), listenRange))
{
return true;
}
if (senseFriendlies && ownerSenses != null && ownerSenses.IsFriendly(entity))
{
return true;
}
float num = float.PositiveInfinity;
if ((Object)(object)baseCombatEntity != (Object)null && AI.accuratevisiondistance)
{
num = Vector3.Distance(((Component)owner).get_transform().get_position(), ((Component)baseCombatEntity).get_transform().get_position());
if (num > maxRange)
{
return false;
}
}
if (checkVision && !IsTargetInVision(entity))
{
if (!ignoreNonVisionSneakers)
{
return false;
}
if ((Object)(object)basePlayer != (Object)null && !basePlayer.IsNpc)
{
if (!AI.accuratevisiondistance)
{
num = Vector3.Distance(((Component)owner).get_transform().get_position(), ((Component)basePlayer).get_transform().get_position());
}
if ((basePlayer.IsDucked() && num >= 4f) || num >= 15f)
{
return false;
}
}
}
if (hostileTargetsOnly && (Object)(object)baseCombatEntity != (Object)null && !baseCombatEntity.IsHostile())
{
return false;
}
if (checkLOS && ownerAttack != null)
{
bool flag = ownerAttack.CanSeeTarget(entity);
Memory.SetLOS(entity, flag);
if (!flag)
{
return false;
}
}
return true;
}
private bool IsValidSenseType(BaseEntity ent)
{
BasePlayer basePlayer = ent as BasePlayer;
if ((Object)(object)basePlayer != (Object)null)
{
if (basePlayer.IsNpc)
{
if (ent is BasePet)
{
return true;
}
if (senseTypes.HasFlag(EntityType.BasePlayerNPC))
{
return true;
}
}
else if (senseTypes.HasFlag(EntityType.Player))
{
return true;
}
}
if (senseTypes.HasFlag(EntityType.NPC) && ent is BaseNpc)
{
return true;
}
if (senseTypes.HasFlag(EntityType.WorldItem) && ent is WorldItem)
{
return true;
}
if (senseTypes.HasFlag(EntityType.Corpse) && ent is BaseCorpse)
{
return true;
}
if (senseTypes.HasFlag(EntityType.TimedExplosive) && ent is TimedExplosive)
{
return true;
}
if (senseTypes.HasFlag(EntityType.Chair) && ent is BaseChair)
{
return true;
}
return false;
}
private bool IsTargetInVision(BaseEntity target)
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
//IL_001b: Unknown result type (might be due to invalid IL or missing references)
//IL_0020: Unknown result type (might be due to invalid IL or missing references)
//IL_003a: Unknown result type (might be due to invalid IL or missing references)
//IL_004c: Unknown result type (might be due to invalid IL or missing references)
//IL_0051: Unknown result type (might be due to invalid IL or missing references)
Vector3 val = Vector3Ex.Direction(((Component)target).get_transform().get_position(), ((Component)owner).get_transform().get_position());
return Vector3.Dot(((Object)(object)playerOwner != (Object)null) ? playerOwner.eyes.BodyForward() : ((Component)owner).get_transform().get_forward(), val) >= visionCone;
}
public BaseEntity GetNearestPlayer(float rangeFraction)
{
return GetNearest(Memory.Players, rangeFraction);
}
public BaseEntity GetNearestThreat(float rangeFraction)
{
return GetNearest(Memory.Threats, rangeFraction);
}
public BaseEntity GetNearestTarget(float rangeFraction)
{
return GetNearest(Memory.Targets, rangeFraction);
}
private BaseEntity GetNearest(List<BaseEntity> entities, float rangeFraction)
{
//IL_0042: Unknown result type (might be due to invalid IL or missing references)
//IL_0052: Unknown result type (might be due to invalid IL or missing references)
if (entities == null || entities.Count == 0)
{
return null;
}
float num = float.PositiveInfinity;
BaseEntity result = null;
foreach (BaseEntity entity in entities)
{
if (!((Object)(object)entity == (Object)null) && !(entity.Health() <= 0f))
{
float num2 = Vector3.Distance(((Component)entity).get_transform().get_position(), ((Component)owner).get_transform().get_position());
if (num2 <= rangeFraction * maxRange && num2 < num)
{
result = entity;
}
}
}
return result;
}
}