Apc device network and apc based light switch (#4908)
* Remove WireNet node group from cables Implement extension cable components and system Remove connection over distance logic from ApcPower... components * Add extension cable components to prototypes * Implement ApcNetwork Implement ApcNetSwitch * Fix ignoredComponents.cs * Add friend attribute to new components * Add construction graph for a light switch * Address reviews * Fix broken test * Move ConnectionType enum to DeviceNetworkComponent Change netId data definition to use the ConnectionType enum values Co-authored-by: Julian Giebel <j.giebel@netrocks.info>
This commit is contained in:
@@ -23,47 +23,11 @@ namespace Content.Server.Power.Components
|
||||
[RegisterComponent]
|
||||
public class ApcPowerReceiverComponent : Component, IExamine
|
||||
{
|
||||
[ViewVariables] [ComponentDependency] private readonly IPhysBody? _physicsComponent = null;
|
||||
|
||||
public override string Name => "ApcPowerReceiver";
|
||||
|
||||
[ViewVariables]
|
||||
public bool Powered => (MathHelper.CloseToPercent(NetworkLoad.ReceivingPower, Load) || !NeedsPower) && !PowerDisabled;
|
||||
|
||||
/// <summary>
|
||||
/// The max distance from a <see cref="ApcPowerProviderComponent"/> that this can receive power from.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public int PowerReceptionRange { get => _powerReceptionRange; set => SetPowerReceptionRange(value); }
|
||||
[DataField("powerReceptionRange")]
|
||||
private int _powerReceptionRange = 3;
|
||||
|
||||
[ViewVariables]
|
||||
public ApcPowerProviderComponent? Provider
|
||||
{
|
||||
get => _provider;
|
||||
set
|
||||
{
|
||||
// Will get updated before power networks process.
|
||||
NetworkLoad.LinkedNetwork = default;
|
||||
_provider?.RemoveReceiver(this);
|
||||
_provider = value;
|
||||
value?.AddReceiver(this);
|
||||
ApcPowerChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private ApcPowerProviderComponent? _provider;
|
||||
|
||||
/// <summary>
|
||||
/// If this should be considered for connection by <see cref="ApcPowerProviderComponent"/>s.
|
||||
/// </summary>
|
||||
public bool Connectable => Anchored;
|
||||
|
||||
private bool Anchored => _physicsComponent == null || _physicsComponent.BodyType == BodyType.Static;
|
||||
|
||||
[ViewVariables] public bool NeedsProvider => Provider == null;
|
||||
|
||||
/// <summary>
|
||||
/// Amount of charge this needs from an APC per second to function.
|
||||
/// </summary>
|
||||
@@ -104,73 +68,11 @@ namespace Content.Server.Power.Components
|
||||
DesiredPower = 5
|
||||
};
|
||||
|
||||
protected override void Startup()
|
||||
{
|
||||
base.Startup();
|
||||
if (NeedsProvider)
|
||||
{
|
||||
TryFindAndSetProvider();
|
||||
}
|
||||
if (_physicsComponent != null)
|
||||
{
|
||||
AnchorUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnRemove()
|
||||
{
|
||||
_provider?.RemoveReceiver(this);
|
||||
|
||||
base.OnRemove();
|
||||
}
|
||||
|
||||
public void TryFindAndSetProvider()
|
||||
{
|
||||
if (TryFindAvailableProvider(out var provider))
|
||||
{
|
||||
Provider = provider;
|
||||
}
|
||||
}
|
||||
|
||||
public void ApcPowerChanged()
|
||||
{
|
||||
OnNewPowerState();
|
||||
}
|
||||
|
||||
private bool TryFindAvailableProvider([NotNullWhen(true)] out ApcPowerProviderComponent? foundProvider)
|
||||
{
|
||||
var nearbyEntities = IoCManager.Resolve<IEntityLookup>()
|
||||
.GetEntitiesInRange(Owner, PowerReceptionRange);
|
||||
|
||||
foreach (var entity in nearbyEntities)
|
||||
{
|
||||
if (entity.TryGetComponent<ApcPowerProviderComponent>(out var provider))
|
||||
{
|
||||
if (provider.Connectable)
|
||||
{
|
||||
if (provider.Owner.Transform.Coordinates.TryDistance(Owner.EntityManager, Owner.Transform.Coordinates, out var distance))
|
||||
{
|
||||
if (distance < Math.Min(PowerReceptionRange, provider.PowerTransferRange))
|
||||
{
|
||||
foundProvider = provider;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foundProvider = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
private void SetPowerReceptionRange(int newPowerReceptionRange)
|
||||
{
|
||||
Provider = null;
|
||||
_powerReceptionRange = newPowerReceptionRange;
|
||||
TryFindAndSetProvider();
|
||||
}
|
||||
|
||||
private void OnNewPowerState()
|
||||
{
|
||||
SendMessage(new PowerChangedMessage(Powered));
|
||||
@@ -182,21 +84,6 @@ namespace Content.Server.Power.Components
|
||||
}
|
||||
}
|
||||
|
||||
public void AnchorUpdate()
|
||||
{
|
||||
if (Anchored)
|
||||
{
|
||||
if (NeedsProvider)
|
||||
{
|
||||
TryFindAndSetProvider();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Provider = null;
|
||||
}
|
||||
}
|
||||
|
||||
///<summary>
|
||||
///Adds some markup to the examine text of whatever object is using this component to tell you if it's powered or not, even if it doesn't have an icon state to do this for you.
|
||||
///</summary>
|
||||
|
||||
Reference in New Issue
Block a user