Re-organize all projects (#4166)

This commit is contained in:
DrSmugleaf
2021-06-09 22:19:39 +02:00
committed by GitHub
parent 9f50e4061b
commit ff1a2d97ea
1773 changed files with 5258 additions and 5508 deletions

View File

@@ -1,206 +0,0 @@
#nullable enable
using System;
using Content.Server.GameObjects.Components.Access;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Content.Server.GameObjects.Components.Power.PowerNetComponents;
using Content.Server.Utility;
using Content.Shared.GameObjects.Components.Power;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Timing;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
{
[RegisterComponent]
[ComponentReference(typeof(IActivate))]
public class ApcComponent : BaseApcNetComponent, IActivate
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
public override string Name => "Apc";
public bool MainBreakerEnabled { get; private set; } = true;
private ApcChargeState _lastChargeState;
private TimeSpan _lastChargeStateChange;
private ApcExternalPowerState _lastExternalPowerState;
private TimeSpan _lastExternalPowerStateChange;
private float _lastCharge;
private TimeSpan _lastChargeChange;
private bool _uiDirty = true;
private const float HighPowerThreshold = 0.9f;
private const int VisualsChangeDelay = 1;
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(ApcUiKey.Key);
public BatteryComponent? Battery => Owner.TryGetComponent(out BatteryComponent? batteryComponent) ? batteryComponent : null;
[ComponentDependency] private AccessReader? _accessReader = null;
public override void Initialize()
{
base.Initialize();
Owner.EnsureComponent<BatteryComponent>();
Owner.EnsureComponent<PowerConsumerComponent>();
Owner.EnsureComponentWarn<ServerUserInterfaceComponent>();
Owner.EnsureComponentWarn<AccessReader>();
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
}
Update();
}
protected override void AddSelfToNet(IApcNet apcNet)
{
apcNet.AddApc(this);
}
protected override void RemoveSelfFromNet(IApcNet apcNet)
{
apcNet.RemoveApc(this);
}
private void UserInterfaceOnReceiveMessage(ServerBoundUserInterfaceMessage serverMsg)
{
if (serverMsg.Message is ApcToggleMainBreakerMessage)
{
var user = serverMsg.Session.AttachedEntity;
if(user == null) return;
if (_accessReader == null || _accessReader.IsAllowed(user))
{
MainBreakerEnabled = !MainBreakerEnabled;
_uiDirty = true;
SoundSystem.Play(Filter.Pvs(Owner), "/Audio/Machines/machine_switch.ogg", Owner, AudioParams.Default.WithVolume(-2f));
}
else
{
user.PopupMessageCursor(Loc.GetString("Insufficient access!"));
}
}
}
public void Update()
{
var newState = CalcChargeState();
if (newState != _lastChargeState && _lastChargeStateChange + TimeSpan.FromSeconds(VisualsChangeDelay) < _gameTiming.CurTime)
{
_lastChargeState = newState;
_lastChargeStateChange = _gameTiming.CurTime;
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
{
appearance.SetData(ApcVisuals.ChargeState, newState);
}
}
Owner.TryGetComponent(out BatteryComponent? battery);
var newCharge = battery?.CurrentCharge;
if (newCharge != null && newCharge != _lastCharge && _lastChargeChange + TimeSpan.FromSeconds(VisualsChangeDelay) < _gameTiming.CurTime)
{
_lastCharge = newCharge.Value;
_lastChargeChange = _gameTiming.CurTime;
_uiDirty = true;
}
var extPowerState = CalcExtPowerState();
if (extPowerState != _lastExternalPowerState && _lastExternalPowerStateChange + TimeSpan.FromSeconds(VisualsChangeDelay) < _gameTiming.CurTime)
{
_lastExternalPowerState = extPowerState;
_lastExternalPowerStateChange = _gameTiming.CurTime;
_uiDirty = true;
}
if (_uiDirty && battery != null && newCharge != null)
{
UserInterface?.SetState(new ApcBoundInterfaceState(MainBreakerEnabled, extPowerState, newCharge.Value / battery.MaxCharge));
_uiDirty = false;
}
}
private ApcChargeState CalcChargeState()
{
if (!Owner.TryGetComponent(out BatteryComponent? battery))
{
return ApcChargeState.Lack;
}
var chargeFraction = battery.CurrentCharge / battery.MaxCharge;
if (chargeFraction > HighPowerThreshold)
{
return ApcChargeState.Full;
}
if (!Owner.TryGetComponent(out PowerConsumerComponent? consumer))
{
return ApcChargeState.Full;
}
if (consumer.DrawRate == consumer.ReceivedPower)
{
return ApcChargeState.Charging;
}
else
{
return ApcChargeState.Lack;
}
}
private ApcExternalPowerState CalcExtPowerState()
{
if (!Owner.TryGetComponent(out BatteryStorageComponent? batteryStorage))
{
return ApcExternalPowerState.None;
}
var consumer = batteryStorage.Consumer;
if (consumer == null)
return ApcExternalPowerState.None;
if (consumer.ReceivedPower == 0 && consumer.DrawRate != 0)
{
return ApcExternalPowerState.None;
}
else if (consumer.ReceivedPower < consumer.DrawRate)
{
return ApcExternalPowerState.Low;
}
else
{
return ApcExternalPowerState.Good;
}
}
void IActivate.Activate(ActivateEventArgs eventArgs)
{
if (!eventArgs.User.TryGetComponent(out ActorComponent? actor))
{
return;
}
UserInterface?.Open(actor.PlayerSession);
}
}
}

View File

@@ -1,10 +0,0 @@
#nullable enable
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
{
public abstract class BaseApcNetComponent : BaseNetConnectorComponent<IApcNet>
{
protected override IApcNet NullNet => ApcNetNodeGroup.NullNet;
}
}

View File

@@ -1,173 +0,0 @@
#nullable enable
using System;
using System.Collections.Generic;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
{
/// <summary>
/// Relays <see cref="PowerReceiverComponent"/>s in an area to a <see cref="IApcNet"/> so they can receive power.
/// </summary>
public interface IPowerProvider
{
void AddReceiver(PowerReceiverComponent receiver);
void RemoveReceiver(PowerReceiverComponent receiver);
void UpdateReceiverLoad(int oldLoad, int newLoad);
public IEntity? ProviderOwner { get; }
public bool HasApcPower { get; }
}
[RegisterComponent]
public class PowerProviderComponent : BaseApcNetComponent, IPowerProvider
{
public override string Name => "PowerProvider";
public IEntity ProviderOwner => Owner;
[ViewVariables]
public bool HasApcPower => Net.Powered;
/// <summary>
/// The max distance this can transmit power to <see cref="PowerReceiverComponent"/>s from.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public int PowerTransferRange { get => _powerTransferRange; set => SetPowerTransferRange(value); }
[DataField("powerTransferRange")]
private int _powerTransferRange = 3;
[ViewVariables]
public IReadOnlyList<PowerReceiverComponent> LinkedReceivers => _linkedReceivers;
private List<PowerReceiverComponent> _linkedReceivers = new();
/// <summary>
/// If <see cref="PowerReceiverComponent"/>s should consider connecting to this.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public bool Connectable { get; private set; } = true;
public static readonly IPowerProvider NullProvider = new NullPowerProvider();
public void AddReceiver(PowerReceiverComponent receiver)
{
var oldLoad = GetTotalLoad();
_linkedReceivers.Add(receiver);
var newLoad = oldLoad + receiver.Load;
Net.UpdatePowerProviderReceivers(this, oldLoad, newLoad);
}
public void RemoveReceiver(PowerReceiverComponent receiver)
{
var oldLoad = GetTotalLoad();
_linkedReceivers.Remove(receiver);
var newLoad = oldLoad - receiver.Load;
Net.UpdatePowerProviderReceivers(this, oldLoad, newLoad);
}
public void UpdateReceiverLoad(int oldLoad, int newLoad)
{
Net.UpdatePowerProviderReceivers(this, oldLoad, newLoad);
}
protected override void Startup()
{
base.Startup();
foreach (var receiver in FindAvailableReceivers())
{
receiver.Provider = this;
}
}
public override void OnRemove()
{
Connectable = false;
var receivers = _linkedReceivers.ToArray();
foreach (var receiver in receivers)
{
receiver.ClearProvider();
}
foreach (var receiver in receivers)
{
receiver.TryFindAndSetProvider();
}
base.OnRemove();
}
private List<PowerReceiverComponent> FindAvailableReceivers()
{
var nearbyEntities = IoCManager.Resolve<IEntityLookup>()
.GetEntitiesInRange(Owner, PowerTransferRange);
var receivers = new List<PowerReceiverComponent>();
foreach (var entity in nearbyEntities)
{
if (entity.TryGetComponent<PowerReceiverComponent>(out var receiver) &&
receiver.Connectable &&
receiver.NeedsProvider &&
receiver.Owner.Transform.Coordinates.TryDistance(Owner.EntityManager, Owner.Transform.Coordinates, out var distance) &&
distance < Math.Min(PowerTransferRange, receiver.PowerReceptionRange))
{
receivers.Add(receiver);
}
}
return receivers;
}
protected override void AddSelfToNet(IApcNet apcNet)
{
apcNet.AddPowerProvider(this);
}
protected override void RemoveSelfFromNet(IApcNet apcNet)
{
apcNet.RemovePowerProvider(this);
}
private void SetPowerTransferRange(int newPowerTransferRange)
{
var receivers = _linkedReceivers.ToArray();
foreach (var receiver in receivers)
{
receiver.ClearProvider();
}
_powerTransferRange = newPowerTransferRange;
foreach (var receiver in receivers)
{
receiver.TryFindAndSetProvider();
}
}
private int GetTotalLoad()
{
var load = 0;
foreach (var receiver in _linkedReceivers)
{
load += receiver.Load;
}
return load;
}
private class NullPowerProvider : IPowerProvider
{
/// <summary>
/// It is important that this returns false, so <see cref="PowerReceiverComponent"/>s with a <see cref="NullPowerProvider"/> have no power.
/// </summary>
public bool HasApcPower => false;
public void AddReceiver(PowerReceiverComponent receiver) { }
public void RemoveReceiver(PowerReceiverComponent receiver) { }
public void UpdateReceiverLoad(int oldLoad, int newLoad) { }
public IEntity? ProviderOwner => default;
}
}
}

