Пример #1
0
        /// <summary>
        /// Gets the difficulty of the AI in the input slot.
        /// <para>If the input slot is not an AI, returns null.
        /// If checking an AI's difficulty in the queue, it will always return easy, or null if it is a player.</para>
        /// </summary>
        /// <param name="slot">Slot to check</param>
        /// <param name="noUpdate"></param>
        /// <returns>Returns the if the difficulty is found. Returns null if the input slot is not an AI.</returns>
        /// <include file='docs.xml' path='doc/exceptions/invalidslot/exception'/>
        public Difficulty?GetAIDifficulty(int slot, bool noUpdate = false)
        {
            if (!CustomGame.IsSlotValid(slot))
            {
                throw new InvalidSlotException(slot);
            }

            if (slot == 5 && cg.OpenChatIsDefault)
            {
                cg.Chat.CloseChat();
            }

            if (!noUpdate)
            {
                cg.updateScreen();
            }

            if (CustomGame.IsSlotBlue(slot) || CustomGame.IsSlotRed(slot))
            {
                bool draw = cg.debugmode;                       // For debug mode

                List <int>        rl = new List <int>();        // Likelyhood in percent for difficulties.
                List <Difficulty> dl = new List <Difficulty>(); // Difficulty

                bool foundWhite      = false;
                int  foundWhiteIndex = 0;
                int  maxWhite        = 3;
                // For each check length in IsAILocations
                for (int xi = 0; xi < DifficultyLocations[slot, 2] && foundWhiteIndex < maxWhite; xi++)
                {
                    if (foundWhite)
                    {
                        foundWhiteIndex++;
                    }

                    Color cc = cg.GetPixelAt(DifficultyLocations[slot, 0] + xi, DifficultyLocations[slot, 1]);
                    // Check for white color of text
                    if (cg.CompareColor(DifficultyLocations[slot, 0] + xi, DifficultyLocations[slot, 1], Colors.WHITE, 110) &&
                        (slot > 5 || cc.B - cc.R < 20))
                    {
                        foundWhite = true;

                        // For each difficulty markup
                        for (int b = 0; b < DifficultyMarkups.Length; b++)
                        {
                            Bitmap tmp = null;
                            if (draw)
                            {
                                tmp = cg.BmpClone();
                            }

                            // Check if bitmap matches checking area
                            double success = 0;
                            double total   = 0;
                            for (int x = 0; x < DifficultyMarkups[b].Width; x++)
                            {
                                for (int y = DifficultyMarkups[b].Height - 1; y >= 0; y--)
                                {
                                    // If the color pixel of the markup is not white, check if valid.
                                    Color pc = DifficultyMarkups[b].GetPixel(x, y);
                                    if (pc != Color.FromArgb(255, 255, 255, 255))
                                    {
                                        // tc is true if the pixel is black, false if it is red.
                                        bool tc = pc == Color.FromArgb(255, 0, 0, 0);

                                        total++; // Indent the total
                                                 // If the checking color in the bmp bitmap is equal to the pc color, add to success.
                                        if (cg.CompareColor(DifficultyLocations[slot, 0] + xi + x, DifficultyLocations[slot, 1] - Extensions.InvertNumber(y, DifficultyMarkups[b].Height - 1), Colors.WHITE, 50) == tc)
                                        {
                                            success++;

                                            if (draw)
                                            {
                                                if (tc)
                                                {
                                                    tmp.SetPixel(DifficultyLocations[slot, 0] + xi + x, DifficultyLocations[slot, 1] - Extensions.InvertNumber(y, DifficultyMarkups[b].Height - 1), Color.Blue);
                                                }
                                                else
                                                {
                                                    tmp.SetPixel(DifficultyLocations[slot, 0] + xi + x, DifficultyLocations[slot, 1] - Extensions.InvertNumber(y, DifficultyMarkups[b].Height - 1), Color.Lime);
                                                }
                                            }
                                        }
                                        else if (draw)
                                        {
                                            if (tc)
                                            {
                                                tmp.SetPixel(DifficultyLocations[slot, 0] + xi + x, DifficultyLocations[slot, 1] - Extensions.InvertNumber(y, DifficultyMarkups[b].Height - 1), Color.Red);
                                            }
                                            else
                                            {
                                                tmp.SetPixel(DifficultyLocations[slot, 0] + xi + x, DifficultyLocations[slot, 1] - Extensions.InvertNumber(y, DifficultyMarkups[b].Height - 1), Color.Orange);
                                            }
                                        }

                                        if (draw)
                                        {
                                            tmp.SetPixel(DifficultyLocations[slot, 0] + xi + x, DifficultyLocations[slot, 1] - DifficultyMarkups[b].Height * 2 + y, DifficultyMarkups[b].GetPixel(x, y));
                                            cg.g.DrawImage(tmp, new Rectangle(0, -750, tmp.Width * 3, tmp.Height * 3));
                                            Thread.Sleep(1);
                                        }
                                    }
                                }
                            }
                            // Get the result
                            double result = (success / total) * 100;

                            rl.Add((int)result);
                            dl.Add((Difficulty)b);

                            if (draw)
                            {
                                tmp.SetPixel(DifficultyLocations[slot, 0] + xi, DifficultyLocations[slot, 1], Color.MediumPurple);
                                cg.g.DrawImage(tmp, new Rectangle(0, -750, tmp.Width * 3, tmp.Height * 3));
                                Console.WriteLine((Difficulty)b + " " + result);
                                Thread.Sleep(1000);
                            }
                        }
                    }
                }

                if (slot == 5 && cg.OpenChatIsDefault)
                {
                    cg.Chat.OpenChat();
                }

                // Return the difficulty that is most possible.
                if (rl.Count > 0)
                {
                    int max = rl.Max();
                    if (max >= 75)
                    {
                        return(dl[rl.IndexOf(max)]);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }

            else if (cg.QueueCount > 0)
            {
                int y = DifficultyLocationsQueue[slot - CustomGame.Queueid];
                for (int x = DifficultyLocationQueueX; x < 150 + DifficultyLocationQueueX; x++)
                {
                    if (cg.CompareColor(x, y, new int[] { 180, 186, 191 }, 10))
                    {
                        return(null);
                    }
                }
                return(Difficulty.Easy);
            }

            else
            {
                return(null);
            }
        }