Esempio n. 1
0
        static void Main(string[] args)
        {
            // Read settings
            DataContractJsonSerializer Settings = new DataContractJsonSerializer(typeof(Settings[]));

            Settings[] settings    = null;
            Settings   auto_config = new Settings(320, 320, "game", true, Keys.MButton, Keys.Insert, Keys.Home, Keys.NumPad9, 0.1f, true, false);

            using (FileStream fs = new FileStream("config.json", FileMode.OpenOrCreate))
            {
                if (fs.Length == 0)
                {
                    Settings.WriteObject(fs, new Settings[1] {
                        auto_config
                    });
                    MessageBox.Show($"Created auto-config, change whatever settings you want and restart.");
                    return;
                }
                else
                {
                    settings = (Settings[])Settings.ReadObject(fs);
                }
            }

            //Vars
            size.X = settings[0].SizeX;
            size.Y = settings[0].SizeY;
            string game              = settings[0].Game;
            bool   SimpleRCS         = settings[0].SimpleRCS;
            Keys   ShootKey          = settings[0].ShootKey;
            Keys   TrainModeKey      = settings[0].TrainModeKey;
            Keys   ScreenshotKey     = settings[0].ScreenshotKey;
            Keys   ScreenshotModeKey = settings[0].ScreenshotModeKey;
            float  SmoothAim         = settings[0].SmoothAim;
            bool   Information       = settings[0].Information;
            bool   Head              = settings[0].Head;

            int i = 0;
            int selectedObject = 0;
            int shooting       = 0;

            string[]      objects = null;
            OverlayWindow _window;

            GameOverlay.Drawing.Graphics _graphics;
            bool trainingMode = false;

            screenshotMode = false;

            YoloWrapper yoloWrapper = null;

            //Check compatibility
            if (Process.GetProcessesByName(game).Count() == 0)
            {
                MessageBox.Show($"You have not launched {game}...");
                Process.GetCurrentProcess().Kill();
            }

            if (File.Exists($"trainfiles/{game}.cfg") && File.Exists($"trainfiles/{game}.weights") && File.Exists($"trainfiles/{game}.names"))
            {
                yoloWrapper = new YoloWrapper($"trainfiles/{game}.cfg", $"trainfiles/{game}.weights", $"trainfiles/{game}.names");
                Console.Clear();
                if (yoloWrapper.EnvironmentReport.CudaExists == false)
                {
                    Console.WriteLine("Install CUDA 10");
                    Process.GetCurrentProcess().Kill();
                }
                if (yoloWrapper.EnvironmentReport.CudnnExists == false)
                {
                    Console.WriteLine("Cudnn doesn't exist");
                    Process.GetCurrentProcess().Kill();
                }
                if (yoloWrapper.EnvironmentReport.MicrosoftVisualCPlusPlus2017RedistributableExists == false)
                {
                    Console.WriteLine("Install Microsoft Visual C++ 2017 Redistributable");
                    Process.GetCurrentProcess().Kill();
                }
                if (yoloWrapper.DetectionSystem.ToString() != "GPU")
                {
                    MessageBox.Show("No GPU card detected. Exiting...");
                    Process.GetCurrentProcess().Kill();
                }
                objects = File.ReadAllLines($"trainfiles/{game}.names");
            }
            else
            {
                trainingMode = true;
                MessageBox.Show($"Looks like you have not configured settings for {game}... Let's train the Neural Network! :)\n Preparing files for training....");

                Console.Write("How many objects will the NN be analyzing and training on? Write each object's name via the separator ',' without spaces (EX: 1,2): ");
                objects = Console.ReadLine().Split(',');
            }


            PrepareFiles(game);
            Random random = new Random();

            //Make transparent window for drawing
            _window = new OverlayWindow(0, 0, size.X, size.Y)
            {
                IsTopmost = true,
                IsVisible = true
            };

            _graphics = new GameOverlay.Drawing.Graphics()
            {
                MeasureFPS = true,
                Height     = _window.Height,
                PerPrimitiveAntiAliasing  = true,
                TextAntiAliasing          = true,
                UseMultiThreadedFactories = false,
                VSync        = true,
                Width        = _window.Width,
                WindowHandle = IntPtr.Zero
            };

            _window.CreateWindow();

            _graphics.WindowHandle = _window.Handle;
            _graphics.Setup();


            GameOverlay.Drawing.Graphics gfx = _graphics;

            System.Drawing.Rectangle trainBox = new System.Drawing.Rectangle(0, 0, size.X / 2, size.Y / 2);

            while (true)
            {
                coordinates = Cursor.Position;

                if (screenshotMode)
                {
                    bitmap = new Bitmap(CaptureWindow(game, true), size.X, size.Y);
                }
                else
                {
                    bitmap = new Bitmap(CaptureWindow(game, false), size.X, size.Y);
                }

                trainBox.X = size.X / 2 - trainBox.Width / 2;
                trainBox.Y = size.Y / 2 - trainBox.Height / 2;

                if (User32.GetAsyncKeyState(TrainModeKey) == -32767)
                {
                    if (yoloWrapper != null)
                    {
                        objects      = File.ReadAllLines($"trainfiles/{game}.names");
                        trainingMode = trainingMode == true ? false : true;
                    }
                }

                if (User32.GetAsyncKeyState(ScreenshotModeKey) == -32767)
                {
                    screenshotMode = screenshotMode == true ? false : true;
                }

                _window.X = coordinates.X - size.X / 2;
                _window.Y = coordinates.Y - size.Y / 2;
                gfx.BeginScene();
                gfx.ClearScene();

                gfx.DrawRectangle(_graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Green), 0, 0, size.X, size.Y, 2);

                if (trainingMode)
                {
                    int rand = random.Next(5000, 999999);

                    if (User32.GetAsyncKeyState(Keys.Left) != 0)
                    {
                        if (trainBox.Width <= 0)
                        {
                            continue;
                        }
                        else
                        {
                            trainBox.Width -= 1;
                        }
                    }

                    if (User32.GetAsyncKeyState(Keys.Down) != 0)
                    {
                        if (trainBox.Height >= size.Y)
                        {
                            continue;
                        }
                        else
                        {
                            trainBox.Height += 1;
                        }
                    }

                    if (User32.GetAsyncKeyState(Keys.Right) != 0)
                    {
                        if (trainBox.Width >= size.X)
                        {
                            continue;
                        }
                        else
                        {
                            trainBox.Width += 1;
                        }
                    }

                    if (User32.GetAsyncKeyState(Keys.Up) != 0)
                    {
                        if (trainBox.Height <= 0)
                        {
                            continue;
                        }
                        else
                        {
                            trainBox.Height -= 1;
                        }
                    }

                    float relative_center_x = (float)(trainBox.X + trainBox.Width / 2) / size.X;
                    float relative_center_y = (float)(trainBox.Y + trainBox.Height / 2) / size.Y;
                    float relative_width    = (float)trainBox.Width / size.X;
                    float relative_height   = (float)trainBox.Height / size.Y;
                    gfx.DrawTextWithBackground(_graphics.CreateFont("Arial", 14), _graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Red), _graphics.CreateSolidBrush(0, 0, 0), new GameOverlay.Drawing.Point(0, 0), "Training mode. Object: " + objects[selectedObject] + Environment.NewLine + "ScreenshotMode: " + (screenshotMode == true ? "following" : "centered"));
                    gfx.DrawRectangle(_graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Blue), GameOverlay.Drawing.Rectangle.Create(trainBox.X, trainBox.Y, trainBox.Width, trainBox.Height), 1);
                    gfx.DrawRectangle(_graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Red), GameOverlay.Drawing.Rectangle.Create(trainBox.X + Convert.ToInt32(trainBox.Width / 2.9), trainBox.Y, Convert.ToInt32(trainBox.Width / 3), trainBox.Height / 7), 2);

                    if (User32.GetAsyncKeyState(Keys.PageUp) == -32767)
                    {
                        selectedObject = selectedObject + 1 == objects.Count() ? 0 : selectedObject + 1;
                    }

                    if (User32.GetAsyncKeyState(Keys.PageDown) == -32767)
                    {
                        selectedObject = selectedObject == 0 ? objects.Count() - 1 : selectedObject - 1;
                    }

                    if (User32.GetAsyncKeyState(ScreenshotKey) == -32767)
                    {
                        bitmap.Save($"darknet/data/img/{game}{i.ToString()}{rand}.png", System.Drawing.Imaging.ImageFormat.Png);
                        File.WriteAllText($"darknet/data/img/{game}{i.ToString()}{rand}.txt", string.Format("{0} {1} {2} {3} {4}", selectedObject, relative_center_x, relative_center_y, relative_width, relative_height).Replace(",", "."));
                        i++;
                        Console.Beep();
                    }

                    if (User32.GetAsyncKeyState(Keys.Back) == -32767)
                    {
                        bitmap.Save($"darknet/data/img/{game}{i.ToString()}{rand}.png", System.Drawing.Imaging.ImageFormat.Png);
                        File.WriteAllText($"darknet/data/img/{game}{i.ToString()}{rand}.txt", "");
                        i++;
                        Console.Beep();
                    }

                    if (User32.GetAsyncKeyState(Keys.End) == -32767)
                    {
                        Console.WriteLine("Okay, we have the pictures for training. Let's train the Neural Network....");
                        File.WriteAllText($"darknet/{game}.cfg", File.ReadAllText($"darknet/{game}.cfg").Replace("NUMBER", objects.Count().ToString()).Replace("FILTERNUM", ((objects.Count() + 5) * 3).ToString()));
                        File.WriteAllText($"darknet/{game}.cfg", File.ReadAllText($"darknet/{game}.cfg").Replace("batch=1", "batch=64").Replace("subdivisions=1", "subdivisions=8"));
                        File.WriteAllText($"darknet/data/{game}.data", File.ReadAllText($"darknet/data/{game}.data").Replace("NUMBER", objects.Count().ToString()).Replace("GAME", game));
                        File.WriteAllText($"darknet/{game}.cmd", File.ReadAllText($"darknet/{game}.cmd").Replace("GAME", game));
                        File.WriteAllText($"darknet/{game}_trainmore.cmd", File.ReadAllText($"darknet/{game}_trainmore.cmd").Replace("GAME", game));
                        File.WriteAllText($"darknet/data/{game}.names", string.Join("\n", objects));
                        // DirectoryInfo d = ;//Assuming Test is your Folder
                        FileInfo[] Files     = new DirectoryInfo(Application.StartupPath + @"\darknet\data\img").GetFiles($"{game}*.png"); //Getting Text files
                        string     PathOfImg = "";
                        foreach (FileInfo file in Files)
                        {
                            PathOfImg += $"data/img/{file.Name}\r\n";
                        }

                        File.WriteAllText($"darknet/data/{game}.txt", PathOfImg);

                        Process.GetProcessesByName(game)[0].Kill();
                        if (File.Exists($"trainfiles/{game}.weights"))
                        {
                            File.Copy($"trainfiles/{game}.weights", $"darknet/{game}.weights", true);
                            Process.Start("cmd", @"/C cd " + Application.StartupPath + $"/darknet/ & {game}_trainmore.cmd");
                        }
                        else
                        {
                            Process.Start("cmd", @"/C cd " + Application.StartupPath + $"/darknet/ & {game}.cmd");
                        }

                        Console.WriteLine("When you have finished training the NN, write \"done\" in this console.");

                        while (true)
                        {
                            if (Console.ReadLine() == "done")
                            {
                                File.Copy($"darknet/data/backup/{game}_last.weights", $"trainfiles/{game}.weights", true);
                                File.Copy($"darknet/data/{game}.names", $"trainfiles/{game}.names", true);
                                File.Copy($"darknet/{game}.cfg", $"trainfiles/{game}.cfg", true);
                                File.WriteAllText($"trainfiles/{game}.cfg", File.ReadAllText($"trainfiles/{game}.cfg").Replace("batch=64", "batch=1").Replace("subdivisions=8", "subdivisions=1"));
                                yoloWrapper  = new YoloWrapper($"trainfiles/{game}.cfg", $"trainfiles/{game}.weights", $"trainfiles/{game}.names");
                                trainingMode = false;
                                break;
                            }
                            else
                            {
                                Console.WriteLine("When you have finished training the NN, write \"done\" in this console.");
                            }
                        }
                        Console.WriteLine("Okay! Training has finished. Let's check detection in the game!");
                    }
                }
                else
                {
                    if (User32.GetAsyncKeyState(Keys.PageUp) == -32767)
                    {
                        selectedObject = selectedObject + 1 == objects.Count() ? 0 : selectedObject + 1;
                    }

                    if (User32.GetAsyncKeyState(Keys.Up) == -32767)
                    {
                        SmoothAim = SmoothAim >= 1 ? SmoothAim : SmoothAim + 0.05f;
                    }

                    if (User32.GetAsyncKeyState(Keys.Down) == -32767)
                    {
                        SmoothAim = SmoothAim <= 0 ? SmoothAim : SmoothAim - 0.05f;
                    }

                    if (User32.GetAsyncKeyState(Keys.Delete) == -32767)
                    {
                        Head = Head == true ? false : true;
                    }

                    if (User32.GetAsyncKeyState(Keys.Home) == -32767)
                    {
                        shooting  = 0;
                        SimpleRCS = SimpleRCS == true ? false : true;
                    }

                    if (User32.GetAsyncKeyState(Keys.PageDown) == -32767)
                    {
                        selectedObject = selectedObject == 0 ? objects.Count() - 1 : selectedObject - 1;
                    }
                    gfx.DrawText(_graphics.CreateFont("Arial", 10), _graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Red), new GameOverlay.Drawing.Point(0, 0), $"Object {objects[selectedObject]}; SmoothAim {Math.Round(SmoothAim, 2)}; Head {Head}; SimpleRCS {SimpleRCS}");

                    using (MemoryStream ms = new MemoryStream())
                    {
                        bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        IEnumerable <Alturos.Yolo.Model.YoloItem> items = yoloWrapper.Detect(ms.ToArray());
                        if (SimpleRCS)
                        {
                            if (User32.GetAsyncKeyState(ShootKey) == 0)
                            {
                                shooting = 0;
                            }
                        }

                        if (items.Count() > 0)
                        {
                            foreach (var item in items)
                            {
                                if (item.Confidence > (double)0.4)
                                {
                                    GameOverlay.Drawing.Rectangle head = GameOverlay.Drawing.Rectangle.Create(item.X + Convert.ToInt32(item.Width / 2.9), item.Y, Convert.ToInt32(item.Width / 3), item.Height / 7);
                                    GameOverlay.Drawing.Rectangle body = GameOverlay.Drawing.Rectangle.Create(item.X + Convert.ToInt32(item.Width / 6), item.Y + item.Height / 6, Convert.ToInt32(item.Width / 1.5f), item.Height / 3);

                                    if (Information)
                                    {
                                        if (Head)
                                        {
                                            gfx.DrawTextWithBackground(_graphics.CreateFont("Arial", 12), _graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Red), _graphics.CreateSolidBrush(0, 0, 0), new GameOverlay.Drawing.Point(item.X + item.Width, item.Y + item.Width), $"{item.Type} {DistanceBetweenCross(head.Left + head.Width / 2, head.Top + head.Height / 2)}");
                                        }
                                        else
                                        {
                                            gfx.DrawTextWithBackground(_graphics.CreateFont("Arial", 12), _graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Red), _graphics.CreateSolidBrush(0, 0, 0), new GameOverlay.Drawing.Point(item.X + item.Width, item.Y + item.Width), $"{item.Type} {DistanceBetweenCross(body.Left + body.Width / 2, body.Top + body.Height / 2)}");
                                        }
                                    }

                                    if (item.Type == objects[selectedObject])
                                    {
                                        gfx.DrawRectangle(_graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Red), GameOverlay.Drawing.Rectangle.Create(item.X, item.Y, item.Width, item.Height), 2);

                                        if (Head)
                                        {
                                            gfx.DrawRectangle(_graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Blue), head, 2);
                                            gfx.DrawCrosshair(_graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Blue), head.Left + head.Width / 2, head.Top + head.Height / 2 + Convert.ToInt32(1 * shooting), 2, 2, CrosshairStyle.Cross);
                                            gfx.DrawLine(_graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Blue), size.X / 2, size.Y / 2, head.Left + head.Width / 2, head.Top + head.Height / 2 + Convert.ToInt32(1 * shooting), 2);
                                        }
                                        else
                                        {
                                            gfx.DrawRectangle(_graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Blue), body, 2);
                                            gfx.DrawCrosshair(_graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Blue), body.Left + body.Width / 2, body.Top + body.Height / 2 + Convert.ToInt32(1 * shooting), 2, 2, CrosshairStyle.Cross);
                                            gfx.DrawLine(_graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Blue), size.X / 2, size.Y / 2, body.Left + body.Width / 2, body.Top + body.Height / 2 + Convert.ToInt32(1 * shooting), 2);
                                        }

                                        if (User32.GetAsyncKeyState(ShootKey) != 0)
                                        {
                                            if (Head)
                                            {
                                                Alturos.Yolo.Model.YoloItem nearestEnemy = items.Where(x => x.Type == objects[selectedObject]).OrderByDescending(x => DistanceBetweenCross(x.X + Convert.ToInt32(x.Width / 2.9) + (x.Width / 3) / 2, x.Y + (x.Height / 7) / 2)).Last();

                                                GameOverlay.Drawing.Rectangle nearestEnemyHead = GameOverlay.Drawing.Rectangle.Create(nearestEnemy.X + Convert.ToInt32(nearestEnemy.Width / 2.9), nearestEnemy.Y, Convert.ToInt32(nearestEnemy.Width / 3), nearestEnemy.Height / 7 + (float)2 * shooting);

                                                if (SmoothAim <= 0)
                                                {
                                                    User32.mouse_event(0x01, Convert.ToInt32(((nearestEnemyHead.Left - size.X / 2) + (nearestEnemyHead.Width / 2))), Convert.ToInt32((nearestEnemyHead.Top - size.Y / 2 + nearestEnemyHead.Height / 7 + 1 * shooting)), 0, (UIntPtr)0);
                                                    User32.mouse_event(0x02, 0, 0, 0, (UIntPtr)0);
                                                    User32.mouse_event(0x04, 0, 0, 0, (UIntPtr)0);
                                                }
                                                else
                                                {
                                                    if (size.X / 2 < nearestEnemyHead.Left | size.X / 2 > nearestEnemyHead.Right
                                                        | size.Y / 2 < nearestEnemyHead.Top | size.Y / 2 > nearestEnemyHead.Bottom)
                                                    {
                                                        User32.mouse_event(0x01, Convert.ToInt32(((nearestEnemyHead.Left - size.X / 2) + (nearestEnemyHead.Width / 2)) * SmoothAim), Convert.ToInt32((nearestEnemyHead.Top - size.Y / 2 + nearestEnemyHead.Height / 7 + 1 * shooting) * SmoothAim), 0, (UIntPtr)0);
                                                    }
                                                    else
                                                    {
                                                        User32.mouse_event(0x02, 0, 0, 0, (UIntPtr)0);
                                                        User32.mouse_event(0x04, 0, 0, 0, (UIntPtr)0);
                                                        if (SimpleRCS)
                                                        {
                                                            shooting += 2;
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                Alturos.Yolo.Model.YoloItem nearestEnemy = items.Where(x => x.Type == objects[selectedObject]).OrderByDescending(x => DistanceBetweenCross(x.X + Convert.ToInt32(x.Width / 6) + (x.Width / 1.5f) / 2, x.Y + x.Height / 6 + (x.Height / 3) / 2)).Last();

                                                GameOverlay.Drawing.Rectangle nearestEnemyBody = GameOverlay.Drawing.Rectangle.Create(nearestEnemy.X + Convert.ToInt32(nearestEnemy.Width / 6), nearestEnemy.Y + nearestEnemy.Height / 6 + (float)2 * shooting, Convert.ToInt32(nearestEnemy.Width / 1.5f), nearestEnemy.Height / 3 + (float)2 * shooting);
                                                if (SmoothAim <= 0)
                                                {
                                                    User32.mouse_event(0x01, Convert.ToInt32(((nearestEnemyBody.Left - size.X / 2) + (nearestEnemyBody.Width / 2))), Convert.ToInt32((nearestEnemyBody.Top - size.Y / 2 + nearestEnemyBody.Height / 7 + 1 * shooting)), 0, (UIntPtr)0);
                                                    User32.mouse_event(0x02, 0, 0, 0, (UIntPtr)0);
                                                    User32.mouse_event(0x04, 0, 0, 0, (UIntPtr)0);
                                                }
                                                else
                                                {
                                                    if (size.X / 2 < nearestEnemyBody.Left | size.X / 2 > nearestEnemyBody.Right
                                                        | size.Y / 2 < nearestEnemyBody.Top | size.Y / 2 > nearestEnemyBody.Bottom)
                                                    {
                                                        User32.mouse_event(0x01, Convert.ToInt32(((nearestEnemyBody.Left - size.X / 2) + (nearestEnemyBody.Width / 2)) * SmoothAim), Convert.ToInt32((nearestEnemyBody.Top - size.Y / 2 + nearestEnemyBody.Height / 7 + 1 * shooting) * SmoothAim), 0, (UIntPtr)0);
                                                    }
                                                    else
                                                    {
                                                        User32.mouse_event(0x02, 0, 0, 0, (UIntPtr)0);
                                                        User32.mouse_event(0x04, 0, 0, 0, (UIntPtr)0);
                                                        if (SimpleRCS)
                                                        {
                                                            shooting += 2;
                                                        }
                                                    }
                                                }
                                            }
                                            //System.Threading.Thread.Sleep(120);
                                        }
                                    }
                                    else
                                    {
                                        gfx.DrawRectangle(_graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Green), GameOverlay.Drawing.Rectangle.Create(item.X, item.Y, item.Width, item.Height), 2);
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (SimpleRCS)
                            {
                                shooting = 0;
                            }
                        }
                    }
                }
                gfx.FillRectangle(_graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Blue), GameOverlay.Drawing.Rectangle.Create(size.X / 2, size.Y / 2, 4, 4));

                gfx.EndScene();
            }
        }
        private void Btn_Detect_Click(object sender, RoutedEventArgs e)
        {
            // Yolov3 process
            yoloWrapper = new YoloWrapper(Paras.Yolov3_Cfg.Value, Paras.Yolov3_Weights.Value, Paras.Yolov3_Names.Value);
            var Items_Temp = yoloWrapper.Detect(this.DataByte_Public);

            Dg_Debug.ItemsSource = Items_Temp;

            #region Draw the result onto Raw image
            List <DetectionResult> tray_ID_list = new List <DetectionResult>();
            DrawingVisual          Dv           = new DrawingVisual();
            using (DrawingContext dc = Dv.RenderOpen())
            {
                dc.DrawImage(this.Bi_Public, new Rect(0, 0, Bi_Public.PixelWidth, Bi_Public.PixelHeight));
                for (int i = 0; i < Dg_Debug.Items.Count - 1; i++)
                {
                    try
                    {
                        Alturos.Yolo.Model.YoloItem row = (Alturos.Yolo.Model.YoloItem)Dg_Debug.Items[i];
                        dc.DrawRectangle(null, new System.Windows.Media.Pen(ReturnColor_byType(row.Type), 3), new Rect(row.X, row.Y, row.Width, row.Height));

                        Typeface      typeface      = new Typeface(new System.Windows.Media.FontFamily("Segoe UI"), FontStyles.Normal, FontWeights.UltraBold, FontStretches.Normal);
                        FormattedText formattedText = new FormattedText(row.Type, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, typeface, 16, System.Windows.Media.Brushes.White);

                        System.Windows.Point textLocation = new System.Windows.Point(row.X + 1, row.Y - row.Height / 2 - 3);
                        dc.DrawRectangle(ReturnColor_byType(row.Type), null, new Rect(textLocation.X - 2.5, textLocation.Y - 2.5, formattedText.Width + 5, formattedText.Height + 5));
                        dc.DrawText(formattedText, textLocation);
                    }
                    catch
                    {
                    }
                }
            }
            RenderTargetBitmap rtb = new RenderTargetBitmap(Bi_Public.PixelWidth, Bi_Public.PixelHeight, 96, 96, PixelFormats.Pbgra32);
            rtb.Render(Dv);
            ImgSnapShoot.Source     = rtb;
            this.Bs_Detected_Public = rtb;
            #endregion

            #region Grouping
            string[]   header_array = { "Type", "Confidence", "X", "Y", "Width", "Height" };
            DataTable  dt           = new DataTable();
            DataTable  ID_table     = new DataTable();
            DataColumn column;
            // Add the column to the table.
            for (int i = 0; i < header_array.Length; i++)
            {
                column               = new DataColumn();
                column.DataType      = System.Type.GetType("System.String");
                column.ColumnName    = header_array[i];
                column.AutoIncrement = false;
                column.Caption       = header_array[i];
                column.ReadOnly      = false;
                column.Unique        = false;
                dt.Columns.Add(column);
            }

            for (int i = 0; i < Dg_Debug.Items.Count - 1; i++)
            {
                try
                {
                    Alturos.Yolo.Model.YoloItem row = (Alturos.Yolo.Model.YoloItem)Dg_Debug.Items[i];
                    DataRow newRow = dt.NewRow();
                    newRow["Type"]       = row.Type;
                    newRow["Confidence"] = row.Confidence;
                    // string a = "asd"
                    newRow["X"]      = row.X.ToString().PadLeft(5, '0');
                    newRow["Y"]      = row.Y.ToString().PadLeft(5, '0');
                    newRow["Width"]  = row.Width;
                    newRow["Height"] = row.Height;
                    dt.Rows.Add(newRow);
                }
                catch
                {
                }
            }
            DataView dview_sort = new DataView();
            DataView boxgroup   = new DataView(dt);
            boxgroup.RowFilter = "Type = 'box'";
            boxgroup.Sort      = "Y asc";
            for (int i_group = 0; i_group < boxgroup.Count; i_group++)
            {
                double _Temp_Box_Height = double.Parse(boxgroup[i_group]["Height"].ToString());
                if (_Temp_Box_Height >= double.Parse(Paras.Box_Height_Min.Value))
                {
                    ID_table = new DataTable();
                    for (int i = 0; i < header_array.Length; i++)
                    {
                        column               = new DataColumn();
                        column.DataType      = System.Type.GetType("System.String");
                        column.ColumnName    = header_array[i];
                        column.AutoIncrement = false;
                        column.Caption       = header_array[i];
                        column.ReadOnly      = false;
                        column.Unique        = false;
                        ID_table.Columns.Add(column);
                    }

                    double x_max, x_min, y_max, y_min;
                    x_min = double.Parse(boxgroup[i_group]["X"].ToString());
                    x_max = x_min + double.Parse(boxgroup[i_group]["Width"].ToString());
                    y_min = double.Parse(boxgroup[i_group]["Y"].ToString());
                    y_max = y_min + _Temp_Box_Height;


                    for (int i = 0; i < Dg_Debug.Items.Count - 1; i++)
                    {
                        Alturos.Yolo.Model.YoloItem row = (Alturos.Yolo.Model.YoloItem)Dg_Debug.Items[i];
                        DataRow newRow = ID_table.NewRow();
                        newRow["Type"]       = row.Type;
                        newRow["Confidence"] = row.Confidence;
                        //Dataview sort by string - X-Y data need to be a sting

                        newRow["X"]      = row.X.ToString().PadLeft(5, '0');
                        newRow["Y"]      = row.Y.ToString().PadLeft(5, '0');
                        newRow["Width"]  = row.Width;
                        newRow["Height"] = row.Height;
                        //double x = double.Parse(newRow["X"].ToString()) + double.Parse(newRow["Width"].ToString()) / 2;
                        //double y = double.Parse(newRow["Y"].ToString()) + double.Parse(newRow["Height"].ToString()) / 2;
                        double x = double.Parse(newRow["X"].ToString()) + double.Parse(newRow["Width"].ToString()) / 2;
                        double y = double.Parse(newRow["Y"].ToString()) + _Temp_Box_Height / 2;
                        if ((x_min <= x) && (x_max >= x) && (y_min <= y) && (y_max >= y) && (row.Type.ToString() != "box"))
                        {
                            ID_table.Rows.Add(newRow);
                        }
                    }
                    //
                    dview_sort      = ID_table.DefaultView;
                    dview_sort.Sort = "X asc";
                    string trayID = "";
                    for (int i_char = 0; i_char < dview_sort.Count; i_char++)
                    {
                        trayID = trayID + dview_sort[i_char]["Type"].ToString();
                    }
                    result = new DetectionResult(i_group.ToString(), trayID, Detection_Target_ID);
                    tray_ID_list.Add(result);
                }
            }

            //Validate the result
            Pass_FLAG = 0;
            Fail_FLAG = 0;
            foreach (DetectionResult _iresult in tray_ID_list)
            {
                if (_iresult.Result == "PASS")
                {
                    Pass_FLAG += 1;
                }
                else if (_iresult.Result == "FAIL")
                {
                    Fail_FLAG += 1;
                }
            }
            //
            if (Pass_FLAG == 0 && Fail_FLAG == 0)
            {
                //Final_Result_Flag = "NO TRAY";
                Lb_Reslut.Content    = "NO TRAY";
                Lb_Reslut.Background = System.Windows.Media.Brushes.Orange;
            }
            else if (Fail_FLAG > 0)
            {
                //Final_Result_Flag = "WRONG TRAY";
                Lb_Reslut.Content    = "WRONG TRAY";
                Lb_Reslut.Background = System.Windows.Media.Brushes.Red;
            }
            else if (Pass_FLAG > 0 && Fail_FLAG == 0)
            {
                //Final_Result_Flag = "PASS";
                Lb_Reslut.Content    = "PASS";
                Lb_Reslut.Background = System.Windows.Media.Brushes.Green;
            }
            #endregion
            Dg_TrayID.ItemsSource = tray_ID_list;
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            // Read settings
            DataContractJsonSerializer Settings = new DataContractJsonSerializer(typeof(Settings[]));
            Settings settings    = null;
            Settings auto_config = new Settings(320, 320, "game", true, Keys.MButton, Keys.Insert, Keys.Home, Keys.NumPad9, 0.1f, true, false, true);

            using (FileStream fs = new FileStream("config.json", FileMode.OpenOrCreate))
            {
                if (fs.Length == 0)
                {
                    Settings.WriteObject(fs, new Settings[1] {
                        auto_config
                    });
                    fs.Close();
                    File.WriteAllText("config.json", File.ReadAllText("config.json").Replace(",", ",\n"));
                    MessageBox.Show($"Criação automática de configuração, altere as configurações desejadas e reinicie.");
                    return;
                }
                else
                {
                    settings = ((Settings[])Settings.ReadObject(fs))[0];
                }
            }



            size.X = settings.SizeX;
            size.Y = settings.SizeY;
            string game              = settings.Game;
            bool   SimpleRCS         = settings.SimpleRCS;
            Keys   AimKey            = settings.AimKey;
            Keys   TrainModeKey      = settings.TrainModeKey;
            Keys   ScreenshotKey     = settings.ScreenshotKey;
            Keys   ScreenshotModeKey = settings.ScreenshotModeKey;
            float  SmoothAim         = settings.SmoothAim;
            bool   Information       = settings.Information;
            bool   Head              = settings.Head;
            bool   AutoShoot         = settings.AutoShoot;


            System.Drawing.Point CenterScreen = new System.Drawing.Point(Screen.PrimaryScreen.Bounds.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2);
            bool CursorToCenter = false;
            int  i = 0;
            int  selectedObject = 0;
            int  shooting       = 0;

            string[]      objects = null;
            OverlayWindow _window;

            GameOverlay.Drawing.Graphics _graphics;
            bool trainingMode = false;

            screenshotMode = false;

            YoloWrapper yoloWrapper = null;

            while (Process.GetProcessesByName(game).Count() == 0)
            {
                DialogResult dialogResult = MessageBox.Show($"Você não iniciou o {game} ainda.", "Game não encontrado ", MessageBoxButtons.RetryCancel);
                if (dialogResult == DialogResult.Retry)
                {
                    continue;
                }
                else
                {
                    Process.GetCurrentProcess().Kill();
                }
            }

            if (File.Exists($"trainfiles/{game}.cfg") && File.Exists($"trainfiles/{game}.weights") && File.Exists($"trainfiles/{game}.names"))
            {
                yoloWrapper = new YoloWrapper($"trainfiles/{game}.cfg", $"trainfiles/{game}.weights", $"trainfiles/{game}.names");
                Console.Clear();
                if (yoloWrapper.EnvironmentReport.CudaExists == false)
                {
                    giveErrorMessage("Instale CUDA 10");
                }
                if (yoloWrapper.EnvironmentReport.CudnnExists == false)
                {
                    giveErrorMessage("Cudnn não");
                }
                if (yoloWrapper.EnvironmentReport.MicrosoftVisualCPlusPlus2017RedistributableExists == false)
                {
                    giveErrorMessage("Instale Microsoft Visual C++ 2017 Redistributable");
                }
                if (yoloWrapper.DetectionSystem.ToString() != "GPU")
                {
                    giveErrorMessage("Não foi detectado uma GPU. Fechando...");
                }
                objects = File.ReadAllLines($"trainfiles/{game}.names");
            }
            else
            {
                trainingMode = true;
                MessageBox.Show($"Parece que você não definiu configurações para { game}... Vamos treinar a  Neural Network! :)\n Preparando arquivos para treinamento....");

                Console.Write("Quantos objetos a NN estará analisando e treinando? Escreva o nome de cada objeto através do separador ',' sem espaços (EX: 1,2): ");
                objects = Console.ReadLine().Split(',');
            }


            PrepareFiles(game);
            Random random = new Random();

            //Make transparent window for drawing
            _window = new OverlayWindow(0, 0, size.X, size.Y)
            {
                IsTopmost = true,
                IsVisible = true
            };

            _graphics = new GameOverlay.Drawing.Graphics()
            {
                MeasureFPS = true,
                Height     = _window.Height,
                PerPrimitiveAntiAliasing  = true,
                TextAntiAliasing          = true,
                UseMultiThreadedFactories = false,
                VSync        = true,
                Width        = _window.Width,
                WindowHandle = IntPtr.Zero
            };

            _window.CreateWindow();

            _graphics.WindowHandle = _window.Handle;
            _graphics.Setup();

            GameOverlay.Drawing.SolidBrush greenbrush = _graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Green);
            GameOverlay.Drawing.SolidBrush redbrush   = _graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Red);
            GameOverlay.Drawing.SolidBrush bluebrush  = _graphics.CreateSolidBrush(GameOverlay.Drawing.Color.Blue);

            GameOverlay.Drawing.Graphics gfx = _graphics;

            System.Drawing.Rectangle trainBox = new System.Drawing.Rectangle(0, 0, size.X / 2, size.Y / 2);

            while (true)
            {
                coordinates = Cursor.Position;

                if (screenshotMode)
                {
                    bitmap = new Bitmap(CaptureWindow(game, true), size.X, size.Y);
                }
                else
                {
                    bitmap = new Bitmap(CaptureWindow(game, false), size.X, size.Y);
                }

                trainBox.X = size.X / 2 - trainBox.Width / 2;
                trainBox.Y = size.Y / 2 - trainBox.Height / 2;

                if (isKeyToggled(TrainModeKey))
                {
                    if (yoloWrapper != null)
                    {
                        objects      = File.ReadAllLines($"trainfiles/{game}.names");
                        trainingMode = !trainingMode;
                    }
                }
                if (isKeyToggled(ScreenshotModeKey))
                {
                    screenshotMode = !screenshotMode;
                }

                _window.X = coordinates.X - size.X / 2;
                _window.Y = coordinates.Y - size.Y / 2;
                gfx.BeginScene();
                gfx.ClearScene();

                gfx.DrawRectangle(greenbrush, 0, 0, size.X, size.Y, 2);

                if (CursorToCenter)
                {
                    Cursor.Position = CenterScreen;
                }

                if (isKeyToggled(Keys.NumPad0))
                {
                    CursorToCenter = !CursorToCenter;
                }

                if (trainingMode)
                {
                    int rand = random.Next(5000, 999999);

                    if (isKeyPressed(Keys.Left))
                    {
                        if (trainBox.Width <= 0)
                        {
                            continue;
                        }
                        trainBox.Width -= 1;
                    }

                    if (isKeyPressed(Keys.Down))
                    {
                        if (trainBox.Height >= size.Y)
                        {
                            continue;
                        }
                        trainBox.Height += 1;
                    }

                    if (isKeyPressed(Keys.Right))
                    {
                        if (trainBox.Width >= size.X)
                        {
                            continue;
                        }
                        trainBox.Width += 1;
                    }

                    if (isKeyPressed(Keys.Up))
                    {
                        if (trainBox.Height <= 0)
                        {
                            continue;
                        }
                        trainBox.Height -= 1;
                    }

                    gfx.DrawText(_graphics.CreateFont("Arial", 14), redbrush, new GameOverlay.Drawing.Point(0, 0),
                                 $"Training mode. Object: {objects[selectedObject]}" + Environment.NewLine +
                                 $"ScreenshotMode: {(screenshotMode == true ? "following" : "centered")}" + Environment.NewLine +
                                 $"CursorToCenter: {CursorToCenter}");
                    gfx.DrawRectangle(bluebrush, Rectangle.Create(trainBox.X, trainBox.Y, trainBox.Width, trainBox.Height), 1);
                    gfx.DrawRectangle(redbrush, Rectangle.Create(trainBox.X + Convert.ToInt32(trainBox.Width / 2.9), trainBox.Y, Convert.ToInt32(trainBox.Width / 3), trainBox.Height / 7), 2);
                    if (isKeyToggled(Keys.PageUp))
                    {
                        selectedObject = (selectedObject + 1) % objects.Count();
                    }

                    if (isKeyToggled(Keys.PageDown))
                    {
                        selectedObject = (selectedObject - 1 + objects.Count()) % objects.Count();
                    }

                    if (isKeyToggled(ScreenshotKey))
                    {
                        float relative_center_x = (float)(trainBox.X + trainBox.Width / 2) / size.X;
                        float relative_center_y = (float)(trainBox.Y + trainBox.Height / 2) / size.Y;
                        float relative_width    = (float)trainBox.Width / size.X;
                        float relative_height   = (float)trainBox.Height / size.Y;
                        bitmap.Save($"darknet/data/img/{game}{i.ToString()}{rand}.png", System.Drawing.Imaging.ImageFormat.Png);
                        File.WriteAllText($"darknet/data/img/{game}{i.ToString()}{rand}.txt", string.Format("{0} {1} {2} {3} {4}", selectedObject, relative_center_x, relative_center_y, relative_width, relative_height).Replace(",", "."));
                        i++;
                        Console.Beep();
                    }
                    if (isKeyToggled(Keys.Back))
                    {
                        bitmap.Save($"darknet/data/img/{game}{i.ToString()}{rand}.png", System.Drawing.Imaging.ImageFormat.Png);
                        File.WriteAllText($"darknet/data/img/{game}{i.ToString()}{rand}.txt", "");
                        i++;
                        Console.Beep();
                    }

                    if (isKeyToggled(Keys.End))
                    {
                        gfx.ClearScene();
                        gfx.EndScene();

                        Console.WriteLine("Okay, nós temos as fotos para treinamento. Vamos treinar a Neural Network....");
                        File.WriteAllText($"darknet/{game}.cfg", File.ReadAllText($"darknet/{game}.cfg").Replace("NUMBER", objects.Count().ToString()).Replace("FILTERNUM", ((objects.Count() + 5) * 3).ToString()));
                        File.WriteAllText($"darknet/{game}.cfg", File.ReadAllText($"darknet/{game}.cfg").Replace("batch=1", "batch=64").Replace("subdivisions=1", "subdivisions=8"));
                        File.WriteAllText($"darknet/data/{game}.data", File.ReadAllText($"darknet/data/{game}.data").Replace("NUMBER", objects.Count().ToString()).Replace("GAME", game));
                        File.WriteAllText($"darknet/{game}.cmd", File.ReadAllText($"darknet/{game}.cmd").Replace("GAME", game));
                        File.WriteAllText($"darknet/{game}_trainmore.cmd", File.ReadAllText($"darknet/{game}_trainmore.cmd").Replace("GAME", game));
                        File.WriteAllText($"darknet/data/{game}.names", string.Join("\n", objects));
                        // DirectoryInfo d = ;//Assuming Test is your Folder
                        FileInfo[] Files     = new DirectoryInfo(Application.StartupPath + @"\darknet\data\img").GetFiles($"{game}*.png"); //Getting Text files
                        string     PathOfImg = "";
                        foreach (FileInfo file in Files)
                        {
                            PathOfImg += $"data/img/{file.Name}\r\n";
                        }

                        File.WriteAllText($"darknet/data/{game}.txt", PathOfImg);
                        DialogResult dialogResult = MessageBox.Show($"O treinamento vai exigir da sua gpu. Quer fechar o jogo?", "Fechar jogo", MessageBoxButtons.YesNo);
                        if (dialogResult == DialogResult.Yes)
                        {
                            Process.GetProcessesByName(game)[0].Kill();
                        }
                        if (File.Exists($"trainfiles/{game}.weights"))
                        {
                            File.Copy($"trainfiles/{game}.weights", $"darknet/{game}.weights", true);
                            Process.Start("cmd", @"/C cd " + Application.StartupPath + $"/darknet/ & {game}_trainmore.cmd");
                        }
                        else
                        {
                            Process.Start("cmd", @"/C cd " + Application.StartupPath + $"/darknet/ & {game}.cmd");
                        }

                        Console.WriteLine("^Quando terminar o treinamento digite \"done\" no console.");

                        while (true)
                        {
                            if (Console.ReadLine() == "done")
                            {
                                File.Copy($"darknet/data/backup/{game}_last.weights", $"trainfiles/{game}.weights", true);
                                File.Copy($"darknet/data/{game}.names", $"trainfiles/{game}.names", true);
                                File.Copy($"darknet/{game}.cfg", $"trainfiles/{game}.cfg", true);
                                File.WriteAllText($"trainfiles/{game}.cfg", File.ReadAllText($"trainfiles/{game}.cfg").Replace("batch=64", "batch=1").Replace("subdivisions=8", "subdivisions=1"));
                                yoloWrapper  = new YoloWrapper($"trainfiles/{game}.cfg", $"trainfiles/{game}.weights", $"trainfiles/{game}.names");
                                trainingMode = false;
                                break;
                            }
                            else
                            {
                                Console.WriteLine("Quando terminar o treinamento digite \"done\" no console.");
                            }
                        }
                        Console.WriteLine("Okay! O treinamento esta finalizado. Vamos testar agora no game!");
                        gfx.BeginScene();
                    }
                }
                else
                {
                    if (isKeyToggled(Keys.PageUp))
                    {
                        selectedObject = (selectedObject + 1) % objects.Count();
                    }

                    if (isKeyToggled(Keys.Up))
                    {
                        SmoothAim = Math.Min(SmoothAim + 0.05f, 1);
                    }

                    if (isKeyToggled(Keys.Down))
                    {
                        SmoothAim = Math.Max(SmoothAim - 0.05f, 0);
                    }

                    if (isKeyToggled(Keys.Delete))
                    {
                        Head = !Head;
                    }

                    if (isKeyToggled(Keys.Home))
                    {
                        shooting  = 0;
                        SimpleRCS = !SimpleRCS;
                    }
                    if (isKeyToggled(Keys.End))
                    {
                        AutoShoot = !AutoShoot;
                    }
                    if (isKeyToggled(Keys.PageDown))
                    {
                        selectedObject = (selectedObject - 1 + objects.Count()) % objects.Count();
                    }
                    gfx.DrawText(_graphics.CreateFont("Arial", 10), redbrush, new GameOverlay.Drawing.Point(0, 0),
                                 $"Object {objects[selectedObject]};" +
                                 $"SmoothAim {Math.Round(SmoothAim, 2)};" +
                                 $"Head {Head};" +
                                 $"SimpleRCS {SimpleRCS};" + Environment.NewLine +
                                 $"AutoShoot {AutoShoot};" +
                                 $"CursorToCenter: {CursorToCenter}");

                    using (MemoryStream ms = new MemoryStream())
                    {
                        bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        IEnumerable <Alturos.Yolo.Model.YoloItem> items = yoloWrapper.Detect(ms.ToArray());
                        if (SimpleRCS)
                        {
                            if (User32.GetAsyncKeyState(AimKey) == 0)
                            {
                                shooting = 0;
                            }
                        }

                        if (items.Count() > 0)
                        {
                            foreach (var item in items)
                            {
                                if (item.Confidence > 0.4d)
                                {
                                    Rectangle head = Rectangle.Create(item.X + Convert.ToInt32(item.Width / 2.9), item.Y, Convert.ToInt32(item.Width / 3), item.Height / 7);
                                    Rectangle body = Rectangle.Create(item.X + Convert.ToInt32(item.Width / 6), item.Y + item.Height / 6, Convert.ToInt32(item.Width / 1.5f), item.Height / 3);

                                    if (Information)
                                    {
                                        if (Head)
                                        {
                                            gfx.DrawTextWithBackground(_graphics.CreateFont("Arial", 12), redbrush, _graphics.CreateSolidBrush(0, 0, 0), new GameOverlay.Drawing.Point(item.X + item.Width, item.Y + item.Width), $"{item.Type} {DistanceBetweenCross(head.Left + head.Width / 2, head.Top + head.Height / 2)}");
                                        }
                                        else
                                        {
                                            gfx.DrawTextWithBackground(_graphics.CreateFont("Arial", 12), redbrush, _graphics.CreateSolidBrush(0, 0, 0), new GameOverlay.Drawing.Point(item.X + item.Width, item.Y + item.Width), $"{item.Type} {DistanceBetweenCross(body.Left + body.Width / 2, body.Top + body.Height / 2)}");
                                        }
                                    }

                                    if (item.Type == objects[selectedObject])
                                    {
                                        gfx.DrawRectangle(redbrush, Rectangle.Create(item.X, item.Y, item.Width, item.Height), 2);

                                        if (Head)
                                        {
                                            gfx.DrawRectangle(bluebrush, head, 2);
                                            gfx.DrawCrosshair(bluebrush, head.Left + head.Width / 2, head.Top + head.Height / 2 + Convert.ToInt32(1 * shooting), 2, 2, CrosshairStyle.Cross);
                                            gfx.DrawLine(bluebrush, size.X / 2, size.Y / 2, head.Left + head.Width / 2, head.Top + head.Height / 2 + Convert.ToInt32(1 * shooting), 2);
                                        }
                                        else
                                        {
                                            gfx.DrawRectangle(bluebrush, body, 2);
                                            gfx.DrawCrosshair(bluebrush, body.Left + body.Width / 2, body.Top + body.Height / 2 + Convert.ToInt32(1 * shooting), 2, 2, CrosshairStyle.Cross);
                                            gfx.DrawLine(bluebrush, size.X / 2, size.Y / 2, body.Left + body.Width / 2, body.Top + body.Height / 2 + Convert.ToInt32(1 * shooting), 2);
                                        }

                                        if (isKeyPressed(AimKey))
                                        {
                                            if (Head)
                                            {
                                                Alturos.Yolo.Model.YoloItem nearestEnemy = items.Where(x => x.Type == objects[selectedObject]).OrderByDescending(x => DistanceBetweenCross(x.X + Convert.ToInt32(x.Width / 2.9) + (x.Width / 3) / 2, x.Y + (x.Height / 7) / 2)).Last();

                                                Rectangle nearestEnemyHead = Rectangle.Create(nearestEnemy.X + Convert.ToInt32(nearestEnemy.Width / 2.9), nearestEnemy.Y, Convert.ToInt32(nearestEnemy.Width / 3), nearestEnemy.Height / 7 + (float)2 * shooting);

                                                if (SmoothAim <= 0)
                                                {
                                                    User32.mouse_event(0x01, Convert.ToInt32(((nearestEnemyHead.Left - size.X / 2) + (nearestEnemyHead.Width / 2))), Convert.ToInt32((nearestEnemyHead.Top - size.Y / 2 + nearestEnemyHead.Height / 7 + 1 * shooting)), 0, (UIntPtr)0);
                                                    if (AimKey != Keys.LButton && AutoShoot)
                                                    {
                                                        User32.mouse_event(0x02, 0, 0, 0, (UIntPtr)0);
                                                        User32.mouse_event(0x04, 0, 0, 0, (UIntPtr)0);
                                                        if (SimpleRCS)
                                                        {
                                                            shooting += 2;
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    if (size.X / 2 < nearestEnemyHead.Left | size.X / 2 > nearestEnemyHead.Right
                                                        | size.Y / 2 < nearestEnemyHead.Top | size.Y / 2 > nearestEnemyHead.Bottom)
                                                    {
                                                        User32.mouse_event(0x01, Convert.ToInt32(((nearestEnemyHead.Left - size.X / 2) + (nearestEnemyHead.Width / 2)) * SmoothAim), Convert.ToInt32((nearestEnemyHead.Top - size.Y / 2 + nearestEnemyHead.Height / 7 + 1 * shooting) * SmoothAim), 0, (UIntPtr)0);
                                                    }
                                                    else
                                                    {
                                                        if (AimKey != Keys.LButton && AutoShoot)
                                                        {
                                                            User32.mouse_event(0x02, 0, 0, 0, (UIntPtr)0);
                                                            User32.mouse_event(0x04, 0, 0, 0, (UIntPtr)0);
                                                        }
                                                        if (SimpleRCS)
                                                        {
                                                            shooting += 2;
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                Alturos.Yolo.Model.YoloItem nearestEnemy = items.Where(x => x.Type == objects[selectedObject]).OrderByDescending(x => DistanceBetweenCross(x.X + Convert.ToInt32(x.Width / 6) + (x.Width / 1.5f) / 2, x.Y + x.Height / 6 + (x.Height / 3) / 2)).Last();

                                                Rectangle nearestEnemyBody = Rectangle.Create(nearestEnemy.X + Convert.ToInt32(nearestEnemy.Width / 6), nearestEnemy.Y + nearestEnemy.Height / 6 + (float)2 * shooting, Convert.ToInt32(nearestEnemy.Width / 1.5f), nearestEnemy.Height / 3 + (float)2 * shooting);
                                                if (SmoothAim <= 0)
                                                {
                                                    User32.mouse_event(0x01, Convert.ToInt32(((nearestEnemyBody.Left - size.X / 2) + (nearestEnemyBody.Width / 2))), Convert.ToInt32((nearestEnemyBody.Top - size.Y / 2 + nearestEnemyBody.Height / 7 + 1 * shooting)), 0, (UIntPtr)0);
                                                    if (AimKey != Keys.LButton && AutoShoot)
                                                    {
                                                        User32.mouse_event(0x02, 0, 0, 0, (UIntPtr)0);
                                                        User32.mouse_event(0x04, 0, 0, 0, (UIntPtr)0);
                                                        if (SimpleRCS)
                                                        {
                                                            shooting += 2;
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    if (size.X / 2 < nearestEnemyBody.Left | size.X / 2 > nearestEnemyBody.Right
                                                        | size.Y / 2 < nearestEnemyBody.Top | size.Y / 2 > nearestEnemyBody.Bottom)
                                                    {
                                                        User32.mouse_event(0x01, Convert.ToInt32(((nearestEnemyBody.Left - size.X / 2) + (nearestEnemyBody.Width / 2)) * SmoothAim), Convert.ToInt32((nearestEnemyBody.Top - size.Y / 2 + nearestEnemyBody.Height / 7 + 1 * shooting) * SmoothAim), 0, (UIntPtr)0);
                                                    }
                                                    else
                                                    {
                                                        if (AimKey != Keys.LButton && AutoShoot)
                                                        {
                                                            User32.mouse_event(0x02, 0, 0, 0, (UIntPtr)0);
                                                            User32.mouse_event(0x04, 0, 0, 0, (UIntPtr)0);
                                                        }
                                                        if (SimpleRCS)
                                                        {
                                                            shooting += 2;
                                                        }
                                                    }
                                                }
                                            }
                                            //System.Threading.Thread.Sleep(120);
                                        }
                                    }
                                    else
                                    {
                                        gfx.DrawRectangle(greenbrush, Rectangle.Create(item.X, item.Y, item.Width, item.Height), 2);
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (SimpleRCS)
                            {
                                shooting = 0;
                            }
                        }
                    }
                }
                gfx.FillRectangle(bluebrush, Rectangle.Create(size.X / 2, size.Y / 2, 4, 4));

                gfx.EndScene();
            }
        }