PkmnLib_rs/src/dynamic_data/models/learned_move.rs

118 lines
3.6 KiB
Rust
Executable File

use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::Arc;
use crate::static_data::MoveData;
use crate::{ValueIdentifiable, ValueIdentifier};
/// 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 {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The immutable move information of the move.
move_data: Arc<dyn MoveData>,
/// The maximal power points for this move.
max_pp: 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)]
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 max_pp = move_data.base_usages();
Self {
identifier: Default::default(),
move_data,
max_pp,
remaining_pp: AtomicU8::new(max_pp),
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.max_pp
}
/// 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| {
if x + uses > self.max_pp {
uses = self.max_pp - x;
}
Some(x + uses)
})
.ok();
}
}
impl ValueIdentifiable for LearnedMove {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
#[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());
}
}