diff --git a/Aetheria.State.Daemon/Program.cs b/Aetheria.State.Daemon/Program.cs index 4b0dfc397..3003218d8 100644 --- a/Aetheria.State.Daemon/Program.cs +++ b/Aetheria.State.Daemon/Program.cs @@ -167,7 +167,6 @@ static async Task TickAsync( ? currentFrame.FixedDeltaSeconds : options.FixedDeltaSeconds; var nextFrameId = (currentFrame?.FrameId ?? -1) + 1; - var simulationTimeSeconds = (currentFrame?.SimulationTimeSeconds ?? 0) + fixedDeltaSeconds; var sessionId = string.IsNullOrWhiteSpace(currentFrame?.SessionId) ? options.SessionId : currentFrame.SessionId; @@ -198,6 +197,31 @@ static async Task TickAsync( authorityLeases, options.DaemonId, policyRejectedCommandIds); + var terminus = string.Equals(ingressState.GameMode, AetheriaGameSessionState.TerminusMode, StringComparison.Ordinal); + var simulationClockCommands = authorizedCommands + .Where(command => command.Kind == AetheriaRuntimeDaemonCommandKinds.SetSimulationRate) + .ToArray(); + if (!terminus && simulationClockCommands.Length > 0) + { + policyRejectedCommandIds.AddRange(simulationClockCommands.Select(command => command.CommandId)); + authorizedCommands = authorizedCommands + .Where(command => command.Kind != AetheriaRuntimeDaemonCommandKinds.SetSimulationRate) + .ToArray(); + } + else if (simulationClockCommands.Length > 0) + { + await ApplySimulationClockCommandsAsync(node, ingressState, simulationClockCommands).ConfigureAwait(false); + } + var simulationStepCount = terminus ? ingressState.TakeTerminusSimulationSteps() : 1; + var advanceSimulation = simulationStepCount > 0; + if (terminus && !advanceSimulation) + { + authorizedCommands = authorizedCommands + .Where(command => command.Kind == AetheriaRuntimeDaemonCommandKinds.SetSimulationRate) + .ToArray(); + } + var simulationTimeSeconds = (currentFrame?.SimulationTimeSeconds ?? 0) + + (simulationStepCount * fixedDeltaSeconds); TracePhase("tick-inputs"); var result = AetheriaRuntimeDaemonTickRunner.Tick( @@ -222,6 +246,8 @@ static async Task TickAsync( SimulationSettings = options.SimulationSettings, PhysicalPayloadPhysics = physicalPayloadPhysics, WorldPhysics = worldPhysics, + AdvanceSimulation = advanceSimulation, + SimulationStepCount = simulationStepCount, StarbridgeScenario = starbridgeScenario, StarbridgeSession = starbridgeSession, BuildPublications = buildPublications, @@ -259,7 +285,7 @@ static async Task TickAsync( if (buildPublications) { - await PublishDaemonApiDocumentsAsync(node, options, result).ConfigureAwait(false); + await PublishDaemonApiDocumentsAsync(node, options, result, publishTopology: true).ConfigureAwait(false); TracePhase("api-publication"); await PublishStateSurfacesAsync(node, options, result.Frame.PublishedAtUtc).ConfigureAwait(false); TracePhase("state-surfaces"); @@ -305,15 +331,17 @@ static async Task PublishPreparedDocumentsAsync( AetheriaDaemonHostOptions options, AetheriaRuntimeDaemonTickResult publication) { - await PublishDaemonApiDocumentsAsync(node, options, publication).ConfigureAwait(false); - await PublishStateSurfacesAsync(node, options, publication.Frame.PublishedAtUtc).ConfigureAwait(false); - await PublishOdinSurfaceAnnouncementsAsync(node, options, publication.Frame.PublishedAtUtc).ConfigureAwait(false); + await PublishDaemonApiDocumentsAsync(node, options, publication, publishTopology: false).ConfigureAwait(false); } static async Task RefreshControlPlaneInputsAsync( AetheriaStateNode node, AetheriaDaemonIngressState ingressState) { + var gameSession = await node.MutableDocument(AetheriaStateNode.GameSessionStateKey) + .ReadAsync().ConfigureAwait(false); + ingressState.GameMode = gameSession?.Mode ?? ""; + ingressState.SimulationRate = gameSession?.SimulationRate ?? 0; ingressState.LoadoutTemplates = node.Cache .GetAll() .Select(ToLoadoutTemplateCommit) @@ -332,6 +360,24 @@ static async Task RefreshControlPlaneInputsAsync( ingressState.ControlPlaneInitialized = true; } +static async Task ApplySimulationClockCommandsAsync( + AetheriaStateNode node, + AetheriaDaemonIngressState ingressState, + IReadOnlyList commands) +{ + var session = await node.MutableDocument(AetheriaStateNode.GameSessionStateKey) + .ReadAsync().ConfigureAwait(false) ?? new AetheriaGameSessionState(); + foreach (var command in commands.OrderBy(command => command.IssuedAtUtc, StringComparer.Ordinal)) + { + if (AetheriaRuntimeDaemonOperations.IsSupportedSimulationRate(command.ScalarValue)) + session.SimulationRate = command.ScalarValue; + } + session.UpdatedAtUtc = DateTimeOffset.UtcNow.ToString("O"); + await node.MutableDocument(AetheriaStateNode.GameSessionStateKey) + .ReplaceAsync(session).ConfigureAwait(false); + ingressState.SimulationRate = session.SimulationRate; +} + static async Task PublishCommittedCommandFactsAsync( AetheriaStateNode node, AetheriaDaemonHostOptions options, @@ -1429,7 +1475,8 @@ static bool TryGetDouble( static async Task PublishDaemonApiDocumentsAsync( AetheriaStateNode node, AetheriaDaemonHostOptions options, - AetheriaRuntimeDaemonTickResult result) + AetheriaRuntimeDaemonTickResult result, + bool publishTopology) { await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.DaemonFrameLatest) .ReplaceAsync(result.Frame) @@ -1448,13 +1495,14 @@ static async Task PublishDaemonApiDocumentsAsync( .ConfigureAwait(false); } - if (result.ProviderAdvertisement != null) + if (publishTopology && result.ProviderAdvertisement != null) await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.DaemonProviderAdvertisement) .ReplaceAsync(result.ProviderAdvertisement) .ConfigureAwait(false); - await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.EveProviderAdvertisement) - .ReplaceAsync(BuildCoreProviderAdvertisement(options, result.Frame.PublishedAtUtc)) - .ConfigureAwait(false); + if (publishTopology) + await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.EveProviderAdvertisement) + .ReplaceAsync(BuildCoreProviderAdvertisement(options, result.Frame.PublishedAtUtc)) + .ConfigureAwait(false); if (result.Health != null) await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.DaemonHealth) .ReplaceAsync(result.Health) @@ -1463,7 +1511,7 @@ static async Task PublishDaemonApiDocumentsAsync( await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.DaemonCommandBoundary) .ReplaceAsync(result.CommandBoundary) .ConfigureAwait(false); - if (result.AssetManifest != null) + if (publishTopology && result.AssetManifest != null) { await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.DaemonAssetManifest) .ReplaceAsync(result.AssetManifest) @@ -1476,8 +1524,12 @@ static async Task PublishDaemonApiDocumentsAsync( await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.StarbridgeSessionSummary) .ReplaceAsync(result.StarbridgeSessionSummary) .ConfigureAwait(false); + var gameSession = await node.MutableDocument(AetheriaStateNode.GameSessionStateKey) + .ReadAsync().ConfigureAwait(false); await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.PilotInputCapability) - .ReplaceAsync(AetheriaRuntimeInputCapabilityDocument.FromFrame(result.Frame).ToEveDocument()) + .ReplaceAsync(AetheriaRuntimeInputCapabilityDocument.FromFrame( + result.Frame, + string.Equals(gameSession?.Mode, AetheriaGameSessionState.TerminusMode, StringComparison.Ordinal)).ToEveDocument()) .ConfigureAwait(false); var mainMenuState = await node.MutableDocument(AetheriaStateNode.MainMenuStateKey) .ReadAsync() @@ -1493,6 +1545,9 @@ static async Task PublishDaemonApiDocumentsAsync( await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.DaemonGameSurface) .ReplaceAsync(AetheriaRuntimeSurfaceDocuments.ToPortableSurface(gameSurface)) .ConfigureAwait(false); + if (!publishTopology) + return; + var commanderSurface = AetheriaRuntimeDaemonGameSurfaceBuilder.BuildCommander( result.Frame, result.Health ?? new AetheriaRuntimeDaemonHealthDocument(), @@ -1821,11 +1876,15 @@ static async Task AcceptCoreEveInvocationsAsync( .Where(receipt => !string.IsNullOrWhiteSpace(receipt.CommandId)) .Select(receipt => receipt.CommandId) .ToHashSet(StringComparer.Ordinal); + var submitted = node.Documents() + .Where(command => command != null && !string.IsNullOrWhiteSpace(command.CommandId)) + .Select(command => command.CommandId) + .ToHashSet(StringComparer.Ordinal); foreach (var request in node.Documents() .Where(request => request != null && !string.IsNullOrWhiteSpace(request.CommandId)) .OrderBy(request => request.IssuedAt)) { - if (accounted.Contains(request.CommandId) || receipted.Contains(request.CommandId)) + if (accounted.Contains(request.CommandId) || receipted.Contains(request.CommandId) || submitted.Contains(request.CommandId)) continue; if (AetheriaRuntimeDaemonOperationsClient.TryCreateSurfaceCommandDocument( @@ -1840,6 +1899,7 @@ static async Task AcceptCoreEveInvocationsAsync( command.ClientId = request.ClientId; command.AuthorRuntimeId = request.ClientId; await node.SubmitDaemonCommandAsync(command).ConfigureAwait(false); + submitted.Add(command.CommandId); continue; } @@ -3052,12 +3112,23 @@ internal sealed class AetheriaDaemonIngressState { public int ObservedEveCommandCount { get; set; } = -1; public bool ControlPlaneInitialized { get; set; } + public string GameMode { get; set; } = ""; + public double SimulationRate { get; set; } + public double SimulationStepAccumulator { get; set; } public AetheriaRuntimeLoadoutTemplateCommit[] LoadoutTemplates { get; set; } = []; public AetheriaRuntimeCatalogSnapshot? Catalog { get; set; } public AetheriaRuntimeVerseAuthorityPolicyDocument? AuthorityPolicy { get; set; } public AetheriaRuntimeStarbridgeScenarioDocument? StarbridgeScenario { get; set; } public AetheriaRuntimeStarbridgeSessionDocument? StarbridgeSession { get; set; } public AetheriaRuntimeAuthorityLeaseDocument[] AuthorityLeases { get; set; } = []; + + public int TakeTerminusSimulationSteps() + { + SimulationStepAccumulator += Math.Max(0, SimulationRate); + var steps = (int)Math.Floor(SimulationStepAccumulator); + SimulationStepAccumulator -= steps; + return steps; + } } internal sealed class AetheriaDaemonHostOptions diff --git a/Aetheria.State/Documents/AetheriaGameSessionState.cs b/Aetheria.State/Documents/AetheriaGameSessionState.cs index 105e4dbe0..b195bcd00 100644 --- a/Aetheria.State/Documents/AetheriaGameSessionState.cs +++ b/Aetheria.State/Documents/AetheriaGameSessionState.cs @@ -17,4 +17,5 @@ public sealed class AetheriaGameSessionState [Key(4)] public string EntrySurfaceId { get; set; } = "aetheria.pilot"; [Key(5)] public string LastStartCommandId { get; set; } = ""; [Key(6)] public string UpdatedAtUtc { get; set; } = ""; + [Key(7)] public double SimulationRate { get; set; } } diff --git a/Packages/manifest.json b/Packages/manifest.json index 255523206..47d8c1a4a 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -21,10 +21,10 @@ "org.gamecult.aetheria.eve-runtime": "file:org.gamecult.aetheria.eve-runtime", "org.gamecult.aetheria.state": "file:org.gamecult.aetheria.state", "org.gamecult.cultmath": "file:../../CultMath", - "org.gamecult.cultlib": "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.7", - "org.gamecult.eve.surface": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.surface#b6f001b300a88aff0bc13372b5c1dbc2002954f8", + "org.gamecult.cultlib": "file:E:/Projects/CultLib-codex-cultmesh-reliability/artifacts/unity/org.gamecult.cultlib", + "org.gamecult.eve.surface": "file:E:/Projects/EveUnity-codex-cultmesh-sessions/packages/org.gamecult.eve.surface", "org.gamecult.eve.plugin-fields": "https://github.com/GameCult/EvePlugins.git?path=/plugins/eve-plugin-fields/unity/org.gamecult.eve.plugin-fields#eve-plugin-fields-unity-v0.1.1", - "org.gamecult.eve.unity-scene": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-scene#b6f001b300a88aff0bc13372b5c1dbc2002954f8", + "org.gamecult.eve.unity-scene": "file:E:/Projects/EveUnity-codex-cultmesh-sessions/packages/org.gamecult.eve.unity-scene", "org.gamecult.eve.unity-uitoolkit": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-uitoolkit#eveunity-uitoolkit-v0.1.0", "com.unity.modules.accessibility": "1.0.0", "com.unity.modules.adaptiveperformance": "1.0.0", diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonDocuments.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonDocuments.cs index 60eb2057a..44ec57606 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonDocuments.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonDocuments.cs @@ -194,7 +194,8 @@ namespace GameCult.Aetheria.State.Verse SetShutdownPerformance, ToggleShieldEnabled, IssueAgentTask, - CancelAgentTask + CancelAgentTask, + SetSimulationRate } [CultDocument("gamecult.aetheria.daemon_provider_advertisement", "gamecult.aetheria.daemon_provider_advertisement.v1")] diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationClient.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationClient.cs index e9325d92f..7861f540a 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationClient.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationClient.cs @@ -156,6 +156,15 @@ namespace GameCult.Aetheria.State.Verse return Send(command); } + internal AetheriaRuntimeDaemonCommandEnvelope SetSimulationRate( + AetheriaRuntimeDaemonFrameDocument? frame, + double rate) + { + var command = Create(AetheriaRuntimeDaemonCommandKinds.SetSimulationRate, frame); + command.ScalarValue = rate; + return Send(command); + } + internal AetheriaRuntimeDaemonCommandEnvelope SetLookDirection( AetheriaRuntimeDaemonFrameDocument? frame, double directionX, diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperations.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperations.cs index 237769baa..ee337bb11 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperations.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperations.cs @@ -179,11 +179,18 @@ namespace GameCult.Aetheria.State.Verse return ApplyIssueAgentTask(run, command); case AetheriaRuntimeDaemonCommandKinds.CancelAgentTask: return ApplyCancelAgentTask(run, command); + case AetheriaRuntimeDaemonCommandKinds.SetSimulationRate: + return IsSupportedSimulationRate(command.ScalarValue); default: return false; } } + public static readonly double[] SupportedSimulationRates = { 0, 0.25, 0.5, 1, 2, 4, 8, 16 }; + + public static bool IsSupportedSimulationRate(double value) => + SupportedSimulationRates.Any(rate => Math.Abs(value - rate) < 0.000001); + private static bool ApplyIssueAgentTask( AetheriaRuntimeRunCheckpointCommit run, AetheriaRuntimeDaemonCommandDocument command) diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationsClient.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationsClient.cs index a2bc41356..1cacb3dcb 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationsClient.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationsClient.cs @@ -479,6 +479,9 @@ public sealed class AetheriaRuntimeDaemonOperationsClient ReadPayloadDouble(request, "directionX", 0.0), ReadPayloadDouble(request, "directionY", 0.0), ReadPayloadDouble(request, "scalarValue", 1.0)), + AetheriaRuntimeDaemonCommandKinds.SetSimulationRate => client.SetSimulationRate( + frame, + ReadPayloadDouble(request, "scalarValue", 0.0)), AetheriaRuntimeDaemonCommandKinds.SetLookDirection => client.SetLookDirection( frame, ReadPayloadDouble(request, "directionX", 0.0), diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonSoaFramePublisher.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonSoaFramePublisher.cs index fba86ef5c..ed315564f 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonSoaFramePublisher.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonSoaFramePublisher.cs @@ -40,8 +40,21 @@ namespace GameCult.Aetheria.State.Verse var run = frame.Run ?? new AetheriaRuntimeRunCheckpointCommit(); var zone = (run.Zones ?? Array.Empty()) .FirstOrDefault(candidate => candidate != null && candidate.ZoneIndex == run.CurrentZoneIndex); + AetheriaRuntimeRunCheckpointCommit.TryParseEntityKey( + run.CurrentEntityKey, + out var controlledZoneIndex, + out var controlledEntityIndex); + var controlled = controlledZoneIndex == run.CurrentZoneIndex + ? (zone?.Entities ?? Array.Empty()) + .FirstOrDefault(entity => entity != null && entity.EntityIndex == controlledEntityIndex) + : null; + var visibleEntityIndices = (controlled?.Contacts ?? Array.Empty()) + .Where(contact => contact != null && contact.Visible) + .Select(contact => contact.TargetEntityIndex) + .Append(controlled?.EntityIndex ?? -1) + .ToHashSet(); var entities = (zone?.Entities ?? Array.Empty()) - .Where(entity => entity != null) + .Where(entity => entity != null && visibleEntityIndices.Contains(entity.EntityIndex)) .OrderBy(entity => entity.EntityIndex) .ToArray(); var count = entities.Length; @@ -84,8 +97,8 @@ namespace GameCult.Aetheria.State.Verse Kind = entity.Kind, Label = entity.Name, Faction = entity.FactionKey, - Selectable = true, - Controllable = string.Equals(entity.EntityId, run.CurrentEntityKey, StringComparison.Ordinal), + Selectable = entity.EntityIndex != controlled?.EntityIndex, + Controllable = entity.EntityIndex == controlled?.EntityIndex, AssetRef = AetheriaRuntimeAssets.ResolveEntityPrefabAssetRef(entity) }).ToArray()); diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonTickRunner.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonTickRunner.cs index 21eb2456b..4180fbe0f 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonTickRunner.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonTickRunner.cs @@ -38,6 +38,8 @@ namespace GameCult.Aetheria.State.Verse public IAetheriaRuntimePhysicalPayloadPhysics PhysicalPayloadPhysics { get; set; } = AetheriaRuntimePhysicalPayloadPhysicsUnavailable.Instance; public IAetheriaRuntimeWorldPhysics? WorldPhysics { get; set; } + public bool AdvanceSimulation { get; set; } = true; + public int SimulationStepCount { get; set; } = 1; public bool BuildPublications { get; set; } = true; } @@ -148,41 +150,47 @@ namespace GameCult.Aetheria.State.Verse .ToArray(), operationResult.Intents); } - var agentCommands = AetheriaRuntimeAgentScheduler.AssignAndPlan( - operationResult.Run, - options.FrameId, - options.Catalog, - options.SimulationTimeSeconds); - Trace("agent-planning"); - if (agentCommands.Count > 0) + var simulationStepCount = options.AdvanceSimulation ? Math.Max(1, options.SimulationStepCount) : 0; + for (var simulationStep = 0; simulationStep < simulationStepCount; simulationStep++) { - var agentResult = AetheriaRuntimeDaemonOperations.Execute( + var stepTime = options.SimulationTimeSeconds - + ((simulationStepCount - simulationStep - 1) * options.FixedDeltaSeconds); + var agentCommands = AetheriaRuntimeAgentScheduler.AssignAndPlan( 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)); + options.Catalog, + stepTime); + 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)); + } + AetheriaRuntimeDaemonSimulation.Step( + operationResult.Run, + operationResult.Intents, + options.FixedDeltaSeconds, + options.SimulationSettings, + options.PhysicalPayloadPhysics, + options.WorldPhysics ?? throw new InvalidOperationException("Ymir world physics owner is required."), + options.Catalog, + options.FrameId, + options.SimulationTimeSeconds); + Trace("world-step"); + StampZoneSimulationTime(operationResult.Run, options.SimulationTimeSeconds); } - AetheriaRuntimeDaemonSimulation.Step( - operationResult.Run, - operationResult.Intents, - options.FixedDeltaSeconds, - options.SimulationSettings, - options.PhysicalPayloadPhysics, - options.WorldPhysics ?? throw new InvalidOperationException("Ymir world physics owner is required."), - options.Catalog, - options.FrameId, - options.SimulationTimeSeconds); - Trace("world-step"); - StampZoneSimulationTime(operationResult.Run, options.SimulationTimeSeconds); var frame = AetheriaRuntimeDaemonFrameDocument.Create( operationResult.Run, diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeEveEntitySoaProjection.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeEveEntitySoaProjection.cs index 66e4ae9d0..589855180 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeEveEntitySoaProjection.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeEveEntitySoaProjection.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using GameCult.Eve.Surface; +using GameCult.Mesh; #nullable enable @@ -11,6 +12,7 @@ namespace GameCult.Aetheria.State.Verse public static EveEntitySoaViewDocument Project(AetheriaRuntimeDaemonSoaViewDocument source) { if (source == null) throw new ArgumentNullException(nameof(source)); + var primaryBuffer = (source.Buffers ?? Array.Empty()).FirstOrDefault(); return new EveEntitySoaViewDocument { ProviderId = "aetheria.daemon", @@ -20,6 +22,24 @@ namespace GameCult.Aetheria.State.Verse PublishedAtUtc = source.PublishedAtUtc, Backend = source.Backend, SynchronizationMode = source.SynchronizationMode, + Body = primaryBuffer == null ? null : new CultMeshBodyDescriptor + { + BodyId = $"eve:entity-soa:{source.DaemonId}:{source.SessionId}:pilot", + SchemaId = EveEntitySoaViewDocument.SchemaId + ".body.v1", + LayoutVersion = 1, + ByteSize = primaryBuffer.ByteLength, + Capacity = (source.Columns ?? Array.Empty()) + .Select(column => column.ElementCount) + .DefaultIfEmpty(0) + .Max(), + ProducerEpoch = StableEpoch(source.SessionId), + Sequence = source.Generation, + AccessMode = CultMeshBodyAccessMode.ReadOnly, + Synchronization = CultMeshBodySynchronization.ImmutableSequence, + LeaseExpiresAtUnixMs = DateTimeOffset.UtcNow.AddSeconds(5).ToUnixTimeMilliseconds(), + TransportKind = CultMeshBodyTransportKind.SharedMemory, + CapabilityToken = primaryBuffer.Location + }, Buffers = (source.Buffers ?? Array.Empty()) .Select(buffer => new EveEntitySoaBuffer { @@ -85,5 +105,21 @@ namespace GameCult.Aetheria.State.Verse }).ToArray() }; } + + private static long StableEpoch(string value) + { + unchecked + { + const long offset = 1469598103934665603; + const long prime = 1099511628211; + var hash = offset; + foreach (var character in value ?? string.Empty) + { + hash ^= character; + hash *= prime; + } + return hash & long.MaxValue; + } + } } } diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeInputCapabilityDocuments.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeInputCapabilityDocuments.cs index a359c2031..cd3975282 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeInputCapabilityDocuments.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeInputCapabilityDocuments.cs @@ -57,12 +57,25 @@ namespace GameCult.Aetheria.State.Verse [Key(4)] public AetheriaRuntimeInputActionDocument[] Actions { get; set; } = Array.Empty(); [Key(5)] public AetheriaRuntimeInputProfileDocument[] DefaultProfiles { get; set; } = Array.Empty(); - public static AetheriaRuntimeInputCapabilityDocument FromFrame(AetheriaRuntimeDaemonFrameDocument frame) + public static AetheriaRuntimeInputCapabilityDocument FromFrame( + AetheriaRuntimeDaemonFrameDocument frame, + bool includeSimulationClock = false) { var run = frame?.Run ?? new AetheriaRuntimeRunCheckpointCommit(); var entity = run.Zones.SelectMany(zone => zone.Entities).FirstOrDefault(candidate => string.Equals(run.EntityRecordKey(run.CurrentZoneIndex, candidate.EntityIndex), run.CurrentEntityKey, StringComparison.Ordinal)); var actions = CoreActions().ToList(); + if (includeSimulationClock) + { + actions.Add(Action("simulation.pause", "Pause", "SetSimulationRate", "simulation", "terminus-clock", ("scalarValue", "0"))); + actions.Add(Action("simulation.rate.quarter", "Quarter Speed", "SetSimulationRate", "simulation", "terminus-clock", ("scalarValue", "0.25"))); + actions.Add(Action("simulation.rate.half", "Half Speed", "SetSimulationRate", "simulation", "terminus-clock", ("scalarValue", "0.5"))); + actions.Add(Action("simulation.rate.realtime", "Real Time", "SetSimulationRate", "simulation", "terminus-clock", ("scalarValue", "1"))); + actions.Add(Action("simulation.rate.double", "2x Speed", "SetSimulationRate", "simulation", "terminus-clock", ("scalarValue", "2"))); + actions.Add(Action("simulation.rate.quadruple", "4x Speed", "SetSimulationRate", "simulation", "terminus-clock", ("scalarValue", "4"))); + actions.Add(Action("simulation.rate.eight", "8x Speed", "SetSimulationRate", "simulation", "terminus-clock", ("scalarValue", "8"))); + actions.Add(Action("simulation.rate.sixteen", "16x Speed", "SetSimulationRate", "simulation", "terminus-clock", ("scalarValue", "16"))); + } if (entity != null) { actions.AddRange((entity.WeaponGroups ?? Array.Empty>()).Select((_, index) => Action($"weapon-group.{index}.fire", $"Fire Weapon Group {index + 1}", "FireWeaponGroup", "weapon-group", $"{run.CurrentEntityKey}#weapon-group/{index}", ("weaponGroup", index.ToString())))); @@ -108,6 +121,10 @@ namespace GameCult.Aetheria.State.Verse bindings.Add(keyboard ? Binding("dock.r", "pilot.dock", "direct", "keyboard.r") : Binding("dock.sequence", "pilot.dock", "sequence", "gamepad.dpad.down", "gamepad.dpad.up")); + if (actions.Any(action => string.Equals(action.ActionId, "simulation.pause", StringComparison.Ordinal))) + bindings.Add(keyboard + ? Binding("simulation.pause", "simulation.pause", "direct", "keyboard.pause") + : Binding("simulation.pause.sequence", "simulation.pause", "sequence", "gamepad.dpad.left", "gamepad.dpad.left")); return bindings.ToArray(); } diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeThermalSimulation.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeThermalSimulation.cs index 1bd137450..3a486f56f 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeThermalSimulation.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeThermalSimulation.cs @@ -150,8 +150,17 @@ namespace GameCult.Aetheria.State.Verse { EnsureState(entity); var temperature = Find(entity.StatGrids, TemperatureGrid)!; - var cells = EquipmentCellIndices(entity, catalog, equipmentIndex).ToArray(); - return cells.Length == 0 ? Mean(temperature.Values) : cells.Average(index => temperature.Values[index]); + var thermalMass = Find(entity.StatGrids, ThermalMassGrid)!; + var cells = EquipmentCellIndices(entity, catalog, equipmentIndex) + .Where(index => index >= 0 && index < thermalMass.Values.Count && thermalMass.Values[index] > 0) + .ToArray(); + if (cells.Length > 0) + return cells.Average(index => temperature.Values[index]); + return Enumerable.Range(0, temperature.Values.Count) + .Where(index => index < thermalMass.Values.Count && thermalMass.Values[index] > 0) + .Select(index => temperature.Values[index]) + .DefaultIfEmpty(InitialTemperature) + .Average(); } public static void UpdateEquipmentStates(AetheriaRuntimeEntitySnapshotCommit entity, diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index b73608dbb..4b94206bb 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -311,11 +311,10 @@ } }, "org.gamecult.cultlib": { - "version": "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-v1.0.7", + "version": "file:E:/Projects/CultLib-codex-cultmesh-reliability/artifacts/unity/org.gamecult.cultlib", "depth": 0, - "source": "git", - "dependencies": {}, - "hash": "1f8f832a788e2bdf59df90fb91da583574d3a42d" + "source": "local", + "dependencies": {} }, "org.gamecult.cultmath": { "version": "file:../../CultMath", @@ -331,25 +330,23 @@ "hash": "0b099274a4e2b4c301eced4322166837792f585a" }, "org.gamecult.eve.surface": { - "version": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.surface#b6f001b300a88aff0bc13372b5c1dbc2002954f8", + "version": "file:E:/Projects/EveUnity-codex-cultmesh-sessions/packages/org.gamecult.eve.surface", "depth": 0, - "source": "git", + "source": "local", "dependencies": { "org.gamecult.cultlib": "1.0.6" - }, - "hash": "b6f001b300a88aff0bc13372b5c1dbc2002954f8" + } }, "org.gamecult.eve.unity-scene": { - "version": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-scene#b6f001b300a88aff0bc13372b5c1dbc2002954f8", + "version": "file:E:/Projects/EveUnity-codex-cultmesh-sessions/packages/org.gamecult.eve.unity-scene", "depth": 0, - "source": "git", + "source": "local", "dependencies": { "org.gamecult.cultlib": "1.0.6", "org.gamecult.eve.plugin-fields": "0.1.1", "org.gamecult.eve.surface": "0.1.0", "com.unity.mathematics": "1.3.2" - }, - "hash": "b6f001b300a88aff0bc13372b5c1dbc2002954f8" + } }, "org.gamecult.eve.unity-uitoolkit": { "version": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-uitoolkit#eveunity-uitoolkit-v0.1.0",