PkmnLib_rs/src/dynamic_data/flow/choice_queue.rs

402 lines
14 KiB
Rust
Executable File

use crate::dynamic_data::choices::TurnChoice;
use crate::dynamic_data::script_handling::ScriptSource;
use crate::dynamic_data::Pokemon;
use crate::{script_hook, PkmnError, ValueIdentifiable, ValueIdentifier, VecExt};
use anyhow::Result;
use anyhow_ext::anyhow;
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)]
pub struct ChoiceQueue {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// 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, and sort the choices.
pub(crate) fn new(mut queue: Vec<Option<TurnChoice>>) -> Self {
queue.sort_unstable_by(|a, b| b.cmp(a));
Self {
identifier: Default::default(),
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) -> Result<Option<TurnChoice>> {
let mut write_lock = self.queue.write();
if self.current >= write_lock.len() {
return Ok(None);
}
let c = write_lock
.get_mut(self.current)
.ok_or(anyhow!("Unable to get current turn choice"))?
.take();
self.current += 1;
Ok(c)
}
/// This reads what the next choice to execute will be, without modifying state.
pub fn peek(&self) -> Result<Option<MappedRwLockReadGuard<'_, RawRwLock, TurnChoice>>> {
let read_lock = self.queue.read();
if self.current >= read_lock.len() {
Ok(None)
} else {
let v = RwLockReadGuard::try_map(read_lock, |a| match a.get(self.current) {
Some(Some(v)) => Some(v),
_ => None,
});
match v {
Ok(v) => Ok(Some(v)),
Err(_) => Err(anyhow!("Could not map choice")),
}
}
}
/// 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) -> Result<()> {
let len = self.queue.read().len();
let mut write_lock = self.queue.write();
for index in self.current..len {
let choice = &mut write_lock.get_mut_res(index)?;
if let Some(choice) = choice {
let mut speed = choice.user().boosted_stats().speed();
script_hook!(change_speed, (*choice), choice, &mut speed);
*choice.speed_mut() = speed;
}
}
write_lock
.get_mut(self.current..len)
.ok_or(PkmnError::IndexOutOfBounds {
index: self.current,
len,
})?
.sort_unstable_by(|a, b| b.cmp(a));
Ok(())
}
/// 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) -> Result<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(Some(choice)) = &queue_lock.get(index) {
if pokemon.value_identifier() == choice.user().value_identifier() {
desired_index = Some(index);
break;
}
}
}
let result = match desired_index {
Some(desired_index) => {
// If the choice we want to move up is already the next choice, just return.
if desired_index == self.current {
return Ok(true);
}
// Take the choice we want to move forward out of it's place.
let choice = queue_lock
.get_mut_res(desired_index)?
.take()
.ok_or(anyhow!("Choice was already taken"))?;
// Iterate backwards from the spot before the choice we want to move up, push them all back
// by 1 spot.
for index in (self.current..desired_index).rev() {
queue_lock.swap(index, index + 1);
}
// Place the choice that needs to be next in the next to be executed position.
let _ = queue_lock.get_mut_res(self.current)?.insert(choice);
true
}
None => false,
};
Ok(result)
}
/// Internal helper function to be easily able to iterate over the yet to be executed choices.
pub(crate) fn get_queue(&self) -> Result<MappedRwLockReadGuard<'_, RawRwLock, [Option<TurnChoice>]>> {
let read_lock = self.queue.read();
match RwLockReadGuard::try_map(read_lock, |a| a.get(self.current..self.queue.read().len())) {
Ok(v) => Ok(v),
Err(_) => Err(PkmnError::IndexOutOfBounds {
index: self.current,
len: self.queue.read().len(),
}
.into()),
}
}
}
impl ValueIdentifiable for ChoiceQueue {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use crate::defines::LevelInt;
use crate::dynamic_data::{DynamicLibrary, PassChoice};
use crate::static_data::{AbilityIndex, Gender};
use std::sync::Arc;
#[test]
fn create_empty_queue() {
let queue = ChoiceQueue::new(Vec::new());
assert!(!queue.has_next());
assert!(queue.peek().unwrap().is_none());
}
#[test]
fn dequeue_from_empty_queue() {
let mut queue = ChoiceQueue::new(Vec::new());
assert!(queue.dequeue().unwrap().is_none());
}
fn get_user(level: LevelInt) -> Pokemon {
let lib = Arc::new(crate::dynamic_data::libraries::dynamic_library::test::build());
let species = lib.static_data().species().get(&"foo".into()).unwrap();
let form = species.get_form(&"default".into()).unwrap();
Pokemon::new(
lib,
species,
&form,
AbilityIndex {
hidden: false,
index: 0,
},
level,
0,
Gender::Male,
0,
&"test_nature".into(),
)
.unwrap()
}
#[test]
fn create_queue_with_single_item() {
let user = Arc::new(get_user(10));
let queue = ChoiceQueue::new(vec![Some(TurnChoice::Pass(PassChoice::new(user)))]);
assert!(queue.has_next());
assert!(queue.peek().unwrap().is_some());
assert_eq!(7, queue.peek().unwrap().unwrap().speed());
}
#[test]
fn dequeue_from_queue_with_single_item() {
let user = Arc::new(get_user(10));
let mut queue = ChoiceQueue::new(vec![Some(TurnChoice::Pass(PassChoice::new(user)))]);
assert!(queue.has_next());
assert_eq!(7, queue.dequeue().unwrap().unwrap().speed());
assert!(!queue.has_next());
assert!(queue.peek().unwrap().is_none());
}
#[test]
fn create_queue_with_two_items_with_equal_order() {
let user1 = Arc::new(get_user(10));
let user2 = Arc::new(get_user(10));
let queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(user1))),
Some(TurnChoice::Pass(PassChoice::new(user2))),
]);
assert!(queue.has_next());
assert!(queue.peek().unwrap().is_some());
assert_eq!(7, queue.peek().unwrap().unwrap().speed());
}
#[test]
fn create_queue_with_two_items_get_queue() {
let user1 = Arc::new(get_user(10));
let user2 = Arc::new(get_user(5));
let queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(user1.clone()))),
Some(TurnChoice::Pass(PassChoice::new(user2))),
]);
let inner_queue = queue.get_queue().unwrap();
assert_eq!(
inner_queue[0].as_ref().unwrap().user().value_identifier(),
user1.value_identifier()
);
}
#[test]
fn create_queue_with_two_items_in_wrong_order_sorts_correctly() {
let user1 = Arc::new(get_user(5));
let user2 = Arc::new(get_user(100));
let mut queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(user1))),
Some(TurnChoice::Pass(PassChoice::new(user2))),
]);
assert_eq!(25, queue.dequeue().unwrap().unwrap().speed());
assert_eq!(6, queue.dequeue().unwrap().unwrap().speed());
}
#[test]
fn resort_with_two_choices() {
let user1 = Arc::new(get_user(50));
let user2 = Arc::new(get_user(1));
let mut queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(user1.clone()))),
Some(TurnChoice::Pass(PassChoice::new(user2.clone()))),
]);
user2.change_level_by(60).unwrap();
assert_eq!(
user1.value_identifier(),
queue.peek().unwrap().unwrap().user().value_identifier()
);
queue.resort().unwrap();
assert_eq!(
user2.value_identifier(),
queue.peek().unwrap().unwrap().user().value_identifier()
);
}
#[test]
fn move_pokemon_choice_first_with_two_choices() {
let user1 = Arc::new(get_user(100));
let user2 = Arc::new(get_user(1));
let mut queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(user1.clone()))),
Some(TurnChoice::Pass(PassChoice::new(user2.clone()))),
]);
assert_eq!(
user1.value_identifier(),
queue.peek().unwrap().unwrap().user().value_identifier()
);
assert!(queue.move_pokemon_choice_next(user2.as_ref()).unwrap());
assert_eq!(
user2.value_identifier(),
queue.dequeue().unwrap().unwrap().user().value_identifier()
);
assert_eq!(
user1.value_identifier(),
queue.dequeue().unwrap().unwrap().user().value_identifier()
);
}
#[test]
fn move_pokemon_choice_first_when_choice_has_already_been() {
let user1 = Arc::new(get_user(10));
let user2 = Arc::new(get_user(100));
let mut queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(user1.clone()))),
Some(TurnChoice::Pass(PassChoice::new(user2.clone()))),
]);
assert_eq!(
user2.value_identifier(),
queue.dequeue().unwrap().unwrap().user().value_identifier()
);
assert!(!queue.move_pokemon_choice_next(user2.as_ref()).unwrap());
assert_eq!(
user1.value_identifier(),
queue.dequeue().unwrap().unwrap().user().value_identifier()
);
assert!(queue.peek().unwrap().is_none())
}
#[test]
fn move_pokemon_choice_first_when_choice_is_next() {
let user1 = Arc::new(get_user(100));
let user2 = Arc::new(get_user(10));
let queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(user1.clone()))),
Some(TurnChoice::Pass(PassChoice::new(user2))),
]);
assert_eq!(
user1.value_identifier(),
queue.peek().unwrap().unwrap().user().value_identifier()
);
assert!(queue.move_pokemon_choice_next(user1.as_ref()).unwrap());
assert_eq!(
user1.value_identifier(),
queue.peek().unwrap().unwrap().user().value_identifier()
);
}
#[test]
fn move_pokemon_choice_first_with_seven_choices() {
let users = [
Arc::new(get_user(100)),
Arc::new(get_user(90)),
Arc::new(get_user(80)),
Arc::new(get_user(70)),
Arc::new(get_user(60)),
Arc::new(get_user(50)),
Arc::new(get_user(40)),
];
let mut queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(users[0].clone()))),
Some(TurnChoice::Pass(PassChoice::new(users[1].clone()))),
Some(TurnChoice::Pass(PassChoice::new(users[2].clone()))),
Some(TurnChoice::Pass(PassChoice::new(users[3].clone()))),
Some(TurnChoice::Pass(PassChoice::new(users[4].clone()))),
Some(TurnChoice::Pass(PassChoice::new(users[5].clone()))),
Some(TurnChoice::Pass(PassChoice::new(users[6].clone()))),
]);
assert_eq!(
users[0].value_identifier(),
queue.peek().unwrap().unwrap().user().value_identifier()
);
assert!(queue.move_pokemon_choice_next(users[4].as_ref()).unwrap());
assert_eq!(
users[4].value_identifier(),
queue.dequeue().unwrap().unwrap().user().value_identifier()
);
for index in 0..4 {
assert_eq!(
users[index].value_identifier(),
queue.dequeue().unwrap().unwrap().user().value_identifier()
);
}
for index in 5..7 {
assert_eq!(
users[index].value_identifier(),
queue.dequeue().unwrap().unwrap().user().value_identifier()
);
}
}
}