Support for serializing and deserializing a Pokemon
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:
@@ -1,3 +1,5 @@
|
||||
use crate::dynamic_data::DynamicLibrary;
|
||||
use crate::PkmnError;
|
||||
use std::sync::atomic::{AtomicU8, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -10,7 +12,7 @@ pub struct LearnedMove {
|
||||
/// The immutable move information of the move.
|
||||
move_data: Arc<dyn MoveData>,
|
||||
/// The maximal power points for this move.
|
||||
max_pp: u8,
|
||||
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.
|
||||
@@ -20,6 +22,7 @@ pub struct LearnedMove {
|
||||
/// 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]
|
||||
@@ -31,11 +34,11 @@ pub enum MoveLearnMethod {
|
||||
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();
|
||||
let base_usages = move_data.base_usages();
|
||||
Self {
|
||||
move_data,
|
||||
max_pp,
|
||||
remaining_pp: AtomicU8::new(max_pp),
|
||||
max_pp_modification: 0,
|
||||
remaining_pp: AtomicU8::new(base_usages),
|
||||
learn_method,
|
||||
}
|
||||
}
|
||||
@@ -46,8 +49,15 @@ impl LearnedMove {
|
||||
}
|
||||
/// The maximal power points for this move.
|
||||
pub fn max_pp(&self) -> u8 {
|
||||
self.max_pp
|
||||
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)
|
||||
@@ -72,20 +82,44 @@ impl LearnedMove {
|
||||
|
||||
/// 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);
|
||||
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;
|
||||
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)]
|
||||
|
||||
Reference in New Issue
Block a user