Predict artifact crushers (#40180)

predict artifact crushers
This commit is contained in:
slarticodefast
2025-10-11 01:48:08 +02:00
committed by GitHub
parent a6dce11566
commit ca5053fe7b
3 changed files with 113 additions and 115 deletions

View File

@@ -1,87 +1,25 @@
using Content.Server.Body.Systems; using Content.Server.Body.Systems;
using Content.Server.Popups;
using Content.Server.Power.EntitySystems;
using Content.Server.Stack; using Content.Server.Stack;
using Content.Shared.Body.Components; using Content.Shared.Body.Components;
using Content.Shared.Damage;
using Content.Shared.Power;
using Content.Shared.Storage.Components; using Content.Shared.Storage.Components;
using Content.Shared.Verbs;
using Content.Shared.Whitelist; using Content.Shared.Whitelist;
using Content.Shared.Xenoarchaeology.Equipment; using Content.Shared.Xenoarchaeology.Equipment;
using Content.Shared.Xenoarchaeology.Equipment.Components; using Content.Shared.Xenoarchaeology.Equipment.Components;
using Robust.Shared.Collections; using Robust.Shared.Collections;
using Robust.Shared.Random; using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Server.Xenoarchaeology.Equipment.Systems; namespace Content.Server.Xenoarchaeology.Equipment.Systems;
/// <inheritdoc/> /// <inheritdoc/>
public sealed class ArtifactCrusherSystem : SharedArtifactCrusherSystem public sealed class ArtifactCrusherSystem : SharedArtifactCrusherSystem
{ {
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IRobustRandom _random = default!; [Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly BodySystem _body = default!; [Dependency] private readonly BodySystem _body = default!;
[Dependency] private readonly DamageableSystem _damageable = default!;
[Dependency] private readonly StackSystem _stack = default!; [Dependency] private readonly StackSystem _stack = default!;
[Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!; [Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
/// <inheritdoc/> // TODO: Move to shared once StackSystem spawning is in Shared and we have RandomPredicted
public override void Initialize() public override void FinishCrushing(Entity<ArtifactCrusherComponent, EntityStorageComponent> ent)
{
base.Initialize();
SubscribeLocalEvent<ArtifactCrusherComponent, GetVerbsEvent<AlternativeVerb>>(OnGetVerbs);
SubscribeLocalEvent<ArtifactCrusherComponent, PowerChangedEvent>(OnPowerChanged);
}
private void OnGetVerbs(Entity<ArtifactCrusherComponent> ent, ref GetVerbsEvent<AlternativeVerb> args)
{
if (!args.CanAccess || !args.CanInteract || args.Hands == null || ent.Comp.Crushing)
return;
if (!TryComp<EntityStorageComponent>(ent, out var entityStorageComp) ||
entityStorageComp.Contents.ContainedEntities.Count == 0)
return;
if (!this.IsPowered(ent, EntityManager))
return;
var verb = new AlternativeVerb
{
Text = Loc.GetString("artifact-crusher-verb-start-crushing"),
Priority = 2,
Act = () => StartCrushing((ent, ent.Comp, entityStorageComp))
};
args.Verbs.Add(verb);
}
private void OnPowerChanged(Entity<ArtifactCrusherComponent> ent, ref PowerChangedEvent args)
{
if (!args.Powered)
StopCrushing(ent);
}
public void StartCrushing(Entity<ArtifactCrusherComponent, EntityStorageComponent> ent)
{
var (uid, crusher, _) = ent;
if (crusher.Crushing)
return;
if (crusher.AutoLock)
_popup.PopupEntity(Loc.GetString("artifact-crusher-autolocks-enable"), uid);
crusher.Crushing = true;
crusher.NextSecond = _timing.CurTime + TimeSpan.FromSeconds(1);
crusher.CrushEndTime = _timing.CurTime + crusher.CrushDuration;
crusher.CrushingSoundEntity = AudioSystem.PlayPvs(crusher.CrushingSound, ent);
Appearance.SetData(ent, ArtifactCrusherVisuals.Crushing, true);
Dirty(ent, ent.Comp1);
}
public void FinishCrushing(Entity<ArtifactCrusherComponent, EntityStorageComponent> ent)
{ {
var (_, crusher, storage) = ent; var (_, crusher, storage) = ent;
StopCrushing((ent, ent.Comp1), false); StopCrushing((ent, ent.Comp1), false);
@@ -113,32 +51,4 @@ public sealed class ArtifactCrusherSystem : SharedArtifactCrusherSystem
} }
} }
} }
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<ArtifactCrusherComponent, EntityStorageComponent>();
while (query.MoveNext(out var uid, out var crusher, out var storage))
{
if (!crusher.Crushing)
continue;
if (crusher.NextSecond < _timing.CurTime)
{
var contents = new ValueList<EntityUid>(storage.Contents.ContainedEntities);
foreach (var contained in contents)
{
_damageable.TryChangeDamage(contained, crusher.CrushingDamage);
}
crusher.NextSecond += TimeSpan.FromSeconds(1);
Dirty(uid, crusher);
}
if (crusher.CrushEndTime < _timing.CurTime)
{
FinishCrushing((uid, crusher, storage));
}
}
}
} }

