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 <noreply@anthropic.com>
This commit is contained in:
parent
1986520bec
commit
b725a975a3
2 changed files with 91 additions and 12 deletions
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Lodur
|
||||
{
|
||||
|
|
@ -165,7 +166,7 @@ namespace Lodur
|
|||
/// <summary>The ordinal (from 0) of the <see cref="Step"/> that killed, -1 while alive.</summary>
|
||||
public long DiedAtStep { get; private set; } = -1;
|
||||
|
||||
/// <summary>Afflictions refused because the table was full.</summary>
|
||||
/// <summary>Afflictions refused because the table lacked room; a span call refused counts once.</summary>
|
||||
public int Refused { get; private set; }
|
||||
|
||||
/// <summary>Wakefulness, 0..1; 0 once dead.</summary>
|
||||
|
|
@ -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.
|
||||
/// </summary>
|
||||
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);
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public bool Afflict(int affliction, int part, ReadOnlySpan<int> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -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<ArgumentException>(() => p.Afflict(lesion, 1, new[] { 0, 2 }, 1));
|
||||
Assert.Throws<ArgumentException>(() => p.Afflict(lesion, 1, new[] { 0, -1 }, 1));
|
||||
Assert.Throws<ArgumentException>(() => p.Afflict(lesion, 1, new[] { 0, 1, 0 }, 1));
|
||||
Assert.Throws<ArgumentException>(() => p.Afflict(lesion, 1, ReadOnlySpan<int>.Empty, 1));
|
||||
Assert.Throws<ArgumentException>(() => p.Afflict(lesion, 9, new[] { 0 }, 1));
|
||||
Assert.Throws<ArgumentException>(() => p.Afflict(lesion, 1, new[] { 0, 1 }, 0));
|
||||
Assert.Throws<ArgumentException>(() => 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) =>
|
||||
|
|
|
|||
Loading…
Reference in a new issue