use crate::dynamic_data::choices::TurnChoice; use crate::dynamic_data::script_handling::ScriptSource; use crate::dynamic_data::Pokemon; use crate::{script_hook, ValueIdentifiable}; 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>>, /// 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>) -> Self { queue.sort_unstable_by(|a, b| b.cmp(a)); 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) -> Option { let mut write_lock = self.queue.write(); if self.current >= write_lock.len() { return None; } let c = write_lock[self.current].take(); self.current += 1; c } /// This reads what the next choice to execute will be, without modifying state. pub fn peek(&self) -> Option> { let read_lock = self.queue.read(); if self.current >= read_lock.len() { None } else { Some(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(); let mut write_lock = self.queue.write(); for index in self.current..len { let choice = &mut write_lock[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[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 pokemon.value_identifier() == choice.user().value_identifier() { 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 (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[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]> { let read_lock = self.queue.read(); RwLockReadGuard::map(read_lock, |a| &a[self.current..self.queue.read().len()]) } } #[cfg(test)] mod tests { use super::*; use crate::defines::LevelInt; use crate::dynamic_data::PassChoice; use crate::static_data::{AbilityIndex, DataLibrary, Gender}; use std::sync::Arc; #[test] fn create_empty_queue() { let queue = ChoiceQueue::new(Vec::new()); assert!(!queue.has_next()); assert!(queue.peek().is_none()); } #[test] fn dequeue_from_empty_queue() { let mut queue = ChoiceQueue::new(Vec::new()); assert!(queue.dequeue().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().clone(); 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(), ) } #[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().is_some()); assert_eq!(7, queue.peek().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().speed()); assert!(!queue.has_next()); assert!(queue.peek().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().is_some()); assert_eq!(7, queue.peek().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(); 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().speed()); assert_eq!(6, queue.dequeue().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); assert_eq!( user1.value_identifier(), queue.peek().unwrap().user().value_identifier() ); queue.resort(); assert_eq!( user2.value_identifier(), queue.peek().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().user().value_identifier() ); assert!(queue.move_pokemon_choice_next(user2.as_ref())); assert_eq!( user2.value_identifier(), queue.dequeue().unwrap().user().value_identifier() ); assert_eq!( user1.value_identifier(), queue.dequeue().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().user().value_identifier() ); assert!(!queue.move_pokemon_choice_next(user2.as_ref())); assert_eq!( user1.value_identifier(), queue.dequeue().unwrap().user().value_identifier() ); assert!(queue.peek().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.clone()))), ]); assert_eq!( user1.value_identifier(), queue.peek().unwrap().user().value_identifier() ); assert!(queue.move_pokemon_choice_next(user1.as_ref())); assert_eq!( user1.value_identifier(), queue.peek().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().user().value_identifier() ); assert!(queue.move_pokemon_choice_next(users[4].as_ref())); assert_eq!( users[4].value_identifier(), queue.dequeue().unwrap().user().value_identifier() ); for index in 0..4 { assert_eq!( users[index].value_identifier(), queue.dequeue().unwrap().user().value_identifier() ); } for index in 5..7 { assert_eq!( users[index].value_identifier(), queue.dequeue().unwrap().user().value_identifier() ); } } }