internal PBEPokemon(PBETeam team, byte id, PBEPokemonShell shell)
        {
            Team    = team;
            Id      = id;
            Species = OriginalSpecies = KnownSpecies = shell.Species;
            var pData = PBEPokemonData.GetData(Species);

            KnownType1       = Type1 = pData.Type1;
            KnownType2       = Type2 = pData.Type2;
            KnownWeight      = Weight = pData.Weight;
            Nickname         = KnownNickname = shell.Nickname;
            Level            = shell.Level;
            Friendship       = shell.Friendship;
            Shiny            = KnownShiny = shell.Shiny;
            Ability          = OriginalAbility = shell.Ability;
            KnownAbility     = PBEAbility.MAX;
            Nature           = shell.Nature;
            Gender           = KnownGender = shell.Gender;
            Item             = OriginalItem = shell.Item;
            KnownItem        = (PBEItem)ushort.MaxValue;
            EffortValues     = new PBEEffortValues(shell.EffortValues);
            IndividualValues = new PBEIndividualValues(shell.IndividualValues);
            SetStats();
            HP                   = MaxHP;
            HPPercentage         = 1D;
            OriginalMoveset      = new PBEMoveset(shell.Moveset);
            Moves                = new PBEBattleMoveset(OriginalMoveset);
            KnownMoves           = new PBEBattleMoveset(Team.Battle.Settings);
            TransformBackupMoves = new PBEBattleMoveset(Team.Battle.Settings);
            team.Party.Add(this);
        }
Exemple #2
0
        // Stats & PP are set from the shell info
        public PBEPokemon(PBETeam team, byte id, PBEPokemonShell shell)
        {
            Team = team;
            SelectedAction.PokemonId = Id = id;
            Shell          = shell;
            VisualGender   = Shell.Gender;
            VisualNickname = Shell.Nickname;
            VisualShiny    = Shell.Shiny;
            VisualSpecies  = Shell.Species;
            Ability        = Shell.Ability;
            Item           = Shell.Item;
            CalculateStats();
            HP           = MaxHP;
            HPPercentage = 1.0;
            Moves        = Shell.Moves;
            PP           = new byte[Moves.Length];
            MaxPP        = new byte[Moves.Length];
            for (int i = 0; i < Moves.Length; i++)
            {
                PBEMove move = Shell.Moves[i];
                if (move != PBEMove.None)
                {
                    byte tier = PBEMoveData.Data[move].PPTier;
                    PP[i] = MaxPP[i] = (byte)Math.Max(1, (tier * Team.Battle.Settings.PPMultiplier) + (tier * Shell.PPUps[i]));
                }
            }
            PBEPokemonData pData = PBEPokemonData.Data[Shell.Species];

            Type1  = pData.Type1;
            Type2  = pData.Type2;
            Weight = pData.Weight;
            Team.Party.Add(this);
        }
        /// <summary>Creates an array of <see cref="PBEPokemonShell"/>s each with completely random properties. The amount to create is <see cref="PBESettings.MaxPartySize"/>.</summary>
        /// <param name="settings">The settings to use.</param>
        /// <param name="setToMaxLevel">True if <see cref="PBEPokemonShell.Level"/> will be set to <see cref="PBESettings.MaxLevel"/>.</param>
        public static PBEPokemonShell[] CreateCompletelyRandomTeam(PBESettings settings, bool setToMaxLevel)
        {
            var team = new PBEPokemonShell[settings.MaxPartySize];

            for (int i = 0; i < settings.MaxPartySize; i++)
            {
                team[i] = new PBEPokemonShell(RandomSpecies(), setToMaxLevel ? settings.MaxLevel : (byte)RandomInt(settings.MinLevel, settings.MaxLevel), settings);
            }
            return(team);
        }
