Don't store the TurnChoices as Arc<RwLock>>, to prevent locking issues.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-06-18 09:49:17 +02:00
parent 94403e2ca1
commit 09c2533bf5
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
6 changed files with 33 additions and 46 deletions

View File

@ -72,7 +72,7 @@ impl<'user, 'library> TurnChoice<'user, 'library> {
self.choice_data_mut().random_value = val; self.choice_data_mut().random_value = val;
} }
pub(crate) fn get_move_turn_data<'b>(&'b mut self) -> &'b mut MoveChoice<'user, 'library> { pub(crate) fn get_move_turn_data<'b>(&'b self) -> &'b MoveChoice<'user, 'library> {
if let TurnChoice::Move(data) = self { if let TurnChoice::Move(data) = self {
return data; return data;
} }

View File

@ -1,26 +1,24 @@
use crate::dynamic_data::choices::TurnChoice; use crate::dynamic_data::choices::TurnChoice;
use crate::dynamic_data::models::pokemon::Pokemon; use crate::dynamic_data::models::pokemon::Pokemon;
use parking_lot::RwLock;
use std::sync::Arc;
#[derive(Debug)] #[derive(Debug)]
pub struct ChoiceQueue<'battle, 'library> { pub struct ChoiceQueue<'battle, 'library> {
queue: Vec<Arc<RwLock<TurnChoice<'battle, 'library>>>>, queue: Vec<TurnChoice<'battle, 'library>>,
current: usize, current: usize,
} }
impl<'battle, 'library> ChoiceQueue<'battle, 'library> { impl<'battle, 'library> ChoiceQueue<'battle, 'library> {
pub fn new(queue: Vec<Arc<RwLock<TurnChoice<'battle, 'library>>>>) -> Self { pub fn new(queue: Vec<TurnChoice<'battle, 'library>>) -> Self {
Self { queue, current: 0 } Self { queue, current: 0 }
} }
pub fn dequeue<'b>(&'b mut self) -> &'b Arc<RwLock<TurnChoice<'battle, 'library>>> { pub fn dequeue<'b>(&'b mut self) -> &'b TurnChoice<'battle, 'library> {
let c = &self.queue[self.current]; let c = &self.queue[self.current];
self.current += 1; self.current += 1;
c c
} }
pub fn peek(&mut self) -> &'battle Arc<RwLock<TurnChoice>> { pub fn peek(&mut self) -> &'battle TurnChoice {
&self.queue[self.current] &self.queue[self.current]
} }
@ -32,7 +30,7 @@ impl<'battle, 'library> ChoiceQueue<'battle, 'library> {
todo!() todo!()
} }
pub(crate) fn get_queue(&self) -> &Vec<Arc<RwLock<TurnChoice<'battle, 'library>>>> { pub(crate) fn get_queue(&self) -> &Vec<TurnChoice<'battle, 'library>> {
&self.queue &self.queue
} }
} }

View File

