Move projectile physics behind daemon Ymir authority
This commit is contained in:
parent
88e77eecf0
commit
fe0c193724
12 changed files with 424 additions and 404 deletions
|
|
@ -16,10 +16,13 @@ namespace GameCult.Aetheria.State.Verse
|
|||
AetheriaRuntimeRunCheckpointCommit run,
|
||||
AetheriaRuntimeDaemonIntentState intents,
|
||||
double deltaSeconds,
|
||||
AetheriaRuntimeDaemonSimulationSettings settings)
|
||||
AetheriaRuntimeDaemonSimulationSettings settings,
|
||||
IAetheriaRuntimeProjectilePhysics projectilePhysics)
|
||||
{
|
||||
if (run == null || deltaSeconds <= 0)
|
||||
return;
|
||||
if (projectilePhysics == null)
|
||||
throw new ArgumentNullException(nameof(projectilePhysics));
|
||||
|
||||
foreach (var zone in run.Zones ?? Array.Empty<AetheriaRuntimeZoneSnapshotCommit>())
|
||||
{
|
||||
|
|
@ -37,7 +40,7 @@ namespace GameCult.Aetheria.State.Verse
|
|||
StepRaiderAi(entities);
|
||||
StepTargetPursuit(entities, settings);
|
||||
StepMovement(entities, deltaSeconds);
|
||||
StepCombat(run, zone, entities, intents, deltaSeconds, settings);
|
||||
StepCombat(run, zone, entities, intents, deltaSeconds, settings, projectilePhysics);
|
||||
StepTractorSalvage(run, zone, entities, settings);
|
||||
RefreshContacts(entities, settings);
|
||||
}
|
||||
|
|
@ -175,7 +178,8 @@ namespace GameCult.Aetheria.State.Verse
|
|||
IReadOnlyList<AetheriaRuntimeEntitySnapshotCommit> entities,
|
||||
AetheriaRuntimeDaemonIntentState intents,
|
||||
double deltaSeconds,
|
||||
AetheriaRuntimeDaemonSimulationSettings settings)
|
||||
AetheriaRuntimeDaemonSimulationSettings settings,
|
||||
IAetheriaRuntimeProjectilePhysics projectilePhysics)
|
||||
{
|
||||
var byIndex = entities.ToDictionary(entity => entity.EntityIndex);
|
||||
foreach (var attacker in entities)
|
||||
|
|
@ -214,7 +218,12 @@ namespace GameCult.Aetheria.State.Verse
|
|||
SetStat(attacker, Heat, Math.Min(100, GetStat(attacker, Heat) + ResolveProjectileDamage(attacker, settings) * settings.ProjectileHeatScale));
|
||||
}
|
||||
|
||||
var projectileStep = AetheriaRuntimeYmirProjectilePhysics.Step(zone, entities, deltaSeconds);
|
||||
PrepareProjectiles(zone, byIndex, deltaSeconds);
|
||||
var projectileStep = zone.Projectiles.Count == 0
|
||||
? new AetheriaRuntimeProjectileStep(
|
||||
Array.Empty<AetheriaRuntimeProjectileCommit>(),
|
||||
Array.Empty<AetheriaRuntimeProjectileHit>())
|
||||
: projectilePhysics.Step(zone, entities, deltaSeconds);
|
||||
zone.Projectiles = projectileStep.Projectiles;
|
||||
foreach (var hit in projectileStep.Hits)
|
||||
{
|
||||
|
|
@ -235,6 +244,56 @@ namespace GameCult.Aetheria.State.Verse
|
|||
}
|
||||
}
|
||||
|
||||
private static void PrepareProjectiles(
|
||||
AetheriaRuntimeZoneSnapshotCommit zone,
|
||||
IReadOnlyDictionary<int, AetheriaRuntimeEntitySnapshotCommit> entities,
|
||||
double deltaSeconds)
|
||||
{
|
||||
var active = new List<AetheriaRuntimeProjectileCommit>();
|
||||
foreach (var projectile in zone.Projectiles ?? Array.Empty<AetheriaRuntimeProjectileCommit>())
|
||||
{
|
||||
if (projectile == null || !projectile.Active)
|
||||
continue;
|
||||
|
||||
projectile.AgeSeconds += deltaSeconds;
|
||||
if (projectile.AgeSeconds >= projectile.LifetimeSeconds)
|
||||
continue;
|
||||
|
||||
if (projectile.Guided &&
|
||||
projectile.TargetEntityIndex >= 0 &&
|
||||
entities.TryGetValue(projectile.TargetEntityIndex, out var target) &&
|
||||
IsAlive(target))
|
||||
{
|
||||
GuideProjectile(projectile, target);
|
||||
}
|
||||
|
||||
active.Add(projectile);
|
||||
}
|
||||
zone.Projectiles = active;
|
||||
}
|
||||
|
||||
private static void GuideProjectile(
|
||||
AetheriaRuntimeProjectileCommit projectile,
|
||||
AetheriaRuntimeEntitySnapshotCommit target)
|
||||
{
|
||||
var speed = Math.Sqrt(
|
||||
projectile.VelocityX * projectile.VelocityX +
|
||||
projectile.VelocityY * projectile.VelocityY);
|
||||
if (speed <= 0.0001)
|
||||
return;
|
||||
|
||||
var x = target.PositionX - projectile.PositionX;
|
||||
var y = target.PositionZ - projectile.PositionZ;
|
||||
var magnitude = Math.Sqrt(x * x + y * y);
|
||||
if (magnitude <= 0.0001)
|
||||
return;
|
||||
|
||||
projectile.DirectionX = x / magnitude;
|
||||
projectile.DirectionY = y / magnitude;
|
||||
projectile.VelocityX = projectile.DirectionX * speed;
|
||||
projectile.VelocityY = projectile.DirectionY * speed;
|
||||
}
|
||||
|
||||
private static bool WantsFire(
|
||||
AetheriaRuntimeRunCheckpointCommit run,
|
||||
AetheriaRuntimeZoneSnapshotCommit zone,
|
||||
|
|
|
|||
|
|
@ -34,6 +34,8 @@ namespace GameCult.Aetheria.State.Verse
|
|||
AetheriaRuntimeDaemonRenderSettings.AetheriaDefault;
|
||||
public AetheriaRuntimeDaemonSimulationSettings SimulationSettings { get; set; } =
|
||||
AetheriaRuntimeDaemonSimulationSettings.AetheriaDefault;
|
||||
public IAetheriaRuntimeProjectilePhysics ProjectilePhysics { get; set; } =
|
||||
AetheriaRuntimeProjectilePhysicsUnavailable.Instance;
|
||||
public bool BuildPublications { get; set; } = true;
|
||||
}
|
||||
|
||||
|
|
@ -135,7 +137,8 @@ namespace GameCult.Aetheria.State.Verse
|
|||
operationResult.Run,
|
||||
operationResult.Intents,
|
||||
options.FixedDeltaSeconds,
|
||||
options.SimulationSettings);
|
||||
options.SimulationSettings,
|
||||
options.ProjectilePhysics);
|
||||
StampZoneSimulationTime(operationResult.Run, options.SimulationTimeSeconds);
|
||||
|
||||
var frame = AetheriaRuntimeDaemonFrameDocument.Create(
|
||||
|
|
|
|||
|
|
@ -0,0 +1,73 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace GameCult.Aetheria.State.Verse
|
||||
{
|
||||
public interface IAetheriaRuntimeProjectilePhysics
|
||||
{
|
||||
string AuthorityId { get; }
|
||||
|
||||
AetheriaRuntimeProjectileStep Step(
|
||||
AetheriaRuntimeZoneSnapshotCommit zone,
|
||||
IReadOnlyList<AetheriaRuntimeEntitySnapshotCommit> entities,
|
||||
double deltaSeconds);
|
||||
}
|
||||
|
||||
public sealed class AetheriaRuntimeProjectilePhysicsUnavailable : IAetheriaRuntimeProjectilePhysics
|
||||
{
|
||||
public static AetheriaRuntimeProjectilePhysicsUnavailable Instance { get; } =
|
||||
new AetheriaRuntimeProjectilePhysicsUnavailable();
|
||||
|
||||
private AetheriaRuntimeProjectilePhysicsUnavailable()
|
||||
{
|
||||
}
|
||||
|
||||
public string AuthorityId => "unavailable";
|
||||
|
||||
public AetheriaRuntimeProjectileStep Step(
|
||||
AetheriaRuntimeZoneSnapshotCommit zone,
|
||||
IReadOnlyList<AetheriaRuntimeEntitySnapshotCommit> entities,
|
||||
double deltaSeconds)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Aetheria projectile simulation requires an injected authoritative physics owner.");
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class AetheriaRuntimeProjectileStep
|
||||
{
|
||||
public AetheriaRuntimeProjectileStep(
|
||||
IReadOnlyList<AetheriaRuntimeProjectileCommit> projectiles,
|
||||
IReadOnlyList<AetheriaRuntimeProjectileHit> hits)
|
||||
{
|
||||
Projectiles = projectiles ?? Array.Empty<AetheriaRuntimeProjectileCommit>();
|
||||
Hits = hits ?? Array.Empty<AetheriaRuntimeProjectileHit>();
|
||||
}
|
||||
|
||||
public IReadOnlyList<AetheriaRuntimeProjectileCommit> Projectiles { get; }
|
||||
|
||||
public IReadOnlyList<AetheriaRuntimeProjectileHit> Hits { get; }
|
||||
}
|
||||
|
||||
public sealed class AetheriaRuntimeProjectileHit
|
||||
{
|
||||
public AetheriaRuntimeProjectileCommit Projectile { get; set; } =
|
||||
new AetheriaRuntimeProjectileCommit();
|
||||
|
||||
public string ProjectileBodyId { get; set; } = "";
|
||||
|
||||
public int TargetEntityIndex { get; set; }
|
||||
|
||||
public string TargetBodyId { get; set; } = "";
|
||||
|
||||
public double PointX { get; set; }
|
||||
|
||||
public double PointZ { get; set; }
|
||||
|
||||
public double NormalX { get; set; }
|
||||
|
||||
public double NormalZ { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5437add74c5d2fb45b81299b41aa999d
|
||||
|
|
@ -1,207 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace GameCult.Aetheria.State.Verse
|
||||
{
|
||||
public static class AetheriaRuntimeYmirProjectilePhysics
|
||||
{
|
||||
private const string ProjectileBodyPrefix = "aetheria.projectile.";
|
||||
private const string DaemonEntityBodyPrefix = "aetheria.daemon.entity.";
|
||||
private const double DefaultEntityPhysicsRadius = 20.0;
|
||||
|
||||
public static AetheriaRuntimeYmirProjectileStep Step(
|
||||
AetheriaRuntimeZoneSnapshotCommit zone,
|
||||
IReadOnlyList<AetheriaRuntimeEntitySnapshotCommit> entities,
|
||||
double deltaSeconds)
|
||||
{
|
||||
if (zone == null || deltaSeconds <= 0)
|
||||
{
|
||||
return new AetheriaRuntimeYmirProjectileStep(
|
||||
zone?.Projectiles ?? Array.Empty<AetheriaRuntimeProjectileCommit>(),
|
||||
Array.Empty<AetheriaRuntimeYmirProjectileHit>());
|
||||
}
|
||||
|
||||
var entityByIndex = (entities ?? Array.Empty<AetheriaRuntimeEntitySnapshotCommit>())
|
||||
.Where(entity => entity != null)
|
||||
.ToDictionary(entity => entity.EntityIndex);
|
||||
var activeProjectiles = new List<AetheriaRuntimeProjectileCommit>();
|
||||
var hits = new List<AetheriaRuntimeYmirProjectileHit>();
|
||||
|
||||
foreach (var projectile in zone.Projectiles ?? Array.Empty<AetheriaRuntimeProjectileCommit>())
|
||||
{
|
||||
if (projectile == null || !projectile.Active)
|
||||
continue;
|
||||
|
||||
projectile.AgeSeconds += deltaSeconds;
|
||||
if (projectile.AgeSeconds >= projectile.LifetimeSeconds)
|
||||
continue;
|
||||
|
||||
AetheriaRuntimeEntitySnapshotCommit? target = null;
|
||||
if (projectile.TargetEntityIndex >= 0 &&
|
||||
entityByIndex.TryGetValue(projectile.TargetEntityIndex, out var resolvedTarget) &&
|
||||
IsActiveBody(resolvedTarget))
|
||||
{
|
||||
target = resolvedTarget;
|
||||
}
|
||||
|
||||
if (projectile.Guided && target != null)
|
||||
GuideProjectile(projectile, target);
|
||||
|
||||
var previousX = projectile.PositionX;
|
||||
var previousZ = projectile.PositionZ;
|
||||
projectile.PositionX += projectile.VelocityX * deltaSeconds;
|
||||
projectile.PositionZ += projectile.VelocityY * deltaSeconds;
|
||||
|
||||
if (target != null &&
|
||||
TryResolveProjectileContact(projectile, previousX, previousZ, target, out var contact))
|
||||
{
|
||||
hits.Add(contact);
|
||||
continue;
|
||||
}
|
||||
|
||||
activeProjectiles.Add(projectile);
|
||||
}
|
||||
|
||||
return new AetheriaRuntimeYmirProjectileStep(
|
||||
activeProjectiles.ToArray(),
|
||||
hits.ToArray());
|
||||
}
|
||||
|
||||
private static void GuideProjectile(
|
||||
AetheriaRuntimeProjectileCommit projectile,
|
||||
AetheriaRuntimeEntitySnapshotCommit target)
|
||||
{
|
||||
var speed = Math.Sqrt(projectile.VelocityX * projectile.VelocityX + projectile.VelocityY * projectile.VelocityY);
|
||||
if (speed <= 0.0001)
|
||||
return;
|
||||
|
||||
var direction = Normalize(target.PositionX - projectile.PositionX, target.PositionZ - projectile.PositionZ);
|
||||
if (Math.Abs(direction.X) + Math.Abs(direction.Y) <= 0.0001)
|
||||
return;
|
||||
|
||||
projectile.DirectionX = direction.X;
|
||||
projectile.DirectionY = direction.Y;
|
||||
projectile.VelocityX = direction.X * speed;
|
||||
projectile.VelocityY = direction.Y * speed;
|
||||
}
|
||||
|
||||
private static bool TryResolveProjectileContact(
|
||||
AetheriaRuntimeProjectileCommit projectile,
|
||||
double previousX,
|
||||
double previousZ,
|
||||
AetheriaRuntimeEntitySnapshotCommit target,
|
||||
out AetheriaRuntimeYmirProjectileHit contact)
|
||||
{
|
||||
var targetRadius = ResolveEntityPhysicsRadius(target);
|
||||
var combinedRadius = Math.Max(0.001, projectile.Radius) + targetRadius;
|
||||
var currentDistanceSq = Sqr(target.PositionX - projectile.PositionX) + Sqr(target.PositionZ - projectile.PositionZ);
|
||||
if (currentDistanceSq <= combinedRadius * combinedRadius)
|
||||
{
|
||||
contact = Contact(projectile, target, projectile.PositionX, projectile.PositionZ);
|
||||
return true;
|
||||
}
|
||||
|
||||
var segmentX = projectile.PositionX - previousX;
|
||||
var segmentZ = projectile.PositionZ - previousZ;
|
||||
var segmentLengthSq = segmentX * segmentX + segmentZ * segmentZ;
|
||||
if (segmentLengthSq <= 0.000001)
|
||||
{
|
||||
contact = default!;
|
||||
return false;
|
||||
}
|
||||
|
||||
var targetX = target.PositionX - previousX;
|
||||
var targetZ = target.PositionZ - previousZ;
|
||||
var t = Math.Max(0, Math.Min(1, (targetX * segmentX + targetZ * segmentZ) / segmentLengthSq));
|
||||
var closestX = previousX + segmentX * t;
|
||||
var closestZ = previousZ + segmentZ * t;
|
||||
var closestDistanceSq = Sqr(target.PositionX - closestX) + Sqr(target.PositionZ - closestZ);
|
||||
if (closestDistanceSq > combinedRadius * combinedRadius)
|
||||
{
|
||||
contact = default!;
|
||||
return false;
|
||||
}
|
||||
|
||||
contact = Contact(projectile, target, closestX, closestZ);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static AetheriaRuntimeYmirProjectileHit Contact(
|
||||
AetheriaRuntimeProjectileCommit projectile,
|
||||
AetheriaRuntimeEntitySnapshotCommit target,
|
||||
double pointX,
|
||||
double pointZ)
|
||||
{
|
||||
var normal = Normalize(pointX - target.PositionX, pointZ - target.PositionZ);
|
||||
return new AetheriaRuntimeYmirProjectileHit
|
||||
{
|
||||
Projectile = projectile,
|
||||
ProjectileBodyId = ProjectileBodyPrefix + (projectile.ProjectileId ?? ""),
|
||||
TargetEntityIndex = target.EntityIndex,
|
||||
TargetBodyId = DaemonEntityBodyPrefix + target.EntityIndex,
|
||||
PointX = pointX,
|
||||
PointZ = pointZ,
|
||||
NormalX = normal.X,
|
||||
NormalZ = normal.Y
|
||||
};
|
||||
}
|
||||
|
||||
private static double ResolveEntityPhysicsRadius(AetheriaRuntimeEntitySnapshotCommit entity)
|
||||
{
|
||||
if (string.Equals(entity.Kind, "station", StringComparison.OrdinalIgnoreCase))
|
||||
return 48.0;
|
||||
|
||||
return DefaultEntityPhysicsRadius;
|
||||
}
|
||||
|
||||
private static bool IsActiveBody(AetheriaRuntimeEntitySnapshotCommit entity)
|
||||
{
|
||||
return entity.IsActive;
|
||||
}
|
||||
|
||||
private static double Sqr(double value) => value * value;
|
||||
|
||||
private static (double X, double Y) Normalize(double x, double y)
|
||||
{
|
||||
var magnitude = Math.Sqrt(x * x + y * y);
|
||||
return magnitude <= 0.0001 ? (0, 0) : (x / magnitude, y / magnitude);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class AetheriaRuntimeYmirProjectileStep
|
||||
{
|
||||
public AetheriaRuntimeYmirProjectileStep(
|
||||
IReadOnlyList<AetheriaRuntimeProjectileCommit> projectiles,
|
||||
IReadOnlyList<AetheriaRuntimeYmirProjectileHit> hits)
|
||||
{
|
||||
Projectiles = projectiles ?? Array.Empty<AetheriaRuntimeProjectileCommit>();
|
||||
Hits = hits ?? Array.Empty<AetheriaRuntimeYmirProjectileHit>();
|
||||
}
|
||||
|
||||
public IReadOnlyList<AetheriaRuntimeProjectileCommit> Projectiles { get; }
|
||||
|
||||
public IReadOnlyList<AetheriaRuntimeYmirProjectileHit> Hits { get; }
|
||||
}
|
||||
|
||||
public sealed class AetheriaRuntimeYmirProjectileHit
|
||||
{
|
||||
public AetheriaRuntimeProjectileCommit Projectile { get; set; } = new AetheriaRuntimeProjectileCommit();
|
||||
|
||||
public string ProjectileBodyId { get; set; } = "";
|
||||
|
||||
public int TargetEntityIndex { get; set; }
|
||||
|
||||
public string TargetBodyId { get; set; } = "";
|
||||
|
||||
public double PointX { get; set; }
|
||||
|
||||
public double PointZ { get; set; }
|
||||
|
||||
public double NormalX { get; set; }
|
||||
|
||||
public double NormalZ { get; set; }
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue