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

improve GetInStation filter #33405

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
51 changes: 38 additions & 13 deletions Content.Server/Station/Systems/StationSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,27 +211,39 @@ public Filter GetInOwningStation(EntityUid source, float range = 32f)
/// </summary>
public Filter GetInStation(StationDataComponent dataComponent, float range = 32f)
{
// Could also use circles if you wanted.
var bounds = new ValueList<Box2>(dataComponent.Grids.Count);
var filter = Filter.Empty();
var mapIds = new ValueList<MapId>();
var xformQuery = GetEntityQuery<TransformComponent>();

// First collect all valid map IDs where station grids exist
foreach (var gridUid in dataComponent.Grids)
{
if (!TryComp(gridUid, out MapGridComponent? grid) ||
!xformQuery.TryGetComponent(gridUid, out var xform))
if (!xformQuery.TryGetComponent(gridUid, out var xform))
continue;

var mapId = xform.MapID;
var position = _transform.GetWorldPosition(xform, xformQuery);
var bound = grid.LocalAABB.Enlarged(range).Translated(position);

bounds.Add(bound);
if (!mapIds.Contains(mapId))
{
mapIds.Add(xform.MapID);
}
mapIds.Add(mapId);
}

// Cache the rotated bounds for each grid
var gridBounds = new List<(Box2Rotated bounds, MapId mapId)>();
foreach (var gridUid in dataComponent.Grids)
{
if (!TryComp(gridUid, out MapGridComponent? grid) ||
!xformQuery.TryGetComponent(gridUid, out var gridXform))
continue;

var worldRotation = _transform.GetWorldRotation(gridXform, xformQuery);
var localBounds = grid.LocalAABB.Enlarged(range);

// Create a rotated box using the grid's transform
var rotatedBounds = new Box2Rotated(
localBounds,
worldRotation,
_transform.GetWorldPosition(gridXform, xformQuery));

gridBounds.Add((rotatedBounds, gridXform.MapID));
}

foreach (var session in Filter.GetAllPlayers(_player))
Expand All @@ -245,11 +257,24 @@ public Filter GetInStation(StationDataComponent dataComponent, float range = 32f
if (!mapIds.Contains(mapId))
continue;

// Check if the player is directly on any station grid
var gridUid = xform.GridUid;
if (gridUid != null && dataComponent.Grids.Contains(gridUid.Value))
{
filter.AddPlayer(session);
continue;
}

// If not directly on a grid, check against cached rotated bounds
var position = _transform.GetWorldPosition(xform, xformQuery);

foreach (var bound in bounds)
foreach (var (bounds, boundsMapId) in gridBounds)
{
if (!bound.Contains(position))
// Skip bounds on different maps
if (boundsMapId != mapId)
continue;

if (!bounds.Contains(position))
continue;

filter.AddPlayer(session);
Expand Down
Loading