fish-station/Content.Shared/Charges/Systems/SharedChargesSystem.cs
Vigers Ray b378fdc706 Merge remote-tracking branch 'space-wizards/master'
# Conflicts:
#	Content.Client/Entry/EntryPoint.cs
#	Content.Client/Stylesheets/StyleNano.cs
#	Content.Server/Nutrition/EntitySystems/CreamPieSystem.cs
#	Content.Server/Nutrition/EntitySystems/FoodSystem.cs
#	Content.Shared/Follower/FollowerSystem.cs
#	Content.Shared/Humanoid/SharedHumanoidAppearanceSystem.cs
#	Content.Shared/Inventory/InventorySystem.Relay.cs
#	Content.Shared/PAI/SharedPAISystem.cs
#	Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.cs
#	Resources/Locale/en-US/_strings/access/components/genpop.ftl
#	Resources/Locale/en-US/_strings/doors/components/turnstile.ftl
#	Resources/Locale/en-US/_strings/power/battery.ftl
#	Resources/Locale/en-US/_strings/store/pai-catalog.ftl
#	Resources/Locale/en-US/_strings/tools/simple-tool-usage.ftl
#	Resources/Locale/en-US/_strings/ui/controls.ftl
#	Resources/Locale/en-US/_strings/weapons/ranged/upgrades.ftl
#	Resources/Prototypes/Access/misc.yml
#	Resources/Prototypes/Catalog/Cargo/cargo_vending.yml
#	Resources/Prototypes/Entities/Clothing/OuterClothing/coats.yml
#	Resources/Prototypes/Entities/Clothing/OuterClothing/hardsuits.yml
#	Resources/Prototypes/Entities/Markers/Spawners/Random/crates.yml
#	Resources/Prototypes/Entities/Mobs/Player/admin_ghost.yml
#	Resources/Prototypes/Entities/Mobs/Player/dragon.yml
#	Resources/Prototypes/Entities/Mobs/Player/humanoid.yml
#	Resources/Prototypes/Entities/Objects/Fun/toys.yml
#	Resources/Prototypes/Entities/Objects/Misc/paper.yml
#	Resources/Prototypes/Entities/Objects/Specific/Robotics/borg_modules.yml
#	Resources/Prototypes/Entities/Objects/Tools/access_configurator.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Magazines/shotgun.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Battery/battery_guns.yml
#	Resources/Prototypes/Entities/Stations/base.yml
#	Resources/Prototypes/Entities/Structures/Walls/asteroid.yml
#	Resources/Prototypes/Loadouts/role_loadouts.yml
#	Resources/Prototypes/Recipes/Lathes/Packs/science.yml
#	Resources/Prototypes/Research/industrial.yml
#	Resources/Prototypes/Species/vox.yml
#	Resources/Prototypes/Store/currency.yml
#	Resources/Prototypes/Voice/speech_emotes.yml
#	Resources/Prototypes/ai_factions.yml
#	Resources/Prototypes/borg_types.yml
#	Resources/Textures/Clothing/Head/Hats/captain.rsi/meta.json
#	Resources/Textures/Clothing/Uniforms/Jumpskirt/capformaldress.rsi/meta.json
#	Resources/Textures/Clothing/Uniforms/Jumpskirt/captain.rsi/meta.json
#	Resources/Textures/Clothing/Uniforms/Jumpskirt/paramedic.rsi/equipped-INNERCLOTHING.png
#	Resources/Textures/Clothing/Uniforms/Jumpskirt/paramedic.rsi/icon.png
#	Resources/Textures/Clothing/Uniforms/Jumpsuit/capformal.rsi/equipped-INNERCLOTHING-monkey.png
#	Resources/Textures/Clothing/Uniforms/Jumpsuit/capformal.rsi/meta.json
#	Resources/Textures/Clothing/Uniforms/Jumpsuit/captain.rsi/equipped-INNERCLOTHING-monkey.png
#	Resources/Textures/Clothing/Uniforms/Jumpsuit/captain.rsi/meta.json
#	Resources/Textures/Clothing/Uniforms/Jumpsuit/paramedic.rsi/equipped-INNERCLOTHING-monkey.png
#	Resources/Textures/Clothing/Uniforms/Jumpsuit/paramedic.rsi/equipped-INNERCLOTHING.png
#	Resources/Textures/Clothing/Uniforms/Jumpsuit/paramedic.rsi/icon.png
#	Resources/Textures/Clothing/Uniforms/Jumpsuit/paramedic.rsi/meta.json
#	Resources/Textures/Objects/Weapons/Melee/captain_sabre.rsi/meta.json
#	Resources/Textures/Objects/Weapons/Melee/energykatana.rsi/meta.json
#	Resources/Textures/Objects/Weapons/Melee/katana.rsi/meta.json
#	Resources/Textures/Objects/Weapons/Melee/machete.rsi/meta.json
#	Resources/Textures/Structures/Doors/Airlocks/Glass/firelock.rsi/closed.png
#	Resources/Textures/Structures/Doors/Airlocks/Glass/firelock.rsi/closing.png
#	Resources/Textures/Structures/Doors/Airlocks/Glass/firelock.rsi/deny.png
#	Resources/Textures/Structures/Doors/Airlocks/Glass/firelock.rsi/locked.png
#	Resources/Textures/Structures/Doors/Airlocks/Glass/firelock.rsi/opening.png
#	Resources/Textures/Structures/Doors/Airlocks/Standard/atmospherics.rsi/assembly.png
#	Resources/Textures/Structures/Doors/Airlocks/Standard/atmospherics.rsi/closed.png
#	Resources/Textures/Structures/Doors/Airlocks/Standard/atmospherics.rsi/closing.png
#	Resources/Textures/Structures/Doors/Airlocks/Standard/atmospherics.rsi/meta.json
#	Resources/Textures/Structures/Doors/Airlocks/Standard/atmospherics.rsi/open.png
#	Resources/Textures/Structures/Doors/Airlocks/Standard/atmospherics.rsi/opening.png
#	Resources/Textures/Structures/Doors/Airlocks/Standard/firelock.rsi/closed.png
#	Resources/Textures/Structures/Doors/Airlocks/Standard/firelock.rsi/closing.png
#	Resources/Textures/Structures/Doors/Airlocks/Standard/firelock.rsi/opening.png
#	Resources/Textures/Structures/Wallmounts/posters.rsi/poster4_contraband.png
#	Resources/migration.yml
2025-04-28 01:29:55 +03:00

246 lines
8 KiB
C#

using Content.Shared.Actions;
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
{
[Dependency] protected readonly IGameTiming _timing = default!;
[Dependency] private readonly SharedActionsSystem _actionsSystem = 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();
SubscribeLocalEvent<LimitedChargesComponent, ExaminedEvent>(OnExamine);
SubscribeLocalEvent<LimitedChargesComponent, ActionAttemptEvent>(OnChargesAttempt);
SubscribeLocalEvent<LimitedChargesComponent, MapInitEvent>(OnChargesMapInit);
SubscribeLocalEvent<LimitedChargesComponent, ActionPerformedEvent>(OnChargesPerformed);
}
private void OnExamine(EntityUid uid, LimitedChargesComponent comp, ExaminedEvent args)
{
if (!args.IsInDetailsRange)
return;
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-max-charges"));
}
// only show the recharging info if it's not full
if (charges == comp.MaxCharges || !Resolve(uid, ref rechargeEnt.Comp2, false))
return;
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;
}
}
private void OnChargesPerformed(Entity<LimitedChargesComponent> ent, ref ActionPerformedEvent args)
{
if (HasComp<DeleteWithoutChargesComponent>(ent.Owner) && (ent.Comp.LastCharges - 1 <= 0))
{
_actionsSystem.RemoveAction(args.Performer, ent.Owner);
return;
}
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?, AutoRechargeComponent?> action, int addCharges)
{
if (addCharges == 0)
return;
action.Comp1 ??= EnsureComp<LimitedChargesComponent>(action.Owner);
var lastCharges = GetCurrentCharges(action);
var charges = lastCharges + addCharges;
if (lastCharges == charges)
return;
// If we were at max then need to reset the timer.
if (charges == action.Comp1.MaxCharges || lastCharges == action.Comp1.MaxCharges)
{
action.Comp1.LastUpdate = _timing.CurTime;
action.Comp1.LastCharges = action.Comp1.MaxCharges;
}
// If it has auto-recharge then make up the difference.
else if (Resolve(action.Owner, ref action.Comp2, false))
{
var duration = action.Comp2.RechargeDuration;
var diff = (_timing.CurTime - action.Comp1.LastUpdate);
var remainder = (int) (diff / duration);
action.Comp1.LastCharges += remainder;
action.Comp1.LastUpdate += (remainder * duration);
}
action.Comp1.LastCharges = Math.Clamp(action.Comp1.LastCharges + addCharges, 0, action.Comp1.MaxCharges);
Dirty(action.Owner, action.Comp1);
}
public bool TryUseCharge(Entity<LimitedChargesComponent?> entity)
{
return TryUseCharges(entity, 1);
}
public bool TryUseCharges(Entity<LimitedChargesComponent?> entity, int amount)
{
var current = GetCurrentCharges(entity);
if (current < amount)
{
return false;
}
AddCharges(entity, -amount);
return true;
}
[Pure]
public bool IsEmpty(Entity<LimitedChargesComponent?> entity)
{
return GetCurrentCharges(entity) == 0;
}
/// <summary>
/// Resets action charges to MaxCharges.
/// </summary>
public void ResetCharges(Entity<LimitedChargesComponent?> action)
{
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);
}
}