Power Cell Refactor (#5943)

Co-authored-by: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
This commit is contained in:
Leon Friedrich
2022-01-05 17:20:25 +13:00
committed by GitHub
parent 4eddefdda1
commit 0aa4f9efbe
37 changed files with 673 additions and 987 deletions

View File

@@ -1,28 +0,0 @@
using Content.Server.Power.Components;
using Content.Shared.Interaction;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.PowerCell.Components
{
/// <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";
public override bool IsEntityCompatible(EntityUid entity)
{
return IoCManager.Resolve<IEntityManager>().HasComponent<BatteryComponent>(entity);
}
protected override BatteryComponent GetBatteryFrom(EntityUid entity)
{
return IoCManager.Resolve<IEntityManager>().GetComponent<BatteryComponent>(entity);
}
}
}

View File

@@ -1,108 +0,0 @@
using System;
using Content.Server.Explosion.EntitySystems;
using Content.Server.Power.Components;
using Content.Shared.Examine;
using Content.Shared.PowerCell;
using Content.Shared.Rounding;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.PowerCell.Components
{
/// <summary>
/// Batteries that can update an <see cref="AppearanceComponent"/> based on their charge percent
/// and fit into a <see cref="PowerCellSlotComponent"/> of the appropriate size.
/// </summary>
[RegisterComponent]
[ComponentReference(typeof(BatteryComponent))]
#pragma warning disable 618
public class PowerCellComponent : BatteryComponent, IExamine
#pragma warning restore 618
{
public override string Name => "PowerCell";
public const string SolutionName = "powerCell";
[ViewVariables] public PowerCellSize CellSize => _cellSize;
[DataField("cellSize")]
private PowerCellSize _cellSize = PowerCellSize.Small;
[ViewVariables] public bool IsRigged { get; set; }
protected override void Initialize()
{
base.Initialize();
CurrentCharge = MaxCharge;
UpdateVisuals();
}
protected override void OnChargeChanged()
{
base.OnChargeChanged();
UpdateVisuals();
}
public override bool TryUseCharge(float chargeToUse)
{
if (IsRigged)
{
Explode();
return false;
}
return base.TryUseCharge(chargeToUse);
}
public override float UseCharge(float toDeduct)
{
if (IsRigged)
{
Explode();
return 0;
}
return base.UseCharge(toDeduct);
}
private void Explode()
{
var heavy = (int) Math.Ceiling(Math.Sqrt(CurrentCharge) / 60);
var light = (int) Math.Ceiling(Math.Sqrt(CurrentCharge) / 30);
CurrentCharge = 0;
EntitySystem.Get<ExplosionSystem>().SpawnExplosion(Owner, 0, heavy, light, light*2);
IoCManager.Resolve<IEntityManager>().DeleteEntity(Owner);
}
private void UpdateVisuals()
{
if (IoCManager.Resolve<IEntityManager>().TryGetComponent(Owner, out AppearanceComponent? appearance))
{
appearance.SetData(PowerCellVisuals.ChargeLevel, GetLevel(CurrentCharge / MaxCharge));
}
}
private byte GetLevel(float fraction)
{
return (byte) ContentHelpers.RoundToNearestLevels(fraction, 1, SharedPowerCell.PowerCellVisualsLevels);
}
void IExamine.Examine(FormattedMessage message, bool inDetailsRange)
{
if (inDetailsRange)
{
message.AddMarkup(Loc.GetString("power-cell-component-examine-details", ("currentCharge", $"{CurrentCharge / MaxCharge * 100:F0}")));
}
}
}
public enum PowerCellSize
{
Small,
Medium,
Large
}
}

View File

