Skip to content

Commit

Permalink
add GetBigMapZoomLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
huiyadanli committed Dec 28, 2024
1 parent 71f2c2f commit b83017f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 22 deletions.
56 changes: 43 additions & 13 deletions BetterGenshinImpact/GameTask/AutoTrackPath/TpTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class TpTask(CancellationToken ct)
public static double ReviveStatueOfTheSevenPointX = 2296.4;
public static double ReviveStatueOfTheSevenPointY = -824.4;
public static int currentZoomLevel = 4;

/// <summary>
/// 通过大地图传送到指定坐标最近的传送点,然后移动到指定坐标
/// </summary>
Expand All @@ -52,14 +53,13 @@ public class TpTask(CancellationToken ct)
}



// 计算传送点位置离哪个地图切换后的中心点最近,切换到该地图
await SwitchRecentlyCountryMap(x, y, country);

// 计算坐标后点击

var bigMapInAllMapRect = GetBigMapRect();
while (!IsPointInBigMapWindow(bigMapInAllMapRect, x, y) || currentZoomLevel>2) // 左上角 350x400也属于禁止点击区域
while (!IsPointInBigMapWindow(bigMapInAllMapRect, x, y) || currentZoomLevel > 2) // 左上角 350x400也属于禁止点击区域
{
Debug.WriteLine($"({x},{y}) 不在 {bigMapInAllMapRect} 内,继续移动");
Logger.LogInformation("传送点不在当前大地图范围内,继续移动");
Expand Down Expand Up @@ -89,6 +89,7 @@ public class TpTask(CancellationToken ct)
break;
}
}

Logger.LogInformation("传送完成");
return (x, y);
}
Expand Down Expand Up @@ -153,6 +154,7 @@ private bool IsPointInBigMapWindow(Rect bigMapInAllMapRect, double x, double y)
Simulation.SendInput.Keyboard.KeyPress(User32.VK.VK_M);
await Delay(1000, ct);
}

