Initial work on implementing Pokemon
This commit is contained in:
@@ -108,7 +108,4 @@ public interface IBattle : IScriptSource
|
||||
/// Gets the current weather of the battle. If no weather is present, this returns null.
|
||||
/// </summary>
|
||||
string? WeatherName { get; }
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using PkmnLib.Dynamic.Libraries;
|
||||
using PkmnLib.Dynamic.ScriptHandling;
|
||||
using PkmnLib.Static;
|
||||
using PkmnLib.Static.Species;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
namespace PkmnLib.Dynamic.Models;
|
||||
|
||||
@@ -191,23 +192,25 @@ public interface IPokemon : IScriptSource
|
||||
/// <summary>
|
||||
/// Checks whether the Pokemon is holding an item with a specific name.
|
||||
/// </summary>
|
||||
bool HasHeldItem(string itemName);
|
||||
|
||||
bool HasHeldItem(StringKey itemName);
|
||||
|
||||
/// <summary>
|
||||
/// Changes the held item of the Pokemon. Returns the previously held item.
|
||||
/// </summary>
|
||||
[MustUseReturnValue] IItem? SetHeldItem(IItem? item);
|
||||
|
||||
[MustUseReturnValue]
|
||||
IItem? SetHeldItem(IItem? item);
|
||||
|
||||
/// <summary>
|
||||
/// Removes the held item from the Pokemon. Returns the previously held item.
|
||||
/// </summary>
|
||||
[MustUseReturnValue] IItem? RemoveHeldItem();
|
||||
[MustUseReturnValue]
|
||||
IItem? RemoveHeldItem();
|
||||
|
||||
/// <summary>
|
||||
/// Makes the Pokemon uses its held item. Returns whether the item was consumed.
|
||||
/// </summary>
|
||||
bool ConsumeHeldItem();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Change a boosted stat by a certain amount.
|
||||
/// </summary>
|
||||
@@ -215,81 +218,453 @@ public interface IPokemon : IScriptSource
|
||||
/// <param name="change">The amount to change the stat by</param>
|
||||
/// <param name="selfInflicted">Whether the change was self-inflicted. This can be relevant in scripts.</param>
|
||||
void ChangeStatBoost(Statistic stat, sbyte change, bool selfInflicted);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns the currently active ability.
|
||||
/// </summary>
|
||||
IAbility ActiveAbility { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the flat stats on the Pokemon. This should be called when for example the base
|
||||
/// stats, level, nature, IV, or EV changes. This has a side effect of recalculating the boosted
|
||||
/// stats, as those depend on the flat stats.
|
||||
/// </summary>
|
||||
void RecalculateFlatStats();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the boosted stats on the Pokemon, _without_ recalculating the flat stats.
|
||||
/// This should be called when a stat boost changes.
|
||||
/// </summary>
|
||||
void RecalculateBoostedStats();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Change the species of the Pokemon.
|
||||
/// </summary>
|
||||
void ChangeSpecies(ISpecies species, IForm form);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Change the form of the Pokemon.
|
||||
/// </summary>
|
||||
void ChangeForm(IForm form);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Whether the Pokemon is useable in a battle.
|
||||
/// </summary>
|
||||
bool IsUsable { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Whether the Pokemon is fainted.
|
||||
/// </summary>
|
||||
bool IsFainted { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Damages the Pokemon by a certain amount of damage, from a damage source.
|
||||
/// </summary>
|
||||
void Damage(uint damage, DamageSource source, EventBatchId batchId);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Heals the Pokemon by a specific amount. Unless allow_revive is set to true, this will not
|
||||
/// heal if the Pokemon has 0 health. If the amount healed is 0, this will return false.
|
||||
/// </summary>
|
||||
bool Heal(uint heal, bool allowRevive);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Learn a move by name.
|
||||
/// </summary>
|
||||
void LearnMove(string moveName, MoveLearnMethod method, byte index);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Removes the current non-volatile status from the Pokemon.
|
||||
/// </summary>
|
||||
void ClearStatus();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Modifies the level by a certain amount
|
||||
/// </summary>
|
||||
void ChangeLevelBy(int change);
|
||||
|
||||
|
||||
// TODO: (de)serialize
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The data of the Pokemon related to being in a battle.
|
||||
/// This is only set when the Pokemon is on the field in a battle.
|
||||
/// </summary>
|
||||
public interface IPokemonBattleData
|
||||
{
|
||||
/// <summary>
|
||||
/// The optional battle the Pokemon is in.
|
||||
/// The battle the Pokemon is in.
|
||||
/// </summary>
|
||||
IBattle? Battle { get; }
|
||||
IBattle Battle { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The index of the side of the Pokemon
|
||||
/// </summary>
|
||||
byte SideIndex { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The index of the position of the Pokemon on the field
|
||||
/// </summary>
|
||||
byte Position { get; }
|
||||
|
||||
/// <summary>
|
||||
/// A list of opponents the Pokemon has seen this battle.
|
||||
/// </summary>
|
||||
IReadOnlyList<IPokemon> SeenOpponents { get; }
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public class PokemonImpl : ScriptSource, IPokemon
|
||||
{
|
||||
/// <inheritdoc cref="PokemonImpl"/>
|
||||
public PokemonImpl(IDynamicLibrary library, ISpecies species, IForm form, AbilityIndex abilityIndex, LevelInt level,
|
||||
uint personalityValue, Gender gender, byte coloring, StringKey natureName)
|
||||
{
|
||||
Library = library;
|
||||
Species = species;
|
||||
Form = form;
|
||||
AbilityIndex = abilityIndex;
|
||||
Level = level;
|
||||
PersonalityValue = personalityValue;
|
||||
Gender = gender;
|
||||
Coloring = coloring;
|
||||
Types = form.Types.ToList();
|
||||
|
||||
Experience = library.StaticLibrary.GrowthRates.CalculateExperience(species.GrowthRate, level);
|
||||
WeightInKm = form.Weight;
|
||||
HeightInMeters = form.Height;
|
||||
Happiness = species.BaseHappiness;
|
||||
if (!library.StaticLibrary.Natures.TryGet(natureName, out var nature))
|
||||
throw new KeyNotFoundException($"Nature {natureName} not found.");
|
||||
Nature = nature;
|
||||
|
||||
RecalculateFlatStats();
|
||||
CurrentHealth = BoostedStats.Hp;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IDynamicLibrary Library { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public ISpecies Species { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IForm Form { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public ISpecies? DisplaySpecies { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IForm? DisplayForm { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public LevelInt Level { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public uint Experience { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public uint PersonalityValue { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public Gender Gender { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte Coloring { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IItem? HeldItem { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public uint CurrentHealth { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public float WeightInKm { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public float HeightInMeters { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte Happiness { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public StatisticSet<uint> FlatStats { get; } = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public StatBoostStatisticSet StatBoost { get; } = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public StatisticSet<uint> BoostedStats { get; } = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public IndividualValueStatisticSet IndividualValues { get; } = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public EffortValueStatisticSet EffortValues { get; } = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public INature Nature { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string? Nickname { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public AbilityIndex AbilityIndex { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IAbility? OverrideAbility { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IPokemonBattleData? BattleData { get; private set; }
|
||||
|
||||
private readonly ILearnedMove?[] _learnedMoves = new ILearnedMove[Const.MovesCount];
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<ILearnedMove?> Moves => _learnedMoves;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool AllowedExperience { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<TypeIdentifier> Types { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsEgg { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsCaught { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public ScriptContainer HeldItemTriggerScript { get; } = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public ScriptContainer AbilityScript { get; } = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public ScriptContainer StatusScript { get; } = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public IScriptSet Volatile { get; } = new ScriptSet();
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool HasHeldItem(StringKey itemName) => HeldItem?.Name == itemName;
|
||||
|
||||
/// <inheritdoc />
|
||||
public IItem? SetHeldItem(IItem? item)
|
||||
{
|
||||
var previous = HeldItem;
|
||||
HeldItem = item;
|
||||
return previous;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IItem? RemoveHeldItem()
|
||||
{
|
||||
var previous = HeldItem;
|
||||
HeldItem = null;
|
||||
return previous;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool ConsumeHeldItem()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ChangeStatBoost(Statistic stat, sbyte change, bool selfInflicted)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IAbility ActiveAbility
|
||||
{
|
||||
get
|
||||
{
|
||||
if (OverrideAbility != null)
|
||||
return OverrideAbility;
|
||||
var ability = Form.GetAbility(AbilityIndex);
|
||||
if (!Library.StaticLibrary.Abilities.TryGet(ability, out var abilityObj))
|
||||
throw new KeyNotFoundException($"Ability {ability} not found.");
|
||||
return abilityObj;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void RecalculateFlatStats()
|
||||
{
|
||||
Library.StatCalculator.CalculateFlatStats(this, FlatStats);
|
||||
RecalculateBoostedStats();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void RecalculateBoostedStats()
|
||||
{
|
||||
Library.StatCalculator.CalculateBoostedStats(this, BoostedStats);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ChangeSpecies(ISpecies species, IForm form)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ChangeForm(IForm form)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <remarks>
|
||||
/// Currently this checks the Pokemon is not an egg, not caught, and not fainted.
|
||||
/// </remarks>
|
||||
public bool IsUsable => !IsCaught && !IsEgg && !IsFainted;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsFainted => CurrentHealth == 0;
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Damage(uint damage, DamageSource source, EventBatchId batchId)
|
||||
{
|
||||
// If the Pokemon is already fainted, we don't need to do anything.
|
||||
if (IsFainted)
|
||||
return;
|
||||
// If the damage is more than the current health, we cap it at the current health, to prevent
|
||||
// underflow.
|
||||
if (damage >= CurrentHealth)
|
||||
damage = CurrentHealth;
|
||||
// Calculate the new health.
|
||||
var newHealth = CurrentHealth - damage;
|
||||
if (BattleData is not null)
|
||||
{
|
||||
// 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
|
||||
});
|
||||
// And allow scripts to execute.
|
||||
this.RunScriptHook(script => script.OnDamage(this, source, CurrentHealth, newHealth));
|
||||
}
|
||||
|
||||
CurrentHealth = newHealth;
|
||||
|
||||
// If the Pokémon is now fainted, we also run faint handling.
|
||||
if (IsFainted)
|
||||
{
|
||||
OnFaint(source);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnFaint(DamageSource source)
|
||||
{
|
||||
// If the Pokémon is not in a battle, we don't need to do anything.
|
||||
if (BattleData is null)
|
||||
return;
|
||||
|
||||
// Trigger the faint event to the front-end.
|
||||
BattleData.Battle.EventHook.Invoke(new FaintEvent(this));
|
||||
|
||||
// Allow scripts to trigger based on the faint.
|
||||
this.RunScriptHook(script => script.OnFaint(this, source));
|
||||
// Make sure the OnRemove script is run.
|
||||
this.RunScriptHook(script => script.OnRemove());
|
||||
|
||||
// Mark the position as unfillable if it can't be filled by any party.
|
||||
if (!BattleData.Battle.CanSlotBeFilled(BattleData.SideIndex, BattleData.Position))
|
||||
{
|
||||
BattleData.Battle.Sides[BattleData.SideIndex].MarkPositionAsUnfillable(BattleData.Position);
|
||||
}
|
||||
|
||||
// Validate the battle state to see if the battle is over.
|
||||
BattleData.Battle.ValidateBattleState();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool Heal(uint heal, bool allowRevive)
|
||||
{
|
||||
if (IsFainted && !allowRevive)
|
||||
return false;
|
||||
var maxAmount = this.BoostedStats.Hp - CurrentHealth;
|
||||
if (heal > maxAmount)
|
||||
heal = maxAmount;
|
||||
if (heal == 0)
|
||||
return false;
|
||||
var newHealth = CurrentHealth + heal;
|
||||
BattleData?.Battle.EventHook.Invoke(new HealEvent(this, CurrentHealth, newHealth));
|
||||
CurrentHealth = newHealth;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <remarks>
|
||||
/// If the index is 255, it will try to find the first empty move slot.
|
||||
/// </remarks>
|
||||
public void LearnMove(string moveName, MoveLearnMethod method, byte index)
|
||||
{
|
||||
if (index == 255)
|
||||
{
|
||||
for (byte i = 0; i < Moves.Count; i++)
|
||||
{
|
||||
if (Moves[i] is not null) continue;
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (index >= Moves.Count)
|
||||
throw new InvalidOperationException("No empty move slot found.");
|
||||
if (!Library.StaticLibrary.Moves.TryGet(moveName, out var move))
|
||||
throw new KeyNotFoundException($"Move {moveName} not found.");
|
||||
|
||||
_learnedMoves[index] = new LearnedMoveImpl(move, method);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ClearStatus()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ChangeLevelBy(int change)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int ScriptCount
|
||||
{
|
||||
get
|
||||
{
|
||||
var c = 4;
|
||||
if (BattleData != null)
|
||||
{
|
||||
var side = BattleData.Battle.Sides[BattleData.SideIndex];
|
||||
c += side.ScriptCount;
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void GetOwnScripts(List<IEnumerable<ScriptContainer>> scripts)
|
||||
{
|
||||
scripts.Add(HeldItemTriggerScript);
|
||||
scripts.Add(AbilityScript);
|
||||
scripts.Add(StatusScript);
|
||||
scripts.Add(Volatile);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void CollectScripts(List<IEnumerable<ScriptContainer>> scripts)
|
||||
{
|
||||
GetOwnScripts(scripts);
|
||||
if (BattleData != null)
|
||||
{
|
||||
var side = BattleData.Battle.Sides[BattleData.SideIndex];
|
||||
side.CollectScripts(scripts);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user