View File

@@ -1,236 +0,0 @@
#nullable enable
using System;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Content.Shared.GameObjects.Components.Power;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Physics;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
{
/// <summary>
/// Attempts to link with a nearby <see cref="IPowerProvider"/>s so that it can receive power from a <see cref="IApcNet"/>.
/// </summary>
[RegisterComponent]
public class PowerReceiverComponent : Component, IExamine
{
[ViewVariables] [ComponentDependency] private readonly IPhysBody? _physicsComponent = null;
public override string Name => "PowerReceiver";
[ViewVariables]
public bool Powered => (HasApcPower || !NeedsPower) && !PowerDisabled;
/// <summary>
/// If this is being powered by an Apc.
/// </summary>
[ViewVariables]
public bool HasApcPower { get; private set; }
/// <summary>
/// The max distance from a <see cref="PowerProviderComponent"/> 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 IPowerProvider Provider { get => _provider; set => SetProvider(value); }
private IPowerProvider _provider = PowerProviderComponent.NullProvider;
/// <summary>
/// If this should be considered for connection by <see cref="PowerProviderComponent"/>s.
/// </summary>
public bool Connectable => Anchored;
private bool Anchored => _physicsComponent == null || _physicsComponent.BodyType == BodyType.Static;
[ViewVariables]
public bool NeedsProvider { get; private set; } = true;
/// <summary>
/// Amount of charge this needs from an APC per second to function.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public int Load { get => _load; set => SetLoad(value); }
[DataField("powerLoad")]
private int _load = 5;
/// <summary>
/// When false, causes this to appear powered even if not receiving power from an Apc.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public bool NeedsPower { get => _needsPower; set => SetNeedsPower(value); }
[DataField("needsPower")]
private bool _needsPower = true;
/// <summary>
/// When true, causes this to never appear powered.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public bool PowerDisabled { get => _powerDisabled; set => SetPowerDisabled(value); }
[DataField("powerDisabled")]
private bool _powerDisabled;
protected override void Startup()
{
base.Startup();
if (NeedsProvider)
{
TryFindAndSetProvider();
}
if (_physicsComponent != null)
{
AnchorUpdate();
}
}
public override void OnRemove()
{
_provider.RemoveReceiver(this);
base.OnRemove();
}
public void TryFindAndSetProvider()
{
if (TryFindAvailableProvider(out var provider))
{
Provider = provider;
}
}
public void ApcPowerChanged()
{
var oldPowered = Powered;
HasApcPower = Provider.HasApcPower;
if (Powered != oldPowered)
OnNewPowerState();
}
private bool TryFindAvailableProvider(out IPowerProvider foundProvider)
{
var nearbyEntities = IoCManager.Resolve<IEntityLookup>()
.GetEntitiesInRange(Owner, PowerReceptionRange);
foreach (var entity in nearbyEntities)
{
if (entity.TryGetComponent<PowerProviderComponent>(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;
}
public void ClearProvider()
{
_provider.RemoveReceiver(this);
_provider = PowerProviderComponent.NullProvider;
NeedsProvider = true;
ApcPowerChanged();
}
private void SetProvider(IPowerProvider newProvider)
{
_provider.RemoveReceiver(this);
_provider = newProvider;
newProvider.AddReceiver(this);
NeedsProvider = false;
ApcPowerChanged();
}
private void SetPowerReceptionRange(int newPowerReceptionRange)
{
ClearProvider();
_powerReceptionRange = newPowerReceptionRange;
TryFindAndSetProvider();
}
private void SetLoad(int newLoad)
{
Provider.UpdateReceiverLoad(Load, newLoad);
_load = newLoad;
}
private void SetNeedsPower(bool newNeedsPower)
{
var oldPowered = Powered;
_needsPower = newNeedsPower;
if (oldPowered != Powered)
{
OnNewPowerState();
}
}
private void SetPowerDisabled(bool newPowerDisabled)
{
var oldPowered = Powered;
_powerDisabled = newPowerDisabled;
if (oldPowered != Powered)
{
OnNewPowerState();
}
}
private void OnNewPowerState()
{
SendMessage(new PowerChangedMessage(Powered));
if (Owner.TryGetComponent<AppearanceComponent>(out var appearance))
{
appearance.SetData(PowerDeviceVisuals.Powered, Powered);
}
}
public void AnchorUpdate()
{
if (Anchored)
{
if (NeedsProvider)
{
TryFindAndSetProvider();
}
}
else
{
ClearProvider();
}
}
///<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>
public void Examine(FormattedMessage message, bool inDetailsRange)
{
message.AddMarkup(Loc.GetString("It appears to be {0}.", Powered ? "[color=darkgreen]powered[/color]" : "[color=darkred]un-powered[/color]"));
}
}
public class PowerChangedMessage : ComponentMessage
{
public readonly bool Powered;
public PowerChangedMessage(bool powered)
{
Powered = powered;
}
}
}

View File

@@ -1,302 +0,0 @@
#nullable enable
using System;
using System.Threading.Tasks;
using Content.Server.GameObjects.Components.GUI;
using Content.Server.GameObjects.Components.Items.Storage;
using Content.Server.GameObjects.Components.Weapon.Ranged.Barrels;
using Content.Shared.GameObjects.Components.Power;
using Content.Shared.GameObjects.EntitySystems.ActionBlocker;
using Content.Shared.GameObjects.Verbs;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerReceiverUsers
{
[ComponentReference(typeof(IActivate))]
[ComponentReference(typeof(IInteractUsing))]
public abstract class BaseCharger : Component, IActivate, IInteractUsing
{
[ViewVariables]
private BatteryComponent? _heldBattery;
[ViewVariables]
private ContainerSlot _container = default!;
[ViewVariables]
private CellChargerStatus _status;
[ViewVariables]
[DataField("chargeRate")]
private int _chargeRate = 100;
[ViewVariables]
[DataField("transferEfficiency")]
private float _transferEfficiency = 0.85f;
public override void Initialize()
{
base.Initialize();
Owner.EnsureComponent<PowerReceiverComponent>();
_container = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-powerCellContainer");
// Default state in the visualizer is OFF, so when this gets powered on during initialization it will generally show empty
}
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
{
case PowerChangedMessage powerChanged:
PowerUpdate(powerChanged);
break;
}
}
public override void OnRemove()
{
_heldBattery = null;
base.OnRemove();
}
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
{
var result = TryInsertItem(eventArgs.Using);
if (!result)
{
eventArgs.User.PopupMessage(Owner, Loc.GetString("Unable to insert capacitor"));
}
return result;
}
void IActivate.Activate(ActivateEventArgs eventArgs)
{
RemoveItem(eventArgs.User);
}
/// <summary>
/// This will remove the item directly into the user's hand / floor
/// </summary>
/// <param name="user"></param>
private void RemoveItem(IEntity user)
{
var heldItem = _container.ContainedEntity;
if (heldItem == null)
{
return;
}
_container.Remove(heldItem);
_heldBattery = null;
if (user.TryGetComponent(out HandsComponent? handsComponent))
{
handsComponent.PutInHandOrDrop(heldItem.GetComponent<ItemComponent>());
}
if (heldItem.TryGetComponent(out ServerBatteryBarrelComponent? batteryBarrelComponent))
{
batteryBarrelComponent.UpdateAppearance();
}
UpdateStatus();
}
private void PowerUpdate(PowerChangedMessage eventArgs)
{
UpdateStatus();
}
[Verb]
private sealed class InsertVerb : Verb<BaseCharger>
{
protected override void GetData(IEntity user, BaseCharger component, VerbData data)
{
if (!ActionBlockerSystem.CanInteract(user))
{
data.Visibility = VerbVisibility.Invisible;
return;
}
if (!user.TryGetComponent(out HandsComponent? handsComponent))
{
data.Visibility = VerbVisibility.Invisible;
return;
}
if (component._container.ContainedEntity != null || handsComponent.GetActiveHand == null)
{
data.Visibility = VerbVisibility.Invisible;
return;
}
var heldItemName = Loc.GetString(handsComponent.GetActiveHand.Owner.Name);
data.Text = Loc.GetString("Insert {0}", heldItemName);
data.IconTexture = "/Textures/Interface/VerbIcons/insert.svg.192dpi.png";
}
protected override void Activate(IEntity user, BaseCharger component)
{
if (!user.TryGetComponent(out HandsComponent? handsComponent))
{
return;
}
if (handsComponent.GetActiveHand == null)
{
return;
}
var userItem = handsComponent.GetActiveHand.Owner;
handsComponent.Drop(userItem);
component.TryInsertItem(userItem);
}
}
[Verb]
private sealed class EjectVerb : Verb<BaseCharger>
{
protected override void GetData(IEntity user, BaseCharger component, VerbData data)
{
if (!ActionBlockerSystem.CanInteract(user))
{
data.Visibility = VerbVisibility.Invisible;
return;
}
if (component._container.ContainedEntity == null)
{
data.Visibility = VerbVisibility.Invisible;
return;
}
var containerItemName = Loc.GetString(component._container.ContainedEntity.Name);
data.Text = Loc.GetString("Eject {0}", containerItemName);
data.IconTexture = "/Textures/Interface/VerbIcons/eject.svg.192dpi.png";
}
protected override void Activate(IEntity user, BaseCharger component)
{
component.RemoveItem(user);
}
}
private CellChargerStatus GetStatus()
{
if (Owner.TryGetComponent(out PowerReceiverComponent? receiver) &&
!receiver.Powered)
{
return CellChargerStatus.Off;
}
if (_container.ContainedEntity == null)
{
return CellChargerStatus.Empty;
}
if (_heldBattery != null && Math.Abs(_heldBattery.MaxCharge - _heldBattery.CurrentCharge) < 0.01)
{
return CellChargerStatus.Charged;
}
return CellChargerStatus.Charging;
}
private bool TryInsertItem(IEntity entity)
{
if (!IsEntityCompatible(entity) || _container.ContainedEntity != null)
{
return false;
}
if (!_container.Insert(entity))
{
return false;
}
_heldBattery = GetBatteryFrom(entity);
UpdateStatus();
return true;
}
/// <summary>
/// If the supplied entity should fit into the charger.
/// </summary>
protected abstract bool IsEntityCompatible(IEntity entity);
protected abstract BatteryComponent? GetBatteryFrom(IEntity entity);
private void UpdateStatus()
{
// Not called UpdateAppearance just because it messes with the load
var status = GetStatus();
if (_status == status ||
!Owner.TryGetComponent(out PowerReceiverComponent? receiver))
{
return;
}
_status = status;
Owner.TryGetComponent(out AppearanceComponent? appearance);
switch (_status)
{
// Update load just in case
case CellChargerStatus.Off:
receiver.Load = 0;
appearance?.SetData(CellVisual.Light, CellChargerStatus.Off);
break;
case CellChargerStatus.Empty:
receiver.Load = 0;
appearance?.SetData(CellVisual.Light, CellChargerStatus.Empty);
break;
case CellChargerStatus.Charging:
receiver.Load = (int) (_chargeRate / _transferEfficiency);
appearance?.SetData(CellVisual.Light, CellChargerStatus.Charging);
break;
case CellChargerStatus.Charged:
receiver.Load = 0;
appearance?.SetData(CellVisual.Light, CellChargerStatus.Charged);
break;
default:
throw new ArgumentOutOfRangeException();
}
appearance?.SetData(CellVisual.Occupied, _container.ContainedEntity != null);
}
public void OnUpdate(float frameTime) //todo: make single system for this
{
if (_status == CellChargerStatus.Empty || _status == CellChargerStatus.Charged || _container.ContainedEntity == null)
{
return;
}
TransferPower(frameTime);
}
private void TransferPower(float frameTime)
{
if (Owner.TryGetComponent(out PowerReceiverComponent? receiver) &&
!receiver.Powered)
{
return;
}
if (_heldBattery == null)
{
return;
}
_heldBattery.CurrentCharge += _chargeRate * frameTime;
// Just so the sprite won't be set to 99.99999% visibility
if (_heldBattery.MaxCharge - _heldBattery.CurrentCharge < 0.01)
{
_heldBattery.CurrentCharge = _heldBattery.MaxCharge;
}
UpdateStatus();
}
}
}

View File

@@ -1,182 +0,0 @@
#nullable enable
using System;
using System.Collections.Generic;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerReceiverUsers
{
/// <summary>
/// Component that represents an emergency light, it has an internal battery that charges when the power is on.
/// </summary>
[RegisterComponent]
public class EmergencyLightComponent : Component, IExamine
{
public override string Name => "EmergencyLight";
[ViewVariables]
private EmergencyLightState State
{
get => _state;
set
{
if (_state == value)
return;
_state = value;
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new EmergencyLightMessage(this, _state));
}
}
private EmergencyLightState _state = EmergencyLightState.Empty;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("wattage")]
private float _wattage = 5;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("chargingWattage")]
private float _chargingWattage = 60;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("chargingEfficiency")]
private float _chargingEfficiency = 0.85f;
/// <summary>
/// For attaching UpdateState() to events.
/// </summary>
public void UpdateState(PowerChangedMessage e)
{
UpdateState();
}
/// <summary>
/// Updates the light's power drain, battery drain, sprite and actual light state.
/// </summary>
public void UpdateState()
{
if (!Owner.TryGetComponent(out PowerReceiverComponent? receiver))
{
return;
}
if (receiver.Powered)
{
receiver.Load = (int) Math.Abs(_wattage);
TurnOff();
State = EmergencyLightState.Charging;
}
else
{
TurnOn();
State = EmergencyLightState.On;
}
}
public void OnUpdate(float frameTime)
{
if (Owner.Deleted || !Owner.TryGetComponent(out BatteryComponent? battery))
{
return;
}
if(State == EmergencyLightState.On)
{
if (!battery.TryUseCharge(_wattage * frameTime))
{
State = EmergencyLightState.Empty;
TurnOff();
}
}
else
{
battery.CurrentCharge += _chargingWattage * frameTime * _chargingEfficiency;
if (battery.BatteryState == BatteryState.Full)
{
if (Owner.TryGetComponent(out PowerReceiverComponent? receiver))
{
receiver.Load = 1;
}
State = EmergencyLightState.Full;
}
}
}
private void TurnOff()
{
if (Owner.TryGetComponent(out SpriteComponent? sprite))
{
sprite.LayerSetState(0, "emergency_light_off");
}
if (Owner.TryGetComponent(out PointLightComponent? light))
{
light.Enabled = false;
}
}
private void TurnOn()
{
if (Owner.TryGetComponent(out SpriteComponent? sprite))
{
sprite.LayerSetState(0, "emergency_light_on");
}
if (Owner.TryGetComponent(out PointLightComponent? light))
{
light.Enabled = true;
}
}
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
{
case PowerChangedMessage powerChanged:
UpdateState(powerChanged);
break;
}
}
void IExamine.Examine(FormattedMessage message, bool inDetailsRange)
{
message.AddMarkup(Loc.GetString($"The battery indicator displays: {BatteryStateText[State]}."));
}
public enum EmergencyLightState
{
Charging,
Full,
Empty,
On
}
public Dictionary<EmergencyLightState, string> BatteryStateText = new()
{
{ EmergencyLightState.Full, "[color=darkgreen]Full[/color]"},
{ EmergencyLightState.Empty, "[color=darkred]Empty[/color]"},
{ EmergencyLightState.Charging, "[color=darkorange]Charging[/color]"},
{ EmergencyLightState.On, "[color=darkorange]Discharging[/color]"}
};
}
public sealed class EmergencyLightMessage : EntityEventArgs
{
public EmergencyLightComponent Component { get; }
public EmergencyLightComponent.EmergencyLightState State { get; }
public EmergencyLightMessage(EmergencyLightComponent component, EmergencyLightComponent.EmergencyLightState state)
{
Component = component;
State = state;
}
}
}

