Drive: arousal from sensory load drains resolve; Actor takes senses and emitters

Arousal rises under load and decays when quiet, drains resolve beside pain and strain
(scaled by pull and trait), and resolve recovers only when pain and arousal are both low.
Actor.Tick gains a trailing emitter span; the pain box's expressions keep their operand order
and the 420-row golden table passes unchanged.

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

View file

@ -37,12 +37,13 @@ namespace Lodur
public Actor(
double tickSeconds, int regionCount,
NerveParameters nerve, WithdrawalParameters withdrawal, DriveParameters drive, HoldParameters hold,
ReadOnlySpan<Quat> holdPose, ReadOnlySpan<Quat> withdrawPose)
ReadOnlySpan<Quat> holdPose, ReadOnlySpan<Quat> withdrawPose,
NerveParameters? senses = null, int senseRegion = 0)
{
_dt = tickSeconds;
_driveP = drive;
_withdrawLatency = Math.Max(1, (long)Math.Round(withdrawal.LatencySeconds / tickSeconds));
_perception = new Perception(nerve, tickSeconds, regionCount);
_perception = new Perception(nerve, tickSeconds, regionCount, senses, senseRegion);
_reflexes = new WithdrawalReflex[regionCount];
for (var r = 0; r < regionCount; r++)
{
@ -67,19 +68,28 @@ namespace Lodur
public double Afferent(int region) => _perception.Afferent(region);
/// <summary>The sensory load this tick: the channels' afferents combined, 0..1.</summary>
public double Load => _perception.Load;
public double ChannelStimulus(Channel channel) => _perception.ChannelStimulus(channel);
public double ChannelAfferent(Channel channel) => _perception.ChannelAfferent(channel);
/// <summary>
/// Runs one physics step's worth of nervous system. The point spans hold one entry per region; the strengths
/// and targets spans one entry per muscle, written for the body to follow.
/// and targets spans one entry per muscle, written for the body to follow. The emitters are the game's point
/// sources, sensed at the sense region's point.
/// </summary>
public TickEvents Tick(
ReadOnlySpan<float> regionX, ReadOnlySpan<float> regionY, ReadOnlySpan<float> regionZ,
ReadOnlySpan<BoxStimulus> sources, Span<float> strengths, Span<Quat> targets)
ReadOnlySpan<BoxStimulus> sources, Span<float> strengths, Span<Quat> targets,
ReadOnlySpan<Emitter> emitters = default)
{
var events = TickEvents.None;
// A flinch is the leak of a suppressed reflex; once the will has broken nothing is suppressed, so none is reported.
var holding = !_drive.WillBroken;
_perception.Sense(regionX, regionY, regionZ, sources);
_perception.Sense(regionX, regionY, regionZ, sources, emitters);
double afferent = 0.0, strainLoad = 0.0, leak = 0.0;
for (var r = 0; r < _reflexes.Length; r++)
@ -95,7 +105,7 @@ namespace Lodur
}
}
if (_drive.Step(_driveP, _dt, afferent, strainLoad))
if (_drive.Step(_driveP, _dt, afferent, strainLoad, _perception.Load))
{
events |= TickEvents.WillBroke;
_withdrawTick = _tick + _withdrawLatency;

View file

@ -2,7 +2,7 @@ using System;
namespace Lodur
{
/// <summary>The drive's tunables: how pain and strain accumulate, and how they drain the will.</summary>
/// <summary>The drive's tunables: how pain, strain and arousal accumulate, and how they drain the will.</summary>
public sealed record DriveParameters
{
public double PainRise { get; init; } = 1.0;
@ -18,25 +18,42 @@ namespace Lodur
/// <summary>Per-character multiplier on resolve drain (the consumer draws its trait spread).</summary>
public double DrainTrait { get; init; } = 1.0;
/// <summary>How fast arousal rises toward 1 under full sensory load.</summary>
public double ArousalRise { get; init; } = 0.5;
/// <summary>How fast arousal falls back once the load stops.</summary>
public double ArousalDecay { get; init; } = 0.1;
/// <summary>Resolve drained per second at full arousal, before the objective's pull and the drain trait scale it.</summary>
public double ResolveDrainArousal { get; init; } = 0.1;
/// <summary>Resolve recovers only while arousal is below this, as well as pain being low.</summary>
public double ResolveRecoversBelowArousal { get; init; } = 0.1;
}
/// <summary>
/// Layer 3: per-character state that evolves every tick. Pain rises with afferent input, strain with the
/// reflex held down, resolve drains under both and recovers when pain is low. Resolve reaching zero breaks
/// reflex held down, arousal with sensory load; resolve drains under all three and recovers when pain and
/// arousal are both low. Resolve reaching zero breaks
/// the will, and a broken will never mends. Only the owning <see cref="Actor"/> steps it.
/// </summary>
public struct DriveState
{
public double Pain { get; private set; }
public double Strain { get; private set; }
/// <summary>Sensory load carried over time, 0..1; it rises under load and decays when quiet.</summary>
public double Arousal { get; private set; }
public double Resolve { get; private set; }
public bool WillBroken { get; private set; }
internal static DriveState Fresh => new DriveState { Resolve = 1.0 };
/// <summary>Steps one tick; returns true on the tick the will breaks.</summary>
internal bool Step(DriveParameters p, double dt, double afferent, double strainLoad)
internal bool Step(DriveParameters p, double dt, double afferent, double strainLoad, double load)
{
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));
if (WillBroken)
@ -44,8 +61,8 @@ namespace Lodur
return false;
}
var drain = (p.ResolveDrainPain * Pain + p.ResolveDrainStrain * Strain) * (1.0 - p.ObjectivePull) * p.DrainTrait;
var recovery = Pain < p.ResolveRecoversBelowPain ? p.ResolveRecovery * (1.0 - Resolve) : 0.0;
var drain = (p.ResolveDrainPain * Pain + p.ResolveDrainStrain * Strain + p.ResolveDrainArousal * Arousal) * (1.0 - p.ObjectivePull) * 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 (Resolve <= 0.0)
{