PkmnLib_rs/src/dynamic_data/flow/choice_queue.rs

100 lines
4.2 KiB
Rust
Executable File

use crate::dynamic_data::choices::TurnChoice;
use crate::dynamic_data::Pokemon;
use parking_lot::lock_api::MappedRwLockReadGuard;
use parking_lot::{RawRwLock, RwLock, RwLockReadGuard};
/// 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)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub struct ChoiceQueue {
/// Our storage of turn choices. Starts out completely filled, then slowly empties as turns get
/// executed.
queue: RwLock<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: RwLock::new(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.write()[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) -> MappedRwLockReadGuard<'_, RawRwLock, TurnChoice> {
let read_lock = self.queue.read();
RwLockReadGuard::map(read_lock, |a| a[self.current].as_ref().unwrap())
}
/// Check if we have any choices remaining.
pub fn has_next(&self) -> bool {
self.current < self.queue.read().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.read().len();
self.queue.write()[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(&self, pokemon: &Pokemon) -> bool {
let mut queue_lock = self.queue.write();
let mut desired_index = None;
// Find the index for the choice we want to move up.
for index in self.current..queue_lock.len() {
if let Some(choice) = &queue_lock[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 = queue_lock[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 {
queue_lock.swap(index, index + 1);
}
// Place the choice that needs to be next in the next to be executed position.
let _ = queue_lock[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) -> MappedRwLockReadGuard<'_, RawRwLock, [Option<TurnChoice>]> {
let read_lock = self.queue.read();
RwLockReadGuard::map(read_lock, |a| &a[self.current..self.queue.read().len()])
}
}