Major rework of extern ref system for WASM, fixes most possible panics in WASM handling
All checks were successful
continuous-integration/drone Build is passing
All checks were successful
continuous-integration/drone Build is passing
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use std::ffi::c_void;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU8, Ordering};
|
||||
@@ -12,7 +13,9 @@ use crate::dynamic_data::event_hooks::Event;
|
||||
use crate::dynamic_data::models::battle::Battle;
|
||||
use crate::dynamic_data::models::learned_move::{LearnedMove, MoveLearnMethod};
|
||||
use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, ScriptWrapper};
|
||||
use crate::dynamic_data::{DynamicLibrary, Script, ScriptCategory, ScriptContainer, ScriptSet, VolatileScriptsOwner};
|
||||
use crate::dynamic_data::{
|
||||
DynamicLibrary, Script, ScriptCategory, ScriptContainer, ScriptSet, VolatileScriptsOwner, WeakBattleReference,
|
||||
};
|
||||
use crate::static_data::AbilityIndex;
|
||||
use crate::static_data::Form;
|
||||
use crate::static_data::Gender;
|
||||
@@ -26,8 +29,8 @@ use crate::utils::Random;
|
||||
use crate::{script_hook, PkmnError, StringKey, ValueIdentifiable, ValueIdentifier, VecExt};
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
|
||||
/// An individual Pokemon as we know and love them.
|
||||
pub struct Pokemon {
|
||||
/// The data of a Pokemon.
|
||||
struct PokemonData {
|
||||
/// A unique identifier so we know what value this is.
|
||||
identifier: ValueIdentifier,
|
||||
/// The library data of the Pokemon.
|
||||
@@ -67,16 +70,16 @@ pub struct Pokemon {
|
||||
height: Atomic<f32>,
|
||||
|
||||
/// The stats of the Pokemon when disregarding any stat boosts.
|
||||
flat_stats: StatisticSet<u32>,
|
||||
flat_stats: Arc<StatisticSet<u32>>,
|
||||
/// The statistics boosts of the Pokemon. Will prevent the value from going above 6, and below
|
||||
/// -6.
|
||||
stat_boost: ClampedStatisticSet<i8, -6, 6>,
|
||||
stat_boost: Arc<ClampedStatisticSet<i8, -6, 6>>,
|
||||
/// The stats of the Pokemon including the stat boosts
|
||||
boosted_stats: StatisticSet<u32>,
|
||||
boosted_stats: Arc<StatisticSet<u32>>,
|
||||
/// The [individual values](https://bulbapedia.bulbagarden.net/wiki/Individual_values) of the Pokemon.
|
||||
individual_values: ClampedStatisticSet<u8, 0, 31>,
|
||||
individual_values: Arc<ClampedStatisticSet<u8, 0, 31>>,
|
||||
/// The [effort values](https://bulbapedia.bulbagarden.net/wiki/Effort_values) of the Pokemon.
|
||||
effort_values: ClampedStatisticSet<u8, 0, 252>,
|
||||
effort_values: Arc<ClampedStatisticSet<u8, 0, 252>>,
|
||||
/// The [nature](https://bulbapedia.bulbagarden.net/wiki/Nature) of the Pokemon.
|
||||
nature: Arc<dyn Nature>,
|
||||
|
||||
@@ -118,6 +121,24 @@ pub struct Pokemon {
|
||||
script_source_data: RwLock<ScriptSourceData>,
|
||||
}
|
||||
|
||||
/// An individual Pokemon.
|
||||
#[derive(Clone)]
|
||||
pub struct Pokemon {
|
||||
/// The data of the Pokemon.
|
||||
data: Arc<PokemonData>,
|
||||
}
|
||||
|
||||
/// A non-owning reference to a Pokemon.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct WeakPokemonReference {
|
||||
/// The weak reference to the data.
|
||||
data: Weak<PokemonData>,
|
||||
}
|
||||
|
||||
unsafe impl Send for WeakPokemonReference {}
|
||||
|
||||
unsafe impl Sync for WeakPokemonReference {}
|
||||
|
||||
impl Pokemon {
|
||||
/// Instantiates a new Pokemon.
|
||||
pub fn new(
|
||||
@@ -143,7 +164,7 @@ impl Pokemon {
|
||||
.natures()
|
||||
.get_nature(nature)
|
||||
.ok_or(PkmnError::InvalidNatureName { nature: nature.clone() })?;
|
||||
let mut pokemon = Self {
|
||||
let pokemon_data = PokemonData {
|
||||
identifier: Default::default(),
|
||||
library,
|
||||
species: RwLock::new(species),
|
||||
@@ -180,28 +201,32 @@ impl Pokemon {
|
||||
volatile: Default::default(),
|
||||
script_source_data: Default::default(),
|
||||
};
|
||||
|
||||
let pokemon = Self {
|
||||
data: Arc::new(pokemon_data),
|
||||
};
|
||||
pokemon.recalculate_flat_stats()?;
|
||||
let health = pokemon.flat_stats().hp();
|
||||
pokemon.current_health = AtomicU32::new(health);
|
||||
pokemon.data.current_health.store(health, Ordering::Relaxed);
|
||||
|
||||
Ok(pokemon)
|
||||
}
|
||||
|
||||
/// The library data of the Pokemon.
|
||||
pub fn library(&self) -> &Arc<dyn DynamicLibrary> {
|
||||
&self.library
|
||||
&self.data.library
|
||||
}
|
||||
/// The species of the Pokemon.
|
||||
pub fn species(&self) -> Arc<dyn Species> {
|
||||
self.species.read().clone()
|
||||
self.data.species.read().clone()
|
||||
}
|
||||
/// The form of the Pokemon.
|
||||
pub fn form(&self) -> Arc<dyn Form> {
|
||||
self.form.read().clone()
|
||||
self.data.form.read().clone()
|
||||
}
|
||||
/// The species that should be displayed to the user. This handles stuff like the Illusion ability.
|
||||
pub fn display_species(&self) -> Arc<dyn Species> {
|
||||
if let Some(v) = &self.display_species {
|
||||
if let Some(v) = &self.data.display_species {
|
||||
v.clone()
|
||||
} else {
|
||||
self.species()
|
||||
@@ -209,7 +234,7 @@ impl Pokemon {
|
||||
}
|
||||
/// The form that should be displayed to the user. This handles stuff like the Illusion ability.
|
||||
pub fn display_form(&self) -> Arc<dyn Form> {
|
||||
if let Some(v) = &self.display_form {
|
||||
if let Some(v) = &self.data.display_form {
|
||||
v.clone()
|
||||
} else {
|
||||
self.form()
|
||||
@@ -217,53 +242,57 @@ impl Pokemon {
|
||||
}
|
||||
/// The current level of the Pokemon.
|
||||
pub fn level(&self) -> LevelInt {
|
||||
self.level.load(Ordering::Relaxed)
|
||||
self.data.level.load(Ordering::Relaxed)
|
||||
}
|
||||
/// The amount of experience of the Pokemon.
|
||||
pub fn experience(&self) -> u32 {
|
||||
self.experience.load(Ordering::Relaxed)
|
||||
self.data.experience.load(Ordering::Relaxed)
|
||||
}
|
||||
/// A unique random number for this Pokemon.
|
||||
pub fn unique_identifier(&self) -> u32 {
|
||||
self.unique_identifier
|
||||
self.data.unique_identifier
|
||||
}
|
||||
/// The gender of the Pokemon.
|
||||
pub fn gender(&self) -> Gender {
|
||||
*self.gender.read()
|
||||
*self.data.gender.read()
|
||||
}
|
||||
/// 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.
|
||||
pub fn coloring(&self) -> u8 {
|
||||
self.coloring
|
||||
self.data.coloring
|
||||
}
|
||||
/// Gets the held item of a Pokemon
|
||||
pub fn held_item(&self) -> &RwLock<Option<Arc<dyn Item>>> {
|
||||
&self.held_item
|
||||
&self.data.held_item
|
||||
}
|
||||
/// Checks whether the Pokemon is holding a specific item.
|
||||
pub fn has_held_item(&self, name: &StringKey) -> bool {
|
||||
// Only true if we have an item, and the item name is the same as the requested item.
|
||||
if let Some(v) = self.held_item.read().deref() {
|
||||
if let Some(v) = self.data.held_item.read().deref() {
|
||||
return v.name() == name;
|
||||
}
|
||||
false
|
||||
}
|
||||
/// Changes the held item of the Pokemon. Returns the previously held item.
|
||||
pub fn set_held_item(&self, item: &Arc<dyn Item>) -> Option<Arc<dyn Item>> {
|
||||
self.held_item.write().replace(item.clone())
|
||||
self.data.held_item.write().replace(item.clone())
|
||||
}
|
||||
/// Removes the held item from the Pokemon. Returns the previously held item.
|
||||
pub fn remove_held_item(&self) -> Option<Arc<dyn Item>> {
|
||||
self.held_item.write().take()
|
||||
self.data.held_item.write().take()
|
||||
}
|
||||
/// Makes the Pokemon uses its held item.
|
||||
pub fn consume_held_item(&self) -> Result<bool> {
|
||||
if self.held_item.read().is_none() {
|
||||
if self.data.held_item.read().is_none() {
|
||||
return Ok(false);
|
||||
}
|
||||
let script = self
|
||||
.library
|
||||
.load_item_script(self.held_item.read().as_ref().ok_or(PkmnError::UnableToAcquireLock)?)?;
|
||||
let script = self.data.library.load_item_script(
|
||||
self.data
|
||||
.held_item
|
||||
.read()
|
||||
.as_ref()
|
||||
.ok_or(PkmnError::UnableToAcquireLock)?,
|
||||
)?;
|
||||
if script.is_none() {
|
||||
return Ok(false);
|
||||
}
|
||||
@@ -274,60 +303,60 @@ impl Pokemon {
|
||||
|
||||
/// The remaining health points of the Pokemon.
|
||||
pub fn current_health(&self) -> u32 {
|
||||
self.current_health.load(Ordering::Relaxed)
|
||||
self.data.current_health.load(Ordering::Relaxed)
|
||||
}
|
||||
/// The max health points of the Pokemon.
|
||||
pub fn max_health(&self) -> u32 {
|
||||
self.boosted_stats.hp()
|
||||
self.data.boosted_stats.hp()
|
||||
}
|
||||
/// The weight of the Pokemon in kilograms.
|
||||
pub fn weight(&self) -> f32 {
|
||||
self.weight.load(Ordering::Relaxed)
|
||||
self.data.weight.load(Ordering::Relaxed)
|
||||
}
|
||||
/// Sets the weight of the Pokemon in kilograms.
|
||||
pub fn set_weight(&self, weight: f32) {
|
||||
self.weight.store(weight, Ordering::Relaxed)
|
||||
self.data.weight.store(weight, Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// The height of the Pokemon in meters.
|
||||
pub fn height(&self) -> f32 {
|
||||
self.height.load(Ordering::Relaxed)
|
||||
self.data.height.load(Ordering::Relaxed)
|
||||
}
|
||||
/// An optional nickname of the Pokemon.
|
||||
pub fn nickname(&self) -> &Option<String> {
|
||||
&self.nickname
|
||||
&self.data.nickname
|
||||
}
|
||||
/// An index of the ability to find the actual ability on the form.
|
||||
pub fn real_ability(&self) -> &AbilityIndex {
|
||||
&self.ability_index
|
||||
&self.data.ability_index
|
||||
}
|
||||
/// The current types of the Pokemon.
|
||||
pub fn types(&self) -> RwLockReadGuard<'_, RawRwLock, Vec<TypeIdentifier>> {
|
||||
self.types.read()
|
||||
self.data.types.read()
|
||||
}
|
||||
/// The moves the Pokemon has learned. This is of a set length of [`MAX_MOVES`]. Empty move slots
|
||||
/// are defined by None.
|
||||
pub fn learned_moves(&self) -> &RwLock<[Option<Arc<LearnedMove>>; MAX_MOVES]> {
|
||||
&self.moves
|
||||
&self.data.moves
|
||||
}
|
||||
|
||||
/// The stats of the Pokemon when disregarding any stat boosts.
|
||||
pub fn flat_stats(&self) -> &StatisticSet<u32> {
|
||||
&self.flat_stats
|
||||
pub fn flat_stats(&self) -> &Arc<StatisticSet<u32>> {
|
||||
&self.data.flat_stats
|
||||
}
|
||||
|
||||
/// The amount of boosts on a specific stat.
|
||||
pub fn stat_boosts(&self) -> &ClampedStatisticSet<i8, -6, 6> {
|
||||
&self.stat_boost
|
||||
pub fn stat_boosts(&self) -> &Arc<ClampedStatisticSet<i8, -6, 6>> {
|
||||
&self.data.stat_boost
|
||||
}
|
||||
|
||||
/// The stats of the Pokemon including the stat boosts
|
||||
pub fn boosted_stats(&self) -> &StatisticSet<u32> {
|
||||
&self.boosted_stats
|
||||
pub fn boosted_stats(&self) -> &Arc<StatisticSet<u32>> {
|
||||
&self.data.boosted_stats
|
||||
}
|
||||
/// Get the stat boosts for a specific stat.
|
||||
pub fn stat_boost(&self, stat: Statistic) -> i8 {
|
||||
self.stat_boost.get_stat(stat)
|
||||
self.data.stat_boost.get_stat(stat)
|
||||
}
|
||||
/// Change a boosted stat by a certain amount.
|
||||
pub fn change_stat_boost(&self, stat: Statistic, mut diff_amount: i8, self_inflicted: bool) -> Result<bool> {
|
||||
@@ -357,13 +386,13 @@ impl Pokemon {
|
||||
}
|
||||
|
||||
let mut changed = false;
|
||||
let old_value = self.stat_boost.get_stat(stat);
|
||||
let old_value = self.data.stat_boost.get_stat(stat);
|
||||
match diff_amount.cmp(&0_i8) {
|
||||
std::cmp::Ordering::Less => {
|
||||
changed = self.stat_boost.decrease_stat(stat, -diff_amount);
|
||||
changed = self.data.stat_boost.decrease_stat(stat, -diff_amount);
|
||||
}
|
||||
std::cmp::Ordering::Greater => {
|
||||
changed = self.stat_boost.increase_stat(stat, -diff_amount);
|
||||
changed = self.data.stat_boost.increase_stat(stat, -diff_amount);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -383,17 +412,17 @@ impl Pokemon {
|
||||
}
|
||||
|
||||
/// The [individual values](https://bulbapedia.bulbagarden.net/wiki/Individual_values) of the Pokemon.
|
||||
pub fn individual_values(&self) -> &ClampedStatisticSet<u8, 0, 31> {
|
||||
&self.individual_values
|
||||
pub fn individual_values(&self) -> &Arc<ClampedStatisticSet<u8, 0, 31>> {
|
||||
&self.data.individual_values
|
||||
}
|
||||
/// The [effort values](https://bulbapedia.bulbagarden.net/wiki/Effort_values) of the Pokemon.
|
||||
pub fn effort_values(&self) -> &ClampedStatisticSet<u8, 0, 252> {
|
||||
&self.effort_values
|
||||
pub fn effort_values(&self) -> &Arc<ClampedStatisticSet<u8, 0, 252>> {
|
||||
&self.data.effort_values
|
||||
}
|
||||
|
||||
/// Gets the battle the battle is currently in.
|
||||
pub fn get_battle(&self) -> Option<Arc<Battle>> {
|
||||
let r = self.battle_data.read();
|
||||
pub fn get_battle(&self) -> Option<Battle> {
|
||||
let r = self.data.battle_data.read();
|
||||
if let Some(data) = &r.deref() {
|
||||
data.battle.upgrade()
|
||||
} else {
|
||||
@@ -403,26 +432,31 @@ impl Pokemon {
|
||||
/// Get the index of the side of the battle the Pokemon is in. Only returns a value if the Pokemon
|
||||
/// is on the battlefield.
|
||||
pub fn get_battle_side_index(&self) -> Option<u8> {
|
||||
self.battle_data.read().as_ref().map(|data| data.battle_side_index())
|
||||
self.data
|
||||
.battle_data
|
||||
.read()
|
||||
.as_ref()
|
||||
.map(|data| data.battle_side_index())
|
||||
}
|
||||
/// Get the index of the slot on the side of the battle the Pokemon is in. Only returns a value
|
||||
/// if the Pokemon is on the battlefield.
|
||||
pub fn get_battle_index(&self) -> Option<u8> {
|
||||
self.battle_data.read().as_ref().map(|data| data.index())
|
||||
self.data.battle_data.read().as_ref().map(|data| data.index())
|
||||
}
|
||||
/// Returns whether something overrides the ability.
|
||||
pub fn is_ability_overriden(&self) -> bool {
|
||||
self.override_ability.is_some()
|
||||
self.data.override_ability.is_some()
|
||||
}
|
||||
/// Returns the currently active ability.
|
||||
pub fn active_ability(&self) -> Result<Arc<dyn Ability>> {
|
||||
if let Some(v) = &self.override_ability {
|
||||
if let Some(v) = &self.data.override_ability {
|
||||
return Ok(v.clone());
|
||||
}
|
||||
|
||||
let form = self.form();
|
||||
let ability = form.get_ability(self.ability_index)?;
|
||||
let ability = form.get_ability(self.data.ability_index)?;
|
||||
Ok(self
|
||||
.data
|
||||
.library
|
||||
.static_data()
|
||||
.abilities()
|
||||
@@ -434,68 +468,70 @@ impl Pokemon {
|
||||
|
||||
/// The script for the status.
|
||||
pub fn status(&self) -> &ScriptContainer {
|
||||
&self.status_script
|
||||
&self.data.status_script
|
||||
}
|
||||
|
||||
/// Returns the script for the currently active ability.
|
||||
pub fn ability_script(&self) -> &ScriptContainer {
|
||||
&self.ability_script
|
||||
&self.data.ability_script
|
||||
}
|
||||
|
||||
/// Whether or not the Pokemon is allowed to gain experience.
|
||||
pub fn allowed_experience_gain(&self) -> bool {
|
||||
self.allowed_experience
|
||||
self.data.allowed_experience
|
||||
}
|
||||
|
||||
/// The [nature](https://bulbapedia.bulbagarden.net/wiki/Nature) of the Pokemon.
|
||||
pub fn nature(&self) -> &Arc<dyn Nature> {
|
||||
&self.nature
|
||||
&self.data.nature
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub fn recalculate_flat_stats(&self) -> Result<()> {
|
||||
self.library
|
||||
self.data
|
||||
.library
|
||||
.stat_calculator()
|
||||
.calculate_flat_stats(self, &self.flat_stats)?;
|
||||
.calculate_flat_stats(self, &self.data.flat_stats)?;
|
||||
self.recalculate_boosted_stats()?;
|
||||
Ok(())
|
||||
}
|
||||
/// Calculates the boosted stats on the Pokemon, _without_ recalculating the flat stats.
|
||||
/// This should be called when a stat boost changes.
|
||||
pub fn recalculate_boosted_stats(&self) -> Result<()> {
|
||||
self.library
|
||||
self.data
|
||||
.library
|
||||
.stat_calculator()
|
||||
.calculate_boosted_stats(self, &self.boosted_stats)
|
||||
.calculate_boosted_stats(self, &self.data.boosted_stats)
|
||||
}
|
||||
|
||||
/// Change the species of the Pokemon.
|
||||
pub fn change_species(&self, species: Arc<dyn Species>, form: Arc<dyn Form>) -> Result<()> {
|
||||
*self.species.write() = species.clone();
|
||||
*self.form.write() = form.clone();
|
||||
*self.data.species.write() = species.clone();
|
||||
*self.data.form.write() = form.clone();
|
||||
|
||||
// If the pokemon is genderless, but it's new species is not, we want to set its gender
|
||||
if self.gender() != Gender::Genderless && species.gender_rate() < 0.0 {
|
||||
// If we're in battle, use the battle random for predictability
|
||||
let r = self.battle_data.read();
|
||||
let r = self.data.battle_data.read();
|
||||
if let Some(data) = r.deref() {
|
||||
let battle = data.battle().ok_or(anyhow!("Battle not set"))?;
|
||||
let mut random = match battle.random().get_rng().lock() {
|
||||
Ok(v) => v,
|
||||
Err(_) => return Err(PkmnError::UnableToAcquireLock.into()),
|
||||
};
|
||||
*self.gender.write() = species.get_random_gender(random.deref_mut());
|
||||
*self.data.gender.write() = species.get_random_gender(random.deref_mut());
|
||||
} else {
|
||||
// If we're not in battle, just use a new random.
|
||||
*self.gender.write() = species.get_random_gender(&mut Random::default());
|
||||
*self.data.gender.write() = species.get_random_gender(&mut Random::default());
|
||||
}
|
||||
}
|
||||
// Else if the new species is genderless, but the pokemon has a gender, make the creature genderless.
|
||||
else if species.gender_rate() < 0.0 && self.gender() != Gender::Genderless {
|
||||
*self.gender.write() = Gender::Genderless;
|
||||
*self.data.gender.write() = Gender::Genderless;
|
||||
}
|
||||
let r = self.battle_data.read();
|
||||
let r = self.data.battle_data.read();
|
||||
if let Some(battle_data) = &r.deref() {
|
||||
if let Some(battle) = battle_data.battle() {
|
||||
battle.event_hook().trigger(Event::SpeciesChange {
|
||||
@@ -513,29 +549,31 @@ impl Pokemon {
|
||||
if self.form().value_identifier() == form.value_identifier() {
|
||||
return Ok(());
|
||||
}
|
||||
*self.form.write() = form.clone();
|
||||
*self.data.form.write() = form.clone();
|
||||
|
||||
{
|
||||
let mut type_lock = self.types.write();
|
||||
let mut type_lock = self.data.types.write();
|
||||
type_lock.clear();
|
||||
for t in form.types() {
|
||||
type_lock.push(*t);
|
||||
}
|
||||
}
|
||||
self.weight.store(form.weight(), Ordering::SeqCst);
|
||||
self.height.store(form.height(), Ordering::SeqCst);
|
||||
self.data.weight.store(form.weight(), Ordering::SeqCst);
|
||||
self.data.height.store(form.height(), Ordering::SeqCst);
|
||||
|
||||
let ability = self.active_ability()?;
|
||||
let ability_script = self
|
||||
.data
|
||||
.library
|
||||
.load_script(self.into(), ScriptCategory::Ability, ability.name())?;
|
||||
if let Some(ability_script) = ability_script {
|
||||
let script_result = self
|
||||
.data
|
||||
.ability_script
|
||||
.set(ability_script)
|
||||
.as_ref()
|
||||
// Ensure the ability script gets initialized with the parameters for the ability.
|
||||
.on_initialize(&self.library, ability.parameters().to_vec());
|
||||
.on_initialize(&self.data.library, ability.parameters().to_vec());
|
||||
match script_result {
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
@@ -543,21 +581,23 @@ impl Pokemon {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.ability_script.clear();
|
||||
self.data.ability_script.clear();
|
||||
}
|
||||
let old_health = self.max_health();
|
||||
self.recalculate_flat_stats()?;
|
||||
let diff_health = (self.max_health() - old_health) as i32;
|
||||
if self.current_health() == 0 && (self.current_health() as i32) < -diff_health {
|
||||
self.current_health.store(0, Ordering::SeqCst);
|
||||
self.data.current_health.store(0, Ordering::SeqCst);
|
||||
} else if diff_health < 0 {
|
||||
self.current_health.fetch_sub(-diff_health as u32, Ordering::SeqCst);
|
||||
self.data
|
||||
.current_health
|
||||
.fetch_sub(-diff_health as u32, Ordering::SeqCst);
|
||||
} else {
|
||||
self.current_health.fetch_add(diff_health as u32, Ordering::SeqCst);
|
||||
self.data.current_health.fetch_add(diff_health as u32, Ordering::SeqCst);
|
||||
}
|
||||
// TODO: consider form specific attacks?
|
||||
|
||||
let r = self.battle_data.read();
|
||||
let r = self.data.battle_data.read();
|
||||
if let Some(battle_data) = r.deref() {
|
||||
if let Some(battle) = battle_data.battle() {
|
||||
battle.event_hook().trigger(Event::FormChange {
|
||||
@@ -571,7 +611,7 @@ impl Pokemon {
|
||||
|
||||
/// Whether or not the Pokemon is useable in a battle.
|
||||
pub fn is_usable(&self) -> bool {
|
||||
!self.is_caught && !self.is_egg && !self.is_fainted()
|
||||
!self.data.is_caught && !self.data.is_egg && !self.is_fainted()
|
||||
}
|
||||
|
||||
/// Returns whether the Pokemon is fainted.
|
||||
@@ -580,8 +620,8 @@ impl Pokemon {
|
||||
}
|
||||
|
||||
/// Sets the current battle the Pokemon is in.
|
||||
pub fn set_battle_data(&self, battle: Weak<Battle>, battle_side_index: u8) {
|
||||
let mut w = self.battle_data.write();
|
||||
pub fn set_battle_data(&self, battle: WeakBattleReference, battle_side_index: u8) {
|
||||
let mut w = self.data.battle_data.write();
|
||||
if let Some(battle_data) = w.deref_mut() {
|
||||
battle_data.battle = battle;
|
||||
battle_data.battle_side_index.store(battle_side_index, Ordering::SeqCst);
|
||||
@@ -598,13 +638,13 @@ impl Pokemon {
|
||||
|
||||
/// Sets whether or not the Pokemon is on the battlefield.
|
||||
pub fn set_on_battlefield(&self, value: bool) -> Result<()> {
|
||||
let r = self.battle_data.read();
|
||||
let r = self.data.battle_data.read();
|
||||
if let Some(data) = &mut r.deref() {
|
||||
data.on_battle_field.store(value, Ordering::SeqCst);
|
||||
if !value {
|
||||
self.volatile.clear()?;
|
||||
self.weight.store(self.form().weight(), Ordering::SeqCst);
|
||||
self.height.store(self.form().height(), Ordering::SeqCst);
|
||||
self.data.volatile.clear()?;
|
||||
self.data.weight.store(self.form().weight(), Ordering::SeqCst);
|
||||
self.data.height.store(self.form().height(), Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -612,7 +652,7 @@ impl Pokemon {
|
||||
|
||||
/// Sets the index of the slot of the side the Pokemon is on.
|
||||
pub fn set_battle_index(&self, index: u8) {
|
||||
let r = self.battle_data.read();
|
||||
let r = self.data.battle_data.read();
|
||||
if let Some(data) = r.deref() {
|
||||
data.index.store(index, Ordering::SeqCst)
|
||||
}
|
||||
@@ -620,16 +660,20 @@ impl Pokemon {
|
||||
|
||||
/// Whether or not the Pokemon is on the battlefield.
|
||||
pub fn is_on_battlefield(&self) -> bool {
|
||||
self.battle_data.read().as_ref().is_some_and(|a| a.on_battle_field())
|
||||
self.data
|
||||
.battle_data
|
||||
.read()
|
||||
.as_ref()
|
||||
.is_some_and(|a| a.on_battle_field())
|
||||
}
|
||||
|
||||
/// Marks an opponent as seen, for use in experience gain.
|
||||
pub fn mark_opponent_as_seen(&self, pokemon: Weak<Pokemon>) {
|
||||
let r = self.battle_data.read();
|
||||
if let Some(battle_data) = &r.deref() {
|
||||
pub fn mark_opponent_as_seen(&self, pokemon: WeakPokemonReference) {
|
||||
let r = self.data.battle_data.read();
|
||||
if let Some(battle_data) = r.deref() {
|
||||
let mut opponents = battle_data.seen_opponents().write();
|
||||
for seen_opponent in opponents.deref() {
|
||||
if seen_opponent.ptr_eq(&pokemon) {
|
||||
if seen_opponent.eq(&pokemon) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -646,7 +690,7 @@ impl Pokemon {
|
||||
return Ok(());
|
||||
}
|
||||
let new_health = self.current_health() - damage;
|
||||
if let Some(battle_data) = &self.battle_data.read().deref() {
|
||||
if let Some(battle_data) = &self.data.battle_data.read().deref() {
|
||||
if let Some(battle) = battle_data.battle() {
|
||||
battle.event_hook().trigger(Event::Damage {
|
||||
pokemon: self,
|
||||
@@ -656,11 +700,17 @@ impl Pokemon {
|
||||
});
|
||||
}
|
||||
}
|
||||
if self.battle_data.read().as_ref().is_some_and(|a| a.on_battle_field()) {
|
||||
if self
|
||||
.data
|
||||
.battle_data
|
||||
.read()
|
||||
.as_ref()
|
||||
.is_some_and(|a| a.on_battle_field())
|
||||
{
|
||||
script_hook!(on_damage, self, self, source, self.current_health(), new_health);
|
||||
}
|
||||
|
||||
self.current_health.store(new_health, Ordering::SeqCst);
|
||||
self.data.current_health.store(new_health, Ordering::SeqCst);
|
||||
if self.is_fainted() && damage > 0 {
|
||||
self.on_faint(source)?;
|
||||
}
|
||||
@@ -669,7 +719,7 @@ impl Pokemon {
|
||||
|
||||
/// Triggers when the Pokemon faints.
|
||||
fn on_faint(&self, source: DamageSource) -> Result<()> {
|
||||
let r = self.battle_data.read();
|
||||
let r = self.data.battle_data.read();
|
||||
if let Some(battle_data) = r.deref() {
|
||||
if let Some(battle) = battle_data.battle() {
|
||||
battle.event_hook().trigger(Event::Faint { pokemon: self });
|
||||
@@ -703,7 +753,7 @@ impl Pokemon {
|
||||
return false;
|
||||
}
|
||||
let new_health = self.current_health() + max_amount;
|
||||
if let Some(battle_data) = &self.battle_data.read().deref() {
|
||||
if let Some(battle_data) = &self.data.battle_data.read().deref() {
|
||||
if let Some(battle) = battle_data.battle() {
|
||||
battle.event_hook().trigger(Event::Heal {
|
||||
pokemon: self,
|
||||
@@ -712,7 +762,7 @@ impl Pokemon {
|
||||
});
|
||||
}
|
||||
}
|
||||
self.current_health.store(new_health, Ordering::SeqCst);
|
||||
self.data.current_health.store(new_health, Ordering::SeqCst);
|
||||
true
|
||||
}
|
||||
|
||||
@@ -727,6 +777,7 @@ impl Pokemon {
|
||||
}
|
||||
};
|
||||
let move_data = self
|
||||
.data
|
||||
.library
|
||||
.static_data()
|
||||
.moves()
|
||||
@@ -742,12 +793,13 @@ impl Pokemon {
|
||||
|
||||
/// Removes the current non-volatile status from the Pokemon.
|
||||
pub fn clear_status(&self) {
|
||||
self.status_script.clear()
|
||||
self.data.status_script.clear()
|
||||
}
|
||||
|
||||
/// Increases the level by a certain amount
|
||||
pub fn change_level_by(&self, amount: LevelInt) -> Result<()> {
|
||||
self.level
|
||||
self.data
|
||||
.level
|
||||
.fetch_update(Ordering::SeqCst, Ordering::Relaxed, |x| {
|
||||
let max_level = self.library().static_data().settings().maximum_level();
|
||||
if x + amount > max_level {
|
||||
@@ -759,13 +811,50 @@ impl Pokemon {
|
||||
.ok();
|
||||
self.recalculate_flat_stats()
|
||||
}
|
||||
|
||||
/// Take a weak reference to the Pokemon.
|
||||
pub fn weak(&self) -> WeakPokemonReference {
|
||||
WeakPokemonReference {
|
||||
data: Arc::downgrade(&self.data),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Pokemon {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
Arc::ptr_eq(&self.data, &other.data)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Pokemon {}
|
||||
|
||||
impl PartialEq for WeakPokemonReference {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
Weak::ptr_eq(&self.data, &other.data)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for WeakPokemonReference {}
|
||||
|
||||
impl WeakPokemonReference {
|
||||
/// Attempts to upgrade the weak reference to a strong reference.
|
||||
pub fn upgrade(&self) -> Option<Pokemon> {
|
||||
Some(Pokemon {
|
||||
data: self.data.upgrade()?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Gets the pointer to the underlying data.
|
||||
pub(crate) fn as_ptr(&self) -> *const c_void {
|
||||
self.data.as_ptr() as *const c_void
|
||||
}
|
||||
}
|
||||
|
||||
/// The data of the Pokemon related to being in a battle.
|
||||
#[derive(Debug)]
|
||||
pub struct PokemonBattleData {
|
||||
/// The battle data of the Pokemon
|
||||
battle: Weak<Battle>,
|
||||
battle: WeakBattleReference,
|
||||
/// The index of the side of the Pokemon
|
||||
battle_side_index: AtomicU8,
|
||||
/// The index of the slot on the side of the Pokemon.
|
||||
@@ -773,16 +862,12 @@ pub struct PokemonBattleData {
|
||||
/// Whether or not the Pokemon is on the battlefield.
|
||||
on_battle_field: AtomicBool,
|
||||
/// A list of opponents the Pokemon has seen this battle.
|
||||
seen_opponents: RwLock<Vec<Weak<Pokemon>>>,
|
||||
seen_opponents: RwLock<Vec<WeakPokemonReference>>,
|
||||
}
|
||||
|
||||
impl PokemonBattleData {
|
||||
/// The battle data of the Pokemon
|
||||
pub fn battle_mut(&mut self) -> Option<Arc<Battle>> {
|
||||
self.battle.upgrade()
|
||||
}
|
||||
/// The battle data of the Pokemon
|
||||
pub fn battle(&self) -> Option<Arc<Battle>> {
|
||||
pub fn battle(&self) -> Option<Battle> {
|
||||
self.battle.upgrade()
|
||||
}
|
||||
|
||||
@@ -799,7 +884,7 @@ impl PokemonBattleData {
|
||||
self.on_battle_field.load(Ordering::Relaxed)
|
||||
}
|
||||
/// A list of opponents the Pokemon has seen this battle.
|
||||
pub fn seen_opponents(&self) -> &RwLock<Vec<Weak<Pokemon>>> {
|
||||
pub fn seen_opponents(&self) -> &RwLock<Vec<WeakPokemonReference>> {
|
||||
&self.seen_opponents
|
||||
}
|
||||
}
|
||||
@@ -807,7 +892,7 @@ impl PokemonBattleData {
|
||||
impl ScriptSource for Pokemon {
|
||||
fn get_script_count(&self) -> Result<usize> {
|
||||
let mut c = 3;
|
||||
if let Some(battle_data) = &self.battle_data.read().deref() {
|
||||
if let Some(battle_data) = &self.data.battle_data.read().deref() {
|
||||
if let Some(battle) = battle_data.battle() {
|
||||
c += battle
|
||||
.sides()
|
||||
@@ -819,19 +904,19 @@ impl ScriptSource for Pokemon {
|
||||
}
|
||||
|
||||
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
|
||||
&self.script_source_data
|
||||
&self.data.script_source_data
|
||||
}
|
||||
|
||||
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
|
||||
scripts.push((&self.held_item_trigger_script).into());
|
||||
scripts.push((&self.ability_script).into());
|
||||
scripts.push((&self.status_script).into());
|
||||
scripts.push((&self.volatile).into());
|
||||
scripts.push((&self.data.held_item_trigger_script).into());
|
||||
scripts.push((&self.data.ability_script).into());
|
||||
scripts.push((&self.data.status_script).into());
|
||||
scripts.push((&self.data.volatile).into());
|
||||
}
|
||||
|
||||
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) -> Result<()> {
|
||||
self.get_own_scripts(scripts);
|
||||
if let Some(battle_data) = &self.battle_data.read().deref() {
|
||||
if let Some(battle_data) = &self.data.battle_data.read().deref() {
|
||||
if let Some(battle) = battle_data.battle() {
|
||||
battle
|
||||
.sides()
|
||||
@@ -845,17 +930,17 @@ impl ScriptSource for Pokemon {
|
||||
|
||||
impl VolatileScriptsOwner for Pokemon {
|
||||
fn volatile_scripts(&self) -> &Arc<ScriptSet> {
|
||||
&self.volatile
|
||||
&self.data.volatile
|
||||
}
|
||||
|
||||
fn load_volatile_script(&self, key: &StringKey) -> Result<Option<Arc<dyn Script>>> {
|
||||
self.library.load_script(self.into(), ScriptCategory::Pokemon, key)
|
||||
self.data.library.load_script(self.into(), ScriptCategory::Pokemon, key)
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIdentifiable for Pokemon {
|
||||
fn value_identifier(&self) -> ValueIdentifier {
|
||||
self.identifier
|
||||
self.data.identifier
|
||||
}
|
||||
}
|
||||
|
||||
@@ -921,7 +1006,7 @@ pub mod test {
|
||||
});
|
||||
|
||||
let mut static_lib = MockStaticData::new();
|
||||
static_lib.expect_species().return_const(Box::new(species_lib));
|
||||
static_lib.expect_species().return_const(Arc::new(species_lib));
|
||||
|
||||
let mut growth_rate_lib = MockGrowthRateLibrary::new();
|
||||
growth_rate_lib
|
||||
@@ -934,8 +1019,8 @@ pub mod test {
|
||||
Some(Arc::new(n))
|
||||
});
|
||||
|
||||
static_lib.expect_growth_rates().return_const(Box::new(growth_rate_lib));
|
||||
static_lib.expect_natures().return_const(Box::new(nature_lib));
|
||||
static_lib.expect_growth_rates().return_const(Arc::new(growth_rate_lib));
|
||||
static_lib.expect_natures().return_const(Arc::new(nature_lib));
|
||||
|
||||
let mut stat_calculator = MockBattleStatCalculator::new();
|
||||
stat_calculator.expect_calculate_flat_stats().returning(|_, _| Ok(()));
|
||||
@@ -944,8 +1029,8 @@ pub mod test {
|
||||
.returning(|_, _| Ok(()));
|
||||
|
||||
let mut lib = MockDynamicLibrary::new();
|
||||
lib.expect_static_data().return_const(Box::new(static_lib));
|
||||
lib.expect_stat_calculator().return_const(Box::new(stat_calculator));
|
||||
lib.expect_static_data().return_const(Arc::new(static_lib));
|
||||
lib.expect_stat_calculator().return_const(Arc::new(stat_calculator));
|
||||
|
||||
Arc::new(lib)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user