Action charges refactor (#33993)
* Action charges refactor - Fixes the slight godmoding of baseactioncomponent. - Gets back 1ms of server time. * chorg * Remove FrameUpdate * Fixes * More fixes * Combine * Fixes * Updates * weh * Last fixes * weh * Fix naughty * YAML fixes * This one too * Merge conflicts * This thing * Review * Fix this as well * Icon fix * weh * Review * Review * seamless * Review
This commit is contained in:
@@ -1,103 +1,232 @@
|
||||
using Content.Shared.Actions.Events;
|
||||
using Content.Shared.Charges.Components;
|
||||
using Content.Shared.Examine;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Shared.Charges.Systems;
|
||||
|
||||
public abstract class SharedChargesSystem : EntitySystem
|
||||
{
|
||||
protected EntityQuery<LimitedChargesComponent> Query;
|
||||
[Dependency] protected readonly IGameTiming _timing = default!;
|
||||
|
||||
/*
|
||||
* Despite what a bunch of systems do you don't need to continuously tick linear number updates and can just derive it easily.
|
||||
*/
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
Query = GetEntityQuery<LimitedChargesComponent>();
|
||||
|
||||
SubscribeLocalEvent<LimitedChargesComponent, ExaminedEvent>(OnExamine);
|
||||
|
||||
SubscribeLocalEvent<LimitedChargesComponent, ActionAttemptEvent>(OnChargesAttempt);
|
||||
SubscribeLocalEvent<LimitedChargesComponent, MapInitEvent>(OnChargesMapInit);
|
||||
SubscribeLocalEvent<LimitedChargesComponent, ActionPerformedEvent>(OnChargesPerformed);
|
||||
}
|
||||
|
||||
protected virtual void OnExamine(EntityUid uid, LimitedChargesComponent comp, ExaminedEvent args)
|
||||
private void OnExamine(EntityUid uid, LimitedChargesComponent comp, ExaminedEvent args)
|
||||
{
|
||||
if (!args.IsInDetailsRange)
|
||||
return;
|
||||
|
||||
using (args.PushGroup(nameof(LimitedChargesComponent)))
|
||||
var rechargeEnt = new Entity<LimitedChargesComponent?, AutoRechargeComponent?>(uid, comp, null);
|
||||
var charges = GetCurrentCharges(rechargeEnt);
|
||||
using var _ = args.PushGroup(nameof(LimitedChargesComponent));
|
||||
|
||||
args.PushMarkup(Loc.GetString("limited-charges-charges-remaining", ("charges", charges)));
|
||||
if (charges == comp.MaxCharges)
|
||||
{
|
||||
args.PushMarkup(Loc.GetString("limited-charges-charges-remaining", ("charges", comp.Charges)));
|
||||
if (comp.Charges == comp.MaxCharges)
|
||||
{
|
||||
args.PushMarkup(Loc.GetString("limited-charges-max-charges"));
|
||||
}
|
||||
args.PushMarkup(Loc.GetString("limited-charges-max-charges"));
|
||||
}
|
||||
|
||||
// only show the recharging info if it's not full
|
||||
if (charges == comp.MaxCharges || !TryComp<AutoRechargeComponent>(uid, out var recharge))
|
||||
return;
|
||||
|
||||
rechargeEnt.Comp2 = recharge;
|
||||
var timeRemaining = GetNextRechargeTime(rechargeEnt);
|
||||
args.PushMarkup(Loc.GetString("limited-charges-recharging", ("seconds", timeRemaining.TotalSeconds.ToString("F1"))));
|
||||
}
|
||||
|
||||
private void OnChargesAttempt(Entity<LimitedChargesComponent> ent, ref ActionAttemptEvent args)
|
||||
{
|
||||
if (args.Cancelled)
|
||||
return;
|
||||
|
||||
var charges = GetCurrentCharges((ent.Owner, ent.Comp, null));
|
||||
|
||||
if (charges <= 0)
|
||||
{
|
||||
args.Cancelled = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to add a number of charges. If it over or underflows it will be clamped, wasting the extra charges.
|
||||
/// </summary>
|
||||
public virtual void AddCharges(EntityUid uid, int change, LimitedChargesComponent? comp = null)
|
||||
private void OnChargesPerformed(Entity<LimitedChargesComponent> ent, ref ActionPerformedEvent args)
|
||||
{
|
||||
if (!Query.Resolve(uid, ref comp, false))
|
||||
AddCharges((ent.Owner, ent.Comp), -1);
|
||||
}
|
||||
|
||||
private void OnChargesMapInit(Entity<LimitedChargesComponent> ent, ref MapInitEvent args)
|
||||
{
|
||||
// If nothing specified use max.
|
||||
if (ent.Comp.LastCharges == 0)
|
||||
{
|
||||
ent.Comp.LastCharges = ent.Comp.MaxCharges;
|
||||
}
|
||||
// If -1 used then we don't want any.
|
||||
else if (ent.Comp.LastCharges < 0)
|
||||
{
|
||||
ent.Comp.LastCharges = 0;
|
||||
}
|
||||
|
||||
ent.Comp.LastUpdate = _timing.CurTime;
|
||||
Dirty(ent);
|
||||
}
|
||||
|
||||
[Pure]
|
||||
public bool HasCharges(Entity<LimitedChargesComponent?> action, int charges)
|
||||
{
|
||||
var current = GetCurrentCharges(action);
|
||||
|
||||
return current >= charges;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the specified charges. Does not reset the accumulator.
|
||||
/// </summary>
|
||||
public void AddCharges(Entity<LimitedChargesComponent?> action, int addCharges)
|
||||
{
|
||||
if (addCharges == 0)
|
||||
return;
|
||||
|
||||
var old = comp.Charges;
|
||||
comp.Charges = Math.Clamp(comp.Charges + change, 0, comp.MaxCharges);
|
||||
if (comp.Charges != old)
|
||||
Dirty(uid, comp);
|
||||
action.Comp ??= EnsureComp<LimitedChargesComponent>(action.Owner);
|
||||
|
||||
// 1. If we're going FROM max then set lastupdate to now (so it doesn't instantly recharge).
|
||||
// 2. If we're going TO max then also set lastupdate to now.
|
||||
// 3. Otherwise don't modify it.
|
||||
// No idea if we go to 0 but future problem.
|
||||
|
||||
var lastCharges = GetCurrentCharges(action);
|
||||
var charges = lastCharges + addCharges;
|
||||
|
||||
if (lastCharges == charges)
|
||||
return;
|
||||
|
||||
if (charges == action.Comp.MaxCharges || lastCharges == action.Comp.MaxCharges)
|
||||
{
|
||||
action.Comp.LastUpdate = _timing.CurTime;
|
||||
}
|
||||
|
||||
action.Comp.LastCharges = Math.Clamp(action.Comp.LastCharges + addCharges, 0, action.Comp.MaxCharges);
|
||||
Dirty(action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the limited charges component and returns true if there are no charges. Will return false if there is no limited charges component.
|
||||
/// </summary>
|
||||
public bool IsEmpty(EntityUid uid, LimitedChargesComponent? comp = null)
|
||||
public bool TryUseCharge(Entity<LimitedChargesComponent?> entity)
|
||||
{
|
||||
// can't be empty if there are no limited charges
|
||||
if (!Query.Resolve(uid, ref comp, false))
|
||||
return TryUseCharges(entity, 1);
|
||||
}
|
||||
|
||||
public bool TryUseCharges(Entity<LimitedChargesComponent?> entity, int amount)
|
||||
{
|
||||
var current = GetCurrentCharges(entity);
|
||||
|
||||
if (current < amount)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return comp.Charges <= 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses a single charge. Must check IsEmpty beforehand to prevent using with 0 charge.
|
||||
/// </summary>
|
||||
public void UseCharge(EntityUid uid, LimitedChargesComponent? comp = null)
|
||||
{
|
||||
AddCharges(uid, -1, comp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks IsEmpty and uses a charge if it isn't empty.
|
||||
/// </summary>
|
||||
public bool TryUseCharge(Entity<LimitedChargesComponent?> ent)
|
||||
{
|
||||
if (!Query.Resolve(ent, ref ent.Comp, false))
|
||||
return true;
|
||||
|
||||
if (IsEmpty(ent, ent.Comp))
|
||||
return false;
|
||||
|
||||
UseCharge(ent, ent.Comp);
|
||||
AddCharges(entity, -amount);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the limited charges component and returns true if the number of charges remaining is less than the specified value.
|
||||
/// Will return false if there is no limited charges component.
|
||||
/// </summary>
|
||||
public bool HasInsufficientCharges(EntityUid uid, int requiredCharges, LimitedChargesComponent? comp = null)
|
||||
[Pure]
|
||||
public bool IsEmpty(Entity<LimitedChargesComponent?> entity)
|
||||
{
|
||||
// can't be empty if there are no limited charges
|
||||
if (!Resolve(uid, ref comp, false))
|
||||
return false;
|
||||
|
||||
return comp.Charges < requiredCharges;
|
||||
return GetCurrentCharges(entity) == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses up a specified number of charges. Must check HasInsufficentCharges beforehand to prevent using with insufficient remaining charges.
|
||||
/// Resets action charges to MaxCharges.
|
||||
/// </summary>
|
||||
public virtual void UseCharges(EntityUid uid, int chargesUsed, LimitedChargesComponent? comp = null)
|
||||
public void ResetCharges(Entity<LimitedChargesComponent?> action)
|
||||
{
|
||||
AddCharges(uid, -chargesUsed, comp);
|
||||
if (!Resolve(action.Owner, ref action.Comp, false))
|
||||
return;
|
||||
|
||||
var charges = GetCurrentCharges((action.Owner, action.Comp, null));
|
||||
|
||||
if (charges == action.Comp.MaxCharges)
|
||||
return;
|
||||
|
||||
action.Comp.LastCharges = action.Comp.MaxCharges;
|
||||
action.Comp.LastUpdate = _timing.CurTime;
|
||||
Dirty(action);
|
||||
}
|
||||
|
||||
public void SetCharges(Entity<LimitedChargesComponent?> action, int value)
|
||||
{
|
||||
action.Comp ??= EnsureComp<LimitedChargesComponent>(action.Owner);
|
||||
|
||||
var adjusted = Math.Clamp(value, 0, action.Comp.MaxCharges);
|
||||
|
||||
if (action.Comp.LastCharges == adjusted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
action.Comp.LastCharges = adjusted;
|
||||
action.Comp.LastUpdate = _timing.CurTime;
|
||||
Dirty(action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The next time a charge will be considered to be filled.
|
||||
/// </summary>
|
||||
/// <returns>0 timespan if invalid or no charges to generate.</returns>
|
||||
[Pure]
|
||||
public TimeSpan GetNextRechargeTime(Entity<LimitedChargesComponent?, AutoRechargeComponent?> entity)
|
||||
{
|
||||
if (!Resolve(entity.Owner, ref entity.Comp1, ref entity.Comp2, false))
|
||||
{
|
||||
return TimeSpan.Zero;
|
||||
}
|
||||
|
||||
// Okay so essentially we need to get recharge time to full, then modulus that by the recharge timer which should be the next tick.
|
||||
var fullTime = ((entity.Comp1.MaxCharges - entity.Comp1.LastCharges) * entity.Comp2.RechargeDuration) + entity.Comp1.LastUpdate;
|
||||
var timeRemaining = fullTime - _timing.CurTime;
|
||||
|
||||
if (timeRemaining < TimeSpan.Zero)
|
||||
{
|
||||
return TimeSpan.Zero;
|
||||
}
|
||||
|
||||
var nextChargeTime = timeRemaining.TotalSeconds % entity.Comp2.RechargeDuration.TotalSeconds;
|
||||
return TimeSpan.FromSeconds(nextChargeTime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Derives the current charges of an entity.
|
||||
/// </summary>
|
||||
[Pure]
|
||||
public int GetCurrentCharges(Entity<LimitedChargesComponent?, AutoRechargeComponent?> entity)
|
||||
{
|
||||
if (!Resolve(entity.Owner, ref entity.Comp1, false))
|
||||
{
|
||||
// I'm all in favor of nullable ints however null-checking return args against comp nullability is dodgy
|
||||
// so we get this.
|
||||
return -1;
|
||||
}
|
||||
|
||||
var calculated = 0;
|
||||
|
||||
if (Resolve(entity.Owner, ref entity.Comp2, false) && entity.Comp2.RechargeDuration.TotalSeconds != 0.0)
|
||||
{
|
||||
calculated = (int)((_timing.CurTime - entity.Comp1.LastUpdate).TotalSeconds / entity.Comp2.RechargeDuration.TotalSeconds);
|
||||
}
|
||||
|
||||
return Math.Clamp(entity.Comp1.LastCharges + calculated,
|
||||
0,
|
||||
entity.Comp1.MaxCharges);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user