From b725a975a38548ca6c46ebaba485941364ed7862 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 26 Nov 2026 05:08:52 +0100 Subject: [PATCH] Physiology: Afflict over a span of layers is all or none; the single-layer call is its one-layer case Co-Authored-By: Claude Sonnet 5.5 --- src/Lodur.Core/Physiology.cs | 51 +++++++++++++++------ tests/Lodur.Core.Tests/PhysiologyTests.cs | 54 +++++++++++++++++++++++ 2 files changed, 92 insertions(+), 13 deletions(-) diff --git a/src/Lodur.Core/Physiology.cs b/src/Lodur.Core/Physiology.cs index f78b8b1..ab06bcb 100644 --- a/src/Lodur.Core/Physiology.cs +++ b/src/Lodur.Core/Physiology.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Runtime.InteropServices; namespace Lodur { @@ -165,7 +166,7 @@ namespace Lodur /// The ordinal (from 0) of the that killed, -1 while alive. public long DiedAtStep { get; private set; } = -1; - /// Afflictions refused because the table was full. + /// Afflictions refused because the table lacked room; a span call refused counts once. public int Refused { get; private set; } /// Wakefulness, 0..1; 0 once dead. @@ -249,24 +250,48 @@ namespace Lodur /// Binds a new instance of the affliction to a tissue, never merged with another. Returns false, counting a refusal and /// overwriting nothing, when the table is full. Allowed while dead: it records the wound and nothing steps. /// - public bool Afflict(int affliction, int part, int layer, double severity) + public bool Afflict(int affliction, int part, int layer, double severity) => + Afflict(affliction, part, MemoryMarshal.CreateReadOnlySpan(ref layer, 1), severity); + + /// + /// A wound of several layers of one part, all or none: binds one new instance of the affliction per layer, in span order into + /// the lowest free slots, and returns true; with fewer free slots than layers it writes nothing, counts one refusal and returns + /// false. The affliction, severity and every layer are validated before anything is written; an empty span or a layer named + /// twice throws. Allowed while dead, as the single-layer call. + /// + public bool Afflict(int affliction, int part, ReadOnlySpan layers, double severity) { if ((uint)affliction >= (uint)_defs.Length) throw new ArgumentException("not an affliction."); - var tissue = Flat(part, layer); + if (layers.Length == 0) throw new ArgumentException("a wound needs at least one layer."); if (!(severity > 0.0 && !double.IsInfinity(severity))) throw new ArgumentException("severity must be finite and above 0."); - for (var s = 0; s < _slotDef.Length; s++) + for (var i = 0; i < layers.Length; i++) { - if (_slotDef[s] >= 0) continue; - _slotDef[s] = affliction; - _slotPart[s] = part; - _slotTissue[s] = tissue; - _slotSeverity[s] = severity; - _slotAge[s] = 0.0; - return true; + Flat(part, layers[i]); + for (var j = 0; j < i; j++) + if (layers[j] == layers[i]) throw new ArgumentException("a layer is named twice."); } - Refused++; - return false; + var free = 0; + for (var s = 0; s < _slotDef.Length; s++) + if (_slotDef[s] < 0) free++; + if (free < layers.Length) + { + Refused++; + return false; + } + + var slot = 0; + for (var i = 0; i < layers.Length; i++) + { + while (_slotDef[slot] >= 0) slot++; + _slotDef[slot] = affliction; + _slotPart[slot] = part; + _slotTissue[slot] = Flat(part, layers[i]); + _slotSeverity[slot] = severity; + _slotAge[slot] = 0.0; + } + + return true; } /// diff --git a/tests/Lodur.Core.Tests/PhysiologyTests.cs b/tests/Lodur.Core.Tests/PhysiologyTests.cs index 21efeab..f96614e 100644 --- a/tests/Lodur.Core.Tests/PhysiologyTests.cs +++ b/tests/Lodur.Core.Tests/PhysiologyTests.cs @@ -626,6 +626,60 @@ public sealed class PhysiologyTests Assert.True(p.Afflict(Af("Lesion"), 1, 0, 1)); } + private static readonly TissueDef[] TwoLayers = { new("skin", 10, 1), new("muscle", 10, 1, Motor: 1) }; + + [Fact] + public void A_span_afflict_is_all_or_none() + { + var p = Make(slots: 3, limbA: TwoLayers); + Assert.True(p.Afflict(Af("Lesion"), 0, 0, 1)); + Assert.True(p.Afflict(Af("Lesion"), 0, 1, 2)); // one free slot, two layers + Assert.False(p.Afflict(Af("Lesion"), 1, new[] { 0, 1 }, 5)); + Assert.Equal(1, p.Refused); + Assert.Equal(2, Live(p)); + Assert.Equal(-1, p.SlotDef(2)); + var q = Make(slots: 2, limbA: TwoLayers); // exactly enough + Assert.True(q.Afflict(Af("Lesion"), 1, new[] { 1, 0 }, 5)); + Assert.Equal(0, q.Refused); + Assert.Equal(2, Live(q)); + Assert.Equal(5.0, q.SlotSeverity(0)); + Assert.Equal(5.0, q.SlotSeverity(1)); + Assert.False(q.Afflict(Af("Lesion"), 1, new[] { 0 }, 5)); + Assert.Equal(1, q.Refused); + } + + [Fact] + public void A_healed_slot_is_free_to_the_span_call() + { + var p = Make(slots: 3, limbA: TwoLayers); + Assert.True(p.Afflict(Af("Heal"), 1, 0, 0.5)); + Assert.True(p.Afflict(Af("Lesion"), 0, 0, 1)); + Assert.True(p.Afflict(Af("Lesion"), 0, 1, 1)); + Assert.False(p.Afflict(Af("Lesion"), 1, new[] { 0, 1 }, 1)); // full + p.Step(1.0); // Heal is gone, its slot is free + Assert.Equal(2, Live(p)); + Assert.False(p.Afflict(Af("Lesion"), 1, new[] { 0, 1 }, 1)); // one free, two wanted + Assert.Equal(2, p.Refused); + Assert.True(p.Afflict(Af("Lesion"), 1, new[] { 1 }, 1)); + Assert.Equal(3, Live(p)); + } + + [Fact] + public void A_bad_or_repeated_layer_throws_before_any_write() + { + var p = Make(slots: 8, limbA: TwoLayers); + var lesion = Af("Lesion"); + Assert.Throws(() => p.Afflict(lesion, 1, new[] { 0, 2 }, 1)); + Assert.Throws(() => p.Afflict(lesion, 1, new[] { 0, -1 }, 1)); + Assert.Throws(() => p.Afflict(lesion, 1, new[] { 0, 1, 0 }, 1)); + Assert.Throws(() => p.Afflict(lesion, 1, ReadOnlySpan.Empty, 1)); + Assert.Throws(() => p.Afflict(lesion, 9, new[] { 0 }, 1)); + Assert.Throws(() => p.Afflict(lesion, 1, new[] { 0, 1 }, 0)); + Assert.Throws(() => p.Afflict(99, 1, new[] { 0, 1 }, 1)); + Assert.Equal(0, Live(p)); + Assert.Equal(0, p.Refused); + } + // Determinism. private static (long Volume, long Consciousness, long Motor) Bits(Physiology p) =>