Skip to content

Commit

Permalink
shitcode v2
Browse files Browse the repository at this point in the history
  • Loading branch information
MilonPL committed Sep 24, 2024
1 parent f029c40 commit bd65819
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 51 deletions.
8 changes: 8 additions & 0 deletions Content.Client/DeltaV/Addictions/AddictionSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Content.Shared.DeltaV.Addictions;

namespace Content.Client.DeltaV.Addictions;

public sealed class AddictionSystem : SharedAddictionSystem
{
protected override void UpdateTime(EntityUid uid) {}
}
60 changes: 19 additions & 41 deletions Content.Server/DeltaV/Addictions/AddictionSystem.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Content.Shared.Dataset;
using Content.Shared.DeltaV.Addictions;
using Content.Shared.Popups;
using Content.Shared.StatusEffect;
Expand All @@ -10,10 +11,10 @@ namespace Content.Server.DeltaV.Addictions;

public sealed class AddictionSystem : SharedAddictionSystem
{
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IGameTiming _timing = default!;

private readonly Dictionary<EntityUid, TimeSpan> _nextEffectTime = new();

Expand All @@ -27,7 +28,7 @@ public sealed class AddictionSystem : SharedAddictionSystem
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<AddictedComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<AddictedComponent, ComponentStartup>(OnInit);
}

public override void Update(float frameTime)
Expand All @@ -46,78 +47,55 @@ public override void Update(float frameTime)
continue;
}

if (!_nextEffectTime.TryGetValue(uid, out var nextTime))
{
// Between 10 and 40 seconds
_nextEffectTime[uid] = curTime + TimeSpan.FromSeconds(_random.Next(MinEffectInterval, MaxEffectInterval));
continue;
}

if (curTime < nextTime)
if (curTime < component.NextEffectTime)
continue;

DoAddictionEffect(uid);
_nextEffectTime[uid] = curTime + TimeSpan.FromSeconds(_random.Next(MinEffectInterval, MaxEffectInterval));
component.NextEffectTime = curTime + TimeSpan.FromSeconds(_random.Next(MinEffectInterval, MaxEffectInterval));
}
}

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

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

private string GetRandomPopup()
{
return _random.Pick(new[]
{
Loc.GetString("reagent-effect-medaddiction-1"),
Loc.GetString("reagent-effect-medaddiction-2"),
Loc.GetString("reagent-effect-medaddiction-3"),
Loc.GetString("reagent-effect-medaddiction-4"),
Loc.GetString("reagent-effect-medaddiction-5"),
Loc.GetString("reagent-effect-medaddiction-6"),
Loc.GetString("reagent-effect-medaddiction-7"),
Loc.GetString("reagent-effect-medaddiction-8")
});
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);
}

protected override void DoAddictionEffect(EntityUid uid)
private void DoAddictionEffect(EntityUid uid)
{
if (_playerManager.TryGetSessionByEntity(uid, out var session))
{
_popupSystem.PopupEntity(GetRandomPopup(), uid, session);
}
_popup.PopupEntity(GetRandomPopup(), uid, 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))
if (TryComp<AddictedComponent>(uid, out var component))
{
component.LastMetabolismTime = _timing.CurTime;
UpdateSuppressed(uid, component);
}
else
return;
}
}
2 changes: 1 addition & 1 deletion Content.Server/DeltaV/EntityEffects/Effects/Addicted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override void Effect(EntityEffectBaseArgs args)
addictionTime *= reagentArgs.Scale.Float();
}

var addictionSystem = args.EntityManager.EntitySysManager.GetEntitySystem<SharedAddictionSystem>();
var addictionSystem = args.EntityManager.System<SharedAddictionSystem>();
addictionSystem.TryApplyAddiction(args.TargetEntity, addictionTime);
}
}
11 changes: 9 additions & 2 deletions Content.Shared/DeltaV/Addictions/AddictedComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@

namespace Content.Shared.DeltaV.Addictions;

[RegisterComponent, NetworkedComponent]
[RegisterComponent, NetworkedComponent, Access(typeof(SharedAddictionSystem))]
[AutoGenerateComponentState]
public sealed partial class AddictedComponent : Component
{
/// <summary>
/// Whether to suppress pop-ups.
/// </summary>
[DataField]
[DataField, AutoNetworkedField]
public bool Suppressed;

/// <summary>
/// The <see cref="IGameTiming.CurTime"/> timestamp of last StatusEffect trigger.
/// </summary>
[DataField(serverOnly: true)]
public TimeSpan? LastMetabolismTime;

/// <summary>
/// The <see cref="IGameTiming.CurTime"/> timestamp of the next popup.
/// </summary>
[DataField(serverOnly: true)]
public TimeSpan? NextEffectTime;
}
7 changes: 0 additions & 7 deletions Content.Shared/DeltaV/Addictions/SharedAddictionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ public abstract class SharedAddictionSystem : EntitySystem

public ProtoId<StatusEffectPrototype> StatusEffectKey = "Addicted";

public override void Initialize()
{
base.Initialize();
}

protected abstract void DoAddictionEffect(EntityUid uid);

protected abstract void UpdateTime(EntityUid uid);

public virtual void TryApplyAddiction(EntityUid uid, float addictionTime, StatusEffectsComponent? status = null)
Expand Down
5 changes: 5 additions & 0 deletions Resources/Prototypes/DeltaV/Datasets/addictions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- type: localizedDataset
id: AddictionEffects
values:
prefix: reagent-effect-medaddiction-
count: 8

0 comments on commit bd65819

Please sign in to comment.