Skip to content

Commit

Permalink
修复有时理智OCR不正确的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Rcmcpe committed Jul 12, 2020
1 parent 3816a3b commit e463503
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
46 changes: 14 additions & 32 deletions Arknights Automation Library/Interactor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,20 @@ public void Clk(Point point)

public Sanity GetCurrentSanity()
{
return Ocr(GetCurrentSanityPart, @"(\d+)\s*\/\s*(\d+)", (string[] arr, out Sanity result) =>
{
int[] ints = arr.SelectCanParse<string, int>(int.TryParse).ToArray();
bool valid = ints.Length == 2;
result = valid ? new Sanity(ints[0], ints[1]) : default;
return valid;
}, TimeSpan.FromSeconds(1));
return Ocr(scrn => ScreenArea.CurrentSanity.Apply(scrn), @"(\d+)\s*\/\s*(\d+)",
(string[] arr, out Sanity result) =>
{
int[] numbers = arr.SelectCanParse<string, int>(int.TryParse).ToArray();
bool valid = numbers.Length == 2;
result = valid ? new Sanity(numbers[0], numbers[1]) : default;
return valid;
});
}

public int GetRequiredSanity()
{
return Ocr(GetRequiredSanityPart, @"\d+",
(string[] arr, out int result) => int.TryParse(arr[0], out result), TimeSpan.FromSeconds(1));
return Ocr(scrn => ScreenArea.RequiredSanity.Apply(scrn), @"\d+",
(string[] arr, out int result) => int.TryParse(arr[0], out result));
}

public LocateResult Loc(string expr)
Expand Down Expand Up @@ -103,36 +104,17 @@ public bool TestAp(string expr)
return Loc(expr).Succeed;
}

public void WaitAp(string expr, double durationSec = 3)
public void WaitAp(string expr, double waitSec = 3)
{
X.While(() => Loc(expr), result => result.Succeed, TimeSpan.FromSeconds(durationSec));
X.While(() => Loc(expr), result => result.Succeed, TimeSpan.FromSeconds(waitSec));
}

private Mat Asset(string expr)
{
return _assets.Get(expr);
}

private static Mat GetCurrentSanityPart(Mat super)
{
var sanityRect = new Rect((int) (super.Width * 0.88), (int) (super.Height * 0.02),
(int) (super.Width * 0.1), (int) (super.Height * 0.08));
return GetPart(super, sanityRect);
}

private static Mat GetPart(Mat super, Rect rect)
{
return super.Clone(rect);
}

private static Mat GetRequiredSanityPart(Mat super)
{
var sanityRect = new Rect((int) (super.Width * 0.927), (int) (super.Height * 0.941),
(int) (super.Width * 0.035), (int) (super.Height * 0.035));
return GetPart(super, sanityRect);
}

private T Ocr<T>(Func<Mat, Mat> src, string regex, TryParser<string[], T> tryParser, TimeSpan waitSpan)
private T Ocr<T>(Func<Mat, Mat> src, string regex, TryParser<string[], T> tryParser, double waitSec = 1)
{
return X.While(() =>
{
Expand All @@ -145,7 +127,7 @@ private T Ocr<T>(Func<Mat, Mat> src, string regex, TryParser<string[], T> tryPar
T result = default;
return (match.Success && tryParser(match.Groups.Values.Select(it => it.Value).ToArray(), out result),
result);
}, waitSpan);
}, TimeSpan.FromSeconds(waitSec));
}

private static Point Randomize(Point point)
Expand Down
5 changes: 2 additions & 3 deletions Arknights Automation Library/RepeatLevelAction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading;

namespace REVUnit.AutoArknights.Core
{
Expand Down Expand Up @@ -77,7 +76,7 @@ private void RunOnce()
_ia.Clk("作战 开始");
_ia.Clk("作战 确认");
_ia.WaitAp("作战 完成");
_ia.Slp(2);
_ia.Slp(3);
_ia.Clk(5, 5);
}

Expand Down Expand Up @@ -122,7 +121,7 @@ private void WaitForSanityRecovery()
{
Log.Info("正在等待理智恢复...", withTime: true);
while (_ia.GetCurrentSanity().Value < _ia.GetRequiredSanity())
Thread.Sleep(TimeSpan.FromSeconds(10));
_ia.Slp(5);
Log.Info("...理智恢复完成", withTime: true);
}

Expand Down
20 changes: 20 additions & 0 deletions Arknights Automation Library/ScreenArea.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using OpenCvSharp;

namespace REVUnit.AutoArknights.Core
{
public static class ScreenArea
{
public static Rect2f CurrentSanity = new Rect2f(0.8776f, 0.025f, 0.1057f, 0.062f);
public static Rect2f RequiredSanity = new Rect2f(0.9271f, 0.9426f, 0.0323f, 0.0296f);

public static Mat Apply(this Rect2f area, Mat super)
{
int w = super.Width;
int h = super.Height;
return super.Clone(new Rect((int) Math.Round(w * area.X), (int) Math.Round(h * area.Y),
(int) Math.Round(w * area.Width),
(int) Math.Round(h * area.Height)));
}
}
}

0 comments on commit e463503

Please sign in to comment.