diff --git a/BetterGenshinImpact/Core/Config/AllConfig.cs b/BetterGenshinImpact/Core/Config/AllConfig.cs
index 18f0d611..d231bfa7 100644
--- a/BetterGenshinImpact/Core/Config/AllConfig.cs
+++ b/BetterGenshinImpact/Core/Config/AllConfig.cs
@@ -1,7 +1,6 @@
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.GameTask.AutoCook;
using BetterGenshinImpact.GameTask.AutoDomain;
-using BetterGenshinImpact.GameTask.AutoEat;
using BetterGenshinImpact.GameTask.AutoFight;
using BetterGenshinImpact.GameTask.AutoFishing;
using BetterGenshinImpact.GameTask.AutoGeniusInvokation;
@@ -102,11 +101,6 @@ public partial class AllConfig : ObservableObject
///
public AutoCookConfig AutoCookConfig { get; set; } = new();
- ///
- /// 自动吃药配置
- ///
- public AutoEatConfig AutoEatConfig{ get; set; } = new();
-
///
/// 自动打牌配置
///
@@ -166,7 +160,6 @@ public partial class AllConfig : ObservableObject
AutoWoodConfig.PropertyChanged += OnAnyPropertyChanged;
AutoFightConfig.PropertyChanged += OnAnyPropertyChanged;
AutoDomainConfig.PropertyChanged += OnAnyPropertyChanged;
- AutoEatConfig.PropertyChanged += OnAnyPropertyChanged;
}
public void OnAnyPropertyChanged(object? sender, EventArgs args)
diff --git a/BetterGenshinImpact/GameTask/AutoDomain/AutoDomainConfig.cs b/BetterGenshinImpact/GameTask/AutoDomain/AutoDomainConfig.cs
index 07c3f1b5..344513bf 100644
--- a/BetterGenshinImpact/GameTask/AutoDomain/AutoDomainConfig.cs
+++ b/BetterGenshinImpact/GameTask/AutoDomain/AutoDomainConfig.cs
@@ -6,7 +6,6 @@ namespace BetterGenshinImpact.GameTask.AutoDomain;
[Serializable]
public partial class AutoDomainConfig : ObservableObject
{
-
///
/// 战斗结束后延迟几秒再开始寻找石化古树,秒
///
@@ -26,4 +25,10 @@ public partial class AutoDomainConfig : ObservableObject
/// 寻找古树时,短距离移动的次数
///
[ObservableProperty] private int _leftRightMoveTimes = 3;
-}
\ No newline at end of file
+
+ ///
+ /// 自动吃药
+ ///
+ [ObservableProperty]
+ private bool _autoEat = false;
+}
diff --git a/BetterGenshinImpact/GameTask/AutoDomain/AutoDomainTask.cs b/BetterGenshinImpact/GameTask/AutoDomain/AutoDomainTask.cs
index 252de922..bc6d83c8 100644
--- a/BetterGenshinImpact/GameTask/AutoDomain/AutoDomainTask.cs
+++ b/BetterGenshinImpact/GameTask/AutoDomain/AutoDomainTask.cs
@@ -2,7 +2,6 @@
using BetterGenshinImpact.Core.Recognition.OCR;
using BetterGenshinImpact.Core.Recognition.ONNX;
using BetterGenshinImpact.Core.Simulator;
-using BetterGenshinImpact.GameTask.AutoEat;
using BetterGenshinImpact.GameTask.AutoFight;
using BetterGenshinImpact.GameTask.AutoFight.Assets;
using BetterGenshinImpact.GameTask.AutoFight.Model;
@@ -26,6 +25,7 @@ using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
+using Vanara.PInvoke;
using static BetterGenshinImpact.GameTask.Common.TaskControl;
using static Vanara.PInvoke.User32;
@@ -338,31 +338,14 @@ public class AutoDomainTask
}
}, cts.Token);
- // 视角操作
-
// 对局结束检测
var domainEndTask = DomainEndDetectionTask(cts);
- // TODO 自动吃药
- if (GameTaskManager.TriggerDictionary != null && GameTaskManager.TriggerDictionary.TryGetValue("autoEat", out var trigger))
- {
- var autoEatTrigger = trigger as AutoEatTrigger;
- if (autoEatTrigger != null && autoEatTrigger.IsEnabled == true)
- {
- var autoEatTask = new Task(() =>
- {
- while (!cts.Token.IsCancellationRequested)
- {
- autoEatTrigger.start();
- // 适当的延迟,防止高 CPU 占用
- Task.Delay(autoEatTrigger.IntervalMs).Wait();
- }
- });
- autoEatTask.Start();
- }
- }
+ // 自动吃药
+ var autoEatRecoveryHpTask = AutoEatRecoveryHpTask(cts);
combatTask.Start();
domainEndTask.Start();
- return Task.WhenAll(combatTask, domainEndTask);
+ autoEatRecoveryHpTask.Start();
+ return Task.WhenAll(combatTask, domainEndTask, autoEatRecoveryHpTask);
}
private void EndFightWait()
@@ -385,19 +368,25 @@ public class AutoDomainTask
///
private Task DomainEndDetectionTask(CancellationTokenSource cts)
{
- return new Task(() =>
+ return new Task(async () =>
{
- while (!_taskParam.Cts.Token.IsCancellationRequested)
+ try
{
- if (IsDomainEnd())
+ while (!_taskParam.Cts.Token.IsCancellationRequested)
{
- cts.Cancel();
- break;
- }
+ if (IsDomainEnd())
+ {
+ await cts.CancelAsync();
+ break;
+ }
- Sleep(1000);
+ await Delay(1000, cts);
+ }
}
- });
+ catch
+ {
+ }
+ }, cts.Token);
}
private bool IsDomainEnd()
@@ -423,6 +412,48 @@ public class AutoDomainTask
return false;
}
+ private Task AutoEatRecoveryHpTask(CancellationTokenSource cts)
+ {
+ return new Task(async () =>
+ {
+ if (!_config.AutoEat)
+ {
+ return;
+ }
+
+ try
+ {
+ while (!_taskParam.Cts.Token.IsCancellationRequested)
+ {
+ if (IsLowHealth())
+ {
+ // 模拟按键 "Z"
+ Simulation.SendInput.Keyboard.KeyPress(User32.VK.VK_Z);
+ Logger.LogInformation("检测到红血,按Z吃药");
+ // TODO 吃饱了会一直吃
+ }
+
+ await Delay(500, cts);
+ }
+ }
+ catch
+ {
+ }
+ }, cts.Token);
+ }
+
+ private bool IsLowHealth()
+ {
+ // 获取图像
+ using var ra = CaptureToRectArea();
+
+ // 获取 (808, 1010) 位置的像素颜色
+ var pixelColor = ra.SrcMat.At(1010, 808);
+
+ // 判断颜色是否是 (255, 90, 90)
+ return pixelColor is { Item2: 255, Item1: 90, Item0: 90 };
+ }
+
///
/// 旋转视角后寻找石化古树
///
diff --git a/BetterGenshinImpact/GameTask/AutoEat/AutoEatConfig.cs b/BetterGenshinImpact/GameTask/AutoEat/AutoEatConfig.cs
deleted file mode 100644
index b47dc8de..00000000
--- a/BetterGenshinImpact/GameTask/AutoEat/AutoEatConfig.cs
+++ /dev/null
@@ -1,26 +0,0 @@
-using CommunityToolkit.Mvvm.ComponentModel;
-using System;
-using System.Collections.Generic;
-using System.Text;
-
-namespace BetterGenshinImpact.GameTask.AutoEat;
-
- ///
- ///自动吃加血药配置
- ///
- [Serializable]
-public partial class AutoEatConfig : ObservableObject
-{
- ///
- /// 触发器是否启用
- ///
- [ObservableProperty]
- private bool _enabled = false;
-
- ///
- /// 触发器触发间隔
- ///
- [ObservableProperty]
- private int _intervalMs = 500;
-}
-
diff --git a/BetterGenshinImpact/GameTask/AutoEat/AutoEatTrigger.cs b/BetterGenshinImpact/GameTask/AutoEat/AutoEatTrigger.cs
deleted file mode 100644
index 91d37502..00000000
--- a/BetterGenshinImpact/GameTask/AutoEat/AutoEatTrigger.cs
+++ /dev/null
@@ -1,81 +0,0 @@
-using BetterGenshinImpact.Core.Simulator;
-using BetterGenshinImpact.GameTask.AutoPick;
-using Microsoft.Extensions.Logging;
-using System;
-using System.Collections.Generic;
-using System.Text;
-using System.Windows.Forms;
-using Vanara.PInvoke;
-using static System.Net.Mime.MediaTypeNames;
-
-namespace BetterGenshinImpact.GameTask.AutoEat;
-
-public class AutoEatTrigger : ITaskTrigger
-{
- public string Name => "自动吃药";
-
- public bool IsEnabled { get; set; }
-
- public int Priority => 10;
-
- public bool IsExclusive { get; set; }
-
- private int _frameIndex = 0;
-
- private User32.VK _pickVk = User32.VK.VK_Z;
- private readonly ILogger _logger = App.GetLogger();
-
- private DateTime _lastExecutionTime = DateTime.MinValue; // 记录上次执行的时间
- public int IntervalMs = 500; // 500ms 的执行间隔
-
- public void Init()
- {
- IsEnabled = TaskContext.Instance().Config.AutoEatConfig.Enabled;
- IntervalMs = TaskContext.Instance().Config.AutoEatConfig.IntervalMs;
- IsExclusive = false;
- }
-
- public void OnCapture(CaptureContent content)
- {
- var now = DateTime.Now;
- // 判断是否已经超过了 500ms 的间隔
- if ((now - _lastExecutionTime).TotalMilliseconds < IntervalMs)
- {
- return;
- }
- // 获取位图对象
- var bitmap = content.SrcBitmap;
-
- // 获取 (808, 1010) 位置的像素颜色
- var pixelColor = bitmap.GetPixel(808, 1010);
-
- // 判断颜色是否是 (255, 90, 90)
- if (pixelColor.R == 255 && pixelColor.G == 90 && pixelColor.B == 90)
- {
- // 模拟按键 "Z"
- Simulation.SendInput.Keyboard.KeyPress(_pickVk);
- _logger.LogInformation("按Z吃药");
- _lastExecutionTime = now;
- // TODO 吃饱了会一直吃
- }
- else
- {
- // _logger.LogInformation("识别的颜色 R:{R} G:{G} B:{B}",pixelColor.R, pixelColor.G, pixelColor.B);
- }
- }
-
- public void start()
- {
- // 帧序号自增 1分钟后归零(MaxFrameIndexSecond)
- _frameIndex = (_frameIndex + 1) % (int)(CaptureContent.MaxFrameIndexSecond * 1000d / 50);
- var bitmap = TaskTriggerDispatcher.GlobalGameCapture.Capture();
- if (bitmap == null)
- {
- _logger.LogWarning("截图失败!");
- return;
- }
- // 循环执行所有触发器 有独占状态的触发器的时候只执行独占触发器
- var content = new CaptureContent(bitmap, _frameIndex, 50);
- OnCapture(content);
- }
-}
diff --git a/BetterGenshinImpact/GameTask/GameTaskManager.cs b/BetterGenshinImpact/GameTask/GameTaskManager.cs
index 415800b7..93814954 100644
--- a/BetterGenshinImpact/GameTask/GameTaskManager.cs
+++ b/BetterGenshinImpact/GameTask/GameTaskManager.cs
@@ -44,7 +44,6 @@ internal class GameTaskManager
TriggerDictionary.TryAdd("AutoSkip", new AutoSkip.AutoSkipTrigger());
TriggerDictionary.TryAdd("AutoFish", new AutoFishing.AutoFishingTrigger());
TriggerDictionary.TryAdd("AutoCook", new AutoCook.AutoCookTrigger());
- TriggerDictionary.TryAdd("AutoEat", new AutoEat.AutoEatTrigger());
return ConvertToTriggerList();
}
@@ -107,7 +106,6 @@ internal class GameTaskManager
TriggerDictionary.GetValueOrDefault("QuickTeleport")?.Init();
TriggerDictionary.GetValueOrDefault("GameLoading")?.Init();
TriggerDictionary.GetValueOrDefault("AutoCook")?.Init();
- TriggerDictionary.GetValueOrDefault("AutoEat")?.Init();
// 清理画布
WeakReferenceMessenger.Default.Send(new PropertyChangedMessage