Office setting and cause as separate data: named emitters, stressor schedules, office sense nerve

Setting holds the emitters and no schedule; Cause holds the stressors, the objective pull and the
sense nerve (threshold 0.05, no sensitisation, no conduction delay) and no geometry. The test
runner takes a name filter so the office tests run without the minutes-long body tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-20 00:57:38 +01:00
commit d5a456707f
3 changed files with 126 additions and 3 deletions

72
src/BadDay.Core/Cause.cs Normal file
View file

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Lodur;
namespace BadDay.Core;
/// <summary>
/// One recurring source of load: when it starts, how it rises, how often it comes back, how long it lasts and how
/// hard, on one emitter of the <see cref="Setting"/>. Times are seconds; a null period is a single burst.
/// </summary>
public sealed record Stressor(
string Id, string EmitterId,
double OnsetSeconds, double RampSeconds, double? PeriodSeconds, double DurationSeconds,
double Intensity, double Jitter)
{
/// <summary>
/// The intensity at <paramref name="tick"/>, in whole ticks so the burst edges never wander with float error:
/// zero before the onset and outside a burst, else Intensity rising over the ramp, scaled by (1 + Jitter * draw).
/// </summary>
public double Value(long tick, double tickSeconds, double draw)
{
long t = tick - Ticks(OnsetSeconds, tickSeconds);
if (t < 0) return 0.0;
long phase = PeriodSeconds is { } p ? t % Math.Max(1, Ticks(p, tickSeconds)) : t;
if (phase >= Ticks(DurationSeconds, tickSeconds)) return 0.0;
long ramp = Ticks(RampSeconds, tickSeconds);
double rise = ramp > 0 ? Math.Min(1.0, (double)phase / ramp) : 1.0;
return Math.Max(0.0, Intensity * rise * (1.0 + Jitter * draw));
}
private static long Ticks(double seconds, double tickSeconds) => (long)Math.Round(seconds / tickSeconds);
}
/// <summary>
/// What goes wrong: the stressors, the objective's pull, and the sense nerve the character hears and sees them
/// with. It holds no geometry (that is the <see cref="Setting"/>'s). Code defaults, not persisted.
/// </summary>
public sealed record Cause(IReadOnlyList<Stressor> Stressors, double ObjectivePull, NerveParameters Senses)
{
/// <summary>
/// The office's sense nerve: threshold 0.05, no sensitisation, no conduction delay. The office's break near
/// 90 s at pull 0.6 holds only with these; Lodur's NerveParameters defaults break at about 100 s.
/// </summary>
public static NerveParameters OfficeSenses { get; } = new()
{
Threshold = 0.05,
SensitisationGrowth = 0.0,
ConductionDelaySeconds = 0.0,
};
/// <summary>
/// The office's sensory-load cause [P]: printer audio 5 s on, every 8 s for 1 s at 0.25; ceiling light 15 s
/// every 11 s for 2 s at 0.2; desk phone 25 s every 14 s for 3 s at 0.35; desktop interrupt 75 s every 20 s for
/// 1 s at 0.3; pull 0.6. The chatty neighbour and the stand-up crowd wait for coworkers (rung 6).
/// </summary>
public static Cause Office { get; } = new(new[]
{
new Stressor("printer", "printer", 5.0, 0.0, 8.0, 1.0, 0.25, 0.1),
new Stressor("ceiling-light", "ceiling-light", 15.0, 0.0, 11.0, 2.0, 0.2, 0.1),
new Stressor("desk-phone", "desk-phone", 25.0, 0.0, 14.0, 3.0, 0.35, 0.1),
new Stressor("desktop-interrupt", "desktop", 75.0, 0.0, 20.0, 1.0, 0.3, 0.1),
}, 0.6, OfficeSenses);
/// <summary>Every stressor's intensity times <paramref name="factor"/>.</summary>
public Cause Scaled(double factor) => this with
{
Stressors = Stressors.Select(s => s with { Intensity = s.Intensity * factor }).ToArray(),
};
public Cause Without(string stressorId) => this with { Stressors = Stressors.Where(s => s.Id != stressorId).ToArray() };
}

View file

