Skip to content

Commit

Permalink
Recreate the leash join if it was destroyed by RT
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnemotechnician committed Sep 17, 2024
1 parent c41a769 commit fd1b913
Showing 1 changed file with 20 additions and 30 deletions.
50 changes: 20 additions & 30 deletions Content.Shared/Floofstation/Leash/LeashSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ public override void Initialize()
UpdatesBefore.Add(typeof(SharedPhysicsSystem));

SubscribeLocalEvent<LeashAnchorComponent, BeingUnequippedAttemptEvent>(OnAnchorUnequipping);
SubscribeLocalEvent<LeashedComponent, ContainerGettingInsertedAttemptEvent>(OnLeashedInserting);
SubscribeLocalEvent<LeashComponent, JointRemovedEvent>(OnJointRemoved);
SubscribeLocalEvent<LeashAnchorComponent, GetVerbsEvent<EquipmentVerb>>(OnGetEquipmentVerbs);
SubscribeLocalEvent<LeashedComponent, JointRemovedEvent>(OnJointRemoved, after: [typeof(SharedJointSystem)]);
SubscribeLocalEvent<LeashedComponent, GetVerbsEvent<InteractionVerb>>(OnGetLeashedVerbs);

SubscribeLocalEvent<LeashAnchorComponent, LeashAttachDoAfterEvent>(OnAttachDoAfter);
Expand Down Expand Up @@ -109,34 +108,6 @@ private void OnAnchorUnequipping(Entity<LeashAnchorComponent> ent, ref BeingUneq
args.Cancel();
}

private void OnLeashedInserting(Entity<LeashedComponent> ent, ref ContainerGettingInsertedAttemptEvent args)
{
// Prevent the entity from entering crates and the like because that would instantly break all joints on it, including the leash
if (_timing.ApplyingState
|| !Exists(ent.Comp.Puller)
|| !Exists(ent.Comp.Anchor)
|| !TryComp<LeashComponent>(ent.Comp.Puller, out var leashPuller)
|| !TryComp<LeashAnchorComponent>(ent.Comp.Anchor, out var leashAnchor))
return;

args.Cancel();
// This is hella unsafe to do, but we recreate the joint because dumb storage system removes it before raising the event.
// We have to pray that OnJointRemoved already was called and that it deferred the removal of everything that used to exist
// I HATE STORAGE
DoLeash((ent.Comp.Anchor.Value, leashAnchor), (ent.Comp.Puller.Value, leashPuller), ent);
}

private void OnJointRemoved(Entity<LeashComponent> ent, ref JointRemovedEvent args)
{
var id = args.Joint.ID;
if (!ent.Comp.Leashed.TryFirstOrDefault(it => it.JointId == id, out var data)
|| !TryGetEntity(data.Pulled, out var leashedEnt)
|| !TryComp<LeashedComponent>(leashedEnt, out var leashed))
return;

RemoveLeash((leashedEnt.Value, leashed), ent!, false);
}

private void OnGetEquipmentVerbs(Entity<LeashAnchorComponent> ent, ref GetVerbsEvent<EquipmentVerb> args)
{
if (!args.CanAccess
Expand Down Expand Up @@ -188,6 +159,25 @@ private void OnGetLeashedVerbs(Entity<LeashedComponent> ent, ref GetVerbsEvent<I
});
}

private void OnJointRemoved(Entity<LeashedComponent> ent, ref JointRemovedEvent args)
{
var id = args.Joint.ID;
if (_timing.ApplyingState
|| ent.Comp.LifeStage >= ComponentLifeStage.Removing
|| ent.Comp.Puller is not { } puller
|| !TryComp<LeashAnchorComponent>(ent.Comp.Anchor, out var anchor)
|| !TryComp<LeashComponent>(puller, out var leash)
|| !Transform(ent).Coordinates.TryDistance(EntityManager, Transform(puller).Coordinates, out var dst)
|| dst > leash.MaxDistance
)
return;

// If the entity still has a leashed comp, and is on the same map, and is within the max distance of the leash
// Then the leash was likely broken due to some weird unforeseen fucking robust toolbox magic. We can try to recreate it.
// This is hella unsafe to do. It will crash in debug builds under certain conditions. Luckily, release builds are safe.
DoLeash((ent.Comp.Anchor.Value, anchor), (puller, leash), ent);
}

private void OnAttachDoAfter(Entity<LeashAnchorComponent> ent, ref LeashAttachDoAfterEvent args)
{
if (args.Cancelled || args.Handled
Expand Down

0 comments on commit fd1b913

Please sign in to comment.