From 4c0ad9c27f9d0ddf5e68f9f6e34100a47521b702 Mon Sep 17 00:00:00 2001 From: Jiesi Luo <512838+luojiesi@users.noreply.github.com> Date: Sat, 14 Nov 2026 22:34:46 -0800 Subject: [PATCH] fix(uxassist): keep stars and planets ahead of hives in auto-cruise escape Weighting every containing obstacle by depth let a hive's 0.5 AU range outweigh the planet right below the mecha, so a take-off under a hive steered back into the ground. Stars and planets keep their priority over hives, as before this branch: while the mecha is inside any star or planet range, hives take no part. Overlapping star and planet ranges are still left together, weighted by depth; their safety margins are equal, so the nearest surface dominates. Inside hive ranges only, the first containing hive is left, as before. Add tools/tests/Test-AutoCruiseAvoidance.ps1: an offline check of the avoidance heading for a plain take-off, a take-off under an overlapping hive range, a take-off from a moon inside its gas giant's range, and a hive on its own. Co-Authored-By: Claude Fable 5.2 --- UXAssist/Patches/PlayerPatch.cs | 38 +++++--- tools/tests/AutoCruiseAvoidanceTests.cs | 110 +++++++++++++++++++++++ tools/tests/Test-AutoCruiseAvoidance.ps1 | 58 ++++++++++++ 3 files changed, 195 insertions(+), 11 deletions(-) create mode 100644 tools/tests/AutoCruiseAvoidanceTests.cs create mode 100644 tools/tests/Test-AutoCruiseAvoidance.ps1 diff --git a/UXAssist/Patches/PlayerPatch.cs b/UXAssist/Patches/PlayerPatch.cs index a584712..57e34c7 100644 --- a/UXAssist/Patches/PlayerPatch.cs +++ b/UXAssist/Patches/PlayerPatch.cs @@ -655,13 +655,18 @@ public class PlayerPatch : PatchImpl VectorLF3 baseDir = toTarget.normalized; - // The mecha can be inside several path radii at once: a moon close to its gas giant puts a mecha - // on the moon inside both, and the giant comes first in the list. Leaving only the first one - // sends a mecha that takes off from the moon's giant-facing side back into the ground, because - // "away from the giant" points down there. Leave all of them together, each weighted by how deep - // the mecha is inside it, so the body right below dominates. - VectorLF3 inward = default; - bool insideObstacle = false; + // The mecha can be inside several path radii at once. Stars and planets keep priority over + // hives: a hive's range is wide enough to cover a whole planet, and leaving the hive first + // can steer a mecha that has just taken off back into the ground. + // Among stars and planets, a moon close to its gas giant puts a mecha on the moon inside both + // ranges, and the giant comes first in the list. Leaving only the giant sends a mecha that + // takes off from the moon's giant-facing side back into the ground, because "away from the + // giant" points down there. Leave all of them together, each weighted by how deep the mecha + // is inside its range: their safety margins are equal, so the nearest surface dominates. + VectorLF3 bodiesInward = default; + VectorLF3 hiveInward = default; + bool insideBody = false; + bool insideHive = false; foreach (Obstacle obstacle in obstacles) { double pathRadius = GetReachablePathRadius(obstacle); @@ -673,12 +678,22 @@ public class PlayerPatch : PatchImpl if (obstacleDistance >= pathRadius) continue; - inward += SafeNorm(toObstacle, baseDir) * (pathRadius - obstacleDistance); - insideObstacle = true; + if (obstacle.HasSurface) + { + bodiesInward += SafeNorm(toObstacle, baseDir) * (pathRadius - obstacleDistance); + insideBody = true; + } + else if (!insideHive) + { + hiveInward = toObstacle; + insideHive = true; + } } - if (insideObstacle) - return ComputeEscapeDirection(currentDirection, inward, baseDir); + if (insideBody) + return ComputeEscapeDirection(currentDirection, bodiesInward, baseDir); + if (insideHive) + return ComputeEscapeDirection(currentDirection, hiveInward, baseDir); foreach (Obstacle obstacle in obstacles) { @@ -1147,6 +1162,7 @@ public class PlayerPatch : PatchImpl { public VectorLF3 Position { get; } = position; public double PathRadius { get; } = radius + safetyMargin; + public bool HasSurface { get; } = radius > 0.0; } #endregion diff --git a/tools/tests/AutoCruiseAvoidanceTests.cs b/tools/tests/AutoCruiseAvoidanceTests.cs new file mode 100644 index 0000000..5dd5ee4 --- /dev/null +++ b/tools/tests/AutoCruiseAvoidanceTests.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Reflection; + +// Checks the auto-cruise obstacle avoidance of a built UXAssist.dll without Unity. The avoidance math and +// its inputs are private to PlayerPatch.AutoNavigation, so they are reached through reflection. +internal static class AutoCruiseAvoidanceTests +{ + private const BindingFlags Members = + BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; + + private const double PlanetRadius = 200.0; + private const double GasGiantRadius = 800.0; + private const double TakeoffAltitude = 49.0; + + private static readonly VectorLF3 Up = new(0.0, 1.0, 0.0); + private static readonly VectorLF3 East = new(1.0, 0.0, 0.0); + private static readonly VectorLF3 North = new(0.0, 0.0, 1.0); + + // A mecha that has just left a planet centred on the origin, still moving along the ground, with a + // destination far away in a direction that no obstacle of these checks blocks. + private static readonly VectorLF3 Mecha = Up * (PlanetRadius + TakeoffAltitude); + private static readonly VectorLF3 Target = North * (100.0 * GalaxyData.AU); + + private static Type _navigation; + private static Type _obstacle; + private static int _checks; + private static int _failures; + + private static void Check(bool condition, string message) + { + _checks++; + if (condition) return; + _failures++; + Console.WriteLine("FAIL " + message); + } + + private static MemberInfo Member(string name) + { + MemberInfo[] members = _navigation.GetMember(name, Members); + if (members.Length != 1) + throw new MissingMemberException($"PlayerPatch.AutoNavigation.{name} was not found; update this check to match the renamed member."); + return members[0]; + } + + private static double Constant(string name) => (double)((FieldInfo)Member(name)).GetRawConstantValue(); + + private static object Obstacle(VectorLF3 position, double radius, double safetyMargin) => + Activator.CreateInstance(_obstacle, Members, null, [position, radius, safetyMargin], null); + + private static VectorLF3 Direction(VectorLF3 heading, params object[] obstacles) + { + ((FieldInfo)Member("_targetUniversePosition")).SetValue(null, Target); + ((FieldInfo)Member("_targetUniverseVelocity")).SetValue(null, default(VectorLF3)); + ((FieldInfo)Member("_targetArriveThresholdRadius")).SetValue(null, 4000.0); + var list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(_obstacle)); + foreach (object obstacle in obstacles) list.Add(obstacle); + return (VectorLF3)((MethodInfo)Member("ComputeDirection")).Invoke(null, [Mecha, heading, list]); + } + + private static int Main(string[] args) + { + if (args.Length != 1) throw new ArgumentException("Usage: AutoCruiseAvoidanceTests "); + Assembly mod = Assembly.LoadFrom(args[0]); + _navigation = mod.GetType("UXAssist.Patches.PlayerPatch+AutoNavigation", true); + _obstacle = mod.GetType("UXAssist.Patches.PlayerPatch+Obstacle", true); + double planetMargin = Constant("PlanetSafeRadius"); + double hiveMargin = Constant("HiveSafeRadius"); + + object planet = Obstacle(default, PlanetRadius, planetMargin); + + Check(VectorLF3.Dot(Direction(East), North) > 0.999, + "With nothing in the way the mecha must head straight for the target."); + + VectorLF3 takeoff = Direction(East, planet); + Check(VectorLF3.Dot(takeoff, Up) > 0.85, + $"Taking off from a planet must climb away from it (heading vs. local up {VectorLF3.Dot(takeoff, Up):0.000})."); + + // A hive's range is wide enough to cover a planet next to its orbit. Here the hive is straight + // overhead, so "away from the hive" points into the ground. + object hive = Obstacle(Mecha + Up * 8000.0, 0.0, hiveMargin); + VectorLF3 underHive = Direction(East, planet, hive); + Check((underHive - takeoff).magnitude < 1e-9, + $"A hive range that overlaps the planet must not change the take-off heading (heading vs. local up {VectorLF3.Dot(underHive, Up):0.000})."); + + // A moon close to its gas giant: on the ground of the moon's giant-facing side the mecha is inside + // the giant's range too, and "away from the giant" points into the ground. The giant is listed + // first, as the planets are in orbit order. + VectorLF3 toGiant = (Up * 0.5 + East * Math.Sqrt(0.75)) * 2520.0; + object giant = Obstacle(Mecha + toGiant, GasGiantRadius, planetMargin); + foreach (VectorLF3 heading in new[] { East, -East, North }) + { + VectorLF3 fromMoon = Direction(heading, giant, planet); + Check(VectorLF3.Dot(fromMoon, Up) > 0.75, + $"Taking off from a moon inside its gas giant's range must climb away from the moon (heading vs. local up {VectorLF3.Dot(fromMoon, Up):0.000})."); + } + + // Away from any surface a hive is still left behind. + object nearbyHive = Obstacle(Mecha + East * 8000.0, 0.0, hiveMargin); + VectorLF3 fromHive = Direction(North, nearbyHive); + Check(VectorLF3.Dot(fromHive, -East) > 0.85, + $"Inside a hive's range alone the mecha must fly away from the hive (heading vs. away {VectorLF3.Dot(fromHive, -East):0.000})."); + + Console.WriteLine(_failures == 0 + ? $"Auto-cruise avoidance checks passed ({_checks} checks)." + : $"Auto-cruise avoidance checks: {_failures} of {_checks} failed."); + return _failures == 0 ? 0 : 1; + } +} diff --git a/tools/tests/Test-AutoCruiseAvoidance.ps1 b/tools/tests/Test-AutoCruiseAvoidance.ps1 new file mode 100644 index 0000000..f2ceb64 --- /dev/null +++ b/tools/tests/Test-AutoCruiseAvoidance.ps1 @@ -0,0 +1,58 @@ +#requires -Version 5.1 +# Runs the auto-cruise obstacle avoidance of a built UXAssist.dll against the original game assemblies, without Unity. +[CmdletBinding()] +param([string]$GameRoot, [string]$UXAssistDll) + +Set-StrictMode -Version Latest +$ErrorActionPreference = 'Stop' +$repoRoot = [IO.Path]::GetFullPath((Join-Path $PSScriptRoot '..\..')) +if (-not $GameRoot) { + $steamRoot = (Get-ItemProperty -LiteralPath 'HKCU:\Software\Valve\Steam' -Name SteamPath).SteamPath + $vdf = Get-Content -LiteralPath (Join-Path $steamRoot 'steamapps\libraryfolders.vdf') -Raw + foreach ($entry in [regex]::Matches($vdf, '"path"\s+"([^"]+)"')) { + $library = $entry.Groups[1].Value.Replace('\\', '\') + $candidate = Join-Path $library 'steamapps\common\Dyson Sphere Program' + if (Test-Path -LiteralPath (Join-Path $candidate 'DSPGAME_Data\Managed\Assembly-CSharp.dll')) { + $GameRoot = $candidate + break + } + } +} +if (-not $GameRoot) { throw 'DSP installation not found. Pass -GameRoot with the installed game directory.' } +$managed = Join-Path ([IO.Path]::GetFullPath($GameRoot)) 'DSPGAME_Data\Managed' +foreach ($name in @('Assembly-CSharp.dll', 'UnityEngine.CoreModule.dll')) { + if (-not (Test-Path -LiteralPath (Join-Path $managed $name))) { throw "Missing original game assembly: $name" } +} +if (-not $UXAssistDll) { + & dotnet build (Join-Path $repoRoot 'UXAssist\UXAssist.csproj') -c Release --no-restore -v quiet -nologo + if ($LASTEXITCODE -ne 0) { throw "UXAssist build failed (exit $LASTEXITCODE)." } + $UXAssistDll = Join-Path $repoRoot 'UXAssist\bin\Release\net472\UXAssist.dll' +} +$modDll = (Resolve-Path -LiteralPath $UXAssistDll).Path +$testRoot = Join-Path $repoRoot 'UXAssist\obj\auto-cruise-tests' +$null = [IO.Directory]::CreateDirectory($testRoot) +# Isolate the temporary console project from the mod's SDK props, packages, and packaging targets. +Set-Content -LiteralPath (Join-Path $testRoot 'Directory.Build.props') -Value '' -Encoding UTF8 +Set-Content -LiteralPath (Join-Path $testRoot 'Directory.Build.targets') -Value '' -Encoding UTF8 +$tests = [Security.SecurityElement]::Escape((Join-Path $PSScriptRoot 'AutoCruiseAvoidanceTests.cs')) +$originalDll = [Security.SecurityElement]::Escape((Join-Path $managed 'Assembly-CSharp.dll')) +$unityDll = [Security.SecurityElement]::Escape((Join-Path $managed 'UnityEngine.CoreModule.dll')) +$project = Join-Path $testRoot 'AutoCruiseTests.csproj' +Set-Content -LiteralPath $project -Encoding UTF8 -Value @" + + + Exe + net8.0 + latest + false + true + + + + + + + +"@ +& dotnet run --project $project -c Release -- $modDll +if ($LASTEXITCODE -ne 0) { throw "Auto-cruise avoidance checks failed (exit $LASTEXITCODE)." }