This commit is contained in:
@@ -13,7 +13,7 @@ namespace PkmnLib.Dynamic.Models;
|
||||
/// A battle is a representation of a battle in the Pokemon games. It contains all the information needed
|
||||
/// to simulate a battle, and can be used to simulate a battle between two parties.
|
||||
/// </summary>
|
||||
public interface IBattle : IScriptSource, IDeepCloneable
|
||||
public interface IBattle : IScriptSource, IDeepCloneable, IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The library the battle uses for handling.
|
||||
@@ -40,6 +40,12 @@ public interface IBattle : IScriptSource, IDeepCloneable
|
||||
/// </summary>
|
||||
byte PositionsPerSide { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether this battle is a wild battle. In a wild battle, the player can catch the opposing Pokemon,
|
||||
/// and moves like roar will end the battle instead of switching out the Pokemon.
|
||||
/// </summary>
|
||||
bool IsWildBattle { get; }
|
||||
|
||||
/// <summary>
|
||||
/// A list of all sides in the battle.
|
||||
/// </summary>
|
||||
@@ -93,6 +99,11 @@ public interface IBattle : IScriptSource, IDeepCloneable
|
||||
/// </summary>
|
||||
void ValidateBattleState();
|
||||
|
||||
/// <summary>
|
||||
/// Forcefully ends the battle. This will set the result to inconclusive and set HasEnded to true.
|
||||
/// </summary>
|
||||
void ForceEndBattle();
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a Pokemon has a forced turn choice. If it does, this returns true and the choice
|
||||
/// is set in the out parameter. If it does not, this returns false and the out parameter is null.
|
||||
@@ -119,7 +130,7 @@ public interface IBattle : IScriptSource, IDeepCloneable
|
||||
/// A duration can be passed to set the duration of the weather in turns. This duration can be modified by
|
||||
/// other scripts before the weather is set through the <see cref="Script.ChangeWeatherDuration"/> script hook.
|
||||
/// </summary>
|
||||
bool SetWeather(StringKey? weatherName, int duration);
|
||||
bool SetWeather(StringKey? weatherName, int duration, EventBatchId batchId = default);
|
||||
|
||||
/// <summary>
|
||||
/// Volatile scripts are scripts that are not permanent and can be removed by other scripts.
|
||||
@@ -134,8 +145,7 @@ public interface IBattle : IScriptSource, IDeepCloneable
|
||||
/// <summary>
|
||||
/// Sets the current terrain for the battle. If null is passed, this clears the terrain.
|
||||
/// </summary>
|
||||
/// <param name="terrainName"></param>
|
||||
void SetTerrain(StringKey? terrainName);
|
||||
void SetTerrain(StringKey? terrainName, EventBatchId batchId = default);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current terrain of the battle. If no terrain is present, this returns null.
|
||||
@@ -165,13 +175,14 @@ public class BattleImpl : ScriptSource, IBattle
|
||||
/// <param name="positionsPerSide">The number of spots there are on each side for Pokémon. 1 for singles, 2 for doubles, etc.</param>
|
||||
/// <param name="randomSeed">The seed for the RNG. If null, this uses a time-dependent seed.</param>
|
||||
public BattleImpl(IDynamicLibrary library, IReadOnlyList<IBattleParty> parties, bool canFlee, byte numberOfSides,
|
||||
byte positionsPerSide, int? randomSeed = null)
|
||||
byte positionsPerSide, bool isWildBattle, int? randomSeed = null)
|
||||
{
|
||||
Library = library;
|
||||
Parties = parties;
|
||||
CanFlee = canFlee;
|
||||
NumberOfSides = numberOfSides;
|
||||
PositionsPerSide = positionsPerSide;
|
||||
IsWildBattle = isWildBattle;
|
||||
Volatile = new ScriptSet(this);
|
||||
var sides = new IBattleSide[numberOfSides];
|
||||
for (byte i = 0; i < numberOfSides; i++)
|
||||
@@ -196,6 +207,9 @@ public class BattleImpl : ScriptSource, IBattle
|
||||
/// <inheritdoc />
|
||||
public byte PositionsPerSide { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsWildBattle { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<IBattleSide> Sides { get; }
|
||||
|
||||
@@ -263,6 +277,13 @@ public class BattleImpl : ScriptSource, IBattle
|
||||
HasEnded = true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ForceEndBattle()
|
||||
{
|
||||
HasEnded = true;
|
||||
Result = BattleResult.Inconclusive;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool HasForcedTurn(IPokemon pokemon, [NotNullWhen(true)] out ITurnChoice? choice)
|
||||
{
|
||||
@@ -376,10 +397,28 @@ public class BattleImpl : ScriptSource, IBattle
|
||||
public IReadOnlyScriptContainer WeatherScript => _weatherScript;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool SetWeather(StringKey? weatherName, int duration)
|
||||
public bool SetWeather(StringKey? weatherName, int duration, EventBatchId batchId = default)
|
||||
{
|
||||
var preventWeatherChange = false;
|
||||
this.RunScriptHook(x => x.PreventWeatherChange(weatherName, ref preventWeatherChange));
|
||||
if (preventWeatherChange)
|
||||
return false;
|
||||
|
||||
var oldWeatherName = WeatherScript.Script?.Name;
|
||||
if (weatherName.HasValue)
|
||||
{
|
||||
if (weatherName == oldWeatherName)
|
||||
{
|
||||
// Extend duration of existing weather
|
||||
if (_weatherScript.Script is ILimitedTurnsScript existingWeatherScript)
|
||||
{
|
||||
this.RunScriptHook(x => x.ChangeWeatherDuration(weatherName.Value, ref duration));
|
||||
if (duration < existingWeatherScript.TurnsRemaining)
|
||||
return true;
|
||||
existingWeatherScript.SetTurns(duration);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (!Library.ScriptResolver.TryResolve(ScriptCategory.Weather, weatherName.Value, null, out var script))
|
||||
throw new InvalidOperationException($"Weather script {weatherName} not found.");
|
||||
|
||||
@@ -396,8 +435,13 @@ public class BattleImpl : ScriptSource, IBattle
|
||||
{
|
||||
_weatherScript.Clear();
|
||||
}
|
||||
EventHook.Invoke(new WeatherChangeEvent(oldWeatherName, weatherName)
|
||||
{
|
||||
BatchId = batchId,
|
||||
});
|
||||
Sides.SelectMany(x => x.Pokemon).WhereNotNull()
|
||||
.RunScriptHook(x => x.OnWeatherChange(this, weatherName, oldWeatherName));
|
||||
return true;
|
||||
// TODO: Trigger weather change script hooks
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -409,12 +453,14 @@ public class BattleImpl : ScriptSource, IBattle
|
||||
private readonly ScriptContainer _terrainScript = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public void SetTerrain(StringKey? terrainName)
|
||||
public void SetTerrain(StringKey? terrainName, EventBatchId batchId = default)
|
||||
{
|
||||
var oldTerrainName = TerrainName;
|
||||
if (terrainName.HasValue)
|
||||
{
|
||||
if (!Library.ScriptResolver.TryResolve(ScriptCategory.Terrain, terrainName.Value, null, out var script))
|
||||
throw new InvalidOperationException($"Terrain script {terrainName} not found.");
|
||||
|
||||
_terrainScript.Set(script);
|
||||
script.OnAddedToParent(this);
|
||||
}
|
||||
@@ -422,6 +468,10 @@ public class BattleImpl : ScriptSource, IBattle
|
||||
{
|
||||
_terrainScript.Clear();
|
||||
}
|
||||
EventHook.Invoke(new TerrainChangeEvent(oldTerrainName, terrainName)
|
||||
{
|
||||
BatchId = batchId,
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -463,4 +513,19 @@ public class BattleImpl : ScriptSource, IBattle
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void CollectScripts(List<IEnumerable<ScriptContainer>> scripts) => GetOwnScripts(scripts);
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var party in Parties)
|
||||
{
|
||||
foreach (var pokemon in party.Party.WhereNotNull())
|
||||
{
|
||||
pokemon.ClearBattleData();
|
||||
}
|
||||
}
|
||||
_weatherScript.Clear();
|
||||
_terrainScript.Clear();
|
||||
Volatile.Clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user