Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leash Tweaks & Fixes #193

Merged
merged 6 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions Content.Shared/Floofstation/Leash/Components/LeashComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,6 @@ public sealed partial class LeashComponent : Component
[DataField, AutoNetworkedField]
public TimeSpan PullInterval = TimeSpan.FromSeconds(1.5f);

/// <summary>
/// How much damage each leash joint can sustain before it breaks.
/// </summary>
/// <remarks>Not currently implemented; needs to be reworked in order to work.</remarks>
[DataField, AutoNetworkedField]
public float BreakDamage = 20f;

/// <summary>
/// How much damage each leash joint loses every <see cref="DamageInterval"/>.
/// </summary>
/// <remarks>Not currently implemented; needs to be reworked in order to work.</remarks>
[DataField, AutoNetworkedField]
public float JointRepairDamage = 1f;

/// <summary>
/// Interval at which damage is calculated for each joint.
/// </summary>
/// <remarks>Not currently implemented; needs to be reworked in order to work.</remarks>
[DataField, AutoNetworkedField]
public TimeSpan DamageInterval = TimeSpan.FromMilliseconds(200);

/// <summary>
/// List of all joints and their respective pulled entities created by this leash.
/// </summary>
Expand All @@ -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;
Expand Down
88 changes: 36 additions & 52 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 @@ -73,30 +72,24 @@ 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<JointComponent>(target, out var jointComp)
&& jointComp.GetJoints.TryGetValue(data.JointId, out var joint)
&& joint is DistanceJoint distanceJoint
)
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
|| !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<JointComponent>(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);
// }
}
}

Expand All @@ -115,33 +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 (!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 @@ -193,6 +159,26 @@ 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.
RemoveLeash(ent!, (puller, leash), false);
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 Expand Up @@ -279,6 +265,7 @@ private DistanceJoint CreateLeashJoint(string jointId, Entity<LeashComponent> 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;
}
Expand Down Expand Up @@ -380,10 +367,7 @@ public void DoLeash(Entity<LeashAnchorComponent> anchor, Entity<LeashComponent>

// 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)
{
Expand Down
1 change: 1 addition & 0 deletions Resources/Prototypes/Entities/Mobs/NPCs/silicon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
- GalacticCommon
- RobotTalk
- type: PsionicInsulation
- type: LeashAnchor # Floofstation

- type: entity
parent: MobSiliconBase
Expand Down
12 changes: 8 additions & 4 deletions Resources/Prototypes/Floof/Entities/Objects/Tools/leash.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
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
sprite: Floof/Objects/Tools/leash.rsi
layers:
- state: icon
- type: Leash
pullInterval: 0.75
leashSprite:
sprite: Floof/Objects/Tools/leash-rope.rsi
state: rope
Expand All @@ -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
Expand All @@ -31,6 +33,7 @@
components:
- type: Leash
length: 1.5
maxDistance: 3
attachDelay: 4.5
detachDelay: 3
selfDetachDelay: 10
Expand All @@ -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
Expand Down
Loading