PkmnLib_rs/src/dynamic_data/libraries/misc_library.rs

105 lines
3.2 KiB
Rust
Executable File

use chrono::Timelike;
use std::fmt::{Debug, Formatter};
use std::sync::Arc;
use hashbrown::HashSet;
use crate::dynamic_data::choices::{MoveChoice, TurnChoice};
use crate::dynamic_data::Pokemon;
use crate::dynamic_data::{LearnedMove, MoveLearnMethod};
use crate::static_data::{MoveCategory, MoveData, MoveDataImpl, MoveTarget, SecondaryEffectImpl, TimeOfDay};
use crate::StringKey;
/// The misc library holds several misc functions required for the battle to run.
pub trait MiscLibrary: Debug {
/// 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.
fn replacement_move(&self, user: &Pokemon, target_side: u8, target_index: u8) -> TurnChoice;
// TODO: can evolve from level up?
/// Gets the current time of day for the battle.
fn time_of_day(&self) -> TimeOfDay;
}
/// A function pointer to get the time of day.
type GetTimeOfDayFn = Box<dyn Fn() -> TimeOfDay>;
/// A gen 7 implementation for the MiscLibrary.
pub struct Gen7MiscLibrary {
/// The learned move data for struggle.
struggle_learned_move: Arc<LearnedMove>,
/// The function to get the time of day.
get_time_fn: GetTimeOfDayFn,
}
impl Gen7MiscLibrary {
/// Instantiates a new MiscLibrary.
pub fn new(get_time_fn: GetTimeOfDayFn) -> Self {
let struggle_data: Arc<dyn MoveData> = Arc::new(MoveDataImpl::new(
&StringKey::new("struggle"),
0.into(),
MoveCategory::Physical,
50,
255,
255,
MoveTarget::Any,
0,
Some(Arc::new(SecondaryEffectImpl::new(
-1.0,
StringKey::new("struggle"),
vec![],
))),
HashSet::new(),
));
let struggle_learned_move = Arc::new(LearnedMove::new(struggle_data, MoveLearnMethod::Unknown));
Self {
struggle_learned_move,
get_time_fn,
}
}
}
impl Default for Gen7MiscLibrary {
fn default() -> Self {
Self::new(Box::new(|| {
let time = chrono::Local::now().time();
let hour = time.hour();
// Following the values for Pokemon Sun.
match hour {
0..=5 => TimeOfDay::Night,
6..=9 => TimeOfDay::Morning,
10..=16 => TimeOfDay::Day,
17 => TimeOfDay::Evening,
18..=23 => TimeOfDay::Night,
_ => unreachable!(),
}
}))
}
}
impl MiscLibrary for Gen7MiscLibrary {
fn can_flee(&self, _choice: &TurnChoice) -> bool {
todo!()
}
fn replacement_move(&self, user: &Pokemon, target_side: u8, target_index: u8) -> TurnChoice {
self.struggle_learned_move.restore_all_uses();
TurnChoice::Move(MoveChoice::new(
user.clone(),
self.struggle_learned_move.clone(),
target_side,
target_index,
))
}
fn time_of_day(&self) -> TimeOfDay {
(self.get_time_fn)()
}
}
impl Debug for Gen7MiscLibrary {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("Gen7MiscLibrary")
}
}