Power cell slot QOL (#15373)

This commit is contained in:
metalgearsloth
2023-04-23 12:25:12 +10:00
committed by GitHub
parent abc84070c6
commit a4dfe8beed
17 changed files with 544 additions and 264 deletions

View File

@@ -4,11 +4,10 @@ using Content.Server.Popups;
using Robust.Shared.Player;
using Robust.Server.GameObjects;
namespace Content.Server.Eye.Blinding
{
namespace Content.Server.Eye.Blinding;
public sealed class ActivatableUIRequiresVisionSystem : EntitySystem
{
[Dependency] private readonly ActivatableUISystem _activatableUISystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
@@ -57,4 +56,3 @@ namespace Content.Server.Eye.Blinding
}
}
}
}

View File

@@ -1,6 +1,8 @@
using Content.Server.Disease;
using Content.Server.Medical.Components;
using Content.Server.Popups;
using Content.Server.PowerCell;
using Content.Server.UserInterface;
using Content.Shared.Damage;
using Content.Shared.DoAfter;
using Content.Shared.IdentityManagement;
@@ -13,28 +15,23 @@ namespace Content.Server.Medical
{
public sealed class HealthAnalyzerSystem : EntitySystem
{
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly DiseaseSystem _disease = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly PowerCellSystem _cell = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<HealthAnalyzerComponent, ActivateInWorldEvent>(HandleActivateInWorld);
SubscribeLocalEvent<HealthAnalyzerComponent, AfterInteractEvent>(OnAfterInteract);
SubscribeLocalEvent<HealthAnalyzerComponent, HealthAnalyzerDoAfterEvent>(OnDoAfter);
}
private void HandleActivateInWorld(EntityUid uid, HealthAnalyzerComponent healthAnalyzer, ActivateInWorldEvent args)
{
OpenUserInterface(args.User, healthAnalyzer);
}
private void OnAfterInteract(EntityUid uid, HealthAnalyzerComponent healthAnalyzer, AfterInteractEvent args)
{
if (args.Target == null || !args.CanReach || !HasComp<MobStateComponent>(args.Target))
if (args.Target == null || !args.CanReach || !HasComp<MobStateComponent>(args.Target) || !_cell.HasActivatableCharge(uid, user: args.User))
return;
_audio.PlayPvs(healthAnalyzer.ScanningBeginSound, uid);
@@ -49,7 +46,7 @@ namespace Content.Server.Medical
private void OnDoAfter(EntityUid uid, HealthAnalyzerComponent component, DoAfterEvent args)
{
if (args.Handled || args.Cancelled || args.Args.Target == null)
if (args.Handled || args.Cancelled || args.Args.Target == null || !_cell.TryUseActivatableCharge(uid, user: args.User))
return;
_audio.PlayPvs(component.ScanningEndSound, args.Args.User);
@@ -58,6 +55,9 @@ namespace Content.Server.Medical
// Below is for the traitor item
// Piggybacking off another component's doafter is complete CBT so I gave up
// and put it on the same component
/*
* this code is cursed wuuuuuuut
*/
if (string.IsNullOrEmpty(component.Disease))
{
args.Handled = true;
@@ -71,8 +71,6 @@ namespace Content.Server.Medical
_popupSystem.PopupEntity(Loc.GetString("disease-scanner-gave-self", ("disease", component.Disease)),
args.Args.User, args.Args.User);
}
else
{
_popupSystem.PopupEntity(Loc.GetString("disease-scanner-gave-other", ("target", Identity.Entity(args.Args.Target.Value, EntityManager)),

View File

@@ -1,4 +1,3 @@
using Content.Shared.Interaction.Events;
using Content.Shared.Pinpointer;
using Robust.Server.GameObjects;

View File

@@ -62,5 +62,5 @@ namespace Content.Server.Power.Components
/// Raised when a battery's charge or capacity changes (capacity affects relative charge percentage).
/// </summary>
[ByRefEvent]
public record struct ChargeChangedEvent(float Charge, float MaxCharge);
public readonly record struct ChargeChangedEvent(float Charge, float MaxCharge);
}

View File

@@ -0,0 +1,25 @@
namespace Content.Server.PowerCell;
/// <summary>
/// Indicates that the entity's ActivatableUI requires power or else it closes.
/// </summary>
[RegisterComponent]
public sealed class PowerCellDrawComponent : Component
{
[ViewVariables(VVAccess.ReadWrite), DataField("enabled")]
public bool Enabled = false;
/// <summary>
/// How much the entity draws while the UI is open.
/// Set to 0 if you just wish to check for power upon opening the UI.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("drawRate")]
public float DrawRate = 1f;
/// <summary>
/// How much power is used whenever the entity is "used".
/// This is used to ensure the UI won't open again without a minimum use power.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("useRate")]
public float UseRate = 0f;
}

View File

@@ -0,0 +1,7 @@
namespace Content.Server.PowerCell;
/// <summary>
/// Raised directed on an entity when its active power cell has no more charge to supply.
/// </summary>
[ByRefEvent]
public readonly record struct PowerCellSlotEmptyEvent;

View File

@@ -10,19 +10,25 @@ using Content.Shared.Rounding;
using Robust.Shared.Containers;
using System.Diagnostics.CodeAnalysis;
using Content.Server.Kitchen.Components;
using Content.Server.UserInterface;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Popups;
using Content.Shared.Rejuvenate;
using Content.Shared.UserInterface;
using Robust.Server.GameObjects;
namespace Content.Server.PowerCell;
public sealed class PowerCellSystem : SharedPowerCellSystem
{
[Dependency] private readonly ActivatableUISystem _activatable = default!;
[Dependency] private readonly SolutionContainerSystem _solutionsSystem = default!;
[Dependency] private readonly ExplosionSystem _explosionSystem = default!;
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
[Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
[Dependency] private readonly SharedAppearanceSystem _sharedAppearanceSystem = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
public override void Initialize()
{
@@ -39,6 +45,28 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
SubscribeLocalEvent<BatteryComponent, BeingMicrowavedEvent>(OnMicrowaved);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<PowerCellDrawComponent, PowerCellSlotComponent>();
while (query.MoveNext(out var uid, out var comp, out var slot))
{
if (!comp.Enabled)
continue;
if (!TryGetBatteryFromSlot(uid, out var battery, slot))
continue;
if (battery.TryUseCharge(comp.DrawRate * frameTime))
continue;
comp.Enabled = false;
var ev = new PowerCellSlotEmptyEvent();
RaiseLocalEvent(uid, ref ev);
}
}
private void OnRejuvenate(EntityUid uid, PowerCellComponent component, RejuvenateEvent args)
{
component.IsRigged = false;
@@ -102,6 +130,89 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
QueueDel(uid);
}
#region Activatable
/// <summary>
/// Returns whether the entity has a slotted battery and <see cref="PowerCellDrawComponent.UseRate"/> charge.
/// </summary>
/// <param name="user">Popup to this user with the relevant detail if specified.</param>
public bool HasActivatableCharge(EntityUid uid, PowerCellDrawComponent? battery = null, PowerCellSlotComponent? cell = null, EntityUid? user = null)
{
if (!Resolve(uid, ref battery, ref cell, false))
return false;
return HasCharge(uid, battery.UseRate, cell, user);
}
/// <summary>
/// Tries to use the <see cref="PowerCellDrawComponent.UseRate"/> for this entity.
/// </summary>
/// <param name="user">Popup to this user with the relevant detail if specified.</param>
public bool TryUseActivatableCharge(EntityUid uid, PowerCellDrawComponent? battery = null, PowerCellSlotComponent? cell = null, EntityUid? user = null)
{
if (!Resolve(uid, ref battery, ref cell, false))
return false;
if (TryUseCharge(uid, battery.UseRate, cell, user))
{
_activatable.CheckUsage(uid);
return true;
}
return false;
}
#endregion
/// <summary>
/// Returns whether the entity has a slotted battery and charge for the requested action.
/// </summary>
/// <param name="user">Popup to this user with the relevant detail if specified.</param>
public bool HasCharge(EntityUid uid, float charge, PowerCellSlotComponent? component = null, EntityUid? user = null)
{
if (!TryGetBatteryFromSlot(uid, out var battery, component))
{
if (user != null)
_popup.PopupEntity(Loc.GetString("power-cell-no-battery"), uid, user.Value);
return false;
}
if (battery.CurrentCharge < charge)
{
if (user != null)
_popup.PopupEntity(Loc.GetString("power-cell-insufficient"), uid, user.Value);
return false;
}
return true;
}
/// <summary>
/// Tries to use charge from a slotted battery.
/// </summary>
public bool TryUseCharge(EntityUid uid, float charge, PowerCellSlotComponent? component = null, EntityUid? user = null)
{
if (!TryGetBatteryFromSlot(uid, out var battery, component))
{
if (user != null)
_popup.PopupEntity(Loc.GetString("power-cell-no-battery"), uid, user.Value);
return false;
}
if (!battery.TryUseCharge(charge))
{
if (user != null)
_popup.PopupEntity(Loc.GetString("power-cell-insufficient"), uid, user.Value);
return false;
}
return true;
}
public bool TryGetBatteryFromSlot(EntityUid uid, [NotNullWhen(true)] out BatteryComponent? battery, PowerCellSlotComponent? component = null)
{
if (!Resolve(uid, ref component, false))

View File

@@ -0,0 +1,13 @@
using Content.Server.PowerCell;
using Content.Shared.UserInterface;
namespace Content.Server.UserInterface;
/// <summary>
/// Specifies that the attached entity requires <see cref="PowerCellDrawComponent"/> power.
/// </summary>
[RegisterComponent]
public sealed class ActivatableUIRequiresPowerCellComponent : Component
{
}

View File

@@ -0,0 +1,77 @@
using Content.Server.PowerCell;
using Content.Shared.PowerCell;
using Robust.Shared.Containers;
namespace Content.Server.UserInterface;
public sealed partial class ActivatableUISystem
{
[Dependency] private readonly PowerCellSystem _cell = default!;
private void InitializePower()
{
SubscribeLocalEvent<ActivatableUIRequiresPowerCellComponent, ActivatableUIOpenAttemptEvent>(OnBatteryOpenAttempt);
SubscribeLocalEvent<ActivatableUIRequiresPowerCellComponent, BoundUIOpenedEvent>(OnBatteryOpened);
SubscribeLocalEvent<ActivatableUIRequiresPowerCellComponent, BoundUIClosedEvent>(OnBatteryClosed);
SubscribeLocalEvent<PowerCellDrawComponent, EntRemovedFromContainerMessage>(OnPowerCellRemoved);
}
private void OnPowerCellRemoved(EntityUid uid, PowerCellDrawComponent component, EntRemovedFromContainerMessage args)
{
if (TryComp<PowerCellDrawComponent>(uid, out var draw))
{
draw.Enabled = false;
}
if (HasComp<ActivatableUIRequiresPowerCellComponent>(uid) &&
TryComp<ActivatableUIComponent>(uid, out var activatable) &&
activatable.Key != null)
{
_uiSystem.TryCloseAll(uid, activatable.Key);
}
}
private void OnBatteryOpened(EntityUid uid, ActivatableUIRequiresPowerCellComponent component, BoundUIOpenedEvent args)
{
if (!TryComp<PowerCellDrawComponent>(uid, out var draw))
return;
draw.Enabled = true;
}
private void OnBatteryClosed(EntityUid uid, ActivatableUIRequiresPowerCellComponent component, BoundUIClosedEvent args)
{
if (!TryComp<PowerCellDrawComponent>(uid, out var draw))
return;
draw.Enabled = false;
}
/// <summary>
/// Call if you want to check if the UI should close due to a recent battery usage.
/// </summary>
public void CheckUsage(EntityUid uid, ActivatableUIComponent? active = null, ActivatableUIRequiresPowerCellComponent? component = null, PowerCellDrawComponent? draw = null)
{
if (!Resolve(uid, ref component, ref draw, ref active, false) || active.Key == null)
return;
if (_cell.HasCharge(uid, draw.UseRate))
return;
_uiSystem.TryCloseAll(uid, active.Key);
}
private void OnBatteryOpenAttempt(EntityUid uid, ActivatableUIRequiresPowerCellComponent component, ActivatableUIOpenAttemptEvent args)
{
if (!TryComp<PowerCellDrawComponent>(uid, out var draw))
return;
// Check if we have the appropriate drawrate / userate to even open it.
if (args.Cancelled || !_cell.HasCharge(uid, MathF.Max(draw.DrawRate, draw.UseRate), user: args.User))
{
args.Cancel();
return;
}
}
}

View File

@@ -6,14 +6,12 @@ using Content.Shared.Hands.Components;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Events;
using Content.Shared.Verbs;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Server.Player;
namespace Content.Server.UserInterface
{
[UsedImplicitly]
internal sealed class ActivatableUISystem : EntitySystem
namespace Content.Server.UserInterface;
public sealed partial class ActivatableUISystem : EntitySystem
{
[Dependency] private readonly IAdminManager _adminManager = default!;
[Dependency] private readonly ActionBlockerSystem _blockerSystem = default!;
@@ -35,6 +33,8 @@ namespace Content.Server.UserInterface
SubscribeLocalEvent<ActivatableUIComponent, GetVerbsEvent<ActivationVerb>>(AddOpenUiVerb);
SubscribeLocalEvent<ServerUserInterfaceComponent, OpenUiActionEvent>(OnActionPerform);
InitializePower();
}
private void OnBoundInterfaceInteractAttempt(BoundUserInterfaceMessageAttempt ev)
@@ -228,4 +228,3 @@ namespace Content.Server.UserInterface
public sealed class ActivatableUIPlayerChangedEvent : EntityEventArgs
{
}
}

View File

@@ -1 +1,3 @@
power-cell-component-examine-details = The charge indicator reads {$currentCharge} %.
power-cell-component-examine-details = The charge indicator reads [color=#5E7C16]{$currentCharge}[/color] %.
power-cell-no-battery = No power cell found
power-cell-insufficient = Insufficient power

View File

@@ -13,4 +13,4 @@ comp-stunbaton-activated-low-charge = Insufficient charge...
stunbaton-component-low-charge = Insufficient charge...
stunbaton-component-on-examine = The light is currently [color=darkgreen]on[/color].
stunbaton-component-on-examine-charge = The charge indicator reads {$charge} %
stunbaton-component-on-examine-charge = The charge indicator reads [color=#5E7C16]{$charge}[/color] %

View File

@@ -1,8 +1,10 @@
- type: entity
id: StationMap
id: HandheldStationMap
name: station map
description: Displays a readout of the current station.
parent: BaseItem
parent:
- BaseItem
- PowerCellSlotSmallItem
suffix: Handheld
components:
- type: StationMap
@@ -13,6 +15,10 @@
- state: tablet
- state: generic
shader: unshaded
- type: PowerCellDraw
drawRate: 0
useRate: 20
- type: ActivatableUIRequiresPowerCell
- type: ActivatableUI
inHandsOnly: true
singleUser: true

View File

@@ -1,12 +1,18 @@
- type: entity
name: handheld crew monitor
parent: BaseItem
parent:
- BaseItem
- PowerCellSlotSmallItem
id: HandheldCrewMonitor
description: A hand-held crew monitor displaying the status of suit sensors.
components:
- type: Sprite
sprite: Objects/Specific/Medical/handheldcrewmonitor.rsi
state: scanner
- type: PowerCellDraw
drawRate: 0
useRate: 20
- type: ActivatableUIRequiresPowerCell
- type: ActivatableUI
key: enum.CrewMonitoringUIKey.Key
closeOnHandDeselect: false

View File

@@ -1,6 +1,8 @@
- type: entity
name: health analyzer
parent: BaseItem
parent:
- BaseItem
- PowerCellSlotSmallItem
id: HandheldHealthAnalyzer
description: A hand-held body scanner capable of distinguishing vital signs of the subject.
components:
@@ -8,6 +10,10 @@
sprite: Objects/Specific/Medical/healthanalyzer.rsi
netsync: false
state: analyzer
- type: PowerCellDraw
drawRate: 0
useRate: 20
- type: ActivatableUIRequiresPowerCell
- type: ActivatableUI
key: enum.HealthAnalyzerUiKey.Key
closeOnHandDeselect: false

View File

@@ -56,3 +56,36 @@
storagebase: !type:Container
ents: []
# PowerCellSlot parents
- type: entity
name: a
id: PowerCellSlotSmallItem
abstract: true
components:
- type: ContainerContainer
containers:
cell_slot: !type:ContainerSlot { }
- type: PowerCellSlot
cellSlotId: cell_slot
- type: ItemSlots
slots:
cell_slot:
name: power-cell-slot-component-slot-name-default
startingItem: PowerCellSmall
- type: entity
name: a
id: PowerCellSlotMediumItem
abstract: true
components:
- type: ContainerContainer
containers:
cell_slot: !type:ContainerSlot { }
- type: PowerCellSlot
cellSlotId: cell_slot
- type: ItemSlots
slots:
cell_slot:
name: power-cell-slot-component-slot-name-default
startingItem: PowerCellMedium

View File

@@ -1,5 +1,5 @@
- type: entity
id: WallStationMapBroken
id: StationMapBroken
name: station map
description: A virtual map of the surrounding station.
suffix: Wall broken
@@ -29,9 +29,9 @@
acts: [ "Destruction" ]
- type: entity
id: WallStationMap
id: StationMap
name: station map
parent: WallStationMapBroken
parent: StationMapBroken
suffix: Wall
placement:
mode: SnapgridCenter
@@ -65,7 +65,7 @@
collection: GlassBreak
- !type:SpawnEntitiesBehavior
spawn:
WallStationMapBroken:
StationMapBroken:
min: 1
max: 1
- !type:DoActsBehavior