Persist daemon consumable behavior state
This commit is contained in:
parent
cadddb4c87
commit
5363714cde
17 changed files with 445 additions and 30 deletions
|
|
@ -135,8 +135,16 @@ public sealed class AetheriaUnityObservedEntityRestorer
|
|||
|
||||
private void RestoreActiveConsumables(Entity entity, AetheriaRuntimeEntitySnapshot snapshot)
|
||||
{
|
||||
var activeEffectIds = new HashSet<string>(StringComparer.Ordinal);
|
||||
foreach (var activeConsumable in snapshot.ActiveConsumables)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(activeConsumable.EffectId))
|
||||
{
|
||||
_logWarning($"Typed active consumable {activeConsumable.ItemKey} has no daemon effect identity on restored entity {snapshot.RecordKey}.");
|
||||
continue;
|
||||
}
|
||||
|
||||
activeEffectIds.Add(activeConsumable.EffectId);
|
||||
var item = _createLoadoutItem(new AetheriaRuntimeLoadoutItemSnapshot(
|
||||
activeConsumable.ItemKey,
|
||||
activeConsumable.Quality,
|
||||
|
|
@ -151,10 +159,13 @@ public sealed class AetheriaUnityObservedEntityRestorer
|
|||
}
|
||||
|
||||
entity.RestoreActiveConsumable(
|
||||
activeConsumable.EffectId,
|
||||
item,
|
||||
(float)activeConsumable.RemainingDuration,
|
||||
(float)activeConsumable.Duration);
|
||||
}
|
||||
|
||||
entity.RetainActiveConsumables(activeEffectIds);
|
||||
}
|
||||
|
||||
private static void RestoreRuntimeBehaviorState(
|
||||
|
|
@ -328,8 +339,6 @@ public sealed class AetheriaUnityObservedEntityRestorer
|
|||
{
|
||||
case "equipment":
|
||||
return ownerIndex < entity.Equipment.Count ? entity.Equipment[ownerIndex].Behaviors : null;
|
||||
case "active_consumable":
|
||||
return ownerIndex < entity.ActiveConsumables.Count ? entity.ActiveConsumables[ownerIndex].Behaviors : null;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -396,12 +396,31 @@ public abstract class Entity
|
|||
Hypothermia = AetheriaScalar.Saturate(hypothermia);
|
||||
}
|
||||
|
||||
public void RestoreActiveConsumable(ConsumableItem item, float remainingDuration, float duration)
|
||||
public void RestoreActiveConsumable(string effectId, ConsumableItem item, float remainingDuration, float duration)
|
||||
{
|
||||
if (item == null)
|
||||
if (string.IsNullOrWhiteSpace(effectId) || item == null)
|
||||
return;
|
||||
|
||||
_activeConsumables.Add(new ConsumableItemEffect(item, this, remainingDuration, duration));
|
||||
var activeConsumable = FindActiveConsumableByEffectId(effectId);
|
||||
if (activeConsumable != null)
|
||||
{
|
||||
activeConsumable.RestorePresentationState(remainingDuration, duration);
|
||||
return;
|
||||
}
|
||||
|
||||
_activeConsumables.Add(new ConsumableItemEffect(item, this, effectId, remainingDuration, duration));
|
||||
}
|
||||
|
||||
public void RetainActiveConsumables(IReadOnlyCollection<string> effectIds)
|
||||
{
|
||||
if (effectIds == null)
|
||||
{
|
||||
_activeConsumables.Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
_activeConsumables.RemoveAll(effect =>
|
||||
string.IsNullOrWhiteSpace(effect.EffectId) || !effectIds.Contains(effect.EffectId));
|
||||
}
|
||||
|
||||
public void RestoreStatGrids(IReadOnlyList<AetheriaRuntimeEntityStatGridSnapshot> grids)
|
||||
|
|
@ -441,6 +460,13 @@ public abstract class Entity
|
|||
return _activeConsumables.FirstOrDefault(ac => ac.Item?.ItemKey == itemKey);
|
||||
}
|
||||
|
||||
public ConsumableItemEffect FindActiveConsumableByEffectId(string effectId)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(effectId)
|
||||
? null
|
||||
: _activeConsumables.FirstOrDefault(active => active.EffectId == effectId);
|
||||
}
|
||||
|
||||
private static void RestoreFloatGrid(AetheriaRuntimeEntityStatGridSnapshot grid, float[,] target)
|
||||
{
|
||||
if (!CanRestoreGrid(grid, target))
|
||||
|
|
@ -1331,6 +1357,7 @@ public abstract class Entity
|
|||
public class ConsumableItemEffect
|
||||
{
|
||||
public float RemainingDuration { get; private set; }
|
||||
public string EffectId { get; }
|
||||
public Entity Entity { get; }
|
||||
public ConsumableItem Item { get; }
|
||||
public Behavior[] Behaviors { get; }
|
||||
|
|
@ -1341,14 +1368,20 @@ public class ConsumableItemEffect
|
|||
private readonly BezierCurve _effectiveness;
|
||||
|
||||
public ConsumableItemEffect(ConsumableItem item, Entity entity)
|
||||
: this(item, entity, null, null)
|
||||
: this(item, entity, "", null, null)
|
||||
{
|
||||
}
|
||||
|
||||
public ConsumableItemEffect(ConsumableItem item, Entity entity, float? remainingDuration, float? duration)
|
||||
public ConsumableItemEffect(
|
||||
ConsumableItem item,
|
||||
Entity entity,
|
||||
string effectId,
|
||||
float? remainingDuration,
|
||||
float? duration)
|
||||
{
|
||||
Item = item;
|
||||
Entity = entity;
|
||||
EffectId = effectId ?? "";
|
||||
RuntimeItem = entity.ItemManager.GetRuntimeItem(item);
|
||||
_duration = duration.GetValueOrDefault(RuntimeItem?.Duration > 0 ? (float)RuntimeItem.Duration : 0);
|
||||
_effectiveness = CreateEffectivenessCurve(RuntimeItem);
|
||||
|
|
@ -1359,6 +1392,11 @@ public class ConsumableItemEffect
|
|||
Behaviors = entity.ItemManager.CreateRuntimeBehaviors(this);
|
||||
}
|
||||
|
||||
public void RestorePresentationState(float remainingDuration, float duration)
|
||||
{
|
||||
RemainingDuration = AetheriaScalar.Clamp(remainingDuration, 0, Math.Max(duration, float.Epsilon));
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
foreach (var behavior in Behaviors)
|
||||
|
|
|
|||
|
|
@ -5045,6 +5045,8 @@ public class DaemonRuntimeDocumentTests
|
|||
|
||||
Assert.AreEqual(1, actor.CargoContents[0].Items[0].Item.Quantity);
|
||||
Assert.AreEqual(1, actor.ActiveConsumables.Count);
|
||||
Assert.IsFalse(string.IsNullOrWhiteSpace(actor.ActiveConsumables[0].EffectId));
|
||||
var effectId = actor.ActiveConsumables[0].EffectId;
|
||||
Assert.AreEqual(0.73, actor.ActiveConsumables[0].Quality, 0.0001);
|
||||
Assert.AreEqual(1.5, actor.ActiveConsumables[0].RemainingDuration, 0.0001);
|
||||
Assert.AreEqual("consumable.activated", run.GameEvents.Single().Kind);
|
||||
|
|
@ -5054,6 +5056,7 @@ public class DaemonRuntimeDocumentTests
|
|||
AetheriaRuntimeConsumableSimulation.Step(run, Array.Empty<AetheriaRuntimeDaemonConsumableIntent>(), catalog, 42, 0.01);
|
||||
Assert.AreEqual(0, actor.ActiveConsumables.Count);
|
||||
Assert.AreEqual(1, run.GameEvents.Count(value => value.Kind == "consumable.expired"));
|
||||
StringAssert.Contains(effectId, run.GameEvents.Single(value => value.Kind == "consumable.expired").EventId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
|
@ -5236,6 +5239,107 @@ public class DaemonRuntimeDocumentTests
|
|||
"Typed effectiveness curves must preserve Unity AnimationCurve tangents.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConsumableCooldownOwnsStableStateAcrossBlockedChainsAndBehaviorReordering()
|
||||
{
|
||||
var run = RunWithTwoEntities();
|
||||
var actor = run.Zones[0].Entities[0];
|
||||
actor.ActiveConsumables = new[]
|
||||
{
|
||||
new AetheriaRuntimeActiveConsumableCommit
|
||||
{
|
||||
EffectId = "effect:stable",
|
||||
ItemKey = "field-kit",
|
||||
Quality = 1,
|
||||
Duration = 10,
|
||||
RemainingDuration = 10
|
||||
}
|
||||
};
|
||||
var energy = BehaviorPayloadId("energy", "EnergyDraw", PerformanceStatField(1, 5), BoolField(2, false));
|
||||
var cooldown = BehaviorPayloadId("cooldown", "Cooldown", PerformanceStatField(1, 1));
|
||||
var originalCatalog = new AetheriaRuntimeCatalogSnapshot(
|
||||
new[] { CatalogItem("field-kit", new[] { energy, cooldown }, category: AetheriaRuntimeItemCategories.Consumable, duration: 10) },
|
||||
Array.Empty<AetheriaRuntimeCorporation>(),
|
||||
Array.Empty<AetheriaRuntimeNameFile>());
|
||||
|
||||
AetheriaRuntimeConsumableSimulation.Step(
|
||||
run, Array.Empty<AetheriaRuntimeDaemonConsumableIntent>(), originalCatalog, 90, 0.25);
|
||||
|
||||
var state = actor.ActiveConsumables[0].BehaviorStates.Single(value => value.BehaviorId == "cooldown");
|
||||
Assert.AreEqual(-0.25, state.ScalarState, 0.0001,
|
||||
"Always-updated cooldowns must age before an earlier behavior stops the chain.");
|
||||
|
||||
var reorderedCatalog = new AetheriaRuntimeCatalogSnapshot(
|
||||
new[] { CatalogItem("field-kit", new[] { cooldown, energy }, category: AetheriaRuntimeItemCategories.Consumable, duration: 10) },
|
||||
Array.Empty<AetheriaRuntimeCorporation>(),
|
||||
Array.Empty<AetheriaRuntimeNameFile>());
|
||||
AetheriaRuntimeConsumableSimulation.Step(
|
||||
run, Array.Empty<AetheriaRuntimeDaemonConsumableIntent>(), reorderedCatalog, 91, 0.1);
|
||||
|
||||
state = actor.ActiveConsumables[0].BehaviorStates.Single(value => value.BehaviorId == "cooldown");
|
||||
Assert.AreEqual(0, state.BehaviorIndex, "Authored order is projection, not behavior identity.");
|
||||
Assert.AreEqual(1, state.ScalarState, 0.0001,
|
||||
"The ready cooldown should execute immediately when the reordered chain reaches it.");
|
||||
|
||||
AetheriaRuntimeConsumableSimulation.Step(
|
||||
run, Array.Empty<AetheriaRuntimeDaemonConsumableIntent>(), reorderedCatalog, 92, 0.5);
|
||||
Assert.AreEqual(0.5, state.ScalarState, 0.0001);
|
||||
Assert.AreEqual("cooldown", run.GameEvents.Last(value => value.FrameId == 92).Reason);
|
||||
|
||||
AetheriaRuntimeConsumableSimulation.Step(
|
||||
run, Array.Empty<AetheriaRuntimeDaemonConsumableIntent>(), reorderedCatalog, 93, 0.5);
|
||||
Assert.AreEqual(0, state.ScalarState, 0.0001);
|
||||
Assert.AreEqual("cooldown", run.GameEvents.Last(value => value.FrameId == 93).Reason,
|
||||
"Exact zero remains blocked in the fossil contract.");
|
||||
|
||||
AetheriaRuntimeConsumableSimulation.Step(
|
||||
run, Array.Empty<AetheriaRuntimeDaemonConsumableIntent>(), reorderedCatalog, 94, 0.01);
|
||||
Assert.AreEqual(1, state.ScalarState, 0.0001);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExpiringConsumableCannotTransferIdentityOrBehaviorStateToItsNeighbor()
|
||||
{
|
||||
var run = RunWithTwoEntities();
|
||||
var actor = run.Zones[0].Entities[0];
|
||||
actor.ActiveConsumables = new[]
|
||||
{
|
||||
new AetheriaRuntimeActiveConsumableCommit
|
||||
{
|
||||
EffectId = "effect:expires", ItemKey = "empty", Duration = 1, RemainingDuration = 0
|
||||
},
|
||||
new AetheriaRuntimeActiveConsumableCommit
|
||||
{
|
||||
EffectId = "effect:survives", ItemKey = "empty", Duration = 3, RemainingDuration = 2,
|
||||
BehaviorStates = new[]
|
||||
{
|
||||
new AetheriaRuntimeConsumableBehaviorStateCommit
|
||||
{
|
||||
BehaviorId = "remember-me", BehaviorIndex = 0, BehaviorKind = "Cooldown", ScalarState = 0.42
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
var catalog = new AetheriaRuntimeCatalogSnapshot(
|
||||
new[]
|
||||
{
|
||||
CatalogItem("empty", new[]
|
||||
{
|
||||
BehaviorPayloadId("remember-me", "Cooldown", PerformanceStatField(1, 1))
|
||||
}, category: AetheriaRuntimeItemCategories.Consumable, stackable: true, duration: 3)
|
||||
},
|
||||
Array.Empty<AetheriaRuntimeCorporation>(),
|
||||
Array.Empty<AetheriaRuntimeNameFile>());
|
||||
|
||||
AetheriaRuntimeConsumableSimulation.Step(
|
||||
run, Array.Empty<AetheriaRuntimeDaemonConsumableIntent>(), catalog, 95, 0.1);
|
||||
|
||||
Assert.AreEqual(1, actor.ActiveConsumables.Count);
|
||||
Assert.AreEqual("effect:survives", actor.ActiveConsumables[0].EffectId);
|
||||
Assert.AreEqual(0.32, actor.ActiveConsumables[0].BehaviorStates.Single().ScalarState, 0.0001,
|
||||
"The surviving effect keeps and advances its own nested state after array compaction.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BehaviorStateProjectorDerivesEquipmentRowsFromCatalogPayloads()
|
||||
{
|
||||
|
|
@ -6032,6 +6136,12 @@ public class DaemonRuntimeDocumentTests
|
|||
params AetheriaRuntimeBehaviorField[] fields) =>
|
||||
new AetheriaRuntimeBehaviorPayload(0, kind, 0, fields);
|
||||
|
||||
private static AetheriaRuntimeBehaviorPayload BehaviorPayloadId(
|
||||
string behaviorId,
|
||||
string kind,
|
||||
params AetheriaRuntimeBehaviorField[] fields) =>
|
||||
new AetheriaRuntimeBehaviorPayload(0, kind, 0, fields, behaviorId);
|
||||
|
||||
private static AetheriaRuntimeBehaviorField PerformanceStatField(int key, double value) =>
|
||||
new AetheriaRuntimeBehaviorField(key, PerformanceStatValue(value, value));
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue