517 lines
25 KiB
C#
517 lines
25 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
|
|
#nullable enable
|
|
|
|
namespace GameCult.Aetheria.State.Verse
|
|
{
|
|
public sealed class AetheriaRuntimeDaemonTickOptions
|
|
{
|
|
public string DaemonId { get; set; } = "aetheria-daemon";
|
|
public string SessionId { get; set; } = "local";
|
|
public string VerseId { get; set; } = "aetheria.local";
|
|
public string CultMeshAddress { get; set; } = "cultmesh://aetheria.local/eve/providers/aetheria.daemon";
|
|
public long FrameId { get; set; }
|
|
public double SimulationTimeSeconds { get; set; }
|
|
public double FixedDeltaSeconds { get; set; }
|
|
public IReadOnlyList<AetheriaRuntimeDaemonCommandDocument> ObservedCommands { get; set; } =
|
|
Array.Empty<AetheriaRuntimeDaemonCommandDocument>();
|
|
public IReadOnlyList<string> AccountedCommandIds { get; set; } =
|
|
Array.Empty<string>();
|
|
public IReadOnlyList<string> PreRejectedCommandIds { get; set; } =
|
|
Array.Empty<string>();
|
|
public IReadOnlyList<string> CumulativeAppliedCommandIds { get; set; } =
|
|
Array.Empty<string>();
|
|
public IReadOnlyList<string> CumulativeRejectedCommandIds { get; set; } =
|
|
Array.Empty<string>();
|
|
public AetheriaRuntimeDaemonOperationContext OperationContext { get; set; } =
|
|
new AetheriaRuntimeDaemonOperationContext();
|
|
public AetheriaRuntimeStarbridgeScenarioDocument? StarbridgeScenario { get; set; }
|
|
public AetheriaRuntimeStarbridgeSessionDocument? StarbridgeSession { get; set; }
|
|
public AetheriaRuntimeCatalogSnapshot? Catalog { get; set; }
|
|
public AetheriaRuntimeDaemonRenderSettings RenderSettings { get; set; } =
|
|
AetheriaRuntimeDaemonRenderSettings.AetheriaDefault;
|
|
public AetheriaRuntimeDaemonSimulationSettings SimulationSettings { get; set; } =
|
|
AetheriaRuntimeDaemonSimulationSettings.AetheriaDefault;
|
|
public IAetheriaRuntimeWorldPhysics? WorldPhysics { get; set; }
|
|
public bool AdvanceSimulation { get; set; } = true;
|
|
public int SimulationStepCount { get; set; } = 1;
|
|
public bool StopCompressedSimulationOnAttention { get; set; }
|
|
public bool BuildPublications { get; set; } = true;
|
|
}
|
|
|
|
public sealed class AetheriaRuntimeDaemonTickResult
|
|
{
|
|
public AetheriaRuntimeDaemonTickResult(
|
|
AetheriaRuntimeRunCheckpointCommit run,
|
|
AetheriaRuntimeDaemonOperationResult operationResult,
|
|
AetheriaRuntimeDaemonFrameDocument frame,
|
|
AetheriaRuntimeDaemonSoaFrame? soaFrame = null,
|
|
AetheriaRuntimeDaemonProviderAdvertisementDocument? providerAdvertisement = null,
|
|
AetheriaRuntimeDaemonHealthDocument? health = null,
|
|
AetheriaRuntimeDaemonCommandBoundaryDocument? commandBoundary = null,
|
|
AetheriaRuntimeAssetManifestDocument? assetManifest = null,
|
|
AetheriaRuntimeStarbridgeSessionSummaryDocument? starbridgeSessionSummary = null,
|
|
AetheriaRuntimeSurfaceDocument? gameSurface = null,
|
|
AetheriaRuntimeSurfaceDocument? gameTuiSurface = null,
|
|
AetheriaRuntimeSurfaceDocument? editorSurface = null,
|
|
AetheriaRuntimeSurfaceDocument? editorTuiSurface = null,
|
|
int simulationStepsExecuted = 0,
|
|
AetheriaRuntimeSimulationInterruption? attentionInterruption = null)
|
|
{
|
|
Run = run ?? new AetheriaRuntimeRunCheckpointCommit();
|
|
OperationResult = operationResult ?? new AetheriaRuntimeDaemonOperationResult(
|
|
Run,
|
|
Array.Empty<string>(),
|
|
Array.Empty<string>());
|
|
Frame = frame ?? new AetheriaRuntimeDaemonFrameDocument();
|
|
SoaFrame = soaFrame;
|
|
ProviderAdvertisement = providerAdvertisement;
|
|
Health = health;
|
|
CommandBoundary = commandBoundary;
|
|
AssetManifest = assetManifest;
|
|
StarbridgeSessionSummary = starbridgeSessionSummary;
|
|
GameSurface = gameSurface;
|
|
GameTuiSurface = gameTuiSurface;
|
|
EditorSurface = editorSurface;
|
|
EditorTuiSurface = editorTuiSurface;
|
|
SimulationStepsExecuted = simulationStepsExecuted;
|
|
AttentionInterruption = attentionInterruption;
|
|
}
|
|
|
|
public AetheriaRuntimeRunCheckpointCommit Run { get; }
|
|
public AetheriaRuntimeDaemonOperationResult OperationResult { get; }
|
|
public AetheriaRuntimeDaemonFrameDocument Frame { get; }
|
|
public AetheriaRuntimeDaemonSoaFrame? SoaFrame { get; }
|
|
public AetheriaRuntimeDaemonSoaViewDocument? SoaView => SoaFrame?.View;
|
|
public AetheriaRuntimeDaemonProviderAdvertisementDocument? ProviderAdvertisement { get; }
|
|
public AetheriaRuntimeDaemonHealthDocument? Health { get; }
|
|
public AetheriaRuntimeDaemonCommandBoundaryDocument? CommandBoundary { get; }
|
|
public AetheriaRuntimeAssetManifestDocument? AssetManifest { get; }
|
|
public AetheriaRuntimeStarbridgeSessionSummaryDocument? StarbridgeSessionSummary { get; }
|
|
public AetheriaRuntimeSurfaceDocument? GameSurface { get; }
|
|
public AetheriaRuntimeSurfaceDocument? GameTuiSurface { get; }
|
|
public AetheriaRuntimeSurfaceDocument? EditorSurface { get; }
|
|
public AetheriaRuntimeSurfaceDocument? EditorTuiSurface { get; }
|
|
public int SimulationStepsExecuted { get; }
|
|
public AetheriaRuntimeSimulationInterruption? AttentionInterruption { get; }
|
|
public AetheriaRuntimeDaemonIntentState Intents => OperationResult.Intents;
|
|
}
|
|
|
|
public sealed class AetheriaRuntimeSimulationInterruption
|
|
{
|
|
public AetheriaRuntimeSimulationInterruption(string causeEventId, string causeKind, double simulationTimeSeconds)
|
|
{
|
|
CauseEventId = causeEventId ?? "";
|
|
CauseKind = causeKind ?? "";
|
|
SimulationTimeSeconds = simulationTimeSeconds;
|
|
}
|
|
|
|
public string CauseEventId { get; }
|
|
public string CauseKind { get; }
|
|
public double SimulationTimeSeconds { get; }
|
|
}
|
|
|
|
public static class AetheriaRuntimeTerminusAttentionPolicy
|
|
{
|
|
private static readonly HashSet<string> InterruptingKinds = new HashSet<string>(StringComparer.Ordinal)
|
|
{
|
|
"consumable.activation.refused",
|
|
"entity.damaged",
|
|
"entity.destroyed",
|
|
"equipment.destroyed",
|
|
"pickup.collected",
|
|
"pickup.rejected",
|
|
"pilot.heatstroke.risk",
|
|
"pilot.hypothermia.risk",
|
|
"run.failed",
|
|
"ship.docked",
|
|
"ship.undocked",
|
|
"weapon.charge.malfunctioned",
|
|
"weapon.fire.refused",
|
|
"wormhole.transferred",
|
|
"wormhole.exit.completed"
|
|
};
|
|
|
|
public static AetheriaRuntimeGameEventCommit? FirstInterruptingEvent(
|
|
IEnumerable<AetheriaRuntimeGameEventCommit> events) =>
|
|
(events ?? Array.Empty<AetheriaRuntimeGameEventCommit>())
|
|
.Where(value => value != null && InterruptingKinds.Contains(value.Kind ?? ""))
|
|
.OrderBy(value => value.EventId, StringComparer.Ordinal)
|
|
.FirstOrDefault();
|
|
}
|
|
|
|
public static class AetheriaRuntimeDaemonTickRunner
|
|
{
|
|
public static AetheriaRuntimeDaemonTickResult Tick(
|
|
string stateFilePath,
|
|
AetheriaRuntimeRunCheckpointCommit run,
|
|
AetheriaRuntimeDaemonTickOptions options)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(stateFilePath))
|
|
throw new ArgumentException("State file path must be non-empty.", nameof(stateFilePath));
|
|
|
|
run ??= new AetheriaRuntimeRunCheckpointCommit();
|
|
options ??= new AetheriaRuntimeDaemonTickOptions();
|
|
options.OperationContext ??= new AetheriaRuntimeDaemonOperationContext();
|
|
options.OperationContext.Catalog = options.Catalog;
|
|
options.OperationContext.WormholeDistanceRatio = options.RenderSettings.WormholeDistanceRatio;
|
|
var trace = string.Equals(Environment.GetEnvironmentVariable("AETHERIA_TRACE_TICK_PHASES"), "1", StringComparison.Ordinal);
|
|
var phase = Stopwatch.StartNew();
|
|
void Trace(string name)
|
|
{
|
|
if (trace && phase.ElapsedMilliseconds >= 20)
|
|
Console.WriteLine($"Aetheria simulation phase {name} took {phase.ElapsedMilliseconds}ms.");
|
|
phase.Restart();
|
|
}
|
|
EnsureEntityIds(run);
|
|
EnsureBehaviorStates(run, options.Catalog);
|
|
Trace("state-projection");
|
|
|
|
var observedCommands = (options.ObservedCommands ?? Array.Empty<AetheriaRuntimeDaemonCommandDocument>())
|
|
.Where(command => command != null)
|
|
.ToArray();
|
|
var accountedBeforeTick = new HashSet<string>(
|
|
options.AccountedCommandIds ?? Array.Empty<string>(),
|
|
StringComparer.Ordinal);
|
|
var commands = observedCommands
|
|
.Where(command => !string.IsNullOrWhiteSpace(command.CommandId))
|
|
.Where(command => !accountedBeforeTick.Contains(command.CommandId))
|
|
.ToArray();
|
|
var operationResult = AetheriaRuntimeDaemonOperations.Execute(
|
|
run,
|
|
commands,
|
|
options.OperationContext);
|
|
Trace("command-execution");
|
|
var preRejectedCommandIds = (options.PreRejectedCommandIds ?? Array.Empty<string>())
|
|
.Where(commandId => !string.IsNullOrWhiteSpace(commandId))
|
|
.Distinct(StringComparer.Ordinal)
|
|
.ToArray();
|
|
if (preRejectedCommandIds.Length > 0)
|
|
{
|
|
operationResult = new AetheriaRuntimeDaemonOperationResult(
|
|
operationResult.Run,
|
|
operationResult.AppliedCommandIds,
|
|
preRejectedCommandIds
|
|
.Concat(operationResult.RejectedCommandIds)
|
|
.Distinct(StringComparer.Ordinal)
|
|
.ToArray(),
|
|
operationResult.Intents,
|
|
MergeRejectedReasons(
|
|
operationResult.RejectedCommandReasons,
|
|
preRejectedCommandIds.ToDictionary(
|
|
commandId => commandId,
|
|
_ => AetheriaRuntimeDaemonRejectionReasons.AuthorityDenied,
|
|
StringComparer.Ordinal)));
|
|
}
|
|
var simulationStepCount = options.AdvanceSimulation && AetheriaRuntimeRunLifecycle.IsActive(operationResult.Run)
|
|
? Math.Max(1, options.SimulationStepCount)
|
|
: 0;
|
|
var simulationStartTimeSeconds = options.SimulationTimeSeconds -
|
|
simulationStepCount * options.FixedDeltaSeconds;
|
|
var simulationStepsExecuted = 0;
|
|
AetheriaRuntimeSimulationInterruption? attentionInterruption = null;
|
|
for (var simulationStep = 0; simulationStep < simulationStepCount; simulationStep++)
|
|
{
|
|
var stepTime = simulationStartTimeSeconds +
|
|
(simulationStep + 1) * options.FixedDeltaSeconds;
|
|
var eventIdsBeforeStep = new HashSet<string>(
|
|
(operationResult.Run.GameEvents ?? Array.Empty<AetheriaRuntimeGameEventCommit>())
|
|
.Where(value => value != null)
|
|
.Select(value => value.EventId ?? ""),
|
|
StringComparer.Ordinal);
|
|
var agentCommands = AetheriaRuntimeAgentScheduler.AssignAndPlan(
|
|
operationResult.Run,
|
|
options.FrameId,
|
|
options.Catalog,
|
|
stepTime,
|
|
options.SimulationSettings);
|
|
Trace("agent-planning");
|
|
if (agentCommands.Count > 0)
|
|
{
|
|
var agentResult = AetheriaRuntimeDaemonOperations.Execute(
|
|
operationResult.Run,
|
|
agentCommands,
|
|
options.OperationContext);
|
|
AetheriaRuntimeAgentScheduler.Reconcile(
|
|
agentResult.Run,
|
|
options.FrameId,
|
|
agentResult.AppliedCommandIds,
|
|
agentResult.RejectedCommandIds);
|
|
operationResult = new AetheriaRuntimeDaemonOperationResult(
|
|
agentResult.Run,
|
|
operationResult.AppliedCommandIds.Concat(agentResult.AppliedCommandIds).ToArray(),
|
|
operationResult.RejectedCommandIds.Concat(agentResult.RejectedCommandIds).ToArray(),
|
|
MergeIntents(operationResult.Intents, agentResult.Intents),
|
|
MergeRejectedReasons(
|
|
operationResult.RejectedCommandReasons,
|
|
agentResult.RejectedCommandReasons));
|
|
}
|
|
AetheriaRuntimeDaemonSimulation.Step(
|
|
operationResult.Run,
|
|
operationResult.Intents,
|
|
options.FixedDeltaSeconds,
|
|
options.SimulationSettings,
|
|
options.WorldPhysics ?? throw new InvalidOperationException("Ymir world physics owner is required."),
|
|
options.Catalog,
|
|
options.FrameId,
|
|
stepTime,
|
|
simulationStep,
|
|
options.RenderSettings);
|
|
Trace("world-step");
|
|
StampZoneSimulationTime(operationResult.Run, stepTime);
|
|
simulationStepsExecuted++;
|
|
|
|
if (options.StopCompressedSimulationOnAttention && simulationStepCount > 1)
|
|
{
|
|
var cause = AetheriaRuntimeTerminusAttentionPolicy.FirstInterruptingEvent(
|
|
(operationResult.Run.GameEvents ?? Array.Empty<AetheriaRuntimeGameEventCommit>())
|
|
.Where(value => value != null && !eventIdsBeforeStep.Contains(value.EventId ?? "")));
|
|
if (cause != null)
|
|
{
|
|
attentionInterruption = new AetheriaRuntimeSimulationInterruption(
|
|
cause.EventId, cause.Kind, stepTime);
|
|
AetheriaRuntimeGameEvents.Append(operationResult.Run, new AetheriaRuntimeGameEventCommit
|
|
{
|
|
EventId = $"frame:{options.FrameId}:simulation:interrupted:{cause.EventId}",
|
|
Kind = "simulation.interrupted",
|
|
FrameId = options.FrameId,
|
|
ZoneIndex = cause.ZoneIndex,
|
|
SourceEntityIndex = cause.SourceEntityIndex,
|
|
TargetEntityIndex = cause.TargetEntityIndex,
|
|
SubjectKey = cause.EventId,
|
|
ItemKey = cause.ItemKey,
|
|
Reason = cause.Kind,
|
|
ScalarValue = simulationStepCount,
|
|
AuxiliaryValue = simulationStepsExecuted,
|
|
PositionX = cause.PositionX,
|
|
PositionZ = cause.PositionZ
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!AetheriaRuntimeRunLifecycle.IsActive(operationResult.Run))
|
|
break;
|
|
}
|
|
|
|
var committedSimulationTimeSeconds = simulationStartTimeSeconds +
|
|
simulationStepsExecuted * options.FixedDeltaSeconds;
|
|
|
|
var frame = AetheriaRuntimeDaemonFrameDocument.Create(
|
|
operationResult.Run,
|
|
options.DaemonId,
|
|
options.SessionId,
|
|
options.FrameId,
|
|
committedSimulationTimeSeconds,
|
|
options.FixedDeltaSeconds,
|
|
renderSettings: options.RenderSettings,
|
|
simulationSettings: options.SimulationSettings);
|
|
Trace("frame-projection");
|
|
frame.AppliedCommandIds = operationResult.AppliedCommandIds;
|
|
frame.RejectedCommandIds = operationResult.RejectedCommandIds;
|
|
frame.RejectedCommandReasons = operationResult.RejectedCommandReasons;
|
|
frame.AccountedCommandIds = accountedBeforeTick
|
|
.Concat(operationResult.AppliedCommandIds)
|
|
.Concat(operationResult.RejectedCommandIds)
|
|
.Where(commandId => !string.IsNullOrWhiteSpace(commandId))
|
|
.Distinct(StringComparer.Ordinal)
|
|
.ToArray();
|
|
frame.CumulativeAppliedCommandIds = (options.CumulativeAppliedCommandIds ?? Array.Empty<string>())
|
|
.Concat(operationResult.AppliedCommandIds)
|
|
.Where(commandId => !string.IsNullOrWhiteSpace(commandId))
|
|
.Distinct(StringComparer.Ordinal)
|
|
.ToArray();
|
|
frame.CumulativeRejectedCommandIds = (options.CumulativeRejectedCommandIds ?? Array.Empty<string>())
|
|
.Concat(operationResult.RejectedCommandIds)
|
|
.Where(commandId => !string.IsNullOrWhiteSpace(commandId))
|
|
.Distinct(StringComparer.Ordinal)
|
|
.ToArray();
|
|
frame.Capabilities = new[]
|
|
{
|
|
"aetheria.daemon.operation_execute.v1",
|
|
"aetheria.daemon.intent_state.v1",
|
|
"aetheria.daemon.authoritative_frame.v1",
|
|
AetheriaRuntimeDaemonSchemas.AssetManifest,
|
|
AetheriaRuntimeDaemonSchemas.ProviderAdvertisement,
|
|
AetheriaRuntimeDaemonSchemas.Health,
|
|
AetheriaRuntimeDaemonSchemas.CommandBoundary,
|
|
AetheriaRuntimeDaemonSchemas.StarbridgeSessionSummary,
|
|
AetheriaRuntimeDaemonSchemas.GameSurface,
|
|
AetheriaRuntimeDaemonSchemas.EditorSurface
|
|
};
|
|
|
|
if (!options.BuildPublications)
|
|
{
|
|
return new AetheriaRuntimeDaemonTickResult(
|
|
operationResult.Run,
|
|
operationResult,
|
|
frame,
|
|
simulationStepsExecuted: simulationStepsExecuted,
|
|
attentionInterruption: attentionInterruption);
|
|
}
|
|
|
|
return BuildPublications(
|
|
stateFilePath,
|
|
operationResult,
|
|
frame,
|
|
options,
|
|
observedCommands.Length,
|
|
simulationStepsExecuted,
|
|
attentionInterruption);
|
|
}
|
|
|
|
public static AetheriaRuntimeDaemonTickResult BuildPublications(
|
|
string stateFilePath,
|
|
AetheriaRuntimeDaemonOperationResult operationResult,
|
|
AetheriaRuntimeDaemonFrameDocument frame,
|
|
AetheriaRuntimeDaemonTickOptions options,
|
|
int observedCommandCount,
|
|
int simulationStepsExecuted = 0,
|
|
AetheriaRuntimeSimulationInterruption? attentionInterruption = null)
|
|
{
|
|
var catalog = options.Catalog ?? new AetheriaRuntimeCatalogSnapshot(
|
|
Array.Empty<AetheriaRuntimeCatalogItem>(),
|
|
Array.Empty<AetheriaRuntimeCorporation>(),
|
|
Array.Empty<AetheriaRuntimeNameFile>());
|
|
var commandBoundary = AetheriaRuntimeDaemonCommandBoundaryDocument.Create(options.DaemonId);
|
|
var providerAdvertisement = AetheriaRuntimeDaemonProviderAdvertisementDocument.Create(
|
|
stateFilePath,
|
|
options.DaemonId,
|
|
options.VerseId,
|
|
options.CultMeshAddress);
|
|
var health = new AetheriaRuntimeDaemonHealthDocument
|
|
{
|
|
DaemonId = string.IsNullOrWhiteSpace(options.DaemonId) ? "aetheria-daemon" : options.DaemonId,
|
|
VerseId = string.IsNullOrWhiteSpace(options.VerseId) ? "aetheria.local" : options.VerseId,
|
|
PublishedAtUtc = DateTime.UtcNow.ToString("O"),
|
|
StatePath = stateFilePath,
|
|
FrameId = frame.FrameId,
|
|
ObservedCommandCount = observedCommandCount,
|
|
AppliedCommandCount = operationResult.AppliedCommandIds.Count,
|
|
RejectedCommandCount = operationResult.RejectedCommandIds.Count,
|
|
Status = operationResult.RejectedCommandIds.Count == 0 ? "healthy" : "commands-rejected",
|
|
PublicationSource = "daemon-published",
|
|
Transport = "cultmesh-managed",
|
|
CommandBoundaryPath = AetheriaRuntimeVerseRecordKeys.DaemonCommandBoundary.ToString()
|
|
};
|
|
var assetManifest = AetheriaRuntimeAssets.ProjectManifest(
|
|
catalog,
|
|
frame.Run?.RunId ?? "",
|
|
"cultmesh://aetheria.local/assets");
|
|
var starbridgeSummary = AetheriaRuntimeStarbridgeDocuments.SessionSummary(
|
|
frame,
|
|
options.StarbridgeScenario,
|
|
options.StarbridgeSession,
|
|
catalog);
|
|
var gameSurface = AetheriaRuntimeDaemonGameSurfaceBuilder.Build(
|
|
frame,
|
|
health,
|
|
commandBoundary,
|
|
catalog: catalog);
|
|
var designerSurfaces = new[]
|
|
{
|
|
AetheriaRuntimeStatRecipeSurfaceBuilder.BuildFromCatalog(catalog),
|
|
AetheriaRuntimeTradeValuePolicySurfaceBuilder.BuildFromCatalog(catalog)
|
|
};
|
|
var editorSurface = AetheriaRuntimeDaemonEditorSurfaceBuilder.Build(
|
|
providerAdvertisement,
|
|
health,
|
|
commandBoundary,
|
|
designerSurfaces);
|
|
return new AetheriaRuntimeDaemonTickResult(
|
|
operationResult.Run,
|
|
operationResult,
|
|
frame,
|
|
soaFrame: null,
|
|
providerAdvertisement,
|
|
health,
|
|
commandBoundary,
|
|
assetManifest,
|
|
starbridgeSummary,
|
|
gameSurface,
|
|
gameSurface,
|
|
editorSurface,
|
|
editorSurface,
|
|
simulationStepsExecuted,
|
|
attentionInterruption);
|
|
}
|
|
|
|
private static AetheriaRuntimeDaemonIntentState MergeIntents(
|
|
AetheriaRuntimeDaemonIntentState commands,
|
|
AetheriaRuntimeDaemonIntentState agents)
|
|
{
|
|
var merged = new AetheriaRuntimeDaemonIntentState();
|
|
merged.Movements.AddRange(commands.Movements);
|
|
merged.Movements.AddRange(agents.Movements);
|
|
merged.WeaponGroups.AddRange(commands.WeaponGroups);
|
|
merged.WeaponGroups.AddRange(agents.WeaponGroups);
|
|
merged.Behaviors.AddRange(commands.Behaviors);
|
|
merged.Behaviors.AddRange(agents.Behaviors);
|
|
merged.Consumables.AddRange(commands.Consumables);
|
|
merged.Consumables.AddRange(agents.Consumables);
|
|
merged.Docking.AddRange(commands.Docking);
|
|
merged.Docking.AddRange(agents.Docking);
|
|
merged.Wormholes.AddRange(commands.Wormholes);
|
|
merged.Wormholes.AddRange(agents.Wormholes);
|
|
merged.SensorPings.AddRange(commands.SensorPings);
|
|
merged.SensorPings.AddRange(agents.SensorPings);
|
|
return merged;
|
|
}
|
|
|
|
private static IReadOnlyDictionary<string, string> MergeRejectedReasons(
|
|
IReadOnlyDictionary<string, string>? first,
|
|
IReadOnlyDictionary<string, string>? second)
|
|
{
|
|
var merged = new Dictionary<string, string>(StringComparer.Ordinal);
|
|
foreach (var pair in first ?? new Dictionary<string, string>())
|
|
merged[pair.Key] = pair.Value ?? "";
|
|
foreach (var pair in second ?? new Dictionary<string, string>())
|
|
merged[pair.Key] = pair.Value ?? "";
|
|
return merged;
|
|
}
|
|
|
|
private static void EnsureEntityIds(AetheriaRuntimeRunCheckpointCommit run)
|
|
{
|
|
var used = new HashSet<string>(StringComparer.Ordinal);
|
|
foreach (var zone in run.Zones ?? Array.Empty<AetheriaRuntimeZoneSnapshotCommit>())
|
|
foreach (var entity in zone?.Entities ?? Array.Empty<AetheriaRuntimeEntitySnapshotCommit>())
|
|
{
|
|
if (entity == null)
|
|
continue;
|
|
if (string.IsNullOrWhiteSpace(entity.EntityId))
|
|
entity.EntityId = $"aetheria.entity:{(string.IsNullOrWhiteSpace(run.RunId) ? "local" : run.RunId)}:{zone.ZoneIndex}:{entity.EntityIndex}";
|
|
if (!used.Add(entity.EntityId))
|
|
throw new InvalidOperationException($"Duplicate runtime entity identity '{entity.EntityId}'.");
|
|
}
|
|
}
|
|
|
|
private static void EnsureBehaviorStates(
|
|
AetheriaRuntimeRunCheckpointCommit run,
|
|
AetheriaRuntimeCatalogSnapshot? catalog)
|
|
{
|
|
if (catalog == null)
|
|
return;
|
|
foreach (var entity in (run.Zones ?? Array.Empty<AetheriaRuntimeZoneSnapshotCommit>())
|
|
.Where(zone => zone != null)
|
|
.SelectMany(zone => zone.Entities ?? Array.Empty<AetheriaRuntimeEntitySnapshotCommit>()))
|
|
{
|
|
AetheriaRuntimeBehaviorStateProjector.EnsureEquipmentBehaviorStates(entity, catalog);
|
|
}
|
|
}
|
|
|
|
private static void StampZoneSimulationTime(
|
|
AetheriaRuntimeRunCheckpointCommit run,
|
|
double simulationTimeSeconds)
|
|
{
|
|
foreach (var zone in run?.Zones ?? Array.Empty<AetheriaRuntimeZoneSnapshotCommit>())
|
|
{
|
|
if (zone != null)
|
|
zone.SimulationTimeSeconds = simulationTimeSeconds;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|