Run daemon combat at realtime cadence

This commit is contained in:
Meta Vens 2026-07-13 01:22:48 +01:00
commit 96923e1dd0
9 changed files with 325 additions and 91 deletions

View file

@ -74,6 +74,8 @@ public sealed class AetheriaDaemonLoadoutGenerator
var cargoBay = AddFreeSpaceItem(hull, availabilityFactionKey, occupied, slots, 3, item =>
string.Equals(item.Category, AetheriaRuntimeItemCategories.CargoBay, StringComparison.Ordinal));
AddFreeSpaceItem(hull, availabilityFactionKey, occupied, slots, 2, item =>
IsGear(item) && HasBehavior(item, "Reactor"));
AddFreeSpaceItem(hull, availabilityFactionKey, occupied, slots, 2, item =>
IsGear(item) && HasBehavior(item, "Capacitor"));
@ -90,7 +92,16 @@ public sealed class AetheriaDaemonLoadoutGenerator
PriceExponent = _priceExponent,
Selections = selectedKeys.Select(value => Selection(value.Role, value.Key, availabilityFactionKey)).ToArray()
};
return new AetheriaDaemonLoadout(hull.ItemKey, slots.ToArray(), cargo, receipt);
var equipment = slots.ToArray();
var defaultWeaponGroup = equipment
.Select((slot, index) => (slot, index))
.Where(value => IsWeapon(_catalog.FindItem(value.slot.ItemKey)))
.Select(value => value.index)
.ToArray();
var weaponGroups = defaultWeaponGroup.Length == 0
? Array.Empty<int[]>()
: new[] { defaultWeaponGroup };
return new AetheriaDaemonLoadout(hull.ItemKey, equipment, cargo, weaponGroups, receipt);
}
private AetheriaLoadoutGenerationSelection Selection(string role, string itemKey, string factionKey)
@ -327,6 +338,12 @@ public sealed class AetheriaDaemonLoadoutGenerator
string.Equals(item.Category, AetheriaRuntimeItemCategories.Gear, StringComparison.Ordinal) ||
string.Equals(item.Category, AetheriaRuntimeItemCategories.Weapon, StringComparison.Ordinal);
private static bool IsWeapon(AetheriaRuntimeCatalogItem? item) =>
item != null &&
(string.Equals(item.Category, AetheriaRuntimeItemCategories.Weapon, StringComparison.Ordinal) ||
(item.BehaviorKinds ?? Array.Empty<string>()).Any(kind =>
kind.Contains("Weapon", StringComparison.Ordinal)));
private static AetheriaEntityItemSlot Slot(int x, int y, string itemKey) => new()
{
Position = new AetheriaGridCoord { X = x, Y = y }, ItemKey = itemKey,
@ -347,4 +364,5 @@ public sealed record AetheriaDaemonLoadout(
string HullItemKey,
AetheriaEntityItemSlot[] Equipment,
AetheriaLoadoutItemSlot[] Cargo,
int[][] WeaponGroups,
AetheriaLoadoutGenerationReceipt Receipt);

View file

@ -211,10 +211,10 @@ internal static class AetheriaDaemonZoneGenerator
Entity(loadouts, availabilityFactions, "Torch Three", "ship", -235, 210, 8, -4, "player", 500, keys[7], [keys[0], keys[1], keys[7]], ["micro-missile", "coolant-pack"]),
Entity(loadouts, availabilityFactions, "Foundry Tug", "ship", 330, -155, -2, 3, "player", 390, keys[6], [keys[0], keys[1], keys[6], keys[10]], ["ore-canister", "field-rations"]),
Entity(loadouts, availabilityFactions, "Derelict Relay", "station", -390, 270, 0, 0, "neutral", 160, "", [keys[0], keys[3]], ["ancient-transponder"]),
Entity(loadouts, availabilityFactions, "Ash Raider", "ship", 455, 180, -5, -2, "raider", 320, keys[1], [keys[0], keys[1], keys[2], keys[4]], ["scrap-metal", "stolen-capacitor"]),
Entity(loadouts, availabilityFactions, "Cinder Knife", "ship", 585, -115, -7, 1, "raider", 280, keys[3], [keys[0], keys[3]], ["volatile-fuel"]),
Entity(loadouts, availabilityFactions, "Blackwake", "ship", 690, 300, -4, -3, "raider", 230, keys[2], [keys[2]], ["ore-cache", "burned-relay-core"]),
Entity(loadouts, availabilityFactions, "Vesper Sloop", "ship", 960, 620, -3, -5, "raider", 250, keys[4], [keys[4]], ["stolen-map-fragment"]),
Entity(loadouts, availabilityFactions, "Ash Raider", "ship", 100, -30, -5, -2, "raider", 320, "", [keys[0], keys[1], keys[2], keys[4]], ["scrap-metal", "stolen-capacitor"]),
Entity(loadouts, availabilityFactions, "Cinder Knife", "ship", 585, -115, -7, 1, "raider", 280, "", [keys[0], keys[3]], ["volatile-fuel"]),
Entity(loadouts, availabilityFactions, "Blackwake", "ship", 690, 300, -4, -3, "raider", 230, "", [keys[2]], ["ore-cache", "burned-relay-core"]),
Entity(loadouts, availabilityFactions, "Vesper Sloop", "ship", 960, 620, -3, -5, "raider", 250, "", [keys[4]], ["stolen-map-fragment"]),
Entity(loadouts, availabilityFactions, "Survey Skiff", "ship", -610, -365, 3, 5, "neutral", 210, "", [keys[4], keys[5]], ["soil-sample", "deep-scan"]),
Entity(loadouts, availabilityFactions, "Lagrange Beacon", "station", 105, 510, 0, 0, "neutral", 190, "", [keys[0], keys[1], keys[6]], ["navigation-key"])
};
@ -272,6 +272,9 @@ internal static class AetheriaDaemonZoneGenerator
StatGrid("heat", 0)
],
Equipment = loadout.Equipment,
WeaponGroups = loadout.WeaponGroups
.Select(indices => new AetheriaWeaponGroupSnapshot { EquipmentIndices = indices })
.ToArray(),
CargoContents =
[
new AetheriaCargoBayLoadout

View file

@ -8,6 +8,7 @@ using GameCult.Mesh;
using GameCult.Networking;
using MessagePack;
using System.Globalization;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
@ -50,7 +51,8 @@ using var clientSubscriptions = new CultNetDatabaseSubscriptionServer(cultMeshRu
using var clientPumpCancellation = new CancellationTokenSource();
var clientPump = RunClientCultMeshPumpAsync(cultMeshRudpHost, clientPumpCancellation.Token);
var nextApiPublicationUtc = DateTimeOffset.UtcNow;
var firstTick = await TickAsync(node, options, physicalPayloadPhysics, worldPhysics, latestFrame, buildPublications: true).ConfigureAwait(false);
var ingressState = new AetheriaDaemonIngressState();
var firstTick = await TickAsync(node, options, physicalPayloadPhysics, worldPhysics, latestFrame, ingressState, buildPublications: true).ConfigureAwait(false);
ThrowIfClientPumpFaulted(clientPump);
latestFrame = firstTick.Frame;
nextApiPublicationUtc = DateTimeOffset.UtcNow.Add(options.ApiPublicationInterval);
@ -75,6 +77,7 @@ await PublishRuntimeSessionAsync(node, options, startedAtUtc, "running").Configu
Console.WriteLine("Aetheria Verse daemon is running. Press Ctrl+C to stop.");
var nextTickUtc = DateTimeOffset.UtcNow.Add(options.TickInterval);
Task publicationTask = Task.CompletedTask;
while (!stopped.Task.IsCompleted)
{
ThrowIfClientPumpFaulted(clientPump);
@ -87,17 +90,19 @@ while (!stopped.Task.IsCompleted)
}
var buildPublications = DateTimeOffset.UtcNow >= nextApiPublicationUtc;
var tick = await TickAsync(node, options, physicalPayloadPhysics, worldPhysics, latestFrame, buildPublications).ConfigureAwait(false);
var tick = await TickAsync(node, options, physicalPayloadPhysics, worldPhysics, latestFrame, ingressState, buildPublications: false).ConfigureAwait(false);
ThrowIfClientPumpFaulted(clientPump);
latestFrame = tick.Frame;
nextTickUtc += options.TickInterval;
if (nextTickUtc < DateTimeOffset.UtcNow - options.TickInterval)
nextTickUtc = DateTimeOffset.UtcNow;
if (buildPublications)
if (buildPublications && publicationTask.IsCompleted)
{
if (publicationTask.IsFaulted)
await publicationTask.ConfigureAwait(false);
var publication = PreparePublication(node.StatePath, options, tick, ingressState);
publicationTask = PublishPreparedDocumentsAsync(node, options, publication);
nextApiPublicationUtc = DateTimeOffset.UtcNow.Add(options.ApiPublicationInterval);
discoveryHost.Update(await node.MutableDocument<AetheriaVerseHostSettings>(AetheriaStateNode.VerseHostSettingsKey).ReadAsync().ConfigureAwait(false));
await PublishRuntimeSessionAsync(node, options, startedAtUtc, "running").ConfigureAwait(false);
}
if (tick.Frame.FrameId % (traceClientRudp ? 10 : 120) == 0)
{
@ -109,6 +114,7 @@ while (!stopped.Task.IsCompleted)
}
await PublishRuntimeSessionAsync(node, options, startedAtUtc, "stopping").ConfigureAwait(false);
await publicationTask.ConfigureAwait(false);
clientPumpCancellation.Cancel();
await clientPump.ConfigureAwait(false);
Console.WriteLine("Aetheria Verse daemon stopping.");
@ -130,12 +136,32 @@ static async Task<AetheriaRuntimeDaemonTickResult> TickAsync(
IAetheriaRuntimePhysicalPayloadPhysics physicalPayloadPhysics,
IAetheriaRuntimeWorldPhysics worldPhysics,
AetheriaRuntimeDaemonFrameDocument? currentFrame,
AetheriaDaemonIngressState ingressState,
bool buildPublications)
{
var traceTickPhases = string.Equals(
Environment.GetEnvironmentVariable("AETHERIA_TRACE_TICK_PHASES"),
"1",
StringComparison.Ordinal);
var phase = Stopwatch.StartNew();
void TracePhase(string name)
{
if (traceTickPhases && phase.ElapsedMilliseconds >= 20)
Console.WriteLine($"Aetheria tick phase {name} took {phase.ElapsedMilliseconds}ms.");
phase.Restart();
}
await AcceptCoreEveInvocationsAsync(node, options, currentFrame).ConfigureAwait(false);
await AcceptEveCommandsAsync(node, options).ConfigureAwait(false);
TracePhase("core-ingress");
await AcceptEveCommandsAsync(node, options, ingressState).ConfigureAwait(false);
TracePhase("provider-ingress");
if (await ApplyRequestedTerminusSessionAsync(node, options).ConfigureAwait(false))
{
currentFrame = null;
ingressState.ControlPlaneInitialized = false;
}
if (!ingressState.ControlPlaneInitialized || buildPublications)
await RefreshControlPlaneInputsAsync(node, ingressState).ConfigureAwait(false);
var fixedDeltaSeconds = currentFrame?.FixedDeltaSeconds > 0
? currentFrame.FixedDeltaSeconds
@ -150,10 +176,7 @@ static async Task<AetheriaRuntimeDaemonTickResult> TickAsync(
: await ReadRuntimeRunCheckpointAsync(node, options.RenderSettings).ConfigureAwait(false) ?? new AetheriaRuntimeRunCheckpointCommit();
ApplyDaemonRenderSettings(run, options.RenderSettings);
var loadoutTemplates = node.Cache
.GetAll<AetheriaLoadoutTemplate>()
.Select(ToLoadoutTemplateCommit)
.ToArray();
var loadoutTemplates = ingressState.LoadoutTemplates;
var observedCommands = node.Documents<AetheriaRuntimeDaemonCommandDocument>()
.OrderBy(command => command.IssuedAtUtc ?? "", StringComparer.Ordinal)
.ThenBy(command => command.CommandId ?? "", StringComparer.Ordinal)
@ -165,16 +188,17 @@ static async Task<AetheriaRuntimeDaemonTickResult> TickAsync(
.Where(command => command != null && !accountedCommandIds.Contains(command.CommandId ?? ""))
.ToArray();
var policyRejectedCommandIds = new List<string>();
var authorityPolicy = await node.MutableDocument<AetheriaRuntimeVerseAuthorityPolicyDocument>(AetheriaRuntimeVerseRecordKeys.VerseAuthorityPolicy).ReadAsync().ConfigureAwait(false);
var starbridgeScenario = await node.MutableDocument<AetheriaRuntimeStarbridgeScenarioDocument>(AetheriaRuntimeVerseRecordKeys.StarbridgeScenarioLatest).ReadAsync().ConfigureAwait(false);
var starbridgeSession = await node.MutableDocument<AetheriaRuntimeStarbridgeSessionDocument>(AetheriaRuntimeVerseRecordKeys.StarbridgeSessionLatest).ReadAsync().ConfigureAwait(false);
var authorityLeases = node.Documents<AetheriaRuntimeAuthorityLeaseDocument>();
var authorityPolicy = ingressState.AuthorityPolicy;
var starbridgeScenario = ingressState.StarbridgeScenario;
var starbridgeSession = ingressState.StarbridgeSession;
var authorityLeases = ingressState.AuthorityLeases;
var authorizedCommands = AetheriaRuntimeAuthorityRouter.AuthorizedCommands(
pendingObservedCommands,
authorityPolicy,
authorityLeases,
options.DaemonId,
policyRejectedCommandIds);
TracePhase("tick-inputs");
var result = AetheriaRuntimeDaemonTickRunner.Tick(
node.StatePath,
@ -193,7 +217,7 @@ static async Task<AetheriaRuntimeDaemonTickResult> TickAsync(
PreRejectedCommandIds = policyRejectedCommandIds,
CumulativeAppliedCommandIds = currentFrame?.CumulativeAppliedCommandIds ?? currentFrame?.AppliedCommandIds ?? Array.Empty<string>(),
CumulativeRejectedCommandIds = currentFrame?.CumulativeRejectedCommandIds ?? currentFrame?.RejectedCommandIds ?? Array.Empty<string>(),
Catalog = node.RuntimeCatalog().Latest(),
Catalog = ingressState.Catalog,
RenderSettings = options.RenderSettings,
SimulationSettings = options.SimulationSettings,
PhysicalPayloadPhysics = physicalPayloadPhysics,
@ -206,6 +230,7 @@ static async Task<AetheriaRuntimeDaemonTickResult> TickAsync(
LoadoutTemplates = loadoutTemplates
}
});
TracePhase("simulation");
if (string.Equals(Environment.GetEnvironmentVariable("AETHERIA_TRACE_EVE_SNAPSHOTS"), "1", StringComparison.Ordinal) &&
result.Frame.AppliedCommandIds.Count > 0)
{
@ -230,17 +255,83 @@ static async Task<AetheriaRuntimeDaemonTickResult> TickAsync(
pendingObservedCommands,
authorizedCommands,
policyRejectedCommandIds).ConfigureAwait(false);
TracePhase("command-facts");
if (buildPublications)
{
await PublishDaemonApiDocumentsAsync(node, options, result).ConfigureAwait(false);
TracePhase("api-publication");
await PublishStateSurfacesAsync(node, options, result.Frame.PublishedAtUtc).ConfigureAwait(false);
TracePhase("state-surfaces");
await PublishOdinSurfaceAnnouncementsAsync(node, options, result.Frame.PublishedAtUtc).ConfigureAwait(false);
TracePhase("odin-announcements");
}
return result;
}
static AetheriaRuntimeDaemonTickResult PreparePublication(
string statePath,
AetheriaDaemonHostOptions options,
AetheriaRuntimeDaemonTickResult tick,
AetheriaDaemonIngressState ingressState)
{
var frameBytes = MessagePackSerializer.Serialize(tick.Frame);
var frame = MessagePackSerializer.Deserialize<AetheriaRuntimeDaemonFrameDocument>(frameBytes);
var operation = new AetheriaRuntimeDaemonOperationResult(
frame.Run ?? new AetheriaRuntimeRunCheckpointCommit(),
tick.OperationResult.AppliedCommandIds.ToArray(),
tick.OperationResult.RejectedCommandIds.ToArray());
return AetheriaRuntimeDaemonTickRunner.BuildPublications(
statePath,
operation,
frame,
new AetheriaRuntimeDaemonTickOptions
{
DaemonId = options.DaemonId,
VerseId = options.VerseId,
CultMeshAddress = options.CultMeshAddress,
Catalog = ingressState.Catalog,
StarbridgeScenario = ingressState.StarbridgeScenario,
StarbridgeSession = ingressState.StarbridgeSession,
RenderSettings = options.RenderSettings,
SimulationSettings = options.SimulationSettings
},
frame.AccountedCommandIds?.Count ?? 0);
}
static async Task PublishPreparedDocumentsAsync(
AetheriaStateNode node,
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);
}
static async Task RefreshControlPlaneInputsAsync(
AetheriaStateNode node,
AetheriaDaemonIngressState ingressState)
{
ingressState.LoadoutTemplates = node.Cache
.GetAll<AetheriaLoadoutTemplate>()
.Select(ToLoadoutTemplateCommit)
.ToArray();
ingressState.Catalog = node.RuntimeCatalog().Latest();
ingressState.AuthorityPolicy = await node
.MutableDocument<AetheriaRuntimeVerseAuthorityPolicyDocument>(AetheriaRuntimeVerseRecordKeys.VerseAuthorityPolicy)
.ReadAsync().ConfigureAwait(false);
ingressState.StarbridgeScenario = await node
.MutableDocument<AetheriaRuntimeStarbridgeScenarioDocument>(AetheriaRuntimeVerseRecordKeys.StarbridgeScenarioLatest)
.ReadAsync().ConfigureAwait(false);
ingressState.StarbridgeSession = await node
.MutableDocument<AetheriaRuntimeStarbridgeSessionDocument>(AetheriaRuntimeVerseRecordKeys.StarbridgeSessionLatest)
.ReadAsync().ConfigureAwait(false);
ingressState.AuthorityLeases = node.Documents<AetheriaRuntimeAuthorityLeaseDocument>().ToArray();
ingressState.ControlPlaneInitialized = true;
}
static async Task PublishCommittedCommandFactsAsync(
AetheriaStateNode node,
AetheriaDaemonHostOptions options,
@ -1774,9 +1865,15 @@ static async Task AcceptCoreEveInvocationsAsync(
}
}
static async Task AcceptEveCommandsAsync(AetheriaStateNode node, AetheriaDaemonHostOptions options)
static async Task AcceptEveCommandsAsync(
AetheriaStateNode node,
AetheriaDaemonHostOptions options,
AetheriaDaemonIngressState ingressState)
{
var commandCountBefore = node.Documents<AetheriaRuntimeEveCommandDocument>().Count;
if (commandCountBefore == ingressState.ObservedEveCommandCount)
return;
ingressState.ObservedEveCommandCount = commandCountBefore;
var now = DateTimeOffset.UtcNow.ToString("O");
try
{
@ -2956,6 +3053,18 @@ static AetheriaRuntimeLoadoutItemCommit ToLoadoutItemCommit(AetheriaLoadoutItem?
};
}
internal sealed class AetheriaDaemonIngressState
{
public int ObservedEveCommandCount { get; set; } = -1;
public bool ControlPlaneInitialized { 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; } = [];
}
internal sealed class AetheriaDaemonHostOptions
{
public string StatePath { get; init; } = "";