-
Notifications
You must be signed in to change notification settings - Fork 592
Expand file tree
/
Copy pathSharedBorgSystem.cs
More file actions
530 lines (453 loc) · 21.7 KB
/
Copy pathSharedBorgSystem.cs
File metadata and controls
530 lines (453 loc) · 21.7 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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
using Content.Shared.Access.Systems;
using Content.Shared.Actions;
using Content.Shared.Administration.Logs;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Database;
using Content.Shared.Gibbing;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.IdentityManagement;
using Content.Shared.Interaction;
using Content.Shared.Light;
using Content.Shared.Light.Components;
using Content.Shared.Mind;
using Content.Shared.Mind.Components;
using Content.Shared.Mobs;
using Content.Shared.Mobs.Systems;
using Content.Shared.Movement.Components;
using Content.Shared.Movement.Systems;
using Content.Shared.Pointing;
using Content.Shared.Popups;
using Content.Shared.PowerCell;
using Content.Shared.PowerCell.Components;
using Content.Shared.Roles;
using Content.Shared.Silicons.Borgs.Components;
using Content.Shared.Throwing;
using Content.Shared.UserInterface;
using Content.Shared.Wires;
using Content.Shared.Whitelist;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Configuration;
using Robust.Shared.Containers;
using Robust.Shared.Network;
using Robust.Shared.Player;
using Robust.Shared.Random;
using Robust.Shared.Timing;
using Content.Shared.Radio.Components;
using Content.Shared._Starlight.Silicons.Borgs;
using Content.Shared.Actions.Components;
using Content.Shared.NameModifier.EntitySystems;
using Robust.Shared.Prototypes;
using Content.Shared._Starlight.TextToSpeech;
using Content.Shared.Tag;
namespace Content.Shared.Silicons.Borgs;
/// <summary>
/// This handles logic, interactions, and UI related to <see cref="BorgChassisComponent"/> and other related components.
/// </summary>
public abstract partial class SharedBorgSystem : EntitySystem
{
[Dependency] private SharedContainerSystem _container = default!;
[Dependency] private ItemSlotsSystem _itemSlots = default!;
[Dependency] private SharedPopupSystem _popup = default!;
[Dependency] private SharedRoleSystem _roles = default!;
[Dependency] private SharedMindSystem _mind = default!;
[Dependency] private SharedAppearanceSystem _appearance = default!;
[Dependency] private IGameTiming _timing = default!;
[Dependency] private MovementSpeedModifierSystem _movementSpeedModifier = default!;
[Dependency] private PowerCellSystem _powerCell = default!;
[Dependency] private EntityWhitelistSystem _whitelist = default!;
[Dependency] private SharedHandsSystem _hands = default!;
[Dependency] private SharedActionsSystem _actions = default!;
[Dependency] private MetaDataSystem _metaData = default!;
[Dependency] private MobStateSystem _mobState = default!;
[Dependency] private ThrowingSystem _throwing = default!;
[Dependency] private ISharedPlayerManager _player = default!;
[Dependency] private IRobustRandom _random = default!;
[Dependency] private IConfigurationManager _configuration = default!;
[Dependency] private ISharedAdminLogManager _adminLog = default!;
[Dependency] private INetManager _net = default!;
[Dependency] private SharedHandheldLightSystem _handheldLight = default!;
[Dependency] private SharedAccessSystem _access = default!;
[Dependency] private SharedAudioSystem _audio = default!;
[Dependency] private NameModifierSystem _nameModifier = default!; // Starlight
[Dependency] private TagSystem _tag = default!; // Starlight
/// <inheritdoc/>
public override void Initialize()
{
base.Initialize();
InitializeMMI();
InitializeModule();
InitializeRelay();
InitializeUI();
SubscribeLocalEvent<TryGetIdentityShortInfoEvent>(OnTryGetIdentityShortInfo);
SubscribeLocalEvent<BorgChassisComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<BorgChassisComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<BorgChassisComponent, ItemSlotInsertAttemptEvent>(OnItemSlotInsertAttempt);
SubscribeLocalEvent<BorgChassisComponent, ItemSlotEjectAttemptEvent>(OnItemSlotEjectAttempt);
SubscribeLocalEvent<BorgChassisComponent, EntInsertedIntoContainerMessage>(OnInserted);
SubscribeLocalEvent<BorgChassisComponent, EntRemovedFromContainerMessage>(OnRemoved);
SubscribeLocalEvent<BorgChassisComponent, MindAddedMessage>(OnMindAdded);
SubscribeLocalEvent<BorgChassisComponent, MindRemovedMessage>(OnMindRemoved);
SubscribeLocalEvent<BorgChassisComponent, AfterInteractUsingEvent>(OnChassisInteractUsing);
SubscribeLocalEvent<BorgChassisComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMovementSpeedModifiers);
SubscribeLocalEvent<BorgChassisComponent, ActivatableUIOpenAttemptEvent>(OnUIOpenAttempt);
SubscribeLocalEvent<BorgChassisComponent, MobStateChangedEvent>(OnMobStateChanged);
SubscribeLocalEvent<BorgChassisComponent, GibbedBeforeDeletionEvent>(OnBeingGibbed);
SubscribeLocalEvent<BorgChassisComponent, GetCharactedDeadIcEvent>(OnGetDeadIC);
SubscribeLocalEvent<BorgChassisComponent, GetCharacterUnrevivableIcEvent>(OnGetUnrevivableIC);
SubscribeLocalEvent<BorgChassisComponent, PowerCellSlotEmptyEvent>(OnPowerCellSlotEmpty);
SubscribeLocalEvent<BorgChassisComponent, PowerCellChangedEvent>(OnPowerCellChanged);
SubscribeLocalEvent<BorgBrainComponent, MindAddedMessage>(OnBrainMindAdded);
SubscribeLocalEvent<BorgBrainComponent, PointAttemptEvent>(OnBrainPointAttempt);
}
private void OnTryGetIdentityShortInfo(TryGetIdentityShortInfoEvent args)
{
if (args.Handled)
{
return;
}
// TODO: Why the hell is this only broadcasted and not raised directed on the entity?
// This is doing a ton of HasComps/TryComps.
if (!HasComp<BorgChassisComponent>(args.ForActor))
{
return;
}
args.Title = Name(args.ForActor).Trim();
args.Handled = true;
}
private void OnStartup(Entity<BorgChassisComponent> chassis, ref ComponentStartup args)
{
if (!TryComp<ContainerManagerComponent>(chassis, out var containerManager))
return;
chassis.Comp.BrainContainer = _container.EnsureContainer<ContainerSlot>(chassis.Owner, chassis.Comp.BrainContainerId, containerManager);
chassis.Comp.ModuleContainer = _container.EnsureContainer<Container>(chassis.Owner, chassis.Comp.ModuleContainerId, containerManager);
}
private void OnMapInit(Entity<BorgChassisComponent> chassis, ref MapInitEvent args)
{
_movementSpeedModifier.RefreshMovementSpeedModifiers(chassis.Owner);
// Starlight-start: If the borg has a brain, synchronize the name of the brain with the chassis.
if (chassis.Comp.BrainEntity is { } brain)
SynchronizeBrainName(chassis, brain);
// Starlight-end
}
// Starlight: function to synchronize the name of the brain with the chassis, and the other way around.
private void SynchronizeBrainName(Entity<BorgChassisComponent> chassis, EntityUid brain)
{
var brainName = _nameModifier.GetBaseName(brain);
var chassisName = _nameModifier.GetBaseName(chassis.Owner);
if (brainName.Equals(chassisName, StringComparison.InvariantCulture))
return;
if (MetaData(brain).EntityPrototype is { } brainPrototype &&
brainName.Equals(brainPrototype.Name, StringComparison.InvariantCulture))
{
_metaData.SetEntityName(brain, chassisName);
}
else
{
_metaData.SetEntityName(chassis, brainName);
}
}
private void OnItemSlotInsertAttempt(Entity<BorgChassisComponent> chassis, ref ItemSlotInsertAttemptEvent args)
{
if (args.Cancelled)
return;
if (!TryComp<PowerCellSlotComponent>(chassis, out var cellSlotComp) ||
!TryComp<WiresPanelComponent>(chassis, out var panelComp))
return;
if (!_itemSlots.TryGetSlot(chassis.Owner, cellSlotComp.CellSlotId, out var cellSlot) || cellSlot != args.Slot)
return;
if (!panelComp.Open || args.User == chassis.Owner)
args.Cancelled = true;
}
private void OnItemSlotEjectAttempt(Entity<BorgChassisComponent> chassis, ref ItemSlotEjectAttemptEvent args)
{
if (args.Cancelled)
return;
if (!TryComp<PowerCellSlotComponent>(chassis, out var cellSlotComp) ||
!TryComp<WiresPanelComponent>(chassis, out var panel))
return;
if (!_itemSlots.TryGetSlot(chassis.Owner, cellSlotComp.CellSlotId, out var cellSlot) || cellSlot != args.Slot)
return;
if (!panel.Open || args.User == chassis.Owner)
args.Cancelled = true;
}
// TODO: consider transferring over the ghost role? managing that might suck.
protected virtual void OnInserted(Entity<BorgChassisComponent> chassis, ref EntInsertedIntoContainerMessage args)
{
if (_timing.ApplyingState)
return; // The changes are already networked with the same game state
if (args.Container != chassis.Comp.BrainContainer)
return;
//#region Starlight
SynchronizeBrainName(chassis, args.Entity);
if (TryComp(args.Entity, out BorgBrainComponent? brain) && _mind.TryGetMind(args.Entity, out var mindId, out var mind))
{
//re-target the station-AI's shunt target to the chassis insteaf of the brain
if (TryComp<StationAIShuntComponent>(args.Entity, out var shunt) &&
TryComp<StationAIShuntableComponent>(shunt.Return, out var shuntable) &&
EnsureComp<StationAIShuntComponent>(chassis, out var borgShunt)
)
{
shuntable.Inhabited = chassis; // Starlight
shuntable.LastShunt = chassis; // Starlight
Dirty(shunt.Return.Value, shuntable); // Starlight
borgShunt.Return = shunt.Return;
borgShunt.ReturnAction = _actions.AddAction(chassis, shuntable.UnshuntAction);
}
//Get borging consent
if(!brain.BorgConsent)
RaiseLocalEvent(args.Entity, new AskBorgingChoiceEvent());
else
TransferMindToChassis(args.Entity, mindId, mind);
//regardless of outcome here, the player will either be
//choosing to play borg or be ghosted, and we do not want
//to ask whoever chooses to take over the gost role again
//if they get a chassis transfer.
brain.BorgConsent = true;
}
//#endregion Starlight
}
protected virtual void OnRemoved(Entity<BorgChassisComponent> chassis, ref EntRemovedFromContainerMessage args)
{
if (_timing.ApplyingState)
return; // The changes are already networked with the same game state
ValidateWhitelists(chassis, args.Entity);
if (args.Container != chassis.Comp.BrainContainer)
return;
if (HasComp<BorgBrainComponent>(args.Entity) && _mind.TryGetMind(chassis.Owner, out var mindId, out var mind))
{
//#region Starlight
//re-target the station-AI's shunt target to the brain instead of the borg. so it doesn't get lost.
if (TryComp<StationAIShuntComponent>(args.Entity, out var shunt) &&
TryComp<StationAIShuntableComponent>(shunt.Return, out var shuntable) &&
TryComp<StationAIShuntComponent>(chassis, out var borgShunt))
{
shuntable.Inhabited = args.Entity; // Starlight
shuntable.LastShunt = args.Entity; // Starlight
Dirty(shunt.Return.Value, shuntable); // Starlight
if (TryComp<ActionComponent>(borgShunt.ReturnAction, out var action))
_actions.RemoveAction((borgShunt.ReturnAction.Value, action)); //delete the action as we leave the body
borgShunt.Return = null;
borgShunt.ReturnAction = null;
}
//#endregion
_mind.TransferTo(mindId, args.Entity, mind: mind);
}
}
private void OnMindAdded(Entity<BorgChassisComponent> chassis, ref MindAddedMessage args)
{
// Unpredicted because the event is raised on the server.
_popup.PopupEntity(Loc.GetString("borg-mind-added", ("name", Identity.Name(chassis.Owner, EntityManager))), chassis.Owner);
//Starlight start, load the borg's tts voice
if (TryComp<TextToSpeechComponent>(chassis, out var ttscomp))
{
ttscomp.VoicePrototypeId = args.Mind.Comp.SiliconVoice;
}
//Starlight end
TryActivate(chassis);
_appearance.SetData(chassis.Owner, BorgVisuals.HasPlayer, true);
}
private void OnMindRemoved(Entity<BorgChassisComponent> chassis, ref MindRemovedMessage args)
{
// Unpredicted because the event is raised on the server.
_popup.PopupEntity(Loc.GetString("borg-mind-removed", ("name", Identity.Name(chassis.Owner, EntityManager))), chassis.Owner);
SetActive(chassis, false);
// Turn off the light so that the no-player visuals can be seen.
if (TryComp<HandheldLightComponent>(chassis.Owner, out var light))
_handheldLight.TurnOff((chassis.Owner, light), makeNoise: false); // Already plays a sound when toggling the borg off.
_appearance.SetData(chassis.Owner, BorgVisuals.HasPlayer, false);
}
private void OnChassisInteractUsing(Entity<BorgChassisComponent> chassis, ref AfterInteractUsingEvent args)
{
if (!args.CanReach || args.Handled || chassis.Owner == args.User)
return;
var used = args.Used;
TryComp<BorgBrainComponent>(used, out var brain);
TryComp<BorgModuleComponent>(used, out var module);
TryComp<EncryptionKeyComponent>(used, out var key); // Starlight, encryption keys.
if (TryComp<WiresPanelComponent>(chassis, out var panel) && !panel.Open)
{
if (brain != null || module != null || key != null) // Starlight, encryption keys.
{
_popup.PopupClient(Loc.GetString("borg-panel-not-open"), chassis, args.User);
}
return;
}
if (chassis.Comp.BrainEntity == null && brain != null &&
_whitelist.IsWhitelistPassOrNull(chassis.Comp.BrainWhitelist, used))
{
if (TryComp<ActorComponent>(used, out var actor) && !CanPlayerBeBorged(actor.PlayerSession))
{
// Don't use PopupClient because CanPlayerBeBorged is not predicted.
_popup.PopupEntity(Loc.GetString("borg-player-not-allowed"), used, args.User);
return;
}
_container.Insert(used, chassis.Comp.BrainContainer);
_adminLog.Add(LogType.Action, LogImpact.Medium,
$"{args.User} installed brain {used} into borg {chassis.Owner}");
args.Handled = true;
return;
}
if (module != null && CanInsertModule(chassis.AsNullable(), (used, module), args.User))
{
InsertModule(chassis, used);
_adminLog.Add(LogType.Action, LogImpact.Low,
$"{args.User} installed module {used} into borg {chassis.Owner}");
args.Handled = true;
}
// Starlight, encryption keys.
if (key != null)
{
AddRadioChannels(chassis, used);
_adminLog.Add(LogType.Action, LogImpact.Low,
$"{ToPrettyString(args.User):player} added encryption key {ToPrettyString(used)} to borg {ToPrettyString(chassis)}");
args.Handled = true;
}
// End Starlight
}
// Starlight, this allows people to use an encryption key on a borg to add channels to them.
public void AddRadioChannels(Entity<BorgChassisComponent> ent, EntityUid args)
{
if (TryComp<EncryptionKeyComponent>(args, out var key))
{
if (TryComp(ent, out ActiveRadioComponent? activeRadio))
{
//Starlight begin
foreach (var channel in key.Channels)
activeRadio.Channels.Add(channel);
foreach (var channel in key.CustomChannels)
activeRadio.CustomChannels.Add(channel);
Dirty(ent, activeRadio);
//Starlight end
}
if (TryComp(ent, out IntrinsicRadioTransmitterComponent? transmitter))
{
//Starlight begin
foreach (var channel in key.Channels)
transmitter.Channels.Add(channel);
foreach (var channel in key.CustomChannels)
transmitter.CustomChannels.Add(channel);
Dirty(ent, transmitter);
//Starlight end
}
}
Dirty(ent);
}
// end Starlight
// Make the borg slower without power.
private void OnRefreshMovementSpeedModifiers(Entity<BorgChassisComponent> chassis, ref RefreshMovementSpeedModifiersEvent args)
{
if (chassis.Comp.Active)
return;
if (!TryComp<MovementSpeedModifierComponent>(chassis, out var movement))
return;
if (movement.BaseSprintSpeed == 0f)
return; // We already cannot move.
// Slow down to walk speed.
var sprintDif = movement.BaseWalkSpeed / movement.BaseSprintSpeed;
args.ModifySpeed(1f, sprintDif);
}
private void OnUIOpenAttempt(Entity<BorgChassisComponent> chassis, ref ActivatableUIOpenAttemptEvent args)
{
// Borgs generally can't view their own UI.
if (args.User == chassis.Owner && !chassis.Comp.CanOpenSelfUi)
args.Cancel();
}
private void OnMobStateChanged(Entity<BorgChassisComponent> chassis, ref MobStateChangedEvent args)
{
if (args.NewMobState == MobState.Alive)
TryActivate(chassis, args.Origin);
else
{
SetActive(chassis, false, user: args.Origin);
}
}
private void OnBeingGibbed(Entity<BorgChassisComponent> chassis, ref GibbedBeforeDeletionEvent args)
{
// Don't use the ItemSlotsSystem eject method since we don't want to play a sound and want we to eject the battery even if the slot is locked.
if (TryComp<PowerCellSlotComponent>(chassis, out var slotComp) &&
_container.TryGetContainer(chassis, slotComp.CellSlotId, out var slotContainer))
_container.EmptyContainer(slotContainer);
_container.EmptyContainer(chassis.Comp.BrainContainer);
_container.EmptyContainer(chassis.Comp.ModuleContainer);
}
private void OnGetDeadIC(Entity<BorgChassisComponent> chassis, ref GetCharactedDeadIcEvent args)
{
args.Dead = true;
}
private void OnGetUnrevivableIC(Entity<BorgChassisComponent> chassis, ref GetCharacterUnrevivableIcEvent args)
{
args.Unrevivable = true;
}
private void OnBrainMindAdded(Entity<BorgBrainComponent> brain, ref MindAddedMessage args)
{
if (!_container.TryGetContainingContainer(brain.Owner, out var container))
return;
var borg = container.Owner;
if (!TryComp<BorgChassisComponent>(borg, out var chassisComponent) ||
container.ID != chassisComponent.BrainContainerId)
return;
if (!_mind.TryGetMind(brain.Owner, out var mindId, out var mind) ||
!_player.TryGetSessionById(mind.UserId, out var session))
return;
if (!CanPlayerBeBorged(session))
{
// Don't use PopupClient because MindAddedMessage and CanPlayerBeBorged are not predicted.
_popup.PopupEntity(Loc.GetString("borg-player-not-allowed-eject"), brain);
_container.RemoveEntity(borg, brain);
_throwing.TryThrow(brain, _random.NextVector2() * 5, 5f);
return;
}
//Starlight Start
if(!brain.Comp.BorgConsent)
RaiseLocalEvent(brain.Owner, new AskBorgingChoiceEvent());
else
TransferMindToChassis(brain.Owner, mindId, mind);
//regardless of outcome here, the player will either be
//choosing to play borg or be ghosted, and we do not want
//to ask whoever chooses to take over the gost role again
//if they get a chassis transfer.
brain.Comp.BorgConsent = true;
}
public void TransferMindToChassis(EntityUid uid, EntityUid mindId, MindComponent mind)
{
if (!_container.TryGetContainingContainer(uid, out var container))
return;
var borg = container.Owner;
//load borg voice
if (TryComp<TextToSpeechComponent>(uid, out var ttscomp))
{
if(mind != null)
ttscomp.VoicePrototypeId = mind.SiliconVoice;
}
//Starlight End
_mind.TransferTo(mindId, borg, mind: mind);
}
private void OnBrainPointAttempt(Entity<BorgBrainComponent> brain, ref PointAttemptEvent args)
{
args.Cancel();
}
// Raised when the power cell is empty or removed from the borg.
private void OnPowerCellSlotEmpty(Entity<BorgChassisComponent> chassis, ref PowerCellSlotEmptyEvent args)
{
SetActive(chassis, false);
}
// Raised when a power cell is inserted.
private void OnPowerCellChanged(Entity<BorgChassisComponent> chassis, ref PowerCellChangedEvent args)
{
TryActivate(chassis);
}
public override void Update(float frameTime)
{
var curTime = _timing.CurTime;
var query = EntityQueryEnumerator<BorgChassisComponent>();
while (query.MoveNext(out var uid, out var borgChassis))
{
if (curTime < borgChassis.NextBatteryUpdate)
continue;
borgChassis.NextBatteryUpdate = curTime + TimeSpan.FromSeconds(1);
Dirty(uid, borgChassis);
// If we aren't drawing and suddenly get enough power to draw again, reenable.
TryActivate((uid, borgChassis));
}
}
}