Merge remote-tracking branch 'origin/main'

This commit is contained in:
辉鸭蛋 2024-12-27 00:58:33 +08:00
commit 1214ca2d65
2 changed files with 54 additions and 42 deletions

View File

@ -67,6 +67,10 @@ public class AutoFightTask : ISoloTask
TimeSpan fightTimeout = TimeSpan.FromSeconds(120); // 默认战斗超时时间 TimeSpan fightTimeout = TimeSpan.FromSeconds(120); // 默认战斗超时时间
Stopwatch stopwatch = Stopwatch.StartNew(); Stopwatch stopwatch = Stopwatch.StartNew();
//战斗前检查,可做成配置
/* if (await CheckFightFinish()) {
return;
}*/
// 战斗操作 // 战斗操作
var fightTask = Task.Run(async () => var fightTask = Task.Run(async () =>
{ {
@ -147,44 +151,47 @@ public class AutoFightTask : ISoloTask
private async Task<bool> CheckFightFinish() private async Task<bool> CheckFightFinish()
{ {
// YOLO 判断血条和怪物位置 // YOLO 判断血条和怪物位置
if (HasFightFlagByYolo(CaptureToRectArea())) // if (HasFightFlagByYolo(CaptureToRectArea()))
{ // {
_lastFightFlagTime = DateTime.Now; // _lastFightFlagTime = DateTime.Now;
return false; // return false;
} // }
//
//Random random = new Random();
Random random = new Random(); //double randomFraction = random.NextDouble(); // 生成 0 到 1 之间的随机小数
double randomFraction = random.NextDouble(); // 生成 0 到 1 之间的随机小数
//此处随机数防止固定招式下使按L正好处于招式下导致无法准确判断战斗结束 //此处随机数防止固定招式下使按L正好处于招式下导致无法准确判断战斗结束
double randomNumber = 1 + (randomFraction * (3 - 1)); // double randomNumber = 1 + (randomFraction * (3 - 1));
// 几秒内没有检测到血条和怪物位置,则开始旋转视角重新检测 // 几秒内没有检测到血条和怪物位置,则开始旋转视角重新检测
if ((DateTime.Now - _lastFightFlagTime).TotalSeconds > randomNumber) //if ((DateTime.Now - _lastFightFlagTime).TotalSeconds > randomNumber)
//{
// 旋转完毕后都没有检测到血条和怪物位置则按L键确认战斗结束
/**
Simulation.SendInput.Mouse.MiddleButtonClick();
await Delay(300, _ct);
for (var i = 0; i < 8; i++)
{ {
// 旋转完毕后都没有检测到血条和怪物位置则按L键确认战斗结束 Simulation.SendInput.Mouse.MoveMouseBy((int)(500 * _dpi), 0);
/** await Delay(800, _ct); // 等待视角稳定
Simulation.SendInput.Mouse.MiddleButtonClick(); if (HasFightFlagByYolo(CaptureToRectArea()))
await Delay(300, _ct);
for (var i = 0; i < 8; i++)
{ {
Simulation.SendInput.Mouse.MoveMouseBy((int)(500 * _dpi), 0); _lastFightFlagTime = DateTime.Now;
await Delay(800, _ct); // 等待视角稳定 return false;
if (HasFightFlagByYolo(CaptureToRectArea()))
{
_lastFightFlagTime = DateTime.Now;
return false;
}
} }
**/ }
await Delay(1000, _ct); **/
//检查延时,根据队伍不同可以进行优化,可做成配置
await Delay(1500, _ct);
Logger.LogInformation("按了L"); Logger.LogInformation("按了L");
// 最终方案确认战斗结束 // 最终方案确认战斗结束
Simulation.SendInput.Keyboard.KeyPress(User32.VK.VK_L); Simulation.SendInput.Keyboard.KeyPress(User32.VK.VK_L);
await Delay(450, _ct); await Delay(450, _ct);
var ra = CaptureToRectArea(); var ra = CaptureToRectArea();
var b3 = ra.SrcMat.At<Vec3b>(50, 790); var b3 = ra.SrcMat.At<Vec3b>(50, 790);
if (b3.Equals(new Vec3b(95, 235, 255)))
int tolerance = 6;
if (Math.Abs(b3.Item0 - 95)< tolerance&& Math.Abs(b3.Item1 - 235) < tolerance&& Math.Abs(b3.Item2 - 255) < tolerance)
{ {
Logger.LogInformation("识别到战斗结束"); Logger.LogInformation("识别到战斗结束");
Simulation.SendInput.Keyboard.KeyPress(User32.VK.VK_SPACE); Simulation.SendInput.Keyboard.KeyPress(User32.VK.VK_SPACE);
@ -192,10 +199,11 @@ public class AutoFightTask : ISoloTask
} }
else else
{ {
Logger.LogInformation($"未识别到战斗结束{b3.Item0},{b3.Item1},{b3.Item2}");
_lastFightFlagTime = DateTime.Now; _lastFightFlagTime = DateTime.Now;
return false; return false;
} }
} // }
return false; return false;
} }

View File

@ -196,23 +196,36 @@ public class TpTask(CancellationToken ct)
int moveMouseX = 100 * Math.Sign(xOffset); int moveMouseX = 100 * Math.Sign(xOffset);
int moveMouseY = 100 * Math.Sign(yOffset); int moveMouseY = 100 * Math.Sign(yOffset);
int moveSteps = 10; int moveSteps = 10;
double totalMoveMouseX = Double.MaxValue;
double totalMoveMouseY = Double.MaxValue;
for (int iteration = 0; iteration < maxIterations; iteration++) for (int iteration = 0; iteration < maxIterations; iteration++)
{ {
// 移动鼠标 // 移动鼠标
await MouseMoveMap(moveMouseX, moveMouseY, moveSteps); await MouseMoveMap(moveMouseX, moveMouseY, moveSteps);
bigMapCenterPoint = newBigMapCenterPoint; // 保存上一次移动的数据 bigMapCenterPoint = newBigMapCenterPoint; // 保存上一次移动的数据
newBigMapCenterPoint = GetPositionFromBigMap(); // 随循环更新的地图中心 try
{
newBigMapCenterPoint = GetPositionFromBigMap(); // 随循环更新的地图中心
}
catch (Exception)
{
newBigMapCenterPoint = new Point2f(
(float)(bigMapCenterPoint.X + xOffset * moveMouseX / totalMoveMouseX),
(float)(bigMapCenterPoint.Y + yOffset * moveMouseY / totalMoveMouseY)
);
// 利用移动鼠标的距离获取新的中心
}
// 本次移动的距离 // 本次移动的距离
double diffMapX = Math.Abs(newBigMapCenterPoint.X - bigMapCenterPoint.X); double diffMapX = Math.Abs(newBigMapCenterPoint.X - bigMapCenterPoint.X);
double diffMapY = Math.Abs(newBigMapCenterPoint.Y - bigMapCenterPoint.Y); double diffMapY = Math.Abs(newBigMapCenterPoint.Y - bigMapCenterPoint.Y);
double moveDistance = Math.Sqrt(diffMapX * diffMapX + diffMapY * diffMapY); double moveDistance = Math.Sqrt(diffMapX * diffMapX + diffMapY * diffMapY);
if (moveDistance > 10) // 移动距离大于10认为本次移动成功 if (moveDistance > 10) // 移动距离大于10认为本次移动成功
{ {
(xOffset, yOffset) = (x - newBigMapCenterPoint.X, y - newBigMapCenterPoint.Y); // 更新目标偏移量 (xOffset, yOffset) = (x - newBigMapCenterPoint.X, y - newBigMapCenterPoint.Y); // 更新目标偏移量
double totalMoveMouseX = Math.Abs(moveMouseX * xOffset / diffMapX); totalMoveMouseX = Math.Abs(moveMouseX * xOffset / diffMapX);
double totalMoveMouseY = Math.Abs(moveMouseY * yOffset / diffMapY); totalMoveMouseY = Math.Abs(moveMouseY * yOffset / diffMapY);
double mouseDistance = Math.Sqrt(totalMoveMouseX * totalMoveMouseX + totalMoveMouseY * totalMoveMouseY); double mouseDistance = Math.Sqrt(totalMoveMouseX * totalMoveMouseX + totalMoveMouseY * totalMoveMouseY);
// 调整地图缩放 // 调整地图缩放
// mapZoomLevel<5 才显示传送锚点和秘境; // mapZoomLevel<5 才显示传送锚点和秘境;
@ -224,15 +237,7 @@ public class TpTask(CancellationToken ct)
Debug.WriteLine($"在 {iteration} 迭代后,已经接近目标点,不再进一步调整。"); Debug.WriteLine($"在 {iteration} 迭代后,已经接近目标点,不再进一步调整。");
break; break;
} }
//else if (mouseDistance > 1000 && mapZoomLevel < 6) while (mouseDistance < 2 * tolerance && mapZoomLevel > 1)
//{ // 缩小地图
// await AdjustMapZoomLevel(false);
// totalMoveMouseX *= (mapZoomLevel) / (mapZoomLevel + 1);
// totalMoveMouseY *= (mapZoomLevel) / (mapZoomLevel + 1);
// mouseDistance *= (mapZoomLevel) / (mapZoomLevel + 1);
// mapZoomLevel++;
//}
else if (mouseDistance < 200 && mapZoomLevel > 1)
{ // 放大地图 { // 放大地图
await AdjustMapZoomLevel(true); await AdjustMapZoomLevel(true);
totalMoveMouseX *= (mapZoomLevel) / (mapZoomLevel - 1); totalMoveMouseX *= (mapZoomLevel) / (mapZoomLevel - 1);
@ -240,8 +245,7 @@ public class TpTask(CancellationToken ct)
mouseDistance *= (mapZoomLevel) / (mapZoomLevel - 1); mouseDistance *= (mapZoomLevel) / (mapZoomLevel - 1);
mapZoomLevel--; mapZoomLevel--;
} }
// 单次移动最大距离为 250 // 单次移动最大距离为 250
moveMouseX = (int)Math.Min(totalMoveMouseX, 250 * totalMoveMouseX / mouseDistance) * Math.Sign(xOffset); moveMouseX = (int)Math.Min(totalMoveMouseX, 250 * totalMoveMouseX / mouseDistance) * Math.Sign(xOffset);
moveMouseY = (int)Math.Min(totalMoveMouseY, 250 * totalMoveMouseY / mouseDistance) * Math.Sign(yOffset); moveMouseY = (int)Math.Min(totalMoveMouseY, 250 * totalMoveMouseY / mouseDistance) * Math.Sign(yOffset);