forked from Simple-Station/Einstein-Engines
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCloningConsoleSystem.cs
More file actions
266 lines (233 loc) · 11.4 KB
/
CloningConsoleSystem.cs
File metadata and controls
266 lines (233 loc) · 11.4 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
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
using System.Linq;
using Content.Server.Administration.Logs;
using Content.Server.Cloning.Components;
using Content.Server.DeviceLinking.Systems;
using Content.Server.Medical.Components;
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Shared.UserInterface;
using Content.Shared.Cloning;
using Content.Shared.Cloning.CloningConsole;
using Content.Shared.Database;
using Content.Shared.DeviceLinking;
using Content.Shared.DeviceLinking.Events;
using Content.Shared.IdentityManagement;
using Content.Shared.Mind;
using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Server.Player;
namespace Content.Server.Cloning
{
[UsedImplicitly]
public sealed class CloningConsoleSystem : EntitySystem
{
[Dependency] private readonly DeviceLinkSystem _signalSystem = default!;
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly CloningSystem _cloningSystem = default!;
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
[Dependency] private readonly MobStateSystem _mobStateSystem = default!;
[Dependency] private readonly PowerReceiverSystem _powerReceiverSystem = default!;
[Dependency] private readonly SharedMindSystem _mindSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<CloningConsoleComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<CloningConsoleComponent, UiButtonPressedMessage>(OnButtonPressed);
SubscribeLocalEvent<CloningConsoleComponent, AfterActivatableUIOpenEvent>(OnUIOpen);
SubscribeLocalEvent<CloningConsoleComponent, PowerChangedEvent>(OnPowerChanged);
SubscribeLocalEvent<CloningConsoleComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<CloningConsoleComponent, NewLinkEvent>(OnNewLink);
SubscribeLocalEvent<CloningConsoleComponent, PortDisconnectedEvent>(OnPortDisconnected);
SubscribeLocalEvent<CloningConsoleComponent, AnchorStateChangedEvent>(OnAnchorChanged);
}
private void OnInit(EntityUid uid, CloningConsoleComponent component, ComponentInit args)
{
_signalSystem.EnsureSourcePorts(uid, CloningConsoleComponent.ScannerPort, CloningConsoleComponent.PodPort);
}
private void OnButtonPressed(EntityUid uid, CloningConsoleComponent consoleComponent, UiButtonPressedMessage args)
{
if (!_powerReceiverSystem.IsPowered(uid)
|| consoleComponent.GeneticScanner is null
|| consoleComponent.CloningPod is null
|| !TryComp<CloningPodComponent>(consoleComponent.CloningPod.Value, out var cloningPod))
return;
switch (args.Button)
{
case UiButton.Clone:
TryClone(uid, consoleComponent.CloningPod.Value, consoleComponent.GeneticScanner.Value, cloningPod, consoleComponent: consoleComponent);
break;
}
UpdateUserInterface(uid, consoleComponent);
}
private void OnPowerChanged(EntityUid uid, CloningConsoleComponent component, ref PowerChangedEvent args)
{
UpdateUserInterface(uid, component);
}
private void OnMapInit(EntityUid uid, CloningConsoleComponent component, MapInitEvent args)
{
if (!TryComp<DeviceLinkSourceComponent>(uid, out var receiver))
return;
foreach (var port in receiver.Outputs.Values.SelectMany(ports => ports))
{
if (TryComp<MedicalScannerComponent>(port, out var scanner))
{
component.GeneticScanner = port;
scanner.ConnectedConsole = uid;
}
if (TryComp<CloningPodComponent>(port, out var pod))
{
component.CloningPod = port;
pod.ConnectedConsole = uid;
}
}
}
private void OnNewLink(EntityUid uid, CloningConsoleComponent component, NewLinkEvent args)
{
if (TryComp<MedicalScannerComponent>(args.Sink, out var scanner)
&& args.SourcePort == CloningConsoleComponent.ScannerPort)
{
component.GeneticScanner = args.Sink;
scanner.ConnectedConsole = uid;
}
if (TryComp<CloningPodComponent>(args.Sink, out var pod)
&& args.SourcePort == CloningConsoleComponent.PodPort)
{
component.CloningPod = args.Sink;
pod.ConnectedConsole = uid;
}
RecheckConnections(uid, component.CloningPod, component.GeneticScanner, component);
}
private void OnPortDisconnected(EntityUid uid, CloningConsoleComponent component, PortDisconnectedEvent args)
{
if (args.Port == CloningConsoleComponent.ScannerPort)
component.GeneticScanner = null;
if (args.Port == CloningConsoleComponent.PodPort)
component.CloningPod = null;
UpdateUserInterface(uid, component);
}
private void OnUIOpen(EntityUid uid, CloningConsoleComponent component, AfterActivatableUIOpenEvent args)
{
UpdateUserInterface(uid, component);
}
private void OnAnchorChanged(EntityUid uid, CloningConsoleComponent component, ref AnchorStateChangedEvent args)
{
if (!args.Anchored
|| !RecheckConnections(uid, component.CloningPod, component.GeneticScanner, component))
return;
UpdateUserInterface(uid, component);
}
public void UpdateUserInterface(EntityUid consoleUid, CloningConsoleComponent consoleComponent)
{
if (!_uiSystem.TryGetUi(consoleUid, CloningConsoleUiKey.Key, out var ui))
return;
if (!_powerReceiverSystem.IsPowered(consoleUid))
{
_uiSystem.CloseAll(ui);
return;
}
var newState = GetUserInterfaceState(consoleComponent);
_uiSystem.SetUiState(ui, newState);
}
public void TryClone(EntityUid uid, EntityUid cloningPodUid, EntityUid scannerUid, CloningPodComponent cloningPod, MedicalScannerComponent? scannerComp = null, CloningConsoleComponent? consoleComponent = null)
{
if (!Resolve(uid, ref consoleComponent)
|| !Resolve(scannerUid, ref scannerComp)
|| !Transform(cloningPodUid).Anchored
|| !Transform(scannerUid).Anchored
|| !consoleComponent.CloningPodInRange
|| !consoleComponent.GeneticScannerInRange)
return;
var body = scannerComp.BodyContainer.ContainedEntity;
if (body is null
|| !_mindSystem.TryGetMind(body.Value, out var mindId, out var mind)
|| mind.UserId.HasValue == false
|| mind.Session == null)
return;
if (_cloningSystem.TryCloning(cloningPodUid, body.Value, (mindId, mind), cloningPod, scannerComp.CloningFailChanceMultiplier))
{
_adminLogger.Add(LogType.Action, LogImpact.Medium, $"{ToPrettyString(uid)} started cloning {ToPrettyString(body.Value)}.");
_cloningSystem.AttemptCloning(cloningPodUid, cloningPod);
}
}
public bool RecheckConnections(EntityUid console, EntityUid? cloningPod, EntityUid? scanner, CloningConsoleComponent? consoleComp = null)
{
if (!Resolve(console, ref consoleComp))
return false;
var connected = true;
if (scanner != null)
{
Transform(scanner.Value).Coordinates.TryDistance(EntityManager, Transform(console).Coordinates, out float scannerDistance);
consoleComp.GeneticScannerInRange = scannerDistance <= consoleComp.MaxDistance;
connected = false;
}
if (cloningPod != null)
{
Transform(cloningPod.Value).Coordinates.TryDistance(EntityManager, Transform(console).Coordinates, out float podDistance);
consoleComp.CloningPodInRange = podDistance <= consoleComp.MaxDistance;
connected = false;
}
UpdateUserInterface(console, consoleComp);
return connected;
}
private CloningConsoleBoundUserInterfaceState GetUserInterfaceState(CloningConsoleComponent consoleComponent)
{
ClonerStatus clonerStatus = ClonerStatus.Ready;
// genetic scanner info
string scanBodyInfo = Loc.GetString("generic-unknown");
bool scannerConnected = false;
bool scannerInRange = consoleComponent.GeneticScannerInRange;
if (consoleComponent.GeneticScanner != null && TryComp<MedicalScannerComponent>(consoleComponent.GeneticScanner, out var scanner))
{
scannerConnected = true;
EntityUid? scanBody = scanner.BodyContainer.ContainedEntity;
// GET STATE
if (scanBody == null
|| !HasComp<MobStateComponent>(scanBody))
clonerStatus = ClonerStatus.ScannerEmpty;
else
{
scanBodyInfo = MetaData(scanBody.Value).EntityName;
if (!_mobStateSystem.IsDead(scanBody.Value))
clonerStatus = ClonerStatus.ScannerOccupantAlive;
else if (!_mindSystem.TryGetMind(scanBody.Value, out _, out var mind)
|| mind.UserId == null
|| !_playerManager.TryGetSessionById(mind.UserId.Value, out _))
clonerStatus = ClonerStatus.NoMindDetected;
}
}
// cloning pod info
var cloneBodyInfo = Loc.GetString("generic-unknown");
bool clonerConnected = false;
bool clonerMindPresent = false;
bool clonerInRange = consoleComponent.CloningPodInRange;
if (consoleComponent.CloningPod != null && TryComp<CloningPodComponent>(consoleComponent.CloningPod, out var clonePod)
&& Transform(consoleComponent.CloningPod.Value).Anchored)
{
clonerConnected = true;
EntityUid? cloneBody = clonePod.BodyContainer.ContainedEntity;
clonerMindPresent = clonePod.Status == CloningPodStatus.Cloning;
if (clonePod.ActivelyCloning)
{
if (cloneBody != null)
cloneBodyInfo = Identity.Name(cloneBody.Value, EntityManager);
clonerStatus = ClonerStatus.ClonerOccupied;
}
}
else
clonerStatus = ClonerStatus.NoClonerDetected;
return new CloningConsoleBoundUserInterfaceState(
scanBodyInfo,
cloneBodyInfo,
clonerMindPresent,
clonerStatus,
scannerConnected,
scannerInRange,
clonerConnected,
clonerInRange
);
}
}
}