@ -0,0 +1,28 @@
using System.Collections.Generic;
using Lodur;
namespace BadDay.Core;
/// <summary>One of the setting's point sources: an id the cause refers to, and a Lodur <see cref="Lodur.Emitter"/> (channel, position, radius).</summary>
public sealed record NamedEmitter(string Id, Emitter Emitter);
/// <summary>
/// Where a bad day happens: the named emitters and where they sit. It holds no schedule; what the emitters do over
/// time is the <see cref="Cause"/>'s. Code defaults for now, not persisted (follow-up office-scenario-as-content).
/// Every number is a placeholder.
/// </summary>
public sealed record Setting(IReadOnlyList<NamedEmitter> Emitters)
{
/// <summary>
/// The office: a printer and a desk phone (audio), a ceiling light (visual) and the desktop's notifications
/// (interrupt). Each sits at the origin, where the head is held, with a radius that reaches it in full: the
/// placement is a placeholder until the client puts real positions in.
/// </summary>
public static Setting Office { get; } = new(new[]
{
new NamedEmitter("printer", new Emitter(Channel.Audio, 0f, 0f, 0f, 10f)),
new NamedEmitter("ceiling-light", new Emitter(Channel.Visual, 0f, 0f, 0f, 10f)),
new NamedEmitter("desk-phone", new Emitter(Channel.Audio, 0f, 0f, 0f, 10f)),
new NamedEmitter("desktop", new Emitter(Channel.Interrupt, 0f, 0f, 0f, 10f)),
});
}

View file

@ -260,6 +260,27 @@ public static class Program
try { Run("run"); } catch (ArgumentException) { threw = true; }
Check(threw, "missing --seed should be rejected");
}),
("office: a stressor is silent before its onset, on for its duration, and back every period", () =>
{
var printer = Cause.Office.Stressors.Single(s => s.Id == "printer"); // 5 s on, 8 s period, 1 s long, 0.25
double V(double seconds) => printer.Value((long)Math.Round(seconds * 100), 0.01, 0.0);
Check(V(4.99) == 0.0 && V(5.0) == 0.25 && V(5.99) == 0.25 && V(6.0) == 0.0, "first burst is not [5 s, 6 s)");
Check(V(12.99) == 0.0 && V(13.0) == 0.25 && V(13.99) == 0.25 && V(14.0) == 0.0, "second burst is not [13 s, 14 s)");
var once = printer with { PeriodSeconds = null };
Check(once.Value(1300, 0.01, 0.0) == 0.0 && once.Value(500, 0.01, 0.0) == 0.25, "a stressor with no period repeated");
var ramped = printer with { RampSeconds = 0.5 };
Check(ramped.Value(525, 0.01, 0.0) == 0.125 && ramped.Value(560, 0.01, 0.0) == 0.25, "the ramp does not rise over its seconds");
}),
("office: the setting holds the emitters and no schedule, the cause refers to them by id", () =>
{
var ids = Setting.Office.Emitters.Select(e => e.Id).ToHashSet();
Check(Cause.Office.Stressors.All(s => ids.Contains(s.EmitterId)), "a stressor names an emitter the setting lacks");
Check(Setting.Office.Emitters.All(e => e.Emitter.Intensity == 0.0), "the setting carries an intensity");
Check(Cause.Office.Senses.Threshold == 0.05 && Cause.Office.Senses.SensitisationGrowth == 0.0 && Cause.Office.Senses.ConductionDelaySeconds == 0.0,
"the office does not pass its own sense parameters");
}),
};
private static int Tick(long ms) => (int)Math.Round(ms * Hz / 1000.0);
@ -281,15 +302,17 @@ public static class Program
return Math.Sqrt(sum / (to - from - 1));
}
public static int Main()
/// <summary>Runs every test; with an argument, only the tests whose name contains it (the body tests take minutes).</summary>
public static int Main(string[] args)
{
int failed = 0;
foreach (var (name, body) in Tests)
var tests = args.Length > 0 ? Tests.Where(t => t.Name.Contains(args[0])).ToArray() : Tests;
foreach (var (name, body) in tests)
{
try { body(); Console.WriteLine($"PASS {name}"); }
catch (Exception e) { failed++; Console.WriteLine($"FAIL {name}: {e.Message}"); }
}
Console.WriteLine($"{Tests.Length - failed}/{Tests.Length} passed");
Console.WriteLine($"{tests.Length - failed}/{tests.Length} passed");
return failed == 0 ? 0 : 1;
}