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

This commit is contained in:
Deukhoofd 2022-06-18 10:41:42 +02:00
parent 09c2533bf5
commit 59d7344729
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 19 additions and 16 deletions

View File

@ -3,23 +3,23 @@ use crate::dynamic_data::models::pokemon::Pokemon;
#[derive(Debug)] #[derive(Debug)]
pub struct ChoiceQueue<'battle, 'library> { pub struct ChoiceQueue<'battle, 'library> {
queue: Vec<TurnChoice<'battle, 'library>>, queue: Vec<Option<TurnChoice<'battle, 'library>>>,
current: usize, current: usize,
} }
impl<'battle, 'library> ChoiceQueue<'battle, 'library> { impl<'battle, 'library> ChoiceQueue<'battle, 'library> {
pub fn new(queue: Vec<TurnChoice<'battle, 'library>>) -> Self { pub fn new(queue: Vec<Option<TurnChoice<'battle, 'library>>>) -> Self {
Self { queue, current: 0 } Self { queue, current: 0 }
} }
pub fn dequeue<'b>(&'b mut self) -> &'b TurnChoice<'battle, 'library> { pub fn dequeue<'b>(&'b mut self) -> TurnChoice<'battle, 'library> {
let c = &self.queue[self.current]; let c = self.queue[self.current].take();
self.current += 1; self.current += 1;
c c.unwrap()
} }
pub fn peek(&mut self) -> &'battle TurnChoice { pub fn peek(&mut self) -> &'battle TurnChoice {
&self.queue[self.current] &self.queue[self.current].as_ref().unwrap()
} }
pub fn has_next(&self) -> bool { pub fn has_next(&self) -> bool {
@ -30,7 +30,7 @@ impl<'battle, 'library> ChoiceQueue<'battle, 'library> {
todo!() todo!()
} }
pub(crate) fn get_queue(&self) -> &Vec<TurnChoice<'battle, 'library>> { pub(crate) fn get_queue(&self) -> &[Option<TurnChoice<'battle, 'library>>] {
&self.queue &self.queue[self.current..self.queue.len()]
} }
} }

View File

@ -14,8 +14,7 @@ use std::sync::Arc;
impl<'own, 'library> Battle<'own, 'library> { impl<'own, 'library> Battle<'own, 'library> {
pub(crate) fn run_turn(&mut self) -> PkmnResult<()> { pub(crate) fn run_turn(&mut self) -> PkmnResult<()> {
let cq = self.current_turn_queue(); let mut choice_queue = self.current_turn_queue().as_ref().unwrap();
let mut choice_queue = cq.as_ref().unwrap().write();
// We are now at the very beginning of a turn. We have assigned speeds and priorities to all // We are now at the very beginning of a turn. We have assigned speeds and priorities to all
// choices, and put them in the correct order. // choices, and put them in the correct order.
@ -24,7 +23,7 @@ impl<'own, 'library> Battle<'own, 'library> {
// is primarily intended to be used to reset variables on a script (for example scripts that need // is primarily intended to be used to reset variables on a script (for example scripts that need
// 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().iter().flatten() {
script_hook!(on_before_turn, choice, choice); script_hook!(on_before_turn, choice, choice);
} }
@ -32,8 +31,9 @@ impl<'own, 'library> Battle<'own, 'library> {
// 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(); let choice = self.current_turn_queue_mut().as_mut().unwrap().dequeue();
self.execute_choice(&choice)?; self.execute_choice(&choice)?;
choice_queue = self.current_turn_queue().as_ref().unwrap();
} }
// 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

View File

@ -27,7 +27,7 @@ pub struct Battle<'own, 'library> {
pokemon_per_side: u8, pokemon_per_side: u8,
sides: Vec<BattleSide<'own, 'library>>, sides: Vec<BattleSide<'own, 'library>>,
random: BattleRandom, random: BattleRandom,
current_turn_queue: Option<Arc<RwLock<ChoiceQueue<'own, 'library>>>>, current_turn_queue: Option<ChoiceQueue<'own, 'library>>,
has_ended: bool, has_ended: bool,
result: BattleResult, result: BattleResult,
event_hook: EventHook, event_hook: EventHook,
@ -128,9 +128,12 @@ impl<'own, 'library> Battle<'own, 'library> {
pub fn last_turn_time(&self) -> chrono::Duration { pub fn last_turn_time(&self) -> chrono::Duration {
self.last_turn_time self.last_turn_time
} }
pub fn current_turn_queue(&self) -> &Option<Arc<RwLock<ChoiceQueue<'own, 'library>>>> { pub fn current_turn_queue(&self) -> &Option<ChoiceQueue<'own, 'library>> {
&self.current_turn_queue &self.current_turn_queue
} }
pub fn current_turn_queue_mut(&mut self) -> &mut Option<ChoiceQueue<'own, 'library>> {
&mut self.current_turn_queue
}
pub fn get_pokemon(&self, side: u8, index: u8) -> &Option<Arc<RwLock<Pokemon<'own, 'library>>>> { pub fn get_pokemon(&self, side: u8, index: u8) -> &Option<Arc<RwLock<Pokemon<'own, 'library>>>> {
let side = self.sides.get(side as usize); let side = self.sides.get(side as usize);
@ -259,14 +262,14 @@ impl<'own, 'library> Battle<'own, 'library> {
*choice.speed_mut() = speed; *choice.speed_mut() = speed;
choice.set_random_value(self.random.get() as u32); choice.set_random_value(self.random.get() as u32);
choices.push(choice_opt.take().unwrap()); choices.push(choice_opt.take());
} }
side.reset_choices(); side.reset_choices();
} }
self.current_turn += 1; self.current_turn += 1;
choices.sort_unstable_by(|a, b| b.cmp(a)); 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(ChoiceQueue::new(choices));
{ {
self.run_turn()?; self.run_turn()?;