2022-06-19 19:34:08 +00:00
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use hashbrown::HashSet;
|
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
use crate::dynamic_data::choices::{MoveChoice, TurnChoice};
|
2022-06-19 19:34:08 +00:00
|
|
|
use crate::dynamic_data::Pokemon;
|
|
|
|
use crate::dynamic_data::{LearnedMove, MoveLearnMethod};
|
2022-06-16 15:59:33 +00:00
|
|
|
use crate::static_data::{MoveCategory, MoveData, MoveTarget, SecondaryEffect};
|
2022-06-30 15:34:57 +00:00
|
|
|
use crate::StringKey;
|
2022-06-16 15:59:33 +00:00
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The misc library holds several misc functions required for the battle to run.
|
2022-06-19 19:34:08 +00:00
|
|
|
pub trait MiscLibrary<'library>: Debug {
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Returns whether or not a Pokemon is allowed to flee or switch out.
|
|
|
|
fn can_flee(&self, choice: &TurnChoice) -> bool;
|
|
|
|
/// Returns the move we need to use if we can't use another move. Typically Struggle.
|
2022-06-16 15:59:33 +00:00
|
|
|
fn replacement_move<'func>(
|
|
|
|
&'func self,
|
2022-06-18 13:52:39 +00:00
|
|
|
user: &Arc<Pokemon<'func, 'library>>,
|
2022-06-16 15:59:33 +00:00
|
|
|
target_side: u8,
|
|
|
|
target_index: u8,
|
|
|
|
) -> TurnChoice<'func, 'library>;
|
|
|
|
// TODO: can evolve from level up?
|
|
|
|
// TODO: get time
|
|
|
|
}
|
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// A gen 7 implementation for the MiscLibrary.
|
2022-06-16 15:59:33 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Gen7MiscLibrary<'library> {
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The move data for struggle. This is a pointer due to lifetime issues; we know that the
|
|
|
|
/// learned move based on this has the same lifetime as the move data, but the compiler does not.
|
|
|
|
/// If possible in a sane manner, we should get rid of this pointer.
|
2022-06-16 15:59:33 +00:00
|
|
|
struggle_data: *const MoveData,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The learned move data for struggle.
|
2022-06-18 12:17:29 +00:00
|
|
|
struggle_learned_move: Arc<LearnedMove<'library>>,
|
2022-06-16 15:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'library> Gen7MiscLibrary<'library> {
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Instantiates a new MiscLibrary.
|
2022-06-16 15:59:33 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
let struggle_data = Box::new(MoveData::new(
|
|
|
|
&StringKey::new("struggle"),
|
|
|
|
0,
|
|
|
|
MoveCategory::Physical,
|
|
|
|
50,
|
|
|
|
255,
|
2022-06-18 12:17:29 +00:00
|
|
|
255,
|
2022-06-16 15:59:33 +00:00
|
|
|
MoveTarget::Any,
|
|
|
|
0,
|
2022-06-19 10:07:54 +00:00
|
|
|
Some(SecondaryEffect::new(-1.0, StringKey::new("struggle"), vec![])),
|
2022-06-16 15:59:33 +00:00
|
|
|
HashSet::new(),
|
|
|
|
));
|
|
|
|
let struggle_ptr = Box::into_raw(struggle_data);
|
2022-06-18 12:17:29 +00:00
|
|
|
let struggle_learned_move = Arc::new(LearnedMove::new(unsafe { &*struggle_ptr }, MoveLearnMethod::Unknown));
|
2022-06-16 15:59:33 +00:00
|
|
|
Self {
|
|
|
|
struggle_data: struggle_ptr,
|
|
|
|
struggle_learned_move,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 14:06:54 +00:00
|
|
|
impl<'library> Default for Gen7MiscLibrary<'library> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 15:59:33 +00:00
|
|
|
impl<'library> Drop for Gen7MiscLibrary<'library> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
Box::from_raw(self.struggle_data as *mut MoveData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'library> MiscLibrary<'library> for Gen7MiscLibrary<'library> {
|
2022-06-30 15:34:57 +00:00
|
|
|
fn can_flee(&self, _choice: &TurnChoice) -> bool {
|
2022-06-16 15:59:33 +00:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn replacement_move<'func>(
|
|
|
|
&'func self,
|
2022-06-18 13:52:39 +00:00
|
|
|
user: &Arc<Pokemon<'func, 'library>>,
|
2022-06-16 15:59:33 +00:00
|
|
|
target_side: u8,
|
|
|
|
target_index: u8,
|
|
|
|
) -> TurnChoice<'func, 'library> {
|
2022-06-30 15:34:57 +00:00
|
|
|
self.struggle_learned_move.restore_all_uses();
|
2022-06-16 15:59:33 +00:00
|
|
|
TurnChoice::Move(MoveChoice::new(
|
|
|
|
user.clone(),
|
|
|
|
self.struggle_learned_move.clone(),
|
|
|
|
target_side,
|
|
|
|
target_index,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|