From 7c270a6d52c412cd1951160bf5a75ce30ab0d457 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 6 Jul 2025 10:27:56 +0200 Subject: [PATCH] Finish script interface refactor --- .../BattleFlow/MoveTurnExecutor.cs | 81 +-- PkmnLib.Dynamic/BattleFlow/TurnRunner.cs | 20 +- PkmnLib.Dynamic/Models/Battle.cs | 19 +- PkmnLib.Dynamic/Models/BattleChoiceQueue.cs | 2 +- PkmnLib.Dynamic/Models/BattleRandom.cs | 4 +- PkmnLib.Dynamic/Models/BattleSide.cs | 13 +- PkmnLib.Dynamic/Models/Pokemon.cs | 40 +- PkmnLib.Dynamic/ScriptHandling/Script.cs | 564 +++++++++++------- .../ScriptHandling/ScriptExecution.cs | 86 +-- PkmnLib.Dynamic/ScriptHandling/ScriptSet.cs | 3 +- .../Battling/Gen7BattleStatCalculator.cs | 10 +- .../Libraries/Battling/Gen7CaptureLibrary.cs | 3 +- .../Battling/Gen7DamageCalculator.cs | 30 +- .../Scripts/Abilities/AuraBreak.cs | 4 +- .../Scripts/Abilities/Comatose.cs | 7 +- .../Scripts/Abilities/CompoundEyes.cs | 4 +- .../Scripts/Abilities/DarkAura.cs | 2 +- .../Scripts/Abilities/EarlyBird.cs | 4 +- .../Scripts/Abilities/FairyAura.cs | 2 +- .../Scripts/Abilities/FlowerGift.cs | 4 +- .../Scripts/Abilities/Forecast.cs | 4 +- .../Scripts/Abilities/HeavyMetal.cs | 4 +- .../Scripts/Abilities/Hustle.cs | 5 +- .../Scripts/Abilities/IceBody.cs | 4 +- .../Scripts/Abilities/Immunity.cs | 5 +- .../Scripts/Abilities/Infiltrator.cs | 4 +- .../Scripts/Abilities/InnerFocus.cs | 4 +- .../Scripts/Abilities/Insomnia.cs | 5 +- .../Scripts/Abilities/LeafGuard.cs | 5 +- .../Scripts/Abilities/Levitate.cs | 4 +- .../Scripts/Abilities/LightMetal.cs | 4 +- .../Scripts/Abilities/Limber.cs | 5 +- .../Scripts/Abilities/LiquidOoze.cs | 4 +- .../Scripts/Abilities/LongReach.cs | 5 +- .../Scripts/Abilities/MagmaArmor.cs | 5 +- .../Scripts/Abilities/MegaLauncher.cs | 4 +- .../Scripts/Abilities/Multitype.cs | 4 +- .../Scripts/Abilities/NoGuard.cs | 7 +- .../Scripts/Abilities/Oblivious.cs | 4 +- .../Scripts/Abilities/Overcoat.cs | 4 +- .../Scripts/Abilities/OwnTempo.cs | 4 +- .../Scripts/Abilities/PoisonHeal.cs | 4 +- .../Scripts/Abilities/Pressure.cs | 4 +- .../Scripts/Abilities/RKSSystem.cs | 4 +- .../Scripts/Abilities/RockHead.cs | 4 +- .../Scripts/Abilities/SandForce.cs | 4 +- .../Scripts/Abilities/SandRush.cs | 4 +- .../Scripts/Abilities/SandVeil.cs | 6 +- .../Scripts/Abilities/Scrappy.cs | 4 +- .../Scripts/Abilities/SkillLink.cs | 4 +- .../Scripts/Abilities/SnowCloak.cs | 6 +- .../Scripts/Abilities/Steadfast.cs | 4 +- .../Scripts/Abilities/StickyHold.cs | 4 +- .../Scripts/Abilities/SweetVeil.cs | 5 +- .../Scripts/Abilities/Synchronize.cs | 4 +- .../Scripts/Abilities/TangledFeet.cs | 4 +- .../Scripts/Abilities/Teravolt.cs | 6 +- .../Scripts/Abilities/VictoryStar.cs | 5 +- .../Scripts/Abilities/VitalSpirit.cs | 5 +- .../Scripts/Abilities/WaterBubble.cs | 5 +- .../Scripts/Abilities/WaterVeil.cs | 5 +- .../Scripts/Abilities/WonderSkin.cs | 4 +- .../Scripts/Battle/Gravity.cs | 7 +- .../Scripts/Battle/UproarEffect.cs | 6 +- .../MoveVolatile/BypassSleepVolatile.cs | 4 +- .../Scripts/Moves/AuroraVeil.cs | 2 +- .../PkmnLib.Plugin.Gen7/Scripts/Moves/Bind.cs | 2 +- .../Scripts/Moves/ChargeMove.cs | 2 +- .../Scripts/Moves/Drain.cs | 2 +- .../Scripts/Moves/DreamEater.cs | 2 +- .../Scripts/Moves/Feint.cs | 4 +- .../Scripts/Moves/FlareBlitz.cs | 2 +- .../Scripts/Moves/FlyingPress.cs | 4 +- .../Scripts/Moves/HealPercent.cs | 2 +- .../Scripts/Moves/LightScreen.cs | 2 +- .../Scripts/Moves/MultiHitMove.cs | 2 +- .../Scripts/Moves/OneHitKo.cs | 5 +- .../Scripts/Moves/PollenPuff.cs | 4 +- .../Scripts/Moves/Present.cs | 7 +- .../Scripts/Moves/Recoil.cs | 2 +- .../Scripts/Moves/Reflect.cs | 2 +- .../Scripts/Moves/SpectralThief.cs | 4 +- .../Scripts/Moves/SuckerPunch.cs | 4 +- .../Scripts/Moves/Whirlpool.cs | 2 +- .../Scripts/Pokemon/AutotomizeEffect.cs | 4 +- .../Scripts/Pokemon/ChargeBounceEffect.cs | 4 +- .../Scripts/Pokemon/ChargeFlyEffect.cs | 4 +- .../Scripts/Pokemon/ChargeSkyDropEffect.cs | 4 +- .../Scripts/Pokemon/DigEffect.cs | 5 +- .../Scripts/Pokemon/DiveEffect.cs | 5 +- .../Scripts/Pokemon/FlinchEffect.cs | 2 +- .../Scripts/Pokemon/ForesightEffect.cs | 4 +- .../Scripts/Pokemon/HealBlockEffect.cs | 5 +- .../Scripts/Pokemon/IngrainEffect.cs | 4 +- .../Scripts/Pokemon/LockOnEffect.cs | 5 +- .../Scripts/Pokemon/MiracleEyeEffect.cs | 4 +- .../Scripts/Pokemon/PhantomForceCharge.cs | 4 +- .../Scripts/Pokemon/ProtectionEffectScript.cs | 3 +- .../Scripts/Pokemon/RoostEffect.cs | 4 +- .../Scripts/Pokemon/ShadowForceCharge.cs | 4 +- .../Scripts/Pokemon/SmackDownEffect.cs | 6 +- .../Scripts/Pokemon/SubstituteEffect.cs | 3 +- .../Scripts/Pokemon/TelekinesisEffect.cs | 7 +- .../Scripts/Pokemon/ThousandArrowsEffect.cs | 6 +- .../Scripts/Side/FlowerVeilEffect.cs | 5 +- .../Scripts/Side/SafeguardEffect.cs | 7 +- .../Scripts/Status/Poisoned.cs | 2 +- .../Scripts/Status/Sleep.cs | 4 +- .../Scripts/Terrain/ElectricTerrain.cs | 7 +- .../Scripts/Terrain/MistyTerrain.cs | 5 +- .../Scripts/Weather/DesolateLands.cs | 5 +- .../Scripts/Weather/Hail.cs | 2 +- .../Scripts/Weather/HarshSunlight.cs | 11 +- .../Scripts/Weather/PrimordialSea.cs | 4 +- .../Scripts/Weather/Rain.cs | 5 +- .../Scripts/Weather/Sandstorm.cs | 3 +- .../Scripts/Weather/StrongWinds.cs | 6 +- 117 files changed, 669 insertions(+), 648 deletions(-) diff --git a/PkmnLib.Dynamic/BattleFlow/MoveTurnExecutor.cs b/PkmnLib.Dynamic/BattleFlow/MoveTurnExecutor.cs index 303373a..3537536 100644 --- a/PkmnLib.Dynamic/BattleFlow/MoveTurnExecutor.cs +++ b/PkmnLib.Dynamic/BattleFlow/MoveTurnExecutor.cs @@ -19,7 +19,7 @@ public static class MoveTurnExecutor var useMove = chosenMove.MoveData; var moveDataName = useMove.Name; - moveChoice.RunScriptHookInterface(x => x.ChangeMove(moveChoice, ref moveDataName)); + moveChoice.RunScriptHook(x => x.ChangeMove(moveChoice, ref moveDataName)); if (useMove.Name != moveDataName) { if (!battle.Library.StaticLibrary.Moves.TryGet(moveDataName, out useMove)) @@ -46,12 +46,12 @@ public static class MoveTurnExecutor moveChoice.Script.Clear(); } } - moveChoice.RunScriptHook(x => x.OnBeforeMoveChoice(moveChoice)); + moveChoice.RunScriptHook(x => x.OnBeforeMoveChoice(moveChoice)); var targetType = useMove.Target; var targets = TargetResolver.ResolveTargets(battle, moveChoice.TargetSide, moveChoice.TargetPosition, targetType); - moveChoice.RunScriptHookInterface(x => x.ChangeTargets(moveChoice, ref targets)); + moveChoice.RunScriptHook(x => x.ChangeTargets(moveChoice, ref targets)); if (targets.Count == 0) { moveChoice.Fail(); @@ -60,13 +60,11 @@ public static class MoveTurnExecutor foreach (var target in targets.WhereNotNull()) { - target.RunScriptHookInterface(x => - x.ChangeIncomingTargets(moveChoice, ref targets)); + target.RunScriptHook(x => x.ChangeIncomingTargets(moveChoice, ref targets)); } byte numberOfHits = 1; - moveChoice.RunScriptHookInterface(x => - x.ChangeNumberOfHits(moveChoice, ref numberOfHits)); + moveChoice.RunScriptHook(x => x.ChangeNumberOfHits(moveChoice, ref numberOfHits)); if (numberOfHits == 0) { return; @@ -77,18 +75,20 @@ public static class MoveTurnExecutor battle.EventHook.Invoke(new MoveUseEvent(executingMove)); var prevented = false; - executingMove.RunScriptHookInterface(x => x.PreventMove(executingMove, ref prevented)); + executingMove.RunScriptHook(x => x.PreventMove(executingMove, ref prevented)); if (prevented) return; byte ppUsed = 1; - executingMove.RunScriptHook(x => x.ModifyPPUsed(executingMove, ref ppUsed)); - targets.WhereNotNull().RunScriptHook(x => x.ModifyPPUsedForIncomingMove(executingMove, ref ppUsed)); + executingMove.RunScriptHook(x => x.ModifyPPUsed(executingMove, ref ppUsed)); + targets.WhereNotNull() + .RunScriptHook(x => + x.ModifyPPUsedForIncomingMove(executingMove, ref ppUsed)); if (!executingMove.ChosenMove.TryUse(ppUsed)) return; var failed = false; - executingMove.RunScriptHookInterface(x => x.FailMove(executingMove, ref failed)); + executingMove.RunScriptHook(x => x.FailMove(executingMove, ref failed)); if (failed) { // TODO: fail handling @@ -111,16 +111,16 @@ public static class MoveTurnExecutor public static void ExecuteMove(IExecutingMove executingMove) { var stopped = false; - executingMove.RunScriptHookInterface(x => x.StopBeforeMove(executingMove, ref stopped)); + executingMove.RunScriptHook(x => x.StopBeforeMove(executingMove, ref stopped)); if (stopped) return; - executingMove.RunScriptHookInterface(x => x.OnBeforeMove(executingMove)); + executingMove.RunScriptHook(x => x.OnBeforeMove(executingMove)); foreach (var target in executingMove.Targets.WhereNotNull()) { ExecuteMoveChoiceForTarget(executingMove.Battle, executingMove, target); } - executingMove.RunScriptHookInterface(x => x.OnAfterMove(executingMove)); + executingMove.RunScriptHook(x => x.OnAfterMove(executingMove)); } private static readonly ThreadLocal> TypeListCache = new(() => []); @@ -128,8 +128,7 @@ public static class MoveTurnExecutor private static void ExecuteMoveChoiceForTarget(IBattle battle, IExecutingMove executingMove, IPokemon target) { var failed = false; - target.RunScriptHookInterface(x => - x.FailIncomingMove(executingMove, target, ref failed)); + target.RunScriptHook(x => x.FailIncomingMove(executingMove, target, ref failed)); if (failed) { // TODO: fail handling @@ -137,7 +136,7 @@ public static class MoveTurnExecutor } var isInvulnerable = false; - target.RunScriptHookInterface(x => + target.RunScriptHook(x => x.IsInvulnerableToMove(executingMove, target, ref isInvulnerable)); if (isInvulnerable) { @@ -158,7 +157,7 @@ public static class MoveTurnExecutor break; var hitIndex = i; - executingMove.RunScriptHook(x => x.OnBeforeHit(executingMove, target, hitIndex)); + executingMove.RunScriptHook(x => x.OnBeforeHit(executingMove, target, hitIndex)); var hitData = (HitData)executingMove.GetDataFromRawIndex(targetHitStat + i); if (hitData.HasFailed) break; @@ -166,11 +165,12 @@ public static class MoveTurnExecutor var useMove = executingMove.UseMove; var isContact = useMove.HasFlag("contact"); - executingMove.RunScriptHook(x => x.ModifyIsContact(executingMove, target, hitIndex, ref isContact)); + executingMove.RunScriptHook(x => + x.ModifyIsContact(executingMove, target, hitIndex, ref isContact)); hitData.IsContact = isContact; var hitType = (TypeIdentifier?)useMove.MoveType; - executingMove.RunScriptHookInterface(x => + executingMove.RunScriptHook(x => x.ChangeMoveType(executingMove, target, hitIndex, ref hitType)); hitData.Type = hitType; @@ -180,22 +180,24 @@ public static class MoveTurnExecutor types.Clear(); types.AddRange(target.Types); - executingMove.RunScriptHook(x => x.ChangeTypesForMove(executingMove, target, hitIndex, types)); - target.RunScriptHook(x => x.ChangeTypesForIncomingMove(executingMove, target, hitIndex, types)); + executingMove.RunScriptHook(x => + x.ChangeTypesForMove(executingMove, target, hitIndex, types)); + target.RunScriptHook(x => + x.ChangeTypesForIncomingMove(executingMove, target, hitIndex, types)); var effectiveness = hitType == null ? 1 : battle.Library.StaticLibrary.Types.GetEffectiveness(hitType.Value, types); - executingMove.RunScriptHookInterface(x => + executingMove.RunScriptHook(x => x.ChangeEffectiveness(executingMove, target, hitIndex, ref effectiveness)); - target.RunScriptHookInterface(x => + target.RunScriptHook(x => x.ChangeIncomingEffectiveness(executingMove, target, hitIndex, ref effectiveness)); hitData.Effectiveness = effectiveness; var blockCritical = false; - executingMove.RunScriptHookInterface(x => + executingMove.RunScriptHook(x => x.BlockCriticalHit(executingMove, target, hitIndex, ref blockCritical)); - target.RunScriptHookInterface(x => + target.RunScriptHook(x => x.BlockIncomingCriticalHit(executingMove, target, hitIndex, ref blockCritical)); if (!blockCritical) { @@ -220,22 +222,23 @@ public static class MoveTurnExecutor if (accuracy < 100 && battle.Random.GetInt(100) >= accuracy) { - executingMove.RunScriptHookInterface(x => x.OnMoveMiss(executingMove, target)); + executingMove.RunScriptHook(x => x.OnMoveMiss(executingMove, target)); battle.EventHook.Invoke(new MoveMissEvent(executingMove)); break; } var blockIncomingHit = false; - target.RunScriptHookInterface(x => + target.RunScriptHook(x => x.BlockIncomingHit(executingMove, target, hitIndex, ref blockIncomingHit)); - executingMove.RunScriptHookInterface(x => + executingMove.RunScriptHook(x => x.BlockOutgoingHit(executingMove, target, hitIndex, ref blockIncomingHit)); if (blockIncomingHit) break; if (executingMove.GetHitData(target, hitIndex).HasFailed) break; var category = useMove.Category; - executingMove.RunScriptHook(x => x.ChangeCategory(executingMove, target, hitIndex, ref category)); + executingMove.RunScriptHook(x => + x.ChangeCategory(executingMove, target, hitIndex, ref category)); if (category == MoveCategory.Status) { var secondaryEffect = useMove.SecondaryEffect; @@ -244,7 +247,7 @@ public static class MoveTurnExecutor var chance = secondaryEffect.Chance; if (chance < 0 || battle.Random.EffectChance(chance, executingMove, target, hitIndex)) { - executingMove.RunScriptHookInterface(x => + executingMove.RunScriptHook(x => x.OnSecondaryEffect(executingMove, target, hitIndex)); } } @@ -268,12 +271,12 @@ public static class MoveTurnExecutor target.Damage(damage, DamageSource.MoveDamage, hitEventBatch); if (!target.IsFainted) { - target.RunScriptHookInterface(x => + target.RunScriptHook(x => x.OnIncomingHit(executingMove, target, hitIndex)); } else { - executingMove.RunScriptHookInterface(x => + executingMove.RunScriptHook(x => x.OnOpponentFaints(executingMove, target, hitIndex)); } @@ -283,9 +286,9 @@ public static class MoveTurnExecutor if (secondaryEffect != null) { var preventSecondary = false; - executingMove.RunScriptHookInterface(x => + executingMove.RunScriptHook(x => x.PreventSecondaryEffect(executingMove, target, hitIndex, ref preventSecondary)); - target.RunScriptHookInterface(x => + target.RunScriptHook(x => x.PreventIncomingSecondaryEffect(executingMove, target, hitIndex, ref preventSecondary)); @@ -294,14 +297,14 @@ public static class MoveTurnExecutor var chance = secondaryEffect.Chance; if (chance < 0 || battle.Random.EffectChance(chance, executingMove, target, hitIndex)) { - executingMove.RunScriptHookInterface(x => + executingMove.RunScriptHook(x => x.OnSecondaryEffect(executingMove, target, hitIndex)); } } } if (target.IsFainted) { - executingMove.RunScriptHookInterface(x => + executingMove.RunScriptHook(x => x.OnOpponentFaints(executingMove, target, hitIndex)); } } @@ -311,13 +314,13 @@ public static class MoveTurnExecutor if (numberOfHits == 0) { - target.RunScriptHookInterface(x => x.OnMoveMiss(executingMove, target)); + target.RunScriptHook(x => x.OnMoveMiss(executingMove, target)); battle.EventHook.Invoke(new MoveMissEvent(executingMove)); } if (!executingMove.User.IsFainted) { - executingMove.RunScriptHookInterface(x => x.OnAfterHits(executingMove, target)); + executingMove.RunScriptHook(x => x.OnAfterHits(executingMove, target)); } } } \ No newline at end of file diff --git a/PkmnLib.Dynamic/BattleFlow/TurnRunner.cs b/PkmnLib.Dynamic/BattleFlow/TurnRunner.cs index 4ded1e0..aed909b 100644 --- a/PkmnLib.Dynamic/BattleFlow/TurnRunner.cs +++ b/PkmnLib.Dynamic/BattleFlow/TurnRunner.cs @@ -31,7 +31,7 @@ public static class TurnRunner // they can then know this later on.) foreach (var choice in queue.GetChoices().WhereNotNull()) { - choice.RunScriptHookInterface(script => script.OnBeforeTurnStart(choice)); + choice.RunScriptHook(script => script.OnBeforeTurnStart(choice)); } // Now we can properly begin executing choices. @@ -60,15 +60,15 @@ public static class TurnRunner { scripts.Clear(); pokemon.GetOwnScripts(scripts); - scripts.RunScriptHookInterface(x => x.OnEndTurn(pokemon, battle)); + scripts.RunScriptHook(x => x.OnEndTurn(pokemon, battle)); } scripts.Clear(); side.GetOwnScripts(scripts); - scripts.RunScriptHookInterface(x => x.OnEndTurn(side, battle)); + scripts.RunScriptHook(x => x.OnEndTurn(side, battle)); } scripts.Clear(); battle.GetOwnScripts(scripts); - scripts.RunScriptHookInterface(x => x.OnEndTurn(battle, battle)); + scripts.RunScriptHook(x => x.OnEndTurn(battle, battle)); } } @@ -77,7 +77,7 @@ public static class TurnRunner /// public static void ExecuteChoice(IBattle battle, ITurnChoice choice) { - choice.RunScriptHook(x => x.ChangeTurnChoice(ref choice)); + choice.RunScriptHook(x => x.ChangeTurnChoice(ref choice)); if (choice is IPassChoice) return; if (battle.HasEnded) @@ -90,7 +90,7 @@ public static class TurnRunner { case IMoveChoice moveChoice: MoveTurnExecutor.ExecuteMoveChoice(battle, moveChoice); - moveChoice.RunScriptHook(script => script.OnAfterMoveChoice(moveChoice)); + moveChoice.RunScriptHook(script => script.OnAfterMoveChoice(moveChoice)); break; case ISwitchChoice switchChoice: ExecuteSwitchChoice(battle, switchChoice); @@ -111,7 +111,7 @@ public static class TurnRunner if (battleData == null) return; var preventSwitch = false; - fleeChoice.RunScriptHookInterface(script => + fleeChoice.RunScriptHook(script => script.PreventSelfSwitch(fleeChoice, ref preventSwitch)); if (preventSwitch) return; @@ -121,7 +121,7 @@ public static class TurnRunner continue; foreach (var pokemon in side.Pokemon.WhereNotNull()) { - pokemon.RunScriptHookInterface(script => + pokemon.RunScriptHook(script => script.PreventOpponentSwitch(fleeChoice, ref preventSwitch)); if (preventSwitch) return; @@ -142,7 +142,7 @@ public static class TurnRunner return; var preventFlee = false; - fleeChoice.RunScriptHookInterface(script => + fleeChoice.RunScriptHook(script => script.PreventSelfRunAway(fleeChoice, ref preventFlee)); if (preventFlee) return; @@ -153,7 +153,7 @@ public static class TurnRunner continue; foreach (var pokemon in side.Pokemon.WhereNotNull()) { - pokemon.RunScriptHookInterface(script => + pokemon.RunScriptHook(script => script.PreventOpponentRunAway(fleeChoice, ref preventFlee)); if (preventFlee) return; diff --git a/PkmnLib.Dynamic/Models/Battle.cs b/PkmnLib.Dynamic/Models/Battle.cs index 0b243f2..534ab0b 100644 --- a/PkmnLib.Dynamic/Models/Battle.cs +++ b/PkmnLib.Dynamic/Models/Battle.cs @@ -311,7 +311,7 @@ public class BattleImpl : ScriptSource, IBattle } ITurnChoice? forcedChoice = null; - pokemon.RunScriptHookInterface(script => + pokemon.RunScriptHook(script => script.ForceTurnSelection(this, battleData.SideIndex, battleData.Position, ref forcedChoice)); choice = forcedChoice; return choice != null; @@ -336,7 +336,7 @@ public class BattleImpl : ScriptSource, IBattle moveChoice.ChosenMove.MoveData.Target, moveChoice.User)) return false; var preventMove = false; - choice.RunScriptHookInterface(script => + choice.RunScriptHook(script => script.PreventMoveSelection(moveChoice, ref preventMove)); if (preventMove) return false; @@ -394,13 +394,13 @@ public class BattleImpl : ScriptSource, IBattle if (choice is IMoveChoice moveChoice) { var priority = moveChoice.ChosenMove.MoveData.Priority; - choice.RunScriptHookInterface(script => + choice.RunScriptHook(script => script.ChangePriority(moveChoice, ref priority)); moveChoice.Priority = priority; } var speed = choice.User.BoostedStats.Speed; - choice.RunScriptHookInterface(script => script.ChangeSpeed(choice, ref speed)); + choice.RunScriptHook(script => script.ChangeSpeed(choice, ref speed)); choice.Speed = speed; choice.RandomValue = (uint)Random.GetInt(); @@ -434,7 +434,8 @@ public class BattleImpl : ScriptSource, IBattle public bool SetWeather(StringKey? weatherName, int duration, EventBatchId batchId = default) { var preventWeatherChange = false; - this.RunScriptHook(x => x.PreventWeatherChange(weatherName, ref preventWeatherChange)); + this.RunScriptHook(x => + x.PreventWeatherChange(weatherName, ref preventWeatherChange)); if (preventWeatherChange) return false; @@ -446,7 +447,8 @@ public class BattleImpl : ScriptSource, IBattle // Extend duration of existing weather if (_weatherScript.Script is ILimitedTurnsScript existingWeatherScript) { - this.RunScriptHook(x => x.ChangeWeatherDuration(weatherName.Value, ref duration)); + this.RunScriptHook(x => + x.ChangeWeatherDuration(weatherName.Value, ref duration)); if (duration < existingWeatherScript.TurnsRemaining) return true; existingWeatherScript.SetTurns(duration); @@ -458,7 +460,8 @@ public class BattleImpl : ScriptSource, IBattle if (script is ILimitedTurnsScript weatherScript) { - this.RunScriptHook(x => x.ChangeWeatherDuration(weatherName.Value, ref duration)); + this.RunScriptHook(x => + x.ChangeWeatherDuration(weatherName.Value, ref duration)); weatherScript.SetTurns(duration); } @@ -474,7 +477,7 @@ public class BattleImpl : ScriptSource, IBattle BatchId = batchId, }); Sides.SelectMany(x => x.Pokemon).WhereNotNull() - .RunScriptHook(x => x.OnWeatherChange(this, weatherName, oldWeatherName)); + .RunScriptHook(x => x.OnWeatherChange(this, weatherName, oldWeatherName)); return true; } diff --git a/PkmnLib.Dynamic/Models/BattleChoiceQueue.cs b/PkmnLib.Dynamic/Models/BattleChoiceQueue.cs index b2704c7..9ad8420 100644 --- a/PkmnLib.Dynamic/Models/BattleChoiceQueue.cs +++ b/PkmnLib.Dynamic/Models/BattleChoiceQueue.cs @@ -72,7 +72,7 @@ public class BattleChoiceQueue : IDeepCloneable continue; // Ensure that the speed is up to date var speed = choice.User.BoostedStats.Speed; - choice.User.RunScriptHookInterface(script => script.ChangeSpeed(choice, ref speed)); + choice.User.RunScriptHook(script => script.ChangeSpeed(choice, ref speed)); choice.Speed = speed; } diff --git a/PkmnLib.Dynamic/Models/BattleRandom.cs b/PkmnLib.Dynamic/Models/BattleRandom.cs index 6be30a0..013ce3c 100644 --- a/PkmnLib.Dynamic/Models/BattleRandom.cs +++ b/PkmnLib.Dynamic/Models/BattleRandom.cs @@ -38,9 +38,9 @@ public class BattleRandomImpl : RandomImpl, IBattleRandom /// public bool EffectChance(float chance, IExecutingMove executingMove, IPokemon target, byte hitNumber) { - executingMove.RunScriptHookInterface(script => + executingMove.RunScriptHook(script => script.ChangeEffectChance(executingMove, target, hitNumber, ref chance)); - target.RunScriptHookInterface(script => + target.RunScriptHook(script => script.ChangeIncomingEffectChance(executingMove, target, hitNumber, ref chance)); if (chance > 100.0) return true; diff --git a/PkmnLib.Dynamic/Models/BattleSide.cs b/PkmnLib.Dynamic/Models/BattleSide.cs index 092f803..b6cd47b 100644 --- a/PkmnLib.Dynamic/Models/BattleSide.cs +++ b/PkmnLib.Dynamic/Models/BattleSide.cs @@ -248,7 +248,7 @@ public class BattleSideImpl : ScriptSource, IBattleSide var pokemon = _pokemon[index]; if (pokemon is not null) { - pokemon.RunScriptHook(script => script.OnRemove()); + pokemon.RunScriptHook(script => script.OnRemove()); pokemon.SetOnBattlefield(false); } @@ -261,8 +261,8 @@ public class BattleSideImpl : ScriptSource, IBattleSide var oldPokemon = _pokemon[position]; if (oldPokemon is not null) { - oldPokemon.RunScriptHookInterface(script => script.OnSwitchOut(oldPokemon, position)); - oldPokemon.RunScriptHook(script => script.OnRemove()); + oldPokemon.RunScriptHook(script => script.OnSwitchOut(oldPokemon, position)); + oldPokemon.RunScriptHook(script => script.OnRemove()); oldPokemon.SetOnBattlefield(false); } _pokemon[position] = pokemon; @@ -272,7 +272,7 @@ public class BattleSideImpl : ScriptSource, IBattleSide pokemon.SetOnBattlefield(true); pokemon.SetBattleSidePosition(position); Battle.EventHook.Invoke(new SwitchEvent(Index, position, pokemon)); - pokemon.RunScriptHookInterface(script => script.OnSwitchIn(pokemon, position)); + pokemon.RunScriptHook(script => script.OnSwitchIn(pokemon, position)); foreach (var side in Battle.Sides) { @@ -286,11 +286,10 @@ public class BattleSideImpl : ScriptSource, IBattleSide scripts.Clear(); opponent.GetOwnScripts(scripts); - opponent.RunScriptHookInterface(script => + opponent.RunScriptHook(script => script.OnOpponentSwitchIn(pokemon, position)); } - side.RunScriptHookInterface(script => - script.OnOpponentSwitchIn(pokemon, position)); + side.RunScriptHook(script => script.OnOpponentSwitchIn(pokemon, position)); } } else diff --git a/PkmnLib.Dynamic/Models/Pokemon.cs b/PkmnLib.Dynamic/Models/Pokemon.cs index ba2806e..15005dd 100644 --- a/PkmnLib.Dynamic/Models/Pokemon.cs +++ b/PkmnLib.Dynamic/Models/Pokemon.cs @@ -684,7 +684,7 @@ public class PokemonImpl : ScriptSource, IPokemon var weight = Form.Weight; if (BattleData is not null) // ReSharper disable once AccessToModifiedClosure - this.RunScriptHook(script => script.ModifyWeight(ref weight)); + this.RunScriptHook(script => script.ModifyWeight(ref weight)); if (weight < 0.1f) weight = 0.1f; return weight; @@ -793,7 +793,7 @@ public class PokemonImpl : ScriptSource, IPokemon { var previous = HeldItem; HeldItem = item; - this.RunScriptHook(x => x.OnAfterHeldItemChange(this, previous, item)); + this.RunScriptHook(x => x.OnAfterHeldItemChange(this, previous, item)); return previous; } @@ -809,7 +809,7 @@ public class PokemonImpl : ScriptSource, IPokemon } var previous = HeldItem; HeldItem = null; - this.RunScriptHook(x => x.OnAfterHeldItemChange(this, previous, null)); + this.RunScriptHook(x => x.OnAfterHeldItemChange(this, previous, null)); return previous; } @@ -833,7 +833,8 @@ public class PokemonImpl : ScriptSource, IPokemon return false; } var prevent = false; - this.RunScriptHook(script => script.PreventHeldItemSteal(this, HeldItem, ref prevent)); + this.RunScriptHook(script => + script.PreventHeldItemSteal(this, HeldItem, ref prevent)); if (prevent) { item = null; @@ -861,7 +862,7 @@ public class PokemonImpl : ScriptSource, IPokemon if (BattleData != null) { var prevented = false; - this.RunScriptHookInterface(script => + this.RunScriptHook(script => script.PreventHeldItemConsume(this, HeldItem, ref prevented)); if (prevented) return false; @@ -880,7 +881,7 @@ public class PokemonImpl : ScriptSource, IPokemon { // TODO: actually consume the item - this.RunScriptHookInterface(x => x.OnAfterItemConsume(this, item)); + this.RunScriptHook(x => x.OnAfterItemConsume(this, item)); } /// @@ -890,11 +891,11 @@ public class PokemonImpl : ScriptSource, IPokemon if (!force) { var prevented = false; - this.RunScriptHookInterface(script => + this.RunScriptHook(script => script.PreventStatBoostChange(this, stat, change, selfInflicted, ref prevented)); if (prevented) return false; - this.RunScriptHookInterface(script => + this.RunScriptHook(script => script.ChangeStatBoostChange(this, stat, selfInflicted, ref change)); if (change == 0) return false; @@ -919,7 +920,7 @@ public class PokemonImpl : ScriptSource, IPokemon } RecalculateBoostedStats(); - this.RunScriptHookInterface(script => + this.RunScriptHook(script => script.OnAfterStatBoostChange(this, stat, selfInflicted, change)); return true; } @@ -1075,7 +1076,7 @@ public class PokemonImpl : ScriptSource, IPokemon if (BattleData is not null && !forceDamage) { var dmg = damage; - this.RunScriptHookInterface(script => + this.RunScriptHook(script => script.ChangeIncomingDamage(this, source, ref dmg)); damage = dmg; } @@ -1096,8 +1097,7 @@ public class PokemonImpl : ScriptSource, IPokemon BatchId = batchId, }); // And allow scripts to execute. - this.RunScriptHookInterface(script => - script.OnDamage(this, source, CurrentHealth, newHealth)); + this.RunScriptHook(script => script.OnDamage(this, source, CurrentHealth, newHealth)); } CurrentHealth = newHealth; @@ -1126,14 +1126,14 @@ public class PokemonImpl : ScriptSource, IPokemon BattleData.Battle.EventHook.Invoke(new FaintEvent(this)); // Allow scripts to trigger based on the faint. - this.RunScriptHookInterface(script => script.OnFaint(this, source)); + this.RunScriptHook(script => script.OnFaint(this, source)); foreach (var ally in BattleData.BattleSide.Pokemon.WhereNotNull().Where(x => x != this)) { - ally.RunScriptHookInterface(script => script.OnAllyFaint(ally, this)); + ally.RunScriptHook(script => script.OnAllyFaint(ally, this)); } // Make sure the OnRemove script is run. - this.RunScriptHook(script => script.OnRemove()); + this.RunScriptHook(script => script.OnRemove()); // Mark the position as unfillable if it can't be filled by any party. if (!BattleData.Battle.CanSlotBeFilled(BattleData.SideIndex, BattleData.Position)) @@ -1162,7 +1162,7 @@ public class PokemonImpl : ScriptSource, IPokemon if (!forceHeal) { var prevented = false; - this.RunScriptHook(x => x.PreventHeal(this, heal, allowRevive, ref prevented)); + this.RunScriptHook(x => x.PreventHeal(this, heal, allowRevive, ref prevented)); if (prevented) return false; } @@ -1226,7 +1226,8 @@ public class PokemonImpl : ScriptSource, IPokemon var selfInflicted = originPokemon == this; var preventStatus = false; - this.RunScriptHook(script => script.PreventStatusChange(this, status, selfInflicted, ref preventStatus)); + this.RunScriptHook(script => + script.PreventStatusChange(this, status, selfInflicted, ref preventStatus)); if (preventStatus) return false; @@ -1236,7 +1237,8 @@ public class PokemonImpl : ScriptSource, IPokemon { BatchId = batchId, }); - this.RunScriptHook(script => script.OnAfterStatusChange(this, status, originPokemon)); + this.RunScriptHook(script => + script.OnAfterStatusChange(this, status, originPokemon)); return true; } @@ -1375,7 +1377,7 @@ public class PokemonImpl : ScriptSource, IPokemon get { var isFloating = Types.Any(x => x.Name == "flying"); - this.RunScriptHook(x => x.IsFloating(this, ref isFloating)); + this.RunScriptHook(x => x.IsFloating(this, ref isFloating)); return isFloating; } } diff --git a/PkmnLib.Dynamic/ScriptHandling/Script.cs b/PkmnLib.Dynamic/ScriptHandling/Script.cs index 2e4caa9..2efd10f 100644 --- a/PkmnLib.Dynamic/ScriptHandling/Script.cs +++ b/PkmnLib.Dynamic/ScriptHandling/Script.cs @@ -14,7 +14,7 @@ namespace PkmnLib.Dynamic.ScriptHandling; /// developer might require. /// [DebuggerDisplay("{Category} - {Name}")] -public abstract class Script : IDeepCloneable +public abstract class Script : IDeepCloneable, IScriptOnRemove { internal event Action