await AdjustMapZoomLevel(initialZoomLevel);
currentZoomLevel = initialZoomLevel;
Logger.LogInformation($"调整缩放等级为{initialZoomLevel},地图移动过程中不要操作鼠标中键。");
Expand Down Expand Up @@ -192,7 +194,7 @@ public async Task MoveMapTo(double x, double y, double tolerance = 100.0, int ma
{
// 获取当前地图中心点并计算到目标传送点的初始偏移
// await AdjustMapZoomLevel(mapZoomLevel);
var bigMapCenterPoint = GetPositionFromBigMap(); // 初始中心
var bigMapCenterPoint = GetPositionFromBigMap(); // 初始中心
var newBigMapCenterPoint = bigMapCenterPoint;
var (xOffset, yOffset) = (x - bigMapCenterPoint.X, y - bigMapCenterPoint.Y);
// double distance = Math.Sqrt(xOffset * xOffset + yOffset * yOffset);
Expand All @@ -214,11 +216,12 @@ public async Task MoveMapTo(double x, double y, double tolerance = 100.0, int ma
catch (Exception)
{
newBigMapCenterPoint = new Point2f(
(float)(bigMapCenterPoint.X + xOffset * moveMouseX / totalMoveMouseX),
(float)(bigMapCenterPoint.Y + yOffset * moveMouseY / totalMoveMouseY)
);
(float)(bigMapCenterPoint.X + xOffset * moveMouseX / totalMoveMouseX),
(float)(bigMapCenterPoint.Y + yOffset * moveMouseY / totalMoveMouseY)
);
// 利用移动鼠标的距离获取新的中心
}

// 本次移动的距离
double diffMapX = Math.Abs(newBigMapCenterPoint.X - bigMapCenterPoint.X);
double diffMapY = Math.Abs(newBigMapCenterPoint.Y - bigMapCenterPoint.Y);
Expand All @@ -240,8 +243,10 @@ public async Task MoveMapTo(double x, double y, double tolerance = 100.0, int ma
Debug.WriteLine($"在 {iteration} 迭代后,已经接近目标点,不再进一步调整。");
break;
}

while (mouseDistance < 200 && currentZoomLevel > 2)
{ // 放大地图
{
// 放大地图
await AdjustMapZoomLevel(true);
totalMoveMouseX *= (currentZoomLevel) / (currentZoomLevel - 1);
totalMoveMouseY *= (currentZoomLevel) / (currentZoomLevel - 1);
Expand Down Expand Up @@ -278,16 +283,19 @@ public async Task AdjustMapZoomLevel(bool zoomIn)
{
GameCaptureRegion.GameRegionClick((rect, scale) => (50 * scale, 650 * scale));
}

await Delay(50, ct);
}

public async Task AdjustMapZoomLevel(int zoomLevel)
{
for (int i = 0; i < 5; i++)
{
await AdjustMapZoomLevel(false);
}

await Delay(200, ct);
for (int i = 0; i < 6-zoomLevel; i++)
for (int i = 0; i < 6 - zoomLevel; i++)
{
await AdjustMapZoomLevel(true);
}
Expand Down Expand Up @@ -330,19 +338,19 @@ public async Task MouseMoveMap(int pixelDeltaX, int pixelDeltaY, int steps = 10,
// 随机起点以避免地图移动无效
GameCaptureRegion.GameRegionMove((rect, _) =>
(rect.Width / 2d + Random.Shared.Next(-rect.Width / 6, rect.Width / 6),
rect.Height / 2d + Random.Shared.Next(-rect.Height / 6, rect.Height / 6)));
rect.Height / 2d + Random.Shared.Next(-rect.Height / 6, rect.Height / 6)));

Simulation.SendInput.Mouse.LeftButtonDown();
for (var i = 0; i < steps; i++)
{
Simulation.SendInput.Mouse.MoveMouseBy(stepX[i], stepY[i]);
await Delay(stepIntervalMilliseconds, ct);
}

Simulation.SendInput.Mouse.LeftButtonUp();
}



public Point2f GetPositionFromBigMap()
{
return GetBigMapCenterPoint();
Expand Down Expand Up @@ -440,6 +448,7 @@ public Point2f GetBigMapCenterPoint()
country = tpPosition.Country;
}
}

// todo: 识别当前国家
return (recentX, recentY, country);
}
Expand Down Expand Up @@ -469,6 +478,7 @@ public async Task<bool> SwitchRecentlyCountryMap(double x, double y, string? for
return false;
}
}

string minCountry = "当前位置";
foreach (var (country, position) in MapLazyAssets.Instance.CountryPositions)
{
Expand All @@ -483,10 +493,11 @@ public async Task<bool> SwitchRecentlyCountryMap(double x, double y, string? for
Logger.LogDebug("离目标传送点最近的区域是:{Country}", minCountry);
if (minCountry != "当前位置")
{
if(forceCountry != null)
if (forceCountry != null)
{
minCountry = forceCountry;
}

GameCaptureRegion.GameRegionClick((rect, scale) => (rect.Width - 160 * scale, rect.Height - 60 * scale));
await Delay(300, ct);
var ra = CaptureToRectArea();
Expand All @@ -505,7 +516,6 @@ public async Task<bool> SwitchRecentlyCountryMap(double x, double y, string? for
}



public async Task Tp(string name)
{
// 通过大地图传送到指定传送点
Expand Down Expand Up @@ -610,6 +620,26 @@ private bool CheckMapChooseIcon(ImageRegion imageRegion)
break;
}
}

return hasMapChooseIcon;
}
}

/// <summary>
/// 给定的映射关系可以表示成 (x, y) 对的形式,其中 x 是输入值,y 是输出值
/// 1 - 1
/// 0.8 - 2
/// 0.6 - 3
/// 0.4 - 4
/// 0.2 - 5
/// 0 - 6
/// y=−5x+6
/// </summary>
/// <param name="region"></param>
/// <returns></returns>
private int GetBigMapZoomLevel(ImageRegion region)
{
var s = Bv.GetBigMapScale(region);
// 1~6 的缩放等级
return (int)(-5 * s) + 6;
}
}
20 changes: 18 additions & 2 deletions BetterGenshinImpact/GameTask/Common/BgiVision/BvStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static async Task<bool> WaitForMainUi(CancellationToken ct, int retryTime

return false;
}

