forked from space-syndicate/space-station-14-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPullingSystem.cs
1057 lines (877 loc) · 39.9 KB
/
PullingSystem.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
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Content.Shared.Contests; // Goobstation - Grab Intent
using System.Diagnostics.CodeAnalysis;
using System.Numerics; // Goobstation - Grab Intent
using Content.Shared._White.Grab; // Goobstation
using Content.Shared.ActionBlocker;
using Content.Shared.Administration.Logs;
using Content.Shared.Alert;
using Content.Shared.Buckle.Components;
using Content.Shared.CombatMode; // Goobstation
using Content.Shared.Cuffs.Components; // Goobstation
using Content.Shared.Damage; // Goobstation
using Content.Shared.Damage.Systems; // Goobstation
using Content.Shared.Database;
using Content.Shared.Effects; // Goobstation
using Content.Shared.Gravity;
using Content.Shared.Hands;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.IdentityManagement;
using Content.Shared.Input;
using Content.Shared.Interaction;
using Content.Shared.Inventory.VirtualItem; // Goobstation
using Content.Shared.Item;
using Content.Shared.Mobs;
using Content.Shared.Mobs.Components; // Goobstation
using Content.Shared.Mobs.Systems;
using Content.Shared.CombatMode.Pacification;
using Content.Shared.Popups;
using Content.Shared.IdentityManagement;
using Content.Shared.Movement.Components;
using Content.Shared.Movement.Events;
using Content.Shared.Movement.Pulling.Components;
using Content.Shared.Movement.Pulling.Events;
using Content.Shared.Movement.Systems;
using Content.Shared.Popups;
using Content.Shared.Pulling.Events;
using Content.Shared.Speech; // Goobstation
using Content.Shared.Standing;
using Content.Shared.Throwing; // Goobstation
using Content.Shared.Verbs;
using Robust.Shared.Audio; // Goobstation
using Robust.Shared.Audio.Systems; // Goobstation
using Robust.Shared.Containers;
using Robust.Shared.Input.Binding;
using Robust.Shared.Network; // Goobstation
using Robust.Shared.Map;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Events;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Player;
using Robust.Shared.Random; // Goobstation
using Robust.Shared.Timing;
namespace Content.Shared.Movement.Pulling.Systems;
/// <summary>
/// Allows one entity to pull another behind them via a physics distance joint.
/// </summary>
public sealed class PullingSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] private readonly ActionBlockerSystem _blocker = default!;
[Dependency] private readonly AlertsSystem _alertsSystem = default!;
[Dependency] private readonly MovementSpeedModifierSystem _modifierSystem = default!;
[Dependency] private readonly SharedJointSystem _joints = default!;
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly SharedInteractionSystem _interaction = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly HeldSpeedModifierSystem _clothingMoveSpeed = default!;
[Dependency] private readonly SharedTransformSystem _xformSys = default!;
[Dependency] private readonly ThrownItemSystem _thrownItem = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly StaminaSystem _stamina = default!;
[Dependency] private readonly SharedColorFlashEffectSystem _color = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] private readonly SharedVirtualItemSystem _virtualSystem = default!;
[Dependency] private readonly GrabThrownSystem _grabThrown = default!;
[Dependency] private readonly SharedCombatModeSystem _combatMode = default!;
[Dependency] private readonly ThrowingSystem _throwing = default!;
[Dependency] private readonly ContestsSystem _contests = default!; // Goobstation - Grab Intent
public override void Initialize()
{
base.Initialize();
UpdatesAfter.Add(typeof(SharedPhysicsSystem));
UpdatesOutsidePrediction = true;
SubscribeLocalEvent<PullableComponent, MoveInputEvent>(OnPullableMoveInput);
SubscribeLocalEvent<PullableComponent, CollisionChangeEvent>(OnPullableCollisionChange);
SubscribeLocalEvent<PullableComponent, JointRemovedEvent>(OnJointRemoved);
SubscribeLocalEvent<PullableComponent, GetVerbsEvent<Verb>>(AddPullVerbs);
SubscribeLocalEvent<PullableComponent, EntGotInsertedIntoContainerMessage>(OnPullableContainerInsert);
SubscribeLocalEvent<PullableComponent, ModifyUncuffDurationEvent>(OnModifyUncuffDuration);
SubscribeLocalEvent<PullableComponent, StopBeingPulledAlertEvent>(OnStopBeingPulledAlert);
SubscribeLocalEvent<PullableComponent, UpdateCanMoveEvent>(OnGrabbedMoveAttempt); // Goobstation
SubscribeLocalEvent<PullableComponent, SpeakAttemptEvent>(OnGrabbedSpeakAttempt); // Goobstation
SubscribeLocalEvent<PullerComponent, AfterAutoHandleStateEvent>(OnAfterState);
SubscribeLocalEvent<PullerComponent, EntGotInsertedIntoContainerMessage>(OnPullerContainerInsert);
SubscribeLocalEvent<PullerComponent, EntityUnpausedEvent>(OnPullerUnpaused);
SubscribeLocalEvent<PullerComponent, VirtualItemDeletedEvent>(OnVirtualItemDeleted);
SubscribeLocalEvent<PullerComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMovespeed);
SubscribeLocalEvent<PullerComponent, DropHandItemsEvent>(OnDropHandItems);
SubscribeLocalEvent<PullerComponent, StopPullingAlertEvent>(OnStopPullingAlert);
SubscribeLocalEvent<PullerComponent, VirtualItemThrownEvent>(OnVirtualItemThrown); // Goobstation - Grab Intent
SubscribeLocalEvent<PullerComponent, AddCuffDoAfterEvent>(OnAddCuffDoAfterEvent); // Goobstation - Grab Intent
SubscribeLocalEvent<PullableComponent, StrappedEvent>(OnBuckled);
SubscribeLocalEvent<PullableComponent, BuckledEvent>(OnGotBuckled);
CommandBinds.Builder
.Bind(ContentKeyFunctions.ReleasePulledObject, InputCmdHandler.FromDelegate(OnReleasePulledObject, handle: false))
.Register<PullingSystem>();
}
// Goobstation - Grab Intent
private void OnAddCuffDoAfterEvent(Entity<PullerComponent> ent, ref AddCuffDoAfterEvent args)
{
if (args.Handled)
return;
if (!args.Cancelled
&& TryComp<PullableComponent>(ent.Comp.Pulling, out var comp)
&& ent.Comp.Pulling != null)
{
if(_netManager.IsServer)
StopPulling((EntityUid) ent.Comp.Pulling, comp);
}
}
// Goobstation
private void OnBuckled(Entity<PullableComponent> ent, ref StrappedEvent args)
{
// Prevent people from pulling the entity they are buckled to
if (ent.Comp.Puller == args.Buckle.Owner && !args.Buckle.Comp.PullStrap)
StopPulling(ent, ent);
}
private void OnGotBuckled(Entity<PullableComponent> ent, ref BuckledEvent args)
{
StopPulling(ent, ent);
}
private void OnAfterState(Entity<PullerComponent> ent, ref AfterAutoHandleStateEvent args)
{
if (ent.Comp.Pulling == null)
RemComp<ActivePullerComponent>(ent.Owner);
else
EnsureComp<ActivePullerComponent>(ent.Owner);
}
private void OnDropHandItems(EntityUid uid, PullerComponent pullerComp, DropHandItemsEvent args)
{
if (pullerComp.Pulling == null || pullerComp.NeedsHands)
return;
if (!TryComp(pullerComp.Pulling, out PullableComponent? pullableComp))
return;
TryStopPull(pullerComp.Pulling.Value, pullableComp, uid);
}
private void OnStopPullingAlert(Entity<PullerComponent> ent, ref StopPullingAlertEvent args)
{
if (args.Handled)
return;
if (!TryComp<PullableComponent>(ent.Comp.Pulling, out var pullable))
return;
args.Handled = TryStopPull(ent.Comp.Pulling.Value, pullable, ent);
}
private void OnPullerContainerInsert(Entity<PullerComponent> ent, ref EntGotInsertedIntoContainerMessage args)
{
if (ent.Comp.Pulling == null)
return;
if (!TryComp(ent.Comp.Pulling.Value, out PullableComponent? pulling))
return;
// Goobstation - Grab Intent
foreach (var item in ent.Comp.GrabVirtualItems)
QueueDel(item);
TryStopPull(ent.Comp.Pulling.Value, pulling, ent.Owner, true);
// Goobstation
}
private void OnPullableContainerInsert(Entity<PullableComponent> ent, ref EntGotInsertedIntoContainerMessage args)
{
TryStopPull(ent.Owner, ent.Comp, ignoreGrab: true); // Goobstation
}
private void OnModifyUncuffDuration(Entity<PullableComponent> ent, ref ModifyUncuffDurationEvent args)
{
if (!ent.Comp.BeingPulled)
return;
// We don't care if the person is being uncuffed by someone else
if (args.User != args.Target)
return;
args.Duration *= 2;
}
private void OnStopBeingPulledAlert(Entity<PullableComponent> ent, ref StopBeingPulledAlertEvent args)
{
if (args.Handled)
return;
args.Handled = TryStopPull(ent, ent, ent);
}
public override void Shutdown()
{
base.Shutdown();
CommandBinds.Unregister<PullingSystem>();
}
private void OnPullerUnpaused(EntityUid uid, PullerComponent component, ref EntityUnpausedEvent args)
{
component.NextThrow += args.PausedTime;
}
// Goobstation - Grab Intent
private void OnVirtualItemDeleted(Entity<PullerComponent> ent, ref VirtualItemDeletedEvent args)
{
// If client deletes the virtual hand then stop the pull.
if (ent.Comp.Pulling == null)
return;
if (ent.Comp.Pulling != args.BlockingEntity)
return;
if (TryComp(args.BlockingEntity, out PullableComponent? comp))
{
TryStopPull(ent.Comp.Pulling.Value, comp, ent);
}
foreach (var item in ent.Comp.GrabVirtualItems)
{
if(TryComp<VirtualItemComponent>(ent, out var virtualItemComponent))
_virtualSystem.DeleteVirtualItem((item,virtualItemComponent), ent);
}
ent.Comp.GrabVirtualItems.Clear();
}
// Goobstation - Grab Intent Refactor
// Goobstation - Grab Intent
private void OnVirtualItemThrown(EntityUid uid, PullerComponent component, VirtualItemThrownEvent args)
{
if (!TryComp<PhysicsComponent>(uid, out var throwerPhysics)
|| component.Pulling == null
|| component.Pulling != args.BlockingEntity)
return;
if (!TryComp(args.BlockingEntity, out PullableComponent? comp))
return;
if (!_combatMode.IsInCombatMode(uid)
|| HasComp<GrabThrownComponent>(args.BlockingEntity)
|| component.GrabStage <= GrabStage.Soft)
return;
var distanceToCursor = args.Direction.Length();
var direction = args.Direction.Normalized() * MathF.Min(distanceToCursor, component.ThrowingDistance);
var damage = new DamageSpecifier();
damage.DamageDict.Add("Blunt", 5);
TryStopPull(args.BlockingEntity, comp, uid, true);
_grabThrown.Throw(args.BlockingEntity,
uid,
direction,
component.GrabThrownSpeed,
component.StaminaDamageOnThrown,
damage * component.GrabThrowDamageModifier); // Throwing the grabbed person
_throwing.TryThrow(uid, -direction * throwerPhysics.InvMass); // Throws back the grabber
_audio.PlayPvs(new SoundPathSpecifier("/Audio/Effects/thudswoosh.ogg"), uid);
component.NextStageChange.Add(TimeSpan.FromSeconds(4f)); // To avoid grab and throw spamming
}
// Goobstation
private void AddPullVerbs(EntityUid uid, PullableComponent component, GetVerbsEvent<Verb> args)
{
if (!args.CanAccess || !args.CanInteract)
return;
// Are they trying to pull themselves up by their bootstraps?
if (args.User == args.Target)
return;
//TODO VERB ICONS add pulling icon
if (component.Puller == args.User)
{
Verb verb = new()
{
Text = Loc.GetString("pulling-verb-get-data-text-stop-pulling"),
Act = () => TryStopPull(uid, component, user: args.User),
DoContactInteraction = false // pulling handle its own contact interaction.
};
args.Verbs.Add(verb);
}
else if (CanPull(args.User, args.Target))
{
Verb verb = new()
{
Text = Loc.GetString("pulling-verb-get-data-text"),
Act = () => TryStartPull(args.User, args.Target),
DoContactInteraction = false // pulling handle its own contact interaction.
};
args.Verbs.Add(verb);
}
}
private void OnRefreshMovespeed(EntityUid uid, PullerComponent component, RefreshMovementSpeedModifiersEvent args)
{
if (TryComp<HeldSpeedModifierComponent>(component.Pulling, out var itemHeldSpeed) && component.Pulling.HasValue)
{
var (walkMod, sprintMod) =
_clothingMoveSpeed.GetHeldMovementSpeedModifiers(component.Pulling.Value, itemHeldSpeed);
args.ModifySpeed(walkMod, sprintMod);
}
if (TryComp<HeldSpeedModifierComponent>(component.Pulling, out var heldMoveSpeed) && component.Pulling.HasValue)
{
var (walkMod, sprintMod) = (args.WalkSpeedModifier, args.SprintSpeedModifier);
switch (component.GrabStage)
{
case GrabStage.No:
args.ModifySpeed(walkMod, sprintMod);
break;
case GrabStage.Soft:
var softSpeedMod = component.SoftGrabSpeedModifier;
args.ModifySpeed(walkMod * softSpeedMod, sprintMod * softSpeedMod);
break;
case GrabStage.Hard:
var hardSpeedMod = component.HardGrabSpeedModifier;
args.ModifySpeed(walkMod * hardSpeedMod, sprintMod * hardSpeedMod);
break;
case GrabStage.Suffocate:
var chokeSpeedMod = component.ChokeGrabSpeedModifier;
args.ModifySpeed(walkMod * chokeSpeedMod, sprintMod * chokeSpeedMod);
break;
default:
args.ModifySpeed(walkMod, sprintMod);
break;
}
return;
}
switch (component.GrabStage)
{
case GrabStage.No:
args.ModifySpeed(component.WalkSpeedModifier, component.SprintSpeedModifier);
break;
case GrabStage.Soft:
var softSpeedMod = component.SoftGrabSpeedModifier;
args.ModifySpeed(component.WalkSpeedModifier * softSpeedMod, component.SprintSpeedModifier * softSpeedMod);
break;
case GrabStage.Hard:
var hardSpeedMod = component.HardGrabSpeedModifier;
args.ModifySpeed(component.WalkSpeedModifier * hardSpeedMod, component.SprintSpeedModifier * hardSpeedMod);
break;
case GrabStage.Suffocate:
var chokeSpeedMod = component.ChokeGrabSpeedModifier;
args.ModifySpeed(component.WalkSpeedModifier * chokeSpeedMod, component.SprintSpeedModifier * chokeSpeedMod);
break;
default:
args.ModifySpeed(component.WalkSpeedModifier, component.SprintSpeedModifier);
break;
}
}
// Goobstation - Grab Intent
private void OnPullableMoveInput(Entity<PullableComponent> ent, ref MoveInputEvent args)
{
// If someone moves then break their pulling.
if (!ent.Comp.BeingPulled)
return;
var entity = args.Entity;
if (ent.Comp.GrabStage == GrabStage.Soft)
TryStopPull(ent, ent, ent);
if (!_blocker.CanMove(entity))
return;
TryStopPull(ent, ent, user: ent);
}
// Goobstation
private void OnPullableCollisionChange(EntityUid uid, PullableComponent component, ref CollisionChangeEvent args)
{
// IDK what this is supposed to be.
if (!_timing.ApplyingState && component.PullJointId != null && !args.CanCollide)
{
_joints.RemoveJoint(uid, component.PullJointId);
}
}
private void OnJointRemoved(EntityUid uid, PullableComponent component, JointRemovedEvent args)
{
// Just handles the joint getting nuked without going through pulling system (valid behavior).
// Not relevant / pullable state handle it.
if (component.Puller != args.OtherEntity ||
args.Joint.ID != component.PullJointId ||
_timing.ApplyingState)
{
return;
}
if (args.Joint.ID != component.PullJointId || component.Puller == null)
return;
StopPulling(uid, component);
}
/// <summary>
/// Forces pulling to stop and handles cleanup.
/// </summary>
private void StopPulling(EntityUid pullableUid, PullableComponent pullableComp)
{
if (!_timing.ApplyingState)
{
// Joint shutdown
if (pullableComp.PullJointId != null)
{
_joints.RemoveJoint(pullableUid, pullableComp.PullJointId);
pullableComp.PullJointId = null;
}
if (TryComp<PhysicsComponent>(pullableUid, out var pullablePhysics))
{
_physics.SetFixedRotation(pullableUid, pullableComp.PrevFixedRotation, body: pullablePhysics);
}
}
var oldPuller = pullableComp.Puller;
if (oldPuller != null)
RemComp<ActivePullerComponent>(oldPuller.Value);
pullableComp.PullJointId = null;
pullableComp.Puller = null;
// Goobstation - Grab Intent
pullableComp.GrabStage = GrabStage.No;
pullableComp.GrabEscapeChance = 1f;
_blocker.UpdateCanMove(pullableUid);
// Goobstation
Dirty(pullableUid, pullableComp);
// No more joints with puller -> force stop pull.
if (TryComp<PullerComponent>(oldPuller, out var pullerComp))
{
var pullerUid = oldPuller.Value;
if (_netManager.IsServer)
_alertsSystem.ClearAlert(pullerUid, pullerComp.PullingAlert);
pullerComp.Pulling = null;
// Goobstation - Grab Intent
pullerComp.GrabStage = GrabStage.No;
var virtItems = pullerComp.GrabVirtualItems;
foreach (var item in virtItems)
QueueDel(item);
pullerComp.GrabVirtualItems.Clear();
// Goobstation
Dirty(oldPuller.Value, pullerComp);
// Messaging
var message = new PullStoppedMessage(pullerUid, pullableUid);
_modifierSystem.RefreshMovementSpeedModifiers(pullerUid);
_adminLogger.Add(LogType.Action, LogImpact.Low, $"{ToPrettyString(pullerUid):user} stopped pulling {ToPrettyString(pullableUid):target}");
RaiseLocalEvent(pullerUid, message);
RaiseLocalEvent(pullableUid, message);
}
if (_netManager.IsServer)
_alertsSystem.ClearAlert(pullableUid, pullableComp.PulledAlert);
}
public bool IsPulled(EntityUid uid, PullableComponent? component = null)
{
return Resolve(uid, ref component, false) && component.BeingPulled;
}
public bool IsPulling(EntityUid puller, PullerComponent? component = null)
{
return Resolve(puller, ref component, false) && component.Pulling != null;
}
private void OnReleasePulledObject(ICommonSession? session)
{
if (session?.AttachedEntity is not { Valid: true } player)
{
return;
}
if (!TryComp(player, out PullerComponent? pullerComp) ||
!TryComp(pullerComp.Pulling, out PullableComponent? pullableComp))
{
return;
}
TryStopPull(pullerComp.Pulling.Value, pullableComp, user: player, true); // Goobstation
}
public bool CanPull(EntityUid puller, EntityUid pullableUid, PullerComponent? pullerComp = null)
{
if (!Resolve(puller, ref pullerComp, false))
{
return false;
}
if (pullerComp.NeedsHands
&& !_handsSystem.TryGetEmptyHand(puller, out _)
&& pullerComp.Pulling == null)
{
return false;
}
if (!_blocker.CanInteract(puller, pullableUid))
{
return false;
}
if (!TryComp<PhysicsComponent>(pullableUid, out var physics)) // Goobstation
{
return false;
}
if (physics.BodyType == BodyType.Static)
{
return false;
}
if (puller == pullableUid)
{
return false;
}
if (!_containerSystem.IsInSameOrNoContainer(puller, pullableUid))
{
return false;
}
var getPulled = new BeingPulledAttemptEvent(puller, pullableUid);
RaiseLocalEvent(pullableUid, getPulled, true);
var startPull = new StartPullAttemptEvent(puller, pullableUid);
RaiseLocalEvent(puller, startPull, true);
return !startPull.Cancelled && !getPulled.Cancelled;
}
// Goobstation - Grab Intent
public bool TogglePull(Entity<PullableComponent?> pullable, EntityUid pullerUid)
{
if (!Resolve(pullable, ref pullable.Comp, false))
return false;
if (pullable.Comp.Puller != pullerUid)
return TryStartPull(pullerUid, pullable, pullableComp: pullable.Comp);
if (TryGrab((pullable, pullable.Comp), pullerUid))
return true;
if (!_combatMode.IsInCombatMode(pullable))
return TryStopPull(pullable, pullable.Comp, ignoreGrab: true);
return false;
}
// Goobstation
public bool TogglePull(EntityUid pullerUid, PullerComponent puller)
{
if (!TryComp<PullableComponent>(puller.Pulling, out var pullable))
return false;
return TogglePull((puller.Pulling.Value, pullable), pullerUid);
}
public bool TryStartPull(EntityUid pullerUid, EntityUid pullableUid,
PullerComponent? pullerComp = null, PullableComponent? pullableComp = null)
{
if (!Resolve(pullerUid, ref pullerComp, false) ||
!Resolve(pullableUid, ref pullableComp, false))
{
return false;
}
if (pullerComp.Pulling == pullableUid)
return true;
if (!CanPull(pullerUid, pullableUid))
return false;
if (!TryComp(pullerUid, out PhysicsComponent? pullerPhysics) || !TryComp(pullableUid, out PhysicsComponent? pullablePhysics))
return false;
// Ensure that the puller is not currently pulling anything.
if (TryComp<PullableComponent>(pullerComp.Pulling, out var oldPullable)
&& !TryStopPull(pullerComp.Pulling.Value, oldPullable, pullerUid, true)) // Goobstation
return false;
// Stop anyone else pulling the entity we want to pull
if (pullableComp.Puller != null)
{
// We're already pulling this item
if (pullableComp.Puller == pullerUid)
return false;
// Goobstation - Grab Intent
if (!TryStopPull(pullableUid, pullableComp, pullableComp.Puller))
{
// Not succeed to retake grabbed entity
if (_netManager.IsServer)
{
_popup.PopupEntity(Loc.GetString("popup-grab-retake-fail",
("puller", Identity.Entity(pullableComp.Puller.Value, EntityManager)),
("pulled", Identity.Entity(pullableUid, EntityManager))),
pullerUid, pullerUid, PopupType.MediumCaution);
_popup.PopupEntity(Loc.GetString("popup-grab-retake-fail-puller",
("puller", Identity.Entity(pullerUid, EntityManager)),
("pulled", Identity.Entity(pullableUid, EntityManager))),
pullableComp.Puller.Value, pullableComp.Puller.Value, PopupType.MediumCaution);
}
return false;
}
else if (pullableComp.GrabStage != GrabStage.No)
{
// Successful retake
if (_netManager.IsServer)
{
_popup.PopupEntity(Loc.GetString("popup-grab-retake-success",
("puller", Identity.Entity(pullableComp.Puller.Value, EntityManager)),
("pulled", Identity.Entity(pullableUid, EntityManager))),
pullerUid, pullerUid, PopupType.MediumCaution);
_popup.PopupEntity(Loc.GetString("popup-grab-retake-success-puller",
("puller", Identity.Entity(pullerUid, EntityManager)),
("pulled", Identity.Entity(pullableUid, EntityManager))),
pullableComp.Puller.Value, pullableComp.Puller.Value, PopupType.MediumCaution);
}
}
// Goobstation
}
var pullAttempt = new PullAttemptEvent(pullerUid, pullableUid);
RaiseLocalEvent(pullerUid, pullAttempt);
if (pullAttempt.Cancelled)
return false;
RaiseLocalEvent(pullableUid, pullAttempt);
if (pullAttempt.Cancelled)
return false;
// Pulling confirmed
_interaction.DoContactInteraction(pullableUid, pullerUid);
// Use net entity so it's consistent across client and server.
pullableComp.PullJointId = $"pull-joint-{GetNetEntity(pullableUid)}";
EnsureComp<ActivePullerComponent>(pullerUid);
pullerComp.Pulling = pullableUid;
pullableComp.Puller = pullerUid;
// store the pulled entity's physics FixedRotation setting in case we change it
pullableComp.PrevFixedRotation = pullablePhysics.FixedRotation;
// joint state handling will manage its own state
if (!_timing.ApplyingState)
{
var joint = _joints.CreateDistanceJoint(pullableUid, pullerUid,
pullablePhysics.LocalCenter, pullerPhysics.LocalCenter,
id: pullableComp.PullJointId);
joint.CollideConnected = false;
// This maximum has to be there because if the object is constrained too closely, the clamping goes backwards and asserts.
// Internally, the joint length has been set to the distance between the pivots.
// Add an additional 15cm (pretty arbitrary) to the maximum length for the hard limit.
joint.MaxLength = joint.Length + 0.15f;
joint.MinLength = 0f;
// Set the spring stiffness to zero. The joint won't have any effect provided
// the current length is beteen MinLength and MaxLength. At those limits, the
// joint will have infinite stiffness.
joint.Stiffness = 0f;
_physics.SetFixedRotation(pullableUid, pullableComp.FixedRotationOnPull, body: pullablePhysics);
}
// Messaging
var message = new PullStartedMessage(pullerUid, pullableUid);
_modifierSystem.RefreshMovementSpeedModifiers(pullerUid);
_alertsSystem.ShowAlert(pullerUid, pullerComp.PullingAlert, 0); // Goobstation
_alertsSystem.ShowAlert(pullableUid, pullableComp.PulledAlert, 0); // Goobstation
RaiseLocalEvent(pullerUid, message);
RaiseLocalEvent(pullableUid, message);
Dirty(pullerUid, pullerComp);
Dirty(pullableUid, pullableComp);
var pullingMessage =
Loc.GetString("getting-pulled-popup", ("puller", Identity.Entity(pullerUid, EntityManager)));
_popup.PopupEntity(pullingMessage, pullableUid, pullableUid);
_adminLogger.Add(LogType.Action, LogImpact.Low,
$"{ToPrettyString(pullerUid):user} started pulling {ToPrettyString(pullableUid):target}");
if (_combatMode.IsInCombatMode(pullerUid)) // Goobstation
TryGrab(pullableUid, pullerUid); // Goobstation
return true;
}
public bool TryStopPull(EntityUid pullableUid, PullableComponent pullable, EntityUid? user = null, bool ignoreGrab = false)
{
var pullerUidNull = pullable.Puller;
if (pullerUidNull == null)
return true;
if (user != null && !_blocker.CanInteract(user.Value, pullableUid))
return false;
var msg = new AttemptStopPullingEvent(user);
RaiseLocalEvent(pullableUid, msg, true); // Goob edit
if (msg.Cancelled)
return false;
// Goobstation - Grab Intent
if (!ignoreGrab)
{
if (_netManager.IsServer && user != null && user.Value == pullableUid)
{
var releaseAttempt = AttemptGrabRelease(pullableUid);
if (!releaseAttempt)
{
_popup.PopupEntity(Loc.GetString("popup-grab-release-fail-self"),
pullableUid,
pullableUid,
PopupType.SmallCaution);
return false;
}
_popup.PopupEntity(Loc.GetString("popup-grab-release-success-self"),
pullableUid,
pullableUid,
PopupType.SmallCaution);
_popup.PopupEntity(
Loc.GetString("popup-grab-release-success-puller",
("target", Identity.Entity(pullableUid, EntityManager))),
pullerUidNull.Value,
pullerUidNull.Value,
PopupType.MediumCaution);
}
}
// Goobstation
StopPulling(pullableUid, pullable);
return true;
}
public void StopAllPulls(EntityUid uid) // Goobstation
{
if (TryComp<PullableComponent>(uid, out var pullable) && IsPulled(uid, pullable))
TryStopPull(uid, pullable);
if (TryComp<PullerComponent>(uid, out var puller) &&
TryComp(puller.Pulling, out PullableComponent? pullableEnt))
TryStopPull(puller.Pulling.Value, pullableEnt);
}
/// <summary>
/// Trying to grab the target
/// </summary>
/// <param name="pullable">Target that would be grabbed</param>
/// <param name="puller">Performer of the grab</param>
/// <param name="ignoreCombatMode">If true, will ignore disabled combat mode</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <returns></returns>
public bool TryGrab(Entity<PullableComponent?> pullable, Entity<PullerComponent?> puller, bool ignoreCombatMode = false)
{
if (!Resolve(pullable.Owner, ref pullable.Comp))
return false;
if (!Resolve(puller.Owner, ref puller.Comp))
return false;
// prevent you from grabbing someone else while being grabbed
if (TryComp<PullableComponent>(puller, out var pullerAsPullable) && pullerAsPullable.Puller != null)
return false;
if (HasComp<PacifiedComponent>(puller))
return false;
if (pullable.Comp.Puller != puller ||
puller.Comp.Pulling != pullable)
return false;
if (puller.Comp.NextStageChange > _timing.CurTime)
return true;
// You can't choke crates
if (!HasComp<MobStateComponent>(pullable))
return false;
// Delay to avoid spamming
puller.Comp.NextStageChange = _timing.CurTime + puller.Comp.StageChangeCooldown;
Dirty(puller);
// Don't grab without grab intent
if (!ignoreCombatMode)
if (!_combatMode.IsInCombatMode(puller))
return false;
if (!HasComp<CanChokeGrabComponent>(puller))
return false;
// It's blocking stage update, maybe better UX?
if (puller.Comp.GrabStage == GrabStage.Suffocate)
{
_stamina.TakeStaminaDamage(pullable, puller.Comp.SuffocateGrabStaminaDamage);
Dirty(pullable);
Dirty(puller);
return true;
}
// Update stage
// TODO: Change grab stage direction
var nextStageAddition = puller.Comp.GrabStageDirection switch
{
GrabStageDirection.Increase => 1,
GrabStageDirection.Decrease => -1,
_ => throw new ArgumentOutOfRangeException(),
};
var newStage = puller.Comp.GrabStage + nextStageAddition;
if (!TrySetGrabStages((puller, puller.Comp), (pullable, pullable.Comp), newStage))
return false;
_color.RaiseEffect(Color.Yellow, new List<EntityUid> { pullable }, Filter.Pvs(pullable, entityManager: EntityManager));
return true;
}
private bool TrySetGrabStages(Entity<PullerComponent> puller, Entity<PullableComponent> pullable, GrabStage stage)
{
puller.Comp.GrabStage = stage;
pullable.Comp.GrabStage = stage;
if (!TryUpdateGrabVirtualItems(puller, pullable))
return false;
var filter = Filter.Empty()
.AddPlayersByPvs(Transform(puller).Coordinates)
.RemovePlayerByAttachedEntity(puller.Owner)
.RemovePlayerByAttachedEntity(pullable.Owner);
var popupType = stage switch
{
GrabStage.No => PopupType.Small,
GrabStage.Soft => PopupType.Small,
GrabStage.Hard => PopupType.MediumCaution,
GrabStage.Suffocate => PopupType.LargeCaution,
_ => throw new ArgumentOutOfRangeException()
};
var massModifier = _contests.MassContest(puller, pullable);
pullable.Comp.GrabEscapeChance = Math.Clamp(puller.Comp.EscapeChances[stage] / massModifier, 0f, 1f);
_alertsSystem.ShowAlert(puller, puller.Comp.PullingAlert, puller.Comp.PullingAlertSeverity[stage]);
_alertsSystem.ShowAlert(pullable, pullable.Comp.PulledAlert, pullable.Comp.PulledAlertAlertSeverity[stage]);
_blocker.UpdateCanMove(pullable);
_modifierSystem.RefreshMovementSpeedModifiers(puller);
// I'm lazy to write client code
if (!_netManager.IsServer)
return true;
_popup.PopupEntity(Loc.GetString($"popup-grab-{puller.Comp.GrabStage.ToString().ToLower()}-target", ("puller", Identity.Entity(puller, EntityManager))), pullable, pullable, popupType);
_popup.PopupEntity(Loc.GetString($"popup-grab-{puller.Comp.GrabStage.ToString().ToLower()}-self", ("target", Identity.Entity(pullable, EntityManager))), pullable, puller, PopupType.Medium);
_popup.PopupEntity(Loc.GetString($"popup-grab-{puller.Comp.GrabStage.ToString().ToLower()}-others", ("target", Identity.Entity(pullable, EntityManager)), ("puller", Identity.Entity(puller, EntityManager))), pullable, filter, true, popupType);
_audio.PlayPvs(new SoundPathSpecifier("/Audio/Effects/thudswoosh.ogg"), pullable);
Dirty(pullable);
Dirty(puller);
return true;
}
private bool TryUpdateGrabVirtualItems(Entity<PullerComponent> puller, Entity<PullableComponent> pullable)
{
// Updating virtual items
var virtualItemsCount = puller.Comp.GrabVirtualItems.Count;
var newVirtualItemsCount = puller.Comp.NeedsHands ? 0 : 1;
if (puller.Comp.GrabVirtualItemStageCount.TryGetValue(puller.Comp.GrabStage, out var count))
newVirtualItemsCount += count;
if (virtualItemsCount == newVirtualItemsCount)
return true;
var delta = newVirtualItemsCount - virtualItemsCount;
// Adding new virtual items
if (delta > 0)
{
for (var i = 0; i < delta; i++)
{
var emptyHand = _handsSystem.TryGetEmptyHand(puller, out _);
if (!emptyHand)
{
if (_netManager.IsServer)
_popup.PopupEntity(Loc.GetString("popup-grab-need-hand"), puller, puller, PopupType.Medium);
return false;
}
if (!_virtualSystem.TrySpawnVirtualItemInHand(pullable, puller.Owner, out var item, true))
{
// I'm lazy write client code
if (_netManager.IsServer)
_popup.PopupEntity(Loc.GetString("popup-grab-need-hand"), puller, puller, PopupType.Medium);
return false;
}
puller.Comp.GrabVirtualItems.Add(item.Value);
}
}
if (delta >= 0)
return true;
for (var i = 0; i < Math.Abs(delta); i++)
{
if (i >= puller.Comp.GrabVirtualItems.Count)
break;
var item = puller.Comp.GrabVirtualItems[i];
puller.Comp.GrabVirtualItems.Remove(item);
if(TryComp<VirtualItemComponent>(item, out var virtualItemComponent))
_virtualSystem.DeleteVirtualItem((item,virtualItemComponent), puller);
}
return true;
}
/// <summary>
/// Attempts to release entity from grab
/// </summary>
/// <param name="pullable">Grabbed entity</param>
/// <returns></returns>
private bool AttemptGrabRelease(Entity<PullableComponent?> pullable)
{
if (!Resolve(pullable.Owner, ref pullable.Comp))
return false;
if (_timing.CurTime < pullable.Comp.NextEscapeAttempt) // No autoclickers! Mwa-ha-ha
return false;
if (_random.Prob(pullable.Comp.GrabEscapeChance))
return true;
pullable.Comp.NextEscapeAttempt = _timing.CurTime.Add(TimeSpan.FromSeconds(3));
Dirty(pullable.Owner, pullable.Comp);
return false;
}
private void OnGrabbedMoveAttempt(EntityUid uid, PullableComponent component, UpdateCanMoveEvent args)
{
if (component.GrabStage == GrabStage.No)
return;
args.Cancel();
}
private void OnGrabbedSpeakAttempt(EntityUid uid, PullableComponent component, SpeakAttemptEvent args)
{
if (component.GrabStage != GrabStage.Suffocate)
return;
_popup.PopupEntity(Loc.GetString("popup-grabbed-cant-speak"), uid, uid, PopupType.MediumCaution); // You cant speak while someone is choking you
args.Cancel();
}
/// <summary>
/// Tries to lower grab stage for target or release it