Improve throwing precision (#29726)

* improve throwing precision

* remove debugging logs

* minor fixes

* f

* Update Content.Shared/Throwing/LandAtCursorComponent.cs

---------

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
slarticodefast
2024-07-08 11:03:53 +02:00
committed by GitHub
parent c8a87ceaab
commit 92e2980534
18 changed files with 93 additions and 39 deletions

View File

@@ -59,7 +59,6 @@ public sealed class ThrownItemVisualizerSystem : EntitySystem
if (length <= TimeSpan.Zero) if (length <= TimeSpan.Zero)
return null; return null;
length += TimeSpan.FromSeconds(ThrowingSystem.FlyTime);
var scale = ent.Comp2.Scale; var scale = ent.Comp2.Scale;
var lenFloat = (float) length.TotalSeconds; var lenFloat = (float) length.TotalSeconds;

View File

@@ -207,13 +207,13 @@ namespace Content.Server.Hands.Systems
var length = direction.Length(); var length = direction.Length();
var distance = Math.Clamp(length, minDistance, hands.ThrowRange); var distance = Math.Clamp(length, minDistance, hands.ThrowRange);
direction *= distance/length; direction *= distance / length;
var throwStrength = hands.ThrowForceMultiplier; var throwSpeed = hands.BaseThrowspeed;
// Let other systems change the thrown entity (useful for virtual items) // Let other systems change the thrown entity (useful for virtual items)
// or the throw strength. // or the throw strength.
var ev = new BeforeThrowEvent(throwEnt, direction, throwStrength, player); var ev = new BeforeThrowEvent(throwEnt, direction, throwSpeed, player);
RaiseLocalEvent(player, ref ev); RaiseLocalEvent(player, ref ev);
if (ev.Cancelled) if (ev.Cancelled)
@@ -223,7 +223,7 @@ namespace Content.Server.Hands.Systems
if (IsHolding(player, throwEnt, out _, hands) && !TryDrop(player, throwEnt, handsComp: hands)) if (IsHolding(player, throwEnt, out _, hands) && !TryDrop(player, throwEnt, handsComp: hands))
return false; return false;
_throwingSystem.TryThrow(ev.ItemUid, ev.Direction, ev.ThrowStrength, ev.PlayerUid); _throwingSystem.TryThrow(ev.ItemUid, ev.Direction, ev.ThrowSpeed, ev.PlayerUid, compensateFriction: !HasComp<LandAtCursorComponent>(ev.ItemUid));
return true; return true;
} }

View File

@@ -43,7 +43,7 @@ public sealed class LubedSystem : EntitySystem
var user = args.Container.Owner; var user = args.Container.Owner;
_transform.SetCoordinates(uid, Transform(user).Coordinates); _transform.SetCoordinates(uid, Transform(user).Coordinates);
_transform.AttachToGridOrMap(uid); _transform.AttachToGridOrMap(uid);
_throwing.TryThrow(uid, _random.NextVector2(), strength: component.SlipStrength); _throwing.TryThrow(uid, _random.NextVector2(), baseThrowSpeed: component.SlipStrength);
_popup.PopupEntity(Loc.GetString("lube-slip", ("target", Identity.Entity(uid, EntityManager))), user, user, PopupType.MediumCaution); _popup.PopupEntity(Loc.GetString("lube-slip", ("target", Identity.Entity(uid, EntityManager))), user, user, PopupType.MediumCaution);
} }

View File

@@ -37,7 +37,7 @@ public sealed class ContainmentFieldSystem : EntitySystem
var fieldDir = Transform(uid).WorldPosition; var fieldDir = Transform(uid).WorldPosition;
var playerDir = Transform(otherBody).WorldPosition; var playerDir = Transform(otherBody).WorldPosition;
_throwing.TryThrow(otherBody, playerDir-fieldDir, strength: component.ThrowForce); _throwing.TryThrow(otherBody, playerDir-fieldDir, baseThrowSpeed: component.ThrowForce);
} }
} }

View File

@@ -38,11 +38,11 @@ public sealed partial class HandsComponent : Component
public bool DisableExplosionRecursion = false; public bool DisableExplosionRecursion = false;
/// <summary> /// <summary>
/// The amount of throw impulse per distance the player is from the throw target. /// Modifies the speed at which items are thrown.
/// </summary> /// </summary>
[DataField("throwForceMultiplier")] [DataField]
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
public float ThrowForceMultiplier { get; set; } = 10f; //should be tuned so that a thrown item lands about under the player's cursor public float BaseThrowspeed { get; set; } = 10f;
/// <summary> /// <summary>
/// Distance after which longer throw targets stop increasing throw impulse. /// Distance after which longer throw targets stop increasing throw impulse.

View File

@@ -5,17 +5,17 @@ namespace Content.Shared.Throwing;
[ByRefEvent] [ByRefEvent]
public struct BeforeThrowEvent public struct BeforeThrowEvent
{ {
public BeforeThrowEvent(EntityUid itemUid, Vector2 direction, float throwStrength, EntityUid playerUid) public BeforeThrowEvent(EntityUid itemUid, Vector2 direction, float throwSpeed, EntityUid playerUid)
{ {
ItemUid = itemUid; ItemUid = itemUid;
Direction = direction; Direction = direction;
ThrowStrength = throwStrength; ThrowSpeed = throwSpeed;
PlayerUid = playerUid; PlayerUid = playerUid;
} }
public EntityUid ItemUid { get; set; } public EntityUid ItemUid { get; set; }
public Vector2 Direction { get; } public Vector2 Direction { get; }
public float ThrowStrength { get; set;} public float ThrowSpeed { get; set;}
public EntityUid PlayerUid { get; } public EntityUid PlayerUid { get; }
public bool Cancelled = false; public bool Cancelled = false;

View File

@@ -0,0 +1,12 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Throwing
{
/// <summary>
/// Makes an item land at the cursor when thrown and slide a little further.
/// Without it the item lands slightly in front and stops moving at the cursor.
/// Use this for throwing weapons that should pierce the opponent, for example spears.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed partial class LandAtCursorComponent : Component { }
}

View File

@@ -1,13 +1,12 @@
using System.Numerics; using System.Numerics;
using Content.Shared.Administration.Logs; using Content.Shared.Administration.Logs;
using Content.Shared.Camera; using Content.Shared.Camera;
using Content.Shared.CCVar;
using Content.Shared.Database; using Content.Shared.Database;
using Content.Shared.Friction;
using Content.Shared.Gravity; using Content.Shared.Gravity;
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction;
using Content.Shared.Projectiles; using Content.Shared.Projectiles;
using Content.Shared.Tag; using Robust.Shared.Configuration;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Physics; using Robust.Shared.Physics;
using Robust.Shared.Physics.Components; using Robust.Shared.Physics.Components;
@@ -31,7 +30,10 @@ public sealed class ThrowingSystem : EntitySystem
/// The minimum amount of time an entity needs to be thrown before the timer can be run. /// The minimum amount of time an entity needs to be thrown before the timer can be run.
/// Anything below this threshold never enters the air. /// Anything below this threshold never enters the air.
/// </summary> /// </summary>
public const float FlyTime = 0.15f; public const float MinFlyTime = 0.15f;
public const float FlyTimePercentage = 0.8f;
private float _frictionModifier;
[Dependency] private readonly IGameTiming _gameTiming = default!; [Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly SharedGravitySystem _gravity = default!; [Dependency] private readonly SharedGravitySystem _gravity = default!;
@@ -40,13 +42,23 @@ public sealed class ThrowingSystem : EntitySystem
[Dependency] private readonly ThrownItemSystem _thrownSystem = default!; [Dependency] private readonly ThrownItemSystem _thrownSystem = default!;
[Dependency] private readonly SharedCameraRecoilSystem _recoil = default!; [Dependency] private readonly SharedCameraRecoilSystem _recoil = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!; [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] private readonly IConfigurationManager _configManager = default!;
public override void Initialize()
{
base.Initialize();
Subs.CVar(_configManager, CCVars.TileFrictionModifier, value => _frictionModifier = value, true);
}
public void TryThrow( public void TryThrow(
EntityUid uid, EntityUid uid,
EntityCoordinates coordinates, EntityCoordinates coordinates,
float strength = 1.0f, float baseThrowSpeed = 10.0f,
EntityUid? user = null, EntityUid? user = null,
float pushbackRatio = PushbackDefault, float pushbackRatio = PushbackDefault,
float? friction = null,
bool compensateFriction = false,
bool recoil = true, bool recoil = true,
bool animated = true, bool animated = true,
bool playSound = true, bool playSound = true,
@@ -58,7 +70,7 @@ public sealed class ThrowingSystem : EntitySystem
if (mapPos.MapId != thrownPos.MapId) if (mapPos.MapId != thrownPos.MapId)
return; return;
TryThrow(uid, mapPos.Position - thrownPos.Position, strength, user, pushbackRatio, recoil: recoil, animated: animated, playSound: playSound, doSpin: doSpin); TryThrow(uid, mapPos.Position - thrownPos.Position, baseThrowSpeed, user, pushbackRatio, friction, compensateFriction: compensateFriction, recoil: recoil, animated: animated, playSound: playSound, doSpin: doSpin);
} }
/// <summary> /// <summary>
@@ -66,14 +78,18 @@ public sealed class ThrowingSystem : EntitySystem
/// </summary> /// </summary>
/// <param name="uid">The entity being thrown.</param> /// <param name="uid">The entity being thrown.</param>
/// <param name="direction">A vector pointing from the entity to its destination.</param> /// <param name="direction">A vector pointing from the entity to its destination.</param>
/// <param name="strength">How much the direction vector should be multiplied for velocity.</param> /// <param name="baseThrowSpeed">Throw velocity. Gets modified if compensateFriction is true.</param>
/// <param name="pushbackRatio">The ratio of impulse applied to the thrower - defaults to 10 because otherwise it's not enough to properly recover from getting spaced</param> /// <param name="pushbackRatio">The ratio of impulse applied to the thrower - defaults to 10 because otherwise it's not enough to properly recover from getting spaced</param>
/// <param name="friction">friction value used for the distance calculation. If set to null this defaults to the standard tile values</param>
/// <param name="compensateFriction">True will adjust the throw so the item stops at the target coordinates. False means it will land at the target and keep sliding.</param>
/// <param name="doSpin">Whether spin will be applied to the thrown entity.</param> /// <param name="doSpin">Whether spin will be applied to the thrown entity.</param>
public void TryThrow(EntityUid uid, public void TryThrow(EntityUid uid,
Vector2 direction, Vector2 direction,
float strength = 1.0f, float baseThrowSpeed = 10.0f,
EntityUid? user = null, EntityUid? user = null,
float pushbackRatio = PushbackDefault, float pushbackRatio = PushbackDefault,
float? friction = null,
bool compensateFriction = false,
bool recoil = true, bool recoil = true,
bool animated = true, bool animated = true,
bool playSound = true, bool playSound = true,
@@ -91,9 +107,10 @@ public sealed class ThrowingSystem : EntitySystem
physics, physics,
Transform(uid), Transform(uid),
projectileQuery, projectileQuery,
strength, baseThrowSpeed,
user, user,
pushbackRatio, recoil: recoil, animated: animated, playSound: playSound, doSpin: doSpin); pushbackRatio,
friction, compensateFriction: compensateFriction, recoil: recoil, animated: animated, playSound: playSound, doSpin: doSpin);
} }
/// <summary> /// <summary>
@@ -101,23 +118,27 @@ public sealed class ThrowingSystem : EntitySystem
/// </summary> /// </summary>
/// <param name="uid">The entity being thrown.</param> /// <param name="uid">The entity being thrown.</param>
/// <param name="direction">A vector pointing from the entity to its destination.</param> /// <param name="direction">A vector pointing from the entity to its destination.</param>
/// <param name="strength">How much the direction vector should be multiplied for velocity.</param> /// <param name="baseThrowSpeed">Throw velocity. Gets modified if compensateFriction is true.</param>
/// <param name="pushbackRatio">The ratio of impulse applied to the thrower - defaults to 10 because otherwise it's not enough to properly recover from getting spaced</param> /// <param name="pushbackRatio">The ratio of impulse applied to the thrower - defaults to 10 because otherwise it's not enough to properly recover from getting spaced</param>
/// <param name="friction">friction value used for the distance calculation. If set to null this defaults to the standard tile values</param>
/// <param name="compensateFriction">True will adjust the throw so the item stops at the target coordinates. False means it will land at the target and keep sliding.</param>
/// <param name="doSpin">Whether spin will be applied to the thrown entity.</param> /// <param name="doSpin">Whether spin will be applied to the thrown entity.</param>
public void TryThrow(EntityUid uid, public void TryThrow(EntityUid uid,
Vector2 direction, Vector2 direction,
PhysicsComponent physics, PhysicsComponent physics,
TransformComponent transform, TransformComponent transform,
EntityQuery<ProjectileComponent> projectileQuery, EntityQuery<ProjectileComponent> projectileQuery,
float strength = 1.0f, float baseThrowSpeed = 10.0f,
EntityUid? user = null, EntityUid? user = null,
float pushbackRatio = PushbackDefault, float pushbackRatio = PushbackDefault,
float? friction = null,
bool compensateFriction = false,
bool recoil = true, bool recoil = true,
bool animated = true, bool animated = true,
bool playSound = true, bool playSound = true,
bool doSpin = true) bool doSpin = true)
{ {
if (strength <= 0 || direction == Vector2Helpers.Infinity || direction == Vector2Helpers.NaN || direction == Vector2.Zero) if (baseThrowSpeed <= 0 || direction == Vector2Helpers.Infinity || direction == Vector2Helpers.NaN || direction == Vector2.Zero || friction < 0)
return; return;
if ((physics.BodyType & (BodyType.Dynamic | BodyType.KinematicController)) == 0x0) if ((physics.BodyType & (BodyType.Dynamic | BodyType.KinematicController)) == 0x0)
@@ -136,16 +157,22 @@ public sealed class ThrowingSystem : EntitySystem
Animate = animated, Animate = animated,
}; };
// Estimate time to arrival so we can apply OnGround status and slow it much faster. // if not given, get the default friction value for distance calculation
var time = direction.Length() / strength; var tileFriction = friction ?? _frictionModifier * TileFrictionController.DefaultFriction;
if (tileFriction == 0f)
compensateFriction = false; // cannot calculate this if there is no friction
// Set the time the item is supposed to be in the air so we can apply OnGround status.
// This is a free parameter, but we should set it to something reasonable.
var flyTime = direction.Length() / baseThrowSpeed;
if (compensateFriction)
flyTime *= FlyTimePercentage;
if (flyTime < MinFlyTime)
flyTime = 0f;
comp.ThrownTime = _gameTiming.CurTime; comp.ThrownTime = _gameTiming.CurTime;
// TODO: This is a bandaid, don't do this. comp.LandTime = comp.ThrownTime + TimeSpan.FromSeconds(flyTime);
// if you want to force landtime have the caller handle it or add a new method.
// did we launch this with something stronger than our hands?
if (TryComp<HandsComponent>(comp.Thrower, out var hands) && strength > hands.ThrowForceMultiplier)
comp.LandTime = comp.ThrownTime + TimeSpan.FromSeconds(time);
else
comp.LandTime = time < FlyTime ? default : comp.ThrownTime + TimeSpan.FromSeconds(time - FlyTime);
comp.PlayLandSound = playSound; comp.PlayLandSound = playSound;
AddComp(uid, comp, true); AddComp(uid, comp, true);
@@ -173,7 +200,12 @@ public sealed class ThrowingSystem : EntitySystem
if (user != null) if (user != null)
_adminLogger.Add(LogType.Throw, LogImpact.Low, $"{ToPrettyString(user.Value):user} threw {ToPrettyString(uid):entity}"); _adminLogger.Add(LogType.Throw, LogImpact.Low, $"{ToPrettyString(user.Value):user} threw {ToPrettyString(uid):entity}");
var impulseVector = direction.Normalized() * strength * physics.Mass; // if compensateFriction==true compensate for the distance the item will slide over the floor after landing by reducing the throw speed accordingly.
// else let the item land on the cursor and from where it slides a little further.
// This is an exact formula we get from exponentially decaying velocity after landing.
// If someone changes how tile friction works at some point, this will have to be adjusted.
var throwSpeed = compensateFriction ? direction.Length() / (flyTime + 1 / tileFriction) : baseThrowSpeed;
var impulseVector = direction.Normalized() * throwSpeed * physics.Mass;
_physics.ApplyLinearImpulse(uid, impulseVector, body: physics); _physics.ApplyLinearImpulse(uid, impulseVector, body: physics);
if (comp.LandTime == null || comp.LandTime <= TimeSpan.Zero) if (comp.LandTime == null || comp.LandTime <= TimeSpan.Zero)

View File

@@ -153,7 +153,7 @@ namespace Content.Shared.Throwing
LandComponent(uid, thrown, physics, thrown.PlayLandSound); LandComponent(uid, thrown, physics, thrown.PlayLandSound);
} }
var stopThrowTime = (thrown.LandTime ?? thrown.ThrownTime) + TimeSpan.FromSeconds(ThrowingSystem.FlyTime); var stopThrowTime = thrown.LandTime ?? thrown.ThrownTime;
if (stopThrowTime <= _gameTiming.CurTime) if (stopThrowTime <= _gameTiming.CurTime)
{ {
StopThrow(uid, thrown); StopThrow(uid, thrown);

View File

@@ -10,6 +10,7 @@
offset: 0.0,0.0 offset: 0.0,0.0
- type: ThrowingAngle - type: ThrowingAngle
angle: 315 angle: 315
- type: LandAtCursor
- type: Fixtures - type: Fixtures
fixtures: fixtures:
fix1: fix1:

View File

@@ -29,6 +29,7 @@
removalTime: 0.0 removalTime: 0.0
- type: ThrowingAngle - type: ThrowingAngle
angle: 315 angle: 315
- type: LandAtCursor
- type: DamageOtherOnHit - type: DamageOtherOnHit
damage: damage:
types: types:

View File

@@ -238,6 +238,7 @@
embedOnThrow: True embedOnThrow: True
- type: ThrowingAngle - type: ThrowingAngle
angle: 0 angle: 0
- type: LandAtCursor
- type: Ammo - type: Ammo
muzzleFlash: null muzzleFlash: null
- type: Projectile - type: Projectile

View File

@@ -31,3 +31,4 @@
removalTime: .2 removalTime: .2
- type: ThrowingAngle - type: ThrowingAngle
angle: 180 angle: 180
- type: LandAtCursor

View File

@@ -92,6 +92,7 @@
Slash: 12 Slash: 12
- type: EmbeddableProjectile - type: EmbeddableProjectile
sound: /Audio/Weapons/star_hit.ogg sound: /Audio/Weapons/star_hit.ogg
- type: LandAtCursor
- type: DamageOtherOnHit - type: DamageOtherOnHit
damage: damage:
types: types:
@@ -150,6 +151,7 @@
damage: damage:
types: types:
Slash: 10 Slash: 10
- type: LandAtCursor
- type: Sprite - type: Sprite
sprite: Clothing/Head/Hats/greyflatcap.rsi sprite: Clothing/Head/Hats/greyflatcap.rsi
- type: Clothing - type: Clothing
@@ -274,6 +276,7 @@
Slash: 5 Slash: 5
- type: EmbeddableProjectile - type: EmbeddableProjectile
sound: /Audio/Weapons/star_hit.ogg sound: /Audio/Weapons/star_hit.ogg
- type: LandAtCursor
- type: DamageOtherOnHit - type: DamageOtherOnHit
ignoreResistances: true ignoreResistances: true
damage: damage:

View File

@@ -8,6 +8,7 @@
offset: 0.15,0.15 offset: 0.15,0.15
- type: ThrowingAngle - type: ThrowingAngle
angle: 225 angle: 225
- type: LandAtCursor
- type: Tag - type: Tag
tags: tags:
- Spear - Spear

View File

@@ -47,4 +47,4 @@
staminaDamage: 55 # Sudden weight increase sapping stamina staminaDamage: 55 # Sudden weight increase sapping stamina
canThrowTrigger: true canThrowTrigger: true
canMoveBreakout: true canMoveBreakout: true
- type: LandAtCursor

View File

@@ -96,6 +96,7 @@
damage: damage:
types: types:
Blunt: 10 Blunt: 10
- type: LandAtCursor
- type: Damageable - type: Damageable
damageContainer: Inorganic damageContainer: Inorganic
- type: EmitSoundOnTrigger - type: EmitSoundOnTrigger
@@ -224,6 +225,7 @@
damage: damage:
types: types:
Blunt: 10 Blunt: 10
- type: LandAtCursor
- type: EmitSoundOnTrigger - type: EmitSoundOnTrigger
sound: sound:
path: "/Audio/Effects/flash_bang.ogg" path: "/Audio/Effects/flash_bang.ogg"

View File

@@ -31,6 +31,7 @@
friction: 0.2 friction: 0.2
- type: EmbeddableProjectile - type: EmbeddableProjectile
sound: /Audio/Weapons/star_hit.ogg sound: /Audio/Weapons/star_hit.ogg
- type: LandAtCursor
- type: DamageOtherOnHit - type: DamageOtherOnHit
damage: damage:
types: types: