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

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

View File

@@ -3,23 +3,23 @@ use crate::dynamic_data::models::pokemon::Pokemon;
#[derive(Debug)]
pub struct ChoiceQueue<'battle, 'library> {
queue: Vec<TurnChoice<'battle, 'library>>,
queue: Vec<Option<TurnChoice<'battle, 'library>>>,
current: usize,
}
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 }
}
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<TurnChoice<'battle, 'library>> {
&self.queue
pub(crate) fn get_queue(&self) -> &[Option<TurnChoice<'battle, 'library>>] {
&self.queue[self.current..self.queue.len()]
}
}

View File

@@ -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