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

Grid physics 2: gun pushback #32415

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions Content.Server/Fluids/Components/SprayComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public sealed partial class SprayComponent : Component
/// <summary>
/// How much the player is pushed back for each spray.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField]
public float PushbackAmount = 2f;
[DataField]
public float PushbackAmount = 5f;

[ViewVariables(VVAccess.ReadWrite), DataField(required: true)]
[DataField(required: true)]
[Access(typeof(SpraySystem), Other = AccessPermissions.ReadExecute)] // FIXME Friends
public SoundSpecifier SpraySound { get; private set; } = default!;
}
30 changes: 28 additions & 2 deletions Content.Server/Fluids/EntitySystems/SpraySystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Content.Server.Fluids.Components;
using Content.Server.Gravity;
using Content.Server.Popups;
using Content.Shared.CCVar;
using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.FixedPoint;
using Content.Shared.Fluids;
using Content.Shared.Interaction;
Expand All @@ -11,6 +13,7 @@
using Content.Shared.Chemistry.EntitySystems;
using Robust.Server.GameObjects;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Configuration;
using Robust.Shared.Physics.Components;
using Robust.Shared.Prototypes;
using System.Numerics;
Expand All @@ -29,12 +32,21 @@ public sealed class SpraySystem : EntitySystem
[Dependency] private readonly VaporSystem _vapor = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;

private float _gridImpulseMultiplier;

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<SprayComponent, AfterInteractEvent>(OnAfterInteract);
Subs.CVar(_cfg, CCVars.GridImpulseMultiplier, UpdateGridMassMultiplier, true);
}

private void UpdateGridMassMultiplier(float value)
{
_gridImpulseMultiplier = value;
}

