More work on using interior mutability instead of exterior mutability for dynamic types (Battle, Pokemon, etc).
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-06-18 15:52:39 +02:00
parent c45c7538bf
commit 5576bc8b80
21 changed files with 324 additions and 385 deletions

View File

@@ -7,15 +7,22 @@ use std::sync::atomic::AtomicU32;
pub struct BattleStatCalculator {}
impl BattleStatCalculator {
pub fn calculate_flat_stats(&self, pokemon: &Pokemon) -> StatisticSet<AtomicU32> {
StatisticSet::<AtomicU32>::new(
self.calculate_health_stat(pokemon),
self.calculate_other_stat(pokemon, Statistic::Attack),
pub fn calculate_flat_stats(&self, pokemon: &Pokemon, stats: &StatisticSet<AtomicU32>) {
stats.set_stat(Statistic::HP, self.calculate_health_stat(pokemon));
stats.set_stat(Statistic::Attack, self.calculate_other_stat(pokemon, Statistic::Attack));
stats.set_stat(
Statistic::Defense,
self.calculate_other_stat(pokemon, Statistic::Defense),
);
stats.set_stat(
Statistic::SpecialAttack,
self.calculate_other_stat(pokemon, Statistic::SpecialAttack),
);
stats.set_stat(
Statistic::SpecialDefense,
self.calculate_other_stat(pokemon, Statistic::SpecialDefense),
self.calculate_other_stat(pokemon, Statistic::Speed),
)
);
stats.set_stat(Statistic::Speed, self.calculate_other_stat(pokemon, Statistic::Speed));
}
pub fn calculate_flat_stat(&self, pokemon: &Pokemon, stat: Statistic) -> u32 {
@@ -26,15 +33,25 @@ impl BattleStatCalculator {
}
}
pub fn calculate_boosted_stats(&self, pokemon: &Pokemon) -> StatisticSet<AtomicU32> {
StatisticSet::<AtomicU32>::new(
self.calculate_boosted_stat(pokemon, Statistic::HP),
pub fn calculate_boosted_stats(&self, pokemon: &Pokemon, stats: &StatisticSet<AtomicU32>) {
stats.set_stat(Statistic::HP, self.calculate_boosted_stat(pokemon, Statistic::HP));
stats.set_stat(
Statistic::Attack,
self.calculate_boosted_stat(pokemon, Statistic::Attack),
);
stats.set_stat(
Statistic::Defense,
self.calculate_boosted_stat(pokemon, Statistic::Defense),
);
stats.set_stat(
Statistic::SpecialAttack,
self.calculate_boosted_stat(pokemon, Statistic::SpecialAttack),
);
stats.set_stat(
Statistic::SpecialDefense,
self.calculate_boosted_stat(pokemon, Statistic::SpecialDefense),
self.calculate_boosted_stat(pokemon, Statistic::Speed),
)
);
stats.set_stat(Statistic::Speed, self.calculate_boosted_stat(pokemon, Statistic::Speed));
}
pub fn calculate_boosted_stat(&self, pokemon: &Pokemon, stat: Statistic) -> u32 {

View File

@@ -1,9 +1,8 @@
use crate::dynamic_data::models::executing_move::{ExecutingMove, HitData};
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::dynamic_data::script_handling::ScriptSource;
use crate::script_hook;
use crate::static_data::{MoveCategory, Statistic};
use crate::{script_hook, script_hook_on_lock};
use parking_lot::RwLock;
use std::sync::Arc;
pub trait DamageLibrary: std::fmt::Debug {
@@ -11,7 +10,7 @@ pub trait DamageLibrary: std::fmt::Debug {
fn get_damage(
&self,
executing_move: &ExecutingMove,
target: &Arc<RwLock<Pokemon>>,
target: &Arc<Pokemon>,
hit_number: u8,
hit_data: &HitData,
) -> u32;
@@ -19,7 +18,7 @@ pub trait DamageLibrary: std::fmt::Debug {
fn get_base_power(
&self,
executing_move: &ExecutingMove,
target: &Arc<RwLock<Pokemon>>,
target: &Arc<Pokemon>,
hit_number: u8,
hit_data: &HitData,
) -> u8;
@@ -27,7 +26,7 @@ pub trait DamageLibrary: std::fmt::Debug {
fn get_stat_modifier(
&self,
executing_move: &ExecutingMove,
target: &Arc<RwLock<Pokemon>>,
target: &Arc<Pokemon>,
hit_number: u8,
hit_data: &HitData,
) -> f32;
@@ -35,7 +34,7 @@ pub trait DamageLibrary: std::fmt::Debug {
fn get_damage_modifier(
&self,
executing_move: &ExecutingMove,
target: &Arc<RwLock<Pokemon>>,
target: &Arc<Pokemon>,
hit_number: u8,
hit_data: &HitData,
) -> f32;
@@ -60,7 +59,7 @@ impl DamageLibrary for Gen7DamageLibrary {
fn get_damage(
&self,
executing_move: &ExecutingMove,
target: &Arc<RwLock<Pokemon>>,
target: &Arc<Pokemon>,
hit_number: u8,
hit_data: &HitData,
) -> u32 {
@@ -68,7 +67,7 @@ impl DamageLibrary for Gen7DamageLibrary {
return 0;
}
let level_modifier = ((2.0 * executing_move.user().read().level() as f32) / 5.0).floor() + 2.0;
let level_modifier = ((2.0 * executing_move.user().level() as f32) / 5.0).floor() + 2.0;
let base_power = hit_data.base_power();
let stat_modifier = self.get_stat_modifier(executing_move, target, hit_number, hit_data);
let damage_modifier = self.get_damage_modifier(executing_move, target, hit_number, hit_data);
@@ -94,18 +93,11 @@ impl DamageLibrary for Gen7DamageLibrary {
}
if self.has_randomness {
let random_percentage = 85
+ executing_move
.user()
.read()
.get_battle()
.unwrap()
.random()
.get_between(0, 16);
let random_percentage = 85 + executing_move.user().get_battle().unwrap().random().get_between(0, 16);
float_damage = (float_damage * (random_percentage as f32 / 100.0)).floor();
}
if executing_move.user().read().types().contains(&hit_data.move_type()) {
if executing_move.user().types().contains(&hit_data.move_type()) {
let mut stab_modifier = 1.5;
script_hook!(
change_stab_modifier,
@@ -139,7 +131,7 @@ impl DamageLibrary for Gen7DamageLibrary {
hit_number,
&mut damage
);
script_hook_on_lock!(
script_hook!(
change_incoming_damage,
target,
executing_move,
@@ -153,7 +145,7 @@ impl DamageLibrary for Gen7DamageLibrary {
fn get_base_power(
&self,
executing_move: &ExecutingMove,
target: &Arc<RwLock<Pokemon>>,
target: &Arc<Pokemon>,
hit_number: u8,
_hit_data: &HitData,
) -> u8 {
@@ -176,7 +168,7 @@ impl DamageLibrary for Gen7DamageLibrary {
fn get_stat_modifier(
&self,
executing_move: &ExecutingMove,
target: &Arc<RwLock<Pokemon>>,
target: &Arc<Pokemon>,
hit_number: u8,
hit_data: &HitData,
) -> f32 {
@@ -199,7 +191,7 @@ impl DamageLibrary for Gen7DamageLibrary {
defensive_stat = Statistic::SpecialDefense;
}
let mut bypass_defensive_stat_boost =
hit_data.is_critical() && target.read().stat_boost().get_stat(defensive_stat) > 0;
hit_data.is_critical() && target.stat_boost().get_stat(defensive_stat) > 0;
script_hook!(
bypass_defensive_stat_boost,
executing_move,
@@ -208,8 +200,7 @@ impl DamageLibrary for Gen7DamageLibrary {
hit_number,
&mut bypass_defensive_stat_boost
);
let mut bypass_offensive_stat_boost =
hit_data.is_critical() && user.read().stat_boost().get_stat(offensive_stat) > 0;
let mut bypass_offensive_stat_boost = hit_data.is_critical() && user.stat_boost().get_stat(offensive_stat) > 0;
script_hook!(
bypass_offensive_stat_boost,
executing_move,
@@ -220,15 +211,15 @@ impl DamageLibrary for Gen7DamageLibrary {
);
let mut defensive_stat = if bypass_defensive_stat_boost {
target.read().flat_stats().get_stat(offensive_stat)
target.flat_stats().get_stat(offensive_stat)
} else {
target.read().boosted_stats().get_stat(offensive_stat)
target.boosted_stats().get_stat(offensive_stat)
};
let mut offensive_stat = if bypass_offensive_stat_boost {
user.read().flat_stats().get_stat(offensive_stat)
user.flat_stats().get_stat(offensive_stat)
} else {
user.read().boosted_stats().get_stat(offensive_stat)
user.boosted_stats().get_stat(offensive_stat)
};
script_hook!(
@@ -264,7 +255,7 @@ impl DamageLibrary for Gen7DamageLibrary {
fn get_damage_modifier(
&self,
executing_move: &ExecutingMove,
target: &Arc<RwLock<Pokemon>>,
target: &Arc<Pokemon>,
hit_number: u8,
_hit_data: &HitData,
) -> f32 {

View File

@@ -7,7 +7,6 @@ use crate::dynamic_data::script_handling::ScriptSource;
use crate::static_data::{MoveCategory, MoveData, MoveTarget, SecondaryEffect};
use crate::{script_hook, StringKey};
use hashbrown::HashSet;
use parking_lot::RwLock;
use std::fmt::{Debug, Formatter};
use std::sync::Arc;
@@ -16,13 +15,13 @@ pub trait MiscLibrary<'library> {
&self,
battle: &Battle,
executing_move: &ExecutingMove,
target: &Arc<RwLock<Pokemon>>,
target: &Arc<Pokemon>,
hit_number: u8,
) -> bool;
fn can_flee(&self, choice: &SwitchChoice) -> bool;
fn replacement_move<'func>(
&'func self,
user: &Arc<RwLock<Pokemon<'func, 'library>>>,
user: &Arc<Pokemon<'func, 'library>>,
target_side: u8,
target_index: u8,
) -> TurnChoice<'func, 'library>;
@@ -78,7 +77,7 @@ impl<'library> MiscLibrary<'library> for Gen7MiscLibrary<'library> {
&self,
battle: &Battle,
executing_move: &ExecutingMove,
target: &Arc<RwLock<Pokemon>>,
target: &Arc<Pokemon>,
hit_number: u8,
) -> bool {
if executing_move.use_move().category() == MoveCategory::Status {
@@ -109,7 +108,7 @@ impl<'library> MiscLibrary<'library> for Gen7MiscLibrary<'library> {
fn replacement_move<'func>(
&'func self,
user: &Arc<RwLock<Pokemon<'func, 'library>>>,
user: &Arc<Pokemon<'func, 'library>>,
target_side: u8,
target_index: u8,
) -> TurnChoice<'func, 'library> {