@@ -1,221 +0,0 @@
using System;
using Content.Server.Hands.Components;
using Content.Shared.Audio;
using Content.Shared.Examine;
using Content.Shared.Item;
using Content.Shared.Sound;
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.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.PowerCell.Components
{
/// <summary>
/// Provides a "battery compartment" that can contain a <see cref="PowerCellComponent"/> of the matching
/// <see cref="PowerCellSize"/>. Intended to supplement other components, not very useful by itself.
/// </summary>
[RegisterComponent]
#pragma warning disable 618
public class PowerCellSlotComponent : Component, IExamine, IMapInit
#pragma warning restore 618
{
[Dependency] private readonly IEntityManager _entities = default!;
public override string Name => "PowerCellSlot";
/// <summary>
/// What size of cell fits into this component.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("slotSize")]
public PowerCellSize SlotSize { get; set; } = PowerCellSize.Small;
/// <summary>
/// Can the cell be removed ?
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("canRemoveCell")]
public bool CanRemoveCell { get; set; } = true;
/// <summary>
/// Should the "Remove cell" verb be displayed on this component?
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("showVerb")]
public bool ShowVerb { get; set; } = true;
/// <summary>
/// String passed to <see><cref>String.Format</cref></see> when showing the description text for this item.
/// String.Format is given a single parameter which is the size letter (S/M/L) of the cells this component uses.
/// Use null to show no text.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("descFormatString")]
public string? DescFormatString { get; set; } = "It uses size {0} power cells.";
/// <summary>
/// File path to a sound file that should be played when the cell is removed.
/// </summary>
/// <example>"/Audio/Items/pistol_magout.ogg"</example>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("cellRemoveSound")]
public SoundSpecifier CellRemoveSound { get; set; } = new SoundPathSpecifier("/Audio/Items/pistol_magin.ogg");
/// <summary>
/// File path to a sound file that should be played when a cell is inserted.
/// </summary>
/// <example>"/Audio/Items/pistol_magin.ogg"</example>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("cellInsertSound")]
public SoundSpecifier CellInsertSound { get; set; } = new SoundPathSpecifier("/Audio/Items/pistol_magout.ogg");
[ViewVariables] private ContainerSlot _cellContainer = default!;
[ViewVariables]
public PowerCellComponent? Cell
{
get
{
if (_cellContainer.ContainedEntity == null) return null;
return _entities.TryGetComponent(_cellContainer.ContainedEntity.Value, out PowerCellComponent? cell) ? cell : null;
}
}
[ViewVariables] public bool HasCell => Cell != null;
/// <summary>
/// True if we don't want a cell inserted during map init.
/// </summary>
[DataField("startEmpty")]
private bool _startEmpty = false;
/// <summary>
/// If not null, this cell type will be inserted at MapInit instead of the default Standard cell.
/// </summary>
[DataField("startingCellType")]
private string? _startingCellType = null;
protected override void Initialize()
{
base.Initialize();
_cellContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, "cellslot_cell_container", out _);
}
void IExamine.Examine(FormattedMessage message, bool inDetailsRange)
{
if (!inDetailsRange) return;
string sizeLetter = SlotSize switch
{
PowerCellSize.Small => Loc.GetString("power-cell-slot-component-small-size-shorthand"),
PowerCellSize.Medium => Loc.GetString("power-cell-slot-component-medium-size-shorthand"),
PowerCellSize.Large => Loc.GetString("power-cell-slot-component-large-size-shorthand"),
_ => "???"
};
if (DescFormatString != null) message.AddMarkup(string.Format(DescFormatString, sizeLetter));
}
/// <summary>
/// Remove the cell from this component. If a user is specified, the cell will be put in their hands
/// or failing that, at their feet. If no user is specified the cell will be put at the location of
/// the parent of this component.
/// </summary>
/// <param name="user">(optional) the user to give the removed cell to.</param>
/// <param name="playSound">Should <see cref="CellRemoveSound"/> be played upon removal?</param>
/// <returns>The cell component of the entity that was removed, or null if removal failed.</returns>
public PowerCellComponent? EjectCell(EntityUid? user = null, bool playSound = true)
{
var cell = Cell;
if (cell == null || !CanRemoveCell) return null;
if (!_cellContainer.Remove(cell.Owner)) return null;
//Dirty();
if (user != null)
{
if (!_entities.TryGetComponent(user, out HandsComponent? hands) || !hands.PutInHand(_entities.GetComponent<SharedItemComponent>(cell.Owner)))
{
_entities.GetComponent<TransformComponent>(cell.Owner).Coordinates = _entities.GetComponent<TransformComponent>(user.Value).Coordinates;
}
}
else
{
_entities.GetComponent<TransformComponent>(cell.Owner).Coordinates = _entities.GetComponent<TransformComponent>(Owner).Coordinates;
}
if (playSound)
{
SoundSystem.Play(Filter.Pvs(Owner), CellRemoveSound.GetSound(), Owner, AudioHelpers.WithVariation(0.125f));
}
_entities.EventBus.RaiseLocalEvent(Owner, new PowerCellChangedEvent(true), false);
return cell;
}
/// <summary>
/// Tries to insert the given cell into this component. The cell will be put into the container of this component.
/// </summary>
/// <param name="cell">The cell to insert.</param>
/// <param name="playSound">Should <see cref="CellInsertSound"/> be played upon insertion?</param>
/// <returns>True if insertion succeeded; false otherwise.</returns>
public bool InsertCell(EntityUid cell, bool playSound = true)
{
if (Cell != null) return false;
if (!_entities.HasComponent<SharedItemComponent>(cell)) return false;
if (!_entities.TryGetComponent<PowerCellComponent?>(cell, out var cellComponent)) return false;
if (cellComponent.CellSize != SlotSize) return false;
if (!_cellContainer.Insert(cell)) return false;
//Dirty();
if (playSound)
{
SoundSystem.Play(Filter.Pvs(Owner), CellInsertSound.GetSound(), Owner, AudioHelpers.WithVariation(0.125f));
}
_entities.EventBus.RaiseLocalEvent(Owner, new PowerCellChangedEvent(false), false);
return true;
}
void IMapInit.MapInit()
{
if (_startEmpty || _cellContainer.ContainedEntity != null)
{
return;
}
string type;
if (_startingCellType != null)
{
type = _startingCellType;
}
else
{
type = SlotSize switch
{
PowerCellSize.Small => "PowerCellSmallStandard",
PowerCellSize.Medium => "PowerCellMediumStandard",
PowerCellSize.Large => "PowerCellLargeStandard",
_ => throw new ArgumentOutOfRangeException()
};
}
var cell = _entities.SpawnEntity(type, _entities.GetComponent<TransformComponent>(Owner).Coordinates);
_cellContainer.Insert(cell);
}
}
public class PowerCellChangedEvent : EntityEventArgs
{
/// <summary>
/// If true, the cell was ejected; if false, it was inserted.
/// </summary>
public bool Ejected { get; }
public PowerCellChangedEvent(bool ejected)
{
Ejected = ejected;
}
}
}

