-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathConsciousnessSystem.cs
More file actions
217 lines (173 loc) · 7.45 KB
/
Copy pathConsciousnessSystem.cs
File metadata and controls
217 lines (173 loc) · 7.45 KB
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
using System.Linq;
using Content.Shared.Backmen.Surgery.Consciousness.Components;
using Content.Shared.Backmen.Surgery.Pain.Systems;
using Content.Shared.Backmen.Surgery.Wounds.Systems;
using Content.Shared.Backmen.Body.Systems;
using Content.Shared.FixedPoint;
using Content.Shared.Mobs;
using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems;
using Content.Shared.Zombies;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Shared.Backmen.Surgery.Consciousness.Systems;
public abstract partial class ConsciousnessSystem : EntitySystem
{
[Dependency] protected IGameTiming Timing = default!;
[Dependency] protected IRobustRandom Random = default!;
[Dependency] protected IPrototypeManager Proto = default!;
[Dependency] protected BkmBodySharedSystem Body = default!;
[Dependency] protected PainSystem Pain = default!;
[Dependency] protected WoundSystem Wound = default!;
[Dependency] protected MobStateSystem MobStateSys = default!;
[Dependency] protected MobThresholdSystem MobThresholds = default!;
[Dependency] protected EntityQuery<ConsciousnessComponent> ConsciousnessQuery = default!;
[Dependency] protected EntityQuery<MobStateComponent> MobStateQuery = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ConsciousnessComponent, MobStateChangedEvent>(OnRelayState);
SubscribeLocalEvent<ConsciousnessComponent, AfterAutoHandleStateEvent>(OnAfterAutoHandleState);
}
private void OnAfterAutoHandleState(Entity<ConsciousnessComponent> ent, ref AfterAutoHandleStateEvent args)
{
SanitizeConsciousnessDictionaries(ent.Comp);
}
private void SanitizeConsciousnessDictionaries(ConsciousnessComponent component)
{
if (component.NerveSystem != null && TerminatingOrDeleted(component.NerveSystem.Value.Owner))
component.NerveSystem = null;
foreach (var key in component.Modifiers.Keys.ToArray())
{
if (TerminatingOrDeleted(key.Item1))
component.Modifiers.Remove(key);
}
foreach (var key in component.Multipliers.Keys.ToArray())
{
if (TerminatingOrDeleted(key.Item1))
component.Multipliers.Remove(key);
}
foreach (var (id, (entity, _, _)) in component.RequiredConsciousnessParts.ToArray())
{
if (entity != null && TerminatingOrDeleted(entity.Value))
component.RequiredConsciousnessParts.Remove(id);
}
}
protected virtual void OnMobStateChanged(Entity<ConsciousnessComponent> consciousness,
ref MobStateChangedEvent args)
{
// do nothing
}
private void OnRelayState(Entity<ConsciousnessComponent> ent, ref MobStateChangedEvent args)
{
if (TryGetNerveSystem(ent.AsNullable(), out var nerveSys))
{
RaiseLocalEvent(nerveSys.Value, args);
}
OnMobStateChanged(ent, ref args);
}
protected void UpdateConsciousnessModifiers(Entity<ConsciousnessComponent?> uid)
{
if (!ConsciousnessQuery.Resolve(uid, ref uid.Comp))
return;
var totalDamage
= uid.Comp.Modifiers.Aggregate(FixedPoint2.Zero,
(current, modifier) => current + modifier.Value.Change * uid.Comp.Multiplier);
var newConsciousness = uid.Comp.Cap + totalDamage;
if (newConsciousness != uid.Comp.RawConsciousness)
{
var ev = new ConsciousnessChangedEvent(uid.Comp, newConsciousness, uid.Comp.RawConsciousness);
RaiseLocalEvent(uid, ref ev);
}
uid.Comp.RawConsciousness = newConsciousness;
CheckConscious(uid);
DirtyField(uid, uid.Comp, nameof(ConsciousnessComponent.RawConsciousness));
}
protected void UpdateConsciousnessMultipliers(Entity<ConsciousnessComponent?> uid)
{
if (!ConsciousnessQuery.Resolve(uid, ref uid.Comp))
return;
uid.Comp.Multiplier = uid.Comp.Multipliers.Count == 0
? FixedPoint2.New(1)
: uid.Comp.Multipliers.Aggregate(FixedPoint2.Zero,
(current, multiplier) => current + multiplier.Value.Change) / uid.Comp.Multipliers.Count;
CheckConscious(uid);
DirtyField(uid, uid.Comp, nameof(ConsciousnessComponent.Multiplier));
}
/// <summary>
/// Only used internally. Do not use this, instead use consciousness modifiers/multipliers!
/// </summary>
/// <param name="target">target entity</param>
/// <param name="isConscious">should this entity be conscious</param>
/// <param name="consciousness">consciousness component</param>
protected void SetConscious(
Entity<ConsciousnessComponent?> target,
bool isConscious)
{
if (!Resolve(target, ref target.Comp))
return;
target.Comp.IsConscious = isConscious;
DirtyField(target, target.Comp, nameof(ConsciousnessComponent.IsConscious));
}
protected void UpdateMobState(
Entity<ConsciousnessComponent?, MobStateComponent?> target)
{
if (TerminatingOrDeleted(target)
|| !ConsciousnessQuery.Resolve(target, ref target.Comp1)
|| !MobStateQuery.Resolve(target, ref target.Comp2, false))
return;
// Zombies are revived corpses; consciousness must not re-apply death/unconscious states.
if (HasComp<ZombieComponent>(target.Owner))
return;
var inPainCrit = !Pain.IsPainImmune(target)
&& TryGetNerveSystem(target, out var nerveSys)
&& (nerveSys.Value.Comp.Pain >= nerveSys.Value.Comp.SoftPainCap
|| nerveSys.Value.Comp.ForcePainCrit);
var newMobState = MobState.Alive;
if (inPainCrit)
newMobState = MobState.SoftCritical;
if (!target.Comp1.ForceConscious)
{
if (target.Comp1.ForceUnconscious)
newMobState = MobState.Critical;
if (!target.Comp1.IsConscious && !inPainCrit)
newMobState = MobState.Critical;
if (target.Comp1.PassedOut)
newMobState = MobState.Critical;
}
// Lethal outcomes override pain crit and forced consciousness (e.g. pain shock adrenaline).
if (target.Comp1.ForceDead || target.Comp1.Consciousness <= 0)
newMobState = MobState.Dead;
MobStateSys.ChangeMobState(target, newMobState, target);
MobThresholds.VerifyThresholds(target, mobState: target.Comp2);
}
protected void CheckRequiredParts(
Entity<ConsciousnessComponent> bodyId)
{
var alive = true;
var conscious = true;
foreach (var (/*identifier */_, (entity, forcesDeath, isLost)) in bodyId.Comp.RequiredConsciousnessParts)
{
if (entity == null || !isLost)
continue;
if (forcesDeath)
{
bodyId.Comp.ForceDead = true;
DirtyField(bodyId, bodyId.Comp, nameof(ConsciousnessComponent.ForceDead));
alive = false;
break;
}
conscious = false;
}
if (alive)
{
bodyId.Comp.ForceDead = false;
bodyId.Comp.ForceUnconscious = !conscious;
DirtyFields(bodyId, bodyId.Comp, null,
nameof(ConsciousnessComponent.ForceDead),
nameof(ConsciousnessComponent.ForceUnconscious));
}
CheckConscious(bodyId.AsNullable());
}
}