makes conveyors to use machine linking & refactors machine linking a bit (#2464)

* makes conveyors to use machine linking & refactors machine linking a bit

* nullable errors

* temp commit, starting work on construction

* working recipies & graphs

* fixes crash

* makes items gravitate towards the center when on a conveyor

* makes conveyors take bool signal too

* ignores components clientside

* default arm
entitymanager
maxtransmitters
unsubscribe methods

* twowayLEVER

* _

* componentreference
struct

* yaml run
leverDefinitelyNotCopiedFromGirderNoNoNo dies today :(

* nullable

* no divide by 0

* making sloth happy

* space gone - happy?

* final fix

* yes

* adds item to lathe

* conveyor item -> conveyor assembly

* technology

* reviews ADRESSED

* Update Content.Shared/GameObjects/Verbs/VerbUtility.cs

Co-authored-by: Paul <ritter.paul1+git@googlemail.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
This commit is contained in:
Paul Ritter
2020-11-18 14:53:46 +01:00
committed by GitHub
parent 2cae1ac641
commit 501156f84c
24 changed files with 345 additions and 447 deletions

View File

@@ -1,13 +1,12 @@
#nullable enable
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Content.Server.GameObjects.Components.Interactable;
using Content.Server.GameObjects.Components.Items.Storage;
using Content.Server.GameObjects.Components.MachineLinking;
using Content.Server.GameObjects.Components.Power.ApcNetComponents;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components.Conveyor;
using Content.Shared.GameObjects.Components.Interactable;
using Content.Shared.GameObjects.Components.MachineLinking;
using Content.Shared.Interfaces.GameObjects.Components;
using Content.Shared.Physics;
using Content.Shared.Utility;
@@ -17,7 +16,6 @@ using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.GameObjects.Components.Map;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
@@ -26,16 +24,14 @@ using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Conveyor
{
[RegisterComponent]
public class ConveyorComponent : Component, IInteractUsing
public class ConveyorComponent : Component, ISignalReceiver<TwoWayLeverSignal>, ISignalReceiver<bool>
{
[Dependency] private readonly IEntityManager _entityManager = default!;
public override string Name => "Conveyor";
/// <summary>
/// The angle to move entities by in relation to the owner's rotation.
/// </summary>
[ViewVariables]
[ViewVariables(VVAccess.ReadWrite)]
private Angle _angle;
/// <summary>
@@ -66,8 +62,6 @@ namespace Content.Server.GameObjects.Components.Conveyor
}
}
private ConveyorGroup? _group = new ConveyorGroup();
/// <summary>
/// Calculates the angle in which entities on top of this conveyor
/// belt are pushed in
@@ -142,7 +136,7 @@ namespace Content.Server.GameObjects.Components.Conveyor
return;
}
var intersecting = _entityManager.GetEntitiesIntersecting(Owner, true);
var intersecting = Owner.EntityManager.GetEntitiesIntersecting(Owner, true);
var direction = GetAngle().ToVec();
foreach (var entity in intersecting)
@@ -155,107 +149,33 @@ namespace Content.Server.GameObjects.Components.Conveyor
if (entity.TryGetComponent(out IPhysicsComponent? physics))
{
var controller = physics.EnsureController<ConveyedController>();
controller.Move(direction, _speed);
controller.Move(direction, _speed, entity.Transform.WorldPosition - Owner.Transform.WorldPosition);
}
}
}
private async Task<bool> ToolUsed(IEntity user, ToolComponent tool)
{
if (!Owner.HasComponent<ItemComponent>() &&
await tool.UseTool(user, Owner, 0.5f, ToolQuality.Prying))
{
State = ConveyorState.Loose;
Owner.AddComponent<ItemComponent>();
_group?.RemoveConveyor(this);
Owner.RandomOffset(0.2f);
return true;
}
return false;
}
public void Sync(ConveyorGroup group)
{
_group = group;
if (State == ConveyorState.Loose)
{
return;
}
State = group.State == ConveyorState.Loose
? ConveyorState.Off
: group.State;
}
/// <summary>
/// Disconnects this conveyor from any switch.
/// </summary>
private void Disconnect()
{
_group?.RemoveConveyor(this);
_group = null;
State = ConveyorState.Off;
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataReadWriteFunction(
"switches",
new List<EntityUid>(),
ids =>
{
if (ids == null)
{
return;
}
foreach (var id in ids)
{
if (!Owner.EntityManager.TryGetEntity(id, out var @switch))
{
continue;
}
if (!@switch.TryGetComponent(out ConveyorSwitchComponent? component))
{
continue;
}
component.Connect(this);
}
},
() => _group?.Switches.Select(@switch => @switch.Owner.Uid).ToList());
serializer.DataField(ref _angle, "angle", 0);
serializer.DataField(ref _speed, "speed", 2);
}
public override void OnRemove()
public void TriggerSignal(TwoWayLeverSignal signal)
{
base.OnRemove();
Disconnect();
State = signal switch
{
TwoWayLeverSignal.Left => ConveyorState.Reversed,
TwoWayLeverSignal.Middle => ConveyorState.Off,
TwoWayLeverSignal.Right => ConveyorState.Forward,
_ => ConveyorState.Off
};
}
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
public void TriggerSignal(bool signal)
{
if (eventArgs.Using.TryGetComponent(out ConveyorSwitchComponent? conveyorSwitch))
{
conveyorSwitch.Connect(this, eventArgs.User);
return true;
}
if (eventArgs.Using.TryGetComponent(out ToolComponent? tool))
{
return await ToolUsed(eventArgs.User, tool);
}
return false;
State = signal ? ConveyorState.Forward : ConveyorState.Off;
}
}
}

