Support for new error handling.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2023-04-15 14:34:42 +02:00
parent 3058739ea0
commit feffb5f030
36 changed files with 466 additions and 274 deletions

View File

@@ -22,7 +22,8 @@ use crate::static_data::TypeIdentifier;
use crate::static_data::{Ability, Statistic};
use crate::static_data::{ClampedStatisticSet, StatisticSet};
use crate::utils::Random;
use crate::{script_hook, PkmnResult, StringKey, ValueIdentifiable, ValueIdentifier};
use crate::{script_hook, StringKey, ValueIdentifiable, ValueIdentifier};
use anyhow::Result;
/// An individual Pokemon as we know and love them.
#[derive(Debug)]
@@ -129,12 +130,12 @@ impl Pokemon {
gender: Gender,
coloring: u8,
nature: &StringKey,
) -> Self {
) -> Result<Self> {
// Calculate experience from the level for the specified growth rate.
let experience = library
.static_data()
.growth_rates()
.calculate_experience(species.growth_rate(), level);
.calculate_experience(species.growth_rate(), level)?;
let weight = form.weight();
let height = form.height();
let nature = library
@@ -183,7 +184,7 @@ impl Pokemon {
let health = pokemon.flat_stats().hp();
pokemon.current_health = AtomicU32::new(health);
pokemon
Ok(pokemon)
}
/// The library data of the Pokemon.
@@ -617,12 +618,12 @@ impl Pokemon {
}
/// Damages the Pokemon by a certain amount of damage, from a damage source.
pub fn damage(&self, mut damage: u32, source: DamageSource) {
pub fn damage(&self, mut damage: u32, source: DamageSource) -> Result<()> {
if damage > self.current_health() {
damage = self.current_health();
}
if damage == 0 {
return;
return Ok(());
}
let new_health = self.current_health() - damage;
if let Some(battle_data) = &self.battle_data.read().deref() {
@@ -641,12 +642,13 @@ impl Pokemon {
self.current_health.store(new_health, Ordering::SeqCst);
if self.is_fainted() && damage > 0 {
self.on_faint(source);
self.on_faint(source)?;
}
Ok(())
}
/// Triggers when the Pokemon faints.
fn on_faint(&self, source: DamageSource) {
fn on_faint(&self, source: DamageSource) -> Result<()> {
let r = self.battle_data.read();
if let Some(battle_data) = r.deref() {
if let Some(battle) = battle_data.battle() {
@@ -659,9 +661,10 @@ impl Pokemon {
.mark_slot_as_unfillable(battle_data.index());
}
battle.validate_battle_state();
battle.validate_battle_state()?;
}
}
Ok(())
}
/// Heals the Pokemon by a specific amount. Unless allow_revive is set to true, this will not
@@ -803,7 +806,7 @@ impl VolatileScriptsOwner for Pokemon {
&self.volatile
}
fn load_volatile_script(&self, key: &StringKey) -> PkmnResult<Option<Arc<dyn Script>>> {
fn load_volatile_script(&self, key: &StringKey) -> Result<Option<Arc<dyn Script>>> {
self.library.load_script(self.into(), ScriptCategory::Pokemon, key)
}
}
@@ -866,7 +869,9 @@ pub mod test {
static_lib.expect_species().return_const(Box::new(species_lib));
let mut growth_rate_lib = MockGrowthRateLibrary::new();
growth_rate_lib.expect_calculate_experience().return_const(1000u32);
growth_rate_lib
.expect_calculate_experience()
.returning(|_, _| Ok(1000u32));
let mut nature_lib = MockNatureLibrary::new();
nature_lib.expect_get_nature().returning(|_| {
@@ -907,7 +912,8 @@ pub mod test {
Gender::Male,
0,
&"test_nature".into(),
);
)
.unwrap();
assert_eq!(pokemon.species().name(), &"test_species".into());
assert_eq!(pokemon.form().name(), &"default".into());
}