Exemple #4
0
        public static PBEBattle LoadReplay(string path)
        {
            PBESettings settings = PBESettings.DefaultSettings;

            byte[] fileBytes = File.ReadAllBytes(path);
            using (var s = new MemoryStream(fileBytes))
                using (var r = new BinaryReader(s))
                {
                    using (var md5 = MD5.Create())
                    {
                        byte[] hash = md5.ComputeHash(fileBytes, 0, fileBytes.Length - 16);
                        for (int i = 0; i < 16; i++)
                        {
                            if (hash[i] != fileBytes[fileBytes.Length - 16 + i])
                            {
                                throw new InvalidDataException();
                            }
                        }
                    }

                    ushort version = r.ReadUInt16();

                    var battle = new PBEBattle((PBEBattleFormat)r.ReadByte(), settings);

                    battle.Teams[0].TrainerName = PBEUtils.StringFromBytes(r);
                    var party = new PBEPokemonShell[r.ReadSByte()];
                    for (int i = 0; i < party.Length; i++)
                    {
                        party[i] = PBEPokemonShell.FromBytes(r, settings);
                    }
                    battle.Teams[0].CreateParty(party, ref battle.pkmnIdCounter);

                    battle.Teams[1].TrainerName = PBEUtils.StringFromBytes(r);
                    party = new PBEPokemonShell[r.ReadSByte()];
                    for (int i = 0; i < party.Length; i++)
                    {
                        party[i] = PBEPokemonShell.FromBytes(r, settings);
                    }
                    battle.Teams[1].CreateParty(party, ref battle.pkmnIdCounter);

                    var        packetProcessor = new PBEPacketProcessor(battle);
                    INetPacket packet;
                    do
                    {
                        byte[] buffer = r.ReadBytes(r.ReadInt16());
                        packet = packetProcessor.CreatePacket(buffer);
                        battle.Events.Add(packet);
                    } while (!(packet is PBEWinnerPacket));

                    return(battle);
                }
        }
Exemple #5
0
 public PBEPartyResponsePacket(byte[] buffer, PBEBattle battle)
 {
     Buffer = buffer;
     using (var r = new BinaryReader(new MemoryStream(buffer)))
     {
         r.ReadInt16(); // Skip Code
         var party = new PBEPokemonShell[r.ReadSByte()];
         for (int i = 0; i < party.Length; i++)
         {
             party[i] = PBEPokemonShell.FromBytes(r, battle.Settings); // What happens if an exception occurs? Similar question to https://github.com/Kermalis/PokemonBattleEngine/issues/167
         }
         Party = Array.AsReadOnly(party);
     }
 }
 public PBEPartyResponsePacket(byte[] buffer, PBEBattle battle)
 {
     Buffer = buffer;
     using (var r = new BinaryReader(new MemoryStream(buffer)))
     {
         r.ReadInt16(); // Skip Code
         var party = new PBEPokemonShell[r.ReadByte()];
         for (int i = 0; i < party.Length; i++)
         {
             party[i] = PBEPokemonShell.FromBytes(r);
         }
         Party = Array.AsReadOnly(party);
     }
 }
        // Stats & PP are set from the shell info
        internal PBEPokemon(PBETeam team, byte id, PBEPokemonShell shell)
        {
            Team = team;
            SelectedAction.PokemonId = Id = id;
            Shell        = shell;
            Ability      = OriginalAbility = Shell.Ability;
            KnownAbility = PBEAbility.MAX;
            Gender       = KnownGender = Shell.Gender;
            Item         = Shell.Item;
            KnownItem    = (PBEItem)ushort.MaxValue;
            Moves        = Shell.Moveset.MoveSlots.Select(m => m.Move).ToArray();
            KnownMoves   = new PBEMove[Team.Battle.Settings.NumMoves];
            for (int i = 0; i < Team.Battle.Settings.NumMoves; i++)
            {
                KnownMoves[i] = PBEMove.MAX;
            }
            Nickname = KnownNickname = Shell.Nickname;
            Shiny    = KnownShiny = Shell.Shiny;
            Species  = OriginalSpecies = KnownSpecies = Shell.Species;
            var pData = PBEPokemonData.GetData(Species);

            KnownType1       = Type1 = pData.Type1;
            KnownType2       = Type2 = pData.Type2;
            KnownWeight      = Weight = pData.Weight;
            EffortValues     = new PBEEffortValueCollection(team.Battle.Settings, shell.EffortValues);
            IndividualValues = new PBEIndividualValueCollection(team.Battle.Settings, shell.IndividualValues);
            Friendship       = Shell.Friendship;
            Level            = Shell.Level;
            Nature           = Shell.Nature;
            SetStats();
            HP           = MaxHP;
            HPPercentage = 1.0;
            PP           = new byte[team.Battle.Settings.NumMoves];
            MaxPP        = new byte[team.Battle.Settings.NumMoves];
            for (int i = 0; i < team.Battle.Settings.NumMoves; i++)
            {
                PBEMove move = Moves[i];
                if (move != PBEMove.None)
                {
                    byte tier = PBEMoveData.Data[move].PPTier;
                    PP[i] = MaxPP[i] = (byte)Math.Max(1, (tier * team.Battle.Settings.PPMultiplier) + (tier * Shell.Moveset.MoveSlots[i].PPUps));
                }
            }
            team.Party.Add(this);
        }
