Implements switching Pokemon and fleeing from battle

This commit is contained in:
2024-12-30 11:43:04 +01:00
parent 9bdd584b54
commit 1f5a320090
5 changed files with 123 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using PkmnLib.Dynamic;
using PkmnLib.Dynamic.Libraries;
using PkmnLib.Dynamic.Models;
@@ -35,4 +36,31 @@ public class Gen7MiscLibrary : IMiscLibrary
_ => TimeOfDay.Night,
};
}
/// <inheritdoc />
public bool CanFlee(IBattle battle, IFleeChoice fleeChoice)
{
var user = fleeChoice.User;
var battleData = user.BattleData;
if (battleData == null)
return false;
var opponentSide = battle.Sides[battleData.SideIndex == 0 ? 1 : 0];
var opponent = opponentSide.Pokemon.FirstOrDefault(x => x is not null);
if (opponent == null)
return true;
var userSpeed = user.FlatStats.Speed;
var opponentSpeed = opponent.FlatStats.Speed;
// If the player's active Pokémon's Speed is greater than or equal to the wild Pokémon's Speed, fleeing will
// always succeed
if (userSpeed >= opponentSpeed)
return true;
var userSide = battle.Sides[battleData.SideIndex];
userSide.RegisterFleeAttempt();
var fleeChance = ((userSpeed * 32) / (opponentSpeed / 4) + (30 * userSide.FleeAttempts)) / 256;
var random = battle.Random.GetInt(0, 100);
return random < fleeChance;
}
}