2019-07-31 15:02:36 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
2019-04-15 21:11:38 -06:00
|
|
|
|
using Robust.Server.Interfaces.GameObjects;
|
|
|
|
|
|
using Robust.Shared.GameObjects;
|
2020-02-06 08:50:52 -05:00
|
|
|
|
using Robust.Shared.GameObjects.Components.Transform;
|
2019-04-15 21:11:38 -06:00
|
|
|
|
using Robust.Shared.Interfaces.GameObjects.Components;
|
|
|
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
|
using Robust.Shared.ViewVariables;
|
2018-02-03 22:35:42 -06:00
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.GameObjects.Components.Power
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Component that connects to the powernet
|
|
|
|
|
|
/// </summary>
|
2019-07-31 15:02:36 +02:00
|
|
|
|
[RegisterComponent]
|
2018-02-03 22:35:42 -06:00
|
|
|
|
public class PowerNodeComponent : Component
|
|
|
|
|
|
{
|
|
|
|
|
|
public override string Name => "PowerNode";
|
2018-05-27 16:44:50 +02:00
|
|
|
|
|
2018-02-03 22:35:42 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The powernet this node is connected to
|
|
|
|
|
|
/// </summary>
|
2018-09-09 15:34:43 +02:00
|
|
|
|
[ViewVariables]
|
|
|
|
|
|
public Powernet Parent { get; set; }
|
2018-02-03 22:35:42 -06:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// An event handling when this node connects to a powernet
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public event EventHandler<PowernetEventArgs> OnPowernetConnect;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// An event handling when this node disconnects from a powernet
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public event EventHandler<PowernetEventArgs> OnPowernetDisconnect;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// An event that registers us to a regenerating powernet
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public event EventHandler<PowernetEventArgs> OnPowernetRegenerate;
|
|
|
|
|
|
|
2019-12-22 04:23:38 -08:00
|
|
|
|
protected override void Startup()
|
2018-02-03 22:35:42 -06:00
|
|
|
|
{
|
2019-12-22 04:23:38 -08:00
|
|
|
|
base.Startup();
|
2019-12-19 11:35:17 -08:00
|
|
|
|
|
2018-02-03 22:35:42 -06:00
|
|
|
|
TryCreatePowernetConnection();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void OnRemove()
|
|
|
|
|
|
{
|
|
|
|
|
|
DisconnectFromPowernet();
|
|
|
|
|
|
|
|
|
|
|
|
base.OnRemove();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Find a nearby wire which will have a powernet and connect ourselves to its powernet
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void TryCreatePowernetConnection()
|
|
|
|
|
|
{
|
2018-05-27 23:39:14 +02:00
|
|
|
|
if (Parent != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2020-02-06 08:50:52 -05:00
|
|
|
|
|
|
|
|
|
|
var position = Owner.Transform.WorldPosition;
|
|
|
|
|
|
|
|
|
|
|
|
var sgc = Owner.GetComponent<SnapGridComponent>();
|
|
|
|
|
|
var wire = sgc.GetCardinalNeighborCells()
|
|
|
|
|
|
.SelectMany(x => x.GetLocal()).Distinct()
|
|
|
|
|
|
.Select(x => x.TryGetComponent<PowerTransferComponent>(out var c) ? c : null)
|
|
|
|
|
|
.Where(x => x != null).Distinct()
|
|
|
|
|
|
.ToArray()
|
|
|
|
|
|
.OrderByDescending(x => (x.Owner.Transform.WorldPosition - position).Length)
|
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
if (wire?.Parent != null)
|
2018-05-27 23:39:14 +02:00
|
|
|
|
{
|
2020-02-06 08:50:52 -05:00
|
|
|
|
ConnectToPowernet(wire.Parent);
|
2018-05-27 23:39:14 +02:00
|
|
|
|
}
|
2018-02-03 22:35:42 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Triggers event telling power components that we connected to a powernet
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="toconnect"></param>
|
|
|
|
|
|
public void ConnectToPowernet(Powernet toconnect)
|
|
|
|
|
|
{
|
|
|
|
|
|
Parent = toconnect;
|
2018-05-27 16:44:50 +02:00
|
|
|
|
Parent.NodeList.Add(this);
|
2018-02-03 22:35:42 -06:00
|
|
|
|
OnPowernetConnect?.Invoke(this, new PowernetEventArgs(Parent));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Triggers event telling power components that we haven't disconnected but have readded ourselves to a regenerated powernet
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="toconnect"></param>
|
|
|
|
|
|
public void RegeneratePowernet(Powernet toconnect)
|
|
|
|
|
|
{
|
|
|
|
|
|
//This removes the device from things that will be powernet disconnected when dirty powernet is killed
|
2018-05-27 16:44:50 +02:00
|
|
|
|
Parent.NodeList.Remove(this);
|
2018-02-03 22:35:42 -06:00
|
|
|
|
|
|
|
|
|
|
Parent = toconnect;
|
2018-05-27 16:44:50 +02:00
|
|
|
|
Parent.NodeList.Add(this);
|
2018-02-03 22:35:42 -06:00
|
|
|
|
OnPowernetRegenerate?.Invoke(this, new PowernetEventArgs(Parent));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Triggers event telling power components we have exited any powernets
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void DisconnectFromPowernet()
|
|
|
|
|
|
{
|
2018-05-27 16:44:50 +02:00
|
|
|
|
if (Parent == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
Parent.NodeList.Remove(this);
|
2018-02-03 22:35:42 -06:00
|
|
|
|
OnPowernetDisconnect?.Invoke(this, new PowernetEventArgs(Parent));
|
|
|
|
|
|
Parent = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class PowernetEventArgs : EventArgs
|
|
|
|
|
|
{
|
|
|
|
|
|
public PowernetEventArgs(Powernet powernet)
|
|
|
|
|
|
{
|
|
|
|
|
|
Powernet = powernet;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Powernet Powernet { get; }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|