Proto-kinetic crusher (#16277)

Co-authored-by: AJCM-git <60196617+AJCM-git@users.noreply.github.com>
This commit is contained in:
metalgearsloth
2023-05-14 13:15:18 +10:00
committed by GitHub
parent 356bf96039
commit 6417bb4fa0
68 changed files with 926 additions and 312 deletions

View File

@@ -1,33 +0,0 @@
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
namespace Content.Server.PowerCell;
/// <summary>
/// Indicates that the entity's ActivatableUI requires power or else it closes.
/// </summary>
[RegisterComponent, Access(typeof(PowerCellSystem))]
public sealed class PowerCellDrawComponent : Component
{
[ViewVariables(VVAccess.ReadWrite), DataField("enabled")]
public bool Enabled;
/// <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;
/// <summary>
/// When the next automatic power draw will occur
/// </summary>
[DataField("nextUpdate", customTypeSerializer: typeof(TimeOffsetSerializer))]
public TimeSpan NextUpdateTime;
}

View File

@@ -0,0 +1,91 @@
using Content.Server.Power.Components;
using Content.Shared.PowerCell;
using Content.Shared.PowerCell.Components;
namespace Content.Server.PowerCell;
public sealed partial class PowerCellSystem
{
/*
* Handles PowerCellDraw
*/
private static readonly TimeSpan Delay = TimeSpan.FromSeconds(1);
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.Drawing)
continue;
if (_timing.CurTime < comp.NextUpdateTime)
continue;
comp.NextUpdateTime += Delay;
if (!TryGetBatteryFromSlot(uid, out var batteryEnt, out var battery, slot))
continue;
if (_battery.TryUseCharge(batteryEnt.Value, comp.DrawRate, battery))
continue;
comp.Drawing = false;
var ev = new PowerCellSlotEmptyEvent();
RaiseLocalEvent(uid, ref ev);
}
}
private void OnUnpaused(EntityUid uid, PowerCellDrawComponent component, ref EntityUnpausedEvent args)
{
component.NextUpdateTime += args.PausedTime;
}
private void OnDrawChargeChanged(EntityUid uid, PowerCellDrawComponent component, ref ChargeChangedEvent args)
{
// Update the bools for client prediction.
bool canDraw;
bool canUse;
if (component.UseRate > 0f)
{
canUse = args.Charge > component.UseRate;
}
else
{
canUse = true;
}
if (component.DrawRate > 0f)
{
canDraw = args.Charge > 0f;
}
else
{
canDraw = true;
}
if (canUse != component.CanUse || canDraw != component.CanDraw)
{
component.CanDraw = canDraw;
component.CanUse = canUse;
Dirty(component);
}
}
private void OnDrawCellChanged(EntityUid uid, PowerCellDrawComponent component, PowerCellChangedEvent args)
{
var canDraw = !args.Ejected && HasCharge(uid, float.MinValue);
var canUse = !args.Ejected && HasActivatableCharge(uid, component);
if (canUse != component.CanUse || canDraw != component.CanDraw)
{
component.CanDraw = canDraw;
component.CanUse = canUse;
Dirty(component);
}
}
}

View File

