2021-11-21 17:09:49 +11:00
|
|
|
using Content.Server.Shuttles.Components;
|
2022-06-16 15:28:16 +10:00
|
|
|
using Content.Server.Shuttles.Systems;
|
2022-04-23 21:05:02 -04:00
|
|
|
using Content.Shared.Vehicle.Components;
|
2021-06-19 10:03:24 +02:00
|
|
|
using Content.Shared.Movement;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Movement.Components;
|
2021-11-21 17:09:49 +11:00
|
|
|
using Content.Shared.Shuttles.Components;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Shared.Map;
|
2021-03-21 09:12:03 -07:00
|
|
|
using Robust.Shared.Player;
|
2021-04-21 20:14:40 +10:00
|
|
|
using Robust.Shared.Utility;
|
2019-04-04 16:18:43 +02:00
|
|
|
|
2021-03-08 04:09:59 +11:00
|
|
|
namespace Content.Server.Physics.Controllers
|
2019-04-04 16:18:43 +02:00
|
|
|
{
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class MoverController : SharedMoverController
|
2019-04-04 16:18:43 +02:00
|
|
|
{
|
2020-06-24 02:21:20 +02:00
|
|
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
2022-03-14 02:42:39 +11:00
|
|
|
[Dependency] private readonly ShuttleSystem _shuttle = default!;
|
|
|
|
|
[Dependency] private readonly ThrusterSystem _thruster = default!;
|
2022-04-23 21:05:02 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// These mobs will get skipped over when checking which mobs
|
|
|
|
|
/// should be moved. Prediction is handled elsewhere by
|
|
|
|
|
/// cancelling the movement attempt in the shared and
|
|
|
|
|
/// client namespace.
|
|
|
|
|
/// </summary>
|
2021-03-08 04:09:59 +11:00
|
|
|
private HashSet<EntityUid> _excludedMobs = new();
|
2021-12-03 13:03:06 +11:00
|
|
|
private Dictionary<ShuttleComponent, List<(PilotComponent, IMoverComponent)>> _shuttlePilots = new();
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2022-03-14 02:42:39 +11:00
|
|
|
protected override Filter GetSoundPlayers(EntityUid mover)
|
2019-04-04 16:18:43 +02:00
|
|
|
{
|
2022-03-14 02:42:39 +11:00
|
|
|
return Filter.Pvs(mover, entityManager: EntityManager).RemoveWhereAttachedEntity(o => o == mover);
|
|
|
|
|
}
|
2021-07-21 21:15:12 +10:00
|
|
|
|
2022-03-14 02:42:39 +11:00
|
|
|
protected override bool CanSound()
|
|
|
|
|
{
|
|
|
|
|
return true;
|
2021-02-28 18:49:48 +01:00
|
|
|
}
|
2021-03-01 03:11:29 +11:00
|
|
|
|
2021-03-08 04:09:59 +11:00
|
|
|
public override void UpdateBeforeSolve(bool prediction, float frameTime)
|
2021-02-28 18:49:48 +01:00
|
|
|
{
|
2021-03-08 04:09:59 +11:00
|
|
|
base.UpdateBeforeSolve(prediction, frameTime);
|
|
|
|
|
_excludedMobs.Clear();
|
|
|
|
|
|
2022-02-02 17:34:25 +11:00
|
|
|
foreach (var (mobMover, mover, physics, xform) in EntityManager.EntityQuery<IMobMoverComponent, IMoverComponent, PhysicsComponent, TransformComponent>())
|
2021-03-01 03:11:29 +11:00
|
|
|
{
|
2021-12-03 15:53:09 +01:00
|
|
|
_excludedMobs.Add(mover.Owner);
|
2022-02-02 17:34:25 +11:00
|
|
|
HandleMobMovement(mover, physics, mobMover, xform);
|
2021-03-08 04:09:59 +11:00
|
|
|
}
|
|
|
|
|
|
2021-12-03 13:03:06 +11:00
|
|
|
HandleShuttleMovement(frameTime);
|
2022-05-28 10:53:46 +10:00
|
|
|
HandleVehicleMovement();
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2021-09-28 13:35:29 +02:00
|
|
|
foreach (var (mover, physics) in EntityManager.EntityQuery<IMoverComponent, PhysicsComponent>(true))
|
2021-03-08 04:09:59 +11:00
|
|
|
{
|
2021-12-03 15:53:09 +01:00
|
|
|
if (_excludedMobs.Contains(mover.Owner)) continue;
|
2021-03-08 04:09:59 +11:00
|
|
|
|
|
|
|
|
HandleKinematicMovement(mover, physics);
|
2019-04-04 16:18:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 13:03:06 +11:00
|
|
|
private void HandleShuttleMovement(float frameTime)
|
2020-06-24 02:21:20 +02:00
|
|
|
{
|
2021-12-03 13:03:06 +11:00
|
|
|
var newPilots = new Dictionary<ShuttleComponent, List<(PilotComponent, IMoverComponent)>>();
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2021-12-03 13:03:06 +11:00
|
|
|
// We just mark off their movement and the shuttle itself does its own movement
|
|
|
|
|
foreach (var (pilot, mover, xform) in EntityManager.EntityQuery<PilotComponent, SharedPlayerInputMoverComponent, TransformComponent>())
|
2021-07-21 21:15:12 +10:00
|
|
|
{
|
2021-12-03 13:03:06 +11:00
|
|
|
if (pilot.Console == null) continue;
|
2021-12-03 15:53:09 +01:00
|
|
|
_excludedMobs.Add(mover.Owner);
|
2021-12-03 13:03:06 +11:00
|
|
|
|
2022-06-11 18:54:41 -07:00
|
|
|
var gridId = xform.GridEntityId;
|
2022-04-23 21:05:02 -04:00
|
|
|
// This tries to see if the grid is a shuttle
|
2021-12-03 13:03:06 +11:00
|
|
|
if (!_mapManager.TryGetGrid(gridId, out var grid) ||
|
|
|
|
|
!EntityManager.TryGetComponent(grid.GridEntityId, out ShuttleComponent? shuttleComponent)) continue;
|
|
|
|
|
|
|
|
|
|
if (!newPilots.TryGetValue(shuttleComponent, out var pilots))
|
|
|
|
|
{
|
|
|
|
|
pilots = new List<(PilotComponent, IMoverComponent)>();
|
|
|
|
|
newPilots[shuttleComponent] = pilots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pilots.Add((pilot, mover));
|
2021-07-21 21:15:12 +10:00
|
|
|
}
|
|
|
|
|
|
2021-12-03 13:03:06 +11:00
|
|
|
// Reset inputs for non-piloted shuttles.
|
|
|
|
|
foreach (var (shuttle, _) in _shuttlePilots)
|
2021-11-21 17:09:49 +11:00
|
|
|
{
|
2021-12-03 13:03:06 +11:00
|
|
|
if (newPilots.ContainsKey(shuttle)) continue;
|
|
|
|
|
|
2022-03-14 02:42:39 +11:00
|
|
|
_thruster.DisableLinearThrusters(shuttle);
|
2021-11-21 17:09:49 +11:00
|
|
|
}
|
|
|
|
|
|
2021-12-03 13:03:06 +11:00
|
|
|
_shuttlePilots = newPilots;
|
2021-07-21 21:15:12 +10:00
|
|
|
|
2021-12-03 13:03:06 +11:00
|
|
|
// Collate all of the linear / angular velocites for a shuttle
|
|
|
|
|
// then do the movement input once for it.
|
|
|
|
|
foreach (var (shuttle, pilots) in _shuttlePilots)
|
2020-07-03 23:32:41 +02:00
|
|
|
{
|
2022-01-26 17:57:48 +11:00
|
|
|
if (Paused(shuttle.Owner) || !TryComp(shuttle.Owner, out PhysicsComponent? body)) continue;
|
2021-12-03 13:03:06 +11:00
|
|
|
|
|
|
|
|
// Collate movement linear and angular inputs together
|
|
|
|
|
var linearInput = Vector2.Zero;
|
|
|
|
|
var angularInput = 0f;
|
|
|
|
|
|
|
|
|
|
switch (shuttle.Mode)
|
|
|
|
|
{
|
|
|
|
|
case ShuttleMode.Cruise:
|
|
|
|
|
foreach (var (pilot, mover) in pilots)
|
|
|
|
|
{
|
|
|
|
|
var console = pilot.Console;
|
|
|
|
|
|
|
|
|
|
if (console == null)
|
|
|
|
|
{
|
|
|
|
|
DebugTools.Assert(false);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sprint = mover.VelocityDir.sprinting;
|
|
|
|
|
|
|
|
|
|
if (sprint.Equals(Vector2.Zero)) continue;
|
|
|
|
|
|
2022-01-26 17:57:48 +11:00
|
|
|
var offsetRotation = EntityManager.GetComponent<TransformComponent>(console.Owner).LocalRotation;
|
2021-12-03 13:03:06 +11:00
|
|
|
|
|
|
|
|
linearInput += offsetRotation.RotateVec(new Vector2(0f, sprint.Y));
|
|
|
|
|
angularInput += sprint.X;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2022-06-16 15:28:16 +10:00
|
|
|
case ShuttleMode.Strafing:
|
2021-12-03 13:03:06 +11:00
|
|
|
// No angular input possible
|
|
|
|
|
foreach (var (pilot, mover) in pilots)
|
|
|
|
|
{
|
|
|
|
|
var console = pilot.Console;
|
|
|
|
|
|
|
|
|
|
if (console == null)
|
|
|
|
|
{
|
|
|
|
|
DebugTools.Assert(false);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sprint = mover.VelocityDir.sprinting;
|
|
|
|
|
|
|
|
|
|
if (sprint.Equals(Vector2.Zero)) continue;
|
|
|
|
|
|
2021-12-07 22:22:34 +11:00
|
|
|
var offsetRotation = EntityManager.GetComponent<TransformComponent>((console).Owner).LocalRotation;
|
2021-12-03 13:03:06 +11:00
|
|
|
sprint = offsetRotation.RotateVec(sprint);
|
|
|
|
|
|
|
|
|
|
linearInput += sprint;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var count = pilots.Count;
|
|
|
|
|
linearInput /= count;
|
|
|
|
|
angularInput /= count;
|
|
|
|
|
|
|
|
|
|
// Handle shuttle movement
|
|
|
|
|
if (linearInput.Length.Equals(0f))
|
|
|
|
|
{
|
2022-03-14 02:42:39 +11:00
|
|
|
_thruster.DisableLinearThrusters(shuttle);
|
|
|
|
|
body.LinearDamping = _shuttle.ShuttleIdleLinearDamping * body.InvMass;
|
2022-03-07 02:03:53 -03:00
|
|
|
if (body.LinearVelocity.Length < 0.08)
|
|
|
|
|
{
|
|
|
|
|
body.LinearVelocity = Vector2.Zero;
|
|
|
|
|
}
|
2021-12-03 13:03:06 +11:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-03-07 02:03:53 -03:00
|
|
|
body.LinearDamping = 0;
|
2021-12-03 13:03:06 +11:00
|
|
|
var angle = linearInput.ToWorldAngle();
|
|
|
|
|
var linearDir = angle.GetDir();
|
|
|
|
|
var dockFlag = linearDir.AsFlag();
|
2022-01-11 18:17:54 +11:00
|
|
|
var shuttleNorth = EntityManager.GetComponent<TransformComponent>(body.Owner).WorldRotation.ToWorldVec();
|
2021-11-21 17:09:49 +11:00
|
|
|
|
2022-03-07 02:03:53 -03:00
|
|
|
var totalForce = new Vector2();
|
|
|
|
|
|
2021-11-21 17:09:49 +11:00
|
|
|
// Won't just do cardinal directions.
|
|
|
|
|
foreach (DirectionFlag dir in Enum.GetValues(typeof(DirectionFlag)))
|
2021-10-09 10:55:10 +11:00
|
|
|
{
|
2021-11-21 17:09:49 +11:00
|
|
|
// Brain no worky but I just want cardinals
|
|
|
|
|
switch (dir)
|
|
|
|
|
{
|
|
|
|
|
case DirectionFlag.South:
|
|
|
|
|
case DirectionFlag.East:
|
|
|
|
|
case DirectionFlag.North:
|
|
|
|
|
case DirectionFlag.West:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 13:03:06 +11:00
|
|
|
if ((dir & dockFlag) == 0x0)
|
2021-10-09 10:55:10 +11:00
|
|
|
{
|
2022-03-14 02:42:39 +11:00
|
|
|
_thruster.DisableLinearThrustDirection(shuttle, dir);
|
2021-12-03 13:03:06 +11:00
|
|
|
continue;
|
2021-10-09 10:55:10 +11:00
|
|
|
}
|
|
|
|
|
|
2021-12-03 13:03:06 +11:00
|
|
|
float length;
|
2022-01-11 18:17:54 +11:00
|
|
|
Angle thrustAngle;
|
2021-11-21 17:09:49 +11:00
|
|
|
|
2021-12-03 13:03:06 +11:00
|
|
|
switch (dir)
|
2021-11-21 17:09:49 +11:00
|
|
|
{
|
2021-12-03 13:03:06 +11:00
|
|
|
case DirectionFlag.North:
|
|
|
|
|
length = linearInput.Y;
|
2022-01-11 18:17:54 +11:00
|
|
|
thrustAngle = new Angle(MathF.PI);
|
2021-11-21 17:09:49 +11:00
|
|
|
break;
|
2021-12-03 13:03:06 +11:00
|
|
|
case DirectionFlag.South:
|
|
|
|
|
length = -linearInput.Y;
|
2022-01-11 18:17:54 +11:00
|
|
|
thrustAngle = new Angle(0f);
|
2021-12-03 13:03:06 +11:00
|
|
|
break;
|
|
|
|
|
case DirectionFlag.East:
|
|
|
|
|
length = linearInput.X;
|
2022-01-11 18:17:54 +11:00
|
|
|
thrustAngle = new Angle(MathF.PI / 2f);
|
2021-12-03 13:03:06 +11:00
|
|
|
break;
|
|
|
|
|
case DirectionFlag.West:
|
|
|
|
|
length = -linearInput.X;
|
2022-01-11 18:17:54 +11:00
|
|
|
thrustAngle = new Angle(-MathF.PI / 2f);
|
2021-11-21 17:09:49 +11:00
|
|
|
break;
|
2021-12-03 13:03:06 +11:00
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
2021-11-21 17:09:49 +11:00
|
|
|
}
|
|
|
|
|
|
2022-03-14 02:42:39 +11:00
|
|
|
_thruster.EnableLinearThrustDirection(shuttle, dir);
|
2021-11-21 17:09:49 +11:00
|
|
|
|
2021-12-03 13:03:06 +11:00
|
|
|
var index = (int) Math.Log2((int) dir);
|
2022-03-07 02:03:53 -03:00
|
|
|
var force = thrustAngle.RotateVec(shuttleNorth) * shuttle.LinearThrust[index] * length;
|
2021-07-21 21:15:12 +10:00
|
|
|
|
2022-03-07 02:03:53 -03:00
|
|
|
totalForce += force;
|
2021-12-03 13:03:06 +11:00
|
|
|
}
|
2022-03-07 02:03:53 -03:00
|
|
|
|
2022-03-14 02:42:39 +11:00
|
|
|
var dragForce = body.LinearVelocity * (totalForce.Length / _shuttle.ShuttleMaxLinearSpeed);
|
2022-03-07 02:03:53 -03:00
|
|
|
body.ApplyLinearImpulse((totalForce - dragForce) * frameTime);
|
2021-12-03 13:03:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (MathHelper.CloseTo(angularInput, 0f))
|
|
|
|
|
{
|
2022-03-14 02:42:39 +11:00
|
|
|
_thruster.SetAngularThrust(shuttle, false);
|
|
|
|
|
body.AngularDamping = _shuttle.ShuttleIdleAngularDamping * body.InvI;
|
2022-03-07 02:03:53 -03:00
|
|
|
body.SleepingAllowed = true;
|
|
|
|
|
|
|
|
|
|
if (Math.Abs(body.AngularVelocity) < 0.01f)
|
|
|
|
|
{
|
|
|
|
|
body.AngularVelocity = 0f;
|
|
|
|
|
}
|
2021-12-03 13:03:06 +11:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-03-07 02:03:53 -03:00
|
|
|
body.AngularDamping = 0;
|
|
|
|
|
body.SleepingAllowed = false;
|
2021-12-03 13:03:06 +11:00
|
|
|
|
2022-03-14 02:42:39 +11:00
|
|
|
var maxSpeed = Math.Min(_shuttle.ShuttleMaxAngularMomentum * body.InvI, _shuttle.ShuttleMaxAngularSpeed);
|
|
|
|
|
var maxTorque = body.Inertia * _shuttle.ShuttleMaxAngularAcc;
|
2022-03-07 02:03:53 -03:00
|
|
|
|
|
|
|
|
var torque = Math.Min(shuttle.AngularThrust, maxTorque);
|
|
|
|
|
var dragTorque = body.AngularVelocity * (torque / maxSpeed);
|
2021-07-21 21:15:12 +10:00
|
|
|
|
2022-03-07 02:03:53 -03:00
|
|
|
body.ApplyAngularImpulse((-angularInput * torque - dragTorque) * frameTime);
|
2021-11-21 17:09:49 +11:00
|
|
|
|
2022-03-14 02:42:39 +11:00
|
|
|
_thruster.SetAngularThrust(shuttle, true);
|
2020-09-06 16:11:53 +02:00
|
|
|
}
|
2019-04-04 16:18:43 +02:00
|
|
|
}
|
2021-04-21 20:14:40 +10:00
|
|
|
}
|
2022-04-23 21:05:02 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Add mobs riding vehicles to the list of mobs whose input
|
|
|
|
|
/// should be ignored.
|
|
|
|
|
/// </summary>
|
2022-05-28 10:53:46 +10:00
|
|
|
private void HandleVehicleMovement()
|
2022-04-23 21:05:02 -04:00
|
|
|
{
|
2022-05-28 10:53:46 +10:00
|
|
|
// TODO: Nuke this code. It's on my list.
|
|
|
|
|
foreach (var (rider, mover) in EntityQuery<RiderComponent, SharedPlayerInputMoverComponent>())
|
2022-04-23 21:05:02 -04:00
|
|
|
{
|
|
|
|
|
if (rider.Vehicle == null) continue;
|
|
|
|
|
_excludedMobs.Add(mover.Owner);
|
2022-05-28 10:53:46 +10:00
|
|
|
|
|
|
|
|
if (!_excludedMobs.Add(rider.Vehicle.Owner)) continue;
|
|
|
|
|
|
|
|
|
|
if (!TryComp<IMoverComponent>(rider.Vehicle.Owner, out var vehicleMover) ||
|
|
|
|
|
!TryComp<PhysicsComponent>(rider.Vehicle.Owner, out var vehicleBody) ||
|
|
|
|
|
rider.Vehicle.Owner.IsWeightless(vehicleBody, mapManager: _mapManager, entityManager: EntityManager)) continue;
|
|
|
|
|
|
|
|
|
|
HandleKinematicMovement(vehicleMover, vehicleBody);
|
2022-04-23 21:05:02 -04:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-04 16:18:43 +02:00
|
|
|
}
|
|
|
|
|
}
|