Adds battle history, fixes code style

This commit is contained in:
2024-08-23 09:24:00 +02:00
parent d48889e21a
commit e7dc885afd
21 changed files with 80 additions and 70 deletions

View File

@@ -110,6 +110,12 @@ public interface IBattle : IScriptSource
/// Gets the current weather of the battle. If no weather is present, this returns null.
/// </summary>
StringKey? WeatherName { get; }
/// <summary>
/// Gets the turn choices of the previous turn. This is a list of lists, where each list represents the choices
/// for a single turn. The outer list is ordered from oldest to newest turn.
/// </summary>
IReadOnlyList<IReadOnlyList<ITurnChoice>> PreviousTurnChoices { get; }
}
/// <inheritdoc cref="IBattle"/>
@@ -172,7 +178,8 @@ public class BattleImpl : ScriptSource, IBattle
public IPokemon? GetPokemon(byte side, byte position) => Sides[side].Pokemon[position];
/// <inheritdoc />
public bool CanSlotBeFilled(byte side, byte position) => Parties.Any(x => x.IsResponsibleForIndex(new ResponsibleIndex(side, position)) && x.HasPokemonNotInField());
public bool CanSlotBeFilled(byte side, byte position) => Parties.Any(x =>
x.IsResponsibleForIndex(new ResponsibleIndex(side, position)) && x.HasPokemonNotInField());
/// <inheritdoc />
public void ValidateBattleState()
@@ -207,7 +214,7 @@ public class BattleImpl : ScriptSource, IBattle
HasEnded = true;
return;
}
// If only one side is left, that side has won
Result = BattleResult.Conclusive(survivingSide!.Index);
HasEnded = true;
@@ -267,17 +274,22 @@ public class BattleImpl : ScriptSource, IBattle
choice.RunScriptHook(script => script.ChangePriority(moveChoice, ref priority));
moveChoice.Priority = priority;
}
var speed = choice.User.BoostedStats.Speed;
choice.RunScriptHook(script => script.ChangeSpeed(choice, ref speed));
choice.Speed = speed;
choice.RandomValue = (uint)Random.GetInt();
choices[index * PositionsPerSide + i] = choice;
choices[index * PositionsPerSide + i] = choice;
}
side.ResetChoices();
}
_previousTurnChoices.Add(choices.ToList());
CurrentTurnNumber += 1;
ChoiceQueue = new BattleChoiceQueue(choices);
@@ -288,6 +300,7 @@ public class BattleImpl : ScriptSource, IBattle
}
private readonly ScriptContainer _weatherScript = new();
/// <inheritdoc />
public void SetWeather(StringKey? weatherName)
{
@@ -303,19 +316,25 @@ public class BattleImpl : ScriptSource, IBattle
_weatherScript.Clear();
}
}
private IScriptSet Volatile { get; } = new ScriptSet();
/// <inheritdoc />
public StringKey? WeatherName => _weatherScript.Script?.Name;
private readonly List<IReadOnlyList<ITurnChoice>> _previousTurnChoices = new();
/// <inheritdoc />
public IReadOnlyList<IReadOnlyList<ITurnChoice>> PreviousTurnChoices => _previousTurnChoices;
/// <inheritdoc />
public override int ScriptCount => 2;
/// <inheritdoc />
public override void GetOwnScripts(List<IEnumerable<ScriptContainer>> scripts)
{
scripts.Add(_weatherScript );
scripts.Add(_weatherScript);
scripts.Add(Volatile);
}

View File

@@ -168,7 +168,7 @@ internal static class MoveTurnExecutor
var hitEventBatch = new EventBatchId();
battle.EventHook.Invoke(new MoveHitEvent(executingMove, hitData, target)
{
BatchId = hitEventBatch
BatchId = hitEventBatch,
});
target.Damage(damage, DamageSource.MoveDamage, hitEventBatch);
if (!target.IsFainted)

View File

