Skip to content

Commit

Permalink
auto expedition: ui
Browse files Browse the repository at this point in the history
  • Loading branch information
huiyadanli committed Nov 5, 2023
1 parent fcc76de commit dbadaa2
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 43 deletions.
2 changes: 1 addition & 1 deletion BetterGenshinImpact/Core/Config/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace BetterGenshinImpact.Core.Config;

public class Global
{
public static string Version = "0.12.0";
public static string Version = "0.13.0";

public static string StartUpPath { get; private set; } = AppContext.BaseDirectory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ public AutoSkipAssets()
RecognitionType = RecognitionTypes.TemplateMatch,
TemplateImageMat = GameTaskManager.LoadAssetImage("AutoSkip", "icon_daily_reward.png"),
RegionOfInterest = new Rect(info.CaptureAreaRect.Width / 2, 0, info.CaptureAreaRect.Width - info.CaptureAreaRect.Width / 2, info.CaptureAreaRect.Height),
DrawOnWindow = true
DrawOnWindow = false
}.InitTemplate();
ExploreIconRo = new RecognitionObject
{
Name = "ExploreIcon",
RecognitionType = RecognitionTypes.TemplateMatch,
TemplateImageMat = GameTaskManager.LoadAssetImage("AutoSkip", "icon_explore.png"),
RegionOfInterest = new Rect(info.CaptureAreaRect.Width / 2, 0, info.CaptureAreaRect.Width - info.CaptureAreaRect.Width / 2, info.CaptureAreaRect.Height),
DrawOnWindow = true
DrawOnWindow = false
}.InitTemplate();

MenuRo = new RecognitionObject
Expand Down
11 changes: 11 additions & 0 deletions BetterGenshinImpact/GameTask/AutoSkip/AutoSkipConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,16 @@ public partial class AutoSkipConfig : ObservableObject
public int ChatOptionTextWidth { get; set; }= 280;

public int ExpeditionOptionTextWidth { get; set; } = 130;


/// <summary>
/// 自动领取每日委托奖励
/// </summary>
[ObservableProperty] private bool _autoGetDailyRewardsEnabled = true;

/// <summary>
/// 自动重新派遣
/// </summary>
[ObservableProperty] private bool _autoReExploreEnabled = false;
}
}
60 changes: 33 additions & 27 deletions BetterGenshinImpact/GameTask/AutoSkip/AutoSkipTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,51 +64,57 @@ public void OnCapture(CaptureContent content)
}

// 领取每日委托奖励
var dailyRewardIconRa = content.CaptureRectArea.Find(_autoSkipAssets.DailyRewardIconRo);
if (!dailyRewardIconRa.IsEmpty())
if (config.AutoGetDailyRewardsEnabled)
{
var text = GetOrangeOptionText(content.CaptureRectArea.SrcMat, dailyRewardIconRa, (int)(config.ChatOptionTextWidth * assetScale));

if (text.Contains("每日委托"))
var dailyRewardIconRa = content.CaptureRectArea.Find(_autoSkipAssets.DailyRewardIconRo);
if (!dailyRewardIconRa.IsEmpty())
{
if (Math.Abs(content.FrameIndex - _prevOtherClickFrameIndex) >= 8)
var text = GetOrangeOptionText(content.CaptureRectArea.SrcMat, dailyRewardIconRa, (int)(config.ChatOptionTextWidth * assetScale));

if (text.Contains("每日委托"))
{
_logger.LogInformation("自动选择:{Text}", text);
if (Math.Abs(content.FrameIndex - _prevOtherClickFrameIndex) >= 8)
{
_logger.LogInformation("自动选择:{Text}", text);
}

dailyRewardIconRa.ClickCenter();
dailyRewardIconRa.Dispose();
return;
}

dailyRewardIconRa.ClickCenter();
_prevOtherClickFrameIndex = content.FrameIndex;
dailyRewardIconRa.Dispose();
return;
}

_prevOtherClickFrameIndex = content.FrameIndex;
dailyRewardIconRa.Dispose();
}

