public Bitmap grabScreen2(bool detect)
        {
            Bitmap returnValue = null;

            if (IsTest)
            {
                if (IsTest)
                {
                    //TEST CODE
                    System.Drawing.Rectangle boundsRect = new System.Drawing.Rectangle(0, 0, testImage.Width, testImage.Height);
                    var bitmapData    = testImage.LockBits(boundsRect, ImageLockMode.WriteOnly, testImage.PixelFormat);
                    var bitmapPointer = bitmapData.Scan0;
                    unsafe
                    {
                        Console.WriteLine("Test image width: " + testImage.Width + ", height: " + testImage.Height);
                        processDetection((byte *)bitmapPointer.ToPointer(), testImage.Width, testImage.Height);
                    }

                    testImage.UnlockBits(bitmapData);
                    displayImage = new Bitmap(testImage);
                    returnValue  = new Bitmap(testImage);
                }
            }
            else
            {
                Bitmap desktopBMP = new Bitmap(
                    System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
                    System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);

                Graphics g = Graphics.FromImage(desktopBMP);

                g.CopyFromScreen(0, 0, 0, 0,
                                 new Size(
                                     System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
                                     System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height));


                g.Dispose();
                if (detect)
                {
                    System.Drawing.Rectangle boundsRect = new System.Drawing.Rectangle(0, 0, desktopBMP.Width, desktopBMP.Height);
                    var bitmapData    = desktopBMP.LockBits(boundsRect, ImageLockMode.WriteOnly, desktopBMP.PixelFormat);
                    var bitmapPointer = bitmapData.Scan0;
                    unsafe
                    {
                        processDetection((byte *)bitmapPointer.ToPointer(), desktopBMP.Width, desktopBMP.Height);
                    }

                    desktopBMP.UnlockBits(bitmapData);
                }
                if (shouldCaptureDisplayImage)
                {
                    displayImage = desktopBMP;
                }
                if (saveStopwatch.DurationInSeconds() > 1.0 && recordDisplayImage)
                {
                    saveStopwatch.Reset();
                    displayImage.Save("Recording/AI Record " + desktopBMP.Width + "x" + desktopBMP.Height + " " + Environment.TickCount + ".png", System.Drawing.Imaging.ImageFormat.Png);
                }
                returnValue = new Bitmap(desktopBMP);
            }

            System.GC.Collect();

            return(returnValue);
        }
示例#2
0
        private void logic()
        {
            if (fpsLimitStopwatch.DurationInMilliseconds() < fpsLimit)
            {
                return;
            }
            fpsLimitStopwatch.Reset();

            bool leagueOfLegendsOpen = false;

            Process[] pname = Process.GetProcessesByName("league of legends");
            if (pname.Length != 0)
            {
                leagueOfLegendsOpen = true;
            }

            bool leagueOfLegendsClientOpen = false;

            pname = Process.GetProcessesByName("LolClient");
            if (pname.Length != 0)
            {
                leagueOfLegendsClientOpen = true;
            }

            TaskScheduler aiContext = TaskScheduler.Current;

            //Measure timer performance
            double aiMilliseconds = timerPerformanceStopwatch.DurationInMilliseconds();
            double aiFps          = (1000.0 / aiMilliseconds);

            timerPerformanceCount++;
            timerPerformanceLength += aiMilliseconds;
            if (timerPerformanceLength >= 1000)
            {
                double averageMilliseconds = timerPerformanceLength / timerPerformanceCount;
                double averageFps          = 1000.0 / averageMilliseconds;
                userInterface.setAIPerformanceLabel("" + Math.Round(averageFps, 4) + " fps (" + Math.Round(averageMilliseconds, 4) + " ms)");
                timerPerformanceCount  = 0;
                timerPerformanceLength = 0;
            }
            timerPerformanceStopwatch.Reset();

            //Only grab the screen when it's done processing.
            //Do on separate task so we don't freeze the AI.
            if (grabbingScreen == false)
            {
                screenCapturePerformanceStopWatch.Reset();
                grabbingScreen = true;
                Task t = Task.Run(() =>
                {
                    //visualCortex.runTest();
                    Bitmap screen = visualCortex.grabScreen2(leagueOfLegendsOpen);

                    DetectionDataStruct data = visualCortex.getVisualDetectionData();
                    TaskHelper.RunTask(aiContext, () =>
                    {
                        updateDetectionData(ref data, screen);
                    });
                }).ContinueWith(_ => {
                    //Run on UI thread
                    if (updateDisplayImage)
                    {
                        userInterface.setDisplayImage(visualCortex.getDisplayImage());
                    }
                    grabbingScreen            = false;
                    double screenMilliseconds = screenCapturePerformanceStopWatch.DurationInMilliseconds();
                    double screenFps          = (1000.0 / screenMilliseconds);
                    userInterface.setScreenPerformanceLabel("" + Math.Round(screenFps, 4) + " fps (" + Math.Round(screenMilliseconds, 4) + " ms)");
                }, aiContext);
            }

            if (leagueIsInGame == false && leagueOfLegendsOpen)
            {
                basicAI.resetAI(autoQueue.getPickedLane());
                autoQueue.enteredLeagueOfLegends();
            }
            if (leagueIsInGame == true && !leagueOfLegendsOpen)
            {
                autoQueue.exitedLeagueOfLegends();
            }
            leagueIsInGame = leagueOfLegendsOpen;

            //Run basic AI algorithm
            if (leagueOfLegendsOpen)
            {
                lock (detectionDataLock)
                {
                    basicAI.processAI();
                }
            }
            else if ((leagueOfLegendsClientOpen || VisualCortex.IsTest) && currentScreen != null)
            {
                //Console.WriteLine("League of legends client open and scanning for auto queue");
                //Run auto queue
                autoQueue.runAutoQueue(currentScreen);
                // userInterface.setDebugImage(new Bitmap(AutoQueueDetection.matchingBitmap));
            }
            if (currentScreen != null)
            {
                autoQueue.runErrorCheck(currentScreen);
            }
            autoQueue.runSleepLogic();
        }