2021-07-21 21:15:12 +10:00
|
|
|
using System;
|
2021-03-08 04:09:59 +11:00
|
|
|
using System.Collections.Generic;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Inventory.Components;
|
|
|
|
|
using Content.Server.Items;
|
|
|
|
|
using Content.Server.Movement.Components;
|
2021-11-21 17:09:49 +11:00
|
|
|
using Content.Server.Shuttles.Components;
|
|
|
|
|
using Content.Server.Shuttles.EntitySystems;
|
2021-07-21 21:15:12 +10:00
|
|
|
using Content.Shared.CCVar;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Inventory;
|
2019-04-05 02:04:34 +02:00
|
|
|
using Content.Shared.Maps;
|
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-07-21 21:15:12 +10:00
|
|
|
using Content.Shared.Shuttles;
|
2021-11-21 17:09:49 +11:00
|
|
|
using Content.Shared.Shuttles.Components;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Tag;
|
2021-02-12 01:31:19 -08:00
|
|
|
using Robust.Shared.Audio;
|
2021-07-21 21:15:12 +10:00
|
|
|
using Robust.Shared.Configuration;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.GameObjects;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Shared.IoC;
|
2019-11-13 17:37:46 -05:00
|
|
|
using Robust.Shared.Log;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Shared.Map;
|
2021-03-08 04:09:59 +11:00
|
|
|
using Robust.Shared.Maths;
|
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
|
|
|
{
|
2021-03-08 04:09:59 +11:00
|
|
|
public class MoverController : SharedMoverController
|
2019-04-04 16:18:43 +02:00
|
|
|
{
|
2020-06-24 02:21:20 +02:00
|
|
|
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
|
|
|
|
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
2019-04-05 02:04:34 +02:00
|
|
|
|
|
|
|
|
private const float StepSoundMoveDistanceRunning = 2;
|
|
|
|
|
private const float StepSoundMoveDistanceWalking = 1.5f;
|
|
|
|
|
|
2021-07-21 21:15:12 +10:00
|
|
|
private float _shuttleDockSpeedCap;
|
|
|
|
|
|
2021-03-08 04:09:59 +11:00
|
|
|
private HashSet<EntityUid> _excludedMobs = new();
|
|
|
|
|
|
2019-04-04 16:18:43 +02:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
2020-06-24 02:21:20 +02:00
|
|
|
base.Initialize();
|
2021-07-21 21:15:12 +10:00
|
|
|
|
|
|
|
|
var configManager = IoCManager.Resolve<IConfigurationManager>();
|
|
|
|
|
configManager.OnValueChanged(CCVars.ShuttleDockSpeedCap, value => _shuttleDockSpeedCap = value, 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();
|
|
|
|
|
|
2021-09-28 13:35:29 +02:00
|
|
|
foreach (var (mobMover, mover, physics) in EntityManager.EntityQuery<IMobMoverComponent, IMoverComponent, PhysicsComponent>())
|
2021-03-01 03:11:29 +11:00
|
|
|
{
|
2021-03-08 04:09:59 +11:00
|
|
|
_excludedMobs.Add(mover.Owner.Uid);
|
|
|
|
|
HandleMobMovement(mover, physics, mobMover);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 13:35:29 +02:00
|
|
|
foreach (var (pilot, mover) in EntityManager.EntityQuery<PilotComponent, SharedPlayerInputMoverComponent>())
|
2021-03-08 04:09:59 +11:00
|
|
|
{
|
2021-07-21 21:15:12 +10:00
|
|
|
if (pilot.Console == null) continue;
|
2021-03-08 04:09:59 +11:00
|
|
|
_excludedMobs.Add(mover.Owner.Uid);
|
|
|
|
|
HandleShuttleMovement(mover);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
if (_excludedMobs.Contains(mover.Owner.Uid)) continue;
|
|
|
|
|
|
|
|
|
|
HandleKinematicMovement(mover, physics);
|
2019-04-04 16:18:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 04:09:59 +11:00
|
|
|
/*
|
|
|
|
|
* Some thoughts:
|
|
|
|
|
* Unreal actually doesn't predict vehicle movement at all, it's purely server-side which I thought was interesting
|
|
|
|
|
* The reason for this is that vehicles change direction very slowly compared to players so you don't really have the requirement for quick movement anyway
|
|
|
|
|
* As such could probably just look at applying a force / impulse to the shuttle server-side only so it controls like the titanic.
|
|
|
|
|
*/
|
2021-07-21 21:15:12 +10:00
|
|
|
private void HandleShuttleMovement(SharedPlayerInputMoverComponent mover)
|
2020-06-24 02:21:20 +02:00
|
|
|
{
|
2021-03-08 04:09:59 +11:00
|
|
|
var gridId = mover.Owner.Transform.GridID;
|
|
|
|
|
|
|
|
|
|
if (!_mapManager.TryGetGrid(gridId, out var grid) || !EntityManager.TryGetEntity(grid.GridEntityId, out var gridEntity)) return;
|
|
|
|
|
|
2021-07-21 21:15:12 +10:00
|
|
|
if (!gridEntity.TryGetComponent(out ShuttleComponent? shuttleComponent) ||
|
|
|
|
|
!gridEntity.TryGetComponent(out PhysicsComponent? physicsComponent))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ShuttleSystem has already worked out the ratio so we'll just multiply it back by the mass.
|
2021-11-21 17:09:49 +11:00
|
|
|
var movement = mover.VelocityDir.walking + mover.VelocityDir.sprinting;
|
|
|
|
|
var system = EntitySystem.Get<ThrusterSystem>();
|
|
|
|
|
|
|
|
|
|
if (movement.Length.Equals(0f))
|
|
|
|
|
{
|
|
|
|
|
// TODO: This visualization doesn't work with multiple pilots so need to address that somehow.
|
|
|
|
|
system.DisableAllThrustDirections(shuttleComponent);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var speedCap = 0f;
|
2021-07-21 21:15:12 +10:00
|
|
|
|
|
|
|
|
switch (shuttleComponent.Mode)
|
2020-07-03 23:32:41 +02:00
|
|
|
{
|
2021-07-21 21:15:12 +10:00
|
|
|
case ShuttleMode.Docking:
|
2021-11-21 17:09:49 +11:00
|
|
|
system.DisableAllThrustDirections(shuttleComponent);
|
|
|
|
|
var dockDirection = movement.ToWorldAngle().GetDir();
|
|
|
|
|
var dockFlag = dockDirection.AsFlag();
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((dir & dockFlag) == 0x0) continue;
|
2021-10-09 10:55:10 +11:00
|
|
|
|
2021-11-21 17:09:49 +11:00
|
|
|
system.EnableThrustDirection(shuttleComponent, dir);
|
|
|
|
|
|
|
|
|
|
var index = (int) Math.Log2((int) dir);
|
|
|
|
|
|
|
|
|
|
var dockSpeed = shuttleComponent.LinearThrusterImpulse[index];
|
|
|
|
|
|
|
|
|
|
// TODO: Cvar for the speed drop
|
|
|
|
|
dockSpeed /= 5f;
|
|
|
|
|
|
|
|
|
|
if (physicsComponent.LinearVelocity.LengthSquared == 0f)
|
|
|
|
|
{
|
|
|
|
|
dockSpeed *= 5f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var moveAngle = movement.ToWorldAngle();
|
|
|
|
|
|
|
|
|
|
physicsComponent.ApplyLinearImpulse(moveAngle.RotateVec(physicsComponent.Owner.Transform.WorldRotation.ToWorldVec()) *
|
|
|
|
|
dockSpeed);
|
|
|
|
|
}
|
2021-07-21 21:15:12 +10:00
|
|
|
|
|
|
|
|
speedCap = _shuttleDockSpeedCap;
|
|
|
|
|
break;
|
|
|
|
|
case ShuttleMode.Cruise:
|
2021-11-21 17:09:49 +11:00
|
|
|
if (movement.Y == 0f)
|
2021-07-21 21:15:12 +10:00
|
|
|
{
|
2021-11-21 17:09:49 +11:00
|
|
|
system.DisableThrustDirection(shuttleComponent, DirectionFlag.South);
|
|
|
|
|
system.DisableThrustDirection(shuttleComponent, DirectionFlag.North);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var direction = movement.Y > 0f ? Direction.North : Direction.South;
|
|
|
|
|
var linearSpeed = shuttleComponent.LinearThrusterImpulse[(int) direction / 2];
|
|
|
|
|
|
2021-10-09 10:55:10 +11:00
|
|
|
if (physicsComponent.LinearVelocity.LengthSquared == 0f)
|
|
|
|
|
{
|
2021-11-21 17:09:49 +11:00
|
|
|
linearSpeed *= 5f;
|
2021-10-09 10:55:10 +11:00
|
|
|
}
|
|
|
|
|
|
2021-07-21 21:15:12 +10:00
|
|
|
// Currently this is slow BUT we'd have a separate multiplier for docking and cruising or whatever.
|
2021-11-21 17:09:49 +11:00
|
|
|
physicsComponent.ApplyLinearImpulse(physicsComponent.Owner.Transform.WorldRotation.Opposite().ToWorldVec() *
|
|
|
|
|
linearSpeed *
|
|
|
|
|
movement.Y);
|
|
|
|
|
|
|
|
|
|
switch (direction)
|
|
|
|
|
{
|
|
|
|
|
case Direction.North:
|
|
|
|
|
system.DisableThrustDirection(shuttleComponent, DirectionFlag.South);
|
|
|
|
|
system.EnableThrustDirection(shuttleComponent, DirectionFlag.North);
|
|
|
|
|
break;
|
|
|
|
|
case Direction.South:
|
|
|
|
|
system.DisableThrustDirection(shuttleComponent, DirectionFlag.North);
|
|
|
|
|
system.EnableThrustDirection(shuttleComponent, DirectionFlag.South);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var angularSpeed = shuttleComponent.AngularThrust;
|
|
|
|
|
|
|
|
|
|
if (movement.X == 0f)
|
|
|
|
|
{
|
|
|
|
|
system.DisableThrustDirection(shuttleComponent, DirectionFlag.West);
|
|
|
|
|
system.DisableThrustDirection(shuttleComponent, DirectionFlag.East);
|
|
|
|
|
}
|
|
|
|
|
else if (movement.X != 0f)
|
|
|
|
|
{
|
|
|
|
|
physicsComponent.ApplyAngularImpulse(-movement.X * angularSpeed);
|
2021-10-09 10:55:10 +11:00
|
|
|
|
2021-11-21 17:09:49 +11:00
|
|
|
if (movement.X < 0f)
|
|
|
|
|
{
|
|
|
|
|
system.EnableThrustDirection(shuttleComponent, DirectionFlag.West);
|
|
|
|
|
system.DisableThrustDirection(shuttleComponent, DirectionFlag.East);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
system.EnableThrustDirection(shuttleComponent, DirectionFlag.East);
|
|
|
|
|
system.DisableThrustDirection(shuttleComponent, DirectionFlag.West);
|
|
|
|
|
}
|
2021-07-21 21:15:12 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO WHEN THIS ACTUALLY WORKS
|
|
|
|
|
speedCap = _shuttleDockSpeedCap * 10;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
2020-07-03 23:32:41 +02:00
|
|
|
}
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2021-07-21 21:15:12 +10:00
|
|
|
var velocity = physicsComponent.LinearVelocity;
|
2021-11-21 17:09:49 +11:00
|
|
|
var angVelocity = physicsComponent.AngularVelocity;
|
2021-07-21 21:15:12 +10:00
|
|
|
|
|
|
|
|
if (velocity.Length > speedCap)
|
|
|
|
|
{
|
|
|
|
|
physicsComponent.LinearVelocity = velocity.Normalized * speedCap;
|
|
|
|
|
}
|
2021-11-21 17:09:49 +11:00
|
|
|
|
|
|
|
|
/* TODO: Need to suss something out but this PR already BEEG
|
|
|
|
|
if (angVelocity > speedCap)
|
|
|
|
|
{
|
|
|
|
|
physicsComponent.AngularVelocity = speedCap;
|
|
|
|
|
}
|
|
|
|
|
*/
|
2020-06-24 02:21:20 +02:00
|
|
|
}
|
2020-05-23 01:23:36 +02:00
|
|
|
|
2021-03-08 04:09:59 +11:00
|
|
|
protected override void HandleFootsteps(IMoverComponent mover, IMobMoverComponent mobMover)
|
2020-06-24 02:21:20 +02:00
|
|
|
{
|
2021-04-21 20:14:40 +10:00
|
|
|
if (!mover.Owner.HasTag("FootstepSound")) return;
|
|
|
|
|
|
2020-06-24 02:21:20 +02:00
|
|
|
var transform = mover.Owner.Transform;
|
2021-04-21 20:14:40 +10:00
|
|
|
var coordinates = transform.Coordinates;
|
|
|
|
|
var gridId = coordinates.GetGridId(EntityManager);
|
|
|
|
|
var distanceNeeded = mover.Sprinting ? StepSoundMoveDistanceRunning : StepSoundMoveDistanceWalking;
|
|
|
|
|
|
2020-06-24 02:21:20 +02:00
|
|
|
// Handle footsteps.
|
2021-04-21 20:14:40 +10:00
|
|
|
if (_mapManager.GridExists(gridId))
|
2020-06-24 02:21:20 +02:00
|
|
|
{
|
|
|
|
|
// Can happen when teleporting between grids.
|
2021-04-21 20:14:40 +10:00
|
|
|
if (!coordinates.TryDistance(EntityManager, mobMover.LastPosition, out var distance) ||
|
|
|
|
|
distance > distanceNeeded)
|
2020-09-06 16:11:53 +02:00
|
|
|
{
|
2021-04-21 20:14:40 +10:00
|
|
|
mobMover.StepSoundDistance = distanceNeeded;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mobMover.StepSoundDistance += distance;
|
2020-09-06 16:11:53 +02:00
|
|
|
}
|
2019-04-04 16:18:43 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-04-21 20:14:40 +10:00
|
|
|
// In space no one can hear you squeak
|
|
|
|
|
return;
|
2019-04-04 16:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-21 20:14:40 +10:00
|
|
|
DebugTools.Assert(gridId != GridId.Invalid);
|
|
|
|
|
mobMover.LastPosition = coordinates;
|
2020-05-23 17:18:32 +02:00
|
|
|
|
2021-04-21 20:14:40 +10:00
|
|
|
if (mobMover.StepSoundDistance < distanceNeeded) return;
|
2020-05-23 17:18:32 +02:00
|
|
|
|
2021-04-21 20:14:40 +10:00
|
|
|
mobMover.StepSoundDistance -= distanceNeeded;
|
|
|
|
|
|
|
|
|
|
if (mover.Owner.TryGetComponent<InventoryComponent>(out var inventory)
|
|
|
|
|
&& inventory.TryGetSlotItem<ItemComponent>(EquipmentSlotDefines.Slots.SHOES, out var item)
|
|
|
|
|
&& item.Owner.TryGetComponent<FootstepModifierComponent>(out var modifier))
|
|
|
|
|
{
|
|
|
|
|
modifier.PlayFootstep();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PlayFootstepSound(mover.Owner, gridId, coordinates, mover.Sprinting);
|
2020-04-18 12:10:50 +02:00
|
|
|
}
|
2019-04-04 16:18:43 +02:00
|
|
|
}
|
2019-04-05 02:04:34 +02:00
|
|
|
|
2021-04-21 20:14:40 +10:00
|
|
|
private void PlayFootstepSound(IEntity mover, GridId gridId, EntityCoordinates coordinates, bool sprinting)
|
2019-04-05 02:04:34 +02:00
|
|
|
{
|
2021-04-21 20:14:40 +10:00
|
|
|
var grid = _mapManager.GetGrid(gridId);
|
2019-04-28 22:08:27 -07:00
|
|
|
var tile = grid.GetTileRef(coordinates);
|
2019-04-05 02:04:34 +02:00
|
|
|
|
2021-04-21 20:14:40 +10:00
|
|
|
if (tile.IsSpace(_tileDefinitionManager)) return;
|
|
|
|
|
|
2020-11-22 15:02:39 +01:00
|
|
|
// If the coordinates have a FootstepModifier component
|
|
|
|
|
// i.e. component that emit sound on footsteps emit that sound
|
2021-07-10 17:35:33 +02:00
|
|
|
string? soundToPlay = null;
|
2021-04-28 10:49:37 -07:00
|
|
|
foreach (var maybeFootstep in grid.GetAnchoredEntities(tile.GridIndices))
|
2019-04-05 02:04:34 +02:00
|
|
|
{
|
2021-09-28 13:35:29 +02:00
|
|
|
if (EntityManager.TryGetComponent(maybeFootstep, out FootstepModifierComponent? footstep))
|
2019-04-05 02:04:34 +02:00
|
|
|
{
|
2021-07-31 19:52:33 +02:00
|
|
|
soundToPlay = footstep.SoundCollection.GetSound();
|
2019-04-05 02:04:34 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-22 15:02:39 +01:00
|
|
|
// if there is no FootstepModifierComponent, determine sound based on tiles
|
2021-07-10 17:35:33 +02:00
|
|
|
if (soundToPlay == null)
|
2019-04-05 02:04:34 +02:00
|
|
|
{
|
|
|
|
|
// Walking on a tile.
|
2020-06-24 02:21:20 +02:00
|
|
|
var def = (ContentTileDefinition) _tileDefinitionManager[tile.Tile.TypeId];
|
2021-11-12 11:22:36 +01:00
|
|
|
soundToPlay = def.FootstepSounds?.GetSound();
|
2021-08-10 16:18:57 -07:00
|
|
|
if (string.IsNullOrEmpty(soundToPlay))
|
|
|
|
|
return;
|
2019-04-05 02:04:34 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-10 17:35:33 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(soundToPlay))
|
2019-08-10 22:17:49 +10:00
|
|
|
{
|
2021-07-10 17:35:33 +02:00
|
|
|
Logger.ErrorS("sound", $"Unable to find sound in {nameof(PlayFootstepSound)}");
|
2021-04-21 20:14:40 +10:00
|
|
|
return;
|
2019-08-10 22:17:49 +10:00
|
|
|
}
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2021-04-21 20:14:40 +10:00
|
|
|
SoundSystem.Play(
|
|
|
|
|
Filter.Pvs(coordinates),
|
2021-07-10 17:35:33 +02:00
|
|
|
soundToPlay,
|
2021-05-12 14:50:02 +02:00
|
|
|
mover.Transform.Coordinates,
|
2021-04-21 20:14:40 +10:00
|
|
|
sprinting ? AudioParams.Default.WithVolume(0.75f) : null);
|
|
|
|
|
}
|
2019-04-04 16:18:43 +02:00
|
|
|
}
|
|
|
|
|
}
|