2023-05-07 08:07:24 +02:00
|
|
|
using Content.Server.DeviceLinking.Events;
|
|
|
|
|
using Content.Server.DeviceLinking.Systems;
|
2023-04-10 00:38:20 -04:00
|
|
|
using Content.Server.Materials;
|
2022-08-06 16:32:38 +10:00
|
|
|
using Content.Server.Power.Components;
|
2022-01-31 01:30:15 +11:00
|
|
|
using Content.Shared.Conveyor;
|
2022-09-29 20:44:28 -04:00
|
|
|
using Content.Shared.Maps;
|
2022-08-06 16:32:38 +10:00
|
|
|
using Content.Shared.Physics;
|
2023-02-13 07:20:39 -05:00
|
|
|
using Content.Shared.Physics.Controllers;
|
2021-03-08 04:09:59 +11:00
|
|
|
using Robust.Shared.Physics;
|
2022-08-06 16:32:38 +10:00
|
|
|
using Robust.Shared.Physics.Collision.Shapes;
|
2022-09-14 17:26:26 +10:00
|
|
|
using Robust.Shared.Physics.Components;
|
|
|
|
|
using Robust.Shared.Physics.Systems;
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
namespace Content.Server.Physics.Controllers;
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
public sealed class ConveyorController : SharedConveyorController
|
|
|
|
|
{
|
|
|
|
|
[Dependency] private readonly FixtureSystem _fixtures = default!;
|
2023-05-07 08:07:24 +02:00
|
|
|
[Dependency] private readonly DeviceLinkSystem _signalSystem = default!;
|
2023-04-10 00:38:20 -04:00
|
|
|
[Dependency] private readonly MaterialReclaimerSystem _materialReclaimer = default!;
|
2023-02-13 07:20:39 -05:00
|
|
|
[Dependency] private readonly SharedBroadphaseSystem _broadphase = default!;
|
|
|
|
|
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
|
2021-11-13 14:40:10 +11:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
UpdatesAfter.Add(typeof(MoverController));
|
|
|
|
|
SubscribeLocalEvent<ConveyorComponent, ComponentInit>(OnInit);
|
|
|
|
|
SubscribeLocalEvent<ConveyorComponent, ComponentShutdown>(OnConveyorShutdown);
|
2022-08-06 16:32:38 +10:00
|
|
|
|
|
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
SubscribeLocalEvent<ConveyorComponent, SignalReceivedEvent>(OnSignalReceived);
|
|
|
|
|
SubscribeLocalEvent<ConveyorComponent, PowerChangedEvent>(OnPowerChanged);
|
2022-08-06 16:32:38 +10:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
base.Initialize();
|
|
|
|
|
}
|
2022-08-06 16:32:38 +10:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
private void OnInit(EntityUid uid, ConveyorComponent component, ComponentInit args)
|
|
|
|
|
{
|
2023-05-07 08:07:24 +02:00
|
|
|
_signalSystem.EnsureSinkPorts(uid, component.ReversePort, component.ForwardPort, component.OffPort);
|
2022-08-06 16:32:38 +10:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
if (TryComp<PhysicsComponent>(uid, out var physics))
|
2022-08-06 16:32:38 +10:00
|
|
|
{
|
2023-02-13 07:20:39 -05:00
|
|
|
var shape = new PolygonShape();
|
|
|
|
|
shape.SetAsBox(0.55f, 0.55f);
|
2022-08-06 16:32:38 +10:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
_fixtures.TryCreateFixture(uid, shape, ConveyorFixture,
|
|
|
|
|
collisionLayer: (int) (CollisionGroup.LowImpassable | CollisionGroup.MidImpassable |
|
|
|
|
|
CollisionGroup.Impassable), hard: false, body: physics);
|
2022-08-06 16:32:38 +10:00
|
|
|
|
|
|
|
|
}
|
2023-02-13 07:20:39 -05:00
|
|
|
}
|
2022-08-06 16:32:38 +10:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
private void OnConveyorShutdown(EntityUid uid, ConveyorComponent component, ComponentShutdown args)
|
|
|
|
|
{
|
|
|
|
|
if (MetaData(uid).EntityLifeStage >= EntityLifeStage.Terminating)
|
|
|
|
|
return;
|
2022-08-06 16:32:38 +10:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
RemComp<ActiveConveyorComponent>(uid);
|
2022-09-29 20:44:28 -04:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
if (!TryComp<PhysicsComponent>(uid, out var physics))
|
|
|
|
|
return;
|
2022-09-29 20:44:28 -04:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
_fixtures.DestroyFixture(uid, ConveyorFixture, body: physics);
|
|
|
|
|
}
|
2022-09-29 20:44:28 -04:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
private void OnPowerChanged(EntityUid uid, ConveyorComponent component, ref PowerChangedEvent args)
|
|
|
|
|
{
|
|
|
|
|
component.Powered = args.Powered;
|
|
|
|
|
UpdateAppearance(uid, component);
|
|
|
|
|
Dirty(component);
|
|
|
|
|
}
|
2022-09-29 20:44:28 -04:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
private void UpdateAppearance(EntityUid uid, ConveyorComponent component)
|
|
|
|
|
{
|
|
|
|
|
_appearance.SetData(uid, ConveyorVisuals.State, component.Powered ? component.State : ConveyorState.Off);
|
|
|
|
|
}
|
2022-09-29 20:44:28 -04:00
|
|
|
|
2023-05-07 08:07:24 +02:00
|
|
|
private void OnSignalReceived(EntityUid uid, ConveyorComponent component, ref SignalReceivedEvent args)
|
2023-02-13 07:20:39 -05:00
|
|
|
{
|
|
|
|
|
if (args.Port == component.OffPort)
|
|
|
|
|
SetState(uid, ConveyorState.Off, component);
|
2022-09-29 20:44:28 -04:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
else if (args.Port == component.ForwardPort)
|
2022-08-06 16:32:38 +10:00
|
|
|
{
|
2023-02-13 07:20:39 -05:00
|
|
|
AwakenEntities(uid, component);
|
|
|
|
|
SetState(uid, ConveyorState.Forward, component);
|
2022-08-06 16:32:38 +10:00
|
|
|
}
|
|
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
else if (args.Port == component.ReversePort)
|
2022-08-06 16:32:38 +10:00
|
|
|
{
|
2023-02-13 07:20:39 -05:00
|
|
|
AwakenEntities(uid, component);
|
|
|
|
|
SetState(uid, ConveyorState.Reverse, component);
|
2022-08-06 16:32:38 +10:00
|
|
|
}
|
2023-02-13 07:20:39 -05:00
|
|
|
}
|
2022-08-06 16:32:38 +10:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
private void SetState(EntityUid uid, ConveyorState state, ConveyorComponent? component = null)
|
|
|
|
|
{
|
|
|
|
|
if (!Resolve(uid, ref component))
|
|
|
|
|
return;
|
2022-01-31 01:30:15 +11:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
component.State = state;
|
2022-01-31 01:30:15 +11:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
if (TryComp<PhysicsComponent>(uid, out var physics))
|
2023-05-28 23:22:44 +10:00
|
|
|
_broadphase.RegenerateContacts(uid, physics);
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2023-04-10 00:38:20 -04:00
|
|
|
_materialReclaimer.SetReclaimerEnabled(uid, component.State != ConveyorState.Off);
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
UpdateAppearance(uid, component);
|
|
|
|
|
Dirty(component);
|
|
|
|
|
}
|
2022-01-31 01:30:15 +11:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
/// <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.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void AwakenEntities(EntityUid uid, ConveyorComponent component)
|
|
|
|
|
{
|
|
|
|
|
var xformQuery = GetEntityQuery<TransformComponent>();
|
|
|
|
|
var bodyQuery = GetEntityQuery<PhysicsComponent>();
|
2022-03-02 14:32:33 -08:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
if (!xformQuery.TryGetComponent(uid, out var xform))
|
|
|
|
|
return;
|
2022-03-02 14:32:33 -08:00
|
|
|
|
2023-05-16 22:55:22 +10:00
|
|
|
var beltTileRef = xform.Coordinates.GetTileRef(EntityManager, MapManager);
|
2022-01-31 01:30:15 +11:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
if (beltTileRef != null)
|
2022-01-31 01:30:15 +11:00
|
|
|
{
|
2024-01-14 19:10:38 +11:00
|
|
|
var intersecting = Lookup.GetLocalEntitiesIntersecting(beltTileRef.Value, 0f);
|
2022-01-31 01:30:15 +11:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
foreach (var entity in intersecting)
|
2022-01-31 01:30:15 +11:00
|
|
|
{
|
2023-02-13 07:20:39 -05:00
|
|
|
if (!bodyQuery.TryGetComponent(entity, out var physics))
|
2022-08-06 16:32:38 +10:00
|
|
|
continue;
|
2022-01-31 01:30:15 +11:00
|
|
|
|
2023-02-13 07:20:39 -05:00
|
|
|
if (physics.BodyType != BodyType.Static)
|
2023-05-16 22:55:22 +10:00
|
|
|
Physics.WakeBody(entity, body: physics);
|
2022-01-31 01:30:15 +11:00
|
|
|
}
|
2021-03-08 04:09:59 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|