Implements switching Pokemon and fleeing from battle
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user