Move projectile physics behind daemon Ymir authority

This commit is contained in:
Meta Vens 2026-07-11 17:43:48 +01:00
commit fe0c193724
12 changed files with 424 additions and 404 deletions

View file

@ -9,5 +9,6 @@
<ItemGroup>
<ProjectReference Include="..\Aetheria.State\Aetheria.State.csproj" />
<ProjectReference Include="..\..\Ymir\src\Ymir.Core\Ymir.Core.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,120 @@
using GameCult.Aetheria.State.Verse;
using Ymir.Core;
namespace Aetheria.State.Daemon;
public sealed class AetheriaYmirProjectilePhysics : IAetheriaRuntimeProjectilePhysics
{
private const string ProjectileBodyPrefix = "aetheria.projectile.";
private const string EntityBodyPrefix = "aetheria.daemon.entity.";
private readonly YmirSimulator _simulator;
public AetheriaYmirProjectilePhysics(YmirSimulator? simulator = null)
{
_simulator = simulator ?? new YmirSimulator();
}
public string AuthorityId => "ymir.core";
public AetheriaRuntimeProjectileStep Step(
AetheriaRuntimeZoneSnapshotCommit zone,
IReadOnlyList<AetheriaRuntimeEntitySnapshotCommit> entities,
double deltaSeconds)
{
ArgumentNullException.ThrowIfNull(zone);
if (deltaSeconds <= 0)
throw new ArgumentOutOfRangeException(nameof(deltaSeconds));
var entityByBodyId = (entities ?? Array.Empty<AetheriaRuntimeEntitySnapshotCommit>())
.Where(entity => entity is { IsActive: true })
.ToDictionary(entity => EntityBodyId(entity.EntityIndex), StringComparer.Ordinal);
var survivors = new List<AetheriaRuntimeProjectileCommit>();
var hits = new List<AetheriaRuntimeProjectileHit>();
foreach (var projectile in zone.Projectiles ?? Array.Empty<AetheriaRuntimeProjectileCommit>())
{
if (projectile is not { Active: true })
continue;
var projectileBodyId = ProjectileBodyId(projectile.ProjectileId);
var bodies = new List<PhysicsBody>
{
new(
projectileBodyId,
new Vec2((float)projectile.PositionX, (float)projectile.PositionZ),
new Vec2((float)projectile.VelocityX, (float)projectile.VelocityY),
(float)Math.Max(0.001, projectile.Radius),
1.0f,
Restitution: 0.0f)
};
foreach (var pair in entityByBodyId)
{
var entity = pair.Value;
if (entity.EntityIndex == projectile.SourceEntityIndex)
continue;
bodies.Add(new PhysicsBody(
pair.Key,
new Vec2((float)entity.PositionX, (float)entity.PositionZ),
Vec2.Zero,
ResolveEntityRadius(entity),
1.0f,
IsStatic: true,
Restitution: 0.0f));
}
var result = _simulator.Step(new SimulationStepRequest(
(float)deltaSeconds,
new YmirWorld((float)zone.SimulationTimeSeconds, bodies, Array.Empty<RadialField>())));
var projectileBody = result.World.Bodies.Single(body =>
string.Equals(body.Id, projectileBodyId, StringComparison.Ordinal));
projectile.PositionX = projectileBody.Position.X;
projectile.PositionZ = projectileBody.Position.Y;
projectile.VelocityX = projectileBody.Velocity.X;
projectile.VelocityY = projectileBody.Velocity.Y;
var contact = result.Contacts.FirstOrDefault(candidate =>
string.Equals(candidate.BodyA, projectileBodyId, StringComparison.Ordinal) ||
string.Equals(candidate.BodyB, projectileBodyId, StringComparison.Ordinal));
if (contact == null)
{
survivors.Add(projectile);
continue;
}
var targetBodyId = string.Equals(contact.BodyA, projectileBodyId, StringComparison.Ordinal)
? contact.BodyB
: contact.BodyA;
if (!entityByBodyId.TryGetValue(targetBodyId, out var target))
{
survivors.Add(projectile);
continue;
}
var normal = string.Equals(contact.BodyA, projectileBodyId, StringComparison.Ordinal)
? contact.Normal
: new Vec2(-contact.Normal.X, -contact.Normal.Y);
hits.Add(new AetheriaRuntimeProjectileHit
{
Projectile = projectile,
ProjectileBodyId = projectileBodyId,
TargetEntityIndex = target.EntityIndex,
TargetBodyId = targetBodyId,
PointX = contact.Point.X,
PointZ = contact.Point.Y,
NormalX = normal.X,
NormalZ = normal.Y
});
}
return new AetheriaRuntimeProjectileStep(survivors, hits);
}
private static string ProjectileBodyId(string? projectileId) =>
ProjectileBodyPrefix + (projectileId ?? "");
private static string EntityBodyId(int entityIndex) =>
EntityBodyPrefix + entityIndex;
private static float ResolveEntityRadius(AetheriaRuntimeEntitySnapshotCommit entity) =>
string.Equals(entity.Kind, "station", StringComparison.OrdinalIgnoreCase) ? 48.0f : 20.0f;
}

View file