View File

@@ -1,68 +1,98 @@
using Content.Server.Administration.Logs;
using Content.Server.Chemistry.EntitySystems;
using Content.Server.PowerCell.Components;
using Content.Shared.ActionBlocker;
using Content.Shared.Verbs;
using JetBrains.Annotations;
using Content.Server.Explosion.EntitySystems;
using Content.Server.Power.Components;
using Content.Shared.Database;
using Content.Shared.Examine;
using Content.Shared.PowerCell;
using Content.Shared.PowerCell.Components;
using Content.Shared.Rounding;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using System;
using System.Diagnostics.CodeAnalysis;
namespace Content.Server.PowerCell
namespace Content.Server.PowerCell;
public class PowerCellSystem : SharedPowerCellSystem
{
[UsedImplicitly]
public class PowerCellSystem : EntitySystem
[Dependency] private readonly SolutionContainerSystem _solutionsSystem = default!;
[Dependency] private readonly ExplosionSystem _explosionSystem = default!;
[Dependency] private readonly AdminLogSystem _logSystem = default!;
public override void Initialize()
{
[Dependency] private readonly SolutionContainerSystem _solutionsSystem = default!;
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
base.Initialize();
public override void Initialize()
SubscribeLocalEvent<PowerCellComponent, ChargeChangedEvent>(OnChargeChanged);
SubscribeLocalEvent<PowerCellComponent, SolutionChangedEvent>(OnSolutionChange);
SubscribeLocalEvent<PowerCellComponent, ExaminedEvent>(OnCellExamined);
}
private void OnChargeChanged(EntityUid uid, PowerCellComponent component, ChargeChangedEvent args)
{
if (component.IsRigged)
{
base.Initialize();
SubscribeLocalEvent<PowerCellComponent, SolutionChangedEvent>(OnSolutionChange);
SubscribeLocalEvent<PowerCellSlotComponent, GetAlternativeVerbsEvent>(AddEjectVerb);
SubscribeLocalEvent<PowerCellSlotComponent, GetInteractionVerbsEvent>(AddInsertVerb);
Explode(uid);
return;
}
// TODO VERBS EJECTABLES Standardize eject/insert verbs into a single system?
private void AddEjectVerb(EntityUid uid, PowerCellSlotComponent component, GetAlternativeVerbsEvent args)
{
if (args.Hands == null ||
!args.CanAccess ||
!args.CanInteract ||
!component.ShowVerb ||
!component.HasCell ||
!_actionBlockerSystem.CanPickup(args.User))
return;
if (!TryComp(uid, out BatteryComponent? battery))
return;
Verb verb = new();
verb.Text = component.Cell!.Name;
verb.Category = VerbCategory.Eject;
verb.Act = () => component.EjectCell(args.User);
args.Verbs.Add(verb);
if (!TryComp(uid, out AppearanceComponent? appearance))
return;
var frac = battery.CurrentCharge / battery.MaxCharge;
var level = (byte) ContentHelpers.RoundToNearestLevels(frac, 1, PowerCellComponent.PowerCellVisualsLevels);
appearance.SetData(PowerCellVisuals.ChargeLevel, level);
}
private void Explode(EntityUid uid, BatteryComponent? battery = null)
{
_logSystem.Add(LogType.Explosion, LogImpact.High, $"Sabotaged power cell {ToPrettyString(uid)} is exploding");
if (!Resolve(uid, ref battery))
return;
var heavy = (int) Math.Ceiling(Math.Sqrt(battery.CurrentCharge) / 60);
var light = (int) Math.Ceiling(Math.Sqrt(battery.CurrentCharge) / 30);
_explosionSystem.SpawnExplosion(uid, 0, heavy, light, light * 2);
QueueDel(uid);
}
public bool TryGetBatteryFromSlot(EntityUid uid, [NotNullWhen(true)] out BatteryComponent? battery, PowerCellSlotComponent? component = null)
{
if (!Resolve(uid, ref component, false))
{
battery = null;
return false;
}
private void AddInsertVerb(EntityUid uid, PowerCellSlotComponent component, GetInteractionVerbsEvent args)
{
if (args.Using is not {Valid: true} @using ||
!args.CanAccess ||
!args.CanInteract ||
component.HasCell ||
!EntityManager.HasComponent<PowerCellComponent>(@using) ||
!_actionBlockerSystem.CanDrop(args.User))
return;
return TryComp(component.CellSlot.Item, out battery);
}
Verb verb = new();
verb.Text = EntityManager.GetComponent<MetaDataComponent>(@using).EntityName;
verb.Category = VerbCategory.Insert;
verb.Act = () => component.InsertCell(@using);
args.Verbs.Add(verb);
}
private void OnSolutionChange(EntityUid uid, PowerCellComponent component, SolutionChangedEvent args)
{
component.IsRigged = _solutionsSystem.TryGetSolution(uid, PowerCellComponent.SolutionName, out var solution)
&& solution.ContainsReagent("Plasma", out var plasma)
&& plasma >= 5;
private void OnSolutionChange(EntityUid uid, PowerCellComponent component, SolutionChangedEvent args)
if (component.IsRigged)
{
component.IsRigged = _solutionsSystem.TryGetSolution(uid, PowerCellComponent.SolutionName, out var solution)
&& solution.ContainsReagent("Plasma", out var plasma)
&& plasma >= 5;
_logSystem.Add(LogType.Explosion, LogImpact.Medium, $"Power cell {ToPrettyString(uid)} has been rigged up to explode when used.");
}
}
private void OnCellExamined(EntityUid uid, PowerCellComponent component, ExaminedEvent args)
{
if (!TryComp(uid, out BatteryComponent? battery))
return;
var charge = battery.CurrentCharge / battery.MaxCharge * 100;
args.PushMarkup(Loc.GetString("power-cell-component-examine-details", ("currentCharge", $"{charge:F0}")));
}
}