PkmnLib_rs/src/dynamic_data/models/battle_random.rs

76 lines
1.9 KiB
Rust
Raw Normal View History

use crate::dynamic_data::models::executing_move::ExecutingMove;
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::dynamic_data::script_handling::ScriptSource;
2022-06-03 14:35:18 +00:00
use crate::utils::random::Random;
use crate::{script_hook, script_hook_on_lock};
use parking_lot::RwLock;
2022-06-03 14:35:18 +00:00
use std::fmt::{Debug, Formatter};
use std::sync::{Arc, Mutex};
2022-06-03 14:35:18 +00:00
#[derive(Default)]
2022-06-03 14:35:18 +00:00
pub struct BattleRandom {
random: Mutex<Random>,
2022-06-03 14:35:18 +00:00
}
impl BattleRandom {
pub fn new_with_seed(seed: u128) -> Self {
BattleRandom {
random: Mutex::new(Random::new(seed)),
}
2022-06-03 14:35:18 +00:00
}
pub fn get_rng(&self) -> &Mutex<Random> {
&self.random
2022-06-03 14:35:18 +00:00
}
2022-06-06 11:54:59 +00:00
pub fn get(&self) -> i32 {
return self.get_rng().lock().unwrap().get();
}
pub fn get_max(&self, max: i32) -> i32 {
return self.get_rng().lock().unwrap().get_max(max);
}
pub fn get_between(&self, min: i32, max: i32) -> i32 {
return self.get_rng().lock().unwrap().get_between(min, max);
}
pub fn effect_chance(
&self,
mut chance: f32,
executing_move: &ExecutingMove,
target: &Arc<RwLock<Pokemon>>,
hit_number: u8,
) -> bool {
script_hook!(
change_effect_chance,
executing_move,
executing_move,
target,
hit_number,
&mut chance
);
script_hook_on_lock!(
change_incoming_effect_chance,
target,
executing_move,
target,
hit_number,
&mut chance
);
self.get_rng().lock().unwrap().get_float() < (chance / 100.0)
}
}
2022-06-03 14:35:18 +00:00
impl Debug for BattleRandom {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("BattleRandom").finish()
2022-06-03 14:35:18 +00:00
}
}
2022-06-03 14:35:18 +00:00
impl Clone for BattleRandom {
fn clone(&self) -> Self {
Self {
random: Mutex::new(self.random.lock().unwrap().clone()),
}
2022-06-03 14:35:18 +00:00
}
}