Brain: optional minimum dwell per goal; a built Brain owns copies; pin condition reads, latency and phase

GoalSpec.MinDwellSeconds (default 0) holds a goal until it has been active that many ticks, so a condition
hovering at its threshold no longer chatters. Brain, GoalSpec and PoseBehaviour copy the lists and arrays
they are given. New tests pin every drive quantity a condition reads, Below's strictness, the one-tick latency
floor and negative refusal, that the pending goal's behaviour is not written during the latency, that a
behaviour's phase restarts from the time in the goal, and the dwell and copy rules. Defaults leave the
golden table and the connected world hash unchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-20 02:14:55 +01:00
commit 4f421f42cc
4 changed files with 288 additions and 10 deletions

View file

@ -27,6 +27,7 @@ namespace Lodur
private readonly DriveParameters _driveP;
private readonly Brain _brain;
private readonly long _latency;
private readonly long[] _dwell;
private readonly Perception _perception;
private readonly WithdrawalReflex[] _reflexes;
private DriveState _drive = DriveState.Fresh;
@ -47,6 +48,12 @@ namespace Lodur
_brain = brain;
_goal = brain.Start;
_latency = Math.Max(1, (long)Math.Round(brain.DeliberateLatencySeconds / tickSeconds));
_dwell = new long[brain.Goals.Count];
for (var g = 0; g < _dwell.Length; g++)
{
_dwell[g] = (long)Math.Round(brain.Goals[g].MinDwellSeconds / tickSeconds);
}
_perception = new Perception(nerve, tickSeconds, regionCount, senses, senseRegion);
_reflexes = new WithdrawalReflex[regionCount];
for (var r = 0; r < regionCount; r++)
@ -117,8 +124,9 @@ namespace Lodur
events |= TickEvents.WillBroke;
}
// A decision in flight commits at its tick even if the condition that chose it has lapsed; none is made while one is pending.
if (_pending < 0)
// A decision in flight commits at its tick even if the condition that chose it has lapsed; none is made while one is
// pending, nor before the goal in force has had its minimum dwell.
if (_pending < 0 && _tick - _goalSince >= _dwell[_goal])
{
var wanted = _brain.Select(_drive, _goal);
if (wanted != _goal)

View file

@ -46,11 +46,20 @@ namespace Lodur
/// <summary>The eligible goal of the highest priority is wanted.</summary>
public double Priority { get; init; }
/// <summary>How hard the goal pulls, in [0, 1]: resolve drain is scaled by (1 - pull) while it is active. It is not a second resolve bar.</summary>
/// <summary>How hard the goal pulls, in [0, 1], default 0 (no pull): resolve drain is scaled by (1 - pull) while it is active. It is not a second resolve bar.</summary>
public double Pull { get; init; }
/// <summary>All must hold for the goal to be eligible; none means always eligible.</summary>
public Condition[] When { get; init; } = Array.Empty<Condition>();
/// <summary>
/// A goal cannot be left until it has been active this long, counted in ticks from the tick it took over;
/// 0 (the default) never holds a goal. It keeps a condition hovering at its threshold from making the Brain
/// chatter: a switch away is decided only once the dwell has passed, then takes the deliberate latency.
/// </summary>
public double MinDwellSeconds { get; init; }
private Condition[] _when = Array.Empty<Condition>();
/// <summary>All must hold for the goal to be eligible; none means always eligible. Copied when set.</summary>
public Condition[] When { get => _when; init => _when = (Condition[])value.Clone(); }
public PoseBehaviour Behaviour { get; init; } = new PoseBehaviour();
}
@ -61,7 +70,23 @@ namespace Lodur
/// </summary>
public sealed record Brain
{
public IReadOnlyList<GoalSpec> Goals { get; init; } = Array.Empty<GoalSpec>();
private IReadOnlyList<GoalSpec> _goals = Array.Empty<GoalSpec>();
/// <summary>The goals, copied when set: later changes to the caller's list or arrays do not reach a Brain.</summary>
public IReadOnlyList<GoalSpec> Goals
{
get => _goals;
init
{
var copy = new GoalSpec[value.Count];
for (var i = 0; i < copy.Length; i++)
{
copy[i] = value[i];
}
_goals = Array.AsReadOnly(copy);
}
}
/// <summary>The index of the goal a character begins in.</summary>
public int Start { get; init; }
@ -123,6 +148,11 @@ namespace Lodur
throw new ArgumentException($"Goal {goal.Id}: priority must be finite and pull in [0, 1].");
}
if (!(goal.MinDwellSeconds >= 0.0 && double.IsFinite(goal.MinDwellSeconds)))
{
throw new ArgumentException($"Goal {goal.Id}: MinDwellSeconds must be finite and not negative.");
}
foreach (var c in goal.When)
{
if (!double.IsFinite(c.Threshold))

View file

@ -28,14 +28,30 @@ namespace Lodur
/// </summary>
public sealed record PoseBehaviour
{
/// <summary>k keys of one rotation per muscle each.</summary>
public Quat[][] Keys { get; init; } = Array.Empty<Quat[]>();
private Quat[][] _keys = Array.Empty<Quat[]>();
private Quat[] _flinch = Array.Empty<Quat>();
/// <summary>k keys of one rotation per muscle each. Copied when set.</summary>
public Quat[][] Keys
{
get => _keys;
init
{
var copy = new Quat[value.Length][];
for (var k = 0; k < copy.Length; k++)
{
copy[k] = (Quat[])value[k].Clone();
}
_keys = copy;
}
}
/// <summary>Seconds from one key to the next when there is more than one key.</summary>
public double SecondsPerKey { get; init; } = 1.0;
/// <summary>The pose the leak and the shake pull toward, one rotation per muscle.</summary>
public Quat[] FlinchPose { get; init; } = Array.Empty<Quat>();
/// <summary>The pose the leak and the shake pull toward, one rotation per muscle. Copied when set.</summary>
public Quat[] FlinchPose { get => _flinch; init => _flinch = (Quat[])value.Clone(); }
public HoldParameters Hold { get; init; } = new HoldParameters();