private void OnAfterInteract(Entity<SprayComponent> entity, ref AfterInteractEvent args)
Expand Down Expand Up @@ -66,7 +78,7 @@ private void OnAfterInteract(Entity<SprayComponent> entity, ref AfterInteractEve
var userXform = xformQuery.GetComponent(args.User);

var userMapPos = _transform.GetMapCoordinates(userXform);
var clickMapPos = args.ClickLocation.ToMap(EntityManager, _transform);
var clickMapPos = _transform.ToMapCoordinates(args.ClickLocation);

var diffPos = clickMapPos.Position - userMapPos.Position;
if (diffPos == Vector2.Zero || diffPos == Vector2Helpers.NaN)
Expand Down Expand Up @@ -138,7 +150,21 @@ private void OnAfterInteract(Entity<SprayComponent> entity, ref AfterInteractEve
if (TryComp<PhysicsComponent>(args.User, out var body))
{
if (_gravity.IsWeightless(args.User, body))
_physics.ApplyLinearImpulse(args.User, -impulseDirection.Normalized() * entity.Comp.PushbackAmount, body: body);
{
// push back the player
_physics.ApplyLinearImpulse(args.User, -impulseDirection * entity.Comp.PushbackAmount, body: body);
}
else
{
// push back the grid the player is standing on
var userTransform = Transform(args.User);
if (userTransform.GridUid != null)
{
// apply both linear and angular momentum depending on the player position
// multiply by a cvar because grid mass is currently extremely small compared to all other masses
_physics.ApplyLinearImpulse(userTransform.GridUid.Value, -impulseDirection * _gridImpulseMultiplier * entity.Comp.PushbackAmount, userTransform.LocalPosition);
}
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions Content.Shared/CCVar/CCVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,18 @@ public static readonly CVarDef<int>
public static readonly CVarDef<int> HideSplitGridsUnder =
CVarDef.Create("shuttle.hide_split_grids_under", 30, CVar.SERVERONLY);

/// <summary>
/// Impulse multiplier for player interactions that move grids (other than shuttle thrusters, gyroscopes and grid collisons).
/// At the moment this affects the pushback in SpraySystem and from shooting guns.
/// A higher value means grids have a lower effective mass and therefore will get pushed stronger.
/// A value of 0 will disable pushback.
/// The default has been chosen such that a one tile grid roughly equals 2/3 Urist masses.
/// TODO: Make grid mass a sane number so we can get rid of this.
/// At the moment they have a very low mass of roughly 0.48 kg per tile independent of any walls or anchored objects on them.
/// </summary>
public static readonly CVarDef<float> GridImpulseMultiplier =
CVarDef.Create("shuttle.grid_impulse_multiplier", 0.01f, CVar.REPLICATED | CVar.SERVER);

/// <summary>
/// Whether to automatically spawn escape shuttles.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions Content.Shared/Weapons/Ranged/Components/GunComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ public sealed partial class GunComponent : Component
/// </summary>
[DataField]
public bool ClumsyProof = false;

/// <summary>
/// Pushback impulse applied to the gun's holder or the grid they are standing on when fired.
/// In kg*m/s
/// </summary>
[DataField]
public float ImpulseStrength = 25f;
}

[Flags]
Expand Down
35 changes: 29 additions & 6 deletions Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Content.Shared.Actions;
using Content.Shared.Administration.Logs;
using Content.Shared.Audio;
using Content.Shared.CCVar;
using Content.Shared.CombatMode;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Damage;
Expand All @@ -24,6 +25,7 @@
using Content.Shared.Whitelist;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Configuration;
using Robust.Shared.Containers;
using Robust.Shared.Map;
using Robust.Shared.Network;
Expand Down Expand Up @@ -65,7 +67,9 @@ public abstract partial class SharedGunSystem : EntitySystem
[Dependency] protected readonly ThrowingSystem ThrowingSystem = default!;
[Dependency] private readonly UseDelaySystem _useDelay = default!;
[Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;

private float _gridImpulseMultiplier;
private const float InteractNextFire = 0.3f;
private const double SafetyNextFire = 0.5;
private const float EjectOffset = 0.4f;
Expand Down Expand Up @@ -97,6 +101,12 @@ public override void Initialize()
SubscribeLocalEvent<GunComponent, CycleModeEvent>(OnCycleMode);
SubscribeLocalEvent<GunComponent, HandSelectedEvent>(OnGunSelected);
SubscribeLocalEvent<GunComponent, MapInitEvent>(OnMapInit);
Subs.CVar(_cfg, CCVars.GridImpulseMultiplier, UpdateGridMassMultiplier, true);
}

private void UpdateGridMassMultiplier(float value)
{
_gridImpulseMultiplier = value;
}

private void OnMapInit(Entity<GunComponent> gun, ref MapInitEvent args)
Expand Down Expand Up @@ -354,8 +364,7 @@ private void AttemptShoot(EntityUid user, EntityUid gunUid, GunComponent gun)

if (userImpulse && TryComp<PhysicsComponent>(user, out var userPhysics))
{
if (_gravity.IsWeightless(user, userPhysics))
CauseImpulse(fromCoordinates, toCoordinates.Value, user, userPhysics);
CauseImpulse(gun.ImpulseStrength, fromCoordinates, toCoordinates.Value, user, userPhysics);
}

Dirty(gunUid, gun);
Expand Down Expand Up @@ -479,15 +488,29 @@ protected void MuzzleFlash(EntityUid gun, AmmoComponent component, Angle worldAn
CreateEffect(gun, ev, gun);
}

public void CauseImpulse(EntityCoordinates fromCoordinates, EntityCoordinates toCoordinates, EntityUid user, PhysicsComponent userPhysics)
public void CauseImpulse(float impulseStrength, EntityCoordinates fromCoordinates, EntityCoordinates toCoordinates, EntityUid user, PhysicsComponent userPhysics)
{
var fromMap = fromCoordinates.ToMapPos(EntityManager, TransformSystem);
var toMap = toCoordinates.ToMapPos(EntityManager, TransformSystem);
var shotDirection = (toMap - fromMap).Normalized();

const float impulseStrength = 25.0f;
var impulseVector = shotDirection * impulseStrength;
Physics.ApplyLinearImpulse(user, -impulseVector, body: userPhysics);
var impulseVector = shotDirection * impulseStrength;
if (_gravity.IsWeightless(user, userPhysics))
{
// push back the player
Physics.ApplyLinearImpulse(user, -impulseVector, body: userPhysics);
}
else
{
// push back the grid the player is standing on
var userTransform = Transform(user);
if (userTransform.GridUid != null)
{
// apply both linear and angular momentum depending on the player position
// multiply by a cvar because grid mass is currently extremely small compared to all other masses
Physics.ApplyLinearImpulse(userTransform.GridUid.Value, -impulseVector * _gridImpulseMultiplier, userTransform.LocalPosition);
}
}
}

public void RefreshModifiers(Entity<GunComponent?> gun)
Expand Down