diff --git a/src/dynamic_data/flow/choice_queue.rs b/src/dynamic_data/flow/choice_queue.rs index 5e33412..49bd474 100644 --- a/src/dynamic_data/flow/choice_queue.rs +++ b/src/dynamic_data/flow/choice_queue.rs @@ -3,23 +3,23 @@ use crate::dynamic_data::models::pokemon::Pokemon; #[derive(Debug)] pub struct ChoiceQueue<'battle, 'library> { - queue: Vec>, + queue: Vec>>, current: usize, } impl<'battle, 'library> ChoiceQueue<'battle, 'library> { - pub fn new(queue: Vec>) -> Self { + pub fn new(queue: Vec>>) -> Self { Self { queue, current: 0 } } - pub fn dequeue<'b>(&'b mut self) -> &'b TurnChoice<'battle, 'library> { - let c = &self.queue[self.current]; + pub fn dequeue<'b>(&'b mut self) -> TurnChoice<'battle, 'library> { + let c = self.queue[self.current].take(); self.current += 1; - c + c.unwrap() } pub fn peek(&mut self) -> &'battle TurnChoice { - &self.queue[self.current] + &self.queue[self.current].as_ref().unwrap() } pub fn has_next(&self) -> bool { @@ -30,7 +30,7 @@ impl<'battle, 'library> ChoiceQueue<'battle, 'library> { todo!() } - pub(crate) fn get_queue(&self) -> &Vec> { - &self.queue + pub(crate) fn get_queue(&self) -> &[Option>] { + &self.queue[self.current..self.queue.len()] } } diff --git a/src/dynamic_data/flow/turn_runner.rs b/src/dynamic_data/flow/turn_runner.rs index ceacfa9..f89146c 100644 --- a/src/dynamic_data/flow/turn_runner.rs +++ b/src/dynamic_data/flow/turn_runner.rs @@ -14,8 +14,7 @@ use std::sync::Arc; impl<'own, 'library> Battle<'own, 'library> { pub(crate) fn run_turn(&mut self) -> PkmnResult<()> { - let cq = self.current_turn_queue(); - let mut choice_queue = cq.as_ref().unwrap().write(); + let mut choice_queue = self.current_turn_queue().as_ref().unwrap(); // 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. @@ -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 // 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.) - for choice in choice_queue.get_queue() { + for choice in choice_queue.get_queue().iter().flatten() { 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 // continue running however. 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)?; + 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 diff --git a/src/dynamic_data/models/battle.rs b/src/dynamic_data/models/battle.rs index e93578c..70abf3a 100644 --- a/src/dynamic_data/models/battle.rs +++ b/src/dynamic_data/models/battle.rs @@ -27,7 +27,7 @@ pub struct Battle<'own, 'library> { pokemon_per_side: u8, sides: Vec>, random: BattleRandom, - current_turn_queue: Option>>>, + current_turn_queue: Option>, has_ended: bool, result: BattleResult, event_hook: EventHook, @@ -128,9 +128,12 @@ impl<'own, 'library> Battle<'own, 'library> { pub fn last_turn_time(&self) -> chrono::Duration { self.last_turn_time } - pub fn current_turn_queue(&self) -> &Option>>> { + pub fn current_turn_queue(&self) -> &Option> { &self.current_turn_queue } + pub fn current_turn_queue_mut(&mut self) -> &mut Option> { + &mut self.current_turn_queue + } pub fn get_pokemon(&self, side: u8, index: u8) -> &Option>>> { let side = self.sides.get(side as usize); @@ -259,14 +262,14 @@ impl<'own, 'library> Battle<'own, 'library> { *choice.speed_mut() = speed; choice.set_random_value(self.random.get() as u32); - choices.push(choice_opt.take().unwrap()); + choices.push(choice_opt.take()); } side.reset_choices(); } self.current_turn += 1; 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()?;