@@ -22,7 +22,7 @@ public static class TargetResolver
MoveTarget.AllAdjacent => GetAllAdjacent(battle, side, position),
MoveTarget.AllAlly => battle.Sides[side].Pokemon.ToList(),
MoveTarget.AllOpponent => battle.Sides[GetOppositeSide(side)].Pokemon.ToList(),
_ => throw new ArgumentOutOfRangeException(nameof(target), target, null)
_ => throw new ArgumentOutOfRangeException(nameof(target), target, null),
};
}
@@ -49,7 +49,7 @@ public static class TargetResolver
return
[
battle.GetPokemon(side, position), battle.GetPokemon(GetOppositeSide(side), position),
battle.GetPokemon(side, (byte)right)
battle.GetPokemon(side, (byte)right),
];
}
@@ -58,14 +58,14 @@ public static class TargetResolver
return
[
battle.GetPokemon(side, position), battle.GetPokemon(GetOppositeSide(side), position),
battle.GetPokemon(side, (byte)left)
battle.GetPokemon(side, (byte)left),
];
}
return
[
battle.GetPokemon(side, position), battle.GetPokemon(GetOppositeSide(side), position),
battle.GetPokemon(side, (byte)left), battle.GetPokemon(side, (byte)right)
battle.GetPokemon(side, (byte)left), battle.GetPokemon(side, (byte)right),
];
}
@@ -90,7 +90,7 @@ public static class TargetResolver
return
[
battle.GetPokemon(side, position), battle.GetPokemon(side, (byte)left), battle.GetPokemon(side, (byte)right)
battle.GetPokemon(side, position), battle.GetPokemon(side, (byte)left), battle.GetPokemon(side, (byte)right),
];
}
}

View File

@@ -12,7 +12,7 @@ public class TurnChoiceComparer : IComparer<ITurnChoice>
{
XEqualsY = 0,
XLessThanY = -1,
XGreaterThanY = 1
XGreaterThanY = 1,
}
private static CompareValues CompareForSameType(ITurnChoice x, ITurnChoice y)
@@ -58,7 +58,7 @@ public class TurnChoiceComparer : IComparer<ITurnChoice>
{
IMoveChoice => CompareValues.XLessThanY,
IItemChoice itemY => CompareForSameType(itemX, itemY),
_ => CompareValues.XGreaterThanY
_ => CompareValues.XGreaterThanY,
};
case ISwitchChoice switchX:
// Switch choices go third
@@ -66,7 +66,7 @@ public class TurnChoiceComparer : IComparer<ITurnChoice>
{
IMoveChoice or IItemChoice => CompareValues.XLessThanY,
ISwitchChoice switchY => CompareForSameType(switchX, switchY),
_ => CompareValues.XGreaterThanY
_ => CompareValues.XGreaterThanY,
};
case IPassChoice passX:
// Pass choices go last
@@ -74,7 +74,7 @@ public class TurnChoiceComparer : IComparer<ITurnChoice>
{
IMoveChoice or IItemChoice or ISwitchChoice => CompareValues.XLessThanY,
IPassChoice passY => CompareForSameType(passX, passY),
_ => CompareValues.XGreaterThanY
_ => CompareValues.XGreaterThanY,
};
}

View File

@@ -35,7 +35,7 @@ public enum MoveLearnMethod
/// <summary>
/// The move is learned when the Pokémon changes form.
/// </summary>
FormChange
FormChange,
}
/// <summary>

View File

@@ -523,7 +523,7 @@ public class PokemonImpl : ScriptSource, IPokemon
{
> 0 => StatBoost.IncreaseStatistic(stat, change),
< 0 => StatBoost.DecreaseStatistic(stat, change),
_ => changed
_ => changed,
};
if (!changed)
return false;
@@ -599,7 +599,7 @@ public class PokemonImpl : ScriptSource, IPokemon
// If the Pokémon is in a battle, we trigger an event to the front-end.
BattleData.Battle.EventHook.Invoke(new DamageEvent(this, CurrentHealth, newHealth, source)
{
BatchId = batchId
BatchId = batchId,
});
// And allow scripts to execute.
this.RunScriptHook(script => script.OnDamage(this, source, CurrentHealth, newHealth));

View File

@@ -14,7 +14,6 @@ namespace PkmnLib.Dynamic.ScriptHandling;
/// </summary>
public abstract class Script
{
private bool _markedForDeletion;
private int _suppressCount;
/// <summary>
@@ -24,9 +23,6 @@ public abstract class Script
/// </summary>
public virtual StringKey Name => this.ResolveName();
public bool MarkForDeletion() => _markedForDeletion = true;
public bool IsMarkedForDeletion() => _markedForDeletion;
/// <summary>
/// A script can be suppressed by other scripts. If a script is suppressed by at least one script
/// we will not execute its methods. This should return the number of suppressions on the script.

View File

@@ -52,7 +52,6 @@ public class ScriptContainer : IEnumerable<ScriptContainer>
if (Script is not null)
{
Script.OnRemove();
Script.MarkForDeletion();
}
Script = script;
}
@@ -65,7 +64,6 @@ public class ScriptContainer : IEnumerable<ScriptContainer>
if (Script is not null)
{
Script.OnRemove();
Script.MarkForDeletion();
}
Script = null;
}

View File

@@ -122,7 +122,6 @@ public class ScriptSet : IScriptSet
if (!script.IsEmpty)
{
script.Script.OnRemove();
script.Script.MarkForDeletion();
}
}
_scripts.Clear();