// 领取探索派遣奖励
var exploreIconRa = content.CaptureRectArea.Find(_autoSkipAssets.ExploreIconRo);
if (!exploreIconRa.IsEmpty())
if (config.AutoReExploreEnabled)
{
var text = GetOrangeOptionText(content.CaptureRectArea.SrcMat, exploreIconRa, (int)(config.ExpeditionOptionTextWidth * assetScale));
if (text.Contains("探索派遣"))
var exploreIconRa = content.CaptureRectArea.Find(_autoSkipAssets.ExploreIconRo);
if (!exploreIconRa.IsEmpty())
{
if (Math.Abs(content.FrameIndex - _prevOtherClickFrameIndex) >= 8)
var text = GetOrangeOptionText(content.CaptureRectArea.SrcMat, exploreIconRa, (int)(config.ExpeditionOptionTextWidth * assetScale));
if (text.Contains("探索派遣"))
{
_logger.LogInformation("自动选择:{Text}", text);
if (Math.Abs(content.FrameIndex - _prevOtherClickFrameIndex) >= 8)
{
_logger.LogInformation("自动选择:{Text}", text);
}

exploreIconRa.ClickCenter();

// 等待探索派遣界面打开
Thread.Sleep(1000);
new ExpeditionTask().Run(content);
exploreIconRa.Dispose();
return;
}

exploreIconRa.ClickCenter();

// 等待探索派遣界面打开
Thread.Sleep(1000);
new ExpeditionTask().Run(content);
_prevOtherClickFrameIndex = content.FrameIndex;
exploreIconRa.Dispose();
return;
}

_prevOtherClickFrameIndex = content.FrameIndex;
exploreIconRa.Dispose();
return;
}

// 找右下的对话选项按钮
Expand Down
38 changes: 26 additions & 12 deletions BetterGenshinImpact/GameTask/AutoSkip/ExpeditionTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace BetterGenshinImpact.GameTask.AutoSkip;
/// </summary>
public class ExpeditionTask
{
private static readonly List<string> ExpeditionCharacterList = new() { "菲谢尔", "班尼特" };
private static readonly List<string> ExpeditionCharacterList = new() { "菲谢尔", "班尼特", "夜兰", "申鹤", "久岐忍" };

//private static readonly List<string> GameAreaNames = new() { "蒙德", "璃月", "稻妻", "须弥", "枫丹" };

Expand All @@ -50,15 +50,15 @@ public void Run(CaptureContent content)
}
else
{
TaskControl.Sleep(500);
TaskControl.Sleep(800);
content.CaptureRectArea
.Derive(new Rect((int)(110 * assetScale), (int)((145 + 70 * i) * assetScale),
(int)(60 * assetScale), (int)(33 * assetScale)))
.ClickCenter();
ReExplorationGameArea(content);
}
}

TaskControl.Logger.LogInformation("探索派遣:{Text}", "重新派遣完成");
VisionContext.Instance().DrawContent.ClearAll();
}

Expand All @@ -71,10 +71,12 @@ private void ReExplorationGameArea(CaptureContent content)
{
var result = CaptureAndOcr(content, new Rect(0, 0, captureRect.Width - (int)(480 * assetScale), captureRect.Height));
var rect = result.FindRectByText("探险完成");
// TODO i>1 的时候,可以通过关键词“探索派遣限制 4 / 5 ”判断是否已经派遣完成?
if (rect != Rect.Empty)
{
// 点击探险完成
// 点击探险完成下方的人物头像
content.CaptureRectArea.Derive(new Rect(rect.X, rect.Y + (int)(50 * assetScale), rect.Width, (int)(80 * assetScale))).ClickCenter();
TaskControl.Sleep(500);
// 重新截图 找领取
result = CaptureAndOcr(content);
rect = result.FindRectByText("领取");
Expand Down Expand Up @@ -116,13 +118,18 @@ private void ReExplorationGameArea(CaptureContent content)

private bool SelectCharacter(CaptureContent content)
{
var result = CaptureAndOcr(content);
var captureRect = TaskContext.Instance().SystemInfo.CaptureAreaRect;
var result = CaptureAndOcr(content, new Rect(0, 0, captureRect.Width / 2, captureRect.Height));
if (result.RegionHasText("角色选择"))
{
var cards = GetCharacterCards(result);
if (cards.Count > 0)
{
var card = cards.First(c => c.Idle);
var card = cards.FirstOrDefault(c => c.Idle && c.Name != null && ExpeditionCharacterList.Contains(c.Name));
if (card == null)
{
card = cards.First(c => c.Idle);
}
var rect = card.Rects.First();

using var ra = content.CaptureRectArea.Derive(rect);
Expand Down Expand Up @@ -163,7 +170,7 @@ private List<ExpeditionCharacterCard> GetCharacterCards(PaddleOcrResult result)
foreach (var ocrResultRect2 in ocrResultRects)
{
if (ocrResultRect2.Rect.Y > ocrResultRect.Rect.Y - 50 * assetScale
&& ocrResultRect2.Rect.Y + ocrResultRect2.Rect.Height > ocrResultRect.Rect.Y)
&& ocrResultRect2.Rect.Y + ocrResultRect2.Rect.Height < ocrResultRect.Rect.Y + ocrResultRect.Rect.Height)
{
if (ocrResultRect2.Text.Contains("探险完成"))
{
Expand All @@ -174,16 +181,23 @@ private List<ExpeditionCharacterCard> GetCharacterCards(PaddleOcrResult result)
card.Name = name;
}
}
else
else if (!ocrResultRect2.Text.Contains("时间缩短") && !ocrResultRect2.Text.Contains("奖励增加") && !ocrResultRect2.Text.Contains("暂无加成"))
{
card.Name = ocrResultRect2.Text;
}

card.Rects.Add(ocrResultRect.Rect);
card.Rects.Add(ocrResultRect2.Rect);
}
}

cards.Add(card);
if (!string.IsNullOrEmpty(card.Name))
{
cards.Add(card);
}
else
{
TaskControl.Logger.LogWarning("探索派遣:存在未找到角色命的识别内容");
}
}
}

Expand All @@ -198,7 +212,7 @@ private PaddleOcrResult CaptureAndOcr(CaptureContent content)
using var mat = bitmap.ToMat();
Cv2.CvtColor(mat, mat, ColorConversionCodes.BGR2GRAY);
var result = OcrFactory.Paddle.OcrResult(mat);
VisionContext.Instance().DrawContent.PutOrRemoveRectList("OcrResultRects", result.ToRectDrawableList());
//VisionContext.Instance().DrawContent.PutOrRemoveRectList("OcrResultRects", result.ToRectDrawableList());
return result;
}

