| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566 |
- using System.Diagnostics.CodeAnalysis;
- using System.Numerics;
- using Content.Shared.Alert;
- using Content.Shared.Buckle.Components;
- using Content.Shared.Cuffs.Components;
- using Content.Shared.Database;
- using Content.Shared.DoAfter;
- using Content.Shared.Hands.Components;
- using Content.Shared.IdentityManagement;
- using Content.Shared.Movement.Events;
- using Content.Shared.Movement.Pulling.Events;
- using Content.Shared.Popups;
- using Content.Shared.Pulling.Events;
- using Content.Shared.Rotation;
- using Content.Shared.Standing;
- using Content.Shared.Storage.Components;
- using Content.Shared.Stunnable;
- using Content.Shared.Throwing;
- using Content.Shared.Whitelist;
- using Robust.Shared.Containers;
- using Robust.Shared.GameStates;
- using Robust.Shared.Map;
- using Robust.Shared.Physics.Components;
- using Robust.Shared.Physics.Events;
- using Robust.Shared.Prototypes;
- using Robust.Shared.Utility;
- namespace Content.Shared.Buckle;
- public abstract partial class SharedBuckleSystem
- {
- public static ProtoId<AlertCategoryPrototype> BuckledAlertCategory = "Buckled";
- [Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
- private void InitializeBuckle()
- {
- SubscribeLocalEvent<BuckleComponent, ComponentShutdown>(OnBuckleComponentShutdown);
- SubscribeLocalEvent<BuckleComponent, MoveEvent>(OnBuckleMove);
- SubscribeLocalEvent<BuckleComponent, EntParentChangedMessage>(OnParentChanged);
- SubscribeLocalEvent<BuckleComponent, EntGotInsertedIntoContainerMessage>(OnInserted);
- SubscribeLocalEvent<BuckleComponent, StartPullAttemptEvent>(OnPullAttempt);
- SubscribeLocalEvent<BuckleComponent, BeingPulledAttemptEvent>(OnBeingPulledAttempt);
- SubscribeLocalEvent<BuckleComponent, PullStartedMessage>(OnPullStarted);
- SubscribeLocalEvent<BuckleComponent, UnbuckleAlertEvent>(OnUnbuckleAlert);
- SubscribeLocalEvent<BuckleComponent, InsertIntoEntityStorageAttemptEvent>(OnBuckleInsertIntoEntityStorageAttempt);
- SubscribeLocalEvent<BuckleComponent, PreventCollideEvent>(OnBucklePreventCollide);
- SubscribeLocalEvent<BuckleComponent, DownAttemptEvent>(OnBuckleDownAttempt);
- SubscribeLocalEvent<BuckleComponent, StandAttemptEvent>(OnBuckleStandAttempt);
- SubscribeLocalEvent<BuckleComponent, ThrowPushbackAttemptEvent>(OnBuckleThrowPushbackAttempt);
- SubscribeLocalEvent<BuckleComponent, UpdateCanMoveEvent>(OnBuckleUpdateCanMove);
- SubscribeLocalEvent<BuckleComponent, BuckleDoAfterEvent>(OnBuckleDoafter);
- SubscribeLocalEvent<BuckleComponent, DoAfterAttemptEvent<BuckleDoAfterEvent>>((uid, comp, ev) =>
- {
- BuckleDoafterEarly((uid, comp), ev.Event, ev);
- });
- }
- private void OnBuckleComponentShutdown(Entity<BuckleComponent> ent, ref ComponentShutdown args)
- {
- Unbuckle(ent!, null);
- }
- #region Pulling
- private void OnPullAttempt(Entity<BuckleComponent> ent, ref StartPullAttemptEvent args)
- {
- // Prevent people pulling the chair they're on, etc.
- if (ent.Comp.BuckledTo == args.Pulled && !ent.Comp.PullStrap)
- args.Cancel();
- }
- private void OnBeingPulledAttempt(Entity<BuckleComponent> ent, ref BeingPulledAttemptEvent args)
- {
- if (args.Cancelled || !ent.Comp.Buckled)
- return;
- if (!CanUnbuckle(ent!, args.Puller, false))
- args.Cancel();
- }
- private void OnPullStarted(Entity<BuckleComponent> ent, ref PullStartedMessage args)
- {
- Unbuckle(ent!, args.PullerUid);
- }
- private void OnUnbuckleAlert(Entity<BuckleComponent> ent, ref UnbuckleAlertEvent args)
- {
- if (args.Handled)
- return;
- args.Handled = TryUnbuckle(ent, ent, ent);
- }
- #endregion
- #region Transform
- private void OnParentChanged(Entity<BuckleComponent> ent, ref EntParentChangedMessage args)
- {
- BuckleTransformCheck(ent, args.Transform);
- }
- private void OnInserted(Entity<BuckleComponent> ent, ref EntGotInsertedIntoContainerMessage args)
- {
- BuckleTransformCheck(ent, Transform(ent));
- }
- private void OnBuckleMove(Entity<BuckleComponent> ent, ref MoveEvent ev)
- {
- BuckleTransformCheck(ent, ev.Component);
- }
- /// <summary>
- /// Check if the entity should get unbuckled as a result of transform or container changes.
- /// </summary>
- private void BuckleTransformCheck(Entity<BuckleComponent> buckle, TransformComponent xform)
- {
- if (_gameTiming.ApplyingState)
- return;
- if (buckle.Comp.BuckledTo is not { } strapUid)
- return;
- if (!TryComp<StrapComponent>(strapUid, out var strapComp))
- {
- Log.Error($"Encountered buckle entity {ToPrettyString(buckle)} without a valid strap entity {ToPrettyString(strapUid)}");
- SetBuckledTo(buckle, null);
- return;
- }
- if (xform.ParentUid != strapUid || _container.IsEntityInContainer(buckle))
- {
- Unbuckle(buckle, (strapUid, strapComp), null);
- return;
- }
- var delta = (xform.LocalPosition - strapComp.BuckleOffset).LengthSquared();
- if (delta > 1e-5)
- Unbuckle(buckle, (strapUid, strapComp), null);
- }
- #endregion
- private void OnBuckleInsertIntoEntityStorageAttempt(EntityUid uid, BuckleComponent component, ref InsertIntoEntityStorageAttemptEvent args)
- {
- if (component.Buckled)
- args.Cancelled = true;
- }
- private void OnBucklePreventCollide(EntityUid uid, BuckleComponent component, ref PreventCollideEvent args)
- {
- if (args.OtherEntity == component.BuckledTo && component.DontCollide)
- args.Cancelled = true;
- }
- private void OnBuckleDownAttempt(EntityUid uid, BuckleComponent component, DownAttemptEvent args)
- {
- if (component.Buckled)
- args.Cancel();
- }
- private void OnBuckleStandAttempt(EntityUid uid, BuckleComponent component, StandAttemptEvent args)
- {
- if (component.Buckled)
- args.Cancel();
- }
- private void OnBuckleThrowPushbackAttempt(EntityUid uid, BuckleComponent component, ThrowPushbackAttemptEvent args)
- {
- if (component.Buckled)
- args.Cancel();
- }
- private void OnBuckleUpdateCanMove(EntityUid uid, BuckleComponent component, UpdateCanMoveEvent args)
- {
- if (component.Buckled)
- args.Cancel();
- }
- public bool IsBuckled(EntityUid uid, BuckleComponent? component = null)
- {
- return Resolve(uid, ref component, false) && component.Buckled;
- }
- protected void SetBuckledTo(Entity<BuckleComponent> buckle, Entity<StrapComponent?>? strap)
- {
- if (TryComp(buckle.Comp.BuckledTo, out StrapComponent? old))
- {
- old.BuckledEntities.Remove(buckle);
- Dirty(buckle.Comp.BuckledTo.Value, old);
- }
- if (strap is {} strapEnt && Resolve(strapEnt.Owner, ref strapEnt.Comp))
- {
- strapEnt.Comp.BuckledEntities.Add(buckle);
- Dirty(strapEnt);
- _alerts.ShowAlert(buckle, strapEnt.Comp.BuckledAlertType);
- }
- else
- {
- _alerts.ClearAlertCategory(buckle, BuckledAlertCategory);
- }
- buckle.Comp.BuckledTo = strap;
- buckle.Comp.BuckleTime = _gameTiming.CurTime;
- ActionBlocker.UpdateCanMove(buckle);
- Appearance.SetData(buckle, StrapVisuals.State, buckle.Comp.Buckled);
- Dirty(buckle);
- }
- /// <summary>
- /// Checks whether or not buckling is possible
- /// </summary>
- /// <param name="buckleUid"> Uid of the owner of BuckleComponent </param>
- /// <param name="user">
- /// Uid of a third party entity,
- /// i.e, the uid of someone else you are dragging to a chair.
- /// Can equal buckleUid sometimes
- /// </param>
- /// <param name="strapUid"> Uid of the owner of strap component </param>
- /// <param name="strapComp"></param>
- /// <param name="buckleComp"></param>
- private bool CanBuckle(EntityUid buckleUid,
- EntityUid? user,
- EntityUid strapUid,
- bool popup,
- [NotNullWhen(true)] out StrapComponent? strapComp,
- BuckleComponent buckleComp)
- {
- strapComp = null;
- if (!Resolve(strapUid, ref strapComp, false))
- return false;
- // Does it pass the Whitelist
- if (_whitelistSystem.IsWhitelistFail(strapComp.Whitelist, buckleUid) ||
- _whitelistSystem.IsBlacklistPass(strapComp.Blacklist, buckleUid))
- {
- if (popup)
- _popup.PopupClient(Loc.GetString("buckle-component-cannot-fit-message"), user, PopupType.Medium);
- return false;
- }
- if (!_interaction.InRangeUnobstructed(buckleUid,
- strapUid,
- buckleComp.Range,
- predicate: entity => entity == buckleUid || entity == user || entity == strapUid,
- popup: true))
- {
- return false;
- }
- if (!_container.IsInSameOrNoContainer((buckleUid, null, null), (strapUid, null, null)))
- return false;
- if (user != null && !HasComp<HandsComponent>(user))
- {
- if (popup)
- _popup.PopupClient(Loc.GetString("buckle-component-no-hands-message"), user);
- return false;
- }
- if (buckleComp.Buckled && !TryUnbuckle(buckleUid, user, buckleComp))
- {
- if (popup)
- {
- var message = Loc.GetString(buckleUid == user
- ? "buckle-component-already-buckled-message"
- : "buckle-component-other-already-buckled-message",
- ("owner", Identity.Entity(buckleUid, EntityManager)));
- _popup.PopupClient(message, user);
- }
- return false;
- }
- // Check whether someone is attempting to buckle something to their own child
- var parent = Transform(strapUid).ParentUid;
- while (parent.IsValid())
- {
- if (parent != buckleUid)
- {
- parent = Transform(parent).ParentUid;
- continue;
- }
- if (popup)
- {
- var message = Loc.GetString(buckleUid == user
- ? "buckle-component-cannot-buckle-message"
- : "buckle-component-other-cannot-buckle-message",
- ("owner", Identity.Entity(buckleUid, EntityManager)));
- _popup.PopupClient(message, user);
- }
- return false;
- }
- if (!StrapHasSpace(strapUid, buckleComp, strapComp))
- {
- if (popup)
- {
- var message = Loc.GetString(buckleUid == user
- ? "buckle-component-cannot-buckle-message"
- : "buckle-component-other-cannot-buckle-message",
- ("owner", Identity.Entity(buckleUid, EntityManager)));
- _popup.PopupClient(message, user);
- }
- return false;
- }
- var buckleAttempt = new BuckleAttemptEvent((strapUid, strapComp), (buckleUid, buckleComp), user, popup);
- RaiseLocalEvent(buckleUid, ref buckleAttempt);
- if (buckleAttempt.Cancelled)
- return false;
- var strapAttempt = new StrapAttemptEvent((strapUid, strapComp), (buckleUid, buckleComp), user, popup);
- RaiseLocalEvent(strapUid, ref strapAttempt);
- if (strapAttempt.Cancelled)
- return false;
- return true;
- }
- /// <summary>
- /// Attempts to buckle an entity to a strap
- /// </summary>
- /// <param name="buckle"> Uid of the owner of BuckleComponent </param>
- /// <param name="user">
- /// Uid of a third party entity,
- /// i.e, the uid of someone else you are dragging to a chair.
- /// Can equal buckleUid sometimes
- /// </param>
- /// <param name="strap"> Uid of the owner of strap component </param>
- public bool TryBuckle(EntityUid buckle, EntityUid? user, EntityUid strap, BuckleComponent? buckleComp = null, bool popup = true)
- {
- if (!Resolve(buckle, ref buckleComp, false))
- return false;
- if (!CanBuckle(buckle, user, strap, popup, out var strapComp, buckleComp))
- return false;
- Buckle((buckle, buckleComp), (strap, strapComp), user);
- return true;
- }
- private void Buckle(Entity<BuckleComponent> buckle, Entity<StrapComponent> strap, EntityUid? user)
- {
- if (user == buckle.Owner)
- _adminLogger.Add(LogType.Action, LogImpact.Low, $"{ToPrettyString(user):player} buckled themselves to {ToPrettyString(strap)}");
- else if (user != null)
- _adminLogger.Add(LogType.Action, LogImpact.Low, $"{ToPrettyString(user):player} buckled {ToPrettyString(buckle)} to {ToPrettyString(strap)}");
- _audio.PlayPredicted(strap.Comp.BuckleSound, strap, user);
- SetBuckledTo(buckle, strap!);
- Appearance.SetData(strap, StrapVisuals.State, true);
- Appearance.SetData(buckle, BuckleVisuals.Buckled, true);
- _rotationVisuals.SetHorizontalAngle(buckle.Owner, strap.Comp.Rotation);
- var xform = Transform(buckle);
- var coords = new EntityCoordinates(strap, strap.Comp.BuckleOffset);
- _transform.SetCoordinates(buckle, xform, coords, rotation: Angle.Zero);
- _joints.SetRelay(buckle, strap);
- switch (strap.Comp.Position)
- {
- case StrapPosition.Stand:
- _standing.Stand(buckle, force: true);
- break;
- case StrapPosition.Down:
- _standing.Down(buckle, false, false, force: true);
- break;
- }
- var ev = new StrappedEvent(strap, buckle);
- RaiseLocalEvent(strap, ref ev);
- var gotEv = new BuckledEvent(strap, buckle);
- RaiseLocalEvent(buckle, ref gotEv);
- if (TryComp<PhysicsComponent>(buckle, out var physics))
- _physics.ResetDynamics(buckle, physics);
- DebugTools.AssertEqual(xform.ParentUid, strap.Owner);
- }
- /// <summary>
- /// Tries to unbuckle the Owner of this component from its current strap.
- /// </summary>
- /// <param name="buckleUid">The entity to unbuckle.</param>
- /// <param name="user">The entity doing the unbuckling.</param>
- /// <param name="buckleComp">The buckle component of the entity to unbuckle.</param>
- /// <returns>
- /// true if the owner was unbuckled, otherwise false even if the owner
- /// was previously already unbuckled.
- /// </returns>
- public bool TryUnbuckle(EntityUid buckleUid,
- EntityUid? user,
- BuckleComponent? buckleComp = null,
- bool popup = true)
- {
- return TryUnbuckle((buckleUid, buckleComp), user, popup);
- }
- public bool TryUnbuckle(Entity<BuckleComponent?> buckle, EntityUid? user, bool popup)
- {
- if (!Resolve(buckle.Owner, ref buckle.Comp, false))
- return false;
- if (!CanUnbuckle(buckle, user, popup, out var strap))
- return false;
- Unbuckle(buckle!, strap, user);
- return true;
- }
- public void Unbuckle(Entity<BuckleComponent?> buckle, EntityUid? user)
- {
- if (!Resolve(buckle.Owner, ref buckle.Comp, false))
- return;
- if (buckle.Comp.BuckledTo is not { } strap)
- return;
- if (!TryComp(strap, out StrapComponent? strapComp))
- {
- Log.Error($"Encountered buckle {ToPrettyString(buckle.Owner)} with invalid strap entity {ToPrettyString(strap)}");
- SetBuckledTo(buckle!, null);
- return;
- }
- Unbuckle(buckle!, (strap, strapComp), user);
- }
- private void Unbuckle(Entity<BuckleComponent> buckle, Entity<StrapComponent> strap, EntityUid? user)
- {
- if (user == buckle.Owner)
- _adminLogger.Add(LogType.Action, LogImpact.Low, $"{user} unbuckled themselves from {strap}");
- else if (user != null)
- _adminLogger.Add(LogType.Action, LogImpact.Low, $"{user} unbuckled {buckle} from {strap}");
- _audio.PlayPredicted(strap.Comp.UnbuckleSound, strap, user);
- SetBuckledTo(buckle, null);
- var buckleXform = Transform(buckle);
- var oldBuckledXform = Transform(strap);
- if (buckleXform.ParentUid == strap.Owner && !Terminating(buckleXform.ParentUid))
- {
- _transform.PlaceNextTo((buckle, buckleXform), (strap.Owner, oldBuckledXform));
- buckleXform.ActivelyLerping = false;
- var oldBuckledToWorldRot = _transform.GetWorldRotation(strap);
- _transform.SetWorldRotationNoLerp((buckle, buckleXform), oldBuckledToWorldRot);
- // TODO: This is doing 4 moveevents this is why I left the warning in, if you're going to remove it make it only do 1 moveevent.
- if (strap.Comp.BuckleOffset != Vector2.Zero)
- {
- buckleXform.Coordinates = oldBuckledXform.Coordinates.Offset(strap.Comp.BuckleOffset);
- }
- }
- _rotationVisuals.ResetHorizontalAngle(buckle.Owner);
- Appearance.SetData(strap, StrapVisuals.State, strap.Comp.BuckledEntities.Count != 0);
- Appearance.SetData(buckle, BuckleVisuals.Buckled, false);
- if (HasComp<KnockedDownComponent>(buckle) || _mobState.IsIncapacitated(buckle))
- _standing.Down(buckle, playSound: false);
- else
- _standing.Stand(buckle);
- _joints.RefreshRelay(buckle);
- var buckleEv = new UnbuckledEvent(strap, buckle);
- RaiseLocalEvent(buckle, ref buckleEv);
- var strapEv = new UnstrappedEvent(strap, buckle);
- RaiseLocalEvent(strap, ref strapEv);
- }
- public bool CanUnbuckle(Entity<BuckleComponent?> buckle, EntityUid user, bool popup)
- {
- return CanUnbuckle(buckle, user, popup, out _);
- }
- private bool CanUnbuckle(Entity<BuckleComponent?> buckle, EntityUid? user, bool popup, out Entity<StrapComponent> strap)
- {
- strap = default;
- if (!Resolve(buckle.Owner, ref buckle.Comp))
- return false;
- if (buckle.Comp.BuckledTo is not { } strapUid)
- return false;
- if (!TryComp(strapUid, out StrapComponent? strapComp))
- {
- Log.Error($"Encountered buckle {ToPrettyString(buckle.Owner)} with invalid strap entity {ToPrettyString(strap)}");
- SetBuckledTo(buckle!, null);
- return false;
- }
- strap = (strapUid, strapComp);
- if (_gameTiming.CurTime < buckle.Comp.BuckleTime + buckle.Comp.Delay)
- return false;
- if (user != null && !_interaction.InRangeUnobstructed(user.Value, strap.Owner, buckle.Comp.Range, popup: popup))
- return false;
- var unbuckleAttempt = new UnbuckleAttemptEvent(strap, buckle!, user, popup);
- RaiseLocalEvent(buckle, ref unbuckleAttempt);
- if (unbuckleAttempt.Cancelled)
- return false;
- var unstrapAttempt = new UnstrapAttemptEvent(strap, buckle!, user, popup);
- RaiseLocalEvent(strap, ref unstrapAttempt);
- return !unstrapAttempt.Cancelled;
- }
- /// <summary>
- /// Once the do-after is complete, try to buckle target to chair/bed
- /// </summary>
- /// <param name="args.Target"> The person being put in the chair/bed</param>
- /// <param name="args.User"> The person putting a person in a chair/bed</param>
- /// <param name="args.Used"> The chair/bed </param>
- private void OnBuckleDoafter(Entity<BuckleComponent> entity, ref BuckleDoAfterEvent args)
- {
- if (args.Cancelled || args.Handled || args.Target == null || args.Used == null)
- return;
- args.Handled = TryBuckle(args.Target.Value, args.User, args.Used.Value, popup: false);
- }
- /// <summary>
- /// If the target being buckled to a chair/bed goes crit or is cuffed
- /// Cancel the do-after time and try to buckle the target immediately
- /// </summary>
- /// <param name="args.Target"> The person being put in the chair/bed</param>
- /// <param name="args.User"> The person putting a person in a chair/bed</param>
- /// <param name="args.Used"> The chair/bed </param>
- private void BuckleDoafterEarly(Entity<BuckleComponent> entity, BuckleDoAfterEvent args, CancellableEntityEventArgs ev)
- {
- if (args.Target == null || args.Used == null)
- return;
- if (TryComp<CuffableComponent>(args.Target, out var targetCuffableComp) && targetCuffableComp.CuffedHandCount > 0
- || _mobState.IsIncapacitated(args.Target.Value))
- {
- ev.Cancel();
- TryBuckle(args.Target.Value, args.User, args.Used.Value, popup: false);
- }
- }
- }
|