Brain: goals carry an entry effect, an effort and an involuntary onset

GoalSpec.EntryResolve sets resolve on the tick the goal takes over (then the
mend check runs); Effort is resolve drained per second to sustain the goal,
paid only once it is voluntary and scaled by (1 - Pull); InvoluntarySeconds
is the onset that runs without will (+infinity throughout). Brain.Validate
refuses out-of-range values naming the goal. The pain-box fixtures mark their
release goal involuntary throughout. The behaviour weight arrives next.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-20 04:13:27 +01:00
commit c93bdb7e1f
6 changed files with 186 additions and 6 deletions

View file

@ -124,7 +124,10 @@ namespace Lodur
}
}
if (_drive.Step(_driveP, _dt, afferent, strainLoad, _perception.Load, _brain.Goals[_goal].Pull))
// Effort is paid only once the goal is voluntary: the onset (InvoluntarySeconds) runs without will, and without cost.
var inForce = _brain.Goals[_goal];
var effort = (_tick - _goalSince) * _dt >= inForce.InvoluntarySeconds ? inForce.Effort : 0.0;
if (_drive.Step(_driveP, _dt, afferent, strainLoad, _perception.Load, inForce.Pull, effort))
{
events |= TickEvents.WillBroke;
}
@ -147,6 +150,10 @@ namespace Lodur
_pending = -1;
_goalSince = _tick;
events |= TickEvents.GoalChanged;
if (_brain.Goals[_goal].EntryResolve is double entry)
{
_drive.Enter(_driveP, entry);
}
}
_brain.Goals[_goal].Behaviour.Write(leak, _drive.Strain, _tick * _dt, (_tick - _goalSince) * _dt, strengths, targets);

View file

@ -61,6 +61,22 @@ namespace Lodur
/// </summary>
public double MinDwellSeconds { get; init; }
/// <summary>
/// The resolve the character has the tick this goal takes over, in [0, 1]; null leaves resolve as it is. Rage is what
/// the will breaking leads to, and it resets the will (lodur:ruling:rage-involuntary-onset-then-voluntary).
/// </summary>
public double? EntryResolve { get; init; }
/// <summary>Resolve drained per second to sustain this goal, finite and not negative, default 0; it is not paid during <see cref="InvoluntarySeconds"/> and is scaled by (1 - Pull) like the rest of the drain.</summary>
public double Effort { get; init; }
/// <summary>
/// How long after it takes over the goal runs without will, not negative, default 0 (voluntary from the first tick);
/// +infinity is involuntary throughout. While involuntary the behaviour writes at full weight and pays no Effort;
/// after it the goal is voluntary: sustained by <see cref="DriveState.Will"/> like every other voluntary layer.
/// </summary>
public double InvoluntarySeconds { get; init; }
private Condition[] _when = Array.Empty<Condition>();
private IReadOnlyList<Condition> _whenView = Array.AsReadOnly(Array.Empty<Condition>());
@ -179,6 +195,21 @@ namespace Lodur
throw new ArgumentException($"Goal {goal.Id}: MinDwellSeconds must be finite (not infinite) and not negative.");
}
if (goal.EntryResolve is double entry && !(entry >= 0.0 && entry <= 1.0))
{
throw new ArgumentException($"Goal {goal.Id}: EntryResolve must be in [0, 1].");
}
if (!(goal.Effort >= 0.0 && double.IsFinite(goal.Effort)))
{
throw new ArgumentException($"Goal {goal.Id}: Effort must be finite and not negative.");
}
if (!(goal.InvoluntarySeconds >= 0.0))
{
throw new ArgumentException($"Goal {goal.Id}: InvoluntarySeconds must not be negative or NaN (infinity is allowed).");
}
foreach (var c in goal.When)
{
if (!double.IsFinite(c.Threshold))

View file

@ -65,13 +65,23 @@ namespace Lodur
internal static DriveState Fresh => new DriveState { Resolve = 1.0 };
/// <summary>Steps one tick; returns true on the tick the will breaks (each break, not only the first). <paramref name="pull"/> is the active goal's: it scales resolve drain by (1 - pull).</summary>
internal bool Step(DriveParameters p, double dt, double afferent, double strainLoad, double load, double pull)
/// <summary>A goal's entry effect: sets resolve, then the mend check runs (a break the entry lifts past <see cref="DriveParameters.WillMendsAt"/> is mended). It is not a break and counts none.</summary>
internal void Enter(DriveParameters p, double resolve)
{
Resolve = resolve;
if (WillBroken && Resolve >= p.WillMendsAt)
{
WillBroken = false;
}
}
/// <summary>Steps one tick; returns true on the tick the will breaks (each break, not only the first). <paramref name="pull"/> is the active goal's: it scales resolve drain by (1 - pull). <paramref name="effort"/> is the active goal's resolve drained per second to sustain it (0 while it is involuntary); it joins the drain sum, so pull scales it too.</summary>
internal bool Step(DriveParameters p, double dt, double afferent, double strainLoad, double load, double pull, double effort)
{
Arousal = Math.Min(Math.Max(Arousal + dt * (p.ArousalRise * load * (1.0 - Arousal) - p.ArousalDecay * Arousal), 0.0), 1.0);
Pain = Math.Min(Math.Max(Pain + dt * (p.PainRise * afferent * (1.0 - Pain) - p.PainDecay * Pain), 0.0), 1.0);
Strain = Math.Max(0.0, Strain + dt * (strainLoad - p.StrainRecover * Strain));
var drain = (p.ResolveDrainPain * Pain + p.ResolveDrainStrain * Strain + p.ResolveDrainArousal * Arousal) * (1.0 - pull) * p.DrainTrait;
var drain = (p.ResolveDrainPain * Pain + p.ResolveDrainStrain * Strain + p.ResolveDrainArousal * Arousal + effort) * (1.0 - pull) * p.DrainTrait;
var recovery = Pain < p.ResolveRecoversBelowPain && Arousal < p.ResolveRecoversBelowArousal ? p.ResolveRecovery * (1.0 - Resolve) : 0.0;
Resolve = Math.Min(Math.Max(Resolve + dt * (recovery - drain), 0.0), 1.0);
if (WillBroken)