From 0535f948487b8dcc2e31e9ed5ded34fa1769552f Mon Sep 17 00:00:00 2001 From: fox Date: Tue, 17 Sep 2024 18:46:58 +0300 Subject: [PATCH 1/6] Disable client-side leash joint prediction --- Content.Shared/Floofstation/Leash/LeashSystem.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Content.Shared/Floofstation/Leash/LeashSystem.cs b/Content.Shared/Floofstation/Leash/LeashSystem.cs index fcd77a5994a..b9548efd43d 100644 --- a/Content.Shared/Floofstation/Leash/LeashSystem.cs +++ b/Content.Shared/Floofstation/Leash/LeashSystem.cs @@ -73,6 +73,14 @@ public override void Update(float frameTime) if (data.Pulled == NetEntity.Invalid || !TryGetEntity(data.Pulled, out var target)) continue; + // Client side only: set max distance to infinity to prevent the client from ever predicting leashes. + if (_net.IsClient + && TryComp(target, out var jointComp) + && jointComp.GetJoints.TryGetValue(data.JointId, out var joint) + && joint is DistanceJoint distanceJoint + ) + distanceJoint.MaxLength = float.MaxValue; + // Break each leash joint whose entities are on different maps or are too far apart var targetXForm = Transform(target.Value); if (targetXForm.MapUid != sourceXForm.MapUid From 963923d2cbf66dbd6abbd1511ab3e52ed7ad55d1 Mon Sep 17 00:00:00 2001 From: fox Date: Tue, 17 Sep 2024 18:49:00 +0300 Subject: [PATCH 2/6] Remove obsolete code --- .../Leash/Components/LeashComponent.cs | 27 ------------------- .../Floofstation/Leash/LeashSystem.cs | 22 +++------------ 2 files changed, 3 insertions(+), 46 deletions(-) diff --git a/Content.Shared/Floofstation/Leash/Components/LeashComponent.cs b/Content.Shared/Floofstation/Leash/Components/LeashComponent.cs index 20ba744c0a4..7d17ecb8e81 100644 --- a/Content.Shared/Floofstation/Leash/Components/LeashComponent.cs +++ b/Content.Shared/Floofstation/Leash/Components/LeashComponent.cs @@ -46,27 +46,6 @@ public sealed partial class LeashComponent : Component [DataField, AutoNetworkedField] public TimeSpan PullInterval = TimeSpan.FromSeconds(1.5f); - /// - /// How much damage each leash joint can sustain before it breaks. - /// - /// Not currently implemented; needs to be reworked in order to work. - [DataField, AutoNetworkedField] - public float BreakDamage = 20f; - - /// - /// How much damage each leash joint loses every . - /// - /// Not currently implemented; needs to be reworked in order to work. - [DataField, AutoNetworkedField] - public float JointRepairDamage = 1f; - - /// - /// Interval at which damage is calculated for each joint. - /// - /// Not currently implemented; needs to be reworked in order to work. - [DataField, AutoNetworkedField] - public TimeSpan DamageInterval = TimeSpan.FromMilliseconds(200); - /// /// List of all joints and their respective pulled entities created by this leash. /// @@ -88,12 +67,6 @@ public sealed partial class LeashData [DataField] public NetEntity? LeashVisuals = null; - [DataField] - public float Damage = 0f; - - [DataField] - public TimeSpan NextDamage = TimeSpan.Zero; - public LeashData(string jointId, NetEntity pulled) { JointId = jointId; diff --git a/Content.Shared/Floofstation/Leash/LeashSystem.cs b/Content.Shared/Floofstation/Leash/LeashSystem.cs index b9548efd43d..a5f04c4c10c 100644 --- a/Content.Shared/Floofstation/Leash/LeashSystem.cs +++ b/Content.Shared/Floofstation/Leash/LeashSystem.cs @@ -85,26 +85,9 @@ public override void Update(float frameTime) var targetXForm = Transform(target.Value); if (targetXForm.MapUid != sourceXForm.MapUid || !sourceXForm.Coordinates.TryDistance(EntityManager, targetXForm.Coordinates, out var dst) - || dst > leash.MaxDistance) + || dst > leash.MaxDistance + ) RemoveLeash(target.Value, (leashEnt, leash)); - - // Calculate joint damage - if (_timing.CurTime < data.NextDamage - || !TryComp(target, out var jointComp) - || !jointComp.GetJoints.TryGetValue(data.JointId, out var joint)) - continue; - - // TODO reaction force always returns 0 and thus damage doesn't work - // TODO find another way to calculate how much force is being excerted to hold the two entities together - // var damage = joint.GetReactionForce(1 / (float) leash.DamageInterval.TotalSeconds).Length() - leash.JointRepairDamage; - // data.Damage = Math.Max(0f, data.Damage + damage); - // data.NextDamage = _timing.CurTime + leash.DamageInterval; - // - // if (damage >= leash.BreakDamage && !_net.IsClient) - // { - // _popups.PopupPredicted(Loc.GetString("leash-snap-popup", ("leash", leashEnt)), target, null, PopupType.SmallCaution); - // RemoveLeash(target, (leashEnt, leash), true); - // } } } @@ -287,6 +270,7 @@ private DistanceJoint CreateLeashJoint(string jointId, Entity le joint.MaxLength = leash.Comp.Length; joint.Stiffness = 1f; joint.CollideConnected = true; // This is just for performance reasons and doesn't actually make mobs collide. + joint.Damping = 1f; return joint; } From dcb883a5165600e629f7fe0126ed5d327b8d8150 Mon Sep 17 00:00:00 2001 From: fox Date: Tue, 17 Sep 2024 19:40:29 +0300 Subject: [PATCH 3/6] Minor fixes --- Content.Shared/Floofstation/Leash/LeashSystem.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Content.Shared/Floofstation/Leash/LeashSystem.cs b/Content.Shared/Floofstation/Leash/LeashSystem.cs index a5f04c4c10c..3f87649753d 100644 --- a/Content.Shared/Floofstation/Leash/LeashSystem.cs +++ b/Content.Shared/Floofstation/Leash/LeashSystem.cs @@ -81,6 +81,9 @@ public override void Update(float frameTime) ) distanceJoint.MaxLength = float.MaxValue; + if (_net.IsClient) + continue; + // Break each leash joint whose entities are on different maps or are too far apart var targetXForm = Transform(target.Value); if (targetXForm.MapUid != sourceXForm.MapUid @@ -109,7 +112,8 @@ private void OnAnchorUnequipping(Entity ent, ref BeingUneq private void OnLeashedInserting(Entity 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 (!Exists(ent.Comp.Puller) + if (_timing.ApplyingState + || !Exists(ent.Comp.Puller) || !Exists(ent.Comp.Anchor) || !TryComp(ent.Comp.Puller, out var leashPuller) || !TryComp(ent.Comp.Anchor, out var leashAnchor)) @@ -372,10 +376,7 @@ public void DoLeash(Entity anchor, Entity // I'd like to use a chain joint or smth, but it's too hard and oftentimes buggy - lamia is a good bad example of that. var joint = CreateLeashJoint(leashedComp.JointId, leash, leashTarget); - var data = new LeashComponent.LeashData(leashedComp.JointId, netLeashTarget) - { - NextDamage = _timing.CurTime + leash.Comp.DamageInterval - }; + var data = new LeashComponent.LeashData(leashedComp.JointId, netLeashTarget); if (leash.Comp.LeashSprite is { } sprite) { From c41a769ce15166f23e5abad09a3a80440a898fe6 Mon Sep 17 00:00:00 2001 From: fox Date: Tue, 17 Sep 2024 19:48:51 +0300 Subject: [PATCH 4/6] Tweak leash properties & add leash anchors to jani/medi/other bots --- Resources/Prototypes/Entities/Mobs/NPCs/silicon.yml | 1 + .../Floof/Entities/Objects/Tools/leash.yml | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Resources/Prototypes/Entities/Mobs/NPCs/silicon.yml b/Resources/Prototypes/Entities/Mobs/NPCs/silicon.yml index d01fc8b8de2..d91fc08e717 100644 --- a/Resources/Prototypes/Entities/Mobs/NPCs/silicon.yml +++ b/Resources/Prototypes/Entities/Mobs/NPCs/silicon.yml @@ -114,6 +114,7 @@ - GalacticCommon - RobotTalk - type: PsionicInsulation + - type: LeashAnchor # Floofstation - type: entity parent: MobSiliconBase diff --git a/Resources/Prototypes/Floof/Entities/Objects/Tools/leash.yml b/Resources/Prototypes/Floof/Entities/Objects/Tools/leash.yml index 45d080b601d..c674d92913b 100644 --- a/Resources/Prototypes/Floof/Entities/Objects/Tools/leash.yml +++ b/Resources/Prototypes/Floof/Entities/Objects/Tools/leash.yml @@ -2,7 +2,7 @@ id: BaseLeash parent: BaseItem name: leash - description: Helps keep your animals close to you, as well as your friends. Attach to supported object or clothing (such as collars) to use. You can pull attached entities while holding the leash. + description: Helps keep your animals or friends close to you. Attach to supported objects or clothing (such as collars) to use. You can pull attached entities while holding the leash. noSpawn: true components: - type: Sprite @@ -10,6 +10,7 @@ layers: - state: icon - type: Leash + pullInterval: 0.75 leashSprite: sprite: Floof/Objects/Tools/leash-rope.rsi state: rope @@ -19,9 +20,10 @@ parent: BaseLeash components: - type: Leash - length: 3.5 - attachDelay: 4.5 # Gotta be at least as high as cuffs or antags may abuse it - detachDelay: 3 + length: 3 + maxDistance: 6 + attachDelay: 4 + detachDelay: 4 selfDetachDelay: 10 - type: entity @@ -31,6 +33,7 @@ components: - type: Leash length: 1.5 + maxDistance: 3 attachDelay: 4.5 detachDelay: 3 selfDetachDelay: 10 @@ -54,6 +57,7 @@ suffix: DEBUG, DO NOT MAP components: - type: Leash + maxDistance: 100 maxJoints: 25 attachDelay: 0 detachDelay: 10000 # will still be instant for admin ghosts or whatever with instant doafters tag From fd1b913f6c11da2dbc7b90820ce5c5711391cc24 Mon Sep 17 00:00:00 2001 From: fox Date: Tue, 17 Sep 2024 20:53:22 +0300 Subject: [PATCH 5/6] Recreate the leash join if it was destroyed by RT --- .../Floofstation/Leash/LeashSystem.cs | 50 ++++++++----------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/Content.Shared/Floofstation/Leash/LeashSystem.cs b/Content.Shared/Floofstation/Leash/LeashSystem.cs index 3f87649753d..9aa13f21500 100644 --- a/Content.Shared/Floofstation/Leash/LeashSystem.cs +++ b/Content.Shared/Floofstation/Leash/LeashSystem.cs @@ -41,9 +41,8 @@ public override void Initialize() UpdatesBefore.Add(typeof(SharedPhysicsSystem)); SubscribeLocalEvent(OnAnchorUnequipping); - SubscribeLocalEvent(OnLeashedInserting); - SubscribeLocalEvent(OnJointRemoved); SubscribeLocalEvent>(OnGetEquipmentVerbs); + SubscribeLocalEvent(OnJointRemoved, after: [typeof(SharedJointSystem)]); SubscribeLocalEvent>(OnGetLeashedVerbs); SubscribeLocalEvent(OnAttachDoAfter); @@ -109,34 +108,6 @@ private void OnAnchorUnequipping(Entity ent, ref BeingUneq args.Cancel(); } - private void OnLeashedInserting(Entity 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(ent.Comp.Puller, out var leashPuller) - || !TryComp(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 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(leashedEnt, out var leashed)) - return; - - RemoveLeash((leashedEnt.Value, leashed), ent!, false); - } - private void OnGetEquipmentVerbs(Entity ent, ref GetVerbsEvent args) { if (!args.CanAccess @@ -188,6 +159,25 @@ private void OnGetLeashedVerbs(Entity ent, ref GetVerbsEvent ent, ref JointRemovedEvent args) + { + var id = args.Joint.ID; + if (_timing.ApplyingState + || ent.Comp.LifeStage >= ComponentLifeStage.Removing + || ent.Comp.Puller is not { } puller + || !TryComp(ent.Comp.Anchor, out var anchor) + || !TryComp(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 ent, ref LeashAttachDoAfterEvent args) { if (args.Cancelled || args.Handled From 0776bff12ec98a4bd5a7716f2e3af0cdd17eea32 Mon Sep 17 00:00:00 2001 From: fox Date: Tue, 17 Sep 2024 21:24:47 +0300 Subject: [PATCH 6/6] Do not create duplicate joints --- Content.Shared/Floofstation/Leash/LeashSystem.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Content.Shared/Floofstation/Leash/LeashSystem.cs b/Content.Shared/Floofstation/Leash/LeashSystem.cs index 9aa13f21500..bb5356d6763 100644 --- a/Content.Shared/Floofstation/Leash/LeashSystem.cs +++ b/Content.Shared/Floofstation/Leash/LeashSystem.cs @@ -175,6 +175,7 @@ private void OnJointRemoved(Entity ent, ref JointRemovedEvent // 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. + RemoveLeash(ent!, (puller, leash), false); DoLeash((ent.Comp.Anchor.Value, anchor), (puller, leash), ent); }