View File

@@ -1,139 +0,0 @@
#nullable enable
using System;
using Content.Shared.Audio;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerReceiverUsers
{
public enum LightBulbState
{
Normal,
Broken,
Burned,
}
public enum LightBulbType
{
Bulb,
Tube,
}
/// <summary>
/// Component that represents a light bulb. Can be broken, or burned, which turns them mostly useless.
/// </summary>
[RegisterComponent]
public class LightBulbComponent : Component, ILand, IBreakAct
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
/// <summary>
/// Invoked whenever the state of the light bulb changes.
/// </summary>
public event EventHandler<EventArgs>? OnLightBulbStateChange;
public event EventHandler<EventArgs?>? OnLightColorChange;
[DataField("color")]
private Color _color = Color.White;
[ViewVariables(VVAccess.ReadWrite)] public Color Color
{
get { return _color; }
set
{
_color = value;
OnLightColorChange?.Invoke(this, null);
UpdateColor();
}
}
public override string Name => "LightBulb";
[DataField("bulb")]
public LightBulbType Type = LightBulbType.Tube;
[DataField("BurningTemperature")]
private int _burningTemperature = 1400;
public int BurningTemperature => _burningTemperature;
[DataField("PowerUse")]
private int _powerUse = 40;
public int PowerUse => _powerUse;
/// <summary>
/// The current state of the light bulb. Invokes the OnLightBulbStateChange event when set.
/// It also updates the bulb's sprite accordingly.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)] public LightBulbState State
{
get { return _state; }
set
{
var sprite = Owner.GetComponent<SpriteComponent>();
OnLightBulbStateChange?.Invoke(this, EventArgs.Empty);
_state = value;
switch (value)
{
case LightBulbState.Normal:
sprite.LayerSetState(0, "normal");
break;
case LightBulbState.Broken:
sprite.LayerSetState(0, "broken");
break;
case LightBulbState.Burned:
sprite.LayerSetState(0, "burned");
break;
}
}
}
private LightBulbState _state = LightBulbState.Normal;
public void UpdateColor()
{
if (!Owner.TryGetComponent(out SpriteComponent? sprite))
{
return;
}
sprite.Color = Color;
}
public override void Initialize()
{
base.Initialize();
UpdateColor();
}
void ILand.Land(LandEventArgs eventArgs)
{
PlayBreakSound();
State = LightBulbState.Broken;
}
public void OnBreak(BreakageEventArgs eventArgs)
{
State = LightBulbState.Broken;
}
public void PlayBreakSound()
{
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>("GlassBreak");
var file = _random.Pick(soundCollection.PickFiles);
SoundSystem.Play(Filter.Pvs(Owner), file, Owner);
}
}
}