View File

@@ -0,0 +1,13 @@
using Robust.Shared.GameObjects;
namespace Content.Server.GameObjects.Components.Conveyor
{
/// <summary>
/// Dummy component for construction graph
/// </summary>
[RegisterComponent]
public class ConveyorAssemblyComponent : Component
{
public override string Name => "ConveyorAssembly";
}
}

View File

@@ -1,219 +0,0 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components.Conveyor;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Conveyor
{
[RegisterComponent]
public class ConveyorSwitchComponent : Component, IInteractHand, IInteractUsing, IActivate
{
public override string Name => "ConveyorSwitch";
private ConveyorState _state;
/// <summary>
/// The current state of this switch
/// </summary>
[ViewVariables]
public ConveyorState State
{
get => _state;
private set
{
_state = value;
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
{
appearance.SetData(ConveyorVisuals.State, value);
}
}
}
private ConveyorGroup? _group;
public void Sync(ConveyorGroup group)
{
_group = group;
if (State == ConveyorState.Loose)
{
return;
}
State = group.State == ConveyorState.Loose
? ConveyorState.Off
: group.State;
}
/// <summary>
/// Disconnects this switch from any conveyors and other switches.
/// </summary>
private void Disconnect()
{
_group?.RemoveSwitch(this);
_group = null;
State = ConveyorState.Off;
}
/// <summary>
/// Connects a conveyor to this switch.
/// </summary>
/// <param name="conveyor">The conveyor to be connected.</param>
/// <param name="user">The user doing the connecting, if any.</param>
public void Connect(ConveyorComponent conveyor, IEntity? user = null)
{
if (_group == null)
{
_group = new ConveyorGroup();
_group.AddSwitch(this);
}
_group.AddConveyor(conveyor);
user?.PopupMessage(Loc.GetString("Conveyor linked."));
}
/// <summary>
/// Cycles this conveyor switch to its next valid state
/// </summary>
/// <returns>
/// true if the switch can be operated and the state could be cycled,
/// false otherwise
/// </returns>
private bool NextState()
{
State = State switch
{
ConveyorState.Off => ConveyorState.Forward,
ConveyorState.Forward => ConveyorState.Reversed,
ConveyorState.Reversed => ConveyorState.Off,
ConveyorState.Loose => ConveyorState.Off,
_ => throw new ArgumentOutOfRangeException()
};
_group?.SetState(this);
return true;
}
/// <summary>
/// Moves this switch to the group of another.
/// </summary>
/// <param name="other">The conveyor switch to synchronize with.</param>
/// <param name="user">The user doing the syncing, if any.</param>
private void SyncWith(ConveyorSwitchComponent other, IEntity? user = null)
{
other._group?.AddSwitch(this);
if (user == null)
{
return;
}
Owner.PopupMessage(user, Loc.GetString("Switches synchronized."));
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataReadWriteFunction(
"conveyors",
new List<EntityUid>(),
ids =>
{
if (ids == null)
{
return;
}
foreach (var id in ids)
{
if (!Owner.EntityManager.TryGetEntity(id, out var conveyor))
{
continue;
}
if (!conveyor.TryGetComponent(out ConveyorComponent? component))
{
continue;
}
Connect(component);
}
},
() => _group?.Conveyors.Select(conveyor => conveyor.Owner.Uid).ToList());
serializer.DataReadWriteFunction(
"switches",
new List<EntityUid>(),
ids =>
{
if (ids == null)
{
return;
}
foreach (var id in ids)
{
if (!Owner.EntityManager.TryGetEntity(id, out var @switch))
{
continue;
}
if (!@switch.TryGetComponent(out ConveyorSwitchComponent? component))
{
continue;
}
component.SyncWith(this);
}
},
() => _group?.Switches.Select(@switch => @switch.Owner.Uid).ToList());
}
public override void OnRemove()
{
base.OnRemove();
Disconnect();
}
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
{
return NextState();
}
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
{
if (eventArgs.Using.TryGetComponent(out ConveyorComponent? conveyor))
{
Connect(conveyor, eventArgs.User);
return true;
}
if (eventArgs.Using.TryGetComponent(out ConveyorSwitchComponent? otherSwitch))
{
SyncWith(otherSwitch, eventArgs.User);
return true;
}
return true;
}
void IActivate.Activate(ActivateEventArgs eventArgs)
{
NextState();
}
}
}

