static void Fight() { AutomationDetector detector = new AutomationDetector(); // 启用自动化 detector.EnableAutomation(); DateTime startBattleTime = DateTime.Now; Random rand = new Random(); string[] keys = new string[] { "a", "s", "w", "d" }; // wait for combat ending while (_stateWaiter.Detector.Detect() == GameState.Fighting) { // 判断 自动 是否被启用 if (!detector.AutomationEnabled) { // send some random keys before enable automation for (int i = 0; i < 100; ++i) { AutoItX.Send(keys[rand.Next() % keys.Length]); } detector.EnableAutomation(); } else { detector.Detect(); } AutoItX.Sleep(500); DateTime current = DateTime.Now; TimeSpan battleTime = current - startBattleTime; if (battleTime.TotalMinutes > 10) { // overtime, must be stuck Console.WriteLine("Battle over 10 minutes, might be stuck"); MainWindowUtility.Close(); return; } } }
static void Run(string target, JobType job) { bool existsMainWindow = MainWindowUtility.ExistsMainWindow(); if (!existsMainWindow && !ScreenUtility.IsForegroundFullScreen()) { if (!Launch(target)) { return; } } else { Console.WriteLine("Main windows had been opened"); } if (existsMainWindow) { MainWindowUtility.ActivateAndWait(); } var screenBounds = ScreenUtility.GetScreenBounds(null); Console.WriteLine("Screen: <{0}, {1}, {2}, {3}>", screenBounds.Top, screenBounds.Left, screenBounds.Height, screenBounds.Width); IntPtr hwnd = IntPtr.Zero; if (!ScreenUtility.IsForegroundFullScreen()) { hwnd = MainWindowUtility.GetMainWindowHandle(); var windowBounds = ScreenUtility.GetWindowBounds(hwnd); var clientRect = ScreenUtility.GetClientRect(hwnd); Console.WriteLine("Window: <{0}, {1}, {2}, {3}>", windowBounds.Top, windowBounds.Left, windowBounds.Height, windowBounds.Width); Console.WriteLine("Client: <{0}, {1}, {2}, {3}>", clientRect.Top, clientRect.Left, clientRect.Height, clientRect.Width); ScreenUtility.SetScreenAttribute(screenBounds, false); //int left, width, top, height; //if (windowBounds.Left < 0) //{ // width = windowBounds.Width - 2 * Math.Abs(windowBounds.Left); // left = 0; //} //else //{ // width = windowBounds.Width; // left = windowBounds.Left; //} //if (windowBounds.Top < 0) //{ // height = windowBounds.Height - 2 * Math.Abs(windowBounds.Top); // top = 0; //} //else //{ // height = windowBounds.Height; // top = windowBounds.Top; //} } else { ScreenUtility.SetScreenAttribute(screenBounds, true); } if (!_stateWaiter.WaitForState(GameState.Unknown, GameState.MainWindow, null, MaxSingleTaskTime)) { Console.WriteLine("Failed to show main window although main window was opened"); MainWindowUtility.Close(); return; } if (job == JobType.ElementChallenge) { //if (!CheatEngineUtility.EnableCheating()) //{ // Console.WriteLine("Cheating is not enabled, can't run element challenging job"); // return; //} } if (!ScreenUtility.IsForegroundFullScreen()) { MainWindowUtility.ActivateAndWait(); } Run(job); if (_stateWaiter.Detector.Detect() != GameState.MainWindow) { Console.WriteLine("Failed to go back main window after run"); MainWindowUtility.Close(); return; } }