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
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -6,6 +6,7 @@ use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, Scrip
|
||||
use crate::static_data::MoveData;
|
||||
use crate::{PkmnResult, PokemonError};
|
||||
use parking_lot::RwLock;
|
||||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
@@ -62,7 +63,7 @@ impl HitData {
|
||||
pub struct ExecutingMove<'own, 'battle, 'library> {
|
||||
number_of_hits: u8,
|
||||
hits: Vec<HitData>,
|
||||
user: Arc<RwLock<Pokemon<'battle, 'library>>>,
|
||||
user: Arc<Pokemon<'battle, 'library>>,
|
||||
chosen_move: Arc<LearnedMove<'library>>,
|
||||
use_move: &'own MoveData,
|
||||
script: ScriptContainer,
|
||||
@@ -74,7 +75,7 @@ impl<'own, 'battle, 'library> ExecutingMove<'own, 'battle, 'library> {
|
||||
pub fn new(
|
||||
targets: &'own TargetList<'battle, 'library>,
|
||||
number_of_hits: u8,
|
||||
user: Arc<RwLock<Pokemon<'battle, 'library>>>,
|
||||
user: Arc<Pokemon<'battle, 'library>>,
|
||||
chosen_move: Arc<LearnedMove<'library>>,
|
||||
use_move: &'own MoveData,
|
||||
script: ScriptContainer,
|
||||
@@ -101,7 +102,7 @@ impl<'own, 'battle, 'library> ExecutingMove<'own, 'battle, 'library> {
|
||||
pub fn number_of_hits(&self) -> u8 {
|
||||
self.number_of_hits
|
||||
}
|
||||
pub fn user(&self) -> &Arc<RwLock<Pokemon<'battle, 'library>>> {
|
||||
pub fn user(&self) -> &Arc<Pokemon<'battle, 'library>> {
|
||||
&self.user
|
||||
}
|
||||
pub fn chosen_move(&self) -> &Arc<LearnedMove<'library>> {
|
||||
@@ -117,12 +118,12 @@ impl<'own, 'battle, 'library> ExecutingMove<'own, 'battle, 'library> {
|
||||
|
||||
pub fn get_hit_data<'func>(
|
||||
&'func self,
|
||||
for_target: &'func Arc<RwLock<Pokemon<'battle, 'library>>>,
|
||||
for_target: &'func Arc<Pokemon<'battle, 'library>>,
|
||||
hit: u8,
|
||||
) -> PkmnResult<&'func HitData> {
|
||||
for (index, target) in self.targets.iter().enumerate() {
|
||||
if let Some(target) = target {
|
||||
if std::ptr::eq(target.data_ptr(), for_target.data_ptr()) {
|
||||
if std::ptr::eq(target.deref().deref(), for_target.deref().deref()) {
|
||||
let i = index * self.number_of_hits as usize + hit as usize;
|
||||
return Ok(&self.hits[i]);
|
||||
}
|
||||
@@ -131,22 +132,19 @@ impl<'own, 'battle, 'library> ExecutingMove<'own, 'battle, 'library> {
|
||||
Err(PokemonError::InvalidTargetRequested)
|
||||
}
|
||||
|
||||
pub fn is_pokemon_target(&self, pokemon: &Arc<RwLock<Pokemon<'battle, 'library>>>) -> bool {
|
||||
pub fn is_pokemon_target(&self, pokemon: &Arc<Pokemon<'battle, 'library>>) -> bool {
|
||||
for target in self.targets.iter().flatten() {
|
||||
if std::ptr::eq(target.data_ptr(), pokemon.data_ptr()) {
|
||||
if std::ptr::eq(target.deref().deref(), pokemon.deref().deref()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub(crate) fn get_index_of_target(
|
||||
&self,
|
||||
for_target: &Arc<RwLock<Pokemon<'battle, 'library>>>,
|
||||
) -> PkmnResult<usize> {
|
||||
pub(crate) fn get_index_of_target(&self, for_target: &Arc<Pokemon<'battle, 'library>>) -> PkmnResult<usize> {
|
||||
for (index, target) in self.targets.iter().enumerate() {
|
||||
if let Some(target) = target {
|
||||
if std::ptr::eq(target.data_ptr(), for_target.data_ptr()) {
|
||||
if std::ptr::eq(target.deref().deref(), for_target.deref().deref()) {
|
||||
let i = index * self.number_of_hits as usize;
|
||||
return Ok(i);
|
||||
}
|
||||
@@ -178,6 +176,6 @@ impl<'own, 'battle, 'library> ScriptSource<'own> for ExecutingMove<'own, 'battle
|
||||
|
||||
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
|
||||
self.get_own_scripts(scripts);
|
||||
self.user.read().get_own_scripts(scripts);
|
||||
self.user.get_own_scripts(scripts);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user