2022-06-19 19:34:08 +00:00
|
|
|
use std::fmt::{Debug, Formatter};
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
2022-06-16 15:59:33 +00:00
|
|
|
use crate::dynamic_data::models::executing_move::ExecutingMove;
|
|
|
|
use crate::dynamic_data::models::pokemon::Pokemon;
|
|
|
|
use crate::dynamic_data::script_handling::ScriptSource;
|
2022-06-18 13:52:39 +00:00
|
|
|
use crate::script_hook;
|
2022-06-19 19:34:08 +00:00
|
|
|
use crate::utils::Random;
|
2022-06-03 14:35:18 +00:00
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The RNG for a battle.
|
2022-06-04 10:40:32 +00:00
|
|
|
#[derive(Default)]
|
2022-09-07 16:01:26 +00:00
|
|
|
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
2022-06-03 14:35:18 +00:00
|
|
|
pub struct BattleRandom {
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The actual underlying RNG. This is in a mutex, so it is thread safe, and can be ran
|
|
|
|
/// predictably, with guaranteed the same outputs.
|
2022-06-04 10:40:32 +00:00
|
|
|
random: Mutex<Random>,
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-06-04 10:40:32 +00:00
|
|
|
impl BattleRandom {
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Initializes a new RNG with a given seed.
|
2022-06-04 10:40:32 +00:00
|
|
|
pub fn new_with_seed(seed: u128) -> Self {
|
|
|
|
BattleRandom {
|
|
|
|
random: Mutex::new(Random::new(seed)),
|
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Returns the underlying random number generator.
|
2022-06-04 10:40:32 +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
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Get a random 32 bit integer. Can be any value between min int and max int.
|
2022-06-06 11:54:59 +00:00
|
|
|
pub fn get(&self) -> i32 {
|
|
|
|
return self.get_rng().lock().unwrap().get();
|
|
|
|
}
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Get a random 32 bit integer between 0 and max.
|
2022-06-06 11:54:59 +00:00
|
|
|
pub fn get_max(&self, max: i32) -> i32 {
|
|
|
|
return self.get_rng().lock().unwrap().get_max(max);
|
|
|
|
}
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Get a random 32 bit integer between min and max.
|
2022-06-06 11:54:59 +00:00
|
|
|
pub fn get_between(&self, min: i32, max: i32) -> i32 {
|
|
|
|
return self.get_rng().lock().unwrap().get_between(min, max);
|
|
|
|
}
|
2022-06-16 15:59:33 +00:00
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Gets whether or not a move triggers its secondary effect. This takes its chance, and
|
|
|
|
/// rolls whether it triggers. As a side effect this run scripts to allow modifying this random
|
|
|
|
/// chance.
|
2022-06-16 15:59:33 +00:00
|
|
|
pub fn effect_chance(
|
|
|
|
&self,
|
|
|
|
mut chance: f32,
|
|
|
|
executing_move: &ExecutingMove,
|
2022-06-18 13:52:39 +00:00
|
|
|
target: &Arc<Pokemon>,
|
2022-06-16 15:59:33 +00:00
|
|
|
hit_number: u8,
|
|
|
|
) -> bool {
|
|
|
|
script_hook!(
|
|
|
|
change_effect_chance,
|
|
|
|
executing_move,
|
|
|
|
executing_move,
|
|
|
|
target,
|
|
|
|
hit_number,
|
|
|
|
&mut chance
|
|
|
|
);
|
2022-06-18 13:52:39 +00:00
|
|
|
script_hook!(
|
2022-06-16 15:59:33 +00:00
|
|
|
change_incoming_effect_chance,
|
|
|
|
target,
|
|
|
|
executing_move,
|
|
|
|
target,
|
|
|
|
hit_number,
|
|
|
|
&mut chance
|
|
|
|
);
|
2022-08-28 13:34:40 +00:00
|
|
|
if chance < 100.0 {
|
|
|
|
if chance > 0.0 {
|
|
|
|
self.get_rng().lock().unwrap().get_float() < (chance / 100.0)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
2022-06-16 15:59:33 +00:00
|
|
|
}
|
2022-06-04 10:40:32 +00:00
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
|
2022-06-04 10:40:32 +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-04 10:40:32 +00:00
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
|
2022-06-04 10:40:32 +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
|
|
|
}
|
|
|
|
}
|