@@ -19,7 +19,10 @@ using Robust.Shared.Timing;
namespace Content.Server.PowerCell;
public sealed class PowerCellSystem : SharedPowerCellSystem
/// <summary>
/// Handles Power cells
/// </summary>
public sealed partial class PowerCellSystem : SharedPowerCellSystem
{
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly IGameTiming _timing = default!;
@@ -39,43 +42,19 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
SubscribeLocalEvent<PowerCellComponent, ChargeChangedEvent>(OnChargeChanged);
SubscribeLocalEvent<PowerCellComponent, SolutionChangedEvent>(OnSolutionChange);
SubscribeLocalEvent<PowerCellComponent, RejuvenateEvent>(OnRejuvenate);
SubscribeLocalEvent<PowerCellComponent, ExaminedEvent>(OnCellExamined);
SubscribeLocalEvent<PowerCellSlotComponent, ExaminedEvent>(OnCellSlotExamined);
SubscribeLocalEvent<PowerCellDrawComponent, EntityUnpausedEvent>(OnUnpaused);
SubscribeLocalEvent<PowerCellDrawComponent, ChargeChangedEvent>(OnDrawChargeChanged);
SubscribeLocalEvent<PowerCellDrawComponent, PowerCellChangedEvent>(OnDrawCellChanged);
// funny
SubscribeLocalEvent<PowerCellSlotComponent, ExaminedEvent>(OnCellSlotExamined);
SubscribeLocalEvent<PowerCellSlotComponent, BeingMicrowavedEvent>(OnSlotMicrowaved);
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 (_timing.CurTime < comp.NextUpdateTime)
continue;
comp.NextUpdateTime += TimeSpan.FromSeconds(1);
if (!TryGetBatteryFromSlot(uid, out var batteryEnt, out var battery, slot))
continue;
if (_battery.TryUseCharge(batteryEnt.Value, comp.DrawRate, battery))
continue;
comp.Enabled = false;
var ev = new PowerCellSlotEmptyEvent();
RaiseLocalEvent(uid, ref ev);
}
}
private void OnRejuvenate(EntityUid uid, PowerCellComponent component, RejuvenateEvent args)
{
component.IsRigged = false;
@@ -83,13 +62,13 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
private void OnSlotMicrowaved(EntityUid uid, PowerCellSlotComponent component, BeingMicrowavedEvent args)
{
if (_itemSlotsSystem.TryGetSlot(uid, component.CellSlotId, out ItemSlot? slot))
{
if (slot.Item == null)
return;
if (!_itemSlotsSystem.TryGetSlot(uid, component.CellSlotId, out var slot))
return;
RaiseLocalEvent(slot.Item.Value, args);
}
if (slot.Item == null)
return;
RaiseLocalEvent(slot.Item.Value, args);
}
private void OnMicrowaved(EntityUid uid, BatteryComponent component, BeingMicrowavedEvent args)
@@ -111,17 +90,14 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
return;
}
if (!TryComp(uid, out AppearanceComponent? appearance))
return;
var frac = args.Charge / args.MaxCharge;
var level = (byte) ContentHelpers.RoundToNearestLevels(frac, 1, PowerCellComponent.PowerCellVisualsLevels);
_sharedAppearanceSystem.SetData(uid, PowerCellVisuals.ChargeLevel, level, appearance);
_sharedAppearanceSystem.SetData(uid, PowerCellVisuals.ChargeLevel, level);
// If this power cell is inside a cell-slot, inform that entity that the power has changed (for updating visuals n such).
if (_containerSystem.TryGetContainingContainer(uid, out var container)
&& TryComp(container.Owner, out PowerCellSlotComponent? slot)
&& _itemSlotsSystem.TryGetSlot(container.Owner, slot.CellSlotId, out ItemSlot? itemSlot))
&& _itemSlotsSystem.TryGetSlot(container.Owner, slot.CellSlotId, out var itemSlot))
{
if (itemSlot.Item == uid)
RaiseLocalEvent(container.Owner, new PowerCellChangedEvent(false));
@@ -136,11 +112,6 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
RaiseLocalEvent(uid, ref ev);
}
private void OnUnpaused(EntityUid uid, PowerCellDrawComponent component, ref EntityUnpausedEvent args)
{
component.NextUpdateTime += args.PausedTime;
}
private void Explode(EntityUid uid, BatteryComponent? battery = null, EntityUid? cause = null)
{
if (!Resolve(uid, ref battery))
@@ -190,10 +161,10 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
public void SetPowerCellDrawEnabled(EntityUid uid, bool enabled, PowerCellDrawComponent? component = null)
{
if (!Resolve(uid, ref component, false))
if (!Resolve(uid, ref component, false) || enabled == component.Drawing)
return;
component.Enabled = enabled;
component.Drawing = enabled;
component.NextUpdateTime = _timing.CurTime;
}