/// <summary>
/// 在任意可以关闭的UI界面(识别关闭按钮)
/// </summary>
Expand Down Expand Up @@ -111,6 +111,22 @@ public static bool BigMapIsUnderground(ImageRegion captureRa)
return captureRa.Find(QuickTeleportAssets.Instance.MapUndergroundSwitchButtonRo).IsExist();
}

public static double GetBigMapScale(ImageRegion region)
{
var scaleRa = region.Find(QuickTeleportAssets.Instance.MapScaleButtonRo);
if (scaleRa.IsEmpty())
{
throw new Exception("当前未处于大地图界面,不能使用GetBigMapScale方法");
}

// 452 ~ 627 间隔 35 和截图有关的,截图高24
var start = QuickTeleportAssets.MapScaleButton1080StartY;
var end = QuickTeleportAssets.MapScaleButton1080EndY;
var cur = (scaleRa.Y + scaleRa.Height / 2.0) * TaskContext.Instance().SystemInfo.ZoomOutMax1080PRatio; // 转换到1080p坐标系,主要是小于1080p的情况

return (end * 1.0 - cur) / (end - start);
}

public static MotionStatus GetMotionStatus(ImageRegion captureRa)
{
var spaceExist = captureRa.Find(ElementAssets.Instance.SpaceKey).IsExist();
Expand Down Expand Up @@ -224,4 +240,4 @@ public enum MotionStatus
Normal, // 正常
Fly, // 飞行
Climb, // 攀爬
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class QuickTeleportAssets : BaseAssets<QuickTeleportAssets>

public RecognitionObject MapUndergroundSwitchButtonRo;
public RecognitionObject MapUndergroundToGroundButtonRo;

public const int MapScaleButton1080StartY = 452;
public const int MapScaleButton1080EndY = 627;

private QuickTeleportAssets()
{
Expand Down
17 changes: 10 additions & 7 deletions BetterGenshinImpact/ViewModel/Pages/HotKeyPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System.Threading;
using System.Threading.Tasks;
using BetterGenshinImpact.GameTask.Model.Area;
using BetterGenshinImpact.GameTask.QuickTeleport.Assets;
using Vanara.PInvoke;
using HotKeySettingModel = BetterGenshinImpact.Model.HotKeySettingModel;

Expand Down Expand Up @@ -581,13 +582,15 @@ private void BuildHotKeySettingModelList()
// 拾取物品
// Task.Run(async () => { await new ScanPickTask().Start(new CancellationToken()); });

Simulation.SendInput.Keyboard.KeyDown(false, User32.VK.VK_LMENU);
// TaskContext.Instance().PostMessageSimulator.KeyDown(User32.VK.VK_MENU);
Thread.Sleep(500);
GameCaptureRegion.GameRegion1080PPosMove(200, 100);
Thread.Sleep(500);
// TaskContext.Instance().PostMessageSimulator.KeyUp(User32.VK.VK_MENU);
Simulation.SendInput.Keyboard.KeyUp(false, User32.VK.VK_LMENU);
// Simulation.SendInput.Keyboard.KeyDown(false, User32.VK.VK_LMENU);
// // TaskContext.Instance().PostMessageSimulator.KeyDown(User32.VK.VK_MENU);
// Thread.Sleep(500);
// GameCaptureRegion.GameRegion1080PPosMove(200, 100);
// Thread.Sleep(500);
// // TaskContext.Instance().PostMessageSimulator.KeyUp(User32.VK.VK_MENU);
// Simulation.SendInput.Keyboard.KeyUp(false, User32.VK.VK_LMENU);

TaskControl.Logger.LogInformation("大地图界面缩放按钮位置:{Position}", Bv.GetBigMapScale( TaskControl.CaptureToRectArea()));
}
));
debugDirectory.Children.Add(new HotKeySettingModel(
Expand Down

0 comments on commit b83017f

Please sign in to comment.