Anchor only on station component (#43716)

* shared stuff

* server stuff

* locale stuff

* use the comp for the method call

* pass transform comp

* use query instead of GetStations

* return bool inside the while loop

* empty commit

* Update Content.Shared/Station/SharedStationSystem.cs

---------

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
This commit is contained in:
Samuka 2026-04-26 17:08:34 -03:00 • committed by GitHub
parent 609488b80f
commit 03a40855be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 122 additions and 3 deletions

View file

@ -0,0 +1,39 @@
using System.Linq;
using Content.Server.Station.Systems;
using Content.Shared.Construction.Components;
using Robust.Server.GameObjects;
namespace Content.Server.Construction;
public sealed class AnchorOnlyOnStationSystem : EntitySystem
{
[Dependency] private readonly StationSystem _stationSystem = null!;
[Dependency] private readonly TransformSystem _transform = null!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GridSplitEvent>(OnGridSplit);
}
private void OnGridSplit(ref GridSplitEvent args)
{
var allGrids = args.NewGrids.ToList();
var query = AllEntityQuery<AnchorOnlyOnStationComponent, TransformComponent>();
while (query.MoveNext(out var ent, out var anchorOnlyOnStationComp, out var entXform))
{
if (entXform.GridUid == null)
continue;
if (!allGrids.Contains(entXform.GridUid.Value))
continue;
if (_stationSystem.IsOnStation(ent, anchorOnlyOnStationComp.OnlyCountLargestGrid))
continue;
_transform.Unanchor(ent, entXform);
}
}
}

View file

@ -0,0 +1,23 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Construction.Components;
/// <summary>
/// If a entity has this component it can only be anchored to the station
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed partial class AnchorOnlyOnStationComponent : Component
{
/// <summary>
/// Pop up message when you try to anchor the entity on any grid that isn't the station grid
/// </summary>
[DataField]
public LocId PopupMessageAnchorFail = "anchorable-fail-not-on-station";
/// <summary>
/// If true, it will only be able to be anchored on the largest grid of a station.
/// If false, it will only be able to anchored on any grid of a station.
/// </summary>
[DataField]
public bool OnlyCountLargestGrid = true;
}

View file

@ -39,6 +39,11 @@ namespace Content.Shared.Construction.Components
public EntityUid User { get; }
public EntityUid Tool { get; }
/// <summary>
/// This is shown to the player after the entity fails to anchor or unanchor as a popup
/// </summary>
public string? FailMessage;
/// <summary>
/// Extra delay to add to the do_after.
/// Add to this, don't replace it.
@ -46,10 +51,11 @@ namespace Content.Shared.Construction.Components
/// </summary>
public float Delay { get; set; } = 0f;
protected BaseAnchoredAttemptEvent(EntityUid user, EntityUid tool)
protected BaseAnchoredAttemptEvent(EntityUid user, EntityUid tool, string? failMessage = null)
{
User = user;
Tool = tool;
FailMessage = failMessage;
}
}

View file

