using System; using System.Collections.Generic; using PkmnLib.Static.Utils; namespace PkmnLib.Plugin.Gen7.Scripts.Status; [Script(ScriptCategory.Status, "sleep")] public class Sleep : Script { public int Turns { get; set; } /// public override void OnAddedToParent(IScriptSource source) { // Rare case where the script is added again. Can happen for example when baton pass is used. if (Turns != 0) return; if (source is not IPokemon pokemon) throw new InvalidOperationException("Sleep script can only be added to a Pokemon."); var battleData = pokemon.BattleData; if (battleData != null) { // 1-3 turns of sleep Turns = battleData.Battle.Random.GetInt(1, 4); } } /// public override void PreventMove(IExecutingMove move, ref bool prevent) { Turns--; if (Turns <= 0) { RemoveSelf(); move.User.BattleData?.Battle.EventHook.Invoke(new DialogEvent("pokemon_woke_up", new Dictionary { { "pokemon", move.User }, })); return; } if (move.UseMove.HasFlag("usable_while_asleep")) return; var bypass = false; var pars = new Dictionary { { "bypass_sleep", bypass }, }; move.RunScriptHook(x => x.CustomTrigger(CustomTriggers.BypassSleep, pars)); bypass = pars.GetValueOrDefault("bypass_sleep", false) as bool? ?? false; if (bypass) return; prevent = true; } }