@ -1,5 +1,6 @@
using Aetheria.State;
using Aetheria.State.Documents;
using Aetheria.State.Daemon;
using GameCult.Aetheria.State.Verse;
using GameCult.Caching;
using GameCult.Eve.Surface;
@ -12,6 +13,7 @@ using System.Net.Sockets;
var options = AetheriaDaemonHostOptions.Parse(args);
var startedAtUtc = DateTimeOffset.UtcNow.ToString("O");
var projectilePhysics = new AetheriaYmirProjectilePhysics();
Console.WriteLine($"Aetheria Verse daemon starting: {options.StatePath}");
Console.WriteLine(options.EnableOdinAnnouncements
@ -42,7 +44,7 @@ using var cultMeshRudpHost = StartClientCultMeshHost(node, options, () => latest
using var clientPumpCancellation = new CancellationTokenSource();
var clientPump = RunClientCultMeshPumpAsync(cultMeshRudpHost, clientPumpCancellation.Token);
var nextApiPublicationUtc = DateTimeOffset.UtcNow;
var firstTick = await TickAsync(node, options, latestFrame, buildPublications: true).ConfigureAwait(false);
var firstTick = await TickAsync(node, options, projectilePhysics, latestFrame, buildPublications: true).ConfigureAwait(false);
ThrowIfClientPumpFaulted(clientPump);
latestFrame = firstTick.Frame;
nextApiPublicationUtc = DateTimeOffset.UtcNow.Add(options.ApiPublicationInterval);
@ -79,7 +81,7 @@ while (!stopped.Task.IsCompleted)
}
var buildPublications = DateTimeOffset.UtcNow >= nextApiPublicationUtc;
var tick = await TickAsync(node, options, latestFrame, buildPublications).ConfigureAwait(false);
var tick = await TickAsync(node, options, projectilePhysics, latestFrame, buildPublications).ConfigureAwait(false);
ThrowIfClientPumpFaulted(clientPump);
latestFrame = tick.Frame;
nextTickUtc += options.TickInterval;
@ -114,6 +116,7 @@ static void ThrowIfClientPumpFaulted(Task clientPump)
static async Task<AetheriaRuntimeDaemonTickResult> TickAsync(
AetheriaStateNode node,
AetheriaDaemonHostOptions options,
IAetheriaRuntimeProjectilePhysics projectilePhysics,
AetheriaRuntimeDaemonFrameDocument? currentFrame,
bool buildPublications)
{
@ -181,6 +184,7 @@ static async Task<AetheriaRuntimeDaemonTickResult> TickAsync(
Catalog = node.RuntimeCatalog().Latest(),
RenderSettings = options.RenderSettings,
SimulationSettings = options.SimulationSettings,
ProjectilePhysics = projectilePhysics,
StarbridgeScenario = starbridgeScenario,
StarbridgeSession = starbridgeSession,
BuildPublications = buildPublications,
@ -201,19 +205,6 @@ static async Task<AetheriaRuntimeDaemonTickResult> TickAsync(
$"velocity={entity.VelocityX},{entity.VelocityY}");
}
}
if (options.UseAbstractCombatKernel)
{
AetheriaDaemonCombatKernel.Step(
result.Frame.Run,
result.Intents,
fixedDeltaSeconds,
node.RuntimeCatalog().Latest(),
options.CombatKernelSettings);
result.Frame.Capabilities = (result.Frame.Capabilities ?? Array.Empty<string>())
.Concat(new[] { "aetheria.daemon.abstract_combat_kernel.v1" })
.Distinct(StringComparer.Ordinal)
.ToArray();
}
result.Frame.CumulativeImportedFactIds =
currentFrame?.CumulativeImportedFactIds ?? Array.Empty<string>();
result.Frame.CumulativeRejectedImportedFactIds =
@ -1907,7 +1898,11 @@ static EveAssetCatalogDocument BuildCoreAssetCatalog(
bundle.Uri,
bundle.Hash,
bundle.Size,
$"Assets/Resources/{resourcesPath}.prefab"))
$"Assets/Resources/{resourcesPath}.prefab",
new Dictionary<string, string>(StringComparer.Ordinal)
{
["view.pilot.excludeUnityLayers"] = "14"
}))
.ToArray(),
new Dictionary<string, string>(StringComparer.Ordinal)
{
@ -2893,9 +2888,6 @@ internal sealed class AetheriaDaemonHostOptions
AetheriaRuntimeDaemonRenderSettings.AetheriaDefault;
public AetheriaRuntimeDaemonSimulationSettings SimulationSettings { get; init; } =
AetheriaRuntimeDaemonSimulationSettings.AetheriaDefault;
public AetheriaDaemonCombatKernelSettings CombatKernelSettings { get; init; } =
AetheriaDaemonCombatKernelSettings.Default;
public bool UseAbstractCombatKernel { get; init; }
public bool Once { get; init; }
public static AetheriaDaemonHostOptions Parse(IReadOnlyList<string> args)
@ -2956,7 +2948,6 @@ internal sealed class AetheriaDaemonHostOptions
TickInterval = TimeSpan.FromMilliseconds(intervalMs),
ApiPublicationInterval = TimeSpan.FromMilliseconds(apiPublicationIntervalMs),
FixedDeltaSeconds = fixedDeltaMs / 1000.0,
UseAbstractCombatKernel = HasFlag(args, "--abstract-combat-kernel"),
Once = HasFlag(args, "--once")
};
}