2022-06-16 15:59:33 +00:00
|
|
|
use crate::dynamic_data::choices::TurnChoice;
|
2022-06-19 19:34:08 +00:00
|
|
|
use crate::dynamic_data::Pokemon;
|
2022-06-11 15:22:46 +00:00
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// The ChoiceQueue is used to run choices one by one.
|
|
|
|
///
|
|
|
|
/// It functions internally by holding a vector of choices, and passing ownership of the turn choice
|
|
|
|
/// to the turn executor one by one, replacing it with empty spots at the start. It holds several
|
|
|
|
/// helper functions to change the turn order while doing the execution. This is needed, as several
|
|
|
|
/// moves in Pokemon actively mess with this order.
|
2022-06-03 14:35:18 +00:00
|
|
|
#[derive(Debug)]
|
2022-06-16 15:59:33 +00:00
|
|
|
pub struct ChoiceQueue<'battle, 'library> {
|
2022-06-19 19:34:08 +00:00
|
|
|
/// Our storage of turn choices. Starts out completely filled, then slowly empties as turns get
|
|
|
|
/// executed.
|
2022-06-18 08:41:42 +00:00
|
|
|
queue: Vec<Option<TurnChoice<'battle, 'library>>>,
|
2022-06-19 19:34:08 +00:00
|
|
|
/// The current index of the turn we need to execute next.
|
2022-06-11 15:22:46 +00:00
|
|
|
current: usize,
|
|
|
|
}
|
|
|
|
|
2022-06-16 15:59:33 +00:00
|
|
|
impl<'battle, 'library> ChoiceQueue<'battle, 'library> {
|
2022-06-19 19:34:08 +00:00
|
|
|
/// Initializes a ChoiceQueue. We expect the given queue to already be sorted here.
|
|
|
|
pub(crate) fn new(queue: Vec<Option<TurnChoice<'battle, 'library>>>) -> Self {
|
2022-06-11 15:22:46 +00:00
|
|
|
Self { queue, current: 0 }
|
|
|
|
}
|
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// Dequeues the next turn choice to be executed. This gives ownership to the callee, and replaces
|
|
|
|
/// our own reference to the turn choice with an empty spot. It also increments the current position
|
|
|
|
/// by one.
|
2022-06-18 08:41:42 +00:00
|
|
|
pub fn dequeue<'b>(&'b mut self) -> TurnChoice<'battle, 'library> {
|
|
|
|
let c = self.queue[self.current].take();
|
2022-06-11 15:22:46 +00:00
|
|
|
self.current += 1;
|
2022-06-18 08:41:42 +00:00
|
|
|
c.unwrap()
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// This reads what the next choice to execute will be, without modifying state.
|
|
|
|
pub fn peek(&self) -> &'battle TurnChoice {
|
2022-06-18 13:52:39 +00:00
|
|
|
self.queue[self.current].as_ref().unwrap()
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// Check if we have any choices remaining.
|
2022-06-11 15:22:46 +00:00
|
|
|
pub fn has_next(&self) -> bool {
|
|
|
|
self.current < self.queue.len()
|
|
|
|
}
|
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// This resorts the yet to be executed choices. This can be useful for dealing with situations
|
|
|
|
/// such as Pokemon changing forms just after the very start of a turn, when turn order has
|
|
|
|
/// technically already been decided.
|
|
|
|
pub fn resort(&mut self) {
|
|
|
|
let len = self.queue.len();
|
|
|
|
self.queue[self.current..len].sort_unstable_by(|a, b| b.cmp(a));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This moves the choice of a specific Pokemon up to the next choice to be executed.
|
2022-06-11 15:22:46 +00:00
|
|
|
pub fn move_pokemon_choice_next(&mut self, _pokemon: &Pokemon) {
|
|
|
|
todo!()
|
|
|
|
}
|
2022-06-16 15:59:33 +00:00
|
|
|
|
2022-06-19 19:34:08 +00:00
|
|
|
/// Internal helper function to be easily able to iterate over the yet to be executed choices.
|
2022-06-18 08:41:42 +00:00
|
|
|
pub(crate) fn get_queue(&self) -> &[Option<TurnChoice<'battle, 'library>>] {
|
|
|
|
&self.queue[self.current..self.queue.len()]
|
2022-06-16 15:59:33 +00:00
|
|
|
}
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|