using JetBrains.Annotations;
using PkmnLib.Dynamic.Events;
using PkmnLib.Dynamic.Libraries;
using PkmnLib.Dynamic.Models.Serialized;
using PkmnLib.Dynamic.ScriptHandling;
using PkmnLib.Static;
using PkmnLib.Static.Species;
using PkmnLib.Static.Utils;
namespace PkmnLib.Dynamic.Models;
///
/// The data of a Pokemon.
///
public interface IPokemon : IScriptSource
{
///
/// The library data of the Pokemon.
///
IDynamicLibrary Library { get; }
///
/// The species of the Pokemon.
///
ISpecies Species { get; }
///
/// The form of the Pokemon.
///
IForm Form { get; }
///
/// An optional display species of the Pokemon. If this is set, the client should display this
/// species. An example of usage for this is the Illusion ability.
///
ISpecies? DisplaySpecies { get; }
///
/// An optional display form of the Pokemon. If this is set, the client should display this
/// form. An example of usage for this is the Illusion ability.
///
IForm? DisplayForm { get; }
///
/// The current level of the Pokemon.
///
LevelInt Level { get; }
///
/// The amount of experience of the Pokemon.
///
uint Experience { get; }
///
/// Increases the experience of the Pokemon. Returns whether any experience was gained.
///
bool AddExperience(uint experience);
///
/// The personality value of the Pokemon.
///
uint PersonalityValue { get; }
///
/// The gender of the Pokemon.
///
Gender Gender { get; }
///
/// The coloring of the Pokemon. Value 0 is the default, value 1 means shiny. Other values are
/// currently not used, and can be used for other implementations.
///
byte Coloring { get; }
///
/// The held item of the Pokemon.
///
IItem? HeldItem { get; }
///
/// The remaining health points of the Pokemon.
///
uint CurrentHealth { get; }
///
/// The weight of the Pokemon in kilograms.
///
float WeightInKg { get; set; }
///
/// The height of the Pokemon in meters.
///
float HeightInMeters { get; set; }
///
/// The happiness of the Pokemon. Also known as friendship.
///
byte Happiness { get; }
///
/// The stats of the Pokemon when disregarding any stat boosts.
///
StatisticSet FlatStats { get; }
///
/// The statistics boosts of the Pokemon. Will prevent the value from going above 6, and below
/// -6.
///
StatBoostStatisticSet StatBoost { get; }
///
/// The stats of the Pokemon including the stat boosts
///
StatisticSet BoostedStats { get; }
///
/// The maximum health of the Pokemon.
///
uint MaxHealth { get; }
///
/// The individual values of the Pokemon.
///
IndividualValueStatisticSet IndividualValues { get; }
///
/// The effort values of the Pokemon.
///
EffortValueStatisticSet EffortValues { get; }
///
/// The nature of the Pokemon.
///
INature Nature { get; }
///
/// An optional nickname of the Pokemon.
///
string? Nickname { get; }
///
/// An index of the ability to find the actual ability on the form.
///
AbilityIndex AbilityIndex { get; }
///
/// An ability can be overriden to an arbitrary ability. This is for example used for the Mummy
/// ability.
///
IAbility? OverrideAbility { get; }
///
/// If in battle, we have additional data.
///
IPokemonBattleData? BattleData { get; }
///
/// The moves the Pokemon has learned. This is of a set length of . Empty move slots
/// are null.
///
IReadOnlyList Moves { get; }
///
/// Checks whether the Pokemon has a specific move in its current moveset.
///
public bool HasMove(StringKey moveName);
///
/// Whether or not the Pokemon is allowed to gain experience.
///
bool AllowedExperience { get; }
///
/// The current types of the Pokemon.
///
IReadOnlyList Types { get; }
///
/// Whether or not this Pokemon is an egg.
///
bool IsEgg { get; }
///
/// Whether or not this Pokemon was caught this battle.
///
bool IsCaught { get; }
///
/// The script for the held item.
///
ScriptContainer HeldItemTriggerScript { get; }
///
/// The script for the ability.
///
ScriptContainer AbilityScript { get; }
///
/// The script for the status.
///
ScriptContainer StatusScript { get; }
///
/// The volatile status scripts of the Pokemon.
///
IScriptSet Volatile { get; }
///
/// Checks whether the Pokemon is holding an item with a specific name.
///
bool HasHeldItem(StringKey itemName);
///
/// Changes the held item of the Pokemon. Returns the previously held item.
///
[MustUseReturnValue]
IItem? SetHeldItem(IItem? item);
///
/// Removes the held item from the Pokemon. Returns the previously held item.
///
[MustUseReturnValue]
IItem? RemoveHeldItem();
///
/// Makes the Pokemon uses its held item. Returns whether the item was consumed.
///
bool ConsumeHeldItem();
///
/// Change a boosted stat by a certain amount.
///
/// The stat to be changed
/// The amount to change the stat by
/// Whether the change was self-inflicted. This can be relevant in scripts.
bool ChangeStatBoost(Statistic stat, sbyte change, bool selfInflicted);
///
/// Returns the currently active ability.
///
IAbility ActiveAbility { get; }
///
/// 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.
///
void RecalculateFlatStats();
///
/// Calculates the boosted stats on the Pokemon, _without_ recalculating the flat stats.
/// This should be called when a stat boost changes.
///
void RecalculateBoostedStats();
///
/// Change the species of the Pokemon.
///
void ChangeSpecies(ISpecies species, IForm form);
///
/// Change the form of the Pokemon.
///
void ChangeForm(IForm form, EventBatchId batchId = default);
///
/// Whether the Pokemon is useable in a battle.
///
bool IsUsable { get; }
///
/// Whether the Pokemon is fainted.
///
bool IsFainted { get; }
///
/// Damages the Pokemon by a certain amount of damage, from a damage source.
///
void Damage(uint damage, DamageSource source, EventBatchId batchId);
///
/// 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.
///
bool Heal(uint heal, bool allowRevive);
///
/// Restores all PP of the Pokemon.
///
void RestoreAllPP();
///
/// Learn a move by name.
///
void LearnMove(StringKey moveName, MoveLearnMethod method, byte index);
///
/// Removes the current non-volatile status from the Pokemon.
///
void ClearStatus();
///
/// Modifies the level by a certain amount
///
void ChangeLevelBy(int change);
///
/// Sets the current battle the Pokémon is in.
///
void SetBattleData(IBattle battle, byte sideIndex);
///
/// Sets whether the Pokémon is on the battlefield.
///
void SetOnBattlefield(bool onBattleField);
///
/// Sets the position the Pokémon has within its side.
///
///
void SetBattleSidePosition(byte position);
///
/// Marks a Pokemon as seen in the battle.
///
void MarkOpponentAsSeen(IPokemon pokemon);
///
/// Converts the data structure to a serializable format.
///
SerializedPokemon Serialize();
}
///
/// The data of the Pokémon related to being in a battle.
/// This is only set when the Pokémon is on the field in a battle.
///
public interface IPokemonBattleData
{
///
/// The battle the Pokémon is in.
///
IBattle Battle { get; internal set; }
///
/// The index of the side of the Pokémon
///
byte SideIndex { get; internal set; }
///
/// The index of the position of the Pokémon on the field
///
byte Position { get; internal set; }
///
/// A list of opponents the Pokémon has seen this battle.
///
IReadOnlyList SeenOpponents { get; }
///
/// Whether the Pokémon is on the battlefield.
///
bool IsOnBattlefield { get; set; }
///
/// Adds an opponent to the list of seen opponents.
///
void MarkOpponentAsSeen(IPokemon opponent);
}
///
public class PokemonImpl : ScriptSource, IPokemon
{
///
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);
WeightInKg = 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;
}
public PokemonImpl(IDynamicLibrary library, SerializedPokemon serializedPokemon)
{
Library = library;
if (!library.StaticLibrary.Species.TryGet(serializedPokemon.Species, out var species))
throw new KeyNotFoundException($"Species {serializedPokemon.Species} not found.");
Species = species;
if (!species.TryGetForm(serializedPokemon.Form, out var form))
throw new KeyNotFoundException($"Form {serializedPokemon.Form} not found on species {species.Name}.");
Form = form;
Level = serializedPokemon.Level;
Experience = serializedPokemon.Experience;
PersonalityValue = serializedPokemon.PersonalityValue;
Gender = serializedPokemon.Gender;
Coloring = serializedPokemon.Coloring;
if (serializedPokemon.HeldItem != null)
{
if (!library.StaticLibrary.Items.TryGet(serializedPokemon.HeldItem, out var item))
throw new KeyNotFoundException($"Item {serializedPokemon.HeldItem} not found.");
HeldItem = item;
}
CurrentHealth = serializedPokemon.CurrentHealth;
WeightInKg = form.Weight;
HeightInMeters = form.Height;
Happiness = serializedPokemon.Happiness;
IndividualValues = serializedPokemon.IndividualValues.ToIndividualValueStatisticSet();
EffortValues = serializedPokemon.EffortValues.ToEffortValueStatisticSet();
if (!library.StaticLibrary.Natures.TryGet(serializedPokemon.Nature, out var nature))
throw new KeyNotFoundException($"Nature {serializedPokemon.Nature} not found.");
Nature = nature;
Nickname = serializedPokemon.Nickname;
if (!library.StaticLibrary.Abilities.TryGet(serializedPokemon.Ability, out var ability))
throw new KeyNotFoundException($"Ability {serializedPokemon.Ability} not found.");
AbilityIndex = form.FindAbilityIndex(ability) ??
throw new KeyNotFoundException(
$"Ability {ability.Name} not found on species {species.Name} form {form.Name}.");
_learnedMoves = serializedPokemon.Moves.Select(move =>
{
if (move == null)
return null;
if (!library.StaticLibrary.Moves.TryGet(move.MoveName, out var moveData))
throw new KeyNotFoundException($"Move {move.MoveName} not found");
return new LearnedMoveImpl(moveData, move.LearnMethod, move.CurrentPp);
}).ToArray();
AllowedExperience = serializedPokemon.AllowedExperience;
IsEgg = serializedPokemon.IsEgg;
Types = form.Types;
RecalculateFlatStats();
if (serializedPokemon.Status != null)
{
if (!library.ScriptResolver.TryResolve(ScriptCategory.Status, serializedPokemon.Status,
out var statusScript))
throw new KeyNotFoundException($"Status script {serializedPokemon.Status} not found");
StatusScript.Set(statusScript);
}
}
///
public IDynamicLibrary Library { get; }
///
public ISpecies Species { get; }
///
public IForm Form { get; private set; }
///
public ISpecies? DisplaySpecies { get; set; }
///
public IForm? DisplayForm { get; set; }
///
public LevelInt Level { get; private set; }
///
public uint Experience { get; private set; }
///
public bool AddExperience(uint experience)
{
if (!AllowedExperience)
return false;
var maxLevel = Library.StaticLibrary.Settings.MaxLevel;
if (Level >= maxLevel)
return false;
var oldLevel = Level;
var oldExperience = Experience;
Experience += experience;
var newLevel = Library.StaticLibrary.GrowthRates.CalculateLevel(Species.GrowthRate, Experience);
if (newLevel > Level)
{
Level = newLevel;
RecalculateFlatStats();
BattleData?.Battle.EventHook.Invoke(new LevelUpEvent(this, oldLevel, Level));
if (newLevel >= maxLevel)
{
Experience = Library.StaticLibrary.GrowthRates.CalculateExperience(Species.GrowthRate, maxLevel);
}
}
return oldExperience != Experience;
}
///
public uint PersonalityValue { get; }
///
public Gender Gender { get; private set; }
///
public byte Coloring { get; }
///
public IItem? HeldItem { get; private set; }
///
public uint CurrentHealth { get; private set; }
///
public float WeightInKg { get; set; }
///
public float HeightInMeters { get; set; }
///
public byte Happiness { get; }
///
public StatisticSet FlatStats { get; } = new();
///
public StatBoostStatisticSet StatBoost { get; } = new();
///
public StatisticSet BoostedStats { get; } = new();
///
public uint MaxHealth => BoostedStats.Hp;
///
public IndividualValueStatisticSet IndividualValues { get; } = new();
///
public EffortValueStatisticSet EffortValues { get; } = new();
///
public INature Nature { get; }
///
public string? Nickname { get; set; }
///
public AbilityIndex AbilityIndex { get; }
///
public IAbility? OverrideAbility { get; private set; }
///
public IPokemonBattleData? BattleData { get; private set; }
private readonly ILearnedMove?[] _learnedMoves = new ILearnedMove[Const.MovesCount];
///
public IReadOnlyList Moves => _learnedMoves;
///
public bool HasMove(StringKey moveName) => Moves.Any(move => move?.MoveData.Name == moveName);
///
public bool AllowedExperience { get; set; }
///
public IReadOnlyList Types { get; private set; }
///
public bool IsEgg { get; private set; }
///
public bool IsCaught { get; private set; }
///
public ScriptContainer HeldItemTriggerScript { get; } = new();
///
public ScriptContainer AbilityScript { get; } = new();
///
public ScriptContainer StatusScript { get; } = new();
///
public IScriptSet Volatile { get; } = new ScriptSet();
///
public bool HasHeldItem(StringKey itemName) => HeldItem?.Name == itemName;
///
public IItem? SetHeldItem(IItem? item)
{
var previous = HeldItem;
HeldItem = item;
return previous;
}
///
public IItem? RemoveHeldItem()
{
var previous = HeldItem;
HeldItem = null;
return previous;
}
///
public bool ConsumeHeldItem()
{
if (HeldItem is null)
return false;
if (!Library.ScriptResolver.TryResolveItemScript(HeldItem, out _))
return false;
// TODO: actually consume the item
throw new NotImplementedException();
}
///
public bool ChangeStatBoost(Statistic stat, sbyte change, bool selfInflicted)
{
var prevented = false;
this.RunScriptHook(script => script.PreventStatBoostChange(this, stat, change, selfInflicted, ref prevented));
if (prevented)
return false;
this.RunScriptHook(script => script.ChangeStatBoostChange(this, stat, selfInflicted, ref change));
if (change == 0)
return false;
var changed = false;
var oldBoost = StatBoost.GetStatistic(stat);
changed = change switch
{
> 0 => StatBoost.IncreaseStatistic(stat, change),
< 0 => StatBoost.DecreaseStatistic(stat, change),
_ => changed,
};
if (!changed)
return false;
if (BattleData != null)
{
var newBoost = StatBoost.GetStatistic(stat);
BattleData.Battle.EventHook.Invoke(new StatBoostEvent(this, stat, oldBoost, newBoost));
}
RecalculateBoostedStats();
return true;
}
///
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;
}
}
///
public void RecalculateFlatStats()
{
Library.StatCalculator.CalculateFlatStats(this, FlatStats);
RecalculateBoostedStats();
}
///
public void RecalculateBoostedStats() => Library.StatCalculator.CalculateBoostedStats(this, BoostedStats);
///
public void ChangeSpecies(ISpecies species, IForm form)
{
if (Species == species)
{
if (form != Form)
ChangeForm(form, new EventBatchId());
return;
}
// If the Pokémon is genderless, but its new species is not, we want to set its gender
if (Gender != Gender.Genderless && species.GenderRate < 0.0)
{
var random = (IRandom?)BattleData?.Battle.Random ?? new RandomImpl();
Gender = species.GetRandomGender(random);
}
// Else if the new species is genderless, but the Pokémon has a gender, make the creature genderless.
else if (species.GenderRate < 0.0 && Gender != Gender.Genderless)
{
Gender = Gender.Genderless;
}
var batchId = new EventBatchId();
BattleData?.Battle.EventHook.Invoke(new SpeciesChangeEvent(this, species, form)
{
BatchId = batchId,
});
ChangeForm(form, batchId);
}
///
public void ChangeForm(IForm form, EventBatchId batchId)
{
if (form == Form)
return;
var oldAbility = Form.GetAbility(AbilityIndex);
Form = form;
Types = form.Types.ToList();
WeightInKg = form.Weight;
HeightInMeters = form.Height;
var newAbility = Form.GetAbility(AbilityIndex);
if (OverrideAbility == null && oldAbility != newAbility)
{
AbilityScript.Clear();
if (!Library.StaticLibrary.Abilities.TryGet(newAbility, out var ability))
throw new KeyNotFoundException($"Ability {newAbility} not found.");
if (Library.ScriptResolver.TryResolve(ScriptCategory.Ability, newAbility, out var abilityScript))
{
AbilityScript.Set(abilityScript);
abilityScript.OnInitialize(Library, ability.Parameters);
}
else
{
AbilityScript.Clear();
}
}
var oldHealth = BoostedStats.Hp;
RecalculateFlatStats();
var diffHealth = (long)BoostedStats.Hp - oldHealth;
if (diffHealth > 0)
{
Heal((uint)diffHealth, true);
}
// TODO: form specific moves?
BattleData?.Battle.EventHook.Invoke(new FormChangeEvent(this, form)
{
BatchId = batchId,
});
}
///
///
/// Currently this checks the Pokémon is not an egg, not caught, and not fainted.
///
public bool IsUsable => !IsCaught && !IsEgg && !IsFainted;
///
public bool IsFainted => CurrentHealth == 0;
///
public void Damage(uint damage, DamageSource source, EventBatchId batchId)
{
// If the Pokémon 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();
}
///
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;
}
///
public void RestoreAllPP()
{
foreach (var move in Moves)
{
move?.RestoreAllUses();
}
}
///
///
/// If the index is 255, it will try to find the first empty move slot.
///
public void LearnMove(StringKey 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);
}
///
public void ClearStatus() => StatusScript.Clear();
///
public void ChangeLevelBy(int change)
{
var newLevel = Level + change;
Level = (LevelInt)Math.Clamp(newLevel, 1, Library.StaticLibrary.Settings.MaxLevel);
RecalculateFlatStats();
}
///
public void SetBattleData(IBattle battle, byte sideIndex)
{
if (BattleData is not null)
{
BattleData.Battle = battle;
BattleData.SideIndex = sideIndex;
}
else
{
BattleData = new PokemonBattleDataImpl(battle, sideIndex);
}
}
///
public void SetOnBattlefield(bool onBattleField)
{
if (BattleData is not null)
{
BattleData.IsOnBattlefield = onBattleField;
if (!onBattleField)
{
Volatile.Clear();
WeightInKg = Form.Weight;
HeightInMeters = Form.Height;
}
}
}
///
public void SetBattleSidePosition(byte position)
{
if (BattleData is not null)
{
BattleData.Position = position;
}
}
///
public void MarkOpponentAsSeen(IPokemon pokemon) => BattleData?.MarkOpponentAsSeen(pokemon);
///
public SerializedPokemon Serialize() => new(this);
///
public override int ScriptCount
{
get
{
var c = 4;
if (BattleData != null)
{
var side = BattleData.Battle.Sides[BattleData.SideIndex];
c += side.ScriptCount;
}
return c;
}
}
///
public override void GetOwnScripts(List> scripts)
{
scripts.Add(HeldItemTriggerScript);
scripts.Add(AbilityScript);
scripts.Add(StatusScript);
scripts.Add(Volatile);
}
///
public override void CollectScripts(List> scripts)
{
GetOwnScripts(scripts);
if (BattleData != null)
{
var side = BattleData.Battle.Sides[BattleData.SideIndex];
side.CollectScripts(scripts);
}
}
}
///
public class PokemonBattleDataImpl : IPokemonBattleData
{
///
public PokemonBattleDataImpl(IBattle battle, byte sideIndex)
{
Battle = battle;
SideIndex = sideIndex;
}
///
public IBattle Battle { get; set; }
///
public byte SideIndex { get; set; }
///
public byte Position { get; set; }
private readonly List _seenOpponents = [];
///
public IReadOnlyList SeenOpponents => _seenOpponents;
///
public bool IsOnBattlefield { get; set; }
///
public void MarkOpponentAsSeen(IPokemon opponent)
{
_seenOpponents.Add(opponent);
}
}