Expand All @@ -209,7 +223,7 @@ private PaddleOcrResult CaptureAndOcr(CaptureContent content, Rect rect)
using var mat = new Mat(bitmap.ToMat(), rect);
Cv2.CvtColor(mat, mat, ColorConversionCodes.BGR2GRAY);
var result = OcrFactory.Paddle.OcrResult(mat);
VisionContext.Instance().DrawContent.PutOrRemoveRectList("OcrResultRects", result.ToRectDrawableList(_pen));
//VisionContext.Instance().DrawContent.PutOrRemoveRectList("OcrResultRects", result.ToRectDrawableList(_pen));
return result;
}
}
56 changes: 56 additions & 0 deletions BetterGenshinImpact/View/Pages/TriggerSettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,62 @@
Margin="0,0,36,0"
IsChecked="{Binding Config.AutoSkipConfig.QuicklySkipConversationsEnabled, Mode=TwoWay}" />
</Grid>
<Grid Margin="16">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:TextBlock
Grid.Row="0"
Grid.Column="0"
FontTypography="Body"
TextWrapping="Wrap"
Text="凯瑟琳 - 自动领取『每日委托』奖励" />
<ui:TextBlock
Grid.Row="1"
Grid.Column="0"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
TextWrapping="Wrap"
Text="在与凯瑟琳对话时,会自动“领取『每日委托』奖励" />
<ui:ToggleSwitch
Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="1"
Margin="0,0,36,0"
IsChecked="{Binding Config.AutoSkipConfig.AutoGetDailyRewardsEnabled, Mode=TwoWay}" />
</Grid>
<Grid Margin="16">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:TextBlock
Grid.Row="0"
Grid.Column="0"
FontTypography="Body"
TextWrapping="Wrap"
Text="凯瑟琳 - 自动重新派遣(不稳定...)" />
<ui:TextBlock
Grid.Row="1"
Grid.Column="0"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
TextWrapping="Wrap"
Text="自动领取已完成探索的奖励,并按照配置进行重新派遣" />
<ui:ToggleSwitch
Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="1"
Margin="0,0,36,0"
IsChecked="{Binding Config.AutoSkipConfig.AutoReExploreEnabled, Mode=TwoWay}" />
</Grid>
</StackPanel>
</ui:CardExpander>

Expand Down
2 changes: 1 addition & 1 deletion BetterGenshinImpact/ViewModel/Pages/HomePageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void OnNavigatedFrom()
[RelayCommand]
private void OnTest()
{
var result = OcrFactory.Paddle.OcrResult(new Mat("E:\\HuiTask\\更好的原神\\自动派遣\\Clip_20231030_235235.png", ImreadModes.Grayscale));
var result = OcrFactory.Paddle.OcrResult(new Mat(@"E:\HuiTask\更好的原神\七圣召唤\Clip_20231105_143729.png", ImreadModes.Grayscale));
foreach (var region in result.Regions)
{
Debug.WriteLine($"{region.Text}");
Expand Down

0 comments on commit dbadaa2

Please sign in to comment.