PkmnLib_rs/src/dynamic_data/models/learned_move.rs

142 lines
4.6 KiB
Rust
Executable File

use crate::dynamic_data::DynamicLibrary;
use crate::PkmnError;
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::Arc;
use crate::static_data::MoveData;
/// A learned move is the data attached to a Pokemon for a move it has learned. It has information
/// such as the remaining amount of users, how it has been learned, etc.
#[derive(Debug)]
pub struct LearnedMove {
/// The immutable move information of the move.
move_data: Arc<dyn MoveData>,
/// The maximal power points for this move.
max_pp_modification: u8,
/// The amount of remaining power points. If this is 0, we can not use the move anymore.
remaining_pp: AtomicU8,
/// The way the move was learned.
learn_method: MoveLearnMethod,
}
/// The different ways a move can be learned.
#[derive(Copy, Clone, Debug, Default)]
#[repr(u8)]
#[cfg_attr(feature = "serde", derive(serde_repr::Serialize_repr, serde_repr::Deserialize_repr))]
pub enum MoveLearnMethod {
/// We do not know the learn method.
#[default]
Unknown = 0,
/// The move was learned through level up.
Level = 1,
}
impl LearnedMove {
/// Instantiate a new learned move.
pub fn new(move_data: Arc<dyn MoveData>, learn_method: MoveLearnMethod) -> Self {
let base_usages = move_data.base_usages();
Self {
move_data,
max_pp_modification: 0,
remaining_pp: AtomicU8::new(base_usages),
learn_method,
}
}
/// The immutable move information of the move.
pub fn move_data(&self) -> &Arc<dyn MoveData> {
&self.move_data
}
/// The maximal power points for this move.
pub fn max_pp(&self) -> u8 {
self.move_data.base_usages() + self.max_pp_modification
}
/// The amount by which the maximal power points have been modified for this move.
/// This could for example be due to PP Ups.
pub fn max_pp_modification(&self) -> u8 {
self.max_pp_modification
}
/// The amount of remaining power points. If this is 0, we can not use the move anymore.
pub fn remaining_pp(&self) -> u8 {
self.remaining_pp.load(Ordering::Relaxed)
}
/// The way the move was learned.
pub fn learn_method(&self) -> MoveLearnMethod {
self.learn_method
}
/// Try and reduce the PP by a certain amount. If the amount is higher than the current uses,
/// return false. Otherwise, reduce the PP, and return true.
pub fn try_use(&self, amount: u8) -> bool {
let res = self.remaining_pp.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| {
if amount > x {
None
} else {
Some(x - amount)
}
});
res.is_ok()
}
/// Set the remaining PP to the max amount of PP.
pub fn restore_all_uses(&self) {
self.remaining_pp.store(self.max_pp(), Ordering::SeqCst);
}
/// Restore the remaining PP by a certain amount. Will prevent it from going above max PP.
pub fn restore_uses(&self, mut uses: u8) {
self.remaining_pp
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |x| {
let max = self.max_pp();
if x + uses > max {
uses = max - x;
}
Some(x + uses)
})
.ok();
}
/// Deserialize a learned move from a serialized learned move.
pub fn deserialize(
library: &Arc<dyn DynamicLibrary>,
value: &super::serialization::SerializedLearnedMove,
) -> anyhow_ext::Result<Self> {
Ok(Self {
move_data: {
let move_data =
library
.static_data()
.moves()
.get(&value.move_data)
.ok_or_else(|| PkmnError::InvalidMoveName {
move_name: value.move_data.clone(),
})?;
Arc::clone(&move_data)
},
max_pp_modification: value.max_pp_modification,
remaining_pp: AtomicU8::new(value.remaining_pp),
learn_method: value.learn_method,
})
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use crate::static_data::tests::MockMoveData;
#[test]
fn create_learned_move_restore_uses() {
let mut mock = MockMoveData::new();
mock.expect_base_usages().return_const(30);
let data: Arc<dyn MoveData> = Arc::new(mock);
let learned_move = LearnedMove::new(data, MoveLearnMethod::Level);
assert!(learned_move.try_use(15));
learned_move.restore_uses(5);
assert_eq!(20, learned_move.remaining_pp());
}
}