A bunch more work on replacing every potential panic with results
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -22,7 +22,7 @@ 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, PkmnError, StringKey, ValueIdentifiable, ValueIdentifier};
|
||||
use crate::{script_hook, PkmnError, StringKey, ValueIdentifiable, ValueIdentifier, VecExt};
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
|
||||
/// An individual Pokemon as we know and love them.
|
||||
@@ -595,16 +595,17 @@ impl Pokemon {
|
||||
}
|
||||
|
||||
/// Sets whether or not the Pokemon is on the battlefield.
|
||||
pub fn set_on_battlefield(&self, value: bool) {
|
||||
pub fn set_on_battlefield(&self, value: bool) -> Result<()> {
|
||||
let r = self.battle_data.read();
|
||||
if let Some(data) = &mut r.deref() {
|
||||
data.on_battle_field.store(value, Ordering::SeqCst);
|
||||
if !value {
|
||||
self.volatile.clear();
|
||||
self.volatile.clear()?;
|
||||
self.weight.store(self.form().weight(), Ordering::SeqCst);
|
||||
self.height.store(self.form().height(), Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sets the index of the slot of the side the Pokemon is on.
|
||||
@@ -676,11 +677,7 @@ impl Pokemon {
|
||||
if !battle.can_slot_be_filled(battle_data.battle_side_index(), battle_data.index()) {
|
||||
battle
|
||||
.sides()
|
||||
.get(battle_data.battle_side_index() as usize)
|
||||
.ok_or(PkmnError::IndexOutOfBounds {
|
||||
index: battle_data.battle_side_index() as usize,
|
||||
len: battle.sides().len(),
|
||||
})?
|
||||
.get_res(battle_data.battle_side_index() as usize)?
|
||||
.mark_slot_as_unfillable(battle_data.index())?;
|
||||
}
|
||||
|
||||
@@ -721,9 +718,12 @@ impl Pokemon {
|
||||
pub fn learn_move(&self, move_name: &StringKey, learn_method: MoveLearnMethod) -> Result<()> {
|
||||
let mut learned_moves = self.learned_moves().write();
|
||||
let move_pos = learned_moves.iter().position(|a| a.is_none());
|
||||
if move_pos.is_none() {
|
||||
bail!("No more moves with an empty space found.");
|
||||
}
|
||||
let move_pos = match move_pos {
|
||||
Some(a) => a,
|
||||
None => {
|
||||
bail!("No more moves with an empty space found.");
|
||||
}
|
||||
};
|
||||
let move_data = self
|
||||
.library
|
||||
.static_data()
|
||||
@@ -732,7 +732,9 @@ impl Pokemon {
|
||||
.ok_or(PkmnError::InvalidMoveName {
|
||||
move_name: move_name.clone(),
|
||||
})?;
|
||||
learned_moves[move_pos.unwrap()] = Some(Arc::new(LearnedMove::new(move_data, learn_method)));
|
||||
learned_moves
|
||||
.get_mut_res(move_pos)?
|
||||
.replace(Arc::new(LearnedMove::new(move_data, learn_method)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -805,7 +807,10 @@ impl ScriptSource for Pokemon {
|
||||
let mut c = 3;
|
||||
if let Some(battle_data) = &self.battle_data.read().deref() {
|
||||
if let Some(battle) = battle_data.battle() {
|
||||
c += battle.sides()[battle_data.battle_side_index() as usize].get_script_count()?;
|
||||
c += battle
|
||||
.sides()
|
||||
.get_res(battle_data.battle_side_index() as usize)?
|
||||
.get_script_count()?;
|
||||
}
|
||||
}
|
||||
Ok(c)
|
||||
@@ -826,7 +831,10 @@ impl ScriptSource for Pokemon {
|
||||
self.get_own_scripts(scripts);
|
||||
if let Some(battle_data) = &self.battle_data.read().deref() {
|
||||
if let Some(battle) = battle_data.battle() {
|
||||
battle.sides()[battle_data.battle_side_index() as usize].collect_scripts(scripts)?;
|
||||
battle
|
||||
.sides()
|
||||
.get_res(battle_data.battle_side_index() as usize)?
|
||||
.collect_scripts(scripts)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -862,6 +870,7 @@ pub enum DamageSource {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
pub mod test {
|
||||
use crate::dynamic_data::libraries::test::MockDynamicLibrary;
|
||||
use crate::dynamic_data::models::pokemon::Pokemon;
|
||||
|
||||
Reference in New Issue
Block a user