Power Rework (#863)

Co-authored-by: py01 <pyronetics01@gmail.com>
This commit is contained in:
py01
2020-06-28 09:23:26 -06:00
committed by GitHub
parent ffe25de723
commit 23cc6b1d4e
154 changed files with 11253 additions and 3913 deletions

View File

@@ -0,0 +1,282 @@
using System;
using Content.Server.GameObjects.Components.Power.ApcNetComponents;
using Content.Server.GameObjects.Components.Weapon.Ranged.Barrels;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects;
using Content.Shared.GameObjects.Components.Power;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Interfaces;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.Components.Container;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Power.Chargers
{
[ComponentReference(typeof(IActivate))]
[ComponentReference(typeof(IInteractUsing))]
public abstract class BaseCharger : Component, IActivate, IInteractUsing
{
[ViewVariables]
private BatteryComponent _heldBattery;
[ViewVariables]
private ContainerSlot _container;
[ViewVariables]
private PowerReceiverComponent _powerReceiver;
[ViewVariables]
private CellChargerStatus _status;
private AppearanceComponent _appearanceComponent;
[ViewVariables]
private int _chargeRate;
[ViewVariables]
private float _transferEfficiency;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _chargeRate, "chargeRate", 100);
serializer.DataField(ref _transferEfficiency, "transferEfficiency", 0.85f);
}
public override void Initialize()
{
base.Initialize();
_powerReceiver = Owner.GetComponent<PowerReceiverComponent>();
_container = ContainerManagerComponent.Ensure<ContainerSlot>($"{Name}-powerCellContainer", Owner);
_appearanceComponent = Owner.GetComponent<AppearanceComponent>();
// Default state in the visualizer is OFF, so when this gets powered on during initialization it will generally show empty
_powerReceiver.OnPowerStateChanged += PowerUpdate;
}
public override void OnRemove()
{
_powerReceiver.OnPowerStateChanged -= PowerUpdate;
base.OnRemove();
}
bool IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
{
var result = TryInsertItem(eventArgs.Using);
if (!result)
{
var localizationManager = IoCManager.Resolve<ILocalizationManager>();
eventArgs.User.PopupMessage(Owner, localizationManager.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(object sender, PowerStateEventArgs 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.Disabled;
data.Text = "Insert";
return;
}
data.Text = $"Insert {handsComponent.GetActiveHand.Owner.Name}";
}
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.Text = "Eject";
data.Visibility = VerbVisibility.Disabled;
return;
}
data.Text = $"Eject {component._container.ContainedEntity.Name}";
}
protected override void Activate(IEntity user, BaseCharger component)
{
component.RemoveItem(user);
}
}
private CellChargerStatus GetStatus()
{
if (!_powerReceiver.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)
{
return;
}
_status = status;
switch (_status)
{
// Update load just in case
case CellChargerStatus.Off:
_powerReceiver.Load = 0;
_appearanceComponent?.SetData(CellVisual.Light, CellChargerStatus.Off);
break;
case CellChargerStatus.Empty:
_powerReceiver.Load = 0;
_appearanceComponent?.SetData(CellVisual.Light, CellChargerStatus.Empty); ;
break;
case CellChargerStatus.Charging:
_powerReceiver.Load = (int) (_chargeRate / _transferEfficiency);
_appearanceComponent?.SetData(CellVisual.Light, CellChargerStatus.Charging);
break;
case CellChargerStatus.Charged:
_powerReceiver.Load = 0;
_appearanceComponent?.SetData(CellVisual.Light, CellChargerStatus.Charged);
break;
default:
throw new ArgumentOutOfRangeException();
}
_appearanceComponent?.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 (!_powerReceiver.Powered)
{
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

@@ -0,0 +1,136 @@
using System;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.Audio;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using Robust.Shared.Utility;
namespace Content.Server.GameObjects.Components.Power
{
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
{
#pragma warning disable 649
[Dependency] private readonly IPrototypeManager _prototypeManager;
[Dependency] private readonly IRobustRandom _random;
#pragma warning restore 649
/// <summary>
/// Invoked whenever the state of the light bulb changes.
/// </summary>
public event EventHandler<EventArgs> OnLightBulbStateChange;
public event EventHandler<EventArgs> OnLightColorChange;
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";
public LightBulbType Type = LightBulbType.Tube;
private int _burningTemperature;
public int BurningTemperature => _burningTemperature;
private int _powerUse;
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 override void ExposeData(ObjectSerializer serializer)
{
serializer.DataField(ref Type, "bulb", LightBulbType.Tube);
serializer.DataField(ref _color, "color", Color.White);
serializer.DataFieldCached(ref _burningTemperature, "BurningTemperature", 1400);
serializer.DataFieldCached(ref _powerUse, "PowerUse", 40);
}
public void UpdateColor()
{
var sprite = Owner.GetComponent<SpriteComponent>();
sprite.Color = Color;
}
public override void Initialize()
{
base.Initialize();
UpdateColor();
}
public void Land(LandEventArgs eventArgs)
{
if (State == LightBulbState.Broken)
return;
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>("glassbreak");
var file = _random.Pick(soundCollection.PickFiles);
EntitySystem.Get<AudioSystem>().PlayFromEntity(file, Owner);
State = LightBulbState.Broken;
}
}
}

View File

@@ -0,0 +1,27 @@
using Content.Server.GameObjects.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Server.GameObjects.Components.Power.Chargers
{
/// <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

@@ -0,0 +1,226 @@
using System;
using Content.Server.GameObjects.Components.Power.ApcNetComponents;
using Content.Server.GameObjects.Components.Sound;
using Content.Server.GameObjects.EntitySystems;
using Content.Server.Utility;
using Content.Shared.GameObjects;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.Components.Container;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Power
{
/// <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
{
public override string Name => "PoweredLight";
private static readonly TimeSpan _thunkDelay = TimeSpan.FromSeconds(2);
private TimeSpan _lastThunk;
private LightBulbType BulbType = LightBulbType.Tube;
[ViewVariables] private ContainerSlot _lightBulbContainer;
[ViewVariables]
private LightBulbComponent LightBulb
{
get
{
if (_lightBulbContainer.ContainedEntity == null) return null;
_lightBulbContainer.ContainedEntity.TryGetComponent(out LightBulbComponent bulb);
return bulb;
}
}
public bool InteractUsing(InteractUsingEventArgs eventArgs)
{
return InsertBulb(eventArgs.Using);
}
public bool InteractHand(InteractHandEventArgs eventArgs)
{
if (!eventArgs.User.TryGetComponent(out DamageableComponent 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)
{
return _lightState && heatResistance < LightBulb.BurningTemperature;
}
void Burn()
{
damageableComponent.TakeDamage(DamageType.Heat, 20, Owner);
var audioSystem = EntitySystem.Get<AudioSystem>();
audioSystem.PlayFromEntity("/Audio/effects/lightburn.ogg", Owner);
}
void Eject()
{
EjectBulb(eventArgs.User);
UpdateLight();
}
}
/// <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)
{
if (LightBulb == null) return;
var bulb = LightBulb;
bulb.OnLightBulbStateChange -= UpdateLight;
bulb.OnLightColorChange -= UpdateLight;
if (!_lightBulbContainer.Remove(bulb.Owner)) return;
if (!user.TryGetComponent(out HandsComponent hands)
|| !hands.PutInHand(bulb.Owner.GetComponent<ItemComponent>()))
bulb.Owner.Transform.GridPosition = user.Transform.GridPosition;
}
public override void ExposeData(ObjectSerializer serializer)
{
serializer.DataField(ref BulbType, "bulb", LightBulbType.Tube);
}
/// <summary>
/// For attaching UpdateLight() to events.
/// </summary>
public void UpdateLight(object sender, EventArgs e)
{
UpdateLight();
}
private bool _lightState => Owner.GetComponent<PointLightComponent>().Enabled;
/// <summary>
/// Updates the light's power drain, sprite and actual light state.
/// </summary>
public void UpdateLight()
{
var powerReceiver = Owner.GetComponent<PowerReceiverComponent>();
var sprite = Owner.GetComponent<SpriteComponent>();
var light = Owner.GetComponent<PointLightComponent>();
if (LightBulb == null) // No light bulb.
{
powerReceiver.Load = 0;
sprite.LayerSetState(0, "empty");
light.Enabled = false;
return;
}
switch (LightBulb.State)
{
case LightBulbState.Normal:
powerReceiver.Load = LightBulb.PowerUse;
if (powerReceiver.Powered)
{
sprite.LayerSetState(0, "on");
light.Enabled = true;
light.Color = LightBulb.Color;
var time = IoCManager.Resolve<IGameTiming>().CurTime;
if (time > _lastThunk + _thunkDelay)
{
_lastThunk = time;
EntitySystem.Get<AudioSystem>().PlayFromEntity("/Audio/machines/light_tube_on.ogg", Owner, AudioParams.Default.WithVolume(-10f));
}
}
else
{
sprite.LayerSetState(0, "off");
light.Enabled = false;
}
break;
case LightBulbState.Broken:
powerReceiver.Load = 0;
sprite.LayerSetState(0, "broken");
light.Enabled = false;
break;
case LightBulbState.Burned:
powerReceiver.Load = 0;
sprite.LayerSetState(0, "burned");
light.Enabled = false;
break;
}
}
public override void Initialize()
{
base.Initialize();
Owner.GetComponent<PowerReceiverComponent>().OnPowerStateChanged += UpdateLight;
_lightBulbContainer = ContainerManagerComponent.Ensure<ContainerSlot>("light_bulb", Owner, out var existed);
if (!existed) // Insert a light tube if there wasn't any.
{
switch (BulbType)
{
case LightBulbType.Tube:
_lightBulbContainer.Insert(Owner.EntityManager.SpawnEntity("LightTube", Owner.Transform.GridPosition));
break;
case LightBulbType.Bulb:
_lightBulbContainer.Insert(Owner.EntityManager.SpawnEntity("LightBulb", Owner.Transform.GridPosition));
break;
}
}
}
public override void OnRemove()
{
Owner.GetComponent<PowerReceiverComponent>().OnPowerStateChanged -= UpdateLight;
base.OnRemove();
}
}
}

View File

@@ -0,0 +1,28 @@
using Content.Server.GameObjects.Components.Weapon.Ranged.Barrels;
using Content.Server.GameObjects.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Server.GameObjects.Components.Power.Chargers
{
/// <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.HasComponent<ServerBatteryBarrelComponent>();
}
protected override BatteryComponent GetBatteryFrom(IEntity entity)
{
return entity.GetComponent<ServerBatteryBarrelComponent>().PowerCell;
}
}
}