View File

@@ -30,11 +30,15 @@ namespace Content.Server.GameObjects.Components.MachineLinking
return;
}
if (transmitter.TransmitSignal(user, new ToggleSignal()))
if (transmitter.TransmitSignal(new ToggleSignal()))
{
// Since the button doesn't have an animation, I'm going to use a popup message
Owner.PopupMessage(user, Loc.GetString("Click."));
}
else
{
Owner.PopupMessage(user, Loc.GetString("No receivers connected."));
}
}
}

View File

@@ -7,8 +7,11 @@ using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Serialization;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
namespace Content.Server.GameObjects.Components.MachineLinking
{
@@ -19,6 +22,8 @@ namespace Content.Server.GameObjects.Components.MachineLinking
private List<SignalTransmitterComponent> _transmitters;
private int? _maxTransmitters = default;
public override void Initialize()
{
base.Initialize();
@@ -26,6 +31,11 @@ namespace Content.Server.GameObjects.Components.MachineLinking
_transmitters = new List<SignalTransmitterComponent>();
}
public override void ExposeData(ObjectSerializer serializer)
{
serializer.DataField(this, x=> x._maxTransmitters, "maxTransmitters", null);
}
public void DistributeSignal<T>(T state)
{
foreach (var comp in Owner.GetAllComponents<ISignalReceiver<T>>())
@@ -34,15 +44,18 @@ namespace Content.Server.GameObjects.Components.MachineLinking
}
}
public void Subscribe(SignalTransmitterComponent transmitter)
public bool Subscribe(SignalTransmitterComponent transmitter)
{
if (_transmitters.Contains(transmitter))
{
return;
return true;
}
if (_transmitters.Count >= _maxTransmitters) return false;
transmitter.Subscribe(this);
_transmitters.Add(transmitter);
return true;
}
public void Unsubscribe(SignalTransmitterComponent transmitter)
@@ -51,6 +64,20 @@ namespace Content.Server.GameObjects.Components.MachineLinking
_transmitters.Remove(transmitter);
}
public void UnsubscribeAll()
{
for (var i = _transmitters.Count-1; i >= 0; i--)
{
var transmitter = _transmitters[i];
if (transmitter.Deleted)
{
continue;
}
transmitter.Unsubscribe(this);
}
}
/// <summary>
/// Subscribes/Unsubscribes a transmitter to this component. Returns whether it was successful.
/// </summary>
@@ -78,7 +105,11 @@ namespace Content.Server.GameObjects.Components.MachineLinking
return false;
}
Subscribe(transmitter);
if (!Subscribe(transmitter))
{
Owner.PopupMessage(user, Loc.GetString("Max Transmitters reached!"));
return false;
}
Owner.PopupMessage(user, Loc.GetString("Linked!"));
return true;
}
@@ -101,15 +132,8 @@ namespace Content.Server.GameObjects.Components.MachineLinking
{
base.Shutdown();
foreach (var transmitter in _transmitters)
{
if (transmitter.Deleted)
{
continue;
}
UnsubscribeAll();
transmitter.Unsubscribe(this);
}
_transmitters.Clear();
}
}

