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

Conveyor optimisations #33870

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
35 changes: 16 additions & 19 deletions Content.Server/Physics/Controllers/ConveyorController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Content.Server.DeviceLinking.Events;
using Content.Server.DeviceLinking.Systems;
using Content.Server.Materials;
using Content.Server.Power.Components;
using Content.Shared.Conveyor;
using Content.Shared.Destructible;
using Content.Shared.Maps;
Expand All @@ -10,7 +9,6 @@
using Content.Shared.Power;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Systems;

namespace Content.Server.Physics.Controllers;
Expand All @@ -20,7 +18,6 @@ public sealed class ConveyorController : SharedConveyorController
[Dependency] private readonly FixtureSystem _fixtures = default!;
[Dependency] private readonly DeviceLinkSystem _signalSystem = default!;
[Dependency] private readonly MaterialReclaimerSystem _materialReclaimer = default!;
[Dependency] private readonly SharedBroadphaseSystem _broadphase = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;

public override void Initialize()
Expand All @@ -40,7 +37,7 @@ private void OnInit(EntityUid uid, ConveyorComponent component, ComponentInit ar
{
_signalSystem.EnsureSinkPorts(uid, component.ReversePort, component.ForwardPort, component.OffPort);

if (TryComp<PhysicsComponent>(uid, out var physics))
if (PhysicsQuery.TryComp(uid, out var physics))
{
var shape = new PolygonShape();
shape.SetAsBox(0.55f, 0.55f);
Expand All @@ -57,7 +54,7 @@ private void OnConveyorShutdown(EntityUid uid, ConveyorComponent component, Comp
if (MetaData(uid).EntityLifeStage >= EntityLifeStage.Terminating)
return;

if (!TryComp<PhysicsComponent>(uid, out var physics))
if (!PhysicsQuery.TryComp(uid, out var physics))
return;

_fixtures.DestroyFixture(uid, ConveyorFixture, body: physics);
Expand Down Expand Up @@ -87,13 +84,11 @@ private void OnSignalReceived(EntityUid uid, ConveyorComponent component, ref Si

else if (args.Port == component.ForwardPort)
{
AwakenEntities(uid, component);
SetState(uid, ConveyorState.Forward, component);
}

else if (args.Port == component.ReversePort)
{
AwakenEntities(uid, component);
SetState(uid, ConveyorState.Reverse, component);
}
}
Expand All @@ -108,38 +103,40 @@ private void SetState(EntityUid uid, ConveyorState state, ConveyorComponent? com

component.State = state;

if (TryComp<PhysicsComponent>(uid, out var physics))
_broadphase.RegenerateContacts(uid, physics);
if (state != ConveyorState.Off)
{
WakeConveyed(uid);
}

UpdateAppearance(uid, component);
Dirty(uid, component);
}

/// <summary>
/// Awakens sleeping entities on the conveyor belt's tile when it's turned on.
/// Fixes an issue where non-hard/sleeping entities refuse to wake up + collide if a belt is turned off and on again.
/// Need this as we might activate under CollisionWake entities and need to forcefully check them.
/// </summary>
private void AwakenEntities(EntityUid uid, ConveyorComponent component)
protected override void AwakenConveyor(Entity<TransformComponent?> ent)
{
var xformQuery = GetEntityQuery<TransformComponent>();
var bodyQuery = GetEntityQuery<PhysicsComponent>();

if (!xformQuery.TryGetComponent(uid, out var xform))
if (!XformQuery.Resolve(ent.Owner, ref ent.Comp))
return;

var xform = ent.Comp;

var beltTileRef = xform.Coordinates.GetTileRef(EntityManager, MapManager);

if (beltTileRef != null)
{
var intersecting = Lookup.GetLocalEntitiesIntersecting(beltTileRef.Value, 0f);
Intersecting.Clear();
Lookup.GetLocalEntitiesIntersecting(beltTileRef.Value.GridUid, beltTileRef.Value.GridIndices, Intersecting, 0f, flags: LookupFlags.Dynamic | LookupFlags.Sundries | LookupFlags.Approximate);

foreach (var entity in intersecting)
foreach (var entity in Intersecting)
{
if (!bodyQuery.TryGetComponent(entity, out var physics))
if (!PhysicsQuery.TryGetComponent(entity, out var physics))
continue;

if (physics.BodyType != BodyType.Static)
Physics.WakeBody(entity, body: physics);
PhysicsSystem.WakeBody(entity, body: physics);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions Content.Shared/Conveyor/ActiveConveyedComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Robust.Shared.GameStates;

namespace Content.Shared.Conveyor;

/// <summary>
/// Indicates this entity is actively being conveyed.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed partial class ActiveConveyedComponent : Component
{

}
11 changes: 4 additions & 7 deletions Content.Shared/Conveyor/ConveyedComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
namespace Content.Shared.Conveyor;

/// <summary>
/// Indicates this entity is currently being conveyed.
/// Indicates this entity is currently contacting a conveyor and will subscribe to events as appropriate.
/// For entities actively being conveyed see <see cref="ActiveConveyedComponent"/>.
/// </summary>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class ConveyedComponent : Component
{
[ViewVariables, AutoNetworkedField]
public List<EntityUid> Colliding = new();
}
[RegisterComponent, NetworkedComponent]
public sealed partial class ConveyedComponent : Component;
Loading
Loading