Change to RwLock over Mutex, so we can read without locking, changes to BattleRandom to allow for cloning, and prevent race conditions.
This commit is contained in:
@@ -5,22 +5,22 @@ use crate::dynamic_data::models::pokemon::Pokemon;
|
||||
use crate::dynamic_data::script_handling::ScriptSource;
|
||||
use crate::script_hook;
|
||||
use derive_getters::Getters;
|
||||
use std::sync::{Arc, Mutex, Weak};
|
||||
use std::sync::{Arc, RwLock, Weak};
|
||||
|
||||
#[derive(Getters, Debug)]
|
||||
pub struct BattleSide<'a> {
|
||||
index: u8,
|
||||
pokemon_per_side: u8,
|
||||
pokemon: Vec<Option<Arc<Mutex<Pokemon<'a>>>>>,
|
||||
pokemon: Vec<Option<Arc<RwLock<Pokemon<'a>>>>>,
|
||||
choices: Vec<Option<Arc<TurnChoice<'a>>>>,
|
||||
fillable_slots: Vec<bool>,
|
||||
choices_set: u8,
|
||||
battle: Weak<Mutex<Battle<'a>>>,
|
||||
battle: Weak<RwLock<Battle<'a>>>,
|
||||
has_fled: bool,
|
||||
}
|
||||
|
||||
impl<'a> BattleSide<'a> {
|
||||
pub fn new(index: u8, battle: Weak<Mutex<Battle<'a>>>, pokemon_per_side: u8) -> Self {
|
||||
pub fn new(index: u8, battle: Weak<RwLock<Battle<'a>>>, pokemon_per_side: u8) -> Self {
|
||||
let mut pokemon = Vec::with_capacity(pokemon_per_side as usize);
|
||||
let mut choices = Vec::with_capacity(pokemon_per_side as usize);
|
||||
let mut fillable_slots = Vec::with_capacity(pokemon_per_side as usize);
|
||||
@@ -52,12 +52,12 @@ impl<'a> BattleSide<'a> {
|
||||
/// empty, but can't be filled by any party anymore.
|
||||
pub fn all_slots_filled(&self) -> bool {
|
||||
for pokemon in &self.pokemon {
|
||||
if (!pokemon.is_none() || !pokemon.as_ref().unwrap().lock().unwrap().is_usable())
|
||||
if (!pokemon.is_none() || !pokemon.as_ref().unwrap().read().unwrap().is_usable())
|
||||
&& self
|
||||
.battle
|
||||
.upgrade()
|
||||
.unwrap()
|
||||
.lock()
|
||||
.read()
|
||||
.unwrap()
|
||||
.can_slot_be_filled()
|
||||
{
|
||||
@@ -70,7 +70,7 @@ impl<'a> BattleSide<'a> {
|
||||
pub fn set_choice(&mut self, choice: TurnChoice<'a>) {
|
||||
for (index, pokemon_slot) in self.pokemon.iter().enumerate() {
|
||||
if let Some(pokemon) = pokemon_slot {
|
||||
if pokemon.lock().unwrap().unique_identifier() == choice.user().unique_identifier()
|
||||
if pokemon.read().unwrap().unique_identifier() == choice.user().unique_identifier()
|
||||
{
|
||||
self.choices[index] = Some(Arc::new(choice));
|
||||
self.choices_set += 1;
|
||||
@@ -84,29 +84,29 @@ impl<'a> BattleSide<'a> {
|
||||
self.pokemon[index as usize] = None;
|
||||
}
|
||||
|
||||
pub fn set_pokemon(&mut self, index: u8, pokemon: Option<Arc<Mutex<Pokemon<'a>>>>) {
|
||||
pub fn set_pokemon(&mut self, index: u8, pokemon: Option<Arc<RwLock<Pokemon<'a>>>>) {
|
||||
let old = &mut self.pokemon[index as usize];
|
||||
if let Some(old_pokemon) = old {
|
||||
let mut p = old_pokemon.lock().unwrap();
|
||||
let mut p = old_pokemon.write().unwrap();
|
||||
script_hook!(on_remove, p,);
|
||||
p.set_on_battlefield(false);
|
||||
}
|
||||
self.pokemon[index as usize] = pokemon;
|
||||
let pokemon = &self.pokemon[index as usize];
|
||||
if let Some(pokemon_mutex) = pokemon {
|
||||
let mut pokemon = pokemon_mutex.lock().unwrap();
|
||||
let mut pokemon = pokemon_mutex.write().unwrap();
|
||||
pokemon.set_battle_data(self.battle.clone(), self.index);
|
||||
pokemon.set_on_battlefield(true);
|
||||
pokemon.set_battle_index(index);
|
||||
|
||||
let battle = self.battle.upgrade().unwrap();
|
||||
let battle = battle.lock().unwrap();
|
||||
let battle = battle.read().unwrap();
|
||||
for side in battle.sides() {
|
||||
if *side.index() == self.index {
|
||||
continue;
|
||||
}
|
||||
for opponent in side.pokemon().iter().flatten() {
|
||||
let mut opponent = opponent.lock().unwrap();
|
||||
let mut opponent = opponent.write().unwrap();
|
||||
opponent.mark_opponent_as_seen(*pokemon.unique_identifier());
|
||||
pokemon.mark_opponent_as_seen(*opponent.unique_identifier());
|
||||
}
|
||||
@@ -119,7 +119,7 @@ impl<'a> BattleSide<'a> {
|
||||
script_hook!(on_switch_in, pokemon, &pokemon);
|
||||
} else {
|
||||
let battle = self.battle.upgrade().unwrap();
|
||||
let battle = battle.lock().unwrap();
|
||||
let battle = battle.read().unwrap();
|
||||
battle.event_hook().trigger(Event::Switch {
|
||||
side_index: self.index,
|
||||
index,
|
||||
@@ -130,7 +130,7 @@ impl<'a> BattleSide<'a> {
|
||||
|
||||
pub fn is_pokemon_on_side(&self, pokemon: Arc<Pokemon<'a>>) -> bool {
|
||||
for p in self.pokemon.iter().flatten() {
|
||||
if p.lock().unwrap().unique_identifier() == pokemon.unique_identifier() {
|
||||
if p.read().unwrap().unique_identifier() == pokemon.unique_identifier() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user