@ -10,6 +10,7 @@ using Content.Shared.Interaction;
using Content.Shared.Movement.Pulling.Components;
using Content.Shared.Movement.Pulling.Systems;
using Content.Shared.Popups;
using Content.Shared.Station;
using Content.Shared.Tools.Components;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
@ -29,6 +30,7 @@ public sealed partial class AnchorableSystem : EntitySystem
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly PullingSystem _pulling = default!;
[Dependency] private readonly SharedMapSystem _map = default!;
[Dependency] private readonly SharedStationSystem _stationSystem = null!;
[Dependency] private readonly SharedToolSystem _tool = default!;
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
[Dependency] private readonly TagSystem _tagSystem = default!;
@ -49,6 +51,8 @@ public sealed partial class AnchorableSystem : EntitySystem
SubscribeLocalEvent<AnchorableComponent, ExaminedEvent>(OnAnchoredExamine);
SubscribeLocalEvent<AnchorableComponent, ComponentStartup>(OnAnchorStartup);
SubscribeLocalEvent<AnchorableComponent, AnchorStateChangedEvent>(OnAnchorStateChange);
SubscribeLocalEvent<AnchorOnlyOnStationComponent, AnchorAttemptEvent>(OnAnchorOnStation);
}
private void OnAnchorStartup(EntityUid uid, AnchorableComponent comp, ComponentStartup args)
@ -56,6 +60,15 @@ public sealed partial class AnchorableSystem : EntitySystem
_appearance.SetData(uid, AnchorVisuals.Anchored, Transform(uid).Anchored);
}
private void OnAnchorOnStation(Entity<AnchorOnlyOnStationComponent> ent, ref AnchorAttemptEvent args)
{
if (_stationSystem.IsOnStation(ent, ent.Comp.OnlyCountLargestGrid))
return;
args.FailMessage = Loc.GetString(ent.Comp.PopupMessageAnchorFail);
args.Cancel();
}
private void OnAnchorStateChange(EntityUid uid, AnchorableComponent comp, AnchorStateChangedEvent args)
{
_appearance.SetData(uid, AnchorVisuals.Anchored, args.Anchored);
@ -76,8 +89,12 @@ public sealed partial class AnchorableSystem : EntitySystem
if (!Resolve(usingUid, ref usingTool))
return;
if (!Valid(uid, userUid, usingUid, false))
if (!Valid(uid, userUid, usingUid, false, out var failMessage))
{
if (failMessage != null)
_popup.PopupClient(failMessage, uid, userUid);
return;
}
// Log unanchor attempt (server only)
_adminLogger.Add(LogType.Anchor, LogImpact.Low, $"{ToPrettyString(userUid):user} is trying to unanchor {ToPrettyString(uid):entity} from {transform.Coordinates:targetlocation}");
@ -227,8 +244,12 @@ public sealed partial class AnchorableSystem : EntitySystem
if (!Resolve(usingUid, ref usingTool))
return;
if (!Valid(uid, userUid, usingUid, true, anchorable, usingTool))
if (!Valid(uid, userUid, usingUid, true, out var failMessage, anchorable, usingTool))
{
if (failMessage != null)
_popup.PopupClient(Loc.GetString(failMessage), uid, userUid);
return;
}
// Log anchor attempt (server only)
_adminLogger.Add(LogType.Anchor, LogImpact.Low, $"{ToPrettyString(userUid):user} is trying to anchor {ToPrettyString(uid):entity} to {transform.Coordinates:targetlocation}");
@ -250,9 +271,12 @@ public sealed partial class AnchorableSystem : EntitySystem
EntityUid userUid,
EntityUid usingUid,
bool anchoring,
out string? failMessage,
AnchorableComponent? anchorable = null,
ToolComponent? usingTool = null)
{
failMessage = null;
if (!Resolve(uid, ref anchorable))
return false;
@ -276,6 +300,8 @@ public sealed partial class AnchorableSystem : EntitySystem
anchorable.Delay += attempt.Delay;
failMessage = attempt.FailMessage;
return !attempt.Cancelled;
}

View file

@ -175,4 +175,27 @@ public abstract partial class SharedStationSystem : EntitySystem
return null;
}
/// <summary>
/// Returns true if a entity's parent is the station, and false if the entity is not on the station
/// </summary>
/// <param name="entity">The entity to check if is on a grid</param>
/// <param name="onlyCountLargestGrid">If true, only the largest grid of each station will count, so if the entity is on
/// the ATS or a escape pod, it will return false</param>
public bool IsOnStation(EntityUid entity, bool onlyCountLargestGrid = true)
{
var entityParent = Transform(entity).ParentUid;
var query = EntityQueryEnumerator<StationDataComponent>();
while (query.MoveNext(out var uid, out var stationDataComp))
{
if (onlyCountLargestGrid && GetLargestGrid(uid) == entityParent)
return true;
if (!onlyCountLargestGrid && stationDataComp.Grids.Contains(entityParent))
return true;
}
return false;
}
}

View file

@ -1,3 +1,5 @@
anchorable-anchored = Anchored
anchorable-unanchored = Unanchored
anchorable-occupied = Tile occupied
anchorable-fail-not-on-station = This can only be anchored on a station!