using System;
using PkmnLib.Plugin.Gen7.Scripts.Pokemon;
namespace PkmnLib.Plugin.Gen7.Scripts.Moves;
[Script(ScriptCategory.Move, "protect")]
public class ProtectionScript : Script
{
protected virtual Script GetEffectScript() => new ProtectionEffectScript();
///
public override void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit)
{
var battleData = target.BattleData;
if (battleData == null)
return;
// If the user goes last in the turn, the move will fail
if (battleData.Battle.ChoiceQueue is not null && !battleData.Battle.ChoiceQueue.HasNext())
{
move.GetHitData(target, hit).Fail();
return;
}
var failure = target.Volatile.Get();
if (failure == null)
{
failure = new ProtectionFailureScript();
target.Volatile.Add(failure);
}
var successive = failure.ProtectTurns;
// Each time, the chance of success is divided by 3.
var chance = 1.0 / Math.Pow(3, successive);
var random = target.BattleData!.Battle.Random.GetFloat();
if (random < chance)
{
failure.ProtectTurns++;
failure.UsedProtect = true;
target.Volatile.Add(GetEffectScript());
}
else
{
move.GetHitData(target, hit).Fail();
}
}
}