private static void Battle_OnNewEvent(PBEBattle battle, IPBEPacket packet)
        {
            try
            {
                switch (packet)
                {
                case PBEActionsRequestPacket arp:
                {
                    PBETeam         team    = arp.Team;
                    PBETurnAction[] actions = PBEAI.CreateActions(team);
                    if (!PBEBattle.AreActionsValid(team, actions))
                    {
                        throw new Exception($"{team.TrainerName}'s AI created invalid actions!");
                    }
                    PBEBattle.SelectActionsIfValid(team, actions);
                    break;
                }

                case PBESwitchInRequestPacket sirp:
                {
                    PBETeam       team     = sirp.Team;
                    PBESwitchIn[] switches = PBEAI.CreateSwitches(team);
                    if (!PBEBattle.AreSwitchesValid(team, switches))
                    {
                        throw new Exception($"{team.TrainerName}'s AI created invalid switches!");
                    }
                    PBEBattle.SelectSwitchesIfValid(team, switches);
                    break;
                }

                case PBETurnBeganPacket tbp:
                {
                    Console.SetOut(_oldWriter);
                    DateTime time = DateTime.Now;
                    Console.WriteLine($"Emulating turn {tbp.TurnNumber}... ({time.Hour:D2}:{time.Minute:D2}:{time.Second:D2}:{time.Millisecond:D3})");
                    Console.SetOut(_writer);
                    break;
                }
                }
            }
            catch (Exception e)
            {
                CatchException(e);
            }
        }
Exemple #2
0
        static void Battle_OnStateChanged(PBEBattle battle)
        {
            try
            {
                switch (battle.BattleState)
                {
                case PBEBattleState.Ended:
                {
                    Console.SetOut(oldWriter);
                    writer.Close();
                    Console.WriteLine($"Test battle ended. \"{logFile}\" contains the battle.");
                    Console.ReadKey();
                    break;
                }

                case PBEBattleState.ReadyToRunTurn:
                {
                    foreach (PBETeam team in battle.Teams)
                    {
                        Console.WriteLine();
                        Console.WriteLine("{0}'s team:", team.TrainerName);
                        foreach (PBEPokemon pkmn in team.ActiveBattlers)
                        {
                            Console.WriteLine(pkmn);
                            Console.WriteLine();
                        }
                    }
                    battle.RunTurn();
                    break;
                }

                case PBEBattleState.WaitingForActions:
                {
                    foreach (PBETeam team in battle.Teams)
                    {
                        IEnumerable <PBEAction> actions = AIManager.CreateActions(team);
                        bool valid = PBEBattle.AreActionsValid(team, actions);
                        if (!valid)
                        {
                            throw new Exception($"{team.TrainerName}'s AI created invalid actions!");
                        }
                        PBEBattle.SelectActionsIfValid(team, actions);
                    }
                    break;
                }

                case PBEBattleState.WaitingForSwitchIns:
                {
                    foreach (PBETeam team in battle.Teams.Where(t => t.SwitchInsRequired > 0))
                    {
                        IEnumerable <Tuple <byte, PBEFieldPosition> > switches = AIManager.CreateSwitches(team);
                        bool valid = PBEBattle.AreSwitchesValid(team, switches);
                        if (!valid)
                        {
                            throw new Exception($"{team.TrainerName}'s AI created invalid switches!");
                        }
                        PBEBattle.SelectSwitchesIfValid(team, switches);
                    }
                    break;
                }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                Console.SetOut(oldWriter);
                writer.Close();
                Console.WriteLine($"Test battle threw an exception, check \"{logFile}\" for details.");
                Console.ReadKey();
                Environment.Exit(-1);
            }
        }
        private static void Battle_OnStateChanged(PBEBattle battle)
        {
            try
            {
                switch (battle.BattleState)
                {
                case PBEBattleState.Ended:
                {
                    Console.SetOut(oldWriter);
                    writer.Close();
                    try
                    {
                        battle.SaveReplay(ReplayFile);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error saving replay:");
                        Console.WriteLine(e.Message);
                        Console.WriteLine(e.StackTrace);
                    }
                    Console.WriteLine("Test battle ended. The battle was saved to \"{0}\" and \"{1}\".", LogFile, ReplayFile);
                    Console.ReadKey();
                    break;
                }

                case PBEBattleState.ReadyToRunTurn:
                {
                    foreach (PBETeam team in battle.Teams)
                    {
                        Console.WriteLine();
                        Console.WriteLine("{0}'s team:", team.TrainerName);
                        foreach (PBEPokemon pkmn in team.ActiveBattlers)
                        {
                            Console.WriteLine(pkmn);
                            Console.WriteLine();
                        }
                    }
                    battle.RunTurn();
                    break;
                }

                case PBEBattleState.WaitingForActions:
                {
                    foreach (PBETeam team in battle.Teams)
                    {
                        IEnumerable <PBEAction> actions = PBEAIManager.CreateActions(team);
                        if (!PBEBattle.AreActionsValid(team, actions))
                        {
                            throw new Exception($"{team.TrainerName}'s AI created invalid actions!");
                        }
                        PBEBattle.SelectActionsIfValid(team, actions);
                    }
                    break;
                }

                case PBEBattleState.WaitingForSwitchIns:
                {
                    foreach (PBETeam team in battle.Teams.Where(t => t.SwitchInsRequired > 0))
                    {
                        IEnumerable <(byte PokemonId, PBEFieldPosition Position)> switches = PBEAIManager.CreateSwitches(team);
                        if (!PBEBattle.AreSwitchesValid(team, switches))
                        {
                            throw new Exception($"{team.TrainerName}'s AI created invalid switches!");
                        }
                        PBEBattle.SelectSwitchesIfValid(team, switches);
                    }
                    break;
                }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                Console.SetOut(oldWriter);
                writer.Close();
                Console.WriteLine("Test battle threw an exception, check \"{0}\" for details.", LogFile);
                Console.ReadKey();
                Environment.Exit(-1);
            }
        }