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);
            }
        }
        protected override bool ProcessPacket(IPBEPacket packet)
        {
            switch (packet)
            {
            case PBEActionsRequestPacket arp:
            {
                PBETrainer t = arp.Trainer;
                if (t == Trainer)
                {
                    ActionsLoop(true);
                }
                else
                {
                    new Thread(() => PBEBattle.SelectActionsIfValid(t, PBEAI.CreateActions(t)))
                    {
                        Name = "Battle Thread"
                    }.Start();
                }
                return(true);
            }

            case PBESwitchInRequestPacket sirp:
            {
                PBETrainer t = sirp.Trainer;
                if (t == Trainer)
                {
                    _switchesRequired = sirp.Amount;
                    SwitchesLoop(true);
                }
                else
                {
                    new Thread(() => PBEBattle.SelectSwitchesIfValid(t, PBEAI.CreateSwitches(t)))
                    {
                        Name = "Battle Thread"
                    }.Start();
                }
                return(true);
            }
            }
            return(base.ProcessPacket(packet));
        }
示例#3
0
        private bool ProcessPacket(IPBEPacket packet)
        {
            switch (packet)
            {
            case PBEMoveLockPacket _:
            case PBEMovePPChangedPacket _:
            case PBEIllusionPacket _:
            case PBETransformPacket _:
            case PBEBattlePacket _:
            case PBETurnBeganPacket _: return(true);

            case PBEActionsRequestPacket arp:
            {
                PBETrainer t = arp.Trainer;
                if (t == _trainer)
                {
                    ActionsLoop(true);
                }
                else
                {
                    new Thread(() => PBEBattle.SelectActionsIfValid(t, PBEAI.CreateActions(t)))
                    {
                        Name = ThreadName
                    }.Start();
                }
                return(true);
            }

            case PBESwitchInRequestPacket sirp:
            {
                PBETrainer t = sirp.Trainer;
                if (t == _trainer)
                {
                    _switchesRequired = sirp.Amount;
                    SwitchesLoop(true);
                }
                else
                {
                    new Thread(() => PBEBattle.SelectSwitchesIfValid(t, PBEAI.CreateSwitches(t)))
                    {
                        Name = ThreadName
                    }.Start();
                }
                return(true);
            }

                /*case PBEPkmnFaintedPacket pfp:
                 * {
                 *  PBEBattlePokemon pokemon = pfp.PokemonTrainer.TryGetPokemon(pfp.Pokemon);
                 *  BattleView.Field.HidePokemon(pokemon, pfp.OldPosition);
                 *  break;
                 * }
                 * case PBEPkmnFormChangedPacket pfcp:
                 * {
                 *  PBEBattlePokemon pokemon = pfcp.PokemonTrainer.TryGetPokemon(pfcp.Pokemon);
                 *  BattleView.Field.UpdatePokemon(pokemon, false, true);
                 *  break;
                 * }
                 * case PBEPkmnHPChangedPacket phcp:
                 * {
                 *  PBEBattlePokemon pokemon = phcp.PokemonTrainer.TryGetPokemon(phcp.Pokemon);
                 *  BattleView.Field.UpdatePokemon(pokemon, true, false);
                 *  break;
                 * }
                 * case PBEPkmnSwitchInPacket psip:
                 * {
                 *  if (!psip.Forced)
                 *  {
                 *      foreach (PBEPkmnSwitchInPacket.PBESwitchInInfo info in psip.SwitchIns)
                 *      {
                 *          BattleView.Field.ShowPokemon(psip.Trainer.TryGetPokemon(info.Pokemon));
                 *      }
                 *  }
                 *  break;
                 * }
                 * case PBEPkmnSwitchOutPacket psop:
                 * {
                 *  PBEBattlePokemon pokemon = psop.PokemonTrainer.TryGetPokemon(psop.Pokemon);
                 *  BattleView.Field.HidePokemon(pokemon, psop.OldPosition);
                 *  break;
                 * }
                 * case PBEStatus1Packet s1p:
                 * {
                 *  PBEBattlePokemon status1Receiver = s1p.Status1ReceiverTrainer.TryGetPokemon(s1p.Status1Receiver);
                 *  BattleView.Field.UpdatePokemon(status1Receiver, true, false);
                 *  break;
                 * }
                 * case PBEStatus2Packet s2p:
                 * {
                 *  PBEBattlePokemon status2Receiver = s2p.Status2ReceiverTrainer.TryGetPokemon(s2p.Status2Receiver);
                 *  switch (s2p.Status2)
                 *  {
                 *      case PBEStatus2.Airborne: BattleView.Field.UpdatePokemon(status2Receiver, false, true); break;
                 *      case PBEStatus2.Disguised:
                 *      {
                 *          switch (s2p.StatusAction)
                 *          {
                 *              case PBEStatusAction.Ended: BattleView.Field.UpdatePokemon(status2Receiver, true, true); break;
                 *          }
                 *          break;
                 *      }
                 *      case PBEStatus2.ShadowForce: BattleView.Field.UpdatePokemon(status2Receiver, false, true); break;
                 *      case PBEStatus2.Substitute:
                 *      {
                 *          switch (s2p.StatusAction)
                 *          {
                 *              case PBEStatusAction.Added:
                 *              case PBEStatusAction.Ended: BattleView.Field.UpdatePokemon(status2Receiver, false, true); break;
                 *          }
                 *          break;
                 *      }
                 *      case PBEStatus2.Transformed:
                 *      {
                 *          switch (s2p.StatusAction)
                 *          {
                 *              case PBEStatusAction.Added: BattleView.Field.UpdatePokemon(status2Receiver, false, true); break;
                 *          }
                 *          break;
                 *      }
                 *      case PBEStatus2.Underground: BattleView.Field.UpdatePokemon(status2Receiver, false, true); break;
                 *      case PBEStatus2.Underwater: BattleView.Field.UpdatePokemon(status2Receiver, false, true); break;
                 *  }
                 *  break;
                 * }
                 * case PBEWeatherPacket wp:
                 * {
                 *  switch (wp.WeatherAction)
                 *  {
                 *      case PBEWeatherAction.Added:
                 *      case PBEWeatherAction.Ended: BattleView.Field.UpdateWeather(); break;
                 *      case PBEWeatherAction.CausedDamage: break;
                 *  }
                 *  break;
                 * }
                 * case PBEAutoCenterPacket acp:
                 * {
                 *  PBEBattlePokemon pokemon0 = acp.Pokemon0Trainer.TryGetPokemon(acp0.Pokemon0);
                 *  PBEBattlePokemon pokemon1 = acp.Pokemon1Trainer.TryGetPokemon(acp1.Pokemon1);
                 *  BattleView.Field.MovePokemon(pokemon0, acp.Pokemon0OldPosition);
                 *  BattleView.Field.MovePokemon(pokemon1, acp.Pokemon1OldPosition);
                 *  break;
                 * }*/
            }
            string message = PBEBattle.GetDefaultMessage(_battle, packet, userTrainer: _trainer);

            if (string.IsNullOrEmpty(message))
            {
                return(true);
            }
            AddMessage(message);
            return(false);
        }