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

@@ -84,7 +84,75 @@ public static class TurnRunner
case IMoveChoice moveChoice:
MoveTurnExecutor.ExecuteMoveChoice(battle, moveChoice);
break;
// TODO: Implement other choice types
case ISwitchChoice switchChoice:
ExecuteSwitchChoice(battle, switchChoice);
break;
case IFleeChoice fleeChoice:
ExecuteFleeChoice(battle, fleeChoice);
break;
// TODO: Implement item choice types
}
}
private static void ExecuteSwitchChoice(IBattle battle, ISwitchChoice fleeChoice)
{
var user = fleeChoice.User;
var battleData = user.BattleData;
if (battleData == null)
return;
var preventSwitch = false;
fleeChoice.RunScriptHook(script => script.PreventSelfSwitch(fleeChoice, ref preventSwitch));
if (preventSwitch)
return;
foreach (var side in battle.Sides)
{
if (side.Index == battleData.SideIndex)
continue;
foreach (var pokemon in side.Pokemon.WhereNotNull())
{
pokemon.RunScriptHook(script => script.PreventOpponentSwitch(fleeChoice, ref preventSwitch));
if (preventSwitch)
return;
}
}
user.Volatile.Clear();
var userSide = battle.Sides[battleData.SideIndex];
userSide.SwapPokemon(battleData.Position, fleeChoice.SwitchTo);
}
private static void ExecuteFleeChoice(IBattle battle, IFleeChoice fleeChoice)
{
var user = fleeChoice.User;
var battleData = user.BattleData;
if (battleData == null)
return;
if (!battle.CanFlee)
return;
var preventFlee = false;
fleeChoice.RunScriptHook(script => script.PreventSelfRunAway(fleeChoice, ref preventFlee));
if (preventFlee)
return;
foreach (var side in battle.Sides)
{
if (side.Index == battleData.SideIndex)
continue;
foreach (var pokemon in side.Pokemon.WhereNotNull())
{
pokemon.RunScriptHook(script => script.PreventOpponentRunAway(fleeChoice, ref preventFlee));
if (preventFlee)
return;
}
}
if (!battle.Library.MiscLibrary.CanFlee(battle, fleeChoice))
return;
var userSide = battle.Sides[battleData.SideIndex];
userSide.MarkAsFled();
battle.ValidateBattleState();
}
}

View File

@@ -110,6 +110,16 @@ public interface IBattleSide : IScriptSource, IDeepCloneable
/// Checks whether the side has been defeated.
/// </summary>
bool IsDefeated();
/// <summary>
/// The number of times this side has attempted to flee.
/// </summary>
uint FleeAttempts { get; }
/// <summary>
/// Registers a flee attempt for this side.
/// </summary>
void RegisterFleeAttempt();
/// <summary>
/// Mark the side as fled.
@@ -267,6 +277,15 @@ public class BattleSideImpl : ScriptSource, IBattleSide
return _fillablePositions.All(fillable => !fillable);
}
/// <inheritdoc />
public uint FleeAttempts { get; private set; }
/// <inheritdoc />
public void RegisterFleeAttempt()
{
FleeAttempts++;
}
/// <inheritdoc />
public void MarkAsFled() => HasFledBattle = true;

View File

@@ -7,7 +7,10 @@ namespace PkmnLib.Dynamic.Models.Choices;
/// </summary>
public interface ISwitchChoice : ITurnChoice
{
/// <summary>
/// The Pokémon to switch to.
/// </summary>
IPokemon SwitchTo { get; }
}
/// <inheritdoc cref="ISwitchChoice"/>
@@ -18,6 +21,7 @@ public class SwitchChoice : TurnChoice, ISwitchChoice
SwitchTo = switchTo;
}
/// <inheritdoc />
public IPokemon SwitchTo { get; }
/// <inheritdoc />