Пример #1
0
        public static uint GeneratePID(int dex, Nature n, Gender g, bool shiny, bool secondability, ushort trainerID, ushort secretID)
        {
            byte genderval = Pokemon.GetBaseStats(dex, 0).GenderValue;

            if (genderval == 255 && g != Gender.Genderless)
            {
                return(0);
            }
            if (genderval == 254 && g != Gender.Female)
            {
                return(0);
            }
            if (genderval == 0 && g != Gender.Male)
            {
                return(0);
            }

lollabel:

            PokePRNG rand = new PokePRNG((uint)DateTime.Now.Ticks);


            ushort E = (ushort)(trainerID ^ secretID);
            ushort EF;

            if (shiny)
            {
                EF = (ushort)(rand.NextNum() & 0x7);
            }
            else
            {
                EF = (ushort)(rand.NextNum());
            }
            ushort F = (ushort)(EF ^ E);

            ushort p2 = 0;
            ushort p1 = 0;

            if (genderval == 255)              //genderless, randomize last byte
            {
                while (true)
                {
                    byte last = (byte)rand.NextNum();
                    if ((secondability && last % 2 != 0) || (!secondability && last % 2 == 0))
                    {
                        p2 = (ushort)(last + (ushort)((byte)rand.NextNum() << 8));
                        break;
                    }
                }
            }
            else               // gender
            {
                if (g == Gender.Male)
                {
                    while (true)
                    {
                        byte last = (byte)rand.NextInBounds(genderval, 256);
                        if ((secondability && last % 2 != 0) || (!secondability && last % 2 == 0))
                        {
                            p2 = (ushort)(last + (ushort)((byte)rand.NextNum() << 8));
                            break;
                        }
                    }
                }
                else if (g == Gender.Female)
                {
                    while (true)
                    {
                        byte last = (byte)rand.NextInBounds(1, genderval);
                        if ((secondability && last % 2 != 0) || (!secondability && last % 2 == 0))
                        {
                            p2 = (ushort)(last + (ushort)((byte)rand.NextNum() << 8));
                            break;
                        }
                    }
                }
            }

            p1 = (ushort)(F ^ p2);

            int pid = (p1 << 16) + p2;

            if (pid % 25 != (int)n)
            {
                goto lollabel;                     // http://xkcd.com/292/
            }
            // yes, I am lazy

            return((uint)pid);
        }