internal async Task Run(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { WindowHelper.Restore(_hWnd); WindowHelper.SetForegroundWindow(_hWnd); OnReport("Capture screen"); var screenshot = ScreenshotHelper.GetScreenBitmap(_hWnd); var targetIsSelected = await SelectGoodTarget(cancellationToken, screenshot); if (!targetIsSelected && !_timer.Enabled) { _timer.Start(); } if (targetIsSelected) { await FightTarget(cancellationToken); } else { await TurnScreen(cancellationToken); } await Task.Delay(TimeSpan.FromSeconds(0.5), cancellationToken); } }
private async Task <bool> SelectGoodTarget(CancellationToken cancellationToken, [NotNull] Bitmap screenshot) { if (screenshot == null) { throw new ArgumentNullException(nameof(screenshot)); } var screenshotWithExclude = ScreenshotHelper.ApplyExclude(_settings.ExcludeInfo.Data, screenshot); OnScreenCapture(new Bitmap(screenshotWithExclude)); var src = Mat.FromImageData(BitmapHelper.ImageToByte2(screenshotWithExclude)); var blacked = new Mat(); Cv2.Threshold(src, blacked, 127, 255, ThresholdTypes.Binary); var blackedBitmap = blacked.ToBitmap(); var targets = GetTargets(blackedBitmap); foreach (var target in targets) { WindowCommandHelper.LeftClick(target); var screenshotWithHp = ScreenshotHelper.GetScreenBitmap(_hWnd); var targetHealth = ScreenshotHelper.GetSubPart(screenshotWithHp, _settings.RegionInfo.TargetHp); var targetHealthPercent = GetTargetHpPercent(targetHealth); OnReport($"Target health: {targetHealthPercent}"); if (targetHealthPercent > 0) { return(true); } OnReport($"Target is dead or neutral"); await Task.Delay(TimeSpan.FromSeconds(0.5), cancellationToken); } var screenshotWithHp2 = ScreenshotHelper.GetScreenBitmap(_hWnd); var targetHealth2 = ScreenshotHelper.GetSubPart(screenshotWithHp2, _settings.RegionInfo.TargetHp); var targetHealthPercent2 = GetTargetHpPercent(targetHealth2); OnReport($"Target health: {targetHealthPercent2}"); if (targetHealthPercent2 > 0) { return(true); } OnReport($"Target is dead or neutral"); await Task.Delay(TimeSpan.FromSeconds(0.5), cancellationToken); return(false); }
private async Task FightTarget(CancellationToken cancellationToken) { _timer.Stop(); OnReport($"Target found!"); while (!cancellationToken.IsCancellationRequested) { var screenshot = ScreenshotHelper.GetScreenBitmap(_hWnd); var targetHealth = ScreenshotHelper.GetSubPart(screenshot, _settings.RegionInfo.TargetHp); var targetHealthPercent = GetTargetHpPercent(targetHealth); var myHealth = ScreenshotHelper.GetSubPart(screenshot, _settings.RegionInfo.MyHp); var myHealthPercent = GetMyHpPercent(myHealth); if (Math.Abs(myHealthPercent) < 100 && _slaveIntPtr.HasValue) { OnReport("Request self heal"); WindowCommandHelper.PressKey(_slaveIntPtr.Value, WindowCommandHelper.KeyCodes.F7); await Task.Delay(TimeSpan.FromSeconds(1)); WindowCommandHelper.PressKey(_slaveIntPtr.Value, WindowCommandHelper.KeyCodes.F3); } OnReport($"Target HP: {targetHealthPercent:###.##}% MY: {myHealthPercent:###.##}%"); if (Math.Abs(targetHealthPercent) < 0.00001) { break; } WindowCommandHelper.PressKey(_hWnd, WindowCommandHelper.KeyCodes.F1); await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); } OnReport($"Target is dead!"); WindowCommandHelper.PressKey(_hWnd, WindowCommandHelper.KeyCodes.F4); await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken); WindowCommandHelper.PressKey(_hWnd, WindowCommandHelper.KeyCodes.F4); await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken); WindowCommandHelper.PressKey(_hWnd, WindowCommandHelper.KeyCodes.F4); await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken); WindowCommandHelper.PressKey(_hWnd, WindowCommandHelper.KeyCodes.F4); }