diff --git a/UXAssist/CHANGELOG.md b/UXAssist/CHANGELOG.md index bd2f927..55e9ac9 100644 --- a/UXAssist/CHANGELOG.md +++ b/UXAssist/CHANGELOG.md @@ -3,6 +3,10 @@ ## Changlog +* 1.5.4 + * `Auto-contruct`: + * Add a UI option to hide it completely. + * Fix an issue that may cause circling. * 1.5.3 * New feature: `Auto-contruct` * Fly to buildings to be contructed automatically. @@ -377,6 +381,10 @@ ## 更新日志 +* 1.5.4 + * `自动建造`: + * 增加了一个 UI 选项,可完全隐藏该功能。 + * 修复了可能导致盘旋的问题。 * 1.5.3 * 新功能:`自动建造` * 自动飞行到待建造的建筑处进行建造。 diff --git a/UXAssist/Functions/UIFunctions.cs b/UXAssist/Functions/UIFunctions.cs index 357ed62..b8ed631 100644 --- a/UXAssist/Functions/UIFunctions.cs +++ b/UXAssist/Functions/UIFunctions.cs @@ -271,7 +271,7 @@ public static class UIFunctions { if (ToggleAutoConstruct == null) return; var localPlanet = GameMain.localPlanet; - var active = localPlanet != null && localPlanet.factoryLoaded && localPlanet.factory.prebuildCount > 0; + var active = localPlanet != null && localPlanet.factoryLoaded && localPlanet.factory.prebuildCount > 0 && Patches.FactoryPatch.AutoConstructButtonEnabled.Value; ToggleAutoConstruct.gameObject.SetActive(active); ConstructCountPanel.gameObject.SetActive(active); } diff --git a/UXAssist/Patches/FactoryPatch.cs b/UXAssist/Patches/FactoryPatch.cs index 293bcf6..d370d13 100644 --- a/UXAssist/Patches/FactoryPatch.cs +++ b/UXAssist/Patches/FactoryPatch.cs @@ -32,6 +32,7 @@ public class FactoryPatch : PatchImpl public static ConfigEntry DoNotRenderEntitiesEnabled; public static ConfigEntry DragBuildPowerPolesEnabled; public static ConfigEntry DragBuildPowerPolesAlternatelyEnabled; + public static ConfigEntry AutoConstructButtonEnabled; public static ConfigEntry AutoConstructEnabled; public static ConfigEntry BeltSignalsForBuyOutEnabled; public static ConfigEntry TankFastFillInAndTakeOutEnabled; @@ -124,6 +125,7 @@ public class FactoryPatch : PatchImpl DoNotRenderEntitiesEnabled.SettingChanged += (_, _) => DoNotRenderEntities.Enable(DoNotRenderEntitiesEnabled.Value); DragBuildPowerPolesEnabled.SettingChanged += (_, _) => DragBuildPowerPoles.Enable(DragBuildPowerPolesEnabled.Value); DragBuildPowerPolesAlternatelyEnabled.SettingChanged += (_, _) => DragBuildPowerPoles.AlternatelyChanged(); + AutoConstructButtonEnabled.SettingChanged += (_, _) => AutoConstructButton.Enable(AutoConstructButtonEnabled.Value); AutoConstructEnabled.SettingChanged += (_, _) => Functions.UIFunctions.UpdateToggleAutoConstructCheckButtonVisiblility(); BeltSignalsForBuyOutEnabled.SettingChanged += (_, _) => BeltSignalsForBuyOut.Enable(BeltSignalsForBuyOutEnabled.Value); TankFastFillInAndTakeOutEnabled.SettingChanged += (_, _) => TankFastFillInAndTakeOut.Enable(TankFastFillInAndTakeOutEnabled.Value); @@ -154,6 +156,7 @@ public class FactoryPatch : PatchImpl ProtectVeinsFromExhaustion.Enable(ProtectVeinsFromExhaustionEnabled.Value); DoNotRenderEntities.Enable(DoNotRenderEntitiesEnabled.Value); DragBuildPowerPoles.Enable(DragBuildPowerPolesEnabled.Value); + AutoConstructButton.Enable(AutoConstructButtonEnabled.Value); BeltSignalsForBuyOut.Enable(BeltSignalsForBuyOutEnabled.Value); TankFastFillInAndTakeOut.Enable(TankFastFillInAndTakeOutEnabled.Value); TweakBuildingBuffer.Enable(TweakBuildingBufferEnabled.Value); @@ -171,6 +174,7 @@ public class FactoryPatch : PatchImpl TweakBuildingBuffer.Enable(false); TankFastFillInAndTakeOut.Enable(false); BeltSignalsForBuyOut.Enable(false); + AutoConstructButton.Enable(false); DragBuildPowerPoles.Enable(false); DoNotRenderEntities.Enable(false); ProtectVeinsFromExhaustion.Enable(false); @@ -276,78 +280,94 @@ public class FactoryPatch : PatchImpl return matcher.InstructionEnumeration(); } - #region Auto Construct - private static int _lastPrebuildCount = -1; - - [HarmonyPostfix] - [HarmonyPatch(typeof(PlanetData), nameof(PlanetData.NotifyFactoryLoaded))] - private static void PlanetData_NotifyFactoryLoaded_Postfix() + public class AutoConstructButton : PatchImpl { - Functions.UIFunctions.UpdateToggleAutoConstructCheckButtonVisiblility(); - _lastPrebuildCount = -1; - } + private static int _lastPrebuildCount = -1; - [HarmonyPostfix] - [HarmonyPatch(typeof(PlanetData), nameof(PlanetData.UnloadFactory))] - private static void PlanetData_UnloadFactory_Postfix() - { - Functions.UIFunctions.UpdateToggleAutoConstructCheckButtonVisiblility(); - _lastPrebuildCount = -1; - } + protected override void OnEnable() + { + Functions.UIFunctions.UpdateToggleAutoConstructCheckButtonVisiblility(); + } - [HarmonyPostfix] - [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; - var planet = GameMain.localPlanet; - if (planet == null || !planet.factoryLoaded) return; - var factory = planet.factory; - var prebuildCount = factory.prebuildCount; - if (_lastPrebuildCount != prebuildCount) + protected override void OnDisable() { - if (_lastPrebuildCount <= 0 || prebuildCount == 0) - { - Functions.UIFunctions.UpdateToggleAutoConstructCheckButtonVisiblility(); - } - _lastPrebuildCount = prebuildCount; - Functions.UIFunctions.UpdateConstructCountText(prebuildCount); + Functions.UIFunctions.UpdateToggleAutoConstructCheckButtonVisiblility(); + _lastPrebuildCount = -1; } - if (prebuildCount <= 0) return; - if (!AutoConstructEnabled.Value) return; - var player = __instance.player; - if (prebuildCount <= player.mecha.constructionModule.buildTargetTotalCount) return; - if (player.orders.orderCount > 0) return; - var prebuilds = factory.prebuildPool; - var minDist = float.MaxValue; - var minIndex = 0; - var playerPos = player.position; - for (var i = factory.prebuildCursor - 1; i > 0; i--) + + [HarmonyPostfix] + [HarmonyPatch(typeof(PlanetData), nameof(PlanetData.NotifyFactoryLoaded))] + private static void PlanetData_NotifyFactoryLoaded_Postfix() { - ref var prebuild = ref prebuilds[i]; - if (prebuild.id != i || prebuild.isDestroyed) continue; - if (prebuild.itemRequired > 0) - { - if (player.package.GetItemCount(prebuild.protoId) < prebuild.itemRequired) continue; - } - var dist = (prebuild.pos - playerPos).sqrMagnitude; - if (dist < minDist) - { - minDist = dist; - minIndex = i; - } + Functions.UIFunctions.UpdateToggleAutoConstructCheckButtonVisiblility(); + _lastPrebuildCount = -1; } - if (minIndex == 0) return; - var diff = prebuilds[minIndex].pos - playerPos; - if (diff.sqrMagnitude < 400f) return; - if (player.movementState == EMovementState.Walk && player.mecha.thrusterLevel >= 1) + + [HarmonyPostfix] + [HarmonyPatch(typeof(PlanetData), nameof(PlanetData.UnloadFactory))] + private static void PlanetData_UnloadFactory_Postfix() { - player.controller.actionWalk.SwitchToFly(); - return; + Functions.UIFunctions.UpdateToggleAutoConstructCheckButtonVisiblility(); + _lastPrebuildCount = -1; + } + + [HarmonyPostfix] + [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; + var planet = GameMain.localPlanet; + if (planet == null || !planet.factoryLoaded) return; + var factory = planet.factory; + var prebuildCount = factory.prebuildCount; + if (_lastPrebuildCount != prebuildCount) + { + if (_lastPrebuildCount <= 0 || prebuildCount == 0) + { + Functions.UIFunctions.UpdateToggleAutoConstructCheckButtonVisiblility(); + } + _lastPrebuildCount = prebuildCount; + Functions.UIFunctions.UpdateConstructCountText(prebuildCount); + } + if (prebuildCount <= 0) return; + if (!AutoConstructEnabled.Value) 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) + { + return; + } + var prebuilds = factory.prebuildPool; + var minDist = float.MaxValue; + var minIndex = 0; + var playerPos = player.position; + 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 (player.package.GetItemCount(prebuild.protoId) < prebuild.itemRequired) continue; + } + var dist = (prebuild.pos - playerPos).sqrMagnitude; + if (dist < minDist) + { + minDist = dist; + minIndex = 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) + { + player.controller.actionWalk.SwitchToFly(); + return; + } + player.Order(OrderNode.MoveTo(prebuilds[minIndex].pos + diff.normalized * 6f), false); } - player.Order(OrderNode.MoveTo(prebuilds[minIndex].pos + diff.normalized * 4f), false); } - #endregion public class NightLight : PatchImpl { diff --git a/UXAssist/UIConfigWindow.cs b/UXAssist/UIConfigWindow.cs index 8d160e0..5de0e90 100644 --- a/UXAssist/UIConfigWindow.cs +++ b/UXAssist/UIConfigWindow.cs @@ -71,6 +71,7 @@ public static class UIConfigWindow I18N.Add("Do not render factory entities", "Do not render factory entities (except belts and sorters)", "不渲染工厂建筑实体(除了传送带和分拣器)"); I18N.Add("Drag building power poles in maximum connection range", "Drag building power poles in maximum connection range", "拖动建造电线杆时自动使用最大连接距离间隔"); I18N.Add("Build Tesla Tower and Wireless Power Tower alternately", "Build Tesla Tower and Wireless Power Tower alternately", "交替建造电力感应塔和无线输电塔"); + I18N.Add("Auto-construct button", "Auto-construct button", "自动建造按钮"); I18N.Add("Belt signals for buy out dark fog items automatically", "Belt signals for buy out dark fog items automatically", "用于自动购买黑雾物品的传送带信号"); I18N.Add("Ctrl+Shift+Click to pick items from whole belts", "Ctrl+Shift+Click to pick items from whole belts", "按住Ctrl+Shift点击从整条传送带抓取物品"); I18N.Add("Include branches of belts", "Include branches of belts", "包含传送带分支"); @@ -411,6 +412,9 @@ public static class UIConfigWindow } } + y += 36f; + wnd.AddCheckBox(x, y, tab2, FactoryPatch.AutoConstructButtonEnabled, "Auto-construct button"); + { y += 36f; wnd.AddCheckBox(x, y, tab2, FactoryPatch.PressShiftToTakeWholeBeltItemsEnabled, "Ctrl+Shift+Click to pick items from whole belts"); diff --git a/UXAssist/UXAssist.cs b/UXAssist/UXAssist.cs index cddd415..659366a 100644 --- a/UXAssist/UXAssist.cs +++ b/UXAssist/UXAssist.cs @@ -130,6 +130,8 @@ public class UXAssist : BaseUnityPlugin, IModCanSave "Drag building power poles in maximum connection range"); FactoryPatch.DragBuildPowerPolesAlternatelyEnabled = Config.Bind("Factory", "DragBuildPowerPolesAlternately", true, "Build Tesla Tower and Wireless Power Tower alternately"); + FactoryPatch.AutoConstructButtonEnabled = Config.Bind("Factory", "AutoConstructButton", false, + "Enable auto-construct button"); FactoryPatch.AutoConstructEnabled = Config.Bind("Factory", "AutoConstruct", false, "Fly toward the building to be constructed automatically"); FactoryPatch.BeltSignalsForBuyOutEnabled = Config.Bind("Factory", "BeltSignalsForBuyOut", false,