using System.Numerics;
using Content.Shared.Administration.Logs;
using Content.Shared.Camera;
+using Content.Shared.CCVar;
using Content.Shared.Database;
+using Content.Shared.Friction;
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.Tag;
+using Robust.Shared.Configuration;
using Robust.Shared.Map;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
/// 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.
/// </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 SharedGravitySystem _gravity = default!;
[Dependency] private readonly ThrownItemSystem _thrownSystem = default!;
[Dependency] private readonly SharedCameraRecoilSystem _recoil = 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(
EntityUid uid,
EntityCoordinates coordinates,
- float strength = 1.0f,
+ float baseThrowSpeed = 10.0f,
EntityUid? user = null,
float pushbackRatio = PushbackDefault,
+ float? friction = null,
+ bool compensateFriction = false,
bool recoil = true,
bool animated = true,
bool playSound = true,
if (mapPos.MapId != thrownPos.MapId)
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>
/// <param name="uid">The entity being thrown.</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="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>
public void TryThrow(EntityUid uid,
Vector2 direction,
- float strength = 1.0f,
+ float baseThrowSpeed = 10.0f,
EntityUid? user = null,
float pushbackRatio = PushbackDefault,
+ float? friction = null,
+ bool compensateFriction = false,
bool recoil = true,
bool animated = true,
bool playSound = true,
physics,
Transform(uid),
projectileQuery,
- strength,
+ baseThrowSpeed,
user,
- pushbackRatio, recoil: recoil, animated: animated, playSound: playSound, doSpin: doSpin);
+ pushbackRatio,
+ friction, compensateFriction: compensateFriction, recoil: recoil, animated: animated, playSound: playSound, doSpin: doSpin);
}
/// <summary>
/// </summary>
/// <param name="uid">The entity being thrown.</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="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>
public void TryThrow(EntityUid uid,
Vector2 direction,
PhysicsComponent physics,
TransformComponent transform,
EntityQuery<ProjectileComponent> projectileQuery,
- float strength = 1.0f,
+ float baseThrowSpeed = 10.0f,
EntityUid? user = null,
float pushbackRatio = PushbackDefault,
+ float? friction = null,
+ bool compensateFriction = false,
bool recoil = true,
bool animated = true,
bool playSound = 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;
if ((physics.BodyType & (BodyType.Dynamic | BodyType.KinematicController)) == 0x0)
Animate = animated,
};
- // Estimate time to arrival so we can apply OnGround status and slow it much faster.
- var time = direction.Length() / strength;
+ // if not given, get the default friction value for distance calculation
+ 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;
- // TODO: This is a bandaid, don't do this.
- // 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.LandTime = comp.ThrownTime + TimeSpan.FromSeconds(flyTime);
comp.PlayLandSound = playSound;
AddComp(uid, comp, true);
if (user != null)
_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);
if (comp.LandTime == null || comp.LandTime <= TimeSpan.Zero)