Skip to content

Commit

Permalink
what was i even fucking thinking???
Browse files Browse the repository at this point in the history
  • Loading branch information
MilonPL committed Nov 3, 2024
1 parent c215bbd commit 5bbdb9c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
39 changes: 15 additions & 24 deletions Content.Server/DeltaV/Addictions/AddictionSystem.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Content.Shared.Dataset;
using Content.Shared.DeltaV.Addictions;
using Content.Shared.Popups;
using Content.Shared.StatusEffect;
using Robust.Server.Player;
using Microsoft.CodeAnalysis;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
Expand All @@ -23,6 +22,9 @@ public sealed class AddictionSystem : SharedAddictionSystem
// Maximum time between popups
private const int MaxEffectInterval = 41;

// The time to add after the last metabolism cycle
private const int SuppressionDuration = 10;

public override void Initialize()
{
base.Initialize();
Expand All @@ -41,7 +43,7 @@ public override void Update(float frameTime)
// If it's suppressed, check if it's still supposed to be
if (component.Suppressed)
{
UpdateSuppressed(uid, component);
UpdateSuppressed(component);
continue;
}

Expand All @@ -54,34 +56,22 @@ public override void Update(float frameTime)
}

// Make sure we don't get a popup on the first update
private void OnInit(EntityUid uid, AddictedComponent component, ComponentStartup args)
private void OnInit(Entity<AddictedComponent> ent, ref ComponentStartup args)
{
var curTime = _timing.CurTime;
component.NextEffectTime = curTime + TimeSpan.FromSeconds(_random.Next(MinEffectInterval, MaxEffectInterval));
ent.Comp.NextEffectTime = curTime + TimeSpan.FromSeconds(_random.Next(MinEffectInterval, MaxEffectInterval));
}

private void UpdateSuppressed(EntityUid uid, AddictedComponent component)
private void UpdateSuppressed(AddictedComponent component)
{
var componentTime = component.LastMetabolismTime + TimeSpan.FromSeconds(10); // Ten seconds after the last metabolism cycle
var shouldBeSupressed = (componentTime > _timing.CurTime);
if (component.Suppressed != shouldBeSupressed)
{
component.Suppressed = shouldBeSupressed;
}
else
return;
component.Suppressed = (_timing.CurTime < component.SuppressionEndTime);
}

private string GetRandomPopup()
{
return Loc.GetString(_random.Pick(_prototypeManager.Index<LocalizedDatasetPrototype>("AddictionEffects").Values));
}

public override void TryApplyAddiction(EntityUid uid, float addictionTime, StatusEffectsComponent? status = null)
{
base.TryApplyAddiction(uid, addictionTime, status);
}

private void DoAddictionEffect(EntityUid uid)
{
_popup.PopupEntity(GetRandomPopup(), uid, uid);
Expand All @@ -90,10 +80,11 @@ private void DoAddictionEffect(EntityUid uid)
// Called each time a reagent with the Addicted effect gets metabolized
protected override void UpdateTime(EntityUid uid)
{
if (TryComp<AddictedComponent>(uid, out var component))
{
component.LastMetabolismTime = _timing.CurTime;
UpdateSuppressed(uid, component);
}
if (!TryComp<AddictedComponent>(uid, out var component))
return;

component.LastMetabolismTime = _timing.CurTime;
component.SuppressionEndTime = _timing.CurTime + TimeSpan.FromSeconds(SuppressionDuration);
UpdateSuppressed(component);
}
}
17 changes: 13 additions & 4 deletions Content.Shared/DeltaV/Addictions/AddictedComponent.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
using Robust.Shared.Timing;

namespace Content.Shared.DeltaV.Addictions;

[RegisterComponent, NetworkedComponent, Access(typeof(SharedAddictionSystem))]
[AutoGenerateComponentState]
[AutoGenerateComponentState, AutoGenerateComponentPause]
public sealed partial class AddictedComponent : Component
{
/// <summary>
Expand All @@ -17,12 +17,21 @@ public sealed partial class AddictedComponent : Component
/// <summary>
/// The <see cref="IGameTiming.CurTime"/> timestamp of last StatusEffect trigger.
/// </summary>
[DataField(serverOnly: true)]
[DataField(serverOnly: true, customTypeSerializer: typeof(TimeOffsetSerializer))]
[AutoPausedField]
public TimeSpan? LastMetabolismTime;

/// <summary>
/// The <see cref="IGameTiming.CurTime"/> timestamp of the next popup.
/// </summary>
[DataField(serverOnly: true)]
[DataField(serverOnly: true, customTypeSerializer: typeof(TimeOffsetSerializer))]
[AutoPausedField]
public TimeSpan? NextEffectTime;

/// <summary>
/// The <see cref="IGameTiming.CurTime"/> timestamp of the when the suppression ends
/// </summary>
[DataField(serverOnly: true, customTypeSerializer: typeof(TimeOffsetSerializer))]
[AutoPausedField]
public TimeSpan? SuppressionEndTime;
}
11 changes: 4 additions & 7 deletions Content.Shared/DeltaV/Addictions/SharedAddictionSystem.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@

using Content.Shared.DeltaV.Addictions;
using Content.Shared.StatusEffect;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;

namespace Content.Shared.DeltaV.Addictions;

public abstract class SharedAddictionSystem : EntitySystem
{
[Dependency] protected readonly StatusEffectsSystem StatusEffects = default!;
[Dependency] private readonly StatusEffectsSystem _statusEffects = default!;

public ProtoId<StatusEffectPrototype> StatusEffectKey = "Addicted";

Expand All @@ -21,13 +18,13 @@ public virtual void TryApplyAddiction(EntityUid uid, float addictionTime, Status

UpdateTime(uid);

if (!StatusEffects.HasStatusEffect(uid, StatusEffectKey, status))
if (!_statusEffects.HasStatusEffect(uid, StatusEffectKey, status))
{
StatusEffects.TryAddStatusEffect<AddictedComponent>(uid, StatusEffectKey, TimeSpan.FromSeconds(addictionTime), true, status);
_statusEffects.TryAddStatusEffect<AddictedComponent>(uid, StatusEffectKey, TimeSpan.FromSeconds(addictionTime), true, status);
}
else
{
StatusEffects.TryAddTime(uid, StatusEffectKey, TimeSpan.FromSeconds(addictionTime), status);
_statusEffects.TryAddTime(uid, StatusEffectKey, TimeSpan.FromSeconds(addictionTime), status);
}
}
}

0 comments on commit 5bbdb9c

Please sign in to comment.