View File

@@ -1,5 +1,6 @@
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Verbs;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
@@ -52,7 +53,10 @@ namespace Content.Server.GameObjects.Components.MachineLinking
return;
}
transmitter.TransmitSignal(user, _on);
if (!transmitter.TransmitSignal(_on))
{
Owner.PopupMessage(user, Loc.GetString("No receivers connected."));
}
}
private void UpdateSprite()

View File

@@ -10,6 +10,7 @@ using Robust.Shared.Interfaces.Map;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.MachineLinking
@@ -87,11 +88,10 @@ namespace Content.Server.GameObjects.Components.MachineLinking
}
}
public bool TransmitSignal<T>(IEntity user, T signal)
public bool TransmitSignal<T>(T signal)
{
if (_receivers.Count == 0)
{
Owner.PopupMessage(user, Loc.GetString("No receivers connected."));
return false;
}
@@ -150,8 +150,9 @@ namespace Content.Server.GameObjects.Components.MachineLinking
{
base.Shutdown();
foreach (var receiver in _receivers)
for (var i = _receivers.Count-1; i >= 0; i++)
{
var receiver = _receivers[i];
if (receiver.Deleted)
{
continue;
@@ -159,6 +160,7 @@ namespace Content.Server.GameObjects.Components.MachineLinking
receiver.Unsubscribe(this);
}
_receivers.Clear();
}
}

View File

@@ -0,0 +1,68 @@
#nullable enable
using System;
using Content.Server.GameObjects.Components.MachineLinking.Signals;
using Content.Shared.GameObjects.Components.Conveyor;
using Content.Shared.GameObjects.Components.MachineLinking;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Localization;
namespace Content.Server.GameObjects.Components.MachineLinking
{
[RegisterComponent]
[ComponentReference(typeof(IActivate))]
public class SignalTwoWayLeverComponent : SignalTransmitterComponent, IInteractHand, IActivate
{
public override string Name => "TwoWayLever";
private TwoWayLeverSignal _state = TwoWayLeverSignal.Middle;
private bool _nextForward = true;
public TwoWayLeverSignal State
{
get => _state;
private set
{
_state = value;
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
{
appearance.SetData(TwoWayLeverVisuals.State, value);
}
}
}
private void NextState(IEntity user)
{
State = State switch
{
TwoWayLeverSignal.Left => TwoWayLeverSignal.Middle,
TwoWayLeverSignal.Middle => _nextForward ? TwoWayLeverSignal.Right : TwoWayLeverSignal.Left,
TwoWayLeverSignal.Right => TwoWayLeverSignal.Middle,
_ => TwoWayLeverSignal.Middle
};
if (State == TwoWayLeverSignal.Left || State == TwoWayLeverSignal.Right) _nextForward = !_nextForward;
if (!TransmitSignal(State))
{
Owner.PopupMessage(user, Loc.GetString("No receivers connected."));
}
}
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
{
NextState(eventArgs.User);
return true;
}
void IActivate.Activate(ActivateEventArgs eventArgs)
{
NextState(eventArgs.User);
}
}
}

View File

@@ -173,7 +173,7 @@ namespace Content.Server.GameObjects.Components.Recycling
if (entity.TryGetComponent(out IPhysicsComponent physics))
{
var controller = physics.EnsureController<ConveyedController>();
controller.Move(direction, frameTime);
controller.Move(direction, frameTime, entity.Transform.WorldPosition - Owner.Transform.WorldPosition);
}
}
}