Files
tbd-station-14/Content.Server/Shuttles/Systems/ShuttleConsoleSystem.cs
metalgearsloth 1e82d0c7ed Add docking window to shuttle consoles (#8756)
* lewd

* A

* Realtime

* Sleepy dork

* Draw radar position

* Accurate infiltrator

* experiments

* Better drawing

* Labels

* I need aan adult

* Cleanup

* Show toggles

* display I guess

* A

* fix

* fix

* cleanupsies

* Bit more polish

* Make sure mass scanners actually work

* Remove dummy state

* fren

* opposite

* aghost crash

* comment

* What's in a name

* woops

* Show docking ports

* Dock highlighting

* Drawing dock

* Shitty docks

* Lots of docking drawing

* Autodock working

* dork

* More graceful shutdown

* zoomies

* Lines and distance change

* revert

* Good enough

* cleanup

* Fix default range

* Dock UI and loc update

* Update on undock

* Loc fixes
2022-06-16 15:28:16 +10:00

338 lines
12 KiB
C#

using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Server.Shuttles.Components;
using Content.Server.Shuttles.Events;
using Content.Server.UserInterface;
using Content.Shared.ActionBlocker;
using Content.Shared.Alert;
using Content.Shared.Popups;
using Content.Shared.Shuttles.BUIStates;
using Content.Shared.Shuttles.Components;
using Content.Shared.Shuttles.Events;
using Content.Shared.Shuttles.Systems;
using Content.Shared.Tag;
using Robust.Server.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Utility;
namespace Content.Server.Shuttles.Systems
{
public sealed class ShuttleConsoleSystem : SharedShuttleConsoleSystem
{
[Dependency] private readonly ActionBlockerSystem _blocker = default!;
[Dependency] private readonly AlertsSystem _alertsSystem = default!;
[Dependency] private readonly TagSystem _tags = default!;
[Dependency] private readonly UserInterfaceSystem _ui = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ShuttleConsoleComponent, ComponentShutdown>(OnConsoleShutdown);
SubscribeLocalEvent<ShuttleConsoleComponent, PowerChangedEvent>(OnConsolePowerChange);
SubscribeLocalEvent<ShuttleConsoleComponent, AnchorStateChangedEvent>(OnConsoleAnchorChange);
SubscribeLocalEvent<ShuttleConsoleComponent, ActivatableUIOpenAttemptEvent>(OnConsoleUIOpenAttempt);
SubscribeLocalEvent<ShuttleConsoleComponent, ShuttleModeRequestMessage>(OnModeRequest);
SubscribeLocalEvent<ShuttleConsoleComponent, BoundUIClosedEvent>(OnConsoleUIClose);
SubscribeLocalEvent<DockEvent>(OnDock);
SubscribeLocalEvent<UndockEvent>(OnUndock);
SubscribeLocalEvent<PilotComponent, MoveEvent>(HandlePilotMove);
SubscribeLocalEvent<PilotComponent, ComponentGetState>(OnGetState);
}
private void OnDock(DockEvent ev)
{
RefreshShuttleConsoles();
}
private void OnUndock(UndockEvent ev)
{
RefreshShuttleConsoles();
}
/// <summary>
/// Refreshes all of the data for shuttle consoles.
/// </summary>
public void RefreshShuttleConsoles()
{
// TODO: Should really call this per shuttle in some instances.
var docks = GetAllDocks();
foreach (var comp in EntityQuery<ShuttleConsoleComponent>(true))
{
UpdateState(comp, docks);
}
}
/// <summary>
/// Stop piloting if the window is closed.
/// </summary>
private void OnConsoleUIClose(EntityUid uid, ShuttleConsoleComponent component, BoundUIClosedEvent args)
{
if ((ShuttleConsoleUiKey) args.UiKey != ShuttleConsoleUiKey.Key ||
args.Session.AttachedEntity is not {} user) return;
// In case they D/C should still clean them up.
foreach (var comp in EntityQuery<AutoDockComponent>(true))
{
comp.Requesters.Remove(user);
}
RemovePilot(user);
}
private void OnConsoleUIOpenAttempt(EntityUid uid, ShuttleConsoleComponent component, ActivatableUIOpenAttemptEvent args)
{
if (!TryPilot(args.User, uid))
args.Cancel();
}
private void OnConsoleAnchorChange(EntityUid uid, ShuttleConsoleComponent component, ref AnchorStateChangedEvent args)
{
UpdateState(component);
}
private void OnConsolePowerChange(EntityUid uid, ShuttleConsoleComponent component, PowerChangedEvent args)
{
UpdateState(component);
}
private bool TryPilot(EntityUid user, EntityUid uid)
{
if (!_tags.HasTag(user, "CanPilot") ||
!TryComp<ShuttleConsoleComponent>(uid, out var component) ||
!this.IsPowered(uid, EntityManager) ||
!Transform(uid).Anchored ||
!_blocker.CanInteract(user, uid))
{
return false;
}
var pilotComponent = EntityManager.EnsureComponent<PilotComponent>(user);
var console = pilotComponent.Console;
if (console != null)
{
RemovePilot(pilotComponent);
if (console == component)
{
return false;
}
}
AddPilot(user, component);
return true;
}
private void OnGetState(EntityUid uid, PilotComponent component, ref ComponentGetState args)
{
args.State = new PilotComponentState(component.Console?.Owner);
}
/// <summary>
/// Client is requesting a change in the shuttle's driving mode.
/// </summary>
private void OnModeRequest(EntityUid uid, ShuttleConsoleComponent component, ShuttleModeRequestMessage args)
{
if (args.Session.AttachedEntity is not { } player ||
!TryComp<PilotComponent>(player, out var pilot) ||
!TryComp<TransformComponent>(player, out var xform) ||
pilot.Console is not ShuttleConsoleComponent console) return;
if (!console.SubscribedPilots.Contains(pilot) ||
!TryComp<ShuttleComponent>(xform.GridEntityId, out var shuttle)) return;
SetShuttleMode(args.Mode, console, shuttle);
}
/// <summary>
/// Sets the shuttle's movement mode. Does minimal revalidation.
/// </summary>
private void SetShuttleMode(ShuttleMode mode, ShuttleConsoleComponent consoleComponent,
ShuttleComponent shuttleComponent, TransformComponent? consoleXform = null)
{
// Re-validate
if (!this.IsPowered(consoleComponent.Owner, EntityManager) ||
!Resolve(consoleComponent.Owner, ref consoleXform) ||
!consoleXform.Anchored ||
consoleXform.GridID != Transform(shuttleComponent.Owner).GridID)
{
UpdateState(consoleComponent);
return;
}
shuttleComponent.Mode = mode;
switch (mode)
{
case ShuttleMode.Strafing:
break;
case ShuttleMode.Cruise:
break;
default:
throw new ArgumentOutOfRangeException();
}
UpdateState(consoleComponent);
}
/// <summary>
/// Returns the position and angle of all dockingcomponents.
/// </summary>
private List<DockingInterfaceState> GetAllDocks()
{
// TODO: NEED TO MAKE SURE THIS UPDATES ON ANCHORING CHANGES!
var result = new List<DockingInterfaceState>();
foreach (var (comp, xform) in EntityQuery<DockingComponent, TransformComponent>(true))
{
if (xform.ParentUid != xform.GridUid) continue;
var state = new DockingInterfaceState()
{
Coordinates = xform.Coordinates,
Angle = xform.LocalRotation,
Entity = comp.Owner,
Connected = comp.Docked,
};
result.Add(state);
}
return result;
}
private void UpdateState(ShuttleConsoleComponent component, List<DockingInterfaceState>? docks = null)
{
TryComp<RadarConsoleComponent>(component.Owner, out var radar);
var range = radar?.MaxRange ?? 0f;
TryComp<ShuttleComponent>(Transform(component.Owner).GridUid, out var shuttle);
var mode = shuttle?.Mode ?? ShuttleMode.Cruise;
docks ??= GetAllDocks();
_ui.GetUiOrNull(component.Owner, ShuttleConsoleUiKey.Key)
?.SetState(new ShuttleConsoleBoundInterfaceState(
mode,
range,
component.Owner,
docks));
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var toRemove = new RemQueue<PilotComponent>();
foreach (var comp in EntityManager.EntityQuery<PilotComponent>())
{
if (comp.Console == null) continue;
if (!_blocker.CanInteract(comp.Owner, comp.Console.Owner))
{
toRemove.Add(comp);
}
}
foreach (var comp in toRemove)
{
RemovePilot(comp);
}
}
/// <summary>
/// If pilot is moved then we'll stop them from piloting.
/// </summary>
private void HandlePilotMove(EntityUid uid, PilotComponent component, ref MoveEvent args)
{
if (component.Console == null || component.Position == null)
{
DebugTools.Assert(component.Position == null && component.Console == null);
EntityManager.RemoveComponent<PilotComponent>(uid);
return;
}
if (args.NewPosition.TryDistance(EntityManager, component.Position.Value, out var distance) &&
distance < PilotComponent.BreakDistance) return;
RemovePilot(component);
}
protected override void HandlePilotShutdown(EntityUid uid, PilotComponent component, ComponentShutdown args)
{
base.HandlePilotShutdown(uid, component, args);
RemovePilot(component);
}
private void OnConsoleShutdown(EntityUid uid, ShuttleConsoleComponent component, ComponentShutdown args)
{
ClearPilots(component);
}
public void AddPilot(EntityUid entity, ShuttleConsoleComponent component)
{
if (!EntityManager.TryGetComponent(entity, out PilotComponent? pilotComponent) ||
component.SubscribedPilots.Contains(pilotComponent))
{
return;
}
if (TryComp<SharedEyeComponent>(entity, out var eye))
{
eye.Zoom = component.Zoom;
}
component.SubscribedPilots.Add(pilotComponent);
_alertsSystem.ShowAlert(entity, AlertType.PilotingShuttle);
pilotComponent.Console = component;
ActionBlockerSystem.UpdateCanMove(entity);
pilotComponent.Position = EntityManager.GetComponent<TransformComponent>(entity).Coordinates;
Dirty(pilotComponent);
}
public void RemovePilot(PilotComponent pilotComponent)
{
var console = pilotComponent.Console;
if (console is not ShuttleConsoleComponent helmsman) return;
pilotComponent.Console = null;
pilotComponent.Position = null;
if (TryComp<SharedEyeComponent>(pilotComponent.Owner, out var eye))
{
eye.Zoom = new(1.0f, 1.0f);
}
if (!helmsman.SubscribedPilots.Remove(pilotComponent)) return;
_alertsSystem.ClearAlert(pilotComponent.Owner, AlertType.PilotingShuttle);
pilotComponent.Owner.PopupMessage(Loc.GetString("shuttle-pilot-end"));
if (pilotComponent.LifeStage < ComponentLifeStage.Stopping)
EntityManager.RemoveComponent<PilotComponent>(pilotComponent.Owner);
}
public void RemovePilot(EntityUid entity)
{
if (!EntityManager.TryGetComponent(entity, out PilotComponent? pilotComponent)) return;
RemovePilot(pilotComponent);
}
public void ClearPilots(ShuttleConsoleComponent component)
{
while (component.SubscribedPilots.TryGetValue(0, out var pilot))
{
RemovePilot(pilot);
}
}
}
}