@ -25,17 +25,15 @@ impl<'own, 'library> Battle<'own, 'library> {
// to check whether a pokemon was hit this turn. By resetting here, and setting a variable to true // to check whether a pokemon was hit this turn. By resetting here, and setting a variable to true
// they can then know this later on.) // they can then know this later on.)
for choice in choice_queue.get_queue() { for choice in choice_queue.get_queue() {
let choice_guard = choice.read(); script_hook!(on_before_turn, choice, choice);
let c = choice_guard.deref();
script_hook!(on_before_turn, c, c);
} }
// Now we can properly begin executing choices. // Now we can properly begin executing choices.
// One by one dequeue the turns, and run them. If the battle has ended we do not want to // One by one dequeue the turns, and run them. If the battle has ended we do not want to
// continue running however. // continue running however.
while choice_queue.has_next() && !self.has_ended() { while choice_queue.has_next() && !self.has_ended() {
let choice = choice_queue.dequeue().clone(); let choice = choice_queue.dequeue();
self.execute_choice(choice.clone())?; self.execute_choice(&choice)?;
} }
// If the battle is not ended, we have arrived at the normal end of a turn. and thus want // If the battle is not ended, we have arrived at the normal end of a turn. and thus want
@ -63,15 +61,14 @@ impl<'own, 'library> Battle<'own, 'library> {
Ok(()) Ok(())
} }
fn execute_choice(&self, choice: Arc<RwLock<TurnChoice<'own, 'library>>>) -> PkmnResult<()> { fn execute_choice(&self, choice: &TurnChoice<'own, 'library>) -> PkmnResult<()> {
let choice_guard = choice.read(); if let TurnChoice::Pass(..) = choice {
if let TurnChoice::Pass(..) = choice_guard.deref() {
return Ok(()); return Ok(());
} }
if self.has_ended() { if self.has_ended() {
return Ok(()); return Ok(());
} }
let user = choice_guard.user().read(); let user = choice.user().read();
if !user.is_usable() { if !user.is_usable() {
return Ok(()); return Ok(());
} }
@ -79,15 +76,11 @@ impl<'own, 'library> Battle<'own, 'library> {
return Ok(()); return Ok(());
} }
if !self.can_use(choice_guard.deref()) { if !self.can_use(&choice) {
return Ok(()); return Ok(());
} }
match choice_guard.deref() { match choice {
TurnChoice::Move(..) => { TurnChoice::Move(..) => self.execute_move_choice(&choice)?,
drop(user);
drop(choice_guard);
self.execute_move_choice(&choice)?
}
TurnChoice::Item(_) => {} TurnChoice::Item(_) => {}
TurnChoice::Switch(_) => {} TurnChoice::Switch(_) => {}
TurnChoice::Flee(_) => {} TurnChoice::Flee(_) => {}
@ -96,12 +89,8 @@ impl<'own, 'library> Battle<'own, 'library> {
Ok(()) Ok(())
} }
fn execute_move_choice<'func>( fn execute_move_choice<'func>(&'func self, choice: &'func TurnChoice<'own, 'library>) -> PkmnResult<()> {
&'func self, let choice = choice.get_move_turn_data();
choice: &'func Arc<RwLock<TurnChoice<'own, 'library>>>,
) -> PkmnResult<()> {
let mut write_guard = choice.write();
let choice = write_guard.get_move_turn_data();
let used_move = choice.used_move(); let used_move = choice.used_move();
let move_data_lock = used_move.read(); let move_data_lock = used_move.read();
let move_data = move_data_lock.move_data(); let move_data = move_data_lock.move_data();

View File

@ -237,35 +237,35 @@ impl<'own, 'library> Battle<'own, 'library> {
let start_time = chrono::Utc::now(); let start_time = chrono::Utc::now();
let mut choices = Vec::with_capacity(self.number_of_sides as usize * self.pokemon_per_side as usize); let mut choices = Vec::with_capacity(self.number_of_sides as usize * self.pokemon_per_side as usize);
for side in &mut self.sides { for side in &mut self.sides {
for choice in side.choices() { for choice_opt in side.choices_mut() {
if choice.is_none() { if choice_opt.is_none() {
panic!("Choice was none, but all choices were set? Logic error."); panic!("Choice was none, but all choices were set? Logic error.");
} }
let mut choice_guard = choice.as_ref().unwrap().write(); let mut choice = choice_opt.as_mut().unwrap();
let c = choice_guard.deref(); let c = choice.deref();
if let TurnChoice::Move(data) = c { if let TurnChoice::Move(data) = c {
let mut change_priority = data.priority(); let mut change_priority = data.priority();
script_hook!(change_priority, c, c, &mut change_priority); script_hook!(change_priority, c, c, &mut change_priority);
if change_priority != data.priority() { if change_priority != data.priority() {
if let TurnChoice::Move(data) = choice_guard.deref_mut() { if let TurnChoice::Move(data) = choice.deref_mut() {
*data.priority_mut() = change_priority; *data.priority_mut() = change_priority;
} }
} }
} }
let mut speed = choice_guard.speed(); let mut speed = choice.speed();
let c = choice_guard.deref(); let c = choice.deref();
script_hook!(change_speed, c, c, &mut speed); script_hook!(change_speed, c, c, &mut speed);
*choice_guard.speed_mut() = speed; *choice.speed_mut() = speed;
choice_guard.set_random_value(self.random.get() as u32); choice.set_random_value(self.random.get() as u32);
choices.push(choice.as_ref().unwrap().clone()); choices.push(choice_opt.take().unwrap());
} }
side.reset_choices(); side.reset_choices();
} }
self.current_turn += 1; self.current_turn += 1;
choices.sort_unstable_by(|a, b| b.read().deref().cmp(a.read().deref())); choices.sort_unstable_by(|a, b| b.cmp(a));
self.current_turn_queue = Some(Arc::new(RwLock::new(ChoiceQueue::new(choices)))); self.current_turn_queue = Some(Arc::new(RwLock::new(ChoiceQueue::new(choices))));
{ {

View File

@ -16,7 +16,7 @@ pub struct BattleSide<'own, 'library> {
index: u8, index: u8,
pokemon_per_side: u8, pokemon_per_side: u8,
pokemon: Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>>, pokemon: Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>>,
choices: Vec<Option<Arc<RwLock<TurnChoice<'own, 'library>>>>>, choices: Vec<Option<TurnChoice<'own, 'library>>>,
fillable_slots: Vec<bool>, fillable_slots: Vec<bool>,
choices_set: u8, choices_set: u8,
battle: *mut Battle<'own, 'library>, battle: *mut Battle<'own, 'library>,
@ -65,10 +65,10 @@ impl<'own, 'library> BattleSide<'own, 'library> {
pub fn pokemon(&self) -> &Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>> { pub fn pokemon(&self) -> &Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>> {
&self.pokemon &self.pokemon
} }
pub fn choices(&self) -> &Vec<Option<Arc<RwLock<TurnChoice<'own, 'library>>>>> { pub fn choices(&self) -> &Vec<Option<TurnChoice<'own, 'library>>> {
&self.choices &self.choices
} }
pub fn choices_mut(&mut self) -> &mut Vec<Option<Arc<RwLock<TurnChoice<'own, 'library>>>>> { pub fn choices_mut(&mut self) -> &mut Vec<Option<TurnChoice<'own, 'library>>> {
&mut self.choices &mut self.choices
} }
@ -110,7 +110,7 @@ impl<'own, 'library> BattleSide<'own, 'library> {
for (index, pokemon_slot) in self.pokemon.iter().enumerate() { for (index, pokemon_slot) in self.pokemon.iter().enumerate() {
if let Some(pokemon) = pokemon_slot { if let Some(pokemon) = pokemon_slot {
if std::ptr::eq(pokemon.data_ptr(), choice.user().data_ptr()) { if std::ptr::eq(pokemon.data_ptr(), choice.user().data_ptr()) {
self.choices[index] = Some(Arc::new(RwLock::new(choice))); self.choices[index] = Some(choice);
self.choices_set += 1; self.choices_set += 1;
return; return;
} }

View File

@ -251,7 +251,7 @@ pub fn load_species(path: &String, library: &mut StaticData) {
let default_form_value = &forms["default"]; let default_form_value = &forms["default"];
let default_form = parse_form("default".into(), default_form_value, library); let default_form = parse_form("default".into(), default_form_value, library);
let mut species = Box::new(Species::new( let species = Box::new(Species::new(
id as u16, id as u16,
&name, &name,
gender_rate as f32, gender_rate as f32,