-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAnimalBrain.cs
434 lines (393 loc) · 13.3 KB
/
AnimalBrain.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
using UnityEngine;
public class AnimalBrain : BaseAIBrain<BaseAnimalNPC>
{
public class AttackState : BasicAIState
{
private IAIAttack attack;
public AttackState()
: base(AIState.Attack)
{
base.AgrresiveState = true;
}
public override void StateEnter()
{
//IL_0080: Unknown result type (might be due to invalid IL or missing references)
//IL_0085: Unknown result type (might be due to invalid IL or missing references)
//IL_0091: Unknown result type (might be due to invalid IL or missing references)
//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
base.StateEnter();
attack = GetEntity();
BaseEntity baseEntity = brain.Events.Memory.Entity.Get(brain.Events.CurrentInputMemorySlot);
BasePlayer basePlayer = baseEntity as BasePlayer;
if ((Object)(object)basePlayer != (Object)null && basePlayer.IsDead())
{
StopAttacking();
}
else if ((Object)(object)baseEntity != (Object)null && baseEntity.Health() > 0f)
{
BaseCombatEntity target = baseEntity as BaseCombatEntity;
Vector3 aimDirection = GetAimDirection(GetEntity(), target);
brain.Navigator.SetFacingDirectionOverride(aimDirection);
if (attack.CanAttack(baseEntity))
{
StartAttacking(baseEntity);
}
brain.Navigator.SetDestination(((Component)baseEntity).get_transform().get_position(), BaseNavigator.NavigationSpeed.Fast);
}
}
public override void StateLeave()
{
base.StateLeave();
brain.Navigator.ClearFacingDirectionOverride();
brain.Navigator.Stop();
StopAttacking();
}
private void StopAttacking()
{
attack.StopAttacking();
}
public override StateStatus StateThink(float delta)
{
//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
//IL_0139: Unknown result type (might be due to invalid IL or missing references)
//IL_013e: Unknown result type (might be due to invalid IL or missing references)
//IL_014b: Unknown result type (might be due to invalid IL or missing references)
base.StateThink(delta);
BaseEntity baseEntity = brain.Events.Memory.Entity.Get(brain.Events.CurrentInputMemorySlot);
if (attack == null)
{
return StateStatus.Error;
}
if ((Object)(object)baseEntity == (Object)null)
{
brain.Navigator.ClearFacingDirectionOverride();
StopAttacking();
return StateStatus.Finished;
}
if (baseEntity.Health() <= 0f)
{
StopAttacking();
return StateStatus.Finished;
}
BasePlayer basePlayer = baseEntity as BasePlayer;
if ((Object)(object)basePlayer != (Object)null && basePlayer.IsDead())
{
StopAttacking();
return StateStatus.Finished;
}
BaseVehicle baseVehicle = (((Object)(object)basePlayer != (Object)null) ? basePlayer.GetMountedVehicle() : null);
if ((Object)(object)baseVehicle != (Object)null && baseVehicle is BaseModularVehicle)
{
StopAttacking();
return StateStatus.Error;
}
if (brain.Senses.ignoreSafeZonePlayers && (Object)(object)basePlayer != (Object)null && basePlayer.InSafeZone())
{
return StateStatus.Error;
}
if (!brain.Navigator.SetDestination(((Component)baseEntity).get_transform().get_position(), BaseNavigator.NavigationSpeed.Fast, 0.25f, (baseEntity is BasePlayer && attack != null) ? attack.EngagementRange() : 0f))
{
return StateStatus.Error;
}
BaseCombatEntity target = baseEntity as BaseCombatEntity;
Vector3 aimDirection = GetAimDirection(GetEntity(), target);
brain.Navigator.SetFacingDirectionOverride(aimDirection);
if (attack.CanAttack(baseEntity))
{
StartAttacking(baseEntity);
}
else
{
StopAttacking();
}
return StateStatus.Running;
}
private static Vector3 GetAimDirection(BaseCombatEntity from, BaseCombatEntity target)
{
//IL_001b: Unknown result type (might be due to invalid IL or missing references)
//IL_0027: Unknown result type (might be due to invalid IL or missing references)
//IL_0033: Unknown result type (might be due to invalid IL or missing references)
//IL_003e: Unknown result type (might be due to invalid IL or missing references)
//IL_0043: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)from == (Object)null || (Object)(object)target == (Object)null)
{
if (!((Object)(object)from != (Object)null))
{
return Vector3.get_forward();
}
return ((Component)from).get_transform().get_forward();
}
return Vector3Ex.Direction2D(((Component)target).get_transform().get_position(), ((Component)from).get_transform().get_position());
}
private void StartAttacking(BaseEntity entity)
{
attack.StartAttacking(entity);
}
}
public class ChaseState : BasicAIState
{
private IAIAttack attack;
public ChaseState()
: base(AIState.Chase)
{
base.AgrresiveState = true;
}
public override void StateEnter()
{
//IL_0057: Unknown result type (might be due to invalid IL or missing references)
base.StateEnter();
attack = GetEntity();
BaseEntity baseEntity = brain.Events.Memory.Entity.Get(brain.Events.CurrentInputMemorySlot);
if ((Object)(object)baseEntity != (Object)null)
{
brain.Navigator.SetDestination(((Component)baseEntity).get_transform().get_position(), BaseNavigator.NavigationSpeed.Fast);
}
}
public override void StateLeave()
{
base.StateLeave();
Stop();
}
private void Stop()
{
brain.Navigator.Stop();
}
public override StateStatus StateThink(float delta)
{
//IL_0055: Unknown result type (might be due to invalid IL or missing references)
base.StateThink(delta);
BaseEntity baseEntity = brain.Events.Memory.Entity.Get(brain.Events.CurrentInputMemorySlot);
if ((Object)(object)baseEntity == (Object)null)
{
Stop();
return StateStatus.Error;
}
if (!brain.Navigator.SetDestination(((Component)baseEntity).get_transform().get_position(), BaseNavigator.NavigationSpeed.Fast, 0.25f, (baseEntity is BasePlayer && attack != null) ? attack.EngagementRange() : 0f))
{
return StateStatus.Error;
}
if (!brain.Navigator.Moving)
{
return StateStatus.Finished;
}
return StateStatus.Running;
}
}
public class FleeState : BasicAIState
{
private float nextInterval = 2f;
private float stopFleeDistance;
public FleeState()
: base(AIState.Flee)
{
}
public override void StateEnter()
{
//IL_005a: Unknown result type (might be due to invalid IL or missing references)
//IL_0065: Unknown result type (might be due to invalid IL or missing references)
base.StateEnter();
BaseEntity baseEntity = brain.Events.Memory.Entity.Get(brain.Events.CurrentInputMemorySlot);
if ((Object)(object)baseEntity != (Object)null)
{
stopFleeDistance = Random.Range(80f, 100f) + Mathf.Clamp(Vector3Ex.Distance2D(((Component)brain.Navigator).get_transform().get_position(), ((Component)baseEntity).get_transform().get_position()), 0f, 50f);
}
FleeFrom(brain.Events.Memory.Entity.Get(brain.Events.CurrentInputMemorySlot), GetEntity());
}
public override void StateLeave()
{
base.StateLeave();
Stop();
}
private void Stop()
{
brain.Navigator.Stop();
}
public override StateStatus StateThink(float delta)
{
//IL_004e: Unknown result type (might be due to invalid IL or missing references)
//IL_0059: Unknown result type (might be due to invalid IL or missing references)
base.StateThink(delta);
BaseEntity baseEntity = brain.Events.Memory.Entity.Get(brain.Events.CurrentInputMemorySlot);
if ((Object)(object)baseEntity == (Object)null)
{
return StateStatus.Finished;
}
if (Vector3Ex.Distance2D(((Component)brain.Navigator).get_transform().get_position(), ((Component)baseEntity).get_transform().get_position()) >= stopFleeDistance)
{
return StateStatus.Finished;
}
if ((brain.Navigator.UpdateIntervalElapsed(nextInterval) || !brain.Navigator.Moving) && !FleeFrom(baseEntity, GetEntity()))
{
return StateStatus.Error;
}
return StateStatus.Running;
}
private bool FleeFrom(BaseEntity fleeFromEntity, BaseEntity thisEntity)
{
//IL_0061: Unknown result type (might be due to invalid IL or missing references)
//IL_0086: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)thisEntity == (Object)null || (Object)(object)fleeFromEntity == (Object)null)
{
return false;
}
nextInterval = Random.Range(3f, 6f);
if (!brain.PathFinder.GetBestFleePosition(brain.Navigator, brain.Senses, fleeFromEntity, brain.Events.Memory.Position.Get(4), 50f, 100f, out var result))
{
return false;
}
bool num = brain.Navigator.SetDestination(result, BaseNavigator.NavigationSpeed.Fast);
if (!num)
{
Stop();
}
return num;
}
}
public class IdleState : BaseIdleState
{
private float nextTurnTime;
private float minTurnTime = 10f;
private float maxTurnTime = 20f;
private int turnChance = 33;
public override void StateEnter()
{
base.StateEnter();
FaceNewDirection();
}
public override void StateLeave()
{
base.StateLeave();
brain.Navigator.ClearFacingDirectionOverride();
}
private void FaceNewDirection()
{
//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_0021: Unknown result type (might be due to invalid IL or missing references)
//IL_0036: Unknown result type (might be due to invalid IL or missing references)
//IL_003b: Unknown result type (might be due to invalid IL or missing references)
//IL_003c: Unknown result type (might be due to invalid IL or missing references)
//IL_0041: Unknown result type (might be due to invalid IL or missing references)
//IL_0044: Unknown result type (might be due to invalid IL or missing references)
//IL_0049: Unknown result type (might be due to invalid IL or missing references)
//IL_0055: Unknown result type (might be due to invalid IL or missing references)
if (Random.Range(0, 100) <= turnChance)
{
Vector3 position = ((Component)GetEntity()).get_transform().get_position();
Vector3 val = BasePathFinder.GetPointOnCircle(position, 1f, Random.Range(0f, 594f)) - position;
Vector3 normalized = ((Vector3)(ref val)).get_normalized();
brain.Navigator.SetFacingDirectionOverride(normalized);
}
nextTurnTime = Time.get_realtimeSinceStartup() + Random.Range(minTurnTime, maxTurnTime);
}
public override StateStatus StateThink(float delta)
{
base.StateThink(delta);
if (Time.get_realtimeSinceStartup() >= nextTurnTime)
{
FaceNewDirection();
}
return StateStatus.Running;
}
}
public class MoveTowardsState : BaseMoveTorwardsState
{
}
public class RoamState : BasicAIState
{
private StateStatus status = StateStatus.Error;
public RoamState()
: base(AIState.Roam)
{
}
public override void StateLeave()
{
base.StateLeave();
Stop();
}
public override void StateEnter()
{
//IL_0051: Unknown result type (might be due to invalid IL or missing references)
//IL_0056: Unknown result type (might be due to invalid IL or missing references)
//IL_0057: Unknown result type (might be due to invalid IL or missing references)
//IL_0076: Unknown result type (might be due to invalid IL or missing references)
//IL_007b: Unknown result type (might be due to invalid IL or missing references)
//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
//IL_00be: Unknown result type (might be due to invalid IL or missing references)
//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
base.StateEnter();
status = StateStatus.Error;
if (brain.PathFinder == null)
{
return;
}
Vector3 center;
if (brain.InGroup() && !brain.IsGroupLeader)
{
center = brain.Events.Memory.Position.Get(5);
center = BasePathFinder.GetPointOnCircle(center, Random.Range(2f, 7f), Random.Range(0f, 359f));
}
else
{
center = brain.PathFinder.GetBestRoamPosition(brain.Navigator, brain.Events.Memory.Position.Get(4), 20f, 100f);
}
if (brain.Navigator.SetDestination(center, BaseNavigator.NavigationSpeed.Slow))
{
if (brain.InGroup() && brain.IsGroupLeader)
{
brain.SetGroupRoamRootPosition(center);
}
status = StateStatus.Running;
}
else
{
status = StateStatus.Error;
}
}
private void Stop()
{
brain.Navigator.Stop();
}
public override StateStatus StateThink(float delta)
{
base.StateThink(delta);
if (status == StateStatus.Error)
{
return status;
}
if (brain.Navigator.Moving)
{
return StateStatus.Running;
}
return StateStatus.Finished;
}
}
public static int Count;
public override void AddStates()
{
base.AddStates();
AddState(new IdleState());
AddState(new MoveTowardsState());
AddState(new FleeState());
AddState(new RoamState());
AddState(new AttackState());
AddState(new BaseSleepState());
AddState(new ChaseState());
AddState(new BaseCooldownState());
}
public override void InitializeAI()
{
base.InitializeAI();
base.ThinkMode = AIThinkMode.Interval;
thinkRate = 0.25f;
base.PathFinder = new BasePathFinder();
Count++;
}
public override void OnDestroy()
{
base.OnDestroy();
Count--;
}
}