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,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}")));
}
}