45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
using System;
|
|
using PkmnLib.Plugin.Gen7.Scripts.Pokemon;
|
|
|
|
namespace PkmnLib.Plugin.Gen7.Scripts.Moves;
|
|
|
|
public abstract class ProtectionScript : Script
|
|
{
|
|
protected abstract ProtectionEffectScript GetEffectScript();
|
|
|
|
/// <inheritdoc />
|
|
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<ProtectionFailureScript>();
|
|
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();
|
|
}
|
|
}
|
|
} |