diff --git a/Aetheria.State.Verify/Program.cs b/Aetheria.State.Verify/Program.cs index 773071dcf..2720bbafe 100644 --- a/Aetheria.State.Verify/Program.cs +++ b/Aetheria.State.Verify/Program.cs @@ -7689,6 +7689,7 @@ static void RequireUnityObserverDoesNotTickLocalSimulation(string root) "ApplyTargetCycle(run, command, TargetCycleMode.Nearest)", "ApplyTargetCycle(run, command, TargetCycleMode.Next)", "ApplyTargetCycle(run, command, TargetCycleMode.Previous)", + "ApplyTargetReticle(run, command)", "VisibleHostileTargets(zone, actor, actorIndex)", "actor.Contacts ?? Array.Empty()", "contact.TargetEntityIndex == targetIndex && contact.Visible" @@ -7708,9 +7709,11 @@ static void RequireUnityObserverDoesNotTickLocalSimulation(string root) "RequestTargetNearest()", "RequestTargetNext()", "RequestTargetPrevious()", + "RequestTargetReticle()", "observer.Operations.TargetNearest()", "observer.Operations.TargetNext()", - "observer.Operations.TargetPrevious()" + "observer.Operations.TargetPrevious()", + "observer.Operations.TargetReticle(" }; var unityTargetRequestSources = actionGameManager + "\n" + daemonOperations; var missingUnityTargetRequestSymbols = requiredUnityTargetRequestSymbols @@ -7727,6 +7730,7 @@ static void RequireUnityObserverDoesNotTickLocalSimulation(string root) { ".MaxBy(x => CultMath.math.length(x.CultPosition - CurrentEntity.CultPosition))", ".OrderBy(x => CultMath.math.length(x.CultPosition - CurrentEntity.CultPosition))", + ".MaxBy(x => CultMath.math.dot(", "Array.IndexOf(targets" }; var unityTargetCycleHits = forbiddenUnityTargetCycleSymbols diff --git a/Assets/Scripts/Gameplay/ActionGameManager.cs b/Assets/Scripts/Gameplay/ActionGameManager.cs index bb492d71f..0bd256531 100644 --- a/Assets/Scripts/Gameplay/ActionGameManager.cs +++ b/Assets/Scripts/Gameplay/ActionGameManager.cs @@ -2057,12 +2057,7 @@ public class ActionGameManager : MonoBehaviour Input.Player.TargetReticle.performed += context => { - if (!CurrentEntity.VisibleEnemies.Any()) return; - var underReticle = CurrentEntity.VisibleEnemies.Where(x => x != CurrentEntity) - .MaxBy(x => CultMath.math.dot( - CultMath.math.normalize(x.CultPosition - CurrentEntity.CultPosition), - CurrentEntity.CultLookDirection)); - RequestTargetSelection(CurrentEntity.Target.Value == underReticle ? null : underReticle); + RequestTargetReticle(); }; Input.Player.TargetNearest.performed += context => @@ -3744,6 +3739,11 @@ public class ActionGameManager : MonoBehaviour TryRequestDaemonTargetCycle(AetheriaRuntimeDaemonCommandKinds.TargetPrevious); } + private void RequestTargetReticle() + { + TryRequestDaemonTargetReticle(_viewDirection); + } + private bool TryRequestDaemonTargetSelection(Entity target) { var observer = ResolveDaemonObserver(); @@ -3776,6 +3776,26 @@ public class ActionGameManager : MonoBehaviour } } + private bool TryRequestDaemonTargetReticle(Vector3 lookDirection) + { + var observer = ResolveDaemonObserver(); + if (observer == null || !observer.HasAuthoritativeState) + { + return false; + } + + try + { + observer.Operations.TargetReticle(lookDirection.x, lookDirection.y, lookDirection.z); + return true; + } + catch (Exception ex) + { + Debug.LogWarning($"Failed to send Aetheria daemon reticle target operation; operation not submitted: {ex.Message}"); + return false; + } + } + private bool TryRequestDaemonTargetCycle(AetheriaRuntimeDaemonCommandKinds command) { var observer = ResolveDaemonObserver(); diff --git a/Assets/Scripts/Gameplay/AetheriaDaemonOperations.cs b/Assets/Scripts/Gameplay/AetheriaDaemonOperations.cs index 23fb13527..2c190296b 100644 --- a/Assets/Scripts/Gameplay/AetheriaDaemonOperations.cs +++ b/Assets/Scripts/Gameplay/AetheriaDaemonOperations.cs @@ -35,6 +35,11 @@ public sealed class AetheriaDaemonOperations return Send((client, observed) => client.TargetPrevious(observed)); } + public AetheriaRuntimeDaemonCommandEnvelope TargetReticle(double directionX, double directionY, double directionZ) + { + return Send((client, observed) => client.TargetReticle(observed, directionX, directionY, directionZ)); + } + public AetheriaRuntimeDaemonCommandEnvelope SetMoveVector( double directionX, double directionY, diff --git a/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs b/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs index a6a9823f9..0c8b91999 100644 --- a/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs +++ b/Assets/Scripts/Tests/DaemonRuntimeDocumentTests.cs @@ -1144,6 +1144,63 @@ public class DaemonRuntimeDocumentTests Assert.AreEqual(2, zone.Entities[0].TargetEntityIndex); } + [Test] + public void DaemonOperationsTargetsReticleFromAuthoritativeContacts() + { + var run = RunWithTwoEntities(); + var zone = run.Zones[0]; + zone.Entities[0].PositionX = 0; + zone.Entities[0].PositionY = 0; + zone.Entities[0].PositionZ = 0; + zone.Entities[1].PositionX = 0; + zone.Entities[1].PositionY = 0; + zone.Entities[1].PositionZ = 5; + zone.Entities = zone.Entities + .Concat(new[] + { + new AetheriaRuntimeEntitySnapshotCommit + { + EntityIndex = 2, + Name = "Off Reticle", + PositionX = 5, + PositionY = 0, + PositionZ = 0 + } + }) + .ToArray(); + zone.Entities[0].Contacts = new[] + { + new AetheriaRuntimeEntityContactCommit + { + TargetEntityIndex = 1, + Visible = true, + Hostile = true + }, + new AetheriaRuntimeEntityContactCommit + { + TargetEntityIndex = 2, + Visible = true, + Hostile = true + } + }; + + var command = AetheriaRuntimeDaemonCommandDocument.Create( + AetheriaRuntimeDaemonCommandKinds.TargetReticle, + "codex", + "session-reticle", + 18, + "zone.0.entity.0"); + command.PositionZ = 1; + + var targetResult = AetheriaRuntimeDaemonOperations.Execute(run, new[] { command }); + Assert.AreEqual(1, targetResult.AppliedCommandIds.Count); + Assert.AreEqual(1, zone.Entities[0].TargetEntityIndex); + + var toggleResult = AetheriaRuntimeDaemonOperations.Execute(run, new[] { command }); + Assert.AreEqual(1, toggleResult.AppliedCommandIds.Count); + Assert.AreEqual(-1, zone.Entities[0].TargetEntityIndex); + } + [Test] public void DaemonOperationsMutatesHotEntityControlsInDaemonState() { diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonDocuments.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonDocuments.cs index 807ef858b..e0e2e835f 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonDocuments.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonDocuments.cs @@ -64,6 +64,7 @@ namespace GameCult.Aetheria.State.Verse TargetNearest, TargetNext, TargetPrevious, + TargetReticle, SetMoveVector, SetLookDirection, SetTractorPower, diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationClient.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationClient.cs index b2ed138cc..e58ae64db 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationClient.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationClient.cs @@ -132,6 +132,20 @@ namespace GameCult.Aetheria.State.Verse return Send(AetheriaRuntimeDaemonCommandKinds.TargetPrevious, observed); } + public AetheriaRuntimeDaemonCommandEnvelope TargetReticle( + AetheriaRuntimeObservedDaemonState? observed, + double directionX, + double directionY, + double directionZ) + { + return Send(AetheriaRuntimeDaemonCommandKinds.TargetReticle, observed, command => + { + command.DirectionX = directionX; + command.DirectionY = directionY; + command.PositionZ = directionZ; + }); + } + public AetheriaRuntimeDaemonCommandEnvelope SetMoveVector( AetheriaRuntimeObservedDaemonState? observed, double directionX, diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperations.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperations.cs index 4391d81c0..4cc5920e1 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperations.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperations.cs @@ -85,6 +85,8 @@ namespace GameCult.Aetheria.State.Verse return ApplyTargetCycle(run, command, TargetCycleMode.Next); case AetheriaRuntimeDaemonCommandKinds.TargetPrevious: return ApplyTargetCycle(run, command, TargetCycleMode.Previous); + case AetheriaRuntimeDaemonCommandKinds.TargetReticle: + return ApplyTargetReticle(run, command); case AetheriaRuntimeDaemonCommandKinds.SetLookDirection: return ApplySetLookDirection(run, command); case AetheriaRuntimeDaemonCommandKinds.SetTractorPower: @@ -247,7 +249,57 @@ namespace GameCult.Aetheria.State.Verse return true; } - private static IEnumerable<(int EntityIndex, double DistanceSq)> VisibleHostileTargets( + private static bool ApplyTargetReticle( + AetheriaRuntimeRunCheckpointCommit run, + AetheriaRuntimeDaemonCommandDocument command) + { + var actorKey = string.IsNullOrWhiteSpace(command.ActorEntityKey) + ? run.CurrentEntityKey + : command.ActorEntityKey; + if (!TryResolveEntity(run, actorKey, out var actorZone, out var actorIndex, out var actor)) + return false; + + var zone = (run.Zones ?? Array.Empty()) + .FirstOrDefault(candidate => candidate != null && candidate.ZoneIndex == actorZone); + if (zone == null) + return false; + + var lookLength = Math.Sqrt( + (command.DirectionX * command.DirectionX) + + (command.DirectionY * command.DirectionY) + + (command.PositionZ * command.PositionZ)); + if (lookLength <= double.Epsilon) + return false; + + var lookX = command.DirectionX / lookLength; + var lookY = command.DirectionY / lookLength; + var lookZ = command.PositionZ / lookLength; + var scoredTargets = VisibleHostileTargets(zone, actor, actorIndex) + .Select(candidate => + { + var distance = Math.Sqrt(candidate.DistanceSq); + var dot = distance <= double.Epsilon + ? double.NegativeInfinity + : ((candidate.DeltaX / distance) * lookX) + + ((candidate.DeltaY / distance) * lookY) + + ((candidate.DeltaZ / distance) * lookZ); + return (candidate.EntityIndex, Dot: dot); + }) + .Where(candidate => candidate.Dot > double.NegativeInfinity) + .OrderByDescending(candidate => candidate.Dot) + .ThenBy(candidate => candidate.EntityIndex) + .ToArray(); + if (scoredTargets.Length == 0) + return false; + + var target = scoredTargets[0]; + actor.TargetEntityIndex = actor.TargetEntityIndex == target.EntityIndex + ? -1 + : target.EntityIndex; + return true; + } + + private static IEnumerable<(int EntityIndex, double DistanceSq, double DeltaX, double DeltaY, double DeltaZ)> VisibleHostileTargets( AetheriaRuntimeZoneSnapshotCommit zone, AetheriaRuntimeEntitySnapshotCommit actor, int actorIndex) @@ -271,7 +323,13 @@ namespace GameCult.Aetheria.State.Verse var deltaX = target.PositionX - actor.PositionX; var deltaY = target.PositionY - actor.PositionY; - yield return (contact.TargetEntityIndex, (deltaX * deltaX) + (deltaY * deltaY)); + var deltaZ = target.PositionZ - actor.PositionZ; + yield return ( + contact.TargetEntityIndex, + (deltaX * deltaX) + (deltaY * deltaY) + (deltaZ * deltaZ), + deltaX, + deltaY, + deltaZ); } }