View File

@@ -2,7 +2,6 @@ using Content.Shared.Damage;
using Content.Shared.Stacks; using Content.Shared.Stacks;
using Content.Shared.Whitelist; using Content.Shared.Whitelist;
using Robust.Shared.Audio; using Robust.Shared.Audio;
using Robust.Shared.Audio.Components;
using Robust.Shared.Containers; using Robust.Shared.Containers;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
@@ -14,7 +13,8 @@ namespace Content.Shared.Xenoarchaeology.Equipment.Components;
/// <summary> /// <summary>
/// This is an entity storage that, when activated, crushes the artifact inside of it and gives artifact fragments. /// This is an entity storage that, when activated, crushes the artifact inside of it and gives artifact fragments.
/// </summary> /// </summary>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState] [RegisterComponent, NetworkedComponent]
[AutoGenerateComponentState, AutoGenerateComponentPause]
[Access(typeof(SharedArtifactCrusherSystem))] [Access(typeof(SharedArtifactCrusherSystem))]
public sealed partial class ArtifactCrusherComponent : Component public sealed partial class ArtifactCrusherComponent : Component
{ {
@@ -27,19 +27,21 @@ public sealed partial class ArtifactCrusherComponent : Component
/// <summary> /// <summary>
/// When the current crushing will end. /// When the current crushing will end.
/// </summary> /// </summary>
[DataField(customTypeSerializer: typeof(TimeOffsetSerializer)), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] [DataField(customTypeSerializer: typeof(TimeOffsetSerializer))]
[AutoNetworkedField, AutoPausedField]
public TimeSpan CrushEndTime; public TimeSpan CrushEndTime;
/// <summary> /// <summary>
/// The next second. Used to apply damage over time. /// The next second. Used to apply damage over time.
/// </summary> /// </summary>
[DataField(customTypeSerializer: typeof(TimeOffsetSerializer)), ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] [DataField(customTypeSerializer: typeof(TimeOffsetSerializer))]
[AutoNetworkedField, AutoPausedField]
public TimeSpan NextSecond; public TimeSpan NextSecond;
/// <summary> /// <summary>
/// The total duration of the crushing. /// The total duration of the crushing.
/// </summary> /// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] [DataField, AutoNetworkedField]
public TimeSpan CrushDuration = TimeSpan.FromSeconds(10); public TimeSpan CrushDuration = TimeSpan.FromSeconds(10);
/// <summary> /// <summary>
@@ -51,19 +53,19 @@ public sealed partial class ArtifactCrusherComponent : Component
/// <summary> /// <summary>
/// The minimum amount of fragments spawned. /// The minimum amount of fragments spawned.
/// </summary> /// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] [DataField, AutoNetworkedField]
public int MinFragments = 2; public int MinFragments = 2;
/// <summary> /// <summary>
/// The maximum amount of fragments spawned, non-inclusive. /// The maximum amount of fragments spawned, non-inclusive.
/// </summary> /// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] [DataField, AutoNetworkedField]
public int MaxFragments = 5; public int MaxFragments = 5;
/// <summary> /// <summary>
/// The material for the fragments. /// The material for the fragments.
/// </summary> /// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)] [DataField]
public ProtoId<StackPrototype> FragmentStackProtoId = "ArtifactFragment"; public ProtoId<StackPrototype> FragmentStackProtoId = "ArtifactFragment";
/// <summary> /// <summary>
@@ -100,12 +102,12 @@ public sealed partial class ArtifactCrusherComponent : Component
/// Stores entity of <see cref="CrushingSound"/> to allow ending it early. /// Stores entity of <see cref="CrushingSound"/> to allow ending it early.
/// </summary> /// </summary>
[DataField] [DataField]
public (EntityUid, AudioComponent)? CrushingSoundEntity; public EntityUid? CrushingSoundEntity;
/// <summary> /// <summary>
/// When enabled, stops the artifact crusher from being opened when it is being crushed. /// When enabled, stops the artifact crusher from being opened when it is being crushed.
/// </summary> /// </summary>
[DataField, AutoNetworkedField, ViewVariables(VVAccess.ReadWrite)] [DataField, AutoNetworkedField]
public bool AutoLock = false; public bool AutoLock = false;
} }

View File

@@ -1,9 +1,16 @@
using Content.Shared.Examine; using Content.Shared.Damage;
using Content.Shared.Storage.Components;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Containers;
using Content.Shared.Emag.Systems; using Content.Shared.Emag.Systems;
using Content.Shared.Examine;
using Content.Shared.Popups;
using Content.Shared.Power;
using Content.Shared.Power.EntitySystems;
using Content.Shared.Storage.Components;
using Content.Shared.Verbs;
using Content.Shared.Xenoarchaeology.Equipment.Components; using Content.Shared.Xenoarchaeology.Equipment.Components;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Collections;
using Robust.Shared.Containers;
using Robust.Shared.Timing;
namespace Content.Shared.Xenoarchaeology.Equipment; namespace Content.Shared.Xenoarchaeology.Equipment;
@@ -12,10 +19,14 @@ namespace Content.Shared.Xenoarchaeology.Equipment;
/// </summary> /// </summary>
public abstract class SharedArtifactCrusherSystem : EntitySystem public abstract class SharedArtifactCrusherSystem : EntitySystem
{ {
[Dependency] protected readonly SharedAppearanceSystem Appearance = default!;
[Dependency] protected readonly SharedAudioSystem AudioSystem = default!; [Dependency] protected readonly SharedAudioSystem AudioSystem = default!;
[Dependency] protected readonly SharedContainerSystem ContainerSystem = default!; [Dependency] protected readonly SharedContainerSystem ContainerSystem = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly EmagSystem _emag = default!; [Dependency] private readonly EmagSystem _emag = default!;
[Dependency] private readonly SharedPowerReceiverSystem _power = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly DamageableSystem _damageable = default!;
/// <inheritdoc/> /// <inheritdoc/>
public override void Initialize() public override void Initialize()
@@ -27,6 +38,8 @@ public abstract class SharedArtifactCrusherSystem : EntitySystem
SubscribeLocalEvent<ArtifactCrusherComponent, StorageOpenAttemptEvent>(OnStorageOpenAttempt); SubscribeLocalEvent<ArtifactCrusherComponent, StorageOpenAttemptEvent>(OnStorageOpenAttempt);
SubscribeLocalEvent<ArtifactCrusherComponent, ExaminedEvent>(OnExamine); SubscribeLocalEvent<ArtifactCrusherComponent, ExaminedEvent>(OnExamine);
SubscribeLocalEvent<ArtifactCrusherComponent, GotEmaggedEvent>(OnEmagged); SubscribeLocalEvent<ArtifactCrusherComponent, GotEmaggedEvent>(OnEmagged);
SubscribeLocalEvent<ArtifactCrusherComponent, GetVerbsEvent<AlternativeVerb>>(OnGetVerbs);
SubscribeLocalEvent<ArtifactCrusherComponent, PowerChangedEvent>(OnPowerChanged);
} }
private void OnInit(Entity<ArtifactCrusherComponent> ent, ref ComponentInit args) private void OnInit(Entity<ArtifactCrusherComponent> ent, ref ComponentInit args)
@@ -53,6 +66,7 @@ public abstract class SharedArtifactCrusherSystem : EntitySystem
ent.Comp.AutoLock = true; ent.Comp.AutoLock = true;
args.Handled = true; args.Handled = true;
Dirty(ent);
} }
private void OnStorageOpenAttempt(Entity<ArtifactCrusherComponent> ent, ref StorageOpenAttemptEvent args) private void OnStorageOpenAttempt(Entity<ArtifactCrusherComponent> ent, ref StorageOpenAttemptEvent args)
@@ -66,22 +80,94 @@ public abstract class SharedArtifactCrusherSystem : EntitySystem
args.PushMarkup(ent.Comp.AutoLock ? Loc.GetString("artifact-crusher-examine-autolocks") : Loc.GetString("artifact-crusher-examine-no-autolocks")); args.PushMarkup(ent.Comp.AutoLock ? Loc.GetString("artifact-crusher-examine-autolocks") : Loc.GetString("artifact-crusher-examine-no-autolocks"));
} }
public void StopCrushing(Entity<ArtifactCrusherComponent> ent, bool early = true) private void OnGetVerbs(Entity<ArtifactCrusherComponent> ent, ref GetVerbsEvent<AlternativeVerb> args)
{ {
var (_, crusher) = ent; if (!args.CanAccess || !args.CanInteract || args.Hands == null || ent.Comp.Crushing)
if (!crusher.Crushing)
return; return;
crusher.Crushing = false; if (!TryComp<EntityStorageComponent>(ent, out var entityStorageComp) ||
Appearance.SetData(ent, ArtifactCrusherVisuals.Crushing, false); entityStorageComp.Contents.ContainedEntities.Count == 0)
return;
if (!_power.IsPowered(ent.Owner))
return;
var user = args.User;
var verb = new AlternativeVerb
{
Text = Loc.GetString("artifact-crusher-verb-start-crushing"),
Priority = 2,
Act = () => StartCrushing((ent, ent.Comp, entityStorageComp), user)
};
args.Verbs.Add(verb);
}
private void OnPowerChanged(Entity<ArtifactCrusherComponent> ent, ref PowerChangedEvent args)
{
if (!args.Powered)
StopCrushing(ent);
}
public void StartCrushing(Entity<ArtifactCrusherComponent, EntityStorageComponent> ent, EntityUid? user = null)
{
var (uid, crusher, _) = ent;
if (crusher.Crushing)
return;
if (crusher.AutoLock)
_popup.PopupPredicted(Loc.GetString("artifact-crusher-autolocks-enable"), uid, user);
crusher.Crushing = true;
crusher.NextSecond = _timing.CurTime + TimeSpan.FromSeconds(1);
crusher.CrushEndTime = _timing.CurTime + crusher.CrushDuration;
crusher.CrushingSoundEntity = AudioSystem.PlayPvs(crusher.CrushingSound, ent)?.Entity;
_appearance.SetData(ent, ArtifactCrusherVisuals.Crushing, true);
Dirty(ent, ent.Comp1);
}
public void StopCrushing(Entity<ArtifactCrusherComponent> ent, bool early = true)
{
if (!ent.Comp.Crushing)
return;
ent.Comp.Crushing = false;
_appearance.SetData(ent, ArtifactCrusherVisuals.Crushing, false);
if (early) if (early)
{ {
AudioSystem.Stop(crusher.CrushingSoundEntity?.Item1, crusher.CrushingSoundEntity?.Item2); AudioSystem.Stop(ent.Comp.CrushingSoundEntity);
crusher.CrushingSoundEntity = null; ent.Comp.CrushingSoundEntity = null;
} }
Dirty(ent, ent.Comp); Dirty(ent, ent.Comp);
} }
public virtual void FinishCrushing(Entity<ArtifactCrusherComponent, EntityStorageComponent> ent) { }
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<ArtifactCrusherComponent, EntityStorageComponent>();
while (query.MoveNext(out var uid, out var crusher, out var storage))
{
if (!crusher.Crushing)
continue;
if (crusher.NextSecond < _timing.CurTime)
{
var contents = new ValueList<EntityUid>(storage.Contents.ContainedEntities);
foreach (var contained in contents)
{
_damageable.TryChangeDamage(contained, crusher.CrushingDamage);
}
crusher.NextSecond += TimeSpan.FromSeconds(1);
Dirty(uid, crusher);
}
if (crusher.CrushEndTime < _timing.CurTime)
FinishCrushing((uid, crusher, storage));
}
}
} }