diff --git a/AGENTS.md b/AGENTS.md index 6f52676..b5f8a47 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -51,7 +51,7 @@ DSP_Mods/ | Mod | GUID | Description | |-----|------|-------------| -| **UXAssist** | `org.soardev.uxassist` | Core QoL mod and shared library. Window resize, profile-based saves, FPS control, factory/logistics/navigation/Dyson Sphere tweaks, UI improvements, config panel UI, and `Common/` + `UI/` widget library shared by other mods. The Factory tab's building-buffer controls include a comparison tip with the original game values. The Logistics tab can push auto-config values to all existing facilities of a type on the current planet (per-setting `Apply` and per-category `Apply All` buttons), and can set both product limits of every Orbital Collector across all loaded factories; logic is in `LogisticsPatch.Apply*`/`ForEach*` and wired in `UIConfigWindow`. | +| **UXAssist** | `org.soardev.uxassist` | Core QoL mod and shared library. Window resize, profile-based saves, FPS control, factory/logistics/navigation/Dyson Sphere tweaks, UI improvements, config panel UI, and `Common/` + `UI/` widget library shared by other mods. The Factory tab's building-buffer controls include a comparison tip with the original game values. The Logistics tab can push auto-config values to all existing facilities of a type on the current planet (per-setting `Apply` and per-category `Apply All` buttons), and can set both product limits of every Orbital Collector across all loaded factories; logic is in `LogisticsPatch.Apply*`/`ForEach*` and wired in `UIConfigWindow`. Auto-construct uses forward sphere-cast probing, horizontal footprint-based detours, adaptive flight altitude, and stuck recovery while flying to construction ghosts. | | **CheatEnabler** | `org.soardev.cheatenabler` | Cheat pack (depends on UXAssist). Instant build, architect mode, infinite resources, power boosts, Dyson Sphere cheats with a bounded shell-count input (1–99,999), mecha invincibility, and more. | | **LogisticMiner** | — | Makes logistic stations automatically mine ores and water from the current planet. | | **HideTips** | — | Suppresses all tutorial popups, random tips, achievement/milestone cards, and skips the prologue cutscene. | diff --git a/UXAssist/Patches/Factory/FactoryBuildPatches.cs b/UXAssist/Patches/Factory/FactoryBuildPatches.cs index 23c336b..99b00f3 100644 --- a/UXAssist/Patches/Factory/FactoryBuildPatches.cs +++ b/UXAssist/Patches/Factory/FactoryBuildPatches.cs @@ -11,8 +11,25 @@ internal static class FactoryBuildPatches { internal class AutoConstructPatch : PatchImpl { + private const long AutoConstructTickInterval = 15L; + private const float ConstructionStopMargin = 1.5f; + private const float AvoidanceAltitude = 35f; + private const float AvoidanceClearance = 1.5f; + private const float StuckDistance = 1.5f; + private const int StuckTickThreshold = 45; + + private static int _targetPrebuildId; + private static OrderNode _autoOrder; + private static Vector3 _lastPosition; + private static int _stuckTicks; + private static int _detourCount; + private static bool _isAvoidingObstacle; + private static Vector3 _pendingWaypoint; + private static bool _hasPendingWaypoint; + protected override void OnEnable() { + GameLogicProc.OnGameEnd += ResetNavigation; Functions.UIFunctions.UpdateToggleAutoConstructCheckButtonVisiblility(); // Diagnostic aid for reports of the auto-construct button not showing up: // log the visibility predicate inputs once per enable. @@ -26,6 +43,8 @@ internal static class FactoryBuildPatches protected override void OnDisable() { + GameLogicProc.OnGameEnd -= ResetNavigation; + ResetNavigation(); Functions.UIFunctions.UpdateToggleAutoConstructCheckButtonVisiblility(); } @@ -37,7 +56,7 @@ internal static class FactoryBuildPatches [HarmonyPatch(typeof(PlayerAction_Rts), nameof(PlayerAction_Rts.GameTick))] private static void PlayerAction_Rts_GameTick_Postfix(PlayerAction_Rts __instance, long timei) { - if (timei % 60L != 0) return; + if (timei % AutoConstructTickInterval != 0) return; if (!FactoryPatch.AutoConstructEnabled.Value) return; var planet = GameMain.localPlanet; if (planet == null || !planet.factoryLoaded) return; @@ -45,40 +64,278 @@ internal static class FactoryBuildPatches var prebuildCount = factory.prebuildCount; if (prebuildCount <= 0) return; var player = __instance.player; - if (prebuildCount <= player.mecha.constructionModule.buildTargetTotalCount) return; - if (player.orders.orderCount > 0) return; - if (player.controller.horzVelocity.sqrMagnitude > 0.01f) + if (player.planetData != planet) { + ResetNavigation(); return; } + + if (_targetPrebuildId != 0) + { + if (!TryGetPrebuild(factory, _targetPrebuildId, out var target) || + target.itemRequired > 0 && player.package.GetItemCount(target.protoId) < target.itemRequired) + { + ResetNavigation(); + } + else if (player.orders.orderCount > 0 || + player.orders.currentOrder != null && player.orders.currentOrder != _autoOrder) + { + ResetNavigation(); + } + } + + if (_targetPrebuildId == 0) + { + if (prebuildCount <= player.mecha.constructionModule.buildTargetTotalCount || + player.orders.orderCount > 0 || player.orders.currentOrder != null || + player.controller.horzVelocity.sqrMagnitude > 0.01f) + { + return; + } + + if (!TryFindTarget(factory, player, out _targetPrebuildId)) return; + _lastPosition = player.position; + _stuckTicks = 0; + _detourCount = 0; + _isAvoidingObstacle = false; + } + + if (!TryGetPrebuild(factory, _targetPrebuildId, out var prebuild)) + { + ResetNavigation(); + return; + } + + var targetPosition = prebuild.pos; + var constructionStopDistance = Mathf.Max(2f, player.mecha.buildArea - ConstructionStopMargin); + if ((targetPosition - player.position).sqrMagnitude <= constructionStopDistance * constructionStopDistance) + { + player.ClearOrders(); + ResetNavigation(); + return; + } + + if (player.movementState == EMovementState.Walk && player.mecha.thrusterLevel >= 1) + { + player.controller.actionWalk.SwitchToFly(); + } + + if (_autoOrder != null && player.orders.currentOrder == null) + { + _autoOrder = null; + _stuckTicks = 0; + _lastPosition = player.position; + if (_hasPendingWaypoint) + { + var pendingWaypoint = _pendingWaypoint; + _pendingWaypoint = Vector3.zero; + _hasPendingWaypoint = false; + IssueRoute(player, pendingWaypoint, true); + return; + } + } + + if (_autoOrder == null) + { + IssueRoute(player, targetPosition, false); + return; + } + + if ((player.position - _lastPosition).sqrMagnitude < StuckDistance * StuckDistance) + { + _stuckTicks += (int)AutoConstructTickInterval; + } + else + { + _stuckTicks = 0; + _lastPosition = player.position; + } + + var routeTarget = _autoOrder.target; + var altitude = _isAvoidingObstacle ? AvoidanceAltitude : GetCurrentAltitude(player); + if (_stuckTicks >= StuckTickThreshold || !IsPathClear(player, routeTarget.normalized, altitude)) + { + if (_detourCount < 4 && TryFindDetour(player, routeTarget, altitude, out var detour, out var pendingWaypoint)) + { + _detourCount++; + _isAvoidingObstacle = true; + _pendingWaypoint = pendingWaypoint; + _hasPendingWaypoint = pendingWaypoint.sqrMagnitude > 0.01f; + player.controller.actionFly.targetAltitude = AvoidanceAltitude; + IssueRoute(player, detour, true); + } + else + { + _detourCount = 0; + _isAvoidingObstacle = true; + _pendingWaypoint = Vector3.zero; + _hasPendingWaypoint = false; + player.controller.actionFly.targetAltitude = AvoidanceAltitude; + IssueRoute(player, targetPosition, true); + } + + _stuckTicks = 0; + _lastPosition = player.position; + } + } + + private static bool TryFindTarget(PlanetFactory factory, Player player, out int targetId) + { var prebuilds = factory.prebuildPool; - var minDist = float.MaxValue; - var minIndex = 0; - var playerPos = player.position; + var minDistance = float.MaxValue; + targetId = 0; for (var i = factory.prebuildCursor - 1; i > 0; i--) { ref var prebuild = ref prebuilds[i]; if (prebuild.id != i || prebuild.isDestroyed) continue; - if (prebuild.itemRequired > 0) + if (prebuild.itemRequired > 0 && player.package.GetItemCount(prebuild.protoId) < prebuild.itemRequired) continue; + + var distance = (prebuild.pos - player.position).sqrMagnitude; + if (distance < minDistance) { - if (player.package.GetItemCount(prebuild.protoId) < prebuild.itemRequired) continue; - } - var dist = (prebuild.pos - playerPos).sqrMagnitude; - if (dist < minDist) - { - minDist = dist; - minIndex = i; + minDistance = distance; + targetId = i; } } - if (minIndex == 0) return; - var diff = prebuilds[minIndex].pos - playerPos; - if (diff.sqrMagnitude < 400f) return; - if (player.movementState == EMovementState.Walk && player.mecha.thrusterLevel >= 1) + + return targetId != 0; + } + + private static bool TryGetPrebuild(PlanetFactory factory, int id, out PrebuildData prebuild) + { + if (id > 0 && id < factory.prebuildPool.Length) { - player.controller.actionWalk.SwitchToFly(); - return; + prebuild = factory.prebuildPool[id]; + return prebuild.id == id && !prebuild.isDestroyed; } - player.Order(OrderNode.MoveTo(prebuilds[minIndex].pos + diff.normalized * 6f), false); + + prebuild = default; + return false; + } + + private static void IssueRoute(Player player, Vector3 targetPosition, bool keepAltitude) + { + var direction = targetPosition - player.position; + if (direction.sqrMagnitude < 0.01f) return; + + var target = targetPosition + direction.normalized * 6f; + _autoOrder = OrderNode.MoveTo(target); + player.Order(_autoOrder, false); + if (keepAltitude && player.movementState == EMovementState.Fly) + { + player.controller.actionFly.targetAltitude = AvoidanceAltitude; + } + } + + private static bool TryFindDetour(Player player, Vector3 routeTarget, float altitude, out Vector3 detour, out Vector3 pendingWaypoint) + { + detour = default; + pendingWaypoint = default; + var planet = player.planetData; + if (planet == null) return false; + + var origin = GetFlightPoint(player.position.normalized, altitude, planet.realRadius); + var finalDirection = routeTarget.normalized; + var path = GetFlightPoint(finalDirection, altitude, planet.realRadius) - origin; + if (path.sqrMagnitude < 0.01f) return false; + if (!Physics.SphereCast(origin, AvoidanceClearance, path.normalized, out var hit, path.magnitude, 8704, QueryTriggerInteraction.Collide) || + hit.distance + 4f >= path.magnitude) + { + return false; + } + + var obstacleBounds = hit.collider != null ? hit.collider.bounds : new Bounds(hit.point, Vector3.one * 2f); + var anchor = obstacleBounds.center.normalized; + var forward = Vector3.ProjectOnPlane(finalDirection, anchor).normalized; + if (forward.sqrMagnitude < 0.01f) + { + forward = Vector3.ProjectOnPlane(player.position, anchor).normalized; + } + + var side = Vector3.Cross(anchor, forward).normalized; + if (side.sqrMagnitude < 0.01f) return false; + + var sideExtent = Mathf.Min(GetProjectedExtent(obstacleBounds.extents, side), 48f); + var forwardExtent = Mathf.Min(GetProjectedExtent(obstacleBounds.extents, forward), 48f); + var sideOffset = Mathf.Max(6f, sideExtent + AvoidanceClearance + 2f); + var forwardOffset = Mathf.Max(5f, forwardExtent + AvoidanceClearance + 2f); + var sideOffsets = new[] { sideOffset, -sideOffset, sideOffset + 8f, -sideOffset - 8f, sideOffset + 16f, -sideOffset - 16f }; + foreach (var lateralOffset in sideOffsets) + { + var entry = SurfaceOffset(anchor, side * lateralOffset - forward * forwardOffset, planet.realRadius); + var exit = SurfaceOffset(anchor, side * lateralOffset + forward * forwardOffset, planet.realRadius); + if (IsPathClear(player, entry, altitude) && + IsPathClear(entry, exit, altitude, planet.realRadius) && + IsPathClear(exit, finalDirection, altitude, planet.realRadius)) + { + detour = entry * planet.realRadius; + pendingWaypoint = exit * planet.realRadius; + return true; + } + + var candidate = SurfaceOffset(anchor, side * lateralOffset, planet.realRadius); + if (!IsPathClear(player, candidate, altitude) || + !IsPathClear(candidate, finalDirection, altitude, planet.realRadius)) continue; + + detour = candidate * planet.realRadius; + return true; + } + + return false; + } + + private static float GetProjectedExtent(Vector3 extents, Vector3 axis) + { + return Mathf.Abs(axis.x) * extents.x + Mathf.Abs(axis.y) * extents.y + Mathf.Abs(axis.z) * extents.z; + } + + private static bool IsPathClear(Player player, Vector3 targetDirection, float altitude) + { + var planet = player.planetData; + return planet != null && IsPathClear(player.position.normalized, targetDirection, altitude, planet.realRadius); + } + + private static bool IsPathClear(Vector3 startDirection, Vector3 targetDirection, float altitude, float realRadius) + { + var origin = GetFlightPoint(startDirection, altitude, realRadius); + var target = GetFlightPoint(targetDirection, altitude, realRadius); + var path = target - origin; + if (path.sqrMagnitude < 0.01f) return true; + + return !Physics.SphereCast(origin, AvoidanceClearance, path.normalized, out var hit, path.magnitude, 8704, QueryTriggerInteraction.Collide) || + hit.distance + 4f >= path.magnitude; + } + + private static Vector3 SurfaceOffset(Vector3 surfaceDirection, Vector3 tangentOffset, float realRadius) + { + var distance = tangentOffset.magnitude; + if (distance < 0.01f) return surfaceDirection.normalized; + + var tangent = Vector3.ProjectOnPlane(tangentOffset, surfaceDirection).normalized; + var angle = distance / Mathf.Max(1f, realRadius); + return (surfaceDirection.normalized * Mathf.Cos(angle) + tangent * Mathf.Sin(angle)).normalized; + } + + private static Vector3 GetFlightPoint(Vector3 direction, float altitude, float realRadius) + { + return direction.normalized * (realRadius + Mathf.Max(2f, altitude)); + } + + private static float GetCurrentAltitude(Player player) + { + return Mathf.Clamp(player.position.magnitude - player.planetData.realRadius, 15f, AvoidanceAltitude); + } + + private static void ResetNavigation() + { + _targetPrebuildId = 0; + _autoOrder = null; + _lastPosition = Vector3.zero; + _stuckTicks = 0; + _detourCount = 0; + _isAvoidingObstacle = false; + _pendingWaypoint = Vector3.zero; + _hasPendingWaypoint = false; } }