View File

@@ -1,27 +0,0 @@
#nullable enable
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.GameObjects;
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerReceiverUsers
{
/// <summary>
/// Recharges an entity with a <see cref="BatteryComponent"/>.
/// </summary>
[RegisterComponent]
[ComponentReference(typeof(IActivate))]
[ComponentReference(typeof(BaseCharger))]
public sealed class PowerCellChargerComponent : BaseCharger
{
public override string Name => "PowerCellCharger";
protected override bool IsEntityCompatible(IEntity entity)
{
return entity.HasComponent<BatteryComponent>();
}
protected override BatteryComponent GetBatteryFrom(IEntity entity)
{
return entity.GetComponent<BatteryComponent>();
}
}
}

View File

@@ -1,338 +0,0 @@
#nullable enable
using System;
using System.Threading.Tasks;
using Content.Server.GameObjects.Components.GUI;
using Content.Server.GameObjects.Components.Items.Storage;
using Content.Server.GameObjects.Components.MachineLinking;
using Content.Server.GameObjects.Components.MachineLinking.Signals;
using Content.Server.GameObjects.Components.Mobs;
using Content.Server.GameObjects.Components.Observer;
using Content.Shared.Actions;
using Content.Shared.Damage;
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.Components.Power.ApcNetComponents.PowerReceiverUsers;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Timing;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerReceiverUsers
{
/// <summary>
/// Component that represents a wall light. It has a light bulb that can be replaced when broken.
/// </summary>
[RegisterComponent]
public class PoweredLightComponent : Component, IInteractHand, IInteractUsing, IMapInit, ISignalReceiver<bool>, ISignalReceiver<ToggleSignal>, IGhostBooAffected
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
public override string Name => "PoweredLight";
private static readonly TimeSpan _thunkDelay = TimeSpan.FromSeconds(2);
// time to blink light when ghost made boo nearby
private static readonly TimeSpan ghostBlinkingTime = TimeSpan.FromSeconds(10);
private static readonly TimeSpan ghostBlinkingCooldown = TimeSpan.FromSeconds(60);
[ComponentDependency]
private readonly AppearanceComponent? _appearance;
private TimeSpan _lastThunk;
private TimeSpan? _lastGhostBlink;
[DataField("hasLampOnSpawn")]
private bool _hasLampOnSpawn = true;
[ViewVariables] [DataField("on")]
private bool _on = true;
[ViewVariables]
private bool _currentLit;
[ViewVariables]
private bool _isBlinking;
[ViewVariables] [DataField("ignoreGhostsBoo")]
private bool _ignoreGhostsBoo;
[DataField("bulb")] private LightBulbType _bulbType = LightBulbType.Tube;
public LightBulbType BulbType => _bulbType;
[ViewVariables] private ContainerSlot _lightBulbContainer = default!;
[ViewVariables]
public LightBulbComponent? LightBulb
{
get
{
if (_lightBulbContainer.ContainedEntity == null) return null;
_lightBulbContainer.ContainedEntity.TryGetComponent(out LightBulbComponent? bulb);
return bulb;
}
}
// TODO CONSTRUCTION make this use a construction graph
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
{
return InsertBulb(eventArgs.Using);
}
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
{
if (!eventArgs.User.TryGetComponent(out IDamageableComponent? damageableComponent))
{
Eject();
return false;
}
if(eventArgs.User.TryGetComponent(out HeatResistanceComponent? heatResistanceComponent))
{
if(CanBurn(heatResistanceComponent.GetHeatResistance()))
{
Burn();
return true;
}
}
Eject();
return true;
bool CanBurn(int heatResistance)
{
if (LightBulb == null)
return false;
return _currentLit && heatResistance < LightBulb.BurningTemperature;
}
void Burn()
{
Owner.PopupMessage(eventArgs.User, Loc.GetString("You burn your hand!"));
damageableComponent.ChangeDamage(DamageType.Heat, 20, false, Owner);
SoundSystem.Play(Filter.Pvs(Owner), "/Audio/Effects/lightburn.ogg", Owner);
}
void Eject()
{
EjectBulb(eventArgs.User);
UpdateLight();
}
}
/// <summary>
/// Try to replace current bulb with a new one
/// </summary>
public bool ReplaceBulb(IEntity bulb)
{
EjectBulb();
return InsertBulb(bulb);
}
/// <summary>
/// Inserts the bulb if possible.
/// </summary>
/// <returns>True if it could insert it, false if it couldn't.</returns>
private bool InsertBulb(IEntity bulb)
{
if (LightBulb != null) return false;
if (!bulb.TryGetComponent(out LightBulbComponent? lightBulb)) return false;
if (lightBulb.Type != _bulbType) return false;
var inserted = _lightBulbContainer.Insert(bulb);
lightBulb.OnLightBulbStateChange += UpdateLight;
lightBulb.OnLightColorChange += UpdateLight;
UpdateLight();
return inserted;
}
/// <summary>
/// Ejects the bulb to a mob's hand if possible.
/// </summary>
private void EjectBulb(IEntity? user = null)
{
if (LightBulb == null) return;
var bulb = LightBulb;
bulb.OnLightBulbStateChange -= UpdateLight;
bulb.OnLightColorChange -= UpdateLight;
if (!_lightBulbContainer.Remove(bulb.Owner)) return;
if (user != null)
{
if (!user.TryGetComponent(out HandsComponent? hands)
|| !hands.PutInHand(bulb.Owner.GetComponent<ItemComponent>()))
bulb.Owner.Transform.Coordinates = user.Transform.Coordinates;
}
else
{
bulb.Owner.Transform.Coordinates = Owner.Transform.Coordinates;
}
}
/// <summary>
/// For attaching UpdateLight() to events.
/// </summary>
public void UpdateLight(object? sender, EventArgs? e)
{
UpdateLight();
}
/// <summary>
/// Updates the light's power drain, sprite and actual light state.
/// </summary>
public void UpdateLight()
{
var powerReceiver = Owner.GetComponent<PowerReceiverComponent>();
if (LightBulb == null) // No light bulb.
{
_currentLit = false;
powerReceiver.Load = 0;
_appearance?.SetData(PoweredLightVisuals.BulbState, PoweredLightState.Empty);
return;
}
switch (LightBulb.State)
{
case LightBulbState.Normal:
if (powerReceiver.Powered && _on)
{
_currentLit = true;
powerReceiver.Load = LightBulb.PowerUse;
_appearance?.SetData(PoweredLightVisuals.BulbState, PoweredLightState.On);
_appearance?.SetData(PoweredLightVisuals.BulbColor, LightBulb.Color);
var time = _gameTiming.CurTime;
if (time > _lastThunk + _thunkDelay)
{
_lastThunk = time;
SoundSystem.Play(Filter.Pvs(Owner), "/Audio/Machines/light_tube_on.ogg", Owner, AudioParams.Default.WithVolume(-10f));
}
}
else
{
_currentLit = false;
_appearance?.SetData(PoweredLightVisuals.BulbState, PoweredLightState.Off);
}
break;
case LightBulbState.Broken:
_currentLit = false;
_appearance?.SetData(PoweredLightVisuals.BulbState, PoweredLightState.Broken);
break;
case LightBulbState.Burned:
_currentLit = false;
_appearance?.SetData(PoweredLightVisuals.BulbState, PoweredLightState.Burned);
break;
}
}
public override void Initialize()
{
base.Initialize();
_lightBulbContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, "light_bulb");
}
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
{
case PowerChangedMessage:
UpdateLight();
break;
case DamageChangedMessage msg:
TryDestroyBulb(msg);
break;
}
}
private void TryDestroyBulb(DamageChangedMessage msg)
{
if (!msg.TookDamage)
return;
if (LightBulb == null || LightBulb.State == LightBulbState.Broken)
return;
LightBulb.State = LightBulbState.Broken;
LightBulb.PlayBreakSound();
UpdateLight();
}
void IMapInit.MapInit()
{
if (_hasLampOnSpawn)
{
var prototype = _bulbType switch
{
LightBulbType.Bulb => "LightBulb",
LightBulbType.Tube => "LightTube",
_ => throw new ArgumentOutOfRangeException()
};
var entity = Owner.EntityManager.SpawnEntity(prototype, Owner.Transform.Coordinates);
_lightBulbContainer.Insert(entity);
}
// need this to update visualizers
UpdateLight();
}
public void TriggerSignal(bool signal)
{
_on = signal;
UpdateLight();
}
public void TriggerSignal(ToggleSignal signal)
{
_on = !_on;
UpdateLight();
}
public void ToggleBlinkingLight(bool isNowBlinking)
{
if (_isBlinking == isNowBlinking)
return;
_isBlinking = isNowBlinking;
_appearance?.SetData(PoweredLightVisuals.Blinking, _isBlinking);
}
public bool AffectedByGhostBoo(InstantActionEventArgs args)
{
if (_ignoreGhostsBoo)
return false;
// check cooldown first to prevent abuse
var time = _gameTiming.CurTime;
if (_lastGhostBlink != null)
{
if (time <= _lastGhostBlink + ghostBlinkingCooldown)
return false;
}
_lastGhostBlink = time;
ToggleBlinkingLight(true);
Owner.SpawnTimer(ghostBlinkingTime, () => {
ToggleBlinkingLight(false);
});
return true;
}
}
}

View File

@@ -1,45 +0,0 @@
#nullable enable
using Content.Server.GameObjects.Components.Weapon.Ranged.Barrels;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.GameObjects;
namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerReceiverUsers
{
/// <summary>
/// Recharges the battery in a <see cref="ServerBatteryBarrelComponent"/>.
/// </summary>
[RegisterComponent]
[ComponentReference(typeof(IActivate))]
[ComponentReference(typeof(BaseCharger))]
public sealed class WeaponCapacitorChargerComponent : BaseCharger
{
public override string Name => "WeaponCapacitorCharger";
protected override bool IsEntityCompatible(IEntity entity)
{
return entity.TryGetComponent(out ServerBatteryBarrelComponent? battery) && battery.PowerCell != null ||
entity.TryGetComponent(out PowerCellSlotComponent? slot) && slot.HasCell;
}
protected override BatteryComponent? GetBatteryFrom(IEntity entity)
{
if (entity.TryGetComponent(out PowerCellSlotComponent? slot))
{
if (slot.Cell != null)
{
return slot.Cell;
}
}
if (entity.TryGetComponent(out ServerBatteryBarrelComponent? battery))
{
if (battery.PowerCell != null)
{
return battery.PowerCell;
}
}
return null;
}
}
}