PainBox owns its intensity and switch-off: Level, SwitchOff(tick); the session forwards them

BoxIntensity is now PainBox.Level's initial value and BoxDisableAtSeconds is one caller of SwitchOff, so a
scripted and a live switch-off share one primitive. Behaviour unchanged for existing parameters.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-20 00:52:05 +01:00
commit 4ed6649480
2 changed files with 17 additions and 5 deletions

View file

@ -17,13 +17,19 @@ public sealed class PainBox
{
_p = parameters;
_rng = rng;
DisableTick = parameters.BoxDisableAtSeconds is { } d
? (long)Math.Round(d / parameters.TickSeconds)
: long.MaxValue;
Level = parameters.BoxIntensity;
DisableTick = long.MaxValue;
if (parameters.BoxDisableAtSeconds is { } d) SwitchOff((long)Math.Round(d / parameters.TickSeconds));
}
/// <summary>The first tick the box is off; <see cref="long.MaxValue"/> when it is never disabled.</summary>
public long DisableTick { get; }
public long DisableTick { get; private set; }
/// <summary>The intensity the ramp scales: the parameters' <c>BoxIntensity</c> until a caller sets it.</summary>
public double Level { get; set; }
/// <summary>Switches the box off from <paramref name="tick"/>; an earlier switch-off stands.</summary>
public void SwitchOff(long tick) => DisableTick = Math.Min(DisableTick, tick);
/// <summary>Writes this tick's intensity. Always draws the jitter, so the stream does not depend on state.</summary>
public void Write(long tick, ref BoxStimulus box)
@ -31,7 +37,7 @@ public sealed class PainBox
double jitter = _rng.NextSigned();
double ramp = Math.Min(1.0, tick * _p.TickSeconds / _p.BoxRampSeconds);
box.Intensity = tick < DisableTick
? Math.Max(0.0, _p.BoxIntensity * ramp * (1.0 + _p.BoxNoise * jitter))
? Math.Max(0.0, Level * ramp * (1.0 + _p.BoxNoise * jitter))
: 0.0;
}
}

View file

@ -62,6 +62,12 @@ public sealed class PainBoxSession
/// <summary>True from the tick the deliberate withdrawal begins.</summary>
public bool Withdrew { get; private set; }
/// <summary>Sets the box intensity the ramp scales; it takes effect on the next <see cref="Tick"/>.</summary>
public void SetIntensity(double level) => _box.Level = level;
/// <summary>Switches the box off from the next tick; the <see cref="Disabled"/> check then ends the run if the will still holds.</summary>
public void SwitchOff() => _box.SwitchOff(TickIndex);
/// <summary>
/// The disable check, before ticking: if the box switches off now while the will still holds, the run ends
/// with the will intact and this returns true. A will already broken is not given back by the box going off.