using System; using PkmnLib.Dynamic.Libraries; using PkmnLib.Static; namespace PkmnLib.Plugin.Gen7.Libraries; public class Gen7CaptureLibrary : ICaptureLibrary { /// public CaptureResult TryCapture(IPokemon target, IItem captureItem, IBattleRandom random) { var maxHealth = target.BoostedStats.Hp; var currentHealth = target.CurrentHealth; var catchRate = target.Species.CaptureRate; byte bonusBall = 1; if (target.Library.ScriptResolver.TryResolveBattleItemScript(captureItem, out var script) && script is PokeballScript pokeballScript) { bonusBall = pokeballScript.GetCatchRate(target); } byte bonusStatus = 1; target.RunScriptHook(x => x.ChangeCatchRateBonus(target, captureItem, ref bonusStatus)); var modifiedCatchRate = (((3.0 * maxHealth) - (2.0 * currentHealth)) * catchRate * bonusBall) / (3.0 * maxHealth); modifiedCatchRate *= bonusStatus; var shakeProbability = 65536 / Math.Pow((255 / modifiedCatchRate), 0.1875); byte shakes = 0; if (modifiedCatchRate >= 255) { shakes = 4; } else { // FIXME: Implement critical capture for (var i = 0; i < 4; i++) { if (random.GetInt(0, 65536) < shakeProbability) { shakes++; } } } var success = shakes >= 3; return new CaptureResult(success, shakes, false); } }