PkmnLib_rs/src/dynamic_data/flow/choice_queue.rs

91 lines
3.6 KiB
Rust

use crate::dynamic_data::choices::TurnChoice;
use crate::dynamic_data::Pokemon;
/// 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.
#[derive(Debug)]
pub struct ChoiceQueue {
/// Our storage of turn choices. Starts out completely filled, then slowly empties as turns get
/// executed.
queue: Vec<Option<TurnChoice>>,
/// The current index of the turn we need to execute next.
current: usize,
}
impl ChoiceQueue {
/// Initializes a ChoiceQueue. We expect the given queue to already be sorted here.
pub(crate) fn new(queue: Vec<Option<TurnChoice>>) -> Self {
Self { queue, current: 0 }
}
/// 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.
pub fn dequeue(&mut self) -> TurnChoice {
let c = self.queue[self.current].take();
self.current += 1;
c.unwrap()
}
/// This reads what the next choice to execute will be, without modifying state.
pub fn peek(&self) -> &TurnChoice {
self.queue[self.current].as_ref().unwrap()
}
/// Check if we have any choices remaining.
pub fn has_next(&self) -> bool {
self.current < self.queue.len()
}
/// 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.
pub fn move_pokemon_choice_next(&mut self, pokemon: &Pokemon) -> bool {
let mut desired_index = None;
// Find the index for the choice we want to move up.
for index in self.current..self.queue.len() {
if let Some(choice) = &self.queue[index] {
if std::ptr::eq(choice.user().as_ref(), pokemon) {
desired_index = Some(index);
break;
}
}
}
// If we couldn't find a choice, we can't execute, return.
if desired_index.is_none() {
return false;
}
let desired_index = desired_index.unwrap();
// If the choice we want to move up is already the next choice, just return.
if desired_index == self.current {
return true;
}
// Take the choice we want to move forward out of it's place.
let choice = self.queue[desired_index].take().unwrap();
// Iterate backwards from the spot before the choice we want to move up, push them all back
// by 1 spot.
for index in (desired_index - 1)..self.current {
self.queue.swap(index, index + 1);
}
// Place the choice that needs to be next in the next to be executed position.
let _ = self.queue[self.current].insert(choice);
true
}
/// Internal helper function to be easily able to iterate over the yet to be executed choices.
pub(crate) fn get_queue(&self) -> &[Option<TurnChoice>] {
&self.queue[self.current..self.queue.len()]
}
}