Exemple #8
0
        // This constructor is to define a remote Pokémon
        public PBEPokemon(PBETeam team, PBEPkmnSwitchInPacket.PBESwitchInInfo info)
        {
            Team  = team;
            Id    = info.PokemonId;
            Shell = new PBEPokemonShell
            {
                Species  = VisualSpecies = info.Species,
                Shiny    = VisualShiny = info.Shiny,
                Nickname = VisualNickname = info.Nickname,
                Level    = info.Level,
                Gender   = VisualGender = info.Gender,
                Ability  = Ability = PBEAbility.MAX,
                Item     = Item = (PBEItem)ushort.MaxValue,
                Nature   = PBENature.MAX,
                Moves    = new PBEMove[Team.Battle.Settings.NumMoves],
                PPUps    = new byte[Team.Battle.Settings.NumMoves],
                EVs      = new byte[6],
                IVs      = new byte[6]
            };
            Moves = new PBEMove[Shell.Moves.Length];
            PP    = new byte[Moves.Length];
            MaxPP = new byte[Moves.Length];
            for (int i = 0; i < Moves.Length; i++)
            {
                Shell.Moves[i] = Moves[i] = PBEMove.MAX;
            }
            FieldPosition = info.FieldPosition;
            HP            = info.HP;
            MaxHP         = info.MaxHP;
            HPPercentage  = info.HPPercentage;
            Status1       = info.Status1;
            PBEPokemonData pData = PBEPokemonData.Data[Shell.Species];

            Type1  = pData.Type1;
            Type2  = pData.Type2;
            Weight = pData.Weight;
            Team.Party.Add(this);
        }
Exemple #9
0
 public static Uri GetPokemonSpriteUri(PBEPokemonShell shell)
 {
     return(GetPokemonSpriteUri(shell.Species, shell.Shiny, shell.Gender, false, false));
 }
 public static Stream GetPokemonSpriteStream(PBEPokemonShell shell)
 {
     return(GetPokemonSpriteStream(shell.Species, shell.Shiny, shell.Gender, false, false));
 }
 internal static PBEPokemon FromBytes(BinaryReader r, PBETeam team)
 {
     return(new PBEPokemon(team, r.ReadByte(), PBEPokemonShell.FromBytes(r, team.Battle.Settings)));
 }
Exemple #12
0
        public void Basic_Actions_Checks()
        {
            PBEUtils.CreateDatabaseConnection(string.Empty);
            PBESettings settings = PBESettings.DefaultSettings;

            var             team1Shell = new PBETeamShell(settings, 2, true);
            PBEPokemonShell p          = team1Shell[0];

            p.Species         = PBESpecies.Koffing;
            p.Item            = PBEItem.None;
            p.Moveset[0].Move = PBEMove.Selfdestruct;

            var team2Shell = new PBETeamShell(settings, 1, true);

            p                 = team2Shell[0];
            p.Species         = PBESpecies.Darkrai;
            p.Item            = PBEItem.None;
            p.Moveset[0].Move = PBEMove.Protect;

            var battle = new PBEBattle(PBEBattleFormat.Single, team1Shell, "Team 1", team2Shell, "Team 2");

            team1Shell.Dispose();
            team2Shell.Dispose();
            battle.Begin();

            PBETeam t  = battle.Teams[0];
            var     a  = new PBETurnAction(t.Party[0].Id, PBEMove.Selfdestruct, PBETurnTarget.FoeCenter);
            var     a1 = new PBETurnAction[] { a };

            Assert.Throws <ArgumentNullException>(() => PBEBattle.SelectActionsIfValid(null, a1));                        // Throw for null team
            Assert.Throws <ArgumentNullException>(() => PBEBattle.SelectActionsIfValid(t, null));                         // Throw for null collection
            Assert.Throws <ArgumentNullException>(() => PBEBattle.SelectActionsIfValid(t, new PBETurnAction[] { null })); // Throw for null elements
            Assert.False(PBEBattle.SelectActionsIfValid(t, new PBETurnAction[] { a, a }));                                // False for too many actions
            Assert.True(PBEBattle.SelectActionsIfValid(t, a1));                                                           // True for good actions
            // TODO: bad move, bad targets, bad targets with templockedmove, battle status, bad pkmn id, wrong team pkmn id, duplicate pkmn id, can't switch out but tried, invalid switch mon (null hp pos), duplicate switch mon
            Assert.False(PBEBattle.SelectActionsIfValid(t, a1));                                                          // False because actions were already submitted
            Assert.False(PBEBattle.SelectActionsIfValid(t, Array.Empty <PBETurnAction>()));                               // False for 0 despite us now needing 0 additional actions

            t = battle.Teams[1];
            Assert.True(PBEBattle.SelectActionsIfValid(t, new PBETurnAction[] { new PBETurnAction(t.Party[0].Id, PBEMove.Protect, PBETurnTarget.AllyCenter) })); // True for good actions

            battle.RunTurn();                                                                                                                                    // Darkrai uses Protect, Koffing uses Selfdestruct, Koffing faints

            t = battle.Teams[0];
            var s  = new PBESwitchIn(t.Party[1].Id, PBEFieldPosition.Center);
            var s1 = new PBESwitchIn[] { s };

            Assert.Throws <ArgumentNullException>(() => PBEBattle.SelectSwitchesIfValid(null, s1));                      // Throw for null team
            Assert.Throws <ArgumentNullException>(() => PBEBattle.SelectSwitchesIfValid(t, null));                       // Throw for null collection
            Assert.Throws <ArgumentNullException>(() => PBEBattle.SelectSwitchesIfValid(t, new PBESwitchIn[] { null })); // Throw for null elements
            Assert.False(PBEBattle.SelectSwitchesIfValid(t, new PBESwitchIn[] { s, s }));                                // False for too many
            Assert.True(PBEBattle.SelectSwitchesIfValid(t, s1));                                                         // True for good switches
            // Below two wouldn't work because of battle status lol
            //Assert.False(PBEBattle.SelectSwitchesIfValid(t, s1)); // False because switches were already submitted
            //Assert.False(PBEBattle.SelectSwitchesIfValid(t, Array.Empty<PBESwitchIn>())); // False for 0 despite us now needing 0 additional actions

            battle.Dispose();

            Assert.Throws <ObjectDisposedException>(() => PBEBattle.SelectActionsIfValid(t, a1));  // Throw for disposed battle
            Assert.Throws <ObjectDisposedException>(() => PBEBattle.SelectSwitchesIfValid